/[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.10 by wakaba, Sat Jun 23 03:30:06 2007 UTC revision 1.172 by wakaba, Sun Sep 14 03:07:58 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## This is an early version of an HTML parser.  ## ISSUE:
7    ## var doc = implementation.createDocument (null, null, null);
8    ## doc.write ('');
9    ## alert (doc.compatMode);
10    
11    require IO::Handle;
12    
13    my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15    my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    
20    sub A_EL () { 0b1 }
21    sub ADDRESS_EL () { 0b10 }
22    sub BODY_EL () { 0b100 }
23    sub BUTTON_EL () { 0b1000 }
24    sub CAPTION_EL () { 0b10000 }
25    sub DD_EL () { 0b100000 }
26    sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233  my $permitted_slash_tag_name = {  my $svg_attr_name = {
234    base => 1,    attributename => 'attributeName',
235    link => 1,    attributetype => 'attributeType',
236    meta => 1,    basefrequency => 'baseFrequency',
237    hr => 1,    baseprofile => 'baseProfile',
238    br => 1,    calcmode => 'calcMode',
239    img=> 1,    clippathunits => 'clipPathUnits',
240    embed => 1,    contentscripttype => 'contentScriptType',
241    param => 1,    contentstyletype => 'contentStyleType',
242    area => 1,    diffuseconstant => 'diffuseConstant',
243    col => 1,    edgemode => 'edgeMode',
244    input => 1,    externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296  };  };
297    
298  my $entity_char = {  my $foreign_attr_xname = {
299    AElig => "\x{00C6}",    'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300    Aacute => "\x{00C1}",    'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301    Acirc => "\x{00C2}",    'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302    Agrave => "\x{00C0}",    'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303    Alpha => "\x{0391}",    'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304    Aring => "\x{00C5}",    'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305    Atilde => "\x{00C3}",    'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306    Auml => "\x{00C4}",    'xml:base' => [$XML_NS, ['xml', 'base']],
307    Beta => "\x{0392}",    'xml:lang' => [$XML_NS, ['xml', 'lang']],
308    Ccedil => "\x{00C7}",    'xml:space' => [$XML_NS, ['xml', 'space']],
309    Chi => "\x{03A7}",    'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310    Dagger => "\x{2021}",    'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    Delta => "\x{0394}",  };
312    ETH => "\x{00D0}",  
313    Eacute => "\x{00C9}",  ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
   Ecirc => "\x{00CA}",  
   Egrave => "\x{00C8}",  
   Epsilon => "\x{0395}",  
   Eta => "\x{0397}",  
   Euml => "\x{00CB}",  
   Gamma => "\x{0393}",  
   Iacute => "\x{00CD}",  
   Icirc => "\x{00CE}",  
   Igrave => "\x{00CC}",  
   Iota => "\x{0399}",  
   Iuml => "\x{00CF}",  
   Kappa => "\x{039A}",  
   Lambda => "\x{039B}",  
   Mu => "\x{039C}",  
   Ntilde => "\x{00D1}",  
   Nu => "\x{039D}",  
   OElig => "\x{0152}",  
   Oacute => "\x{00D3}",  
   Ocirc => "\x{00D4}",  
   Ograve => "\x{00D2}",  
   Omega => "\x{03A9}",  
   Omicron => "\x{039F}",  
   Oslash => "\x{00D8}",  
   Otilde => "\x{00D5}",  
   Ouml => "\x{00D6}",  
   Phi => "\x{03A6}",  
   Pi => "\x{03A0}",  
   Prime => "\x{2033}",  
   Psi => "\x{03A8}",  
   Rho => "\x{03A1}",  
   Scaron => "\x{0160}",  
   Sigma => "\x{03A3}",  
   THORN => "\x{00DE}",  
   Tau => "\x{03A4}",  
   Theta => "\x{0398}",  
   Uacute => "\x{00DA}",  
   Ucirc => "\x{00DB}",  
   Ugrave => "\x{00D9}",  
   Upsilon => "\x{03A5}",  
   Uuml => "\x{00DC}",  
   Xi => "\x{039E}",  
   Yacute => "\x{00DD}",  
   Yuml => "\x{0178}",  
   Zeta => "\x{0396}",  
   aacute => "\x{00E1}",  
   acirc => "\x{00E2}",  
   acute => "\x{00B4}",  
   aelig => "\x{00E6}",  
   agrave => "\x{00E0}",  
   alefsym => "\x{2135}",  
   alpha => "\x{03B1}",  
   amp => "\x{0026}",  
   AMP => "\x{0026}",  
   and => "\x{2227}",  
   ang => "\x{2220}",  
   apos => "\x{0027}",  
   aring => "\x{00E5}",  
   asymp => "\x{2248}",  
   atilde => "\x{00E3}",  
   auml => "\x{00E4}",  
   bdquo => "\x{201E}",  
   beta => "\x{03B2}",  
   brvbar => "\x{00A6}",  
   bull => "\x{2022}",  
   cap => "\x{2229}",  
   ccedil => "\x{00E7}",  
   cedil => "\x{00B8}",  
   cent => "\x{00A2}",  
   chi => "\x{03C7}",  
   circ => "\x{02C6}",  
   clubs => "\x{2663}",  
   cong => "\x{2245}",  
   copy => "\x{00A9}",  
   COPY => "\x{00A9}",  
   crarr => "\x{21B5}",  
   cup => "\x{222A}",  
   curren => "\x{00A4}",  
   dArr => "\x{21D3}",  
   dagger => "\x{2020}",  
   darr => "\x{2193}",  
   deg => "\x{00B0}",  
   delta => "\x{03B4}",  
   diams => "\x{2666}",  
   divide => "\x{00F7}",  
   eacute => "\x{00E9}",  
   ecirc => "\x{00EA}",  
   egrave => "\x{00E8}",  
   empty => "\x{2205}",  
   emsp => "\x{2003}",  
   ensp => "\x{2002}",  
   epsilon => "\x{03B5}",  
   equiv => "\x{2261}",  
   eta => "\x{03B7}",  
   eth => "\x{00F0}",  
   euml => "\x{00EB}",  
   euro => "\x{20AC}",  
   exist => "\x{2203}",  
   fnof => "\x{0192}",  
   forall => "\x{2200}",  
   frac12 => "\x{00BD}",  
   frac14 => "\x{00BC}",  
   frac34 => "\x{00BE}",  
   frasl => "\x{2044}",  
   gamma => "\x{03B3}",  
   ge => "\x{2265}",  
   gt => "\x{003E}",  
   GT => "\x{003E}",  
   hArr => "\x{21D4}",  
   harr => "\x{2194}",  
   hearts => "\x{2665}",  
   hellip => "\x{2026}",  
   iacute => "\x{00ED}",  
   icirc => "\x{00EE}",  
   iexcl => "\x{00A1}",  
   igrave => "\x{00EC}",  
   image => "\x{2111}",  
   infin => "\x{221E}",  
   int => "\x{222B}",  
   iota => "\x{03B9}",  
   iquest => "\x{00BF}",  
   isin => "\x{2208}",  
   iuml => "\x{00EF}",  
   kappa => "\x{03BA}",  
   lArr => "\x{21D0}",  
   lambda => "\x{03BB}",  
   lang => "\x{2329}",  
   laquo => "\x{00AB}",  
   larr => "\x{2190}",  
   lceil => "\x{2308}",  
   ldquo => "\x{201C}",  
   le => "\x{2264}",  
   lfloor => "\x{230A}",  
   lowast => "\x{2217}",  
   loz => "\x{25CA}",  
   lrm => "\x{200E}",  
   lsaquo => "\x{2039}",  
   lsquo => "\x{2018}",  
   lt => "\x{003C}",  
   LT => "\x{003C}",  
   macr => "\x{00AF}",  
   mdash => "\x{2014}",  
   micro => "\x{00B5}",  
   middot => "\x{00B7}",  
   minus => "\x{2212}",  
   mu => "\x{03BC}",  
   nabla => "\x{2207}",  
   nbsp => "\x{00A0}",  
   ndash => "\x{2013}",  
   ne => "\x{2260}",  
   ni => "\x{220B}",  
   not => "\x{00AC}",  
   notin => "\x{2209}",  
   nsub => "\x{2284}",  
   ntilde => "\x{00F1}",  
   nu => "\x{03BD}",  
   oacute => "\x{00F3}",  
   ocirc => "\x{00F4}",  
   oelig => "\x{0153}",  
   ograve => "\x{00F2}",  
   oline => "\x{203E}",  
   omega => "\x{03C9}",  
   omicron => "\x{03BF}",  
   oplus => "\x{2295}",  
   or => "\x{2228}",  
   ordf => "\x{00AA}",  
   ordm => "\x{00BA}",  
   oslash => "\x{00F8}",  
   otilde => "\x{00F5}",  
   otimes => "\x{2297}",  
   ouml => "\x{00F6}",  
   para => "\x{00B6}",  
   part => "\x{2202}",  
   permil => "\x{2030}",  
   perp => "\x{22A5}",  
   phi => "\x{03C6}",  
   pi => "\x{03C0}",  
   piv => "\x{03D6}",  
   plusmn => "\x{00B1}",  
   pound => "\x{00A3}",  
   prime => "\x{2032}",  
   prod => "\x{220F}",  
   prop => "\x{221D}",  
   psi => "\x{03C8}",  
   quot => "\x{0022}",  
   QUOT => "\x{0022}",  
   rArr => "\x{21D2}",  
   radic => "\x{221A}",  
   rang => "\x{232A}",  
   raquo => "\x{00BB}",  
   rarr => "\x{2192}",  
   rceil => "\x{2309}",  
   rdquo => "\x{201D}",  
   real => "\x{211C}",  
   reg => "\x{00AE}",  
   REG => "\x{00AE}",  
   rfloor => "\x{230B}",  
   rho => "\x{03C1}",  
   rlm => "\x{200F}",  
   rsaquo => "\x{203A}",  
   rsquo => "\x{2019}",  
   sbquo => "\x{201A}",  
   scaron => "\x{0161}",  
   sdot => "\x{22C5}",  
   sect => "\x{00A7}",  
   shy => "\x{00AD}",  
   sigma => "\x{03C3}",  
   sigmaf => "\x{03C2}",  
   sim => "\x{223C}",  
   spades => "\x{2660}",  
   sub => "\x{2282}",  
   sube => "\x{2286}",  
   sum => "\x{2211}",  
   sup => "\x{2283}",  
   sup1 => "\x{00B9}",  
   sup2 => "\x{00B2}",  
   sup3 => "\x{00B3}",  
   supe => "\x{2287}",  
   szlig => "\x{00DF}",  
   tau => "\x{03C4}",  
   there4 => "\x{2234}",  
   theta => "\x{03B8}",  
   thetasym => "\x{03D1}",  
   thinsp => "\x{2009}",  
   thorn => "\x{00FE}",  
   tilde => "\x{02DC}",  
   times => "\x{00D7}",  
   trade => "\x{2122}",  
   uArr => "\x{21D1}",  
   uacute => "\x{00FA}",  
   uarr => "\x{2191}",  
   ucirc => "\x{00FB}",  
   ugrave => "\x{00F9}",  
   uml => "\x{00A8}",  
   upsih => "\x{03D2}",  
   upsilon => "\x{03C5}",  
   uuml => "\x{00FC}",  
   weierp => "\x{2118}",  
   xi => "\x{03BE}",  
   yacute => "\x{00FD}",  
   yen => "\x{00A5}",  
   yuml => "\x{00FF}",  
   zeta => "\x{03B6}",  
   zwj => "\x{200D}",  
   zwnj => "\x{200C}",  
 }; # $entity_char  
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
# Line 315  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
350  my $special_category = {  sub parse_byte_string ($$$$;$) {
351    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = shift;
352    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
353    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  } # parse_byte_string
356    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
357    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  sub parse_byte_stream ($$$$;$$) {
358    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    # my ($self, $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      my $s = ref $_[0] ? $_[0] : \($_[0]);
622      require Whatpm::Charset::DecodeHandle;
623      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($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    ## TODO: support for abort/streaming
673          my $next = $input->getc;
674          if (defined $next and $next ne "\x0A") {
675            $self->{next_next_char} = $next;
676          }
677          $self->{next_char} = 0x000A; # LF # MUST
678          $self->{line}++;
679          $self->{column} = 0;
680        } elsif ($self->{next_char} > 0x10FFFF) {
681          !!!cp ('j3');
682          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
683        } elsif ($self->{next_char} == 0x0000) { # NULL
684          !!!cp ('j4');
685          !!!parse-error (type => 'NULL');
686          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
687        } elsif ($self->{next_char} <= 0x0008 or
688                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
689                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
690                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
691                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
692    ## ISSUE: U+FDE0-U+FDEF are not excluded
693                 {
694                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
695                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
696                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
697                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
698                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
699                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
700                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
701                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
702                  0x10FFFE => 1, 0x10FFFF => 1,
703                 }->{$self->{next_char}}) {
704          !!!cp ('j5');
705          if ($self->{next_char} < 0x10000) {
706            !!!parse-error (type => 'control char',
707                            text => (sprintf 'U+%04X', $self->{next_char}));
708        } else {        } else {
709          my $next_char = ord substr $$s, $i++, 1;          !!!parse-error (type => 'control char',
710          if ($next_char == 0x000A) { # LF                          text => (sprintf 'U-%08X', $self->{next_char}));
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
711        }        }
       $self->{next_input_character} = 0x000A; # LF # MUST  
       $line++;  
       $column = 0;  
     } elsif ($self->{next_input_character} > 0x10FFFF) {  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
     } elsif ($self->{next_input_character} == 0x0000) { # NULL  
       !!!parse-error (type => 'NULL');  
 ## TODO: test  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
712      }      }
713    };    };
714      $self->{prev_char} = [-1, -1, -1];
715      $self->{next_char} = -1;
716    
717      $self->{read_until} = sub {
718        #my ($scalar, $specials_range, $offset) = @_;
719        my $specials_range = $_[1];
720        return 0 if defined $self->{next_next_char};
721        my $count = $input->manakai_read_until
722           ($_[0],
723            qr/(?![$specials_range\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
724            $_[2]);
725        if ($count) {
726          $self->{column} += $count;
727          $self->{column_prev} += $count;
728          $self->{prev_char} = [-1, -1, -1];
729          $self->{next_char} = -1;
730        }
731        return $count;
732      }; # $self->{read_until}
733    
734    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
735      my (%opt) = @_;      my (%opt) = @_;
736      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
737        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
738        warn "Parse error ($opt{type}) at line $line column $column\n";
739    };    };
740    $self->{parse_error} = sub {    $self->{parse_error} = sub {
741      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
742    };    };
743    
744    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 392  sub parse_string ($$$;$) { Line 746  sub parse_string ($$$;$) {
746    $self->_construct_tree;    $self->_construct_tree;
747    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
748    
749      delete $self->{parse_error}; # remove loop
750    
751    return $self->{document};    return $self->{document};
752  } # parse_string  } # parse_char_stream
753    
754  sub new ($) {  sub new ($) {
755    my $class = shift;    my $class = shift;
756    my $self = bless {}, $class;    my $self = bless {
757    $self->{set_next_input_character} = sub {      level => {must => 'm',
758      $self->{next_input_character} = -1;                should => 's',
759                  warn => 'w',
760                  info => 'i',
761                  uncertain => 'u'},
762      }, $class;
763      $self->{set_next_char} = sub {
764        $self->{next_char} = -1;
765    };    };
766    $self->{parse_error} = sub {    $self->{parse_error} = sub {
767      #      #
768    };    };
769      $self->{change_encoding} = sub {
770        # if ($_[0] is a supported encoding) {
771        #   run "change the encoding" algorithm;
772        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
773        # }
774      };
775      $self->{application_cache_selection} = sub {
776        #
777      };
778    return $self;    return $self;
779  } # new  } # new
780    
781    sub CM_ENTITY () { 0b001 } # & markup in data
782    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
783    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
784    
785    sub PLAINTEXT_CONTENT_MODEL () { 0 }
786    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
787    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
788    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
789    
790    sub DATA_STATE () { 0 }
791    #sub ENTITY_DATA_STATE () { 1 }
792    sub TAG_OPEN_STATE () { 2 }
793    sub CLOSE_TAG_OPEN_STATE () { 3 }
794    sub TAG_NAME_STATE () { 4 }
795    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
796    sub ATTRIBUTE_NAME_STATE () { 6 }
797    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
798    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
799    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
800    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
801    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
802    #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
803    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
804    sub COMMENT_START_STATE () { 14 }
805    sub COMMENT_START_DASH_STATE () { 15 }
806    sub COMMENT_STATE () { 16 }
807    sub COMMENT_END_STATE () { 17 }
808    sub COMMENT_END_DASH_STATE () { 18 }
809    sub BOGUS_COMMENT_STATE () { 19 }
810    sub DOCTYPE_STATE () { 20 }
811    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
812    sub DOCTYPE_NAME_STATE () { 22 }
813    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
814    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
815    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
816    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
817    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
818    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
819    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
820    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
821    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
822    sub BOGUS_DOCTYPE_STATE () { 32 }
823    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
824    sub SELF_CLOSING_START_TAG_STATE () { 34 }
825    sub CDATA_SECTION_STATE () { 35 }
826    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
827    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
828    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
829    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
830    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
831    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
832    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
833    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
834    ## NOTE: "Entity data state", "entity in attribute value state", and
835    ## "consume a character reference" algorithm are jointly implemented
836    ## using the following six states:
837    sub ENTITY_STATE () { 44 }
838    sub ENTITY_HASH_STATE () { 45 }
839    sub NCR_NUM_STATE () { 46 }
840    sub HEXREF_X_STATE () { 47 }
841    sub HEXREF_HEX_STATE () { 48 }
842    sub ENTITY_NAME_STATE () { 49 }
843    
844    sub DOCTYPE_TOKEN () { 1 }
845    sub COMMENT_TOKEN () { 2 }
846    sub START_TAG_TOKEN () { 3 }
847    sub END_TAG_TOKEN () { 4 }
848    sub END_OF_FILE_TOKEN () { 5 }
849    sub CHARACTER_TOKEN () { 6 }
850    
851    sub AFTER_HTML_IMS () { 0b100 }
852    sub HEAD_IMS ()       { 0b1000 }
853    sub BODY_IMS ()       { 0b10000 }
854    sub BODY_TABLE_IMS () { 0b100000 }
855    sub TABLE_IMS ()      { 0b1000000 }
856    sub ROW_IMS ()        { 0b10000000 }
857    sub BODY_AFTER_IMS () { 0b100000000 }
858    sub FRAME_IMS ()      { 0b1000000000 }
859    sub SELECT_IMS ()     { 0b10000000000 }
860    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
861        ## NOTE: "in foreign content" insertion mode is special; it is combined
862        ## with the secondary insertion mode.  In this parser, they are stored
863        ## together in the bit-or'ed form.
864    
865    ## NOTE: "initial" and "before html" insertion modes have no constants.
866    
867    ## NOTE: "after after body" insertion mode.
868    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
869    
870    ## NOTE: "after after frameset" insertion mode.
871    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
872    
873    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
874    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
875    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
876    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
877    sub IN_BODY_IM () { BODY_IMS }
878    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
879    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
880    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
881    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
882    sub IN_TABLE_IM () { TABLE_IMS }
883    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
884    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
885    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
886    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
887    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
888    sub IN_COLUMN_GROUP_IM () { 0b10 }
889    
890  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
891    
892  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
893    my $self = shift;    my $self = shift;
894    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
895    $self->{content_model_flag} = 'PCDATA'; # be    #$self->{state_keyword}; # initialized when used
896    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    #$self->{entity__value}; # initialized when used
897      #$self->{entity__match}; # initialized when used
898      $self->{content_model} = PCDATA_CONTENT_MODEL; # be
899      undef $self->{current_token};
900    undef $self->{current_attribute};    undef $self->{current_attribute};
901    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
902    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
903    $self->{char} = [];    delete $self->{self_closing};
904    # $self->{next_input_character}    # $self->{next_char}
905    !!!next-input-character;    !!!next-input-character;
906    $self->{token} = [];    $self->{token} = [];
907      # $self->{escape}
908  } # _initialize_tokenizer  } # _initialize_tokenizer
909    
910  ## A token has:  ## A token has:
911  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
912  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
913  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
914      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
915  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
916  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
917  ##   ->{data} (comment, character)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
918    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
919  ## Macros  ##        ->{name}
920  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
921  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
922  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
923    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
924    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
925    ##     while the token is pushed back to the stack.
926    
927  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
928    
# Line 445  sub _initialize_tokenizer ($) { Line 932  sub _initialize_tokenizer ($) {
932  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
933  ## and removed from the list.  ## and removed from the list.
934    
935  ## ISSUE: <http://html5.org/tools/web-apps-tracker?from=874&to=876>  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
936    ## (This requirement was dropped from HTML5 spec, unfortunately.)
937    
938  sub _get_next_token ($) {  sub _get_next_token ($) {
939    my $self = shift;    my $self = shift;
940    
941      if ($self->{self_closing}) {
942        !!!parse-error (type => 'nestc', token => $self->{current_token});
943        ## NOTE: The |self_closing| flag is only set by start tag token.
944        ## In addition, when a start tag token is emitted, it is always set to
945        ## |current_token|.
946        delete $self->{self_closing};
947      }
948    
949    if (@{$self->{token}}) {    if (@{$self->{token}}) {
950        $self->{self_closing} = $self->{token}->[0]->{self_closing};
951      return shift @{$self->{token}};      return shift @{$self->{token}};
952    }    }
953    
954    A: {    A: {
955      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
956        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
957          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
958              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
959            $self->{state} = 'entity data';            !!!cp (1);
960              ## NOTE: In the spec, the tokenizer is switched to the
961              ## "entity data state".  In this implementation, the tokenizer
962              ## is switched to the |ENTITY_STATE|, which is an implementation
963              ## of the "consume a character reference" algorithm.
964              $self->{entity_additional} = -1;
965              $self->{prev_state} = DATA_STATE;
966              $self->{state} = ENTITY_STATE;
967            !!!next-input-character;            !!!next-input-character;
968            redo A;            redo A;
969          } else {          } else {
970              !!!cp (2);
971            #            #
972          }          }
973        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x002D) { # -
974          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
975            $self->{state} = 'tag open';            unless ($self->{escape}) {
976                if ($self->{prev_char}->[0] == 0x002D and # -
977                    $self->{prev_char}->[1] == 0x0021 and # !
978                    $self->{prev_char}->[2] == 0x003C) { # <
979                  !!!cp (3);
980                  $self->{escape} = 1;
981                } else {
982                  !!!cp (4);
983                }
984              } else {
985                !!!cp (5);
986              }
987            }
988            
989            #
990          } elsif ($self->{next_char} == 0x003C) { # <
991            if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
992                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
993                 not $self->{escape})) {
994              !!!cp (6);
995              $self->{state} = TAG_OPEN_STATE;
996            !!!next-input-character;            !!!next-input-character;
997            redo A;            redo A;
998          } else {          } else {
999              !!!cp (7);
1000            #            #
1001          }          }
1002        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
1003          !!!emit ({type => 'end-of-file'});          if ($self->{escape} and
1004                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1005              if ($self->{prev_char}->[0] == 0x002D and # -
1006                  $self->{prev_char}->[1] == 0x002D) { # -
1007                !!!cp (8);
1008                delete $self->{escape};
1009              } else {
1010                !!!cp (9);
1011              }
1012            } else {
1013              !!!cp (10);
1014            }
1015            
1016            #
1017          } elsif ($self->{next_char} == -1) {
1018            !!!cp (11);
1019            !!!emit ({type => END_OF_FILE_TOKEN,
1020                      line => $self->{line}, column => $self->{column}});
1021          last A; ## TODO: ok?          last A; ## TODO: ok?
1022          } else {
1023            !!!cp (12);
1024        }        }
1025        # Anything else        # Anything else
1026        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
1027                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
1028                       line => $self->{line}, column => $self->{column},
1029                      };
1030          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1031    
1032        ## Stay in the data state        ## Stay in the data state
1033        !!!next-input-character;        !!!next-input-character;
1034    
1035        !!!emit ($token);        !!!emit ($token);
1036    
1037        redo A;        redo A;
1038      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1039        ## (cannot happen in CDATA state)        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1040                  if ($self->{next_char} == 0x002F) { # /
1041        my $token = $self->_tokenize_attempt_to_consume_an_entity;            !!!cp (15);
   
       $self->{state} = 'data';  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!emit ({type => 'character', data => '&'});  
       } else {  
         !!!emit ($token);  
       }  
   
       redo A;  
     } elsif ($self->{state} eq 'tag open') {  
       if ($self->{content_model_flag} eq 'RCDATA' or  
           $self->{content_model_flag} eq 'CDATA') {  
         if ($self->{next_input_character} == 0x002F) { # /  
1042            !!!next-input-character;            !!!next-input-character;
1043            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1044            redo A;            redo A;
1045          } else {          } else {
1046              !!!cp (16);
1047            ## reconsume            ## reconsume
1048            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1049    
1050            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1051                        line => $self->{line_prev},
1052                        column => $self->{column_prev},
1053                       });
1054    
1055            redo A;            redo A;
1056          }          }
1057        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1058          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1059            $self->{state} = 'markup declaration open';            !!!cp (17);
1060              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1061            !!!next-input-character;            !!!next-input-character;
1062            redo A;            redo A;
1063          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1064            $self->{state} = 'close tag open';            !!!cp (18);
1065              $self->{state} = CLOSE_TAG_OPEN_STATE;
1066            !!!next-input-character;            !!!next-input-character;
1067            redo A;            redo A;
1068          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1069                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1070              !!!cp (19);
1071            $self->{current_token}            $self->{current_token}
1072              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1073                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1074            $self->{state} = 'tag name';                 line => $self->{line_prev},
1075                   column => $self->{column_prev}};
1076              $self->{state} = TAG_NAME_STATE;
1077            !!!next-input-character;            !!!next-input-character;
1078            redo A;            redo A;
1079          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1080                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1081            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1082                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1083            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1084                                        line => $self->{line_prev},
1085                                        column => $self->{column_prev}};
1086              $self->{state} = TAG_NAME_STATE;
1087            !!!next-input-character;            !!!next-input-character;
1088            redo A;            redo A;
1089          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1090            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1091            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1092                              line => $self->{line_prev},
1093                              column => $self->{column_prev});
1094              $self->{state} = DATA_STATE;
1095            !!!next-input-character;            !!!next-input-character;
1096    
1097            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1098                        line => $self->{line_prev},
1099                        column => $self->{column_prev},
1100                       });
1101    
1102            redo A;            redo A;
1103          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1104            !!!parse-error (type => 'pio');            !!!cp (22);
1105            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1106            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1107                              column => $self->{column_prev});
1108              $self->{state} = BOGUS_COMMENT_STATE;
1109              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1110                                        line => $self->{line_prev},
1111                                        column => $self->{column_prev},
1112                                       };
1113              ## $self->{next_char} is intentionally left as is
1114            redo A;            redo A;
1115          } else {          } else {
1116            !!!parse-error (type => 'bare stago');            !!!cp (23);
1117            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1118                              line => $self->{line_prev},
1119                              column => $self->{column_prev});
1120              $self->{state} = DATA_STATE;
1121            ## reconsume            ## reconsume
1122    
1123            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1124                        line => $self->{line_prev},
1125                        column => $self->{column_prev},
1126                       });
1127    
1128            redo A;            redo A;
1129          }          }
1130        } else {        } else {
1131          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1132        }        }
1133      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1134        if ($self->{content_model_flag} eq 'RCDATA' or        ## NOTE: The "close tag open state" in the spec is implemented as
1135            $self->{content_model_flag} eq 'CDATA') {        ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1136          my @next_char;  
1137          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"</"
1138            push @next_char, $self->{next_input_character};        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1139            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);          if (defined $self->{last_emitted_start_tag_name}) {
1140            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1141            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {            $self->{state_keyword} = '';
1142              !!!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 => '</'});  
   
1143            redo A;            redo A;
1144          } else {          } else {
1145            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
1146            !!!back-next-input-character (@next_char);            ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1147            # and consume...            !!!cp (28);
1148              $self->{state} = DATA_STATE;
1149              ## Reconsume.
1150              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1151                        line => $l, column => $c,
1152                       });
1153              redo A;
1154          }          }
1155        }        }
1156          
1157        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1158            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1159          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1160                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1161          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1162          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1163          redo A;                 line => $l, column => $c};
1164        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1165                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1166          $self->{current_token} = {type => 'end tag',          redo A;
1167                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1168          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1169          !!!next-input-character;          !!!cp (30);
1170          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1171        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1172          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1173          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1174            !!!next-input-character;
1175            redo A;
1176          } elsif ($self->{next_char} == 0x003E) { # >
1177            !!!cp (31);
1178            !!!parse-error (type => 'empty end tag',
1179                            line => $self->{line_prev}, ## "<" in "</>"
1180                            column => $self->{column_prev} - 1);
1181            $self->{state} = DATA_STATE;
1182          !!!next-input-character;          !!!next-input-character;
1183          redo A;          redo A;
1184        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1185            !!!cp (32);
1186          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1187          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1188          # reconsume          # reconsume
1189    
1190          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1191                      line => $l, column => $c,
1192                     });
1193    
1194          redo A;          redo A;
1195        } else {        } else {
1196            !!!cp (33);
1197          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1198          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1199          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1200          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1201                                      column => $self->{column_prev} - 1,
1202                                     };
1203            ## NOTE: $self->{next_char} is intentionally left as is.
1204            ## Although the "anything else" case of the spec not explicitly
1205            ## states that the next input character is to be reconsumed,
1206            ## it will be included to the |data| of the comment token
1207            ## generated from the bogus end tag, as defined in the
1208            ## "bogus comment state" entry.
1209            redo A;
1210          }
1211        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1212          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1213          if (length $ch) {
1214            my $CH = $ch;
1215            $ch =~ tr/a-z/A-Z/;
1216            my $nch = chr $self->{next_char};
1217            if ($nch eq $ch or $nch eq $CH) {
1218              !!!cp (24);
1219              ## Stay in the state.
1220              $self->{state_keyword} .= $nch;
1221              !!!next-input-character;
1222              redo A;
1223            } else {
1224              !!!cp (25);
1225              $self->{state} = DATA_STATE;
1226              ## Reconsume.
1227              !!!emit ({type => CHARACTER_TOKEN,
1228                        data => '</' . $self->{state_keyword},
1229                        line => $self->{line_prev},
1230                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1231                       });
1232              redo A;
1233            }
1234          } else { # after "<{tag-name}"
1235            unless ({
1236                     0x0009 => 1, # HT
1237                     0x000A => 1, # LF
1238                     0x000B => 1, # VT
1239                     0x000C => 1, # FF
1240                     0x0020 => 1, # SP
1241                     0x003E => 1, # >
1242                     0x002F => 1, # /
1243                     -1 => 1, # EOF
1244                    }->{$self->{next_char}}) {
1245              !!!cp (26);
1246              ## Reconsume.
1247              $self->{state} = DATA_STATE;
1248              !!!emit ({type => CHARACTER_TOKEN,
1249                        data => '</' . $self->{state_keyword},
1250                        line => $self->{line_prev},
1251                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1252                       });
1253              redo A;
1254            } else {
1255              !!!cp (27);
1256              $self->{current_token}
1257                  = {type => END_TAG_TOKEN,
1258                     tag_name => $self->{last_emitted_start_tag_name},
1259                     line => $self->{line_prev},
1260                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1261              $self->{state} = TAG_NAME_STATE;
1262              ## Reconsume.
1263              redo A;
1264            }
1265        }        }
1266      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
1267        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1268            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1269            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1270            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1271            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1272          $self->{state} = 'before attribute name';          !!!cp (34);
1273            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1274          !!!next-input-character;          !!!next-input-character;
1275          redo A;          redo A;
1276        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1277          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1278              !!!cp (35);
1279            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1280          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1281            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1282            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1283              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1284            }            #  !!! cp (36);
1285              #  !!! parse-error (type => 'end tag attribute');
1286              #} else {
1287                !!!cp (37);
1288              #}
1289          } else {          } else {
1290            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1291          }          }
1292          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1293          !!!next-input-character;          !!!next-input-character;
1294    
1295          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1296    
1297          redo A;          redo A;
1298        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1299                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1300          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1301            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1302            # start tag or end tag            # start tag or end tag
1303          ## Stay in this state          ## Stay in this state
1304          !!!next-input-character;          !!!next-input-character;
1305          redo A;          redo A;
1306        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1307          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1308          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1309              !!!cp (39);
1310            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1311          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1312            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1313            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1314              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1315            }            #  !!! cp (40);
1316              #  !!! parse-error (type => 'end tag attribute');
1317              #} else {
1318                !!!cp (41);
1319              #}
1320          } else {          } else {
1321            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1322          }          }
1323          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1324          # reconsume          # reconsume
1325    
1326          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1327    
1328          redo A;          redo A;
1329        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1330            !!!cp (42);
1331            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1332          !!!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  
1333          redo A;          redo A;
1334        } else {        } else {
1335          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1336            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1337            # start tag or end tag            # start tag or end tag
1338          ## Stay in the state          ## Stay in the state
1339          !!!next-input-character;          !!!next-input-character;
1340          redo A;          redo A;
1341        }        }
1342      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1343        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1344            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1345            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1346            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1347            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1348            !!!cp (45);
1349          ## Stay in the state          ## Stay in the state
1350          !!!next-input-character;          !!!next-input-character;
1351          redo A;          redo A;
1352        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1353          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1354              !!!cp (46);
1355            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1356          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1357            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1358            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1359                !!!cp (47);
1360              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1361              } else {
1362                !!!cp (48);
1363            }            }
1364          } else {          } else {
1365            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1366          }          }
1367          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1368          !!!next-input-character;          !!!next-input-character;
1369    
1370          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1371    
1372          redo A;          redo A;
1373        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1374                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1375          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1376                                value => ''};          $self->{current_attribute}
1377          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1378                   value => '',
1379                   line => $self->{line}, column => $self->{column}};
1380            $self->{state} = ATTRIBUTE_NAME_STATE;
1381            !!!next-input-character;
1382            redo A;
1383          } elsif ($self->{next_char} == 0x002F) { # /
1384            !!!cp (50);
1385            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1386          !!!next-input-character;          !!!next-input-character;
1387          redo A;          redo A;
1388        } 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) {  
1389          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1390          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1391              !!!cp (52);
1392            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1393          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1394            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1395            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1396                !!!cp (53);
1397              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1398              } else {
1399                !!!cp (54);
1400            }            }
1401          } else {          } else {
1402            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1403          }          }
1404          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1405          # reconsume          # reconsume
1406    
1407          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1408    
1409          redo A;          redo A;
1410        } else {        } else {
1411          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1412                                value => ''};               0x0022 => 1, # "
1413          $self->{state} = 'attribute name';               0x0027 => 1, # '
1414                 0x003D => 1, # =
1415                }->{$self->{next_char}}) {
1416              !!!cp (55);
1417              !!!parse-error (type => 'bad attribute name');
1418            } else {
1419              !!!cp (56);
1420            }
1421            $self->{current_attribute}
1422                = {name => chr ($self->{next_char}),
1423                   value => '',
1424                   line => $self->{line}, column => $self->{column}};
1425            $self->{state} = ATTRIBUTE_NAME_STATE;
1426          !!!next-input-character;          !!!next-input-character;
1427          redo A;          redo A;
1428        }        }
1429      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1430        my $before_leave = sub {        my $before_leave = sub {
1431          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1432              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1433            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1434              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1435            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1436          } else {          } else {
1437              !!!cp (58);
1438            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1439              = $self->{current_attribute};              = $self->{current_attribute};
1440          }          }
1441        }; # $before_leave        }; # $before_leave
1442    
1443        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1444            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1445            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1446            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1447            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1448            !!!cp (59);
1449          $before_leave->();          $before_leave->();
1450          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1451          !!!next-input-character;          !!!next-input-character;
1452          redo A;          redo A;
1453        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1454            !!!cp (60);
1455          $before_leave->();          $before_leave->();
1456          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1457          !!!next-input-character;          !!!next-input-character;
1458          redo A;          redo A;
1459        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1460          $before_leave->();          $before_leave->();
1461          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1462              !!!cp (61);
1463            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1464          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1465            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
1466              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1467            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1468              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1469            }            }
1470          } else {          } else {
1471            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1472          }          }
1473          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1474          !!!next-input-character;          !!!next-input-character;
1475    
1476          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1477    
1478          redo A;          redo A;
1479        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1480                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1481          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1482            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1483          ## Stay in the state          ## Stay in the state
1484          !!!next-input-character;          !!!next-input-character;
1485          redo A;          redo A;
1486        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1487            !!!cp (64);
1488          $before_leave->();          $before_leave->();
1489            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1490          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
1491          redo A;          redo A;
1492        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1493          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1494          $before_leave->();          $before_leave->();
1495          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1496              !!!cp (66);
1497            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1498          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1499            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1500            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1501                !!!cp (67);
1502              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1503              } else {
1504                ## NOTE: This state should never be reached.
1505                !!!cp (68);
1506            }            }
1507          } else {          } else {
1508            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1509          }          }
1510          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1511          # reconsume          # reconsume
1512    
1513          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1514    
1515          redo A;          redo A;
1516        } else {        } else {
1517          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1518                $self->{next_char} == 0x0027) { # '
1519              !!!cp (69);
1520              !!!parse-error (type => 'bad attribute name');
1521            } else {
1522              !!!cp (70);
1523            }
1524            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1525          ## Stay in the state          ## Stay in the state
1526          !!!next-input-character;          !!!next-input-character;
1527          redo A;          redo A;
1528        }        }
1529      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1530        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1531            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1532            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1533            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1534            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1535            !!!cp (71);
1536          ## Stay in the state          ## Stay in the state
1537          !!!next-input-character;          !!!next-input-character;
1538          redo A;          redo A;
1539        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1540          $self->{state} = 'before attribute value';          !!!cp (72);
1541            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1542          !!!next-input-character;          !!!next-input-character;
1543          redo A;          redo A;
1544        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1545          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1546              !!!cp (73);
1547            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1548          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1549            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1550            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1551                !!!cp (74);
1552              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1553              } else {
1554                ## NOTE: This state should never be reached.
1555                !!!cp (75);
1556            }            }
1557          } else {          } else {
1558            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1559          }          }
1560          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1561          !!!next-input-character;          !!!next-input-character;
1562    
1563          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1564    
1565          redo A;          redo A;
1566        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1567                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1568          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1569                                value => ''};          $self->{current_attribute}
1570          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1571          !!!next-input-character;                 value => '',
1572          redo A;                 line => $self->{line}, column => $self->{column}};
1573        } elsif ($self->{next_input_character} == 0x002F) { # /          $self->{state} = ATTRIBUTE_NAME_STATE;
1574            !!!next-input-character;
1575            redo A;
1576          } elsif ($self->{next_char} == 0x002F) { # /
1577            !!!cp (77);
1578            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1579          !!!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  
1580          redo A;          redo A;
1581        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1582          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1583          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1584              !!!cp (79);
1585            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1586          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1587            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1588            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1589                !!!cp (80);
1590              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1591              } else {
1592                ## NOTE: This state should never be reached.
1593                !!!cp (81);
1594            }            }
1595          } else {          } else {
1596            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1597          }          }
1598          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1599          # reconsume          # reconsume
1600    
1601          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1602    
1603          redo A;          redo A;
1604        } else {        } else {
1605          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{next_char} == 0x0022 or # "
1606                                value => ''};              $self->{next_char} == 0x0027) { # '
1607          $self->{state} = 'attribute name';            !!!cp (78);
1608              !!!parse-error (type => 'bad attribute name');
1609            } else {
1610              !!!cp (82);
1611            }
1612            $self->{current_attribute}
1613                = {name => chr ($self->{next_char}),
1614                   value => '',
1615                   line => $self->{line}, column => $self->{column}};
1616            $self->{state} = ATTRIBUTE_NAME_STATE;
1617          !!!next-input-character;          !!!next-input-character;
1618          redo A;                  redo A;        
1619        }        }
1620      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1621        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1622            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1623            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1624            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1625            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1626            !!!cp (83);
1627          ## Stay in the state          ## Stay in the state
1628          !!!next-input-character;          !!!next-input-character;
1629          redo A;          redo A;
1630        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1631          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1632            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1633          !!!next-input-character;          !!!next-input-character;
1634          redo A;          redo A;
1635        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1636          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1637            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1638          ## reconsume          ## reconsume
1639          redo A;          redo A;
1640        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1641          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1642            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1643          !!!next-input-character;          !!!next-input-character;
1644          redo A;          redo A;
1645        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1646          if ($self->{current_token}->{type} eq 'start tag') {          !!!parse-error (type => 'empty unquoted attribute value');
1647            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1648              !!!cp (87);
1649            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1650          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1651            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1652            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1653                !!!cp (88);
1654              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1655              } else {
1656                ## NOTE: This state should never be reached.
1657                !!!cp (89);
1658            }            }
1659          } else {          } else {
1660            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1661          }          }
1662          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1663          !!!next-input-character;          !!!next-input-character;
1664    
1665          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1666    
1667          redo A;          redo A;
1668        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1669          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1670          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1671              !!!cp (90);
1672            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1673          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1674            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1675            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1676                !!!cp (91);
1677              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1678              } else {
1679                ## NOTE: This state should never be reached.
1680                !!!cp (92);
1681            }            }
1682          } else {          } else {
1683            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1684          }          }
1685          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1686          ## reconsume          ## reconsume
1687    
1688          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1689    
1690          redo A;          redo A;
1691        } else {        } else {
1692          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1693          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1694              !!!parse-error (type => 'bad attribute value');
1695            } else {
1696              !!!cp (94);
1697            }
1698            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1699            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1700          !!!next-input-character;          !!!next-input-character;
1701          redo A;          redo A;
1702        }        }
1703      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1704        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1705          $self->{state} = 'before attribute name';          !!!cp (95);
1706            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1707          !!!next-input-character;          !!!next-input-character;
1708          redo A;          redo A;
1709        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1710          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1711          $self->{state} = 'entity in attribute value';          ## NOTE: In the spec, the tokenizer is switched to the
1712            ## "entity in attribute value state".  In this implementation, the
1713            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1714            ## implementation of the "consume a character reference" algorithm.
1715            $self->{prev_state} = $self->{state};
1716            $self->{entity_additional} = 0x0022; # "
1717            $self->{state} = ENTITY_STATE;
1718          !!!next-input-character;          !!!next-input-character;
1719          redo A;          redo A;
1720        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1721          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1722          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1723              !!!cp (97);
1724            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1725          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1726            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1727            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1728                !!!cp (98);
1729              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1730              } else {
1731                ## NOTE: This state should never be reached.
1732                !!!cp (99);
1733            }            }
1734          } else {          } else {
1735            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1736          }          }
1737          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1738          ## reconsume          ## reconsume
1739    
1740          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1741    
1742          redo A;          redo A;
1743        } else {        } else {
1744          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1745            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1746          ## Stay in the state          ## Stay in the state
1747          !!!next-input-character;          !!!next-input-character;
1748          redo A;          redo A;
1749        }        }
1750      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1751        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1752          $self->{state} = 'before attribute name';          !!!cp (101);
1753          !!!next-input-character;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1754          redo A;          !!!next-input-character;
1755        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1756          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1757          $self->{state} = 'entity in attribute value';          !!!cp (102);
1758            ## NOTE: In the spec, the tokenizer is switched to the
1759            ## "entity in attribute value state".  In this implementation, the
1760            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1761            ## implementation of the "consume a character reference" algorithm.
1762            $self->{entity_additional} = 0x0027; # '
1763            $self->{prev_state} = $self->{state};
1764            $self->{state} = ENTITY_STATE;
1765          !!!next-input-character;          !!!next-input-character;
1766          redo A;          redo A;
1767        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1768          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1769          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1770              !!!cp (103);
1771            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1772          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1773            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1774            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1775                !!!cp (104);
1776              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1777              } else {
1778                ## NOTE: This state should never be reached.
1779                !!!cp (105);
1780            }            }
1781          } else {          } else {
1782            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1783          }          }
1784          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1785          ## reconsume          ## reconsume
1786    
1787          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1788    
1789          redo A;          redo A;
1790        } else {        } else {
1791          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1792            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1793          ## Stay in the state          ## Stay in the state
1794          !!!next-input-character;          !!!next-input-character;
1795          redo A;          redo A;
1796        }        }
1797      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1798        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1799            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1800            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1801            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1802            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1803          $self->{state} = 'before attribute name';          !!!cp (107);
1804          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1805          redo A;          !!!next-input-character;
1806        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1807          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1808          $self->{state} = 'entity in attribute value';          !!!cp (108);
1809          !!!next-input-character;          ## NOTE: In the spec, the tokenizer is switched to the
1810          redo A;          ## "entity in attribute value state".  In this implementation, the
1811        } elsif ($self->{next_input_character} == 0x003E) { # >          ## tokenizer is switched to the |ENTITY_STATE|, which is an
1812          if ($self->{current_token}->{type} eq 'start tag') {          ## implementation of the "consume a character reference" algorithm.
1813            $self->{entity_additional} = -1;
1814            $self->{prev_state} = $self->{state};
1815            $self->{state} = ENTITY_STATE;
1816            !!!next-input-character;
1817            redo A;
1818          } elsif ($self->{next_char} == 0x003E) { # >
1819            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1820              !!!cp (109);
1821            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1822          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1823            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1824            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1825                !!!cp (110);
1826              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1827              } else {
1828                ## NOTE: This state should never be reached.
1829                !!!cp (111);
1830            }            }
1831          } else {          } else {
1832            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1833          }          }
1834          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1835          !!!next-input-character;          !!!next-input-character;
1836    
1837          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1838    
1839          redo A;          redo A;
1840        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1841          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1842          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1843              !!!cp (112);
1844            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1845          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1846            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1847            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1848                !!!cp (113);
1849              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1850              } else {
1851                ## NOTE: This state should never be reached.
1852                !!!cp (114);
1853            }            }
1854          } else {          } else {
1855            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1856          }          }
1857          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1858          ## reconsume          ## reconsume
1859    
1860          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1861    
1862          redo A;          redo A;
1863        } else {        } else {
1864          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1865                 0x0022 => 1, # "
1866                 0x0027 => 1, # '
1867                 0x003D => 1, # =
1868                }->{$self->{next_char}}) {
1869              !!!cp (115);
1870              !!!parse-error (type => 'bad attribute value');
1871            } else {
1872              !!!cp (116);
1873            }
1874            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1875          ## Stay in the state          ## Stay in the state
1876          !!!next-input-character;          !!!next-input-character;
1877          redo A;          redo A;
1878        }        }
1879      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1880        my $token = $self->_tokenize_attempt_to_consume_an_entity;        if ($self->{next_char} == 0x0009 or # HT
1881              $self->{next_char} == 0x000A or # LF
1882              $self->{next_char} == 0x000B or # VT
1883              $self->{next_char} == 0x000C or # FF
1884              $self->{next_char} == 0x0020) { # SP
1885            !!!cp (118);
1886            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1887            !!!next-input-character;
1888            redo A;
1889          } elsif ($self->{next_char} == 0x003E) { # >
1890            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1891              !!!cp (119);
1892              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1893            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1894              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1895              if ($self->{current_token}->{attributes}) {
1896                !!!cp (120);
1897                !!!parse-error (type => 'end tag attribute');
1898              } else {
1899                ## NOTE: This state should never be reached.
1900                !!!cp (121);
1901              }
1902            } else {
1903              die "$0: $self->{current_token}->{type}: Unknown token type";
1904            }
1905            $self->{state} = DATA_STATE;
1906            !!!next-input-character;
1907    
1908        unless (defined $token) {          !!!emit ($self->{current_token}); # start tag or end tag
1909          $self->{current_attribute}->{value} .= '&';  
1910            redo A;
1911          } elsif ($self->{next_char} == 0x002F) { # /
1912            !!!cp (122);
1913            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1914            !!!next-input-character;
1915            redo A;
1916          } elsif ($self->{next_char} == -1) {
1917            !!!parse-error (type => 'unclosed tag');
1918            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1919              !!!cp (122.3);
1920              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1921            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1922              if ($self->{current_token}->{attributes}) {
1923                !!!cp (122.1);
1924                !!!parse-error (type => 'end tag attribute');
1925              } else {
1926                ## NOTE: This state should never be reached.
1927                !!!cp (122.2);
1928              }
1929            } else {
1930              die "$0: $self->{current_token}->{type}: Unknown token type";
1931            }
1932            $self->{state} = DATA_STATE;
1933            ## Reconsume.
1934            !!!emit ($self->{current_token}); # start tag or end tag
1935            redo A;
1936        } else {        } else {
1937          $self->{current_attribute}->{value} .= $token->{data};          !!!cp ('124.1');
1938          ## ISSUE: spec says "append the returned character token to the current attribute's value"          !!!parse-error (type => 'no space between attributes');
1939            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1940            ## reconsume
1941            redo A;
1942        }        }
1943        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1944          if ($self->{next_char} == 0x003E) { # >
1945            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1946              !!!cp ('124.2');
1947              !!!parse-error (type => 'nestc', token => $self->{current_token});
1948              ## TODO: Different type than slash in start tag
1949              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1950              if ($self->{current_token}->{attributes}) {
1951                !!!cp ('124.4');
1952                !!!parse-error (type => 'end tag attribute');
1953              } else {
1954                !!!cp ('124.5');
1955              }
1956              ## TODO: Test |<title></title/>|
1957            } else {
1958              !!!cp ('124.3');
1959              $self->{self_closing} = 1;
1960            }
1961    
1962        $self->{state} = $self->{last_attribute_value_state};          $self->{state} = DATA_STATE;
1963        # next-input-character is already done          !!!next-input-character;
       redo A;  
     } elsif ($self->{state} eq 'bogus comment') {  
       ## (only happen if PCDATA state)  
         
       my $token = {type => 'comment', data => ''};  
   
       BC: {  
         if ($self->{next_input_character} == 0x003E) { # >  
           $self->{state} = 'data';  
           !!!next-input-character;  
   
           !!!emit ($token);  
   
           redo A;  
         } elsif ($self->{next_input_character} == -1) {  
           $self->{state} = 'data';  
           ## reconsume  
1964    
1965            !!!emit ($token);          !!!emit ($self->{current_token}); # start tag or end tag
1966    
1967            redo A;          redo A;
1968          } elsif ($self->{next_char} == -1) {
1969            !!!parse-error (type => 'unclosed tag');
1970            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1971              !!!cp (124.7);
1972              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1973            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1974              if ($self->{current_token}->{attributes}) {
1975                !!!cp (124.5);
1976                !!!parse-error (type => 'end tag attribute');
1977              } else {
1978                ## NOTE: This state should never be reached.
1979                !!!cp (124.6);
1980              }
1981          } else {          } else {
1982            $token->{data} .= chr ($self->{next_input_character});            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!next-input-character;  
           redo BC;  
1983          }          }
1984        } # BC          $self->{state} = DATA_STATE;
1985      } elsif ($self->{state} eq 'markup declaration open') {          ## Reconsume.
1986            !!!emit ($self->{current_token}); # start tag or end tag
1987            redo A;
1988          } else {
1989            !!!cp ('124.4');
1990            !!!parse-error (type => 'nestc');
1991            ## TODO: This error type is wrong.
1992            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1993            ## Reconsume.
1994            redo A;
1995          }
1996        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1997        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1998    
1999        my @next_char;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2000        push @next_char, $self->{next_input_character};        ## consumes characters one-by-one basis.
2001                
2002        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x003E) { # >
2003            !!!cp (124);
2004            $self->{state} = DATA_STATE;
2005          !!!next-input-character;          !!!next-input-character;
2006          push @next_char, $self->{next_input_character};  
2007          if ($self->{next_input_character} == 0x002D) { # -          !!!emit ($self->{current_token}); # comment
2008            $self->{current_token} = {type => 'comment', data => ''};          redo A;
2009            $self->{state} = 'comment';        } elsif ($self->{next_char} == -1) {
2010            !!!next-input-character;          !!!cp (125);
2011            redo A;          $self->{state} = DATA_STATE;
2012          }          ## reconsume
2013        } elsif ($self->{next_input_character} == 0x0044 or # D  
2014                 $self->{next_input_character} == 0x0064) { # d          !!!emit ($self->{current_token}); # comment
2015            redo A;
2016          } else {
2017            !!!cp (126);
2018            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2019            ## Stay in the state.
2020          !!!next-input-character;          !!!next-input-character;
2021          push @next_char, $self->{next_input_character};          redo A;
2022          if ($self->{next_input_character} == 0x004F or # O        }
2023              $self->{next_input_character} == 0x006F) { # o      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2024            !!!next-input-character;        ## (only happen if PCDATA state)
2025            push @next_char, $self->{next_input_character};        
2026            if ($self->{next_input_character} == 0x0043 or # C        if ($self->{next_char} == 0x002D) { # -
2027                $self->{next_input_character} == 0x0063) { # c          !!!cp (133);
2028              !!!next-input-character;          $self->{state} = MD_HYPHEN_STATE;
2029              push @next_char, $self->{next_input_character};          !!!next-input-character;
2030              if ($self->{next_input_character} == 0x0054 or # T          redo A;
2031                  $self->{next_input_character} == 0x0074) { # t        } elsif ($self->{next_char} == 0x0044 or # D
2032                !!!next-input-character;                 $self->{next_char} == 0x0064) { # d
2033                push @next_char, $self->{next_input_character};          ## ASCII case-insensitive.
2034                if ($self->{next_input_character} == 0x0059 or # Y          !!!cp (130);
2035                    $self->{next_input_character} == 0x0079) { # y          $self->{state} = MD_DOCTYPE_STATE;
2036                  !!!next-input-character;          $self->{state_keyword} = chr $self->{next_char};
2037                  push @next_char, $self->{next_input_character};          !!!next-input-character;
2038                  if ($self->{next_input_character} == 0x0050 or # P          redo A;
2039                      $self->{next_input_character} == 0x0070) { # p        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2040                    !!!next-input-character;                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2041                    push @next_char, $self->{next_input_character};                 $self->{next_char} == 0x005B) { # [
2042                    if ($self->{next_input_character} == 0x0045 or # E          !!!cp (135.4);                
2043                        $self->{next_input_character} == 0x0065) { # e          $self->{state} = MD_CDATA_STATE;
2044                      ## ISSUE: What a stupid code this is!          $self->{state_keyword} = '[';
2045                      $self->{state} = 'DOCTYPE';          !!!next-input-character;
2046                      !!!next-input-character;          redo A;
2047                      redo A;        } else {
2048                    }          !!!cp (136);
                 }  
               }  
             }  
           }  
         }  
2049        }        }
2050    
2051        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment',
2052        $self->{next_input_character} = shift @next_char;                        line => $self->{line_prev},
2053        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2054        $self->{state} = 'bogus comment';        ## Reconsume.
2055          $self->{state} = BOGUS_COMMENT_STATE;
2056          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2057                                    line => $self->{line_prev},
2058                                    column => $self->{column_prev} - 1,
2059                                   };
2060        redo A;        redo A;
2061              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2062        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2063        ## 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);
2064      } elsif ($self->{state} eq 'comment') {          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2065        if ($self->{next_input_character} == 0x002D) { # -                                    line => $self->{line_prev},
2066          $self->{state} = 'comment dash';                                    column => $self->{column_prev} - 2,
2067                                     };
2068            $self->{state} = COMMENT_START_STATE;
2069            !!!next-input-character;
2070            redo A;
2071          } else {
2072            !!!cp (128);
2073            !!!parse-error (type => 'bogus comment',
2074                            line => $self->{line_prev},
2075                            column => $self->{column_prev} - 2);
2076            $self->{state} = BOGUS_COMMENT_STATE;
2077            ## Reconsume.
2078            $self->{current_token} = {type => COMMENT_TOKEN,
2079                                      data => '-',
2080                                      line => $self->{line_prev},
2081                                      column => $self->{column_prev} - 2,
2082                                     };
2083            redo A;
2084          }
2085        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2086          ## ASCII case-insensitive.
2087          if ($self->{next_char} == [
2088                undef,
2089                0x004F, # O
2090                0x0043, # C
2091                0x0054, # T
2092                0x0059, # Y
2093                0x0050, # P
2094              ]->[length $self->{state_keyword}] or
2095              $self->{next_char} == [
2096                undef,
2097                0x006F, # o
2098                0x0063, # c
2099                0x0074, # t
2100                0x0079, # y
2101                0x0070, # p
2102              ]->[length $self->{state_keyword}]) {
2103            !!!cp (131);
2104            ## Stay in the state.
2105            $self->{state_keyword} .= chr $self->{next_char};
2106            !!!next-input-character;
2107            redo A;
2108          } elsif ((length $self->{state_keyword}) == 6 and
2109                   ($self->{next_char} == 0x0045 or # E
2110                    $self->{next_char} == 0x0065)) { # e
2111            !!!cp (129);
2112            $self->{state} = DOCTYPE_STATE;
2113            $self->{current_token} = {type => DOCTYPE_TOKEN,
2114                                      quirks => 1,
2115                                      line => $self->{line_prev},
2116                                      column => $self->{column_prev} - 7,
2117                                     };
2118            !!!next-input-character;
2119            redo A;
2120          } else {
2121            !!!cp (132);        
2122            !!!parse-error (type => 'bogus comment',
2123                            line => $self->{line_prev},
2124                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2125            $self->{state} = BOGUS_COMMENT_STATE;
2126            ## Reconsume.
2127            $self->{current_token} = {type => COMMENT_TOKEN,
2128                                      data => $self->{state_keyword},
2129                                      line => $self->{line_prev},
2130                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2131                                     };
2132            redo A;
2133          }
2134        } elsif ($self->{state} == MD_CDATA_STATE) {
2135          if ($self->{next_char} == {
2136                '[' => 0x0043, # C
2137                '[C' => 0x0044, # D
2138                '[CD' => 0x0041, # A
2139                '[CDA' => 0x0054, # T
2140                '[CDAT' => 0x0041, # A
2141              }->{$self->{state_keyword}}) {
2142            !!!cp (135.1);
2143            ## Stay in the state.
2144            $self->{state_keyword} .= chr $self->{next_char};
2145            !!!next-input-character;
2146            redo A;
2147          } elsif ($self->{state_keyword} eq '[CDATA' and
2148                   $self->{next_char} == 0x005B) { # [
2149            !!!cp (135.2);
2150            $self->{current_token} = {type => CHARACTER_TOKEN,
2151                                      data => '',
2152                                      line => $self->{line_prev},
2153                                      column => $self->{column_prev} - 7};
2154            $self->{state} = CDATA_SECTION_STATE;
2155          !!!next-input-character;          !!!next-input-character;
2156          redo A;          redo A;
2157        } elsif ($self->{next_input_character} == -1) {        } else {
2158            !!!cp (135.3);
2159            !!!parse-error (type => 'bogus comment',
2160                            line => $self->{line_prev},
2161                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2162            $self->{state} = BOGUS_COMMENT_STATE;
2163            ## Reconsume.
2164            $self->{current_token} = {type => COMMENT_TOKEN,
2165                                      data => $self->{state_keyword},
2166                                      line => $self->{line_prev},
2167                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2168                                     };
2169            redo A;
2170          }
2171        } elsif ($self->{state} == COMMENT_START_STATE) {
2172          if ($self->{next_char} == 0x002D) { # -
2173            !!!cp (137);
2174            $self->{state} = COMMENT_START_DASH_STATE;
2175            !!!next-input-character;
2176            redo A;
2177          } elsif ($self->{next_char} == 0x003E) { # >
2178            !!!cp (138);
2179            !!!parse-error (type => 'bogus comment');
2180            $self->{state} = DATA_STATE;
2181            !!!next-input-character;
2182    
2183            !!!emit ($self->{current_token}); # comment
2184    
2185            redo A;
2186          } elsif ($self->{next_char} == -1) {
2187            !!!cp (139);
2188          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2189          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2190          ## reconsume          ## reconsume
2191    
2192          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2193    
2194          redo A;          redo A;
2195        } else {        } else {
2196          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (140);
2197            $self->{current_token}->{data} # comment
2198                .= chr ($self->{next_char});
2199            $self->{state} = COMMENT_STATE;
2200            !!!next-input-character;
2201            redo A;
2202          }
2203        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2204          if ($self->{next_char} == 0x002D) { # -
2205            !!!cp (141);
2206            $self->{state} = COMMENT_END_STATE;
2207            !!!next-input-character;
2208            redo A;
2209          } elsif ($self->{next_char} == 0x003E) { # >
2210            !!!cp (142);
2211            !!!parse-error (type => 'bogus comment');
2212            $self->{state} = DATA_STATE;
2213            !!!next-input-character;
2214    
2215            !!!emit ($self->{current_token}); # comment
2216    
2217            redo A;
2218          } elsif ($self->{next_char} == -1) {
2219            !!!cp (143);
2220            !!!parse-error (type => 'unclosed comment');
2221            $self->{state} = DATA_STATE;
2222            ## reconsume
2223    
2224            !!!emit ($self->{current_token}); # comment
2225    
2226            redo A;
2227          } else {
2228            !!!cp (144);
2229            $self->{current_token}->{data} # comment
2230                .= '-' . chr ($self->{next_char});
2231            $self->{state} = COMMENT_STATE;
2232            !!!next-input-character;
2233            redo A;
2234          }
2235        } elsif ($self->{state} == COMMENT_STATE) {
2236          if ($self->{next_char} == 0x002D) { # -
2237            !!!cp (145);
2238            $self->{state} = COMMENT_END_DASH_STATE;
2239            !!!next-input-character;
2240            redo A;
2241          } elsif ($self->{next_char} == -1) {
2242            !!!cp (146);
2243            !!!parse-error (type => 'unclosed comment');
2244            $self->{state} = DATA_STATE;
2245            ## reconsume
2246    
2247            !!!emit ($self->{current_token}); # comment
2248    
2249            redo A;
2250          } else {
2251            !!!cp (147);
2252            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2253          ## Stay in the state          ## Stay in the state
2254          !!!next-input-character;          !!!next-input-character;
2255          redo A;          redo A;
2256        }        }
2257      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2258        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2259          $self->{state} = 'comment end';          !!!cp (148);
2260            $self->{state} = COMMENT_END_STATE;
2261          !!!next-input-character;          !!!next-input-character;
2262          redo A;          redo A;
2263        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2264            !!!cp (149);
2265          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2266          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2267          ## reconsume          ## reconsume
2268    
2269          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2270    
2271          redo A;          redo A;
2272        } else {        } else {
2273          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2274          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2275            $self->{state} = COMMENT_STATE;
2276          !!!next-input-character;          !!!next-input-character;
2277          redo A;          redo A;
2278        }        }
2279      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2280        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2281          $self->{state} = 'data';          !!!cp (151);
2282            $self->{state} = DATA_STATE;
2283          !!!next-input-character;          !!!next-input-character;
2284    
2285          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2286    
2287          redo A;          redo A;
2288        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2289          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2290            !!!parse-error (type => 'dash in comment',
2291                            line => $self->{line_prev},
2292                            column => $self->{column_prev});
2293          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2294          ## Stay in the state          ## Stay in the state
2295          !!!next-input-character;          !!!next-input-character;
2296          redo A;          redo A;
2297        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2298            !!!cp (153);
2299          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2300          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2301          ## reconsume          ## reconsume
2302    
2303          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2304    
2305          redo A;          redo A;
2306        } else {        } else {
2307          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2308          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2309          $self->{state} = 'comment';                          line => $self->{line_prev},
2310                            column => $self->{column_prev});
2311            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2312            $self->{state} = COMMENT_STATE;
2313          !!!next-input-character;          !!!next-input-character;
2314          redo A;          redo A;
2315        }        }
2316      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2317        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2318            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2319            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2320            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2321            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2322          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2323            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2324          !!!next-input-character;          !!!next-input-character;
2325          redo A;          redo A;
2326        } else {        } else {
2327            !!!cp (156);
2328          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2329          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2330          ## reconsume          ## reconsume
2331          redo A;          redo A;
2332        }        }
2333      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2334        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2335            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2336            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2337            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2338            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2339            !!!cp (157);
2340          ## Stay in the state          ## Stay in the state
2341          !!!next-input-character;          !!!next-input-character;
2342          redo A;          redo A;
2343        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
2344                 $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) { # >  
2345          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2346          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2347          !!!next-input-character;          !!!next-input-character;
2348    
2349          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2350    
2351          redo A;          redo A;
2352        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2353            !!!cp (159);
2354          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2355          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2356          ## reconsume          ## reconsume
2357    
2358          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2359    
2360          redo A;          redo A;
2361        } else {        } else {
2362          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (160);
2363                            name => chr ($self->{next_input_character}),          $self->{current_token}->{name} = chr $self->{next_char};
2364                            error => 1};          delete $self->{current_token}->{quirks};
2365  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2366          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2367          !!!next-input-character;          !!!next-input-character;
2368          redo A;          redo A;
2369        }        }
2370      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2371        if ($self->{next_input_character} == 0x0009 or # HT  ## ISSUE: Redundant "First," in the spec.
2372            $self->{next_input_character} == 0x000A or # LF        if ($self->{next_char} == 0x0009 or # HT
2373            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000A or # LF
2374            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000B or # VT
2375            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x000C or # FF
2376          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE            $self->{next_char} == 0x0020) { # SP
2377          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2378            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2379          !!!next-input-character;          !!!next-input-character;
2380          redo A;          redo A;
2381        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2382          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (162);
2383          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2384          !!!next-input-character;          !!!next-input-character;
2385    
2386          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2387    
2388          redo A;          redo A;
2389        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
2390                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (163);
2391          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed DOCTYPE');
2392          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{state} = DATA_STATE;
2393            ## reconsume
2394    
2395            $self->{current_token}->{quirks} = 1;
2396            !!!emit ($self->{current_token}); # DOCTYPE
2397    
2398            redo A;
2399          } else {
2400            !!!cp (164);
2401            $self->{current_token}->{name}
2402              .= chr ($self->{next_char}); # DOCTYPE
2403            ## Stay in the state
2404            !!!next-input-character;
2405            redo A;
2406          }
2407        } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2408          if ($self->{next_char} == 0x0009 or # HT
2409              $self->{next_char} == 0x000A or # LF
2410              $self->{next_char} == 0x000B or # VT
2411              $self->{next_char} == 0x000C or # FF
2412              $self->{next_char} == 0x0020) { # SP
2413            !!!cp (165);
2414          ## Stay in the state          ## Stay in the state
2415          !!!next-input-character;          !!!next-input-character;
2416          redo A;          redo A;
2417        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2418            !!!cp (166);
2419            $self->{state} = DATA_STATE;
2420            !!!next-input-character;
2421    
2422            !!!emit ($self->{current_token}); # DOCTYPE
2423    
2424            redo A;
2425          } elsif ($self->{next_char} == -1) {
2426            !!!cp (167);
2427          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2428          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
2429          ## reconsume          ## reconsume
2430    
2431          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
2432          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
2433    
2434            redo A;
2435          } elsif ($self->{next_char} == 0x0050 or # P
2436                   $self->{next_char} == 0x0070) { # p
2437            $self->{state} = PUBLIC_STATE;
2438            $self->{state_keyword} = chr $self->{next_char};
2439            !!!next-input-character;
2440            redo A;
2441          } elsif ($self->{next_char} == 0x0053 or # S
2442                   $self->{next_char} == 0x0073) { # s
2443            $self->{state} = SYSTEM_STATE;
2444            $self->{state_keyword} = chr $self->{next_char};
2445            !!!next-input-character;
2446            redo A;
2447          } else {
2448            !!!cp (180);
2449            !!!parse-error (type => 'string after DOCTYPE name');
2450            $self->{current_token}->{quirks} = 1;
2451    
2452            $self->{state} = BOGUS_DOCTYPE_STATE;
2453            !!!next-input-character;
2454            redo A;
2455          }
2456        } elsif ($self->{state} == PUBLIC_STATE) {
2457          ## ASCII case-insensitive
2458          if ($self->{next_char} == [
2459                undef,
2460                0x0055, # U
2461                0x0042, # B
2462                0x004C, # L
2463                0x0049, # I
2464              ]->[length $self->{state_keyword}] or
2465              $self->{next_char} == [
2466                undef,
2467                0x0075, # u
2468                0x0062, # b
2469                0x006C, # l
2470                0x0069, # i
2471              ]->[length $self->{state_keyword}]) {
2472            !!!cp (175);
2473            ## Stay in the state.
2474            $self->{state_keyword} .= chr $self->{next_char};
2475            !!!next-input-character;
2476            redo A;
2477          } elsif ((length $self->{state_keyword}) == 5 and
2478                   ($self->{next_char} == 0x0043 or # C
2479                    $self->{next_char} == 0x0063)) { # c
2480            !!!cp (168);
2481            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2482            !!!next-input-character;
2483          redo A;          redo A;
2484        } else {        } else {
2485          $self->{current_token}->{name}          !!!cp (169);
2486            .= chr ($self->{next_input_character}); # DOCTYPE          !!!parse-error (type => 'string after DOCTYPE name',
2487          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');                          line => $self->{line_prev},
2488                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2489            $self->{current_token}->{quirks} = 1;
2490    
2491            $self->{state} = BOGUS_DOCTYPE_STATE;
2492            ## Reconsume.
2493            redo A;
2494          }
2495        } elsif ($self->{state} == SYSTEM_STATE) {
2496          ## ASCII case-insensitive
2497          if ($self->{next_char} == [
2498                undef,
2499                0x0059, # Y
2500                0x0053, # S
2501                0x0054, # T
2502                0x0045, # E
2503              ]->[length $self->{state_keyword}] or
2504              $self->{next_char} == [
2505                undef,
2506                0x0079, # y
2507                0x0073, # s
2508                0x0074, # t
2509                0x0065, # e
2510              ]->[length $self->{state_keyword}]) {
2511            !!!cp (170);
2512            ## Stay in the state.
2513            $self->{state_keyword} .= chr $self->{next_char};
2514            !!!next-input-character;
2515            redo A;
2516          } elsif ((length $self->{state_keyword}) == 5 and
2517                   ($self->{next_char} == 0x004D or # M
2518                    $self->{next_char} == 0x006D)) { # m
2519            !!!cp (171);
2520            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2521            !!!next-input-character;
2522            redo A;
2523          } else {
2524            !!!cp (172);
2525            !!!parse-error (type => 'string after DOCTYPE name',
2526                            line => $self->{line_prev},
2527                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2528            $self->{current_token}->{quirks} = 1;
2529    
2530            $self->{state} = BOGUS_DOCTYPE_STATE;
2531            ## Reconsume.
2532            redo A;
2533          }
2534        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2535          if ({
2536                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2537                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2538              }->{$self->{next_char}}) {
2539            !!!cp (181);
2540          ## Stay in the state          ## Stay in the state
2541          !!!next-input-character;          !!!next-input-character;
2542          redo A;          redo A;
2543          } elsif ($self->{next_char} eq 0x0022) { # "
2544            !!!cp (182);
2545            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2546            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2547            !!!next-input-character;
2548            redo A;
2549          } elsif ($self->{next_char} eq 0x0027) { # '
2550            !!!cp (183);
2551            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2552            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2553            !!!next-input-character;
2554            redo A;
2555          } elsif ($self->{next_char} eq 0x003E) { # >
2556            !!!cp (184);
2557            !!!parse-error (type => 'no PUBLIC literal');
2558    
2559            $self->{state} = DATA_STATE;
2560            !!!next-input-character;
2561    
2562            $self->{current_token}->{quirks} = 1;
2563            !!!emit ($self->{current_token}); # DOCTYPE
2564    
2565            redo A;
2566          } elsif ($self->{next_char} == -1) {
2567            !!!cp (185);
2568            !!!parse-error (type => 'unclosed DOCTYPE');
2569    
2570            $self->{state} = DATA_STATE;
2571            ## reconsume
2572    
2573            $self->{current_token}->{quirks} = 1;
2574            !!!emit ($self->{current_token}); # DOCTYPE
2575    
2576            redo A;
2577          } else {
2578            !!!cp (186);
2579            !!!parse-error (type => 'string after PUBLIC');
2580            $self->{current_token}->{quirks} = 1;
2581    
2582            $self->{state} = BOGUS_DOCTYPE_STATE;
2583            !!!next-input-character;
2584            redo A;
2585        }        }
2586      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2587        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0022) { # "
2588            $self->{next_input_character} == 0x000A or # LF          !!!cp (187);
2589            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2590            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
2591            $self->{next_input_character} == 0x0020) { # SP          redo A;
2592          } elsif ($self->{next_char} == 0x003E) { # >
2593            !!!cp (188);
2594            !!!parse-error (type => 'unclosed PUBLIC literal');
2595    
2596            $self->{state} = DATA_STATE;
2597            !!!next-input-character;
2598    
2599            $self->{current_token}->{quirks} = 1;
2600            !!!emit ($self->{current_token}); # DOCTYPE
2601    
2602            redo A;
2603          } elsif ($self->{next_char} == -1) {
2604            !!!cp (189);
2605            !!!parse-error (type => 'unclosed PUBLIC literal');
2606    
2607            $self->{state} = DATA_STATE;
2608            ## reconsume
2609    
2610            $self->{current_token}->{quirks} = 1;
2611            !!!emit ($self->{current_token}); # DOCTYPE
2612    
2613            redo A;
2614          } else {
2615            !!!cp (190);
2616            $self->{current_token}->{public_identifier} # DOCTYPE
2617                .= chr $self->{next_char};
2618          ## Stay in the state          ## Stay in the state
2619          !!!next-input-character;          !!!next-input-character;
2620          redo A;          redo A;
2621        } elsif ($self->{next_input_character} == 0x003E) { # >        }
2622          $self->{state} = 'data';      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2623          if ($self->{next_char} == 0x0027) { # '
2624            !!!cp (191);
2625            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2626            !!!next-input-character;
2627            redo A;
2628          } elsif ($self->{next_char} == 0x003E) { # >
2629            !!!cp (192);
2630            !!!parse-error (type => 'unclosed PUBLIC literal');
2631    
2632            $self->{state} = DATA_STATE;
2633          !!!next-input-character;          !!!next-input-character;
2634    
2635            $self->{current_token}->{quirks} = 1;
2636          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2637    
2638          redo A;          redo A;
2639        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2640            !!!cp (193);
2641            !!!parse-error (type => 'unclosed PUBLIC literal');
2642    
2643            $self->{state} = DATA_STATE;
2644            ## reconsume
2645    
2646            $self->{current_token}->{quirks} = 1;
2647            !!!emit ($self->{current_token}); # DOCTYPE
2648    
2649            redo A;
2650          } else {
2651            !!!cp (194);
2652            $self->{current_token}->{public_identifier} # DOCTYPE
2653                .= chr $self->{next_char};
2654            ## Stay in the state
2655            !!!next-input-character;
2656            redo A;
2657          }
2658        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2659          if ({
2660                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2661                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2662              }->{$self->{next_char}}) {
2663            !!!cp (195);
2664            ## Stay in the state
2665            !!!next-input-character;
2666            redo A;
2667          } elsif ($self->{next_char} == 0x0022) { # "
2668            !!!cp (196);
2669            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2670            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2671            !!!next-input-character;
2672            redo A;
2673          } elsif ($self->{next_char} == 0x0027) { # '
2674            !!!cp (197);
2675            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2676            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2677            !!!next-input-character;
2678            redo A;
2679          } elsif ($self->{next_char} == 0x003E) { # >
2680            !!!cp (198);
2681            $self->{state} = DATA_STATE;
2682            !!!next-input-character;
2683    
2684            !!!emit ($self->{current_token}); # DOCTYPE
2685    
2686            redo A;
2687          } elsif ($self->{next_char} == -1) {
2688            !!!cp (199);
2689          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2690          $self->{state} = 'data';  
2691            $self->{state} = DATA_STATE;
2692          ## reconsume          ## reconsume
2693    
2694            $self->{current_token}->{quirks} = 1;
2695          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2696    
2697          redo A;          redo A;
2698        } else {        } else {
2699          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (200);
2700          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after PUBLIC literal');
2701          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2702    
2703            $self->{state} = BOGUS_DOCTYPE_STATE;
2704          !!!next-input-character;          !!!next-input-character;
2705          redo A;          redo A;
2706        }        }
2707      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2708        if ($self->{next_input_character} == 0x003E) { # >        if ({
2709          $self->{state} = 'data';              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2710                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2711              }->{$self->{next_char}}) {
2712            !!!cp (201);
2713            ## Stay in the state
2714            !!!next-input-character;
2715            redo A;
2716          } elsif ($self->{next_char} == 0x0022) { # "
2717            !!!cp (202);
2718            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2719            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2720            !!!next-input-character;
2721            redo A;
2722          } elsif ($self->{next_char} == 0x0027) { # '
2723            !!!cp (203);
2724            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2725            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2726            !!!next-input-character;
2727            redo A;
2728          } elsif ($self->{next_char} == 0x003E) { # >
2729            !!!cp (204);
2730            !!!parse-error (type => 'no SYSTEM literal');
2731            $self->{state} = DATA_STATE;
2732          !!!next-input-character;          !!!next-input-character;
2733    
2734            $self->{current_token}->{quirks} = 1;
2735          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2736    
2737          redo A;          redo A;
2738        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2739            !!!cp (205);
2740          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2741          $self->{state} = 'data';  
2742            $self->{state} = DATA_STATE;
2743            ## reconsume
2744    
2745            $self->{current_token}->{quirks} = 1;
2746            !!!emit ($self->{current_token}); # DOCTYPE
2747    
2748            redo A;
2749          } else {
2750            !!!cp (206);
2751            !!!parse-error (type => 'string after SYSTEM');
2752            $self->{current_token}->{quirks} = 1;
2753    
2754            $self->{state} = BOGUS_DOCTYPE_STATE;
2755            !!!next-input-character;
2756            redo A;
2757          }
2758        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2759          if ($self->{next_char} == 0x0022) { # "
2760            !!!cp (207);
2761            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2762            !!!next-input-character;
2763            redo A;
2764          } elsif ($self->{next_char} == 0x003E) { # >
2765            !!!cp (208);
2766            !!!parse-error (type => 'unclosed SYSTEM literal');
2767    
2768            $self->{state} = DATA_STATE;
2769            !!!next-input-character;
2770    
2771            $self->{current_token}->{quirks} = 1;
2772            !!!emit ($self->{current_token}); # DOCTYPE
2773    
2774            redo A;
2775          } elsif ($self->{next_char} == -1) {
2776            !!!cp (209);
2777            !!!parse-error (type => 'unclosed SYSTEM literal');
2778    
2779            $self->{state} = DATA_STATE;
2780          ## reconsume          ## reconsume
2781    
2782            $self->{current_token}->{quirks} = 1;
2783          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2784    
2785          redo A;          redo A;
2786        } else {        } else {
2787            !!!cp (210);
2788            $self->{current_token}->{system_identifier} # DOCTYPE
2789                .= chr $self->{next_char};
2790          ## Stay in the state          ## Stay in the state
2791          !!!next-input-character;          !!!next-input-character;
2792          redo A;          redo A;
2793        }        }
2794      } else {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2795        die "$0: $self->{state}: Unknown state";        if ($self->{next_char} == 0x0027) { # '
2796      }          !!!cp (211);
2797    } # A            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2798            !!!next-input-character;
2799            redo A;
2800          } elsif ($self->{next_char} == 0x003E) { # >
2801            !!!cp (212);
2802            !!!parse-error (type => 'unclosed SYSTEM literal');
2803    
2804    die "$0: _get_next_token: unexpected case";          $self->{state} = DATA_STATE;
2805  } # _get_next_token          !!!next-input-character;
2806    
2807  sub _tokenize_attempt_to_consume_an_entity ($) {          $self->{current_token}->{quirks} = 1;
2808    my $self = shift;          !!!emit ($self->{current_token}); # DOCTYPE
     
   if ($self->{next_input_character} == 0x0023) { # #  
     !!!next-input-character;  
     if ($self->{next_input_character} == 0x0078 or # x  
         $self->{next_input_character} == 0x0058) { # X  
       my $num;  
       X: {  
         my $x_char = $self->{next_input_character};  
         !!!next-input-character;  
         if (0x0030 <= $self->{next_input_character} and  
             $self->{next_input_character} <= 0x0039) { # 0..9  
           $num ||= 0;  
           $num *= 0x10;  
           $num += $self->{next_input_character} - 0x0030;  
           redo X;  
         } elsif (0x0061 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0066) { # a..f  
           ## ISSUE: the spec says U+0078, which is apparently incorrect  
           $num ||= 0;  
           $num *= 0x10;  
           $num += $self->{next_input_character} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0046) { # A..F  
           ## ISSUE: the spec says U+0058, which is apparently incorrect  
           $num ||= 0;  
           $num *= 0x10;  
           $num += $self->{next_input_character} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $num) { # no hexadecimal digit  
           !!!parse-error (type => 'bare hcro');  
           $self->{next_input_character} = 0x0023; # #  
           !!!back-next-input-character ($x_char);  
           return undef;  
         } elsif ($self->{next_input_character} == 0x003B) { # ;  
           !!!next-input-character;  
         } else {  
           !!!parse-error (type => 'no refc');  
         }  
2809    
2810          ## TODO: check the definition for |a valid Unicode character|.          redo A;
2811          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>        } elsif ($self->{next_char} == -1) {
2812          if ($num > 1114111 or $num == 0) {          !!!cp (213);
2813            $num = 0xFFFD; # REPLACEMENT CHARACTER          !!!parse-error (type => 'unclosed SYSTEM literal');
2814            ## ISSUE: Why this is not an error?  
2815          } elsif (0x80 <= $num and $num <= 0x9F) {          $self->{state} = DATA_STATE;
2816            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          ## reconsume
2817            $num = $c1_entity_char->{$num};  
2818          }          $self->{current_token}->{quirks} = 1;
2819            !!!emit ($self->{current_token}); # DOCTYPE
2820          return {type => 'character', data => chr $num};  
2821        } # X          redo A;
2822      } elsif (0x0030 <= $self->{next_input_character} and        } else {
2823               $self->{next_input_character} <= 0x0039) { # 0..9          !!!cp (214);
2824        my $code = $self->{next_input_character} - 0x0030;          $self->{current_token}->{system_identifier} # DOCTYPE
2825        !!!next-input-character;              .= chr $self->{next_char};
2826            ## Stay in the state
2827            !!!next-input-character;
2828            redo A;
2829          }
2830        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2831          if ({
2832                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2833                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2834              }->{$self->{next_char}}) {
2835            !!!cp (215);
2836            ## Stay in the state
2837            !!!next-input-character;
2838            redo A;
2839          } elsif ($self->{next_char} == 0x003E) { # >
2840            !!!cp (216);
2841            $self->{state} = DATA_STATE;
2842            !!!next-input-character;
2843    
2844            !!!emit ($self->{current_token}); # DOCTYPE
2845    
2846            redo A;
2847          } elsif ($self->{next_char} == -1) {
2848            !!!cp (217);
2849            !!!parse-error (type => 'unclosed DOCTYPE');
2850            $self->{state} = DATA_STATE;
2851            ## reconsume
2852    
2853            $self->{current_token}->{quirks} = 1;
2854            !!!emit ($self->{current_token}); # DOCTYPE
2855    
2856            redo A;
2857          } else {
2858            !!!cp (218);
2859            !!!parse-error (type => 'string after SYSTEM literal');
2860            #$self->{current_token}->{quirks} = 1;
2861    
2862            $self->{state} = BOGUS_DOCTYPE_STATE;
2863            !!!next-input-character;
2864            redo A;
2865          }
2866        } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2867          if ($self->{next_char} == 0x003E) { # >
2868            !!!cp (219);
2869            $self->{state} = DATA_STATE;
2870            !!!next-input-character;
2871    
2872            !!!emit ($self->{current_token}); # DOCTYPE
2873    
2874            redo A;
2875          } elsif ($self->{next_char} == -1) {
2876            !!!cp (220);
2877            !!!parse-error (type => 'unclosed DOCTYPE');
2878            $self->{state} = DATA_STATE;
2879            ## reconsume
2880    
2881            !!!emit ($self->{current_token}); # DOCTYPE
2882    
2883            redo A;
2884          } else {
2885            !!!cp (221);
2886            ## Stay in the state
2887            !!!next-input-character;
2888            redo A;
2889          }
2890        } elsif ($self->{state} == CDATA_SECTION_STATE) {
2891          ## NOTE: "CDATA section state" in the state is jointly implemented
2892          ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2893          ## and |CDATA_SECTION_MSE2_STATE|.
2894                
2895        while (0x0030 <= $self->{next_input_character} and        if ($self->{next_char} == 0x005D) { # ]
2896                  $self->{next_input_character} <= 0x0039) { # 0..9          !!!cp (221.1);
2897          $code *= 10;          $self->{state} = CDATA_SECTION_MSE1_STATE;
2898          $code += $self->{next_input_character} - 0x0030;          !!!next-input-character;
2899                    redo A;
2900          } elsif ($self->{next_char} == -1) {
2901            $self->{state} = DATA_STATE;
2902            !!!next-input-character;
2903            if (length $self->{current_token}->{data}) { # character
2904              !!!cp (221.2);
2905              !!!emit ($self->{current_token}); # character
2906            } else {
2907              !!!cp (221.3);
2908              ## No token to emit. $self->{current_token} is discarded.
2909            }        
2910            redo A;
2911          } else {
2912            !!!cp (221.4);
2913            $self->{current_token}->{data} .= chr $self->{next_char};
2914            ## Stay in the state.
2915          !!!next-input-character;          !!!next-input-character;
2916            redo A;
2917        }        }
2918    
2919        if ($self->{next_input_character} == 0x003B) { # ;        ## ISSUE: "text tokens" in spec.
2920        } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2921          if ($self->{next_char} == 0x005D) { # ]
2922            !!!cp (221.5);
2923            $self->{state} = CDATA_SECTION_MSE2_STATE;
2924            !!!next-input-character;
2925            redo A;
2926          } else {
2927            !!!cp (221.6);
2928            $self->{current_token}->{data} .= ']';
2929            $self->{state} = CDATA_SECTION_STATE;
2930            ## Reconsume.
2931            redo A;
2932          }
2933        } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2934          if ($self->{next_char} == 0x003E) { # >
2935            $self->{state} = DATA_STATE;
2936            !!!next-input-character;
2937            if (length $self->{current_token}->{data}) { # character
2938              !!!cp (221.7);
2939              !!!emit ($self->{current_token}); # character
2940            } else {
2941              !!!cp (221.8);
2942              ## No token to emit. $self->{current_token} is discarded.
2943            }
2944            redo A;
2945          } elsif ($self->{next_char} == 0x005D) { # ]
2946            !!!cp (221.9); # character
2947            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
2948            ## Stay in the state.
2949          !!!next-input-character;          !!!next-input-character;
2950            redo A;
2951          } else {
2952            !!!cp (221.11);
2953            $self->{current_token}->{data} .= ']]'; # character
2954            $self->{state} = CDATA_SECTION_STATE;
2955            ## Reconsume.
2956            redo A;
2957          }
2958        } elsif ($self->{state} == ENTITY_STATE) {
2959          if ({
2960            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2961            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
2962            $self->{entity_additional} => 1,
2963          }->{$self->{next_char}}) {
2964            !!!cp (1001);
2965            ## Don't consume
2966            ## No error
2967            ## Return nothing.
2968            #
2969          } elsif ($self->{next_char} == 0x0023) { # #
2970            !!!cp (999);
2971            $self->{state} = ENTITY_HASH_STATE;
2972            $self->{state_keyword} = '#';
2973            !!!next-input-character;
2974            redo A;
2975          } elsif ((0x0041 <= $self->{next_char} and
2976                    $self->{next_char} <= 0x005A) or # A..Z
2977                   (0x0061 <= $self->{next_char} and
2978                    $self->{next_char} <= 0x007A)) { # a..z
2979            !!!cp (998);
2980            require Whatpm::_NamedEntityList;
2981            $self->{state} = ENTITY_NAME_STATE;
2982            $self->{state_keyword} = chr $self->{next_char};
2983            $self->{entity__value} = $self->{state_keyword};
2984            $self->{entity__match} = 0;
2985            !!!next-input-character;
2986            redo A;
2987        } else {        } else {
2988            !!!cp (1027);
2989            !!!parse-error (type => 'bare ero');
2990            ## Return nothing.
2991            #
2992          }
2993    
2994          ## NOTE: No character is consumed by the "consume a character
2995          ## reference" algorithm.  In other word, there is an "&" character
2996          ## that does not introduce a character reference, which would be
2997          ## appended to the parent element or the attribute value in later
2998          ## process of the tokenizer.
2999    
3000          if ($self->{prev_state} == DATA_STATE) {
3001            !!!cp (997);
3002            $self->{state} = $self->{prev_state};
3003            ## Reconsume.
3004            !!!emit ({type => CHARACTER_TOKEN, data => '&',
3005                      line => $self->{line_prev},
3006                      column => $self->{column_prev},
3007                     });
3008            redo A;
3009          } else {
3010            !!!cp (996);
3011            $self->{current_attribute}->{value} .= '&';
3012            $self->{state} = $self->{prev_state};
3013            ## Reconsume.
3014            redo A;
3015          }
3016        } elsif ($self->{state} == ENTITY_HASH_STATE) {
3017          if ($self->{next_char} == 0x0078 or # x
3018              $self->{next_char} == 0x0058) { # X
3019            !!!cp (995);
3020            $self->{state} = HEXREF_X_STATE;
3021            $self->{state_keyword} .= chr $self->{next_char};
3022            !!!next-input-character;
3023            redo A;
3024          } elsif (0x0030 <= $self->{next_char} and
3025                   $self->{next_char} <= 0x0039) { # 0..9
3026            !!!cp (994);
3027            $self->{state} = NCR_NUM_STATE;
3028            $self->{state_keyword} = $self->{next_char} - 0x0030;
3029            !!!next-input-character;
3030            redo A;
3031          } else {
3032            !!!parse-error (type => 'bare nero',
3033                            line => $self->{line_prev},
3034                            column => $self->{column_prev} - 1);
3035    
3036            ## NOTE: According to the spec algorithm, nothing is returned,
3037            ## and then "&#" is appended to the parent element or the attribute
3038            ## value in the later processing.
3039    
3040            if ($self->{prev_state} == DATA_STATE) {
3041              !!!cp (1019);
3042              $self->{state} = $self->{prev_state};
3043              ## Reconsume.
3044              !!!emit ({type => CHARACTER_TOKEN,
3045                        data => '&#',
3046                        line => $self->{line_prev},
3047                        column => $self->{column_prev} - 1,
3048                       });
3049              redo A;
3050            } else {
3051              !!!cp (993);
3052              $self->{current_attribute}->{value} .= '&#';
3053              $self->{state} = $self->{prev_state};
3054              ## Reconsume.
3055              redo A;
3056            }
3057          }
3058        } elsif ($self->{state} == NCR_NUM_STATE) {
3059          if (0x0030 <= $self->{next_char} and
3060              $self->{next_char} <= 0x0039) { # 0..9
3061            !!!cp (1012);
3062            $self->{state_keyword} *= 10;
3063            $self->{state_keyword} += $self->{next_char} - 0x0030;
3064            
3065            ## Stay in the state.
3066            !!!next-input-character;
3067            redo A;
3068          } elsif ($self->{next_char} == 0x003B) { # ;
3069            !!!cp (1013);
3070            !!!next-input-character;
3071            #
3072          } else {
3073            !!!cp (1014);
3074          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
3075            ## Reconsume.
3076            #
3077        }        }
3078    
3079        ## TODO: check the definition for |a valid Unicode character|.        my $code = $self->{state_keyword};
3080        if ($code > 1114111 or $code == 0) {        my $l = $self->{line_prev};
3081          $code = 0xFFFD; # REPLACEMENT CHARACTER        my $c = $self->{column_prev};
3082          ## ISSUE: Why this is not an error?        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3083            !!!cp (1015);
3084            !!!parse-error (type => 'invalid character reference',
3085                            text => (sprintf 'U+%04X', $code),
3086                            line => $l, column => $c);
3087            $code = 0xFFFD;
3088          } elsif ($code > 0x10FFFF) {
3089            !!!cp (1016);
3090            !!!parse-error (type => 'invalid character reference',
3091                            text => (sprintf 'U-%08X', $code),
3092                            line => $l, column => $c);
3093            $code = 0xFFFD;
3094          } elsif ($code == 0x000D) {
3095            !!!cp (1017);
3096            !!!parse-error (type => 'CR character reference',
3097                            line => $l, column => $c);
3098            $code = 0x000A;
3099        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3100          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!cp (1018);
3101            !!!parse-error (type => 'C1 character reference',
3102                            text => (sprintf 'U+%04X', $code),
3103                            line => $l, column => $c);
3104          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3105        }        }
3106          
3107        return {type => 'character', data => chr $code};        if ($self->{prev_state} == DATA_STATE) {
3108      } else {          !!!cp (992);
3109        !!!parse-error (type => 'bare nero');          $self->{state} = $self->{prev_state};
3110        !!!back-next-input-character ($self->{next_input_character});          ## Reconsume.
3111        $self->{next_input_character} = 0x0023; # #          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3112        return undef;                    line => $l, column => $c,
3113      }                   });
3114    } elsif ((0x0041 <= $self->{next_input_character} and          redo A;
             $self->{next_input_character} <= 0x005A) or  
            (0x0061 <= $self->{next_input_character} and  
             $self->{next_input_character} <= 0x007A)) {  
     my $entity_name = chr $self->{next_input_character};  
     !!!next-input-character;  
   
     my $value = $entity_name;  
     my $match;  
   
     while (length $entity_name < 10 and  
            ## NOTE: Some number greater than the maximum length of entity name  
            ((0x0041 <= $self->{next_input_character} and  
              $self->{next_input_character} <= 0x005A) or  
             (0x0061 <= $self->{next_input_character} and  
              $self->{next_input_character} <= 0x007A) or  
             (0x0030 <= $self->{next_input_character} and  
              $self->{next_input_character} <= 0x0039))) {  
       $entity_name .= chr $self->{next_input_character};  
       if (defined $entity_char->{$entity_name}) {  
         $value = $entity_char->{$entity_name};  
         $match = 1;  
3115        } else {        } else {
3116          $value .= chr $self->{next_input_character};          !!!cp (991);
3117            $self->{current_attribute}->{value} .= chr $code;
3118            $self->{current_attribute}->{has_reference} = 1;
3119            $self->{state} = $self->{prev_state};
3120            ## Reconsume.
3121            redo A;
3122          }
3123        } elsif ($self->{state} == HEXREF_X_STATE) {
3124          if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3125              (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3126              (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3127            # 0..9, A..F, a..f
3128            !!!cp (990);
3129            $self->{state} = HEXREF_HEX_STATE;
3130            $self->{state_keyword} = 0;
3131            ## Reconsume.
3132            redo A;
3133          } else {
3134            !!!parse-error (type => 'bare hcro',
3135                            line => $self->{line_prev},
3136                            column => $self->{column_prev} - 2);
3137    
3138            ## NOTE: According to the spec algorithm, nothing is returned,
3139            ## and then "&#" followed by "X" or "x" is appended to the parent
3140            ## element or the attribute value in the later processing.
3141    
3142            if ($self->{prev_state} == DATA_STATE) {
3143              !!!cp (1005);
3144              $self->{state} = $self->{prev_state};
3145              ## Reconsume.
3146              !!!emit ({type => CHARACTER_TOKEN,
3147                        data => '&' . $self->{state_keyword},
3148                        line => $self->{line_prev},
3149                        column => $self->{column_prev} - length $self->{state_keyword},
3150                       });
3151              redo A;
3152            } else {
3153              !!!cp (989);
3154              $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3155              $self->{state} = $self->{prev_state};
3156              ## Reconsume.
3157              redo A;
3158            }
3159        }        }
3160        !!!next-input-character;      } elsif ($self->{state} == HEXREF_HEX_STATE) {
3161      }        if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3162                # 0..9
3163      if ($match) {          !!!cp (1002);
3164        if ($self->{next_input_character} == 0x003B) { # ;          $self->{state_keyword} *= 0x10;
3165            $self->{state_keyword} += $self->{next_char} - 0x0030;
3166            ## Stay in the state.
3167            !!!next-input-character;
3168            redo A;
3169          } elsif (0x0061 <= $self->{next_char} and
3170                   $self->{next_char} <= 0x0066) { # a..f
3171            !!!cp (1003);
3172            $self->{state_keyword} *= 0x10;
3173            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3174            ## Stay in the state.
3175            !!!next-input-character;
3176            redo A;
3177          } elsif (0x0041 <= $self->{next_char} and
3178                   $self->{next_char} <= 0x0046) { # A..F
3179            !!!cp (1004);
3180            $self->{state_keyword} *= 0x10;
3181            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3182            ## Stay in the state.
3183          !!!next-input-character;          !!!next-input-character;
3184            redo A;
3185          } elsif ($self->{next_char} == 0x003B) { # ;
3186            !!!cp (1006);
3187            !!!next-input-character;
3188            #
3189        } else {        } else {
3190          !!!parse-error (type => 'refc');          !!!cp (1007);
3191            !!!parse-error (type => 'no refc',
3192                            line => $self->{line},
3193                            column => $self->{column});
3194            ## Reconsume.
3195            #
3196          }
3197    
3198          my $code = $self->{state_keyword};
3199          my $l = $self->{line_prev};
3200          my $c = $self->{column_prev};
3201          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3202            !!!cp (1008);
3203            !!!parse-error (type => 'invalid character reference',
3204                            text => (sprintf 'U+%04X', $code),
3205                            line => $l, column => $c);
3206            $code = 0xFFFD;
3207          } elsif ($code > 0x10FFFF) {
3208            !!!cp (1009);
3209            !!!parse-error (type => 'invalid character reference',
3210                            text => (sprintf 'U-%08X', $code),
3211                            line => $l, column => $c);
3212            $code = 0xFFFD;
3213          } elsif ($code == 0x000D) {
3214            !!!cp (1010);
3215            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3216            $code = 0x000A;
3217          } elsif (0x80 <= $code and $code <= 0x9F) {
3218            !!!cp (1011);
3219            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3220            $code = $c1_entity_char->{$code};
3221        }        }
3222    
3223        return {type => 'character', data => $value};        if ($self->{prev_state} == DATA_STATE) {
3224            !!!cp (988);
3225            $self->{state} = $self->{prev_state};
3226            ## Reconsume.
3227            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3228                      line => $l, column => $c,
3229                     });
3230            redo A;
3231          } else {
3232            !!!cp (987);
3233            $self->{current_attribute}->{value} .= chr $code;
3234            $self->{current_attribute}->{has_reference} = 1;
3235            $self->{state} = $self->{prev_state};
3236            ## Reconsume.
3237            redo A;
3238          }
3239        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3240          if (length $self->{state_keyword} < 30 and
3241              ## NOTE: Some number greater than the maximum length of entity name
3242              ((0x0041 <= $self->{next_char} and # a
3243                $self->{next_char} <= 0x005A) or # x
3244               (0x0061 <= $self->{next_char} and # a
3245                $self->{next_char} <= 0x007A) or # z
3246               (0x0030 <= $self->{next_char} and # 0
3247                $self->{next_char} <= 0x0039) or # 9
3248               $self->{next_char} == 0x003B)) { # ;
3249            our $EntityChar;
3250            $self->{state_keyword} .= chr $self->{next_char};
3251            if (defined $EntityChar->{$self->{state_keyword}}) {
3252              if ($self->{next_char} == 0x003B) { # ;
3253                !!!cp (1020);
3254                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3255                $self->{entity__match} = 1;
3256                !!!next-input-character;
3257                #
3258              } else {
3259                !!!cp (1021);
3260                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3261                $self->{entity__match} = -1;
3262                ## Stay in the state.
3263                !!!next-input-character;
3264                redo A;
3265              }
3266            } else {
3267              !!!cp (1022);
3268              $self->{entity__value} .= chr $self->{next_char};
3269              $self->{entity__match} *= 2;
3270              ## Stay in the state.
3271              !!!next-input-character;
3272              redo A;
3273            }
3274          }
3275    
3276          my $data;
3277          my $has_ref;
3278          if ($self->{entity__match} > 0) {
3279            !!!cp (1023);
3280            $data = $self->{entity__value};
3281            $has_ref = 1;
3282            #
3283          } elsif ($self->{entity__match} < 0) {
3284            !!!parse-error (type => 'no refc');
3285            if ($self->{prev_state} != DATA_STATE and # in attribute
3286                $self->{entity__match} < -1) {
3287              !!!cp (1024);
3288              $data = '&' . $self->{state_keyword};
3289              #
3290            } else {
3291              !!!cp (1025);
3292              $data = $self->{entity__value};
3293              $has_ref = 1;
3294              #
3295            }
3296          } else {
3297            !!!cp (1026);
3298            !!!parse-error (type => 'bare ero',
3299                            line => $self->{line_prev},
3300                            column => $self->{column_prev});
3301            $data = '&' . $self->{state_keyword};
3302            #
3303          }
3304      
3305          ## NOTE: In these cases, when a character reference is found,
3306          ## it is consumed and a character token is returned, or, otherwise,
3307          ## nothing is consumed and returned, according to the spec algorithm.
3308          ## In this implementation, anything that has been examined by the
3309          ## tokenizer is appended to the parent element or the attribute value
3310          ## as string, either literal string when no character reference or
3311          ## entity-replaced string otherwise, in this stage, since any characters
3312          ## that would not be consumed are appended in the data state or in an
3313          ## appropriate attribute value state anyway.
3314    
3315          if ($self->{prev_state} == DATA_STATE) {
3316            !!!cp (986);
3317            $self->{state} = $self->{prev_state};
3318            ## Reconsume.
3319            !!!emit ({type => CHARACTER_TOKEN,
3320                      data => $data,
3321                      line => $self->{line_prev},
3322                      column => $self->{column_prev} + 1 - length $self->{state_keyword},
3323                     });
3324            redo A;
3325          } else {
3326            !!!cp (985);
3327            $self->{current_attribute}->{value} .= $data;
3328            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3329            $self->{state} = $self->{prev_state};
3330            ## Reconsume.
3331            redo A;
3332          }
3333      } else {      } else {
3334        !!!parse-error (type => 'bare ero');        die "$0: $self->{state}: Unknown state";
       ## NOTE: No characters are consumed in the spec.  
       !!!back-token ({type => 'character', data => $value});  
       return undef;  
3335      }      }
3336    } else {    } # A  
3337      ## no characters are consumed  
3338      !!!parse-error (type => 'bare ero');    die "$0: _get_next_token: unexpected case";
3339      return undef;  } # _get_next_token
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3340    
3341  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3342    my $self = shift;    my $self = shift;
# Line 1637  sub _initialize_tree_constructor ($) { Line 3344  sub _initialize_tree_constructor ($) {
3344    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
3345    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3346    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3347    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
3348      $self->{document}->set_user_data (manakai_source_line => 1);
3349      $self->{document}->set_user_data (manakai_source_column => 1);
3350  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3351    
3352  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1664  sub _construct_tree ($) { Line 3373  sub _construct_tree ($) {
3373        
3374    !!!next-token;    !!!next-token;
3375    
   $self->{insertion_mode} = 'before head';  
3376    undef $self->{form_element};    undef $self->{form_element};
3377    undef $self->{head_element};    undef $self->{head_element};
3378    $self->{open_elements} = [];    $self->{open_elements} = [];
3379    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3380    
3381      ## NOTE: The "initial" insertion mode.
3382    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3383    
3384      ## NOTE: The "before html" insertion mode.
3385    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3386      $self->{insertion_mode} = BEFORE_HEAD_IM;
3387    
3388      ## NOTE: The "before head" insertion mode and so on.
3389    $self->_tree_construction_main;    $self->_tree_construction_main;
3390  } # _construct_tree  } # _construct_tree
3391    
3392  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3393    my $self = shift;    my $self = shift;
3394    B: {  
3395        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
3396          if ($token->{error}) {  
3397            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
3398            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
3399          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3400          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
3401            ($token->{name});        ## language.
3402          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
3403          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
3404          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3405          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
3406          return;            defined $token->{system_identifier}) {
3407        } elsif ({          !!!cp ('t1');
3408                  comment => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3409                  'start tag' => 1,        } elsif ($doctype_name ne 'HTML') {
3410                  'end tag' => 1,          !!!cp ('t2');
3411                  'end-of-file' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3412                 }->{$token->{type}}) {        } elsif (defined $token->{public_identifier}) {
3413          ## ISSUE: Spec currently left this case undefined.          if ($token->{public_identifier} eq 'XSLT-compat') {
3414          !!!parse-error (type => 'missing DOCTYPE');            !!!cp ('t1.2');
3415          #$phase = 'root element';            !!!parse-error (type => 'XSLT-compat', token => $token,
3416          ## reprocess                            level => $self->{level}->{should});
3417          #redo B;          } else {
3418          return;            !!!parse-error (type => 'not HTML5', token => $token);
3419        } elsif ($token->{type} eq 'character') {          }
3420          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } else {
3421            $self->{document}->manakai_append_text ($1);          !!!cp ('t3');
3422            ## ISSUE: DOM3 Core does not allow Document > Text          #
3423            unless (length $token->{data}) {        }
3424              ## Stay in the phase        
3425              !!!next-token;        my $doctype = $self->{document}->create_document_type_definition
3426              redo B;          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3427          ## NOTE: Default value for both |public_id| and |system_id| attributes
3428          ## are empty strings, so that we don't set any value in missing cases.
3429          $doctype->public_id ($token->{public_identifier})
3430              if defined $token->{public_identifier};
3431          $doctype->system_id ($token->{system_identifier})
3432              if defined $token->{system_identifier};
3433          ## NOTE: Other DocumentType attributes are null or empty lists.
3434          ## ISSUE: internalSubset = null??
3435          $self->{document}->append_child ($doctype);
3436          
3437          if ($token->{quirks} or $doctype_name ne 'HTML') {
3438            !!!cp ('t4');
3439            $self->{document}->manakai_compat_mode ('quirks');
3440          } elsif (defined $token->{public_identifier}) {
3441            my $pubid = $token->{public_identifier};
3442            $pubid =~ tr/a-z/A-z/;
3443            my $prefix = [
3444              "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3445              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3446              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3447              "-//IETF//DTD HTML 2.0 LEVEL 1//",
3448              "-//IETF//DTD HTML 2.0 LEVEL 2//",
3449              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3450              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3451              "-//IETF//DTD HTML 2.0 STRICT//",
3452              "-//IETF//DTD HTML 2.0//",
3453              "-//IETF//DTD HTML 2.1E//",
3454              "-//IETF//DTD HTML 3.0//",
3455              "-//IETF//DTD HTML 3.2 FINAL//",
3456              "-//IETF//DTD HTML 3.2//",
3457              "-//IETF//DTD HTML 3//",
3458              "-//IETF//DTD HTML LEVEL 0//",
3459              "-//IETF//DTD HTML LEVEL 1//",
3460              "-//IETF//DTD HTML LEVEL 2//",
3461              "-//IETF//DTD HTML LEVEL 3//",
3462              "-//IETF//DTD HTML STRICT LEVEL 0//",
3463              "-//IETF//DTD HTML STRICT LEVEL 1//",
3464              "-//IETF//DTD HTML STRICT LEVEL 2//",
3465              "-//IETF//DTD HTML STRICT LEVEL 3//",
3466              "-//IETF//DTD HTML STRICT//",
3467              "-//IETF//DTD HTML//",
3468              "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3469              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3470              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3471              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3472              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3473              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3474              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3475              "-//NETSCAPE COMM. CORP.//DTD HTML//",
3476              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3477              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3478              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3479              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3480              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3481              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3482              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3483              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3484              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3485              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3486              "-//W3C//DTD HTML 3 1995-03-24//",
3487              "-//W3C//DTD HTML 3.2 DRAFT//",
3488              "-//W3C//DTD HTML 3.2 FINAL//",
3489              "-//W3C//DTD HTML 3.2//",
3490              "-//W3C//DTD HTML 3.2S DRAFT//",
3491              "-//W3C//DTD HTML 4.0 FRAMESET//",
3492              "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3493              "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3494              "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3495              "-//W3C//DTD W3 HTML//",
3496              "-//W3O//DTD W3 HTML 3.0//",
3497              "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3498              "-//WEBTECHS//DTD MOZILLA HTML//",
3499            ]; # $prefix
3500            my $match;
3501            for (@$prefix) {
3502              if (substr ($prefix, 0, length $_) eq $_) {
3503                $match = 1;
3504                last;
3505              }
3506            }
3507            if ($match or
3508                $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3509                $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3510                $pubid eq "HTML") {
3511              !!!cp ('t5');
3512              $self->{document}->manakai_compat_mode ('quirks');
3513            } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3514                     $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3515              if (defined $token->{system_identifier}) {
3516                !!!cp ('t6');
3517                $self->{document}->manakai_compat_mode ('quirks');
3518              } else {
3519                !!!cp ('t7');
3520                $self->{document}->manakai_compat_mode ('limited quirks');
3521            }            }
3522            } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3523                     $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3524              !!!cp ('t8');
3525              $self->{document}->manakai_compat_mode ('limited quirks');
3526            } else {
3527              !!!cp ('t9');
3528            }
3529          } else {
3530            !!!cp ('t10');
3531          }
3532          if (defined $token->{system_identifier}) {
3533            my $sysid = $token->{system_identifier};
3534            $sysid =~ tr/A-Z/a-z/;
3535            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3536              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3537              ## marked as quirks.
3538              $self->{document}->manakai_compat_mode ('quirks');
3539              !!!cp ('t11');
3540            } else {
3541              !!!cp ('t12');
3542            }
3543          } else {
3544            !!!cp ('t13');
3545          }
3546          
3547          ## Go to the "before html" insertion mode.
3548          !!!next-token;
3549          return;
3550        } elsif ({
3551                  START_TAG_TOKEN, 1,
3552                  END_TAG_TOKEN, 1,
3553                  END_OF_FILE_TOKEN, 1,
3554                 }->{$token->{type}}) {
3555          !!!cp ('t14');
3556          !!!parse-error (type => 'no DOCTYPE', token => $token);
3557          $self->{document}->manakai_compat_mode ('quirks');
3558          ## Go to the "before html" insertion mode.
3559          ## reprocess
3560          !!!ack-later;
3561          return;
3562        } elsif ($token->{type} == CHARACTER_TOKEN) {
3563          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3564            ## Ignore the token
3565    
3566            unless (length $token->{data}) {
3567              !!!cp ('t15');
3568              ## Stay in the insertion mode.
3569              !!!next-token;
3570              redo INITIAL;
3571            } else {
3572              !!!cp ('t16');
3573          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
3574        } else {        } else {
3575          die "$0: $token->{type}: Unknown token";          !!!cp ('t17');
3576        }        }
3577      } # B  
3578          !!!parse-error (type => 'no DOCTYPE', token => $token);
3579          $self->{document}->manakai_compat_mode ('quirks');
3580          ## Go to the "before html" insertion mode.
3581          ## reprocess
3582          return;
3583        } elsif ($token->{type} == COMMENT_TOKEN) {
3584          !!!cp ('t18');
3585          my $comment = $self->{document}->create_comment ($token->{data});
3586          $self->{document}->append_child ($comment);
3587          
3588          ## Stay in the insertion mode.
3589          !!!next-token;
3590          redo INITIAL;
3591        } else {
3592          die "$0: $token->{type}: Unknown token type";
3593        }
3594      } # INITIAL
3595    
3596      die "$0: _tree_construction_initial: This should be never reached";
3597  } # _tree_construction_initial  } # _tree_construction_initial
3598    
3599  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3600    my $self = shift;    my $self = shift;
3601    
3602      ## NOTE: "before html" insertion mode.
3603        
3604    B: {    B: {
3605        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3606          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3607            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3608          ## Ignore the token          ## Ignore the token
3609          ## Stay in the phase          ## Stay in the insertion mode.
3610          !!!next-token;          !!!next-token;
3611          redo B;          redo B;
3612        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3613            !!!cp ('t20');
3614          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3615          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3616          ## Stay in the phase          ## Stay in the insertion mode.
3617          !!!next-token;          !!!next-token;
3618          redo B;          redo B;
3619        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3620          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3621            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
3622            ## ISSUE: DOM3 Core does not allow Document > Text  
3623            unless (length $token->{data}) {            unless (length $token->{data}) {
3624              ## Stay in the phase              !!!cp ('t21');
3625                ## Stay in the insertion mode.
3626              !!!next-token;              !!!next-token;
3627              redo B;              redo B;
3628              } else {
3629                !!!cp ('t22');
3630            }            }
3631            } else {
3632              !!!cp ('t23');
3633          }          }
3634    
3635            $self->{application_cache_selection}->(undef);
3636    
3637          #          #
3638          } elsif ($token->{type} == START_TAG_TOKEN) {
3639            if ($token->{tag_name} eq 'html') {
3640              my $root_element;
3641              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3642              $self->{document}->append_child ($root_element);
3643              push @{$self->{open_elements}},
3644                  [$root_element, $el_category->{html}];
3645    
3646              if ($token->{attributes}->{manifest}) {
3647                !!!cp ('t24');
3648                $self->{application_cache_selection}
3649                    ->($token->{attributes}->{manifest}->{value});
3650                ## ISSUE: Spec is unclear on relative references.
3651                ## According to Hixie (#whatwg 2008-03-19), it should be
3652                ## resolved against the base URI of the document in HTML
3653                ## or xml:base of the element in XHTML.
3654              } else {
3655                !!!cp ('t25');
3656                $self->{application_cache_selection}->(undef);
3657              }
3658    
3659              !!!nack ('t25c');
3660    
3661              !!!next-token;
3662              return; ## Go to the "before head" insertion mode.
3663            } else {
3664              !!!cp ('t25.1');
3665              #
3666            }
3667        } elsif ({        } elsif ({
3668                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3669                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3670                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3671          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3672          #          #
3673        } else {        } else {
3674          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3675        }        }
3676        my $root_element; !!!create-element ($root_element, 'html');  
3677        $self->{document}->append_child ($root_element);      my $root_element;
3678        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3679        #$phase = 'main';      $self->{document}->append_child ($root_element);
3680        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3681        #redo B;  
3682        return;      $self->{application_cache_selection}->(undef);
3683    
3684        ## NOTE: Reprocess the token.
3685        !!!ack-later;
3686        return; ## Go to the "before head" insertion mode.
3687    
3688        ## ISSUE: There is an issue in the spec
3689    } # B    } # B
3690    
3691      die "$0: _tree_construction_root_element: This should never be reached";
3692  } # _tree_construction_root_element  } # _tree_construction_root_element
3693    
3694  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1783  sub _reset_insertion_mode ($) { Line 3703  sub _reset_insertion_mode ($) {
3703            
3704      ## Step 3      ## Step 3
3705      S3: {      S3: {
3706        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3707        if (defined $self->{inner_html_node}) {          $last = 1;
3708          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
3709              $self->{inner_html_node}->[1] eq 'th') {            !!!cp ('t28');
3710              $node = $self->{inner_html_node};
3711            } else {
3712              die "_reset_insertion_mode: t27";
3713            }
3714          }
3715          
3716          ## Step 4..14
3717          my $new_mode;
3718          if ($node->[1] & FOREIGN_EL) {
3719            !!!cp ('t28.1');
3720            ## NOTE: Strictly spaking, the line below only applies to MathML and
3721            ## SVG elements.  Currently the HTML syntax supports only MathML and
3722            ## SVG elements as foreigners.
3723            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3724          } elsif ($node->[1] & TABLE_CELL_EL) {
3725            if ($last) {
3726              !!!cp ('t28.2');
3727            #            #
3728          } else {          } else {
3729            $node = $self->{inner_html_node};            !!!cp ('t28.3');
3730              $new_mode = IN_CELL_IM;
3731          }          }
3732          } else {
3733            !!!cp ('t28.4');
3734            $new_mode = {
3735                          select => IN_SELECT_IM,
3736                          ## NOTE: |option| and |optgroup| do not set
3737                          ## insertion mode to "in select" by themselves.
3738                          tr => IN_ROW_IM,
3739                          tbody => IN_TABLE_BODY_IM,
3740                          thead => IN_TABLE_BODY_IM,
3741                          tfoot => IN_TABLE_BODY_IM,
3742                          caption => IN_CAPTION_IM,
3743                          colgroup => IN_COLUMN_GROUP_IM,
3744                          table => IN_TABLE_IM,
3745                          head => IN_BODY_IM, # not in head!
3746                          body => IN_BODY_IM,
3747                          frameset => IN_FRAMESET_IM,
3748                         }->{$node->[0]->manakai_local_name};
3749        }        }
       
       ## 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]};  
3750        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3751                
3752        ## Step 14        ## Step 15
3753        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3754          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3755            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3756              $self->{insertion_mode} = BEFORE_HEAD_IM;
3757          } else {          } else {
3758            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3759              !!!cp ('t30');
3760              $self->{insertion_mode} = AFTER_HEAD_IM;
3761          }          }
3762          return;          return;
3763          } else {
3764            !!!cp ('t31');
3765        }        }
3766                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3767        ## Step 16        ## Step 16
3768          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3769          
3770          ## Step 17
3771        $i--;        $i--;
3772        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3773                
3774        ## Step 17        ## Step 18
3775        redo S3;        redo S3;
3776      } # S3      } # S3
3777    
3778      die "$0: _reset_insertion_mode: This line should never be reached";
3779  } # _reset_insertion_mode  } # _reset_insertion_mode
3780    
3781  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3782    my $self = shift;    my $self = shift;
3783    
   my $phase = 'main';  
   
3784    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3785    
3786    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1854  sub _tree_construction_main ($) { Line 3797  sub _tree_construction_main ($) {
3797      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3798      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3799        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3800            !!!cp ('t32');
3801          return;          return;
3802        }        }
3803      }      }
# Line 1868  sub _tree_construction_main ($) { Line 3812  sub _tree_construction_main ($) {
3812    
3813        ## Step 6        ## Step 6
3814        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3815            !!!cp ('t33_1');
3816          #          #
3817        } else {        } else {
3818          my $in_open_elements;          my $in_open_elements;
3819          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3820            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3821                !!!cp ('t33');
3822              $in_open_elements = 1;              $in_open_elements = 1;
3823              last OE;              last OE;
3824            }            }
3825          }          }
3826          if ($in_open_elements) {          if ($in_open_elements) {
3827              !!!cp ('t34');
3828            #            #
3829          } else {          } else {
3830              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3831              !!!cp ('t35');
3832            redo S4;            redo S4;
3833          }          }
3834        }        }
# Line 1902  sub _tree_construction_main ($) { Line 3851  sub _tree_construction_main ($) {
3851    
3852        ## Step 11        ## Step 11
3853        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3854            !!!cp ('t36');
3855          ## Step 7'          ## Step 7'
3856          $i++;          $i++;
3857          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3858                    
3859          redo S7;          redo S7;
3860        }        }
3861    
3862          !!!cp ('t37');
3863      } # S7      } # S7
3864    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3865    
3866    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3867      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3868        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3869            !!!cp ('t38');
3870          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3871          return;          return;
3872        }        }
3873      }      }
3874    
3875        !!!cp ('t39');
3876    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3877    
3878    my $style_start_tag = sub {    my $insert;
3879      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});  
3880      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
3881      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
3882       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
3883        ->append_child ($style_el);      ## Step 1
3884      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
3885                      my $el;
3886        !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3887    
3888        ## Step 2
3889        $insert->($el);
3890    
3891        ## Step 3
3892        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3893        delete $self->{escape}; # MUST
3894    
3895        ## Step 4
3896      my $text = '';      my $text = '';
3897        !!!nack ('t40.1');
3898      !!!next-token;      !!!next-token;
3899      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3900          !!!cp ('t40');
3901        $text .= $token->{data};        $text .= $token->{data};
3902        !!!next-token;        !!!next-token;
3903      } # stop if non-character token or tokenizer stops tokenising      }
3904    
3905        ## Step 5
3906      if (length $text) {      if (length $text) {
3907        $style_el->manakai_append_text ($text);        !!!cp ('t41');
3908          my $text = $self->{document}->create_text_node ($text);
3909          $el->append_child ($text);
3910      }      }
3911        
3912      $self->{content_model_flag} = 'PCDATA';      ## Step 6
3913                      $self->{content_model} = PCDATA_CONTENT_MODEL;
3914      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
3915        ## Step 7
3916        if ($token->{type} == END_TAG_TOKEN and
3917            $token->{tag_name} eq $start_tag_name) {
3918          !!!cp ('t42');
3919        ## Ignore the token        ## Ignore the token
3920      } else {      } else {
3921        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
3922        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3923            !!!cp ('t43');
3924            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3925          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3926            !!!cp ('t44');
3927            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3928          } else {
3929            die "$0: $content_model_flag in parse_rcdata";
3930          }
3931      }      }
3932      !!!next-token;      !!!next-token;
3933    }; # $style_start_tag    }; # $parse_rcdata
3934    
3935    my $script_start_tag = sub {    my $script_start_tag = sub () {
3936      my $script_el;      my $script_el;
3937      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3938      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3939    
3940      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3941        delete $self->{escape}; # MUST
3942            
3943      my $text = '';      my $text = '';
3944        !!!nack ('t45.1');
3945      !!!next-token;      !!!next-token;
3946      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3947          !!!cp ('t45');
3948        $text .= $token->{data};        $text .= $token->{data};
3949        !!!next-token;        !!!next-token;
3950      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3951      if (length $text) {      if (length $text) {
3952          !!!cp ('t46');
3953        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3954      }      }
3955                                
3956      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3957    
3958      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3959          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3960          !!!cp ('t47');
3961        ## Ignore the token        ## Ignore the token
3962      } else {      } else {
3963        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3964          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3965        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3966        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3967      }      }
3968            
3969      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3970          !!!cp ('t49');
3971        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3972      } else {      } else {
3973          !!!cp ('t50');
3974        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3975        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3976          
3977        (($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);  
3978                
3979        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
3980                
# Line 1994  sub _tree_construction_main ($) { Line 3984  sub _tree_construction_main ($) {
3984      !!!next-token;      !!!next-token;
3985    }; # $script_start_tag    }; # $script_start_tag
3986    
3987      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3988      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3989      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3990    
3991    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3992      my $tag_name = shift;      my $end_tag_token = shift;
3993        my $tag_name = $end_tag_token->{tag_name};
3994    
3995        ## NOTE: The adoption agency algorithm (AAA).
3996    
3997      FET: {      FET: {
3998        ## Step 1        ## Step 1
3999        my $formatting_element;        my $formatting_element;
4000        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4001        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4002          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4003              !!!cp ('t52');
4004              last AFE;
4005            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4006                         eq $tag_name) {
4007              !!!cp ('t51');
4008            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4009            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4010            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
4011          }          }
4012        } # AFE        } # AFE
4013        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4014          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
4015            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4016          ## Ignore the token          ## Ignore the token
4017          !!!next-token;          !!!next-token;
4018          return;          return;
# Line 2023  sub _tree_construction_main ($) { Line 4024  sub _tree_construction_main ($) {
4024          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4025          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
4026            if ($in_scope) {            if ($in_scope) {
4027                !!!cp ('t54');
4028              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
4029              last INSCOPE;              last INSCOPE;
4030            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4031              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
4032                !!!parse-error (type => 'unmatched end tag',
4033                                text => $token->{tag_name},
4034                                token => $end_tag_token);
4035              ## Ignore the token              ## Ignore the token
4036              !!!next-token;              !!!next-token;
4037              return;              return;
4038            }            }
4039          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
4040                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4041            $in_scope = 0;            $in_scope = 0;
4042          }          }
4043        } # INSCOPE        } # INSCOPE
4044        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4045          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
4046            !!!parse-error (type => 'unmatched end tag',
4047                            text => $token->{tag_name},
4048                            token => $end_tag_token);
4049          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4050          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4051          return;          return;
4052        }        }
4053        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4054          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
4055            !!!parse-error (type => 'not closed',
4056                            text => $self->{open_elements}->[-1]->[0]
4057                                ->manakai_local_name,
4058                            token => $end_tag_token);
4059        }        }
4060                
4061        ## Step 2        ## Step 2
# Line 2053  sub _tree_construction_main ($) { Line 4063  sub _tree_construction_main ($) {
4063        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4064        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4065          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4066          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4067              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4068              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4069               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4070              !!!cp ('t59');
4071            $furthest_block = $node;            $furthest_block = $node;
4072            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
4073          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
4074              !!!cp ('t60');
4075            last OE;            last OE;
4076          }          }
4077        } # OE        } # OE
4078                
4079        ## Step 3        ## Step 3
4080        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
4081            !!!cp ('t61');
4082          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
4083          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
4084          !!!next-token;          !!!next-token;
# Line 2078  sub _tree_construction_main ($) { Line 4091  sub _tree_construction_main ($) {
4091        ## Step 5        ## Step 5
4092        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
4093        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
4094            !!!cp ('t62');
4095          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
4096        }        }
4097                
# Line 2100  sub _tree_construction_main ($) { Line 4114  sub _tree_construction_main ($) {
4114          S7S2: {          S7S2: {
4115            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
4116              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4117                  !!!cp ('t63');
4118                $node_i_in_active = $_;                $node_i_in_active = $_;
4119                last S7S2;                last S7S2;
4120              }              }
# Line 2113  sub _tree_construction_main ($) { Line 4128  sub _tree_construction_main ($) {
4128                    
4129          ## Step 4          ## Step 4
4130          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
4131              !!!cp ('t64');
4132            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
4133          }          }
4134                    
4135          ## Step 5          ## Step 5
4136          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
4137              !!!cp ('t65');
4138            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
4139            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
4140            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2135  sub _tree_construction_main ($) { Line 4152  sub _tree_construction_main ($) {
4152        } # S7          } # S7  
4153                
4154        ## Step 8        ## Step 8
4155        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4156            my $foster_parent_element;
4157            my $next_sibling;
4158            OE: for (reverse 0..$#{$self->{open_elements}}) {
4159              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4160                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4161                                 if (defined $parent and $parent->node_type == 1) {
4162                                   !!!cp ('t65.1');
4163                                   $foster_parent_element = $parent;
4164                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4165                                 } else {
4166                                   !!!cp ('t65.2');
4167                                   $foster_parent_element
4168                                     = $self->{open_elements}->[$_ - 1]->[0];
4169                                 }
4170                                 last OE;
4171                               }
4172                             } # OE
4173                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4174                               unless defined $foster_parent_element;
4175            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4176            $open_tables->[-1]->[1] = 1; # tainted
4177          } else {
4178            !!!cp ('t65.3');
4179            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4180          }
4181                
4182        ## Step 9        ## Step 9
4183        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2152  sub _tree_construction_main ($) { Line 4194  sub _tree_construction_main ($) {
4194        my $i;        my $i;
4195        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4196          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4197              !!!cp ('t66');
4198            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
4199            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
4200          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4201              !!!cp ('t67');
4202            $i = $_;            $i = $_;
4203          }          }
4204        } # AFE        } # AFE
# Line 2164  sub _tree_construction_main ($) { Line 4208  sub _tree_construction_main ($) {
4208        undef $i;        undef $i;
4209        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4210          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4211              !!!cp ('t68');
4212            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4213            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4214          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4215              !!!cp ('t69');
4216            $i = $_;            $i = $_;
4217          }          }
4218        } # OE        } # OE
# Line 2177  sub _tree_construction_main ($) { Line 4223  sub _tree_construction_main ($) {
4223      } # FET      } # FET
4224    }; # $formatting_end_tag    }; # $formatting_end_tag
4225    
4226    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4227      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4228    }; # $insert_to_current    }; # $insert_to_current
4229    
4230    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4231                         my $child = shift;      my $child = shift;
4232                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4233                              table => 1, tbody => 1, tfoot => 1,        # MUST
4234                              thead => 1, tr => 1,        my $foster_parent_element;
4235                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4236                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4237                           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') {  
4238                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4239                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4240                                   !!!cp ('t70');
4241                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4242                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4243                               } else {                               } else {
4244                                   !!!cp ('t71');
4245                                 $foster_parent_element                                 $foster_parent_element
4246                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4247                               }                               }
# Line 2207  sub _tree_construction_main ($) { Line 4252  sub _tree_construction_main ($) {
4252                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4253                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4254                             ($child, $next_sibling);                             ($child, $next_sibling);
4255                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4256                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4257                         }        !!!cp ('t72');
4258          $self->{open_elements}->[-1]->[0]->append_child ($child);
4259        }
4260    }; # $insert_to_foster    }; # $insert_to_foster
4261    
4262    my $in_body = sub {    B: while (1) {
4263      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
4264      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
4265        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4266          $script_start_tag->();        ## Ignore the token
4267          return;        ## Stay in the phase
4268        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
4269          $style_start_tag->();        next B;
4270          return;      } elsif ($token->{type} == START_TAG_TOKEN and
4271        } elsif ({               $token->{tag_name} eq 'html') {
4272                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4273                 }->{$token->{tag_name}}) {          !!!cp ('t79');
4274          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html', text => 'html', token => $token);
4275          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
4276          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4277          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
4278          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html', text => 'html', token => $token);
4279            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4280          } else {        } else {
4281            $insert->($el);          !!!cp ('t81');
4282          }        }
4283            
4284          !!!next-token;        !!!cp ('t82');
4285          return;        !!!parse-error (type => 'not first start tag', token => $token);
4286        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
4287          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
4288          ## NOTE: There is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4289          my $title_el;            !!!cp ('t84');
4290          !!!create-element ($title_el, 'title', $token->{attributes});            $top_el->set_attribute_ns
4291          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              (undef, [undef, $attr_name],
4292            ->append_child ($title_el);               $token->{attributes}->{$attr_name}->{value});
         $self->{content_model_flag} = 'RCDATA';  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
4293          }          }
4294                    }
4295          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!nack ('t84.1');
4296                    !!!next-token;
4297          next B;
4298        } elsif ($token->{type} == COMMENT_TOKEN) {
4299          my $comment = $self->{document}->create_comment ($token->{data});
4300          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4301            !!!cp ('t85');
4302            $self->{document}->append_child ($comment);
4303          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4304            !!!cp ('t86');
4305            $self->{open_elements}->[0]->[0]->append_child ($comment);
4306          } else {
4307            !!!cp ('t87');
4308            $self->{open_elements}->[-1]->[0]->append_child ($comment);
4309          }
4310          !!!next-token;
4311          next B;
4312        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4313          if ($token->{type} == CHARACTER_TOKEN) {
4314            !!!cp ('t87.1');
4315            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4316          !!!next-token;          !!!next-token;
4317          return;          next B;
4318        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4319          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4320            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4321            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4322              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4323                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4324              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4325              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4326              $formatting_end_tag->($token->{tag_name});            #
4327                        } elsif ({
4328              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4329                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4330                  splice @$active_formatting_elements, $_, 1;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4331                  last AFE2;                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4332                }                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4333              } # AFE2                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4334              OE: for (reverse 0..$#{$self->{open_elements}}) {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4335                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4336                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4337                  last OE;            !!!cp ('t87.2');
4338                }            !!!parse-error (type => 'not closed',
4339              } # OE                            text => $self->{open_elements}->[-1]->[0]
4340              last AFE;                                ->manakai_local_name,
4341            } elsif ($node->[0] eq '#marker') {                            token => $token);
4342              last AFE;  
4343              pop @{$self->{open_elements}}
4344                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4345    
4346              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4347              ## Reprocess.
4348              next B;
4349            } else {
4350              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4351              my $tag_name = $token->{tag_name};
4352              if ($nsuri eq $SVG_NS) {
4353                $tag_name = {
4354                   altglyph => 'altGlyph',
4355                   altglyphdef => 'altGlyphDef',
4356                   altglyphitem => 'altGlyphItem',
4357                   animatecolor => 'animateColor',
4358                   animatemotion => 'animateMotion',
4359                   animatetransform => 'animateTransform',
4360                   clippath => 'clipPath',
4361                   feblend => 'feBlend',
4362                   fecolormatrix => 'feColorMatrix',
4363                   fecomponenttransfer => 'feComponentTransfer',
4364                   fecomposite => 'feComposite',
4365                   feconvolvematrix => 'feConvolveMatrix',
4366                   fediffuselighting => 'feDiffuseLighting',
4367                   fedisplacementmap => 'feDisplacementMap',
4368                   fedistantlight => 'feDistantLight',
4369                   feflood => 'feFlood',
4370                   fefunca => 'feFuncA',
4371                   fefuncb => 'feFuncB',
4372                   fefuncg => 'feFuncG',
4373                   fefuncr => 'feFuncR',
4374                   fegaussianblur => 'feGaussianBlur',
4375                   feimage => 'feImage',
4376                   femerge => 'feMerge',
4377                   femergenode => 'feMergeNode',
4378                   femorphology => 'feMorphology',
4379                   feoffset => 'feOffset',
4380                   fepointlight => 'fePointLight',
4381                   fespecularlighting => 'feSpecularLighting',
4382                   fespotlight => 'feSpotLight',
4383                   fetile => 'feTile',
4384                   feturbulence => 'feTurbulence',
4385                   foreignobject => 'foreignObject',
4386                   glyphref => 'glyphRef',
4387                   lineargradient => 'linearGradient',
4388                   radialgradient => 'radialGradient',
4389                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4390                   textpath => 'textPath',  
4391                }->{$tag_name} || $tag_name;
4392            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4393    
4394          !!!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];  
4395    
4396          !!!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', ''];  
4397    
4398          !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4399          return;  
4400        } elsif ($token->{tag_name} eq 'marquee' or            if ($self->{self_closing}) {
4401                 $token->{tag_name} eq 'object') {              pop @{$self->{open_elements}};
4402          $reconstruct_active_formatting_elements->($insert_to_current);              !!!ack ('t87.3');
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
           
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                         {type => 'character',  
                          data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD  
                         ## TODO: make this configurable  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'},  
                        );  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ({  
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: $self->{form_element} if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
           
         $insert->($el);  
           
         my $text = '';  
         if ($token->{tag_name} eq 'textarea') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
4403            } else {            } else {
4404              !!!parse-error (type => 'in CDATA:#'.$token->{type});              !!!cp ('t87.4');
4405            }            }
4406            ## ISSUE: And ignore?  
4407              !!!next-token;
4408              next B;
4409          }          }
4410          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4411          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4412        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
4413          $reconstruct_active_formatting_elements->($insert_to_current);          #
4414                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4415          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t87.6');
4416                    !!!parse-error (type => 'not closed',
4417          $self->{insertion_mode} = 'in select';                          text => $self->{open_elements}->[-1]->[0]
4418          !!!next-token;                              ->manakai_local_name,
4419          return;                          token => $token);
4420        } elsif ({  
4421                  caption => 1, col => 1, colgroup => 1, frame => 1,          pop @{$self->{open_elements}}
4422                  frameset => 1, head => 1, option => 1, optgroup => 1,              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4423                  tbody => 1, td => 1, tfoot => 1, th => 1,  
4424                  thead => 1, tr => 1,          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4425                 }->{$token->{tag_name}}) {          ## Reprocess.
4426          !!!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.  
4427        } else {        } else {
4428          $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;  
4429        }        }
4430      } elsif ($token->{type} eq 'end tag') {      }
4431        if ($token->{tag_name} eq 'body') {  
4432          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      if ($self->{insertion_mode} & HEAD_IMS) {
4433            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
4434            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4435              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4436            }              !!!cp ('t88.2');
4437            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4438            !!!next-token;            } else {
4439            return;              !!!cp ('t88.1');
4440          } else {              ## Ignore the token.
4441            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
4442            ## 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;  
4443            }            }
4444          } # INSCOPE            unless (length $token->{data}) {
4445                        !!!cp ('t88');
4446          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
4447            !!!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;  
4448            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4449          }          }
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
 ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
4450    
4451              !!!next-token;          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4452              last S2;            !!!cp ('t89');
4453            } else {            ## As if <head>
4454              ## Step 3            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4455              if (not $formatting_category->{$node->[1]} and            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4456                  #not $phrasing_category->{$node->[1]} and            push @{$self->{open_elements}},
4457                  ($special_category->{$node->[1]} or                [$self->{head_element}, $el_category->{head}];
4458                   $scoping_category->{$node->[1]})) {  
4459                !!!parse-error (type => 'not closed:'.$node->[1]);            ## Reprocess in the "in head" insertion mode...
4460                ## Ignore the token            pop @{$self->{open_elements}};
4461                !!!next-token;  
4462                last S2;            ## Reprocess in the "after head" insertion mode...
4463              }          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4464            }            !!!cp ('t90');
4465              ## As if </noscript>
4466              pop @{$self->{open_elements}};
4467              !!!parse-error (type => 'in noscript:#text', token => $token);
4468                        
4469            ## Step 4            ## Reprocess in the "in head" insertion mode...
4470            $node_i--;            ## As if </head>
4471            $node = $self->{open_elements}->[$node_i];            pop @{$self->{open_elements}};
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
4472    
4473    B: {            ## Reprocess in the "after head" insertion mode...
4474      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4475        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
4476          !!!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]);  
         }  
4477    
4478          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4479          last B;          } else {
4480              !!!cp ('t92');
4481            }
4482    
4483          ## ISSUE: There is an issue in the spec.          ## "after head" insertion mode
4484        } else {          ## As if <body>
4485          if ($self->{insertion_mode} eq 'before head') {          !!!insert-element ('body',, $token);
4486            if ($token->{type} eq 'character') {          $self->{insertion_mode} = IN_BODY_IM;
4487              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## reprocess
4488                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          next B;
4489                unless (length $token->{data}) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4490                  !!!next-token;          if ($token->{tag_name} eq 'head') {
4491                  redo B;            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4492                }              !!!cp ('t93');
4493              }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4494              ## As if <head>              $self->{open_elements}->[-1]->[0]->append_child
4495              !!!create-element ($self->{head_element}, 'head');                  ($self->{head_element});
4496              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              push @{$self->{open_elements}},
4497              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  [$self->{head_element}, $el_category->{head}];
4498              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = IN_HEAD_IM;
4499              ## 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);  
4500              !!!next-token;              !!!next-token;
4501              redo B;              next B;
4502            } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4503              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              !!!cp ('t93.2');
4504              !!!create-element ($self->{head_element}, 'head', $attr);              !!!parse-error (type => 'after head', text => 'head',
4505              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token);
4506              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4507              $self->{insertion_mode} = 'in head';              !!!nack ('t93.3');
4508              if ($token->{tag_name} eq 'head') {              !!!next-token;
4509                !!!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;  
             }  
4510            } else {            } else {
4511              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4512            }              !!!parse-error (type => 'in head:head',
4513          } elsif ($self->{insertion_mode} eq 'in head') {                              token => $token); # or in head noscript
4514            if ($token->{type} eq 'character') {              ## Ignore the token
4515              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);  
4516              !!!next-token;              !!!next-token;
4517              redo B;              next B;
4518            } elsif ($token->{type} eq 'start tag') {            }
4519              if ($token->{tag_name} eq 'title') {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4520                ## NOTE: There is an "as if in head" code clone            !!!cp ('t96');
4521                my $title_el;            ## As if <head>
4522                !!!create-element ($title_el, 'title', $token->{attributes});            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4523                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4524                  ->append_child ($title_el);            push @{$self->{open_elements}},
4525                $self->{content_model_flag} = 'RCDATA';                [$self->{head_element}, $el_category->{head}];
4526    
4527                my $text = '';            $self->{insertion_mode} = IN_HEAD_IM;
4528                !!!next-token;            ## Reprocess in the "in head" insertion mode...
4529                while ($token->{type} eq 'character') {          } else {
4530                  $text .= $token->{data};            !!!cp ('t97');
4531                  !!!next-token;          }
4532                }  
4533                if (length $text) {              if ($token->{tag_name} eq 'base') {
4534                  $title_el->manakai_append_text ($text);                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4535                }                  !!!cp ('t98');
4536                                  ## As if </noscript>
4537                $self->{content_model_flag} = 'PCDATA';                  pop @{$self->{open_elements}};
4538                    !!!parse-error (type => 'in noscript', text => 'base',
4539                                    token => $token);
4540                                
4541                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
4542                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
4543                } else {                } else {
4544                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
4545                }                }
               !!!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);  
4546    
4547                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
4548                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4549              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
4550                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head',
4551                ## Ignore the token                                  text => $token->{tag_name}, token => $token);
4552                !!!next-token;                  push @{$self->{open_elements}},
4553                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}};  
4554                } else {                } else {
4555                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
4556                }                }
4557                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4558                !!!next-token;                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4559                redo B;                pop @{$self->{open_elements}} # <head>
4560              } elsif ($token->{tag_name} eq 'html') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4561                #                !!!nack ('t101.1');
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
4562                !!!next-token;                !!!next-token;
4563                redo B;                next B;
4564              }              } elsif ($token->{tag_name} eq 'link') {
4565            } else {                ## NOTE: There is a "as if in head" code clone.
4566              #                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4567            }                  !!!cp ('t102');
4568                    !!!parse-error (type => 'after head',
4569            if ($self->{open_elements}->[-1]->[1] eq 'head') {                                  text => $token->{tag_name}, token => $token);
4570              ## As if </head>                  push @{$self->{open_elements}},
4571              pop @{$self->{open_elements}};                      [$self->{head_element}, $el_category->{head}];
4572            }                } else {
4573            $self->{insertion_mode} = 'after head';                  !!!cp ('t103');
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
4574                }                }
4575              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4576                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4577              #                pop @{$self->{open_elements}} # <head>
4578            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4579              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';  
4580                !!!next-token;                !!!next-token;
4581                redo B;                next B;
4582              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
4583                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
4584                        script => 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4585                       }->{$token->{tag_name}}) {                  !!!cp ('t104');
4586                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4587                $self->{insertion_mode} = 'in head';                                  text => $token->{tag_name}, token => $token);
4588                ## reprocess                  push @{$self->{open_elements}},
4589                redo B;                      [$self->{head_element}, $el_category->{head}];
4590              } else {                } else {
4591                #                  !!!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;  
4592                }                }
4593              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4594                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
             !!!parse-error (type => 'in table:#character');  
4595    
4596              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
4597              ## ISSUE: Spec says that "whenever a node would be inserted                  if ($token->{attributes}->{charset}) {
4598              ## into the current node" while characters might not be                    !!!cp ('t106');
4599              ## result in a new Text node.                    ## NOTE: Whether the encoding is supported or not is handled
4600              $reconstruct_active_formatting_elements->($insert_to_foster);                    ## in the {change_encoding} callback.
4601                                  $self->{change_encoding}
4602              if ({                        ->($self, $token->{attributes}->{charset}->{value},
4603                   table => 1, tbody => 1, tfoot => 1,                           $token);
4604                   thead => 1, tr => 1,                    
4605                  }->{$self->{open_elements}->[-1]->[1]}) {                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4606                # MUST                        ->set_user_data (manakai_has_reference =>
4607                my $foster_parent_element;                                             $token->{attributes}->{charset}
4608                my $next_sibling;                                                 ->{has_reference});
4609                my $prev_sibling;                  } elsif ($token->{attributes}->{content}) {
4610                OE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($token->{attributes}->{content}->{value}
4611                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4612                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            [\x09-\x0D\x20]*=
4613                    if (defined $parent and $parent->node_type == 1) {                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4614                      $foster_parent_element = $parent;                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4615                      $next_sibling = $self->{open_elements}->[$_]->[0];                      !!!cp ('t107');
4616                      $prev_sibling = $next_sibling->previous_sibling;                      ## NOTE: Whether the encoding is supported or not is handled
4617                        ## in the {change_encoding} callback.
4618                        $self->{change_encoding}
4619                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4620                               $token);
4621                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4622                            ->set_user_data (manakai_has_reference =>
4623                                                 $token->{attributes}->{content}
4624                                                       ->{has_reference});
4625                    } else {                    } else {
4626                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
4627                    }                    }
                   last OE;  
4628                  }                  }
               } # 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});  
4629                } else {                } else {
4630                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
4631                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
4632                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4633                }                        ->set_user_data (manakai_has_reference =>
4634              } else {                                             $token->{attributes}->{charset}
4635                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
4636              }                  }
4637                                if ($token->{attributes}->{content}) {
4638              !!!next-token;                    !!!cp ('t110');
4639              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4640            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
4641              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
4642              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
4643              !!!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}};  
4644                }                }
4645    
4646                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
4647                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4648                  !!!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}};  
4649                !!!next-token;                !!!next-token;
4650                redo B;                next B;
4651              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
4652                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4653                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
4654                       }->{$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]);  
4655                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4656                    !!!parse-error (type => 'in noscript', text => 'title',
4657                                    token => $token);
4658                  
4659                    $self->{insertion_mode} = IN_HEAD_IM;
4660                    ## Reprocess in the "in head" insertion mode...
4661                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4662                    !!!cp ('t112');
4663                    !!!parse-error (type => 'after head',
4664                                    text => $token->{tag_name}, token => $token);
4665                    push @{$self->{open_elements}},
4666                        [$self->{head_element}, $el_category->{head}];
4667                  } else {
4668                    !!!cp ('t113');
4669                }                }
4670    
4671                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
4672                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
4673                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
4674                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4675                redo B;                pop @{$self->{open_elements}} # <head>
4676              } elsif ($token->{tag_name} eq 'table') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4677                ## NOTE: There are code clones for this "table in table"                next B;
4678                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } elsif ($token->{tag_name} eq 'style' or
4679                         $token->{tag_name} eq 'noframes') {
4680                ## As if </table>                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4681                ## have a table element in table scope                ## insertion mode IN_HEAD_IM)
4682                my $i;                ## NOTE: There is a "as if in head" code clone.
4683                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4684                  my $node = $self->{open_elements}->[$_];                  !!!cp ('t114');
4685                  if ($node->[1] eq 'table') {                  !!!parse-error (type => 'after head',
4686                    $i = $_;                                  text => $token->{tag_name}, token => $token);
4687                    last INSCOPE;                  push @{$self->{open_elements}},
4688                  } elsif ({                      [$self->{head_element}, $el_category->{head}];
4689                            table => 1, html => 1,                } else {
4690                           }->{$node->[1]}) {                  !!!cp ('t115');
4691                    last INSCOPE;                }
4692                  }                $parse_rcdata->(CDATA_CONTENT_MODEL);
4693                } # INSCOPE                pop @{$self->{open_elements}} # <head>
4694                unless (defined $i) {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4695                  !!!parse-error (type => 'unmatched end tag:table');                next B;
4696                  ## Ignore tokens </table><table>              } elsif ($token->{tag_name} eq 'noscript') {
4697                  if ($self->{insertion_mode} == IN_HEAD_IM) {
4698                    !!!cp ('t116');
4699                    ## NOTE: and scripting is disalbed
4700                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4701                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4702                    !!!nack ('t116.1');
4703                    !!!next-token;
4704                    next B;
4705                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4706                    !!!cp ('t117');
4707                    !!!parse-error (type => 'in noscript', text => 'noscript',
4708                                    token => $token);
4709                    ## Ignore the token
4710                    !!!nack ('t117.1');
4711                  !!!next-token;                  !!!next-token;
4712                  redo B;                  next B;
4713                  } else {
4714                    !!!cp ('t118');
4715                    #
4716                }                }
4717                } elsif ($token->{tag_name} eq 'script') {
4718                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4719                    !!!cp ('t119');
4720                    ## As if </noscript>
4721                    pop @{$self->{open_elements}};
4722                    !!!parse-error (type => 'in noscript', text => 'script',
4723                                    token => $token);
4724                                
4725                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
4726                if ({                  ## Reprocess in the "in head" insertion mode...
4727                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4728                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
4729                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head',
4730                  !!!back-token; # <table>                                  text => $token->{tag_name}, token => $token);
4731                  $token = {type => 'end tag', tag_name => 'table'};                  push @{$self->{open_elements}},
4732                  !!!back-token;                      [$self->{head_element}, $el_category->{head}];
4733                  $token = {type => 'end tag',                } else {
4734                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!cp ('t121');
                 redo B;  
4735                }                }
4736    
4737                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
4738                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
4739                  pop @{$self->{open_elements}} # <head>
4740                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4741                  next B;
4742                } elsif ($token->{tag_name} eq 'body' or
4743                         $token->{tag_name} eq 'frameset') {
4744                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4745                    !!!cp ('t122');
4746                    ## As if </noscript>
4747                    pop @{$self->{open_elements}};
4748                    !!!parse-error (type => 'in noscript',
4749                                    text => $token->{tag_name}, token => $token);
4750                    
4751                    ## Reprocess in the "in head" insertion mode...
4752                    ## As if </head>
4753                    pop @{$self->{open_elements}};
4754                    
4755                    ## Reprocess in the "after head" insertion mode...
4756                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4757                    !!!cp ('t124');
4758                    pop @{$self->{open_elements}};
4759                    
4760                    ## Reprocess in the "after head" insertion mode...
4761                  } else {
4762                    !!!cp ('t125');
4763                }                }
4764    
4765                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
4766                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4767                  if ($token->{tag_name} eq 'body') {
4768                    !!!cp ('t126');
4769                    $self->{insertion_mode} = IN_BODY_IM;
4770                  } elsif ($token->{tag_name} eq 'frameset') {
4771                    !!!cp ('t127');
4772                    $self->{insertion_mode} = IN_FRAMESET_IM;
4773                  } else {
4774                    die "$0: tag name: $self->{tag_name}";
4775                  }
4776                  !!!nack ('t127.1');
4777                  !!!next-token;
4778                  next B;
4779                } else {
4780                  !!!cp ('t128');
4781                  #
4782                }
4783    
4784                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4785                  !!!cp ('t129');
4786                  ## As if </noscript>
4787                  pop @{$self->{open_elements}};
4788                  !!!parse-error (type => 'in noscript:/',
4789                                  text => $token->{tag_name}, token => $token);
4790                  
4791                  ## Reprocess in the "in head" insertion mode...
4792                  ## As if </head>
4793                  pop @{$self->{open_elements}};
4794    
4795                ## reprocess                ## Reprocess in the "after head" insertion mode...
4796                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4797                  !!!cp ('t130');
4798                  ## As if </head>
4799                  pop @{$self->{open_elements}};
4800    
4801                  ## Reprocess in the "after head" insertion mode...
4802              } else {              } else {
4803                #                !!!cp ('t131');
4804              }              }
4805            } elsif ($token->{type} eq 'end tag') {  
4806              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
4807                ## have a table element in table scope              ## As if <body>
4808                my $i;              !!!insert-element ('body',, $token);
4809                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
4810                  my $node = $self->{open_elements}->[$_];              ## reprocess
4811                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
4812                    $i = $_;              next B;
4813                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
4814                  } elsif ({              if ($token->{tag_name} eq 'head') {
4815                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4816                           }->{$node->[1]}) {                  !!!cp ('t132');
4817                    last INSCOPE;                  ## As if <head>
4818                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4819                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4820                unless (defined $i) {                  push @{$self->{open_elements}},
4821                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
4822    
4823                    ## Reprocess in the "in head" insertion mode...
4824                    pop @{$self->{open_elements}};
4825                    $self->{insertion_mode} = AFTER_HEAD_IM;
4826                    !!!next-token;
4827                    next B;
4828                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4829                    !!!cp ('t133');
4830                    ## As if </noscript>
4831                    pop @{$self->{open_elements}};
4832                    !!!parse-error (type => 'in noscript:/',
4833                                    text => 'head', token => $token);
4834                    
4835                    ## Reprocess in the "in head" insertion mode...
4836                    pop @{$self->{open_elements}};
4837                    $self->{insertion_mode} = AFTER_HEAD_IM;
4838                    !!!next-token;
4839                    next B;
4840                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4841                    !!!cp ('t134');
4842                    pop @{$self->{open_elements}};
4843                    $self->{insertion_mode} = AFTER_HEAD_IM;
4844                    !!!next-token;
4845                    next B;
4846                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4847                    !!!cp ('t134.1');
4848                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4849                                    token => $token);
4850                  ## Ignore the token                  ## Ignore the token
4851                  !!!next-token;                  !!!next-token;
4852                  redo B;                  next B;
4853                  } else {
4854                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4855                }                }
4856                              } elsif ($token->{tag_name} eq 'noscript') {
4857                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4858                if ({                  !!!cp ('t136');
4859                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}};
4860                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
4861                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!next-token;
4862                  !!!back-token;                  next B;
4863                  $token = {type => 'end tag',                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4864                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4865                  redo B;                  !!!cp ('t137');
4866                    !!!parse-error (type => 'unmatched end tag',
4867                                    text => 'noscript', token => $token);
4868                    ## Ignore the token ## ISSUE: An issue in the spec.
4869                    !!!next-token;
4870                    next B;
4871                  } else {
4872                    !!!cp ('t138');
4873                    #
4874                }                }
4875                } elsif ({
4876                if ($self->{open_elements}->[-1]->[1] ne 'table') {                        body => 1, html => 1,
4877                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                       }->{$token->{tag_name}}) {
4878                  if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4879                      $self->{insertion_mode} == IN_HEAD_IM or
4880                      $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4881                    !!!cp ('t140');
4882                    !!!parse-error (type => 'unmatched end tag',
4883                                    text => $token->{tag_name}, token => $token);
4884                    ## Ignore the token
4885                    !!!next-token;
4886                    next B;
4887                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4888                    !!!cp ('t140.1');
4889                    !!!parse-error (type => 'unmatched end tag',
4890                                    text => $token->{tag_name}, token => $token);
4891                    ## Ignore the token
4892                    !!!next-token;
4893                    next B;
4894                  } else {
4895                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4896                }                }
4897                } elsif ($token->{tag_name} eq 'p') {
4898                  !!!cp ('t142');
4899                  !!!parse-error (type => 'unmatched end tag',
4900                                  text => $token->{tag_name}, token => $token);
4901                  ## Ignore the token
4902                  !!!next-token;
4903                  next B;
4904                } elsif ($token->{tag_name} eq 'br') {
4905                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4906                    !!!cp ('t142.2');
4907                    ## (before head) as if <head>, (in head) as if </head>
4908                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4909                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4910                    $self->{insertion_mode} = AFTER_HEAD_IM;
4911      
4912                    ## Reprocess in the "after head" insertion mode...
4913                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4914                    !!!cp ('t143.2');
4915                    ## As if </head>
4916                    pop @{$self->{open_elements}};
4917                    $self->{insertion_mode} = AFTER_HEAD_IM;
4918      
4919                    ## Reprocess in the "after head" insertion mode...
4920                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4921                    !!!cp ('t143.3');
4922                    ## ISSUE: Two parse errors for <head><noscript></br>
4923                    !!!parse-error (type => 'unmatched end tag',
4924                                    text => 'br', token => $token);
4925                    ## As if </noscript>
4926                    pop @{$self->{open_elements}};
4927                    $self->{insertion_mode} = IN_HEAD_IM;
4928    
4929                splice @{$self->{open_elements}}, $i;                  ## Reprocess in the "in head" insertion mode...
4930                    ## As if </head>
4931                    pop @{$self->{open_elements}};
4932                    $self->{insertion_mode} = AFTER_HEAD_IM;
4933    
4934                $self->_reset_insertion_mode;                  ## Reprocess in the "after head" insertion mode...
4935                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4936                    !!!cp ('t143.4');
4937                    #
4938                  } else {
4939                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4940                  }
4941    
4942                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4943                  !!!parse-error (type => 'unmatched end tag',
4944                                  text => 'br', token => $token);
4945                  ## Ignore the token
4946                !!!next-token;                !!!next-token;
4947                redo B;                next B;
4948              } elsif ({              } else {
4949                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t145');
4950                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                !!!parse-error (type => 'unmatched end tag',
4951                        thead => 1, tr => 1,                                text => $token->{tag_name}, token => $token);
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4952                ## Ignore the token                ## Ignore the token
4953                !!!next-token;                !!!next-token;
4954                redo B;                next B;
4955                }
4956    
4957                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4958                  !!!cp ('t146');
4959                  ## As if </noscript>
4960                  pop @{$self->{open_elements}};
4961                  !!!parse-error (type => 'in noscript:/',
4962                                  text => $token->{tag_name}, token => $token);
4963                  
4964                  ## Reprocess in the "in head" insertion mode...
4965                  ## As if </head>
4966                  pop @{$self->{open_elements}};
4967    
4968                  ## Reprocess in the "after head" insertion mode...
4969                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4970                  !!!cp ('t147');
4971                  ## As if </head>
4972                  pop @{$self->{open_elements}};
4973    
4974                  ## Reprocess in the "after head" insertion mode...
4975                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4976    ## ISSUE: This case cannot be reached?
4977                  !!!cp ('t148');
4978                  !!!parse-error (type => 'unmatched end tag',
4979                                  text => $token->{tag_name}, token => $token);
4980                  ## Ignore the token ## ISSUE: An issue in the spec.
4981                  !!!next-token;
4982                  next B;
4983              } else {              } else {
4984                #                !!!cp ('t149');
4985              }              }
           } else {  
             #  
           }  
4986    
4987            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
4988            $in_body->($insert_to_foster);              ## As if <body>
4989            redo B;              !!!insert-element ('body',, $token);
4990          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
4991            if ($token->{type} eq 'character') {              ## reprocess
4992              ## NOTE: This is a code clone of "character in body".              next B;
4993          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4994            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4995              !!!cp ('t149.1');
4996    
4997              ## NOTE: As if <head>
4998              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4999              $self->{open_elements}->[-1]->[0]->append_child
5000                  ($self->{head_element});
5001              #push @{$self->{open_elements}},
5002              #    [$self->{head_element}, $el_category->{head}];
5003              #$self->{insertion_mode} = IN_HEAD_IM;
5004              ## NOTE: Reprocess.
5005    
5006              ## NOTE: As if </head>
5007              #pop @{$self->{open_elements}};
5008              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5009              ## NOTE: Reprocess.
5010              
5011              #
5012            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5013              !!!cp ('t149.2');
5014    
5015              ## NOTE: As if </head>
5016              pop @{$self->{open_elements}};
5017              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5018              ## NOTE: Reprocess.
5019    
5020              #
5021            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5022              !!!cp ('t149.3');
5023    
5024              !!!parse-error (type => 'in noscript:#eof', token => $token);
5025    
5026              ## As if </noscript>
5027              pop @{$self->{open_elements}};
5028              #$self->{insertion_mode} = IN_HEAD_IM;
5029              ## NOTE: Reprocess.
5030    
5031              ## NOTE: As if </head>
5032              pop @{$self->{open_elements}};
5033              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5034              ## NOTE: Reprocess.
5035    
5036              #
5037            } else {
5038              !!!cp ('t149.4');
5039              #
5040            }
5041    
5042            ## NOTE: As if <body>
5043            !!!insert-element ('body',, $token);
5044            $self->{insertion_mode} = IN_BODY_IM;
5045            ## NOTE: Reprocess.
5046            next B;
5047          } else {
5048            die "$0: $token->{type}: Unknown token type";
5049          }
5050    
5051              ## ISSUE: An issue in the spec.
5052        } elsif ($self->{insertion_mode} & BODY_IMS) {
5053              if ($token->{type} == CHARACTER_TOKEN) {
5054                !!!cp ('t150');
5055                ## NOTE: There is a code clone of "character in body".
5056              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
5057                            
5058              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5059    
5060              !!!next-token;              !!!next-token;
5061              redo B;              next B;
5062            } 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') {  
5063              if ({              if ({
5064                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
5065                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
5066                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5067                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
5068                    ## have an element in table scope
5069                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
5070                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
5071                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
5072                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
5073                  my $node = $self->{open_elements}->[$_];  
5074                  if ($node->[1] eq 'caption') {                      ## Close the cell
5075                    $i = $_;                      !!!back-token; # <x>
5076                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
5077                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
5078                            table => 1, html => 1,                                line => $token->{line},
5079                           }->{$node->[1]}) {                                column => $token->{column}};
5080                    last INSCOPE;                      next B;
5081                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5082                        !!!cp ('t152');
5083                        ## ISSUE: This case can never be reached, maybe.
5084                        last;
5085                      }
5086                  }                  }
5087                } # INSCOPE  
5088                unless (defined $i) {                  !!!cp ('t153');
5089                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
5090                        text => $token->{tag_name}, token => $token);
5091                  ## Ignore the token                  ## Ignore the token
5092                    !!!nack ('t153.1');
5093                  !!!next-token;                  !!!next-token;
5094                  redo B;                  next B;
5095                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5096                                  !!!parse-error (type => 'not closed', text => 'caption',
5097                ## generate implied end tags                                  token => $token);
5098                if ({                  
5099                     dd => 1, dt => 1, li => 1, p => 1,                  ## NOTE: As if </caption>.
5100                     td => 1, th => 1, tr => 1,                  ## have a table element in table scope
5101                    }->{$self->{open_elements}->[-1]->[1]}) {                  my $i;
5102                  !!!back-token; # <?>                  INSCOPE: {
5103                  $token = {type => 'end tag', tag_name => 'caption'};                    for (reverse 0..$#{$self->{open_elements}}) {
5104                  !!!back-token;                      my $node = $self->{open_elements}->[$_];
5105                  $token = {type => 'end tag',                      if ($node->[1] & CAPTION_EL) {
5106                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        !!!cp ('t155');
5107                  redo B;                        $i = $_;
5108                }                        last INSCOPE;
5109                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5110                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        !!!cp ('t156');
5111                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        last;
5112                }                      }
5113                      }
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
5114    
5115                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
5116                      !!!parse-error (type => 'start tag not allowed',
5117                                      text => $token->{tag_name}, token => $token);
5118                      ## Ignore the token
5119                      !!!nack ('t157.1');
5120                      !!!next-token;
5121                      next B;
5122                    } # INSCOPE
5123                    
5124                    ## generate implied end tags
5125                    while ($self->{open_elements}->[-1]->[1]
5126                               & END_TAG_OPTIONAL_EL) {
5127                      !!!cp ('t158');
5128                      pop @{$self->{open_elements}};
5129                    }
5130    
5131                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5132                redo B;                    !!!cp ('t159');
5133                      !!!parse-error (type => 'not closed',
5134                                      text => $self->{open_elements}->[-1]->[0]
5135                                          ->manakai_local_name,
5136                                      token => $token);
5137                    } else {
5138                      !!!cp ('t160');
5139                    }
5140                    
5141                    splice @{$self->{open_elements}}, $i;
5142                    
5143                    $clear_up_to_marker->();
5144                    
5145                    $self->{insertion_mode} = IN_TABLE_IM;
5146                    
5147                    ## reprocess
5148                    !!!ack-later;
5149                    next B;
5150                  } else {
5151                    !!!cp ('t161');
5152                    #
5153                  }
5154              } else {              } else {
5155                  !!!cp ('t162');
5156                #                #
5157              }              }
5158            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5159              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
5160                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
5161                my $i;                  ## have an element in table scope
5162                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
5163                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5164                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5165                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5166                    last INSCOPE;                      !!!cp ('t163');
5167                  } elsif ({                      $i = $_;
5168                            table => 1, html => 1,                      last INSCOPE;
5169                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5170                    last INSCOPE;                      !!!cp ('t164');
5171                        last INSCOPE;
5172                      }
5173                    } # INSCOPE
5174                      unless (defined $i) {
5175                        !!!cp ('t165');
5176                        !!!parse-error (type => 'unmatched end tag',
5177                                        text => $token->{tag_name},
5178                                        token => $token);
5179                        ## Ignore the token
5180                        !!!next-token;
5181                        next B;
5182                      }
5183                    
5184                    ## generate implied end tags
5185                    while ($self->{open_elements}->[-1]->[1]
5186                               & END_TAG_OPTIONAL_EL) {
5187                      !!!cp ('t166');
5188                      pop @{$self->{open_elements}};
5189                  }                  }
5190                } # INSCOPE  
5191                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5192                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
5193                      !!!cp ('t167');
5194                      !!!parse-error (type => 'not closed',
5195                                      text => $self->{open_elements}->[-1]->[0]
5196                                          ->manakai_local_name,
5197                                      token => $token);
5198                    } else {
5199                      !!!cp ('t168');
5200                    }
5201                    
5202                    splice @{$self->{open_elements}}, $i;
5203                    
5204                    $clear_up_to_marker->();
5205                    
5206                    $self->{insertion_mode} = IN_ROW_IM;
5207                    
5208                    !!!next-token;
5209                    next B;
5210                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5211                    !!!cp ('t169');
5212                    !!!parse-error (type => 'unmatched end tag',
5213                                    text => $token->{tag_name}, token => $token);
5214                  ## Ignore the token                  ## Ignore the token
5215                  !!!next-token;                  !!!next-token;
5216                  redo B;                  next B;
5217                }                } else {
5218                                  !!!cp ('t170');
5219                ## 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;  
5220                }                }
5221                } elsif ($token->{tag_name} eq 'caption') {
5222                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
5223                    ## have a table element in table scope
5224                    my $i;
5225                    INSCOPE: {
5226                      for (reverse 0..$#{$self->{open_elements}}) {
5227                        my $node = $self->{open_elements}->[$_];
5228                        if ($node->[1] & CAPTION_EL) {
5229                          !!!cp ('t171');
5230                          $i = $_;
5231                          last INSCOPE;
5232                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5233                          !!!cp ('t172');
5234                          last;
5235                        }
5236                      }
5237    
5238                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
5239                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
5240                                      text => $token->{tag_name}, token => $token);
5241                      ## Ignore the token
5242                      !!!next-token;
5243                      next B;
5244                    } # INSCOPE
5245                    
5246                    ## generate implied end tags
5247                    while ($self->{open_elements}->[-1]->[1]
5248                               & END_TAG_OPTIONAL_EL) {
5249                      !!!cp ('t174');
5250                      pop @{$self->{open_elements}};
5251                    }
5252                    
5253                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5254                      !!!cp ('t175');
5255                      !!!parse-error (type => 'not closed',
5256                                      text => $self->{open_elements}->[-1]->[0]
5257                                          ->manakai_local_name,
5258                                      token => $token);
5259                    } else {
5260                      !!!cp ('t176');
5261                    }
5262                    
5263                    splice @{$self->{open_elements}}, $i;
5264                    
5265                    $clear_up_to_marker->();
5266                    
5267                    $self->{insertion_mode} = IN_TABLE_IM;
5268                    
5269                    !!!next-token;
5270                    next B;
5271                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5272                    !!!cp ('t177');
5273                    !!!parse-error (type => 'unmatched end tag',
5274                                    text => $token->{tag_name}, token => $token);
5275                    ## Ignore the token
5276                    !!!next-token;
5277                    next B;
5278                  } else {
5279                    !!!cp ('t178');
5280                    #
5281                }                }
5282                } elsif ({
5283                          table => 1, tbody => 1, tfoot => 1,
5284                          thead => 1, tr => 1,
5285                         }->{$token->{tag_name}} and
5286                         $self->{insertion_mode} == IN_CELL_IM) {
5287                  ## have an element in table scope
5288                  my $i;
5289                  my $tn;
5290                  INSCOPE: {
5291                    for (reverse 0..$#{$self->{open_elements}}) {
5292                      my $node = $self->{open_elements}->[$_];
5293                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5294                        !!!cp ('t179');
5295                        $i = $_;
5296    
5297                        ## Close the cell
5298                        !!!back-token; # </x>
5299                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
5300                                  line => $token->{line},
5301                                  column => $token->{column}};
5302                        next B;
5303                      } elsif ($node->[1] & TABLE_CELL_EL) {
5304                        !!!cp ('t180');
5305                        $tn = $node->[0]->manakai_local_name;
5306                        ## NOTE: There is exactly one |td| or |th| element
5307                        ## in scope in the stack of open elements by definition.
5308                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5309                        ## ISSUE: Can this be reached?
5310                        !!!cp ('t181');
5311                        last;
5312                      }
5313                    }
5314    
5315                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
5316                    !!!parse-error (type => 'unmatched end tag',
5317                $clear_up_to_marker->();                      text => $token->{tag_name}, token => $token);
5318                    ## Ignore the token
5319                $self->{insertion_mode} = 'in table';                  !!!next-token;
5320                    next B;
5321                !!!next-token;                } # INSCOPE
5322                redo B;              } elsif ($token->{tag_name} eq 'table' and
5323              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5324                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5325                                  token => $token);
5326    
5327                ## As if </caption>                ## As if </caption>
5328                ## have a table element in table scope                ## have a table element in table scope
5329                my $i;                my $i;
5330                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5331                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5332                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5333                      !!!cp ('t184');
5334                    $i = $_;                    $i = $_;
5335                    last INSCOPE;                    last INSCOPE;
5336                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5337                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5338                    last INSCOPE;                    last INSCOPE;
5339                  }                  }
5340                } # INSCOPE                } # INSCOPE
5341                unless (defined $i) {                unless (defined $i) {
5342                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5343                    !!!parse-error (type => 'unmatched end tag',
5344                                    text => 'caption', token => $token);
5345                  ## Ignore the token                  ## Ignore the token
5346                  !!!next-token;                  !!!next-token;
5347                  redo B;                  next B;
5348                }                }
5349                                
5350                ## generate implied end tags                ## generate implied end tags
5351                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5352                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5353                     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;  
5354                }                }
5355    
5356                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5357                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5358                    !!!parse-error (type => 'not closed',
5359                                    text => $self->{open_elements}->[-1]->[0]
5360                                        ->manakai_local_name,
5361                                    token => $token);
5362                  } else {
5363                    !!!cp ('t189');
5364                }                }
5365    
5366                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5367    
5368                $clear_up_to_marker->();                $clear_up_to_marker->();
5369    
5370                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5371    
5372                ## reprocess                ## reprocess
5373                redo B;                next B;
5374              } elsif ({              } elsif ({
5375                        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,  
5376                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5377                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5378                ## Ignore the token                  !!!cp ('t190');
5379                redo B;                  !!!parse-error (type => 'unmatched end tag',
5380              } 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');  
5381                  ## Ignore the token                  ## Ignore the token
5382                  !!!next-token;                  !!!next-token;
5383                  redo B;                  next B;
5384                } else {                } else {
5385                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
5386                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
5387                }                }
5388              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
5389                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
5390                          thead => 1, tr => 1,
5391                         }->{$token->{tag_name}} and
5392                         $self->{insertion_mode} == IN_CAPTION_IM) {
5393                  !!!cp ('t192');
5394                  !!!parse-error (type => 'unmatched end tag',
5395                                  text => $token->{tag_name}, token => $token);
5396                ## Ignore the token                ## Ignore the token
5397                !!!next-token;                !!!next-token;
5398                redo B;                next B;
5399              } else {              } else {
5400                #                !!!cp ('t193');
5401                  #
5402              }              }
5403            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5404              #          for my $entry (@{$self->{open_elements}}) {
5405              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5406                !!!cp ('t75');
5407                !!!parse-error (type => 'in body:#eof', token => $token);
5408                last;
5409            }            }
5410            }
5411    
5412            ## As if </colgroup>          ## Stop parsing.
5413            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
5414              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
5415              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
5416          }
5417    
5418          $insert = $insert_to_current;
5419          #
5420        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5421          if ($token->{type} == CHARACTER_TOKEN) {
5422            if (not $open_tables->[-1]->[1] and # tainted
5423                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5424              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5425                  
5426              unless (length $token->{data}) {
5427                !!!cp ('t194');
5428              !!!next-token;              !!!next-token;
5429              redo B;              next B;
5430            } else {            } else {
5431              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5432            }            }
5433          } 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;  
               }  
             }  
5434    
5435              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5436    
5437              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5438              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5439              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5440              ## result in a new Text node.              ## result in a new Text node.
5441              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5442                
5443              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]}) {  
5444                # MUST                # MUST
5445                my $foster_parent_element;                my $foster_parent_element;
5446                my $next_sibling;                my $next_sibling;
5447                my $prev_sibling;                my $prev_sibling;
5448                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5449                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5450                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5451                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5452                        !!!cp ('t196');
5453                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5454                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5455                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5456                    } else {                    } else {
5457                        !!!cp ('t197');
5458                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5459                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5460                    }                    }
# Line 3749  sub _tree_construction_main ($) { Line 5466  sub _tree_construction_main ($) {
5466                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5467                if (defined $prev_sibling and                if (defined $prev_sibling and
5468                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5469                    !!!cp ('t198');
5470                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5471                } else {                } else {
5472                    !!!cp ('t199');
5473                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5474                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5475                     $next_sibling);                     $next_sibling);
5476                }                }
5477              } else {            $open_tables->[-1]->[1] = 1; # tainted
5478                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5479              !!!cp ('t200');
5480              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5481            }
5482                
5483            !!!next-token;
5484            next B;
5485          } elsif ($token->{type} == START_TAG_TOKEN) {
5486            if ({
5487                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5488                 th => 1, td => 1,
5489                }->{$token->{tag_name}}) {
5490              if ($self->{insertion_mode} == IN_TABLE_IM) {
5491                ## Clear back to table context
5492                while (not ($self->{open_elements}->[-1]->[1]
5493                                & TABLE_SCOPING_EL)) {
5494                  !!!cp ('t201');
5495                  pop @{$self->{open_elements}};
5496              }              }
5497                            
5498              !!!next-token;              !!!insert-element ('tbody',, $token);
5499              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5500            } elsif ($token->{type} eq 'comment') {              ## reprocess in the "in table body" insertion mode...
5501              ## Copied from 'in table'            }
5502              my $comment = $self->{document}->create_comment ($token->{data});            
5503              $self->{open_elements}->[-1]->[0]->append_child ($comment);            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5504              !!!next-token;              unless ($token->{tag_name} eq 'tr') {
5505              redo B;                !!!cp ('t202');
5506            } elsif ($token->{type} eq 'start tag') {                !!!parse-error (type => 'missing start tag:tr', token => $token);
5507              if ({              }
5508                   tr => 1,                  
5509                   th => 1, td => 1,              ## Clear back to table body context
5510                  }->{$token->{tag_name}}) {              while (not ($self->{open_elements}->[-1]->[1]
5511                unless ($token->{tag_name} eq 'tr') {                              & TABLE_ROWS_SCOPING_EL)) {
5512                  !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t203');
5513                  ## ISSUE: Can this case be reached?
5514                  pop @{$self->{open_elements}};
5515                }
5516                    
5517                    $self->{insertion_mode} = IN_ROW_IM;
5518                    if ($token->{tag_name} eq 'tr') {
5519                      !!!cp ('t204');
5520                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5521                      !!!nack ('t204');
5522                      !!!next-token;
5523                      next B;
5524                    } else {
5525                      !!!cp ('t205');
5526                      !!!insert-element ('tr',, $token);
5527                      ## reprocess in the "in row" insertion mode
5528                    }
5529                  } else {
5530                    !!!cp ('t206');
5531                }                }
5532    
5533                ## Clear back to table body context                ## Clear back to table row context
5534                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5535                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5536                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5537                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5538                }                }
5539                                
5540                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5541                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5542                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5543                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5544                } else {                
5545                  !!!insert-element ('tr');                !!!nack ('t207.1');
5546                  ## reprocess                !!!next-token;
5547                }                next B;
               redo B;  
5548              } elsif ({              } elsif ({
5549                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5550                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5551                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5552                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5553                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5554                my $i;                  ## As if </tr>
5555                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5556                  my $node = $self->{open_elements}->[$_];                  my $i;
5557                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5558                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5559                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5560                    $i = $_;                      !!!cp ('t208');
5561                    last INSCOPE;                      $i = $_;
5562                  } elsif ({                      last INSCOPE;
5563                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5564                           }->{$node->[1]}) {                      !!!cp ('t209');
5565                    last INSCOPE;                      last INSCOPE;
5566                      }
5567                    } # INSCOPE
5568                    unless (defined $i) {
5569                      !!!cp ('t210');
5570    ## TODO: This type is wrong.
5571                      !!!parse-error (type => 'unmacthed end tag',
5572                                      text => $token->{tag_name}, token => $token);
5573                      ## Ignore the token
5574                      !!!nack ('t210.1');
5575                      !!!next-token;
5576                      next B;
5577                    }
5578                    
5579                    ## Clear back to table row context
5580                    while (not ($self->{open_elements}->[-1]->[1]
5581                                    & TABLE_ROW_SCOPING_EL)) {
5582                      !!!cp ('t211');
5583                      ## ISSUE: Can this case be reached?
5584                      pop @{$self->{open_elements}};
5585                    }
5586                    
5587                    pop @{$self->{open_elements}}; # tr
5588                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5589                    if ($token->{tag_name} eq 'tr') {
5590                      !!!cp ('t212');
5591                      ## reprocess
5592                      !!!ack-later;
5593                      next B;
5594                    } else {
5595                      !!!cp ('t213');
5596                      ## reprocess in the "in table body" insertion mode...
5597                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5598                }                }
5599    
5600                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5601                while (not {                  ## have an element in table scope
5602                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5603                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5604                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5605                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5606                        !!!cp ('t214');
5607                        $i = $_;
5608                        last INSCOPE;
5609                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5610                        !!!cp ('t215');
5611                        last INSCOPE;
5612                      }
5613                    } # INSCOPE
5614                    unless (defined $i) {
5615                      !!!cp ('t216');
5616    ## TODO: This erorr type is wrong.
5617                      !!!parse-error (type => 'unmatched end tag',
5618                                      text => $token->{tag_name}, token => $token);
5619                      ## Ignore the token
5620                      !!!nack ('t216.1');
5621                      !!!next-token;
5622                      next B;
5623                    }
5624    
5625                    ## Clear back to table body context
5626                    while (not ($self->{open_elements}->[-1]->[1]
5627                                    & TABLE_ROWS_SCOPING_EL)) {
5628                      !!!cp ('t217');
5629                      ## ISSUE: Can this state be reached?
5630                      pop @{$self->{open_elements}};
5631                    }
5632                    
5633                    ## As if <{current node}>
5634                    ## have an element in table scope
5635                    ## true by definition
5636                    
5637                    ## Clear back to table body context
5638                    ## nop by definition
5639                    
5640                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5641                    $self->{insertion_mode} = IN_TABLE_IM;
5642                    ## reprocess in "in table" insertion mode...
5643                  } else {
5644                    !!!cp ('t218');
5645                }                }
5646    
5647                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5648                ## have an element in table scope                  ## Clear back to table context
5649                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5650                                    & TABLE_SCOPING_EL)) {
5651                ## Clear back to table body context                    !!!cp ('t219');
5652                ## nop by definition                    ## ISSUE: Can this state be reached?
5653                      pop @{$self->{open_elements}};
5654                pop @{$self->{open_elements}};                  }
5655                $self->{insertion_mode} = 'in table';                  
5656                ## reprocess                  !!!insert-element ('colgroup',, $token);
5657                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5658                    ## reprocess
5659                    !!!ack-later;
5660                    next B;
5661                  } elsif ({
5662                            caption => 1,
5663                            colgroup => 1,
5664                            tbody => 1, tfoot => 1, thead => 1,
5665                           }->{$token->{tag_name}}) {
5666                    ## Clear back to table context
5667                    while (not ($self->{open_elements}->[-1]->[1]
5668                                    & TABLE_SCOPING_EL)) {
5669                      !!!cp ('t220');
5670                      ## ISSUE: Can this state be reached?
5671                      pop @{$self->{open_elements}};
5672                    }
5673                    
5674                    push @$active_formatting_elements, ['#marker', '']
5675                        if $token->{tag_name} eq 'caption';
5676                    
5677                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5678                    $self->{insertion_mode} = {
5679                                               caption => IN_CAPTION_IM,
5680                                               colgroup => IN_COLUMN_GROUP_IM,
5681                                               tbody => IN_TABLE_BODY_IM,
5682                                               tfoot => IN_TABLE_BODY_IM,
5683                                               thead => IN_TABLE_BODY_IM,
5684                                              }->{$token->{tag_name}};
5685                    !!!next-token;
5686                    !!!nack ('t220.1');
5687                    next B;
5688                  } else {
5689                    die "$0: in table: <>: $token->{tag_name}";
5690                  }
5691              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5692                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5693                !!!parse-error (type => 'not closed:table');                                text => $self->{open_elements}->[-1]->[0]
5694                                      ->manakai_local_name,
5695                                  token => $token);
5696    
5697                ## As if </table>                ## As if </table>
5698                ## have a table element in table scope                ## have a table element in table scope
5699                my $i;                my $i;
5700                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5701                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5702                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5703                      !!!cp ('t221');
5704                    $i = $_;                    $i = $_;
5705                    last INSCOPE;                    last INSCOPE;
5706                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5707                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5708                    last INSCOPE;                    last INSCOPE;
5709                  }                  }
5710                } # INSCOPE                } # INSCOPE
5711                unless (defined $i) {                unless (defined $i) {
5712                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5713    ## TODO: The following is wrong, maybe.
5714                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5715                                    token => $token);
5716                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5717                    !!!nack ('t223.1');
5718                  !!!next-token;                  !!!next-token;
5719                  redo B;                  next B;
5720                }                }
5721                                
5722    ## TODO: Followings are removed from the latest spec.
5723                ## generate implied end tags                ## generate implied end tags
5724                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5725                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5726                     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;  
5727                }                }
5728    
5729                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5730                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5731                    ## NOTE: |<table><tr><table>|
5732                    !!!parse-error (type => 'not closed',
5733                                    text => $self->{open_elements}->[-1]->[0]
5734                                        ->manakai_local_name,
5735                                    token => $token);
5736                  } else {
5737                    !!!cp ('t226');
5738                }                }
5739    
5740                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5741                  pop @{$open_tables};
5742    
5743                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5744    
5745                ## reprocess            ## reprocess
5746                redo B;            !!!ack-later;
5747              } else {            next B;
5748                #          } elsif ($token->{tag_name} eq 'style') {
5749              }            if (not $open_tables->[-1]->[1]) { # tainted
5750            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5751              if ({              ## NOTE: This is a "as if in head" code clone.
5752                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5753                  }->{$token->{tag_name}}) {              next B;
5754                ## have an element in table scope            } else {
5755                my $i;              !!!cp ('t227.7');
5756                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5757                  my $node = $self->{open_elements}->[$_];            }
5758                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5759                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5760                    last INSCOPE;              !!!cp ('t227.6');
5761                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5762                            table => 1, html => 1,              $script_start_tag->();
5763                           }->{$node->[1]}) {              next B;
5764                    last INSCOPE;            } else {
5765                  }              !!!cp ('t227.5');
5766                } # INSCOPE              #
5767                unless (defined $i) {            }
5768                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5769                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5770                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5771                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5772                }                if ($type eq 'hidden') {
5773                    !!!cp ('t227.3');
5774                    !!!parse-error (type => 'in table',
5775                                    text => $token->{tag_name}, token => $token);
5776    
5777                ## 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}};  
               }  
5778    
5779                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;  
               }  
5780    
               ## 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]);  
5781                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
5782    
5783                ## Clear back to table body context                  !!!next-token;
5784                ## nop by definition                  !!!ack ('t227.2.1');
5785                    next B;
5786                pop @{$self->{open_elements}};                } else {
5787                $self->{insertion_mode} = 'in table';                  !!!cp ('t227.2');
5788                ## reprocess                  #
5789                redo B;                }
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
5790              } else {              } else {
5791                  !!!cp ('t227.1');
5792                #                #
5793              }              }
5794            } else {            } else {
5795                !!!cp ('t227.4');
5796              #              #
5797            }            }
5798                      } else {
5799            ## As if in table            !!!cp ('t227');
5800            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5801            $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;  
               }  
             }  
5802    
5803              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table', text => $token->{tag_name},
5804                            token => $token);
5805    
5806              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
5807              ## ISSUE: Spec says that "whenever a node would be inserted          #
5808              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
5809              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
5810              $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>  
5811                ## have an element in table scope                ## have an element in table scope
5812                my $i;                my $i;
5813                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5814                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5815                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5816                      !!!cp ('t228');
5817                    $i = $_;                    $i = $_;
5818                    last INSCOPE;                    last INSCOPE;
5819                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5820                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5821                    last INSCOPE;                    last INSCOPE;
5822                  }                  }
5823                } # INSCOPE                } # INSCOPE
5824                unless (defined $i) {                unless (defined $i) {
5825                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5826                    !!!parse-error (type => 'unmatched end tag',
5827                                    text => $token->{tag_name}, token => $token);
5828                  ## Ignore the token                  ## Ignore the token
5829                    !!!nack ('t230.1');
5830                  !!!next-token;                  !!!next-token;
5831                  redo B;                  next B;
5832                  } else {
5833                    !!!cp ('t232');
5834                }                }
5835    
5836                ## Clear back to table row context                ## Clear back to table row context
5837                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5838                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5839                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5840                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5841                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5842                }                }
5843    
5844                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5845                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5846                ## reprocess                !!!next-token;
5847                redo B;                !!!nack ('t231.1');
5848                  next B;
5849              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5850                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5851                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5852                    ## have an element in table scope
5853                ## As if </table>                  my $i;
5854                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5855                my $i;                    my $node = $self->{open_elements}->[$_];
5856                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5857                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5858                  if ($node->[1] eq 'table') {                      $i = $_;
5859                    $i = $_;                      last INSCOPE;
5860                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5861                  } elsif ({                      !!!cp ('t234');
5862                            table => 1, html => 1,                      last INSCOPE;
5863                           }->{$node->[1]}) {                    }
5864                    last INSCOPE;                  } # INSCOPE
5865                    unless (defined $i) {
5866                      !!!cp ('t235');
5867    ## TODO: The following is wrong.
5868                      !!!parse-error (type => 'unmatched end tag',
5869                                      text => $token->{type}, token => $token);
5870                      ## Ignore the token
5871                      !!!nack ('t236.1');
5872                      !!!next-token;
5873                      next B;
5874                  }                  }
5875                } # INSCOPE                  
5876                unless (defined $i) {                  ## Clear back to table row context
5877                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5878                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5879                  !!!next-token;                    !!!cp ('t236');
5880                  redo B;  ## ISSUE: Can this state be reached?
5881                }                    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;  
5882                  }                  }
5883                } # INSCOPE                  
5884                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5885                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5886                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5887                  !!!next-token;                }
5888                  redo B;  
5889                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5890                    ## have an element in table scope
5891                ## Clear back to table row context                  my $i;
5892                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5893                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5894                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5895                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5896                        $i = $_;
5897                        last INSCOPE;
5898                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5899                        !!!cp ('t238');
5900                        last INSCOPE;
5901                      }
5902                    } # INSCOPE
5903                    unless (defined $i) {
5904                      !!!cp ('t239');
5905                      !!!parse-error (type => 'unmatched end tag',
5906                                      text => $token->{tag_name}, token => $token);
5907                      ## Ignore the token
5908                      !!!nack ('t239.1');
5909                      !!!next-token;
5910                      next B;
5911                    }
5912                    
5913                    ## Clear back to table body context
5914                    while (not ($self->{open_elements}->[-1]->[1]
5915                                    & TABLE_ROWS_SCOPING_EL)) {
5916                      !!!cp ('t240');
5917                      pop @{$self->{open_elements}};
5918                    }
5919                    
5920                    ## As if <{current node}>
5921                    ## have an element in table scope
5922                    ## true by definition
5923                    
5924                    ## Clear back to table body context
5925                    ## nop by definition
5926                    
5927                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5928                    $self->{insertion_mode} = IN_TABLE_IM;
5929                    ## reprocess in the "in table" insertion mode...
5930                }                }
5931    
5932                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
5933                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
5934                !!!next-token;                ## the code for <table> in the "in table" insertion mode
5935                redo B;                ## is synced with it.
5936              } elsif ($token->{tag_name} eq 'table') {  
5937                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
5938                my $i;                my $i;
5939                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5940                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5941                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
5942                      !!!cp ('t241');
5943                    $i = $_;                    $i = $_;
5944                    last INSCOPE;                    last INSCOPE;
5945                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5946                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5947                    last INSCOPE;                    last INSCOPE;
5948                  }                  }
5949                } # INSCOPE                } # INSCOPE
5950                unless (defined $i) {                unless (defined $i) {
5951                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
5952                    !!!parse-error (type => 'unmatched end tag',
5953                                    text => $token->{tag_name}, token => $token);
5954                  ## Ignore the token                  ## Ignore the token
5955                    !!!nack ('t243.1');
5956                  !!!next-token;                  !!!next-token;
5957                  redo B;                  next B;
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
5958                }                }
5959                    
5960                pop @{$self->{open_elements}}; # tr                splice @{$self->{open_elements}}, $i;
5961                $self->{insertion_mode} = 'in table body';                pop @{$open_tables};
5962                ## reprocess                
5963                redo B;                $self->_reset_insertion_mode;
5964                  
5965                  !!!next-token;
5966                  next B;
5967              } elsif ({              } elsif ({
5968                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5969                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
5970                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
5971                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
5972                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5973                  my $node = $self->{open_elements}->[$_];                  my $i;
5974                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5975                    $i = $_;                    my $node = $self->{open_elements}->[$_];
5976                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5977                  } elsif ({                      !!!cp ('t247');
5978                            table => 1, html => 1,                      $i = $_;
5979                           }->{$node->[1]}) {                      last INSCOPE;
5980                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5981                        !!!cp ('t248');
5982                        last INSCOPE;
5983                      }
5984                    } # INSCOPE
5985                      unless (defined $i) {
5986                        !!!cp ('t249');
5987                        !!!parse-error (type => 'unmatched end tag',
5988                                        text => $token->{tag_name}, token => $token);
5989                        ## Ignore the token
5990                        !!!nack ('t249.1');
5991                        !!!next-token;
5992                        next B;
5993                      }
5994                    
5995                    ## As if </tr>
5996                    ## have an element in table scope
5997                    my $i;
5998                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5999                      my $node = $self->{open_elements}->[$_];
6000                      if ($node->[1] & TABLE_ROW_EL) {
6001                        !!!cp ('t250');
6002                        $i = $_;
6003                        last INSCOPE;
6004                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
6005                        !!!cp ('t251');
6006                        last INSCOPE;
6007                      }
6008                    } # INSCOPE
6009                      unless (defined $i) {
6010                        !!!cp ('t252');
6011                        !!!parse-error (type => 'unmatched end tag',
6012                                        text => 'tr', token => $token);
6013                        ## Ignore the token
6014                        !!!nack ('t252.1');
6015                        !!!next-token;
6016                        next B;
6017                      }
6018                    
6019                    ## Clear back to table row context
6020                    while (not ($self->{open_elements}->[-1]->[1]
6021                                    & TABLE_ROW_SCOPING_EL)) {
6022                      !!!cp ('t253');
6023    ## ISSUE: Can this case be reached?
6024                      pop @{$self->{open_elements}};
6025                  }                  }
6026                } # INSCOPE                  
6027                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
6028                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
6029                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
6030                }                }
6031    
               ## As if </tr>  
6032                ## have an element in table scope                ## have an element in table scope
6033                my $i;                my $i;
6034                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6035                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6036                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6037                      !!!cp ('t254');
6038                    $i = $_;                    $i = $_;
6039                    last INSCOPE;                    last INSCOPE;
6040                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6041                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
6042                    last INSCOPE;                    last INSCOPE;
6043                  }                  }
6044                } # INSCOPE                } # INSCOPE
6045                unless (defined $i) {                unless (defined $i) {
6046                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
6047                    !!!parse-error (type => 'unmatched end tag',
6048                                    text => $token->{tag_name}, token => $token);
6049                  ## Ignore the token                  ## Ignore the token
6050                    !!!nack ('t256.1');
6051                  !!!next-token;                  !!!next-token;
6052                  redo B;                  next B;
6053                }                }
6054    
6055                ## Clear back to table row context                ## Clear back to table body context
6056                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6057                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
6058                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
6059                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6060                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6061                }                }
6062    
6063                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
6064                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
6065                ## reprocess                !!!nack ('t257.1');
6066                redo B;                !!!next-token;
6067                  next B;
6068              } elsif ({              } elsif ({
6069                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6070                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6071                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6072                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6073                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6074                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
6075                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
6076                !!!next-token;                            text => $token->{tag_name}, token => $token);
6077                redo B;            ## Ignore the token
6078              } else {            !!!nack ('t258.1');
6079                #             !!!next-token;
6080              }            next B;
6081            } else {          } else {
6082              #            !!!cp ('t259');
6083            }            !!!parse-error (type => 'in table:/',
6084                              text => $token->{tag_name}, token => $token);
6085    
6086            ## As if in table            $insert = $insert_to_foster;
6087            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
6088            $in_body->($insert_to_foster);          }
6089            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6090          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6091            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
6092              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
6093              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
6094                          #
6095              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6096              !!!cp ('t259.2');
6097              #
6098            }
6099    
6100              !!!next-token;          ## Stop parsing
6101              redo B;          last B;
6102            } elsif ($token->{type} eq 'comment') {        } else {
6103              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
6104              my $comment = $self->{document}->create_comment ($token->{data});        }
6105              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6106              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
6107              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6108            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6109              if ({                unless (length $token->{data}) {
6110                   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  
6111                  !!!next-token;                  !!!next-token;
6112                  redo B;                  next B;
6113                }                }
6114                }
6115                ## Close the cell              
6116                !!!back-token; # <?>              !!!cp ('t261');
6117                $token = {type => 'end tag', tag_name => $tn};              #
6118                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
6119              } else {              if ($token->{tag_name} eq 'col') {
6120                  !!!cp ('t262');
6121                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6122                  pop @{$self->{open_elements}};
6123                  !!!ack ('t262.1');
6124                  !!!next-token;
6125                  next B;
6126                } else {
6127                  !!!cp ('t263');
6128                #                #
6129              }              }
6130            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
6131              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
6132                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6133                my $i;                  !!!cp ('t264');
6134                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag',
6135                  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});  
6136                  ## Ignore the token                  ## Ignore the token
6137                  !!!next-token;                  !!!next-token;
6138                  redo B;                  next B;
6139                }                } else {
6140                                  !!!cp ('t265');
6141                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
6142                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
6143                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
6144                     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]);  
6145                }                }
6146                } elsif ($token->{tag_name} eq 'col') {
6147                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
6148                  !!!parse-error (type => 'unmatched end tag',
6149                $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});  
6150                ## Ignore the token                ## Ignore the token
6151                !!!next-token;                !!!next-token;
6152                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;  
6153              } else {              } else {
6154                #                !!!cp ('t267');
6155                  #
6156              }              }
6157          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6158            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6159                @{$self->{open_elements}} == 1) { # redundant, maybe
6160              !!!cp ('t270.2');
6161              ## Stop parsing.
6162              last B;
6163            } else {
6164              ## NOTE: As if </colgroup>.
6165              !!!cp ('t270.1');
6166              pop @{$self->{open_elements}}; # colgroup
6167              $self->{insertion_mode} = IN_TABLE_IM;
6168              ## Reprocess.
6169              next B;
6170            }
6171          } else {
6172            die "$0: $token->{type}: Unknown token type";
6173          }
6174    
6175              ## As if </colgroup>
6176              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6177                !!!cp ('t269');
6178    ## TODO: Wrong error type?
6179                !!!parse-error (type => 'unmatched end tag',
6180                                text => 'colgroup', token => $token);
6181                ## Ignore the token
6182                !!!nack ('t269.1');
6183                !!!next-token;
6184                next B;
6185            } else {            } else {
6186              #              !!!cp ('t270');
6187                pop @{$self->{open_elements}}; # colgroup
6188                $self->{insertion_mode} = IN_TABLE_IM;
6189                !!!ack-later;
6190                ## reprocess
6191                next B;
6192              }
6193        } elsif ($self->{insertion_mode} & SELECT_IMS) {
6194          if ($token->{type} == CHARACTER_TOKEN) {
6195            !!!cp ('t271');
6196            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6197            !!!next-token;
6198            next B;
6199          } elsif ($token->{type} == START_TAG_TOKEN) {
6200            if ($token->{tag_name} eq 'option') {
6201              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6202                !!!cp ('t272');
6203                ## As if </option>
6204                pop @{$self->{open_elements}};
6205              } else {
6206                !!!cp ('t273');
6207            }            }
             
           $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}};  
               }  
6208    
6209                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6210                !!!next-token;            !!!nack ('t273.1');
6211                redo B;            !!!next-token;
6212              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6213                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6214                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6215                  pop @{$self->{open_elements}};              !!!cp ('t274');
6216                }              ## As if </option>
6217                pop @{$self->{open_elements}};
6218              } else {
6219                !!!cp ('t275');
6220              }
6221    
6222                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6223                  ## As if </optgroup>              !!!cp ('t276');
6224                  pop @{$self->{open_elements}};              ## As if </optgroup>
6225                }              pop @{$self->{open_elements}};
6226              } else {
6227                !!!cp ('t277');
6228              }
6229    
6230                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6231                !!!next-token;            !!!nack ('t277.1');
6232                redo B;            !!!next-token;
6233              } elsif ($token->{tag_name} eq 'select') {            next B;
6234                !!!parse-error (type => 'not closed:select');          } elsif ({
6235                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6236                ## have an element in table scope                   }->{$token->{tag_name}} or
6237                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6238                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6239                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6240                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6241                    $i = $_;                     tr => 1, td => 1, th => 1,
6242                    last INSCOPE;                    }->{$token->{tag_name}})) {
6243                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6244                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6245                           }->{$node->[1]}) {                            token => $token);
6246                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6247                  }            ## as if there were </select> (otherwise).
6248                } # INSCOPE            ## have an element in table scope
6249                unless (defined $i) {            my $i;
6250                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6251                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6252                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6253                  redo B;                !!!cp ('t278');
6254                }                $i = $_;
6255                  last INSCOPE;
6256                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6257                  !!!cp ('t279');
6258                  last INSCOPE;
6259                }
6260              } # INSCOPE
6261              unless (defined $i) {
6262                !!!cp ('t280');
6263                !!!parse-error (type => 'unmatched end tag',
6264                                text => 'select', token => $token);
6265                ## Ignore the token
6266                !!!nack ('t280.1');
6267                !!!next-token;
6268                next B;
6269              }
6270                                
6271                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6272              splice @{$self->{open_elements}}, $i;
6273    
6274                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6275    
6276                !!!next-token;            if ($token->{tag_name} eq 'select') {
6277                redo B;              !!!nack ('t281.2');
6278              } else {              !!!next-token;
6279                #              next B;
6280              } else {
6281                !!!cp ('t281.1');
6282                !!!ack-later;
6283                ## Reprocess the token.
6284                next B;
6285              }
6286            } else {
6287              !!!cp ('t282');
6288              !!!parse-error (type => 'in select',
6289                              text => $token->{tag_name}, token => $token);
6290              ## Ignore the token
6291              !!!nack ('t282.1');
6292              !!!next-token;
6293              next B;
6294            }
6295          } elsif ($token->{type} == END_TAG_TOKEN) {
6296            if ($token->{tag_name} eq 'optgroup') {
6297              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6298                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6299                !!!cp ('t283');
6300                ## As if </option>
6301                splice @{$self->{open_elements}}, -2;
6302              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6303                !!!cp ('t284');
6304                pop @{$self->{open_elements}};
6305              } else {
6306                !!!cp ('t285');
6307                !!!parse-error (type => 'unmatched end tag',
6308                                text => $token->{tag_name}, token => $token);
6309                ## Ignore the token
6310              }
6311              !!!nack ('t285.1');
6312              !!!next-token;
6313              next B;
6314            } elsif ($token->{tag_name} eq 'option') {
6315              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6316                !!!cp ('t286');
6317                pop @{$self->{open_elements}};
6318              } else {
6319                !!!cp ('t287');
6320                !!!parse-error (type => 'unmatched end tag',
6321                                text => $token->{tag_name}, token => $token);
6322                ## Ignore the token
6323              }
6324              !!!nack ('t287.1');
6325              !!!next-token;
6326              next B;
6327            } elsif ($token->{tag_name} eq 'select') {
6328              ## have an element in table scope
6329              my $i;
6330              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6331                my $node = $self->{open_elements}->[$_];
6332                if ($node->[1] & SELECT_EL) {
6333                  !!!cp ('t288');
6334                  $i = $_;
6335                  last INSCOPE;
6336                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6337                  !!!cp ('t289');
6338                  last INSCOPE;
6339              }              }
6340            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6341              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6342                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6343                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6344                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6345                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6346                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6347                  pop @{$self->{open_elements}};              !!!next-token;
6348                } else {              next B;
6349                  !!!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;  
               }  
6350                                
6351                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6352              splice @{$self->{open_elements}}, $i;
6353    
6354                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6355    
6356                !!!next-token;            !!!nack ('t291.1');
6357                redo B;            !!!next-token;
6358              } elsif ({            next B;
6359                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6360                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6361                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6362                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6363                                   }->{$token->{tag_name}}) {
6364                ## have an element in table scope  ## TODO: The following is wrong?
6365                my $i;            !!!parse-error (type => 'unmatched end tag',
6366                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;  
               }  
6367                                
6368                ## As if </select>            ## have an element in table scope
6369                ## have an element in table scope            my $i;
6370                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6371                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6372                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6373                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6374                    $i = $_;                $i = $_;
6375                    last INSCOPE;                last INSCOPE;
6376                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6377                            table => 1, html => 1,                !!!cp ('t293');
6378                           }->{$node->[1]}) {                last INSCOPE;
6379                    last INSCOPE;              }
6380                  }            } # INSCOPE
6381                } # INSCOPE            unless (defined $i) {
6382                unless (defined $i) {              !!!cp ('t294');
6383                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6384                  ## Ignore the </select> token              !!!nack ('t294.1');
6385                  !!!next-token; ## TODO: ok?              !!!next-token;
6386                  redo B;              next B;
6387                }            }
6388                                
6389                splice @{$self->{open_elements}}, $i;            ## As if </select>
6390              ## have an element in table scope
6391                $self->_reset_insertion_mode;            undef $i;
6392              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6393                ## reprocess              my $node = $self->{open_elements}->[$_];
6394                redo B;              if ($node->[1] & SELECT_EL) {
6395              } else {                !!!cp ('t295');
6396                #                $i = $_;
6397                  last INSCOPE;
6398                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6399    ## ISSUE: Can this state be reached?
6400                  !!!cp ('t296');
6401                  last INSCOPE;
6402              }              }
6403            } else {            } # INSCOPE
6404              #            unless (defined $i) {
6405                !!!cp ('t297');
6406    ## TODO: The following error type is correct?
6407                !!!parse-error (type => 'unmatched end tag',
6408                                text => 'select', token => $token);
6409                ## Ignore the </select> token
6410                !!!nack ('t297.1');
6411                !!!next-token; ## TODO: ok?
6412                next B;
6413            }            }
6414                  
6415              !!!cp ('t298');
6416              splice @{$self->{open_elements}}, $i;
6417    
6418              $self->_reset_insertion_mode;
6419    
6420            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
6421              ## reprocess
6422              next B;
6423            } else {
6424              !!!cp ('t299');
6425              !!!parse-error (type => 'in select:/',
6426                              text => $token->{tag_name}, token => $token);
6427            ## Ignore the token            ## Ignore the token
6428              !!!nack ('t299.3');
6429            !!!next-token;            !!!next-token;
6430            redo B;            next B;
6431          } elsif ($self->{insertion_mode} eq 'after body') {          }
6432            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6433              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6434                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
6435                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
6436                            !!!parse-error (type => 'in body:#eof', token => $token);
6437                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6438              !!!cp ('t299.2');
6439            }
6440    
6441                unless (length $token->{data}) {          ## Stop parsing.
6442                  !!!next-token;          last B;
6443                  redo B;        } else {
6444                }          die "$0: $token->{type}: Unknown token type";
6445              }        }
6446                    } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6447              #        if ($token->{type} == CHARACTER_TOKEN) {
6448              !!!parse-error (type => 'after body:#'.$token->{type});          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6449            } elsif ($token->{type} eq 'comment') {            my $data = $1;
6450              my $comment = $self->{document}->create_comment ($token->{data});            ## As if in body
6451              $self->{open_elements}->[0]->[0]->append_child ($comment);            $reconstruct_active_formatting_elements->($insert_to_current);
6452                  
6453              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6454              
6455              unless (length $token->{data}) {
6456                !!!cp ('t300');
6457              !!!next-token;              !!!next-token;
6458              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});  
6459            }            }
6460            }
6461            
6462            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6463              !!!cp ('t301');
6464              !!!parse-error (type => 'after html:#text', token => $token);
6465    
6466            $self->{insertion_mode} = 'in body';            ## Reprocess in the "after body" insertion mode.
6467            ## reprocess          } else {
6468            redo B;            !!!cp ('t302');
6469          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6470            if ($token->{type} eq 'character') {          
6471              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## "after body" insertion mode
6472                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          !!!parse-error (type => 'after body:#text', token => $token);
6473    
6474                unless (length $token->{data}) {          $self->{insertion_mode} = IN_BODY_IM;
6475                  !!!next-token;          ## reprocess
6476                  redo B;          next B;
6477                }        } elsif ($token->{type} == START_TAG_TOKEN) {
6478              }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6479              !!!cp ('t303');
6480              !!!parse-error (type => 'after html',
6481                              text => $token->{tag_name}, token => $token);
6482              
6483              ## Reprocess in the "after body" insertion mode.
6484            } else {
6485              !!!cp ('t304');
6486            }
6487    
6488              #          ## "after body" insertion mode
6489            } elsif ($token->{type} eq 'comment') {          !!!parse-error (type => 'after body',
6490              my $comment = $self->{document}->create_comment ($token->{data});                          text => $token->{tag_name}, token => $token);
6491              $self->{open_elements}->[-1]->[0]->append_child ($comment);  
6492            $self->{insertion_mode} = IN_BODY_IM;
6493            !!!ack-later;
6494            ## reprocess
6495            next B;
6496          } elsif ($token->{type} == END_TAG_TOKEN) {
6497            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6498              !!!cp ('t305');
6499              !!!parse-error (type => 'after html:/',
6500                              text => $token->{tag_name}, token => $token);
6501              
6502              $self->{insertion_mode} = AFTER_BODY_IM;
6503              ## Reprocess in the "after body" insertion mode.
6504            } else {
6505              !!!cp ('t306');
6506            }
6507    
6508            ## "after body" insertion mode
6509            if ($token->{tag_name} eq 'html') {
6510              if (defined $self->{inner_html_node}) {
6511                !!!cp ('t307');
6512                !!!parse-error (type => 'unmatched end tag',
6513                                text => 'html', token => $token);
6514                ## Ignore the token
6515              !!!next-token;              !!!next-token;
6516              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 {  
               #  
             }  
6517            } else {            } else {
6518              #              !!!cp ('t308');
6519                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6520                !!!next-token;
6521                next B;
6522            }            }
6523            } else {
6524              !!!cp ('t309');
6525              !!!parse-error (type => 'after body:/',
6526                              text => $token->{tag_name}, token => $token);
6527    
6528              $self->{insertion_mode} = IN_BODY_IM;
6529              ## reprocess
6530              next B;
6531            }
6532          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6533            !!!cp ('t309.2');
6534            ## Stop parsing
6535            last B;
6536          } else {
6537            die "$0: $token->{type}: Unknown token type";
6538          }
6539        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6540          if ($token->{type} == CHARACTER_TOKEN) {
6541            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6542              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6543                        
6544            if (defined $token->{tag_name}) {            unless (length $token->{data}) {
6545              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t310');
6546                !!!next-token;
6547                next B;
6548              }
6549            }
6550            
6551            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6552              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6553                !!!cp ('t311');
6554                !!!parse-error (type => 'in frameset:#text', token => $token);
6555              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6556                !!!cp ('t312');
6557                !!!parse-error (type => 'after frameset:#text', token => $token);
6558              } else { # "after after frameset"
6559                !!!cp ('t313');
6560                !!!parse-error (type => 'after html:#text', token => $token);
6561              }
6562              
6563              ## Ignore the token.
6564              if (length $token->{data}) {
6565                !!!cp ('t314');
6566                ## reprocess the rest of characters
6567            } else {            } else {
6568              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t315');
6569                !!!next-token;
6570              }
6571              next B;
6572            }
6573            
6574            die qq[$0: Character "$token->{data}"];
6575          } elsif ($token->{type} == START_TAG_TOKEN) {
6576            if ($token->{tag_name} eq 'frameset' and
6577                $self->{insertion_mode} == IN_FRAMESET_IM) {
6578              !!!cp ('t318');
6579              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6580              !!!nack ('t318.1');
6581              !!!next-token;
6582              next B;
6583            } elsif ($token->{tag_name} eq 'frame' and
6584                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6585              !!!cp ('t319');
6586              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6587              pop @{$self->{open_elements}};
6588              !!!ack ('t319.1');
6589              !!!next-token;
6590              next B;
6591            } elsif ($token->{tag_name} eq 'noframes') {
6592              !!!cp ('t320');
6593              ## NOTE: As if in head.
6594              $parse_rcdata->(CDATA_CONTENT_MODEL);
6595              next B;
6596    
6597              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6598              ## has no parse error.
6599            } else {
6600              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6601                !!!cp ('t321');
6602                !!!parse-error (type => 'in frameset',
6603                                text => $token->{tag_name}, token => $token);
6604              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6605                !!!cp ('t322');
6606                !!!parse-error (type => 'after frameset',
6607                                text => $token->{tag_name}, token => $token);
6608              } else { # "after after frameset"
6609                !!!cp ('t322.2');
6610                !!!parse-error (type => 'after after frameset',
6611                                text => $token->{tag_name}, token => $token);
6612            }            }
6613            ## Ignore the token            ## Ignore the token
6614              !!!nack ('t322.1');
6615            !!!next-token;            !!!next-token;
6616            redo B;            next B;
6617          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6618            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6619              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{tag_name} eq 'frameset' and
6620                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{insertion_mode} == IN_FRAMESET_IM) {
6621              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6622                  @{$self->{open_elements}} == 1) {
6623                !!!cp ('t325');
6624                !!!parse-error (type => 'unmatched end tag',
6625                                text => $token->{tag_name}, token => $token);
6626                ## Ignore the token
6627                !!!next-token;
6628              } else {
6629                !!!cp ('t326');
6630                pop @{$self->{open_elements}};
6631                !!!next-token;
6632              }
6633    
6634                unless (length $token->{data}) {            if (not defined $self->{inner_html_node} and
6635                  !!!next-token;                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6636                  redo B;              !!!cp ('t327');
6637                }              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6638              } else {
6639                !!!cp ('t328');
6640              }
6641              next B;
6642            } elsif ($token->{tag_name} eq 'html' and
6643                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6644              !!!cp ('t329');
6645              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6646              !!!next-token;
6647              next B;
6648            } else {
6649              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6650                !!!cp ('t330');
6651                !!!parse-error (type => 'in frameset:/',
6652                                text => $token->{tag_name}, token => $token);
6653              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6654                !!!cp ('t330.1');
6655                !!!parse-error (type => 'after frameset:/',
6656                                text => $token->{tag_name}, token => $token);
6657              } else { # "after after html"
6658                !!!cp ('t331');
6659                !!!parse-error (type => 'after after frameset:/',
6660                                text => $token->{tag_name}, token => $token);
6661              }
6662              ## Ignore the token
6663              !!!next-token;
6664              next B;
6665            }
6666          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6667            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6668                    @{$self->{open_elements}} == 1) { # redundant, maybe
6669              !!!cp ('t331.1');
6670              !!!parse-error (type => 'in body:#eof', token => $token);
6671            } else {
6672              !!!cp ('t331.2');
6673            }
6674            
6675            ## Stop parsing
6676            last B;
6677          } else {
6678            die "$0: $token->{type}: Unknown token type";
6679          }
6680    
6681          ## ISSUE: An issue in spec here
6682        } else {
6683          die "$0: $self->{insertion_mode}: Unknown insertion mode";
6684        }
6685    
6686        ## "in body" insertion mode
6687        if ($token->{type} == START_TAG_TOKEN) {
6688          if ($token->{tag_name} eq 'script') {
6689            !!!cp ('t332');
6690            ## NOTE: This is an "as if in head" code clone
6691            $script_start_tag->();
6692            next B;
6693          } elsif ($token->{tag_name} eq 'style') {
6694            !!!cp ('t333');
6695            ## NOTE: This is an "as if in head" code clone
6696            $parse_rcdata->(CDATA_CONTENT_MODEL);
6697            next B;
6698          } elsif ({
6699                    base => 1, link => 1,
6700                   }->{$token->{tag_name}}) {
6701            !!!cp ('t334');
6702            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6703            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6704            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6705            !!!ack ('t334.1');
6706            !!!next-token;
6707            next B;
6708          } elsif ($token->{tag_name} eq 'meta') {
6709            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6710            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6711            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6712    
6713            unless ($self->{confident}) {
6714              if ($token->{attributes}->{charset}) {
6715                !!!cp ('t335');
6716                ## NOTE: Whether the encoding is supported or not is handled
6717                ## in the {change_encoding} callback.
6718                $self->{change_encoding}
6719                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6720                
6721                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6722                    ->set_user_data (manakai_has_reference =>
6723                                         $token->{attributes}->{charset}
6724                                             ->{has_reference});
6725              } elsif ($token->{attributes}->{content}) {
6726                if ($token->{attributes}->{content}->{value}
6727                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6728                        [\x09-\x0D\x20]*=
6729                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6730                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6731                  !!!cp ('t336');
6732                  ## NOTE: Whether the encoding is supported or not is handled
6733                  ## in the {change_encoding} callback.
6734                  $self->{change_encoding}
6735                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6736                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6737                      ->set_user_data (manakai_has_reference =>
6738                                           $token->{attributes}->{content}
6739                                                 ->{has_reference});
6740              }              }
6741              }
6742            } else {
6743              if ($token->{attributes}->{charset}) {
6744                !!!cp ('t337');
6745                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6746                    ->set_user_data (manakai_has_reference =>
6747                                         $token->{attributes}->{charset}
6748                                             ->{has_reference});
6749              }
6750              if ($token->{attributes}->{content}) {
6751                !!!cp ('t338');
6752                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6753                    ->set_user_data (manakai_has_reference =>
6754                                         $token->{attributes}->{content}
6755                                             ->{has_reference});
6756              }
6757            }
6758    
6759              #          !!!ack ('t338.1');
6760            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6761              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6762              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6763              !!!next-token;          !!!cp ('t341');
6764              redo B;          ## NOTE: This is an "as if in head" code clone
6765            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6766              if ($token->{tag_name} eq 'noframes') {          next B;
6767                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6768                redo B;          !!!parse-error (type => 'in body', text => 'body', token => $token);
6769              } else {                
6770                #          if (@{$self->{open_elements}} == 1 or
6771                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6772              !!!cp ('t342');
6773              ## Ignore the token
6774            } else {
6775              my $body_el = $self->{open_elements}->[1]->[0];
6776              for my $attr_name (keys %{$token->{attributes}}) {
6777                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6778                  !!!cp ('t343');
6779                  $body_el->set_attribute_ns
6780                    (undef, [undef, $attr_name],
6781                     $token->{attributes}->{$attr_name}->{value});
6782              }              }
6783            } elsif ($token->{type} eq 'end tag') {            }
6784              if ($token->{tag_name} eq 'html') {          }
6785                $phase = 'trailing end';          !!!nack ('t343.1');
6786            !!!next-token;
6787            next B;
6788          } elsif ({
6789                    address => 1, blockquote => 1, center => 1, dir => 1,
6790                    div => 1, dl => 1, fieldset => 1,
6791                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6792                    menu => 1, ol => 1, p => 1, ul => 1,
6793                    pre => 1, listing => 1,
6794                    form => 1,
6795                    table => 1,
6796                    hr => 1,
6797                   }->{$token->{tag_name}}) {
6798            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6799              !!!cp ('t350');
6800              !!!parse-error (type => 'in form:form', token => $token);
6801              ## Ignore the token
6802              !!!nack ('t350.1');
6803              !!!next-token;
6804              next B;
6805            }
6806    
6807            ## has a p element in scope
6808            INSCOPE: for (reverse @{$self->{open_elements}}) {
6809              if ($_->[1] & P_EL) {
6810                !!!cp ('t344');
6811                !!!back-token; # <form>
6812                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6813                          line => $token->{line}, column => $token->{column}};
6814                next B;
6815              } elsif ($_->[1] & SCOPING_EL) {
6816                !!!cp ('t345');
6817                last INSCOPE;
6818              }
6819            } # INSCOPE
6820              
6821            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6822            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6823              !!!nack ('t346.1');
6824              !!!next-token;
6825              if ($token->{type} == CHARACTER_TOKEN) {
6826                $token->{data} =~ s/^\x0A//;
6827                unless (length $token->{data}) {
6828                  !!!cp ('t346');
6829                !!!next-token;                !!!next-token;
               redo B;  
6830              } else {              } else {
6831                #                !!!cp ('t349');
6832              }              }
6833            } else {            } else {
6834              #              !!!cp ('t348');
6835              }
6836            } elsif ($token->{tag_name} eq 'form') {
6837              !!!cp ('t347.1');
6838              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6839    
6840              !!!nack ('t347.2');
6841              !!!next-token;
6842            } elsif ($token->{tag_name} eq 'table') {
6843              !!!cp ('t382');
6844              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6845              
6846              $self->{insertion_mode} = IN_TABLE_IM;
6847    
6848              !!!nack ('t382.1');
6849              !!!next-token;
6850            } elsif ($token->{tag_name} eq 'hr') {
6851              !!!cp ('t386');
6852              pop @{$self->{open_elements}};
6853            
6854              !!!nack ('t386.1');
6855              !!!next-token;
6856            } else {
6857              !!!nack ('t347.1');
6858              !!!next-token;
6859            }
6860            next B;
6861          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6862            ## has a p element in scope
6863            INSCOPE: for (reverse @{$self->{open_elements}}) {
6864              if ($_->[1] & P_EL) {
6865                !!!cp ('t353');
6866                !!!back-token; # <x>
6867                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6868                          line => $token->{line}, column => $token->{column}};
6869                next B;
6870              } elsif ($_->[1] & SCOPING_EL) {
6871                !!!cp ('t354');
6872                last INSCOPE;
6873            }            }
6874            } # INSCOPE
6875                        
6876            if (defined $token->{tag_name}) {          ## Step 1
6877              !!!parse-error (type => 'after frameset:'.$token->{tag_name});          my $i = -1;
6878            my $node = $self->{open_elements}->[$i];
6879            my $li_or_dtdd = {li => {li => 1},
6880                              dt => {dt => 1, dd => 1},
6881                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6882            LI: {
6883              ## Step 2
6884              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6885                if ($i != -1) {
6886                  !!!cp ('t355');
6887                  !!!parse-error (type => 'not closed',
6888                                  text => $self->{open_elements}->[-1]->[0]
6889                                      ->manakai_local_name,
6890                                  token => $token);
6891                } else {
6892                  !!!cp ('t356');
6893                }
6894                splice @{$self->{open_elements}}, $i;
6895                last LI;
6896            } else {            } else {
6897              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
6898              }
6899              
6900              ## Step 3
6901              if (not ($node->[1] & FORMATTING_EL) and
6902                  #not $phrasing_category->{$node->[1]} and
6903                  ($node->[1] & SPECIAL_EL or
6904                   $node->[1] & SCOPING_EL) and
6905                  not ($node->[1] & ADDRESS_EL) and
6906                  not ($node->[1] & DIV_EL)) {
6907                !!!cp ('t358');
6908                last LI;
6909              }
6910              
6911              !!!cp ('t359');
6912              ## Step 4
6913              $i--;
6914              $node = $self->{open_elements}->[$i];
6915              redo LI;
6916            } # LI
6917              
6918            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6919            !!!nack ('t359.1');
6920            !!!next-token;
6921            next B;
6922          } elsif ($token->{tag_name} eq 'plaintext') {
6923            ## has a p element in scope
6924            INSCOPE: for (reverse @{$self->{open_elements}}) {
6925              if ($_->[1] & P_EL) {
6926                !!!cp ('t367');
6927                !!!back-token; # <plaintext>
6928                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6929                          line => $token->{line}, column => $token->{column}};
6930                next B;
6931              } elsif ($_->[1] & SCOPING_EL) {
6932                !!!cp ('t368');
6933                last INSCOPE;
6934              }
6935            } # INSCOPE
6936              
6937            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6938              
6939            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6940              
6941            !!!nack ('t368.1');
6942            !!!next-token;
6943            next B;
6944          } elsif ($token->{tag_name} eq 'a') {
6945            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6946              my $node = $active_formatting_elements->[$i];
6947              if ($node->[1] & A_EL) {
6948                !!!cp ('t371');
6949                !!!parse-error (type => 'in a:a', token => $token);
6950                
6951                !!!back-token; # <a>
6952                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6953                          line => $token->{line}, column => $token->{column}};
6954                $formatting_end_tag->($token);
6955                
6956                AFE2: for (reverse 0..$#$active_formatting_elements) {
6957                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6958                    !!!cp ('t372');
6959                    splice @$active_formatting_elements, $_, 1;
6960                    last AFE2;
6961                  }
6962                } # AFE2
6963                OE: for (reverse 0..$#{$self->{open_elements}}) {
6964                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6965                    !!!cp ('t373');
6966                    splice @{$self->{open_elements}}, $_, 1;
6967                    last OE;
6968                  }
6969                } # OE
6970                last AFE;
6971              } elsif ($node->[0] eq '#marker') {
6972                !!!cp ('t374');
6973                last AFE;
6974              }
6975            } # AFE
6976              
6977            $reconstruct_active_formatting_elements->($insert_to_current);
6978    
6979            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6980            push @$active_formatting_elements, $self->{open_elements}->[-1];
6981    
6982            !!!nack ('t374.1');
6983            !!!next-token;
6984            next B;
6985          } elsif ($token->{tag_name} eq 'nobr') {
6986            $reconstruct_active_formatting_elements->($insert_to_current);
6987    
6988            ## has a |nobr| element in scope
6989            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6990              my $node = $self->{open_elements}->[$_];
6991              if ($node->[1] & NOBR_EL) {
6992                !!!cp ('t376');
6993                !!!parse-error (type => 'in nobr:nobr', token => $token);
6994                !!!back-token; # <nobr>
6995                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6996                          line => $token->{line}, column => $token->{column}};
6997                next B;
6998              } elsif ($node->[1] & SCOPING_EL) {
6999                !!!cp ('t377');
7000                last INSCOPE;
7001              }
7002            } # INSCOPE
7003            
7004            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7005            push @$active_formatting_elements, $self->{open_elements}->[-1];
7006            
7007            !!!nack ('t377.1');
7008            !!!next-token;
7009            next B;
7010          } elsif ($token->{tag_name} eq 'button') {
7011            ## has a button element in scope
7012            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7013              my $node = $self->{open_elements}->[$_];
7014              if ($node->[1] & BUTTON_EL) {
7015                !!!cp ('t378');
7016                !!!parse-error (type => 'in button:button', token => $token);
7017                !!!back-token; # <button>
7018                $token = {type => END_TAG_TOKEN, tag_name => 'button',
7019                          line => $token->{line}, column => $token->{column}};
7020                next B;
7021              } elsif ($node->[1] & SCOPING_EL) {
7022                !!!cp ('t379');
7023                last INSCOPE;
7024            }            }
7025            } # INSCOPE
7026              
7027            $reconstruct_active_formatting_elements->($insert_to_current);
7028              
7029            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7030    
7031            ## TODO: associate with $self->{form_element} if defined
7032    
7033            push @$active_formatting_elements, ['#marker', ''];
7034    
7035            !!!nack ('t379.1');
7036            !!!next-token;
7037            next B;
7038          } elsif ({
7039                    xmp => 1,
7040                    iframe => 1,
7041                    noembed => 1,
7042                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7043                    noscript => 0, ## TODO: 1 if scripting is enabled
7044                   }->{$token->{tag_name}}) {
7045            if ($token->{tag_name} eq 'xmp') {
7046              !!!cp ('t381');
7047              $reconstruct_active_formatting_elements->($insert_to_current);
7048            } else {
7049              !!!cp ('t399');
7050            }
7051            ## NOTE: There is an "as if in body" code clone.
7052            $parse_rcdata->(CDATA_CONTENT_MODEL);
7053            next B;
7054          } elsif ($token->{tag_name} eq 'isindex') {
7055            !!!parse-error (type => 'isindex', token => $token);
7056            
7057            if (defined $self->{form_element}) {
7058              !!!cp ('t389');
7059            ## Ignore the token            ## Ignore the token
7060              !!!nack ('t389'); ## NOTE: Not acknowledged.
7061            !!!next-token;            !!!next-token;
7062            redo B;            next B;
7063            } else {
7064              !!!ack ('t391.1');
7065    
7066            ## ISSUE: An issue in spec there            my $at = $token->{attributes};
7067              my $form_attrs;
7068              $form_attrs->{action} = $at->{action} if $at->{action};
7069              my $prompt_attr = $at->{prompt};
7070              $at->{name} = {name => 'name', value => 'isindex'};
7071              delete $at->{action};
7072              delete $at->{prompt};
7073              my @tokens = (
7074                            {type => START_TAG_TOKEN, tag_name => 'form',
7075                             attributes => $form_attrs,
7076                             line => $token->{line}, column => $token->{column}},
7077                            {type => START_TAG_TOKEN, tag_name => 'hr',
7078                             line => $token->{line}, column => $token->{column}},
7079                            {type => START_TAG_TOKEN, tag_name => 'p',
7080                             line => $token->{line}, column => $token->{column}},
7081                            {type => START_TAG_TOKEN, tag_name => 'label',
7082                             line => $token->{line}, column => $token->{column}},
7083                           );
7084              if ($prompt_attr) {
7085                !!!cp ('t390');
7086                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7087                               #line => $token->{line}, column => $token->{column},
7088                              };
7089              } else {
7090                !!!cp ('t391');
7091                push @tokens, {type => CHARACTER_TOKEN,
7092                               data => 'This is a searchable index. Insert your search keywords here: ',
7093                               #line => $token->{line}, column => $token->{column},
7094                              }; # SHOULD
7095                ## TODO: make this configurable
7096              }
7097              push @tokens,
7098                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7099                             line => $token->{line}, column => $token->{column}},
7100                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7101                            {type => END_TAG_TOKEN, tag_name => 'label',
7102                             line => $token->{line}, column => $token->{column}},
7103                            {type => END_TAG_TOKEN, tag_name => 'p',
7104                             line => $token->{line}, column => $token->{column}},
7105                            {type => START_TAG_TOKEN, tag_name => 'hr',
7106                             line => $token->{line}, column => $token->{column}},
7107                            {type => END_TAG_TOKEN, tag_name => 'form',
7108                             line => $token->{line}, column => $token->{column}};
7109              !!!back-token (@tokens);
7110              !!!next-token;
7111              next B;
7112            }
7113          } elsif ($token->{tag_name} eq 'textarea') {
7114            my $tag_name = $token->{tag_name};
7115            my $el;
7116            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7117            
7118            ## TODO: $self->{form_element} if defined
7119            $self->{content_model} = RCDATA_CONTENT_MODEL;
7120            delete $self->{escape}; # MUST
7121            
7122            $insert->($el);
7123            
7124            my $text = '';
7125            !!!nack ('t392.1');
7126            !!!next-token;
7127            if ($token->{type} == CHARACTER_TOKEN) {
7128              $token->{data} =~ s/^\x0A//;
7129              unless (length $token->{data}) {
7130                !!!cp ('t392');
7131                !!!next-token;
7132              } else {
7133                !!!cp ('t393');
7134              }
7135          } else {          } else {
7136            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t394');
7137            }
7138            while ($token->{type} == CHARACTER_TOKEN) {
7139              !!!cp ('t395');
7140              $text .= $token->{data};
7141              !!!next-token;
7142            }
7143            if (length $text) {
7144              !!!cp ('t396');
7145              $el->manakai_append_text ($text);
7146            }
7147            
7148            $self->{content_model} = PCDATA_CONTENT_MODEL;
7149            
7150            if ($token->{type} == END_TAG_TOKEN and
7151                $token->{tag_name} eq $tag_name) {
7152              !!!cp ('t397');
7153              ## Ignore the token
7154            } else {
7155              !!!cp ('t398');
7156              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7157          }          }
       }  
     } 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  
7158          !!!next-token;          !!!next-token;
7159          redo B;          next B;
7160        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'rt' or
7161          my $comment = $self->{document}->create_comment ($token->{data});                 $token->{tag_name} eq 'rp') {
7162          $self->{document}->append_child ($comment);          ## has a |ruby| element in scope
7163            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7164              my $node = $self->{open_elements}->[$_];
7165              if ($node->[1] & RUBY_EL) {
7166                !!!cp ('t398.1');
7167                ## generate implied end tags
7168                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7169                  !!!cp ('t398.2');
7170                  pop @{$self->{open_elements}};
7171                }
7172                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7173                  !!!cp ('t398.3');
7174                  !!!parse-error (type => 'not closed',
7175                                  text => $self->{open_elements}->[-1]->[0]
7176                                      ->manakai_local_name,
7177                                  token => $token);
7178                  pop @{$self->{open_elements}}
7179                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7180                }
7181                last INSCOPE;
7182              } elsif ($node->[1] & SCOPING_EL) {
7183                !!!cp ('t398.4');
7184                last INSCOPE;
7185              }
7186            } # INSCOPE
7187    
7188            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7189    
7190            !!!nack ('t398.5');
7191          !!!next-token;          !!!next-token;
7192          redo B;          redo B;
7193        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'math' or
7194          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                 $token->{tag_name} eq 'svg') {
7195            my $data = $1;          $reconstruct_active_formatting_elements->($insert_to_current);
7196            ## As if in the main phase.  
7197            ## NOTE: The insertion mode in the main phase          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7198            ## just before the phase has been changed to the trailing  
7199            ## end phase is either "after body" or "after frameset".          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7200            $reconstruct_active_formatting_elements->($insert_to_current)  
7201              if $phase eq 'main';          ## "adjust foreign attributes" - done in insert-element-f
7202            
7203            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7204            
7205            if ($self->{self_closing}) {
7206              pop @{$self->{open_elements}};
7207              !!!ack ('t398.1');
7208            } else {
7209              !!!cp ('t398.2');
7210              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7211              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7212              ## mode, "in body" (not "in foreign content") secondary insertion
7213              ## mode, maybe.
7214            }
7215    
7216            !!!next-token;
7217            next B;
7218          } elsif ({
7219                    caption => 1, col => 1, colgroup => 1, frame => 1,
7220                    frameset => 1, head => 1, option => 1, optgroup => 1,
7221                    tbody => 1, td => 1, tfoot => 1, th => 1,
7222                    thead => 1, tr => 1,
7223                   }->{$token->{tag_name}}) {
7224            !!!cp ('t401');
7225            !!!parse-error (type => 'in body',
7226                            text => $token->{tag_name}, token => $token);
7227            ## Ignore the token
7228            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7229            !!!next-token;
7230            next B;
7231            
7232            ## ISSUE: An issue on HTML5 new elements in the spec.
7233          } else {
7234            if ($token->{tag_name} eq 'image') {
7235              !!!cp ('t384');
7236              !!!parse-error (type => 'image', token => $token);
7237              $token->{tag_name} = 'img';
7238            } else {
7239              !!!cp ('t385');
7240            }
7241    
7242            ## NOTE: There is an "as if <br>" code clone.
7243            $reconstruct_active_formatting_elements->($insert_to_current);
7244            
7245            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7246    
7247            if ({
7248                 applet => 1, marquee => 1, object => 1,
7249                }->{$token->{tag_name}}) {
7250              !!!cp ('t380');
7251              push @$active_formatting_elements, ['#marker', ''];
7252              !!!nack ('t380.1');
7253            } elsif ({
7254                      b => 1, big => 1, em => 1, font => 1, i => 1,
7255                      s => 1, small => 1, strile => 1,
7256                      strong => 1, tt => 1, u => 1,
7257                     }->{$token->{tag_name}}) {
7258              !!!cp ('t375');
7259              push @$active_formatting_elements, $self->{open_elements}->[-1];
7260              !!!nack ('t375.1');
7261            } elsif ($token->{tag_name} eq 'input') {
7262              !!!cp ('t388');
7263              ## TODO: associate with $self->{form_element} if defined
7264              pop @{$self->{open_elements}};
7265              !!!ack ('t388.2');
7266            } elsif ({
7267                      area => 1, basefont => 1, bgsound => 1, br => 1,
7268                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7269                      #image => 1,
7270                     }->{$token->{tag_name}}) {
7271              !!!cp ('t388.1');
7272              pop @{$self->{open_elements}};
7273              !!!ack ('t388.3');
7274            } elsif ($token->{tag_name} eq 'select') {
7275              ## TODO: associate with $self->{form_element} if defined
7276            
7277              if ($self->{insertion_mode} & TABLE_IMS or
7278                  $self->{insertion_mode} & BODY_TABLE_IMS or
7279                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7280                !!!cp ('t400.1');
7281                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7282              } else {
7283                !!!cp ('t400.2');
7284                $self->{insertion_mode} = IN_SELECT_IM;
7285              }
7286              !!!nack ('t400.3');
7287            } else {
7288              !!!nack ('t402');
7289            }
7290            
7291            !!!next-token;
7292            next B;
7293          }
7294        } elsif ($token->{type} == END_TAG_TOKEN) {
7295          if ($token->{tag_name} eq 'body') {
7296            ## has a |body| element in scope
7297            my $i;
7298            INSCOPE: {
7299              for (reverse @{$self->{open_elements}}) {
7300                if ($_->[1] & BODY_EL) {
7301                  !!!cp ('t405');
7302                  $i = $_;
7303                  last INSCOPE;
7304                } elsif ($_->[1] & SCOPING_EL) {
7305                  !!!cp ('t405.1');
7306                  last;
7307                }
7308              }
7309    
7310              !!!parse-error (type => 'start tag not allowed',
7311                              text => $token->{tag_name}, token => $token);
7312              ## NOTE: Ignore the token.
7313              !!!next-token;
7314              next B;
7315            } # INSCOPE
7316    
7317            for (@{$self->{open_elements}}) {
7318              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7319                !!!cp ('t403');
7320                !!!parse-error (type => 'not closed',
7321                                text => $_->[0]->manakai_local_name,
7322                                token => $token);
7323                last;
7324              } else {
7325                !!!cp ('t404');
7326              }
7327            }
7328    
7329            $self->{insertion_mode} = AFTER_BODY_IM;
7330            !!!next-token;
7331            next B;
7332          } elsif ($token->{tag_name} eq 'html') {
7333            ## TODO: Update this code.  It seems that the code below is not
7334            ## up-to-date, though it has same effect as speced.
7335            if (@{$self->{open_elements}} > 1 and
7336                $self->{open_elements}->[1]->[1] & BODY_EL) {
7337              ## ISSUE: There is an issue in the spec.
7338              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7339                !!!cp ('t406');
7340                !!!parse-error (type => 'not closed',
7341                                text => $self->{open_elements}->[1]->[0]
7342                                    ->manakai_local_name,
7343                                token => $token);
7344              } else {
7345                !!!cp ('t407');
7346              }
7347              $self->{insertion_mode} = AFTER_BODY_IM;
7348              ## reprocess
7349              next B;
7350            } else {
7351              !!!cp ('t408');
7352              !!!parse-error (type => 'unmatched end tag',
7353                              text => $token->{tag_name}, token => $token);
7354              ## Ignore the token
7355              !!!next-token;
7356              next B;
7357            }
7358          } elsif ({
7359                    address => 1, blockquote => 1, center => 1, dir => 1,
7360                    div => 1, dl => 1, fieldset => 1, listing => 1,
7361                    menu => 1, ol => 1, pre => 1, ul => 1,
7362                    dd => 1, dt => 1, li => 1,
7363                    applet => 1, button => 1, marquee => 1, object => 1,
7364                   }->{$token->{tag_name}}) {
7365            ## has an element in scope
7366            my $i;
7367            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7368              my $node = $self->{open_elements}->[$_];
7369              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7370                !!!cp ('t410');
7371                $i = $_;
7372                last INSCOPE;
7373              } elsif ($node->[1] & SCOPING_EL) {
7374                !!!cp ('t411');
7375                last INSCOPE;
7376              }
7377            } # INSCOPE
7378    
7379            unless (defined $i) { # has an element in scope
7380              !!!cp ('t413');
7381              !!!parse-error (type => 'unmatched end tag',
7382                              text => $token->{tag_name}, token => $token);
7383              ## NOTE: Ignore the token.
7384            } else {
7385              ## Step 1. generate implied end tags
7386              while ({
7387                      ## END_TAG_OPTIONAL_EL
7388                      dd => ($token->{tag_name} ne 'dd'),
7389                      dt => ($token->{tag_name} ne 'dt'),
7390                      li => ($token->{tag_name} ne 'li'),
7391                      p => 1,
7392                      rt => 1,
7393                      rp => 1,
7394                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7395                !!!cp ('t409');
7396                pop @{$self->{open_elements}};
7397              }
7398    
7399              ## Step 2.
7400              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7401                      ne $token->{tag_name}) {
7402                !!!cp ('t412');
7403                !!!parse-error (type => 'not closed',
7404                                text => $self->{open_elements}->[-1]->[0]
7405                                    ->manakai_local_name,
7406                                token => $token);
7407              } else {
7408                !!!cp ('t414');
7409              }
7410    
7411              ## Step 3.
7412              splice @{$self->{open_elements}}, $i;
7413    
7414              ## Step 4.
7415              $clear_up_to_marker->()
7416                  if {
7417                    applet => 1, button => 1, marquee => 1, object => 1,
7418                  }->{$token->{tag_name}};
7419            }
7420            !!!next-token;
7421            next B;
7422          } elsif ($token->{tag_name} eq 'form') {
7423            undef $self->{form_element};
7424    
7425            ## has an element in scope
7426            my $i;
7427            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7428              my $node = $self->{open_elements}->[$_];
7429              if ($node->[1] & FORM_EL) {
7430                !!!cp ('t418');
7431                $i = $_;
7432                last INSCOPE;
7433              } elsif ($node->[1] & SCOPING_EL) {
7434                !!!cp ('t419');
7435                last INSCOPE;
7436              }
7437            } # INSCOPE
7438    
7439            unless (defined $i) { # has an element in scope
7440              !!!cp ('t421');
7441              !!!parse-error (type => 'unmatched end tag',
7442                              text => $token->{tag_name}, token => $token);
7443              ## NOTE: Ignore the token.
7444            } else {
7445              ## Step 1. generate implied end tags
7446              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7447                !!!cp ('t417');
7448                pop @{$self->{open_elements}};
7449              }
7450                        
7451            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
7452              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7453                      ne $token->{tag_name}) {
7454                !!!cp ('t417.1');
7455                !!!parse-error (type => 'not closed',
7456                                text => $self->{open_elements}->[-1]->[0]
7457                                    ->manakai_local_name,
7458                                token => $token);
7459              } else {
7460                !!!cp ('t420');
7461              }  
7462                        
7463            unless (length $token->{data}) {            ## Step 3.
7464              !!!next-token;            splice @{$self->{open_elements}}, $i;
7465              redo B;          }
7466    
7467            !!!next-token;
7468            next B;
7469          } elsif ({
7470                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7471                   }->{$token->{tag_name}}) {
7472            ## has an element in scope
7473            my $i;
7474            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7475              my $node = $self->{open_elements}->[$_];
7476              if ($node->[1] & HEADING_EL) {
7477                !!!cp ('t423');
7478                $i = $_;
7479                last INSCOPE;
7480              } elsif ($node->[1] & SCOPING_EL) {
7481                !!!cp ('t424');
7482                last INSCOPE;
7483            }            }
7484            } # INSCOPE
7485    
7486            unless (defined $i) { # has an element in scope
7487              !!!cp ('t425.1');
7488              !!!parse-error (type => 'unmatched end tag',
7489                              text => $token->{tag_name}, token => $token);
7490              ## NOTE: Ignore the token.
7491            } else {
7492              ## Step 1. generate implied end tags
7493              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7494                !!!cp ('t422');
7495                pop @{$self->{open_elements}};
7496              }
7497              
7498              ## Step 2.
7499              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7500                      ne $token->{tag_name}) {
7501                !!!cp ('t425');
7502                !!!parse-error (type => 'unmatched end tag',
7503                                text => $token->{tag_name}, token => $token);
7504              } else {
7505                !!!cp ('t426');
7506              }
7507    
7508              ## Step 3.
7509              splice @{$self->{open_elements}}, $i;
7510          }          }
7511            
7512            !!!next-token;
7513            next B;
7514          } elsif ($token->{tag_name} eq 'p') {
7515            ## has an element in scope
7516            my $i;
7517            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7518              my $node = $self->{open_elements}->[$_];
7519              if ($node->[1] & P_EL) {
7520                !!!cp ('t410.1');
7521                $i = $_;
7522                last INSCOPE;
7523              } elsif ($node->[1] & SCOPING_EL) {
7524                !!!cp ('t411.1');
7525                last INSCOPE;
7526              }
7527            } # INSCOPE
7528    
7529          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7530          $phase = 'main';            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7531          ## reprocess                    ne $token->{tag_name}) {
7532          redo B;              !!!cp ('t412.1');
7533        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
7534                 $token->{type} eq 'end tag') {                              text => $self->{open_elements}->[-1]->[0]
7535          !!!parse-error (type => 'after html:'.$token->{tag_name});                                  ->manakai_local_name,
7536          $phase = 'main';                              token => $token);
7537          ## reprocess            } else {
7538          redo B;              !!!cp ('t414.1');
7539        } elsif ($token->{type} eq 'end-of-file') {            }
7540          ## Stop parsing  
7541          last B;            splice @{$self->{open_elements}}, $i;
7542            } else {
7543              !!!cp ('t413.1');
7544              !!!parse-error (type => 'unmatched end tag',
7545                              text => $token->{tag_name}, token => $token);
7546    
7547              !!!cp ('t415.1');
7548              ## As if <p>, then reprocess the current token
7549              my $el;
7550              !!!create-element ($el, $HTML_NS, 'p',, $token);
7551              $insert->($el);
7552              ## NOTE: Not inserted into |$self->{open_elements}|.
7553            }
7554    
7555            !!!next-token;
7556            next B;
7557          } elsif ({
7558                    a => 1,
7559                    b => 1, big => 1, em => 1, font => 1, i => 1,
7560                    nobr => 1, s => 1, small => 1, strile => 1,
7561                    strong => 1, tt => 1, u => 1,
7562                   }->{$token->{tag_name}}) {
7563            !!!cp ('t427');
7564            $formatting_end_tag->($token);
7565            next B;
7566          } elsif ($token->{tag_name} eq 'br') {
7567            !!!cp ('t428');
7568            !!!parse-error (type => 'unmatched end tag',
7569                            text => 'br', token => $token);
7570    
7571            ## As if <br>
7572            $reconstruct_active_formatting_elements->($insert_to_current);
7573            
7574            my $el;
7575            !!!create-element ($el, $HTML_NS, 'br',, $token);
7576            $insert->($el);
7577            
7578            ## Ignore the token.
7579            !!!next-token;
7580            next B;
7581          } elsif ({
7582                    caption => 1, col => 1, colgroup => 1, frame => 1,
7583                    frameset => 1, head => 1, option => 1, optgroup => 1,
7584                    tbody => 1, td => 1, tfoot => 1, th => 1,
7585                    thead => 1, tr => 1,
7586                    area => 1, basefont => 1, bgsound => 1,
7587                    embed => 1, hr => 1, iframe => 1, image => 1,
7588                    img => 1, input => 1, isindex => 1, noembed => 1,
7589                    noframes => 1, param => 1, select => 1, spacer => 1,
7590                    table => 1, textarea => 1, wbr => 1,
7591                    noscript => 0, ## TODO: if scripting is enabled
7592                   }->{$token->{tag_name}}) {
7593            !!!cp ('t429');
7594            !!!parse-error (type => 'unmatched end tag',
7595                            text => $token->{tag_name}, token => $token);
7596            ## Ignore the token
7597            !!!next-token;
7598            next B;
7599            
7600            ## ISSUE: Issue on HTML5 new elements in spec
7601            
7602        } else {        } else {
7603          die "$0: $token->{type}: Unknown token";          ## Step 1
7604            my $node_i = -1;
7605            my $node = $self->{open_elements}->[$node_i];
7606    
7607            ## Step 2
7608            S2: {
7609              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7610                ## Step 1
7611                ## generate implied end tags
7612                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7613                  !!!cp ('t430');
7614                  ## NOTE: |<ruby><rt></ruby>|.
7615                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7616                  ## which seems wrong.
7617                  pop @{$self->{open_elements}};
7618                  $node_i++;
7619                }
7620            
7621                ## Step 2
7622                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7623                        ne $token->{tag_name}) {
7624                  !!!cp ('t431');
7625                  ## NOTE: <x><y></x>
7626                  !!!parse-error (type => 'not closed',
7627                                  text => $self->{open_elements}->[-1]->[0]
7628                                      ->manakai_local_name,
7629                                  token => $token);
7630                } else {
7631                  !!!cp ('t432');
7632                }
7633                
7634                ## Step 3
7635                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7636    
7637                !!!next-token;
7638                last S2;
7639              } else {
7640                ## Step 3
7641                if (not ($node->[1] & FORMATTING_EL) and
7642                    #not $phrasing_category->{$node->[1]} and
7643                    ($node->[1] & SPECIAL_EL or
7644                     $node->[1] & SCOPING_EL)) {
7645                  !!!cp ('t433');
7646                  !!!parse-error (type => 'unmatched end tag',
7647                                  text => $token->{tag_name}, token => $token);
7648                  ## Ignore the token
7649                  !!!next-token;
7650                  last S2;
7651                }
7652    
7653                !!!cp ('t434');
7654              }
7655              
7656              ## Step 4
7657              $node_i--;
7658              $node = $self->{open_elements}->[$node_i];
7659              
7660              ## Step 5;
7661              redo S2;
7662            } # S2
7663            next B;
7664        }        }
7665      }      }
7666        next B;
7667      } continue { # B
7668        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7669          ## NOTE: The code below is executed in cases where it does not have
7670          ## to be, but it it is harmless even in those cases.
7671          ## has an element in scope
7672          INSCOPE: {
7673            for (reverse 0..$#{$self->{open_elements}}) {
7674              my $node = $self->{open_elements}->[$_];
7675              if ($node->[1] & FOREIGN_EL) {
7676                last INSCOPE;
7677              } elsif ($node->[1] & SCOPING_EL) {
7678                last;
7679              }
7680            }
7681            
7682            ## NOTE: No foreign element in scope.
7683            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7684          } # INSCOPE
7685        }
7686    } # B    } # B
7687    
7688    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4840  sub _tree_construction_main ($) { Line 7690  sub _tree_construction_main ($) {
7690    ## TODO: script stuffs    ## TODO: script stuffs
7691  } # _tree_construct_main  } # _tree_construct_main
7692    
7693  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7694    my $class = shift;    my $class = shift;
7695    my $node = shift;    my $node = shift;
7696    my $s = \$_[0];    my $s = \$_[0];
7697    my $onerror = $_[1];    my $onerror = $_[1];
7698      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7699    
7700      ## ISSUE: Should {confident} be true?
7701    
7702    my $nt = $node->node_type;    my $nt = $node->node_type;
7703    if ($nt == 9) {    if ($nt == 9) {
# Line 4861  sub set_inner_html ($$$) { Line 7714  sub set_inner_html ($$$) {
7714      }      }
7715    
7716      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7717      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7718    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7719      ## TODO: If non-html element      ## TODO: If non-html element
7720    
7721      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7722    
7723    ## TODO: Support for $get_wrapper
7724    
7725      ## Step 1 # MUST      ## Step 1 # MUST
7726      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
7727      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
7728        $doc->manakai_is_html (1);
7729      my $p = $class->new;      my $p = $class->new;
7730      $p->{document} = $doc;      $p->{document} = $doc;
7731    
7732      ## Step 9 # MUST      ## Step 8 # MUST
7733      my $i = 0;      my $i = 0;
7734      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7735      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7736      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7737        my $self = shift;        my $self = shift;
7738        $self->{next_input_character} = -1 and return if $i >= length $$s;  
7739        $self->{next_input_character} = ord substr $$s, $i++, 1;        pop @{$self->{prev_char}};
7740        $column++;        unshift @{$self->{prev_char}}, $self->{next_char};
7741    
7742        if ($self->{next_input_character} == 0x000A) { # LF        $self->{next_char} = -1 and return if $i >= length $$s;
7743          $line++;        $self->{next_char} = ord substr $$s, $i++, 1;
7744          $column = 0;  
7745        } elsif ($self->{next_input_character} == 0x000D) { # CR        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7746          if ($i >= length $$s) {        $p->{column}++;
7747            #  
7748          if ($self->{next_char} == 0x000A) { # LF
7749            $p->{line}++;
7750            $p->{column} = 0;
7751            !!!cp ('i1');
7752          } elsif ($self->{next_char} == 0x000D) { # CR
7753            $i++ if substr ($$s, $i, 1) eq "\x0A";
7754            $self->{next_char} = 0x000A; # LF # MUST
7755            $p->{line}++;
7756            $p->{column} = 0;
7757            !!!cp ('i2');
7758          } elsif ($self->{next_char} > 0x10FFFF) {
7759            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7760            !!!cp ('i3');
7761          } elsif ($self->{next_char} == 0x0000) { # NULL
7762            !!!cp ('i4');
7763            !!!parse-error (type => 'NULL');
7764            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7765          } elsif ($self->{next_char} <= 0x0008 or
7766                   (0x000E <= $self->{next_char} and
7767                    $self->{next_char} <= 0x001F) or
7768                   (0x007F <= $self->{next_char} and
7769                    $self->{next_char} <= 0x009F) or
7770                   (0xD800 <= $self->{next_char} and
7771                    $self->{next_char} <= 0xDFFF) or
7772                   (0xFDD0 <= $self->{next_char} and
7773                    $self->{next_char} <= 0xFDDF) or
7774                   {
7775                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7776                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7777                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7778                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7779                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7780                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7781                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7782                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7783                    0x10FFFE => 1, 0x10FFFF => 1,
7784                   }->{$self->{next_char}}) {
7785            !!!cp ('i4.1');
7786            if ($self->{next_char} < 0x10000) {
7787              !!!parse-error (type => 'control char',
7788                              text => (sprintf 'U+%04X', $self->{next_char}));
7789          } else {          } else {
7790            my $next_char = ord substr $$s, $i++, 1;            !!!parse-error (type => 'control char',
7791            if ($next_char == 0x000A) { # LF                            text => (sprintf 'U-%08X', $self->{next_char}));
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
7792          }          }
         $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  
7793        }        }
7794      };      };
7795            $p->{prev_char} = [-1, -1, -1];
7796        $p->{next_char} = -1;
7797    
7798        $p->{read_until} = sub {
7799          ## TODO: ...
7800          return 0;
7801        }; # $p->{read_until};
7802    
7803      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7804        my (%opt) = @_;        my (%opt) = @_;
7805        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7806          my $column = $opt{column};
7807          if (defined $opt{token} and defined $opt{token}->{line}) {
7808            $line = $opt{token}->{line};
7809            $column = $opt{token}->{column};
7810          }
7811          warn "Parse error ($opt{type}) at line $line column $column\n";
7812      };      };
7813      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7814        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7815      };      };
7816            
7817      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7818      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7819    
7820      ## Step 2      ## Step 2
7821      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7822      $p->{content_model_flag} = {      $p->{content_model} = {
7823        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7824        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7825        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7826        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7827        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7828        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7829        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7830        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7831        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7832        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7833      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7834         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7835            unless defined $p->{content_model};
7836            ## ISSUE: What is "the name of the element"? local name?
7837    
7838      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7839          ## TODO: Foreign element OK?
7840    
7841      ## Step 4      ## Step 3
7842      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7843        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7844    
7845      ## Step 5 # MUST      ## Step 4 # MUST
7846      $doc->append_child ($root);      $doc->append_child ($root);
7847    
7848      ## Step 6 # MUST      ## Step 5 # MUST
7849      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7850    
7851      undef $p->{head_element};      undef $p->{head_element};
7852    
7853      ## Step 7 # MUST      ## Step 6 # MUST
7854      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7855    
7856      ## Step 8 # MUST      ## Step 7 # MUST
7857      my $anode = $node;      my $anode = $node;
7858      AN: while (defined $anode) {      AN: while (defined $anode) {
7859        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7860          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7861          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7862            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7863                !!!cp ('i5');
7864              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7865              last AN;              last AN;
7866            }            }
# Line 4966  sub set_inner_html ($$$) { Line 7869  sub set_inner_html ($$$) {
7869        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7870      } # AN      } # AN
7871            
7872      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7873      {      {
7874        my $self = $p;        my $self = $p;
7875        !!!next-token;        !!!next-token;
7876      }      }
7877      $p->_tree_construction_main;      $p->_tree_construction_main;
7878    
7879      ## Step 11 # MUST      ## Step 10 # MUST
7880      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7881      for (@cn) {      for (@cn) {
7882        $node->remove_child ($_);        $node->remove_child ($_);
7883      }      }
7884      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7885    
7886      ## Step 12 # MUST      ## Step 11 # MUST
7887      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7888      for (@cn) {      for (@cn) {
7889          $this_doc->adopt_node ($_);
7890        $node->append_child ($_);        $node->append_child ($_);
7891      }      }
7892      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
7893    
7894      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7895    
7896        delete $p->{parse_error}; # delete loop
7897    } else {    } else {
7898      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";
7899    }    }
# Line 4996  sub set_inner_html ($$$) { Line 7901  sub set_inner_html ($$$) {
7901    
7902  } # tree construction stage  } # tree construction stage
7903    
7904  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7905    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  
7906    
7907  1;  1;
7908  # $Date$  # $Date$

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.172

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24