/[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.13 by wakaba, Sat Jun 23 05:29:48 2007 UTC revision 1.177 by wakaba, Sun Sep 14 09:05:54 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];
362  my $scoping_category = {  
363    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    my $onerror = $_[2] || sub {
364    table => 1, td => 1, th => 1,      my (%opt) = @_;
365  };      warn "Parse error ($opt{type})\n";
366  my $formatting_category = {    };
367    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,    $self->{parse_error} = $onerror; # updated later by parse_char_string
368    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
369  };    my $get_wrapper = $_[3] || sub ($) {
370  # $phrasing_category: all other elements      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  sub parse_string ($$$;$) {        !!!parse-error (type => 'charset label detected',
544    my $self = shift->new;                        text => $self->{input_encoding},
545    my $s = \$_[0];                        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                        line => $self->{line}, column => $self->{column} + 1,
565                        %opt, type => $type);
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    
649      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
650      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      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->{next_input_character} = -1 and return if $i >= length $$s;      ($self->{line_prev}, $self->{column_prev})
663      $self->{next_input_character} = ord substr $$s, $i++, 1;          = ($self->{line}, $self->{column});
664      $column++;      $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');  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
712      }      }
713    };    };
714    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
715    $self->{next_input_character} = -1;    $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 397  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 450  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    ## 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} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
974          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
975            unless ($self->{escape}) {            unless ($self->{escape}) {
976              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
977                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
978                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
979                  !!!cp (3);
980                $self->{escape} = 1;                $self->{escape} = 1;
981                } else {
982                  !!!cp (4);
983              }              }
984              } else {
985                !!!cp (5);
986            }            }
987          }          }
988                    
989          #          #
990        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
991          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
992              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
993               not $self->{escape})) {               not $self->{escape})) {
994            $self->{state} = 'tag open';            !!!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} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1003          if ($self->{escape} and          if ($self->{escape} and
1004              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1005               $self->{content_model_flag} eq 'CDATA')) {            if ($self->{prev_char}->[0] == 0x002D and # -
1006            if ($self->{prev_input_character}->[0] == 0x002D and # -                $self->{prev_char}->[1] == 0x002D) { # -
1007                $self->{prev_input_character}->[1] == 0x002D) { # -              !!!cp (8);
1008              delete $self->{escape};              delete $self->{escape};
1009              } else {
1010                !!!cp (9);
1011            }            }
1012            } else {
1013              !!!cp (10);
1014          }          }
1015                    
1016          #          #
1017        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1018          !!!emit ({type => 'end-of-file'});          !!!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                   value => '',
1572                   line => $self->{line}, column => $self->{column}};
1573            $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;
1580          redo A;          redo A;
1581        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003C or # <  
                $self->{next_input_character} == -1) {  
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            $self->{read_until}->($self->{current_attribute}->{value},
1747                                  q["&],
1748                                  length $self->{current_attribute}->{value});
1749    
1750          ## Stay in the state          ## Stay in the state
1751          !!!next-input-character;          !!!next-input-character;
1752          redo A;          redo A;
1753        }        }
1754      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1755        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1756          $self->{state} = 'before attribute name';          !!!cp (101);
1757            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1758            !!!next-input-character;
1759            redo A;
1760          } elsif ($self->{next_char} == 0x0026) { # &
1761            !!!cp (102);
1762            ## NOTE: In the spec, the tokenizer is switched to the
1763            ## "entity in attribute value state".  In this implementation, the
1764            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1765            ## implementation of the "consume a character reference" algorithm.
1766            $self->{entity_additional} = 0x0027; # '
1767            $self->{prev_state} = $self->{state};
1768            $self->{state} = ENTITY_STATE;
1769          !!!next-input-character;          !!!next-input-character;
1770          redo A;          redo A;
1771        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == -1) {
         $self->{last_attribute_value_state} = 'attribute value (single-quoted)';  
         $self->{state} = 'entity in attribute value';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1772          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1773          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1774              !!!cp (103);
1775            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1776          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1777            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1778            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1779                !!!cp (104);
1780              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1781              } else {
1782                ## NOTE: This state should never be reached.
1783                !!!cp (105);
1784            }            }
1785          } else {          } else {
1786            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1787          }          }
1788          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1789          ## reconsume          ## reconsume
1790    
1791          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1792    
1793          redo A;          redo A;
1794        } else {        } else {
1795          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1796            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1797            $self->{read_until}->($self->{current_attribute}->{value},
1798                                  q['&],
1799                                  length $self->{current_attribute}->{value});
1800    
1801          ## Stay in the state          ## Stay in the state
1802          !!!next-input-character;          !!!next-input-character;
1803          redo A;          redo A;
1804        }        }
1805      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1806        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1807            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1808            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1809            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1810            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1811          $self->{state} = 'before attribute name';          !!!cp (107);
1812          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1813          redo A;          !!!next-input-character;
1814        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1815          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1816          $self->{state} = 'entity in attribute value';          !!!cp (108);
1817          !!!next-input-character;          ## NOTE: In the spec, the tokenizer is switched to the
1818          redo A;          ## "entity in attribute value state".  In this implementation, the
1819        } elsif ($self->{next_input_character} == 0x003E) { # >          ## tokenizer is switched to the |ENTITY_STATE|, which is an
1820          if ($self->{current_token}->{type} eq 'start tag') {          ## implementation of the "consume a character reference" algorithm.
1821            $self->{entity_additional} = -1;
1822            $self->{prev_state} = $self->{state};
1823            $self->{state} = ENTITY_STATE;
1824            !!!next-input-character;
1825            redo A;
1826          } elsif ($self->{next_char} == 0x003E) { # >
1827            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1828              !!!cp (109);
1829            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1830          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1831            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1832            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1833                !!!cp (110);
1834              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1835              } else {
1836                ## NOTE: This state should never be reached.
1837                !!!cp (111);
1838            }            }
1839          } else {          } else {
1840            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1841          }          }
1842          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1843          !!!next-input-character;          !!!next-input-character;
1844    
1845          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1846    
1847          redo A;          redo A;
1848        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1849          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1850          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1851              !!!cp (112);
1852            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1853          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1854            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1855            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1856                !!!cp (113);
1857              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1858              } else {
1859                ## NOTE: This state should never be reached.
1860                !!!cp (114);
1861            }            }
1862          } else {          } else {
1863            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1864          }          }
1865          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1866          ## reconsume          ## reconsume
1867    
1868          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1869    
1870          redo A;          redo A;
1871        } else {        } else {
1872          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1873                 0x0022 => 1, # "
1874                 0x0027 => 1, # '
1875                 0x003D => 1, # =
1876                }->{$self->{next_char}}) {
1877              !!!cp (115);
1878              !!!parse-error (type => 'bad attribute value');
1879            } else {
1880              !!!cp (116);
1881            }
1882            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1883            $self->{read_until}->($self->{current_attribute}->{value},
1884                                  q["'=& >],
1885                                  length $self->{current_attribute}->{value});
1886    
1887          ## Stay in the state          ## Stay in the state
1888          !!!next-input-character;          !!!next-input-character;
1889          redo A;          redo A;
1890        }        }
1891      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1892        my $token = $self->_tokenize_attempt_to_consume_an_entity;        if ($self->{next_char} == 0x0009 or # HT
1893              $self->{next_char} == 0x000A or # LF
1894              $self->{next_char} == 0x000B or # VT
1895              $self->{next_char} == 0x000C or # FF
1896              $self->{next_char} == 0x0020) { # SP
1897            !!!cp (118);
1898            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1899            !!!next-input-character;
1900            redo A;
1901          } elsif ($self->{next_char} == 0x003E) { # >
1902            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1903              !!!cp (119);
1904              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1905            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1906              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1907              if ($self->{current_token}->{attributes}) {
1908                !!!cp (120);
1909                !!!parse-error (type => 'end tag attribute');
1910              } else {
1911                ## NOTE: This state should never be reached.
1912                !!!cp (121);
1913              }
1914            } else {
1915              die "$0: $self->{current_token}->{type}: Unknown token type";
1916            }
1917            $self->{state} = DATA_STATE;
1918            !!!next-input-character;
1919    
1920        unless (defined $token) {          !!!emit ($self->{current_token}); # start tag or end tag
1921          $self->{current_attribute}->{value} .= '&';  
1922            redo A;
1923          } elsif ($self->{next_char} == 0x002F) { # /
1924            !!!cp (122);
1925            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1926            !!!next-input-character;
1927            redo A;
1928          } elsif ($self->{next_char} == -1) {
1929            !!!parse-error (type => 'unclosed tag');
1930            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1931              !!!cp (122.3);
1932              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1933            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1934              if ($self->{current_token}->{attributes}) {
1935                !!!cp (122.1);
1936                !!!parse-error (type => 'end tag attribute');
1937              } else {
1938                ## NOTE: This state should never be reached.
1939                !!!cp (122.2);
1940              }
1941            } else {
1942              die "$0: $self->{current_token}->{type}: Unknown token type";
1943            }
1944            $self->{state} = DATA_STATE;
1945            ## Reconsume.
1946            !!!emit ($self->{current_token}); # start tag or end tag
1947            redo A;
1948        } else {        } else {
1949          $self->{current_attribute}->{value} .= $token->{data};          !!!cp ('124.1');
1950          ## ISSUE: spec says "append the returned character token to the current attribute's value"          !!!parse-error (type => 'no space between attributes');
1951            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1952            ## reconsume
1953            redo A;
1954        }        }
1955        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1956          if ($self->{next_char} == 0x003E) { # >
1957            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1958              !!!cp ('124.2');
1959              !!!parse-error (type => 'nestc', token => $self->{current_token});
1960              ## TODO: Different type than slash in start tag
1961              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1962              if ($self->{current_token}->{attributes}) {
1963                !!!cp ('124.4');
1964                !!!parse-error (type => 'end tag attribute');
1965              } else {
1966                !!!cp ('124.5');
1967              }
1968              ## TODO: Test |<title></title/>|
1969            } else {
1970              !!!cp ('124.3');
1971              $self->{self_closing} = 1;
1972            }
1973    
1974        $self->{state} = $self->{last_attribute_value_state};          $self->{state} = DATA_STATE;
1975        # 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  
1976    
1977            !!!emit ($token);          !!!emit ($self->{current_token}); # start tag or end tag
1978    
1979            redo A;          redo A;
1980          } elsif ($self->{next_char} == -1) {
1981            !!!parse-error (type => 'unclosed tag');
1982            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1983              !!!cp (124.7);
1984              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1985            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1986              if ($self->{current_token}->{attributes}) {
1987                !!!cp (124.5);
1988                !!!parse-error (type => 'end tag attribute');
1989              } else {
1990                ## NOTE: This state should never be reached.
1991                !!!cp (124.6);
1992              }
1993          } else {          } else {
1994            $token->{data} .= chr ($self->{next_input_character});            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!next-input-character;  
           redo BC;  
1995          }          }
1996        } # BC          $self->{state} = DATA_STATE;
1997      } elsif ($self->{state} eq 'markup declaration open') {          ## Reconsume.
1998            !!!emit ($self->{current_token}); # start tag or end tag
1999            redo A;
2000          } else {
2001            !!!cp ('124.4');
2002            !!!parse-error (type => 'nestc');
2003            ## TODO: This error type is wrong.
2004            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2005            ## Reconsume.
2006            redo A;
2007          }
2008        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2009        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2010    
2011        my @next_char;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2012        push @next_char, $self->{next_input_character};        ## consumes characters one-by-one basis.
2013                
2014        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x003E) { # >
2015            !!!cp (124);
2016            $self->{state} = DATA_STATE;
2017          !!!next-input-character;          !!!next-input-character;
2018          push @next_char, $self->{next_input_character};  
2019          if ($self->{next_input_character} == 0x002D) { # -          !!!emit ($self->{current_token}); # comment
2020            $self->{current_token} = {type => 'comment', data => ''};          redo A;
2021            $self->{state} = 'comment';        } elsif ($self->{next_char} == -1) {
2022            !!!next-input-character;          !!!cp (125);
2023            redo A;          $self->{state} = DATA_STATE;
2024          }          ## reconsume
2025        } elsif ($self->{next_input_character} == 0x0044 or # D  
2026                 $self->{next_input_character} == 0x0064) { # d          !!!emit ($self->{current_token}); # comment
2027            redo A;
2028          } else {
2029            !!!cp (126);
2030            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2031            $self->{read_until}->($self->{current_token}->{data},
2032                                  q[>],
2033                                  length $self->{current_token}->{data});
2034    
2035            ## Stay in the state.
2036          !!!next-input-character;          !!!next-input-character;
2037          push @next_char, $self->{next_input_character};          redo A;
2038          if ($self->{next_input_character} == 0x004F or # O        }
2039              $self->{next_input_character} == 0x006F) { # o      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2040            !!!next-input-character;        ## (only happen if PCDATA state)
2041            push @next_char, $self->{next_input_character};        
2042            if ($self->{next_input_character} == 0x0043 or # C        if ($self->{next_char} == 0x002D) { # -
2043                $self->{next_input_character} == 0x0063) { # c          !!!cp (133);
2044              !!!next-input-character;          $self->{state} = MD_HYPHEN_STATE;
2045              push @next_char, $self->{next_input_character};          !!!next-input-character;
2046              if ($self->{next_input_character} == 0x0054 or # T          redo A;
2047                  $self->{next_input_character} == 0x0074) { # t        } elsif ($self->{next_char} == 0x0044 or # D
2048                !!!next-input-character;                 $self->{next_char} == 0x0064) { # d
2049                push @next_char, $self->{next_input_character};          ## ASCII case-insensitive.
2050                if ($self->{next_input_character} == 0x0059 or # Y          !!!cp (130);
2051                    $self->{next_input_character} == 0x0079) { # y          $self->{state} = MD_DOCTYPE_STATE;
2052                  !!!next-input-character;          $self->{state_keyword} = chr $self->{next_char};
2053                  push @next_char, $self->{next_input_character};          !!!next-input-character;
2054                  if ($self->{next_input_character} == 0x0050 or # P          redo A;
2055                      $self->{next_input_character} == 0x0070) { # p        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2056                    !!!next-input-character;                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2057                    push @next_char, $self->{next_input_character};                 $self->{next_char} == 0x005B) { # [
2058                    if ($self->{next_input_character} == 0x0045 or # E          !!!cp (135.4);                
2059                        $self->{next_input_character} == 0x0065) { # e          $self->{state} = MD_CDATA_STATE;
2060                      ## ISSUE: What a stupid code this is!          $self->{state_keyword} = '[';
2061                      $self->{state} = 'DOCTYPE';          !!!next-input-character;
2062                      !!!next-input-character;          redo A;
2063                      redo A;        } else {
2064                    }          !!!cp (136);
                 }  
               }  
             }  
           }  
         }  
2065        }        }
2066    
2067        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment',
2068        $self->{next_input_character} = shift @next_char;                        line => $self->{line_prev},
2069        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2070        $self->{state} = 'bogus comment';        ## Reconsume.
2071          $self->{state} = BOGUS_COMMENT_STATE;
2072          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2073                                    line => $self->{line_prev},
2074                                    column => $self->{column_prev} - 1,
2075                                   };
2076        redo A;        redo A;
2077              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2078        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2079        ## 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);
2080      } elsif ($self->{state} eq 'comment') {          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2081        if ($self->{next_input_character} == 0x002D) { # -                                    line => $self->{line_prev},
2082          $self->{state} = 'comment dash';                                    column => $self->{column_prev} - 2,
2083                                     };
2084            $self->{state} = COMMENT_START_STATE;
2085            !!!next-input-character;
2086            redo A;
2087          } else {
2088            !!!cp (128);
2089            !!!parse-error (type => 'bogus comment',
2090                            line => $self->{line_prev},
2091                            column => $self->{column_prev} - 2);
2092            $self->{state} = BOGUS_COMMENT_STATE;
2093            ## Reconsume.
2094            $self->{current_token} = {type => COMMENT_TOKEN,
2095                                      data => '-',
2096                                      line => $self->{line_prev},
2097                                      column => $self->{column_prev} - 2,
2098                                     };
2099            redo A;
2100          }
2101        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2102          ## ASCII case-insensitive.
2103          if ($self->{next_char} == [
2104                undef,
2105                0x004F, # O
2106                0x0043, # C
2107                0x0054, # T
2108                0x0059, # Y
2109                0x0050, # P
2110              ]->[length $self->{state_keyword}] or
2111              $self->{next_char} == [
2112                undef,
2113                0x006F, # o
2114                0x0063, # c
2115                0x0074, # t
2116                0x0079, # y
2117                0x0070, # p
2118              ]->[length $self->{state_keyword}]) {
2119            !!!cp (131);
2120            ## Stay in the state.
2121            $self->{state_keyword} .= chr $self->{next_char};
2122            !!!next-input-character;
2123            redo A;
2124          } elsif ((length $self->{state_keyword}) == 6 and
2125                   ($self->{next_char} == 0x0045 or # E
2126                    $self->{next_char} == 0x0065)) { # e
2127            !!!cp (129);
2128            $self->{state} = DOCTYPE_STATE;
2129            $self->{current_token} = {type => DOCTYPE_TOKEN,
2130                                      quirks => 1,
2131                                      line => $self->{line_prev},
2132                                      column => $self->{column_prev} - 7,
2133                                     };
2134            !!!next-input-character;
2135            redo A;
2136          } else {
2137            !!!cp (132);        
2138            !!!parse-error (type => 'bogus comment',
2139                            line => $self->{line_prev},
2140                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2141            $self->{state} = BOGUS_COMMENT_STATE;
2142            ## Reconsume.
2143            $self->{current_token} = {type => COMMENT_TOKEN,
2144                                      data => $self->{state_keyword},
2145                                      line => $self->{line_prev},
2146                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2147                                     };
2148            redo A;
2149          }
2150        } elsif ($self->{state} == MD_CDATA_STATE) {
2151          if ($self->{next_char} == {
2152                '[' => 0x0043, # C
2153                '[C' => 0x0044, # D
2154                '[CD' => 0x0041, # A
2155                '[CDA' => 0x0054, # T
2156                '[CDAT' => 0x0041, # A
2157              }->{$self->{state_keyword}}) {
2158            !!!cp (135.1);
2159            ## Stay in the state.
2160            $self->{state_keyword} .= chr $self->{next_char};
2161            !!!next-input-character;
2162            redo A;
2163          } elsif ($self->{state_keyword} eq '[CDATA' and
2164                   $self->{next_char} == 0x005B) { # [
2165            !!!cp (135.2);
2166            $self->{current_token} = {type => CHARACTER_TOKEN,
2167                                      data => '',
2168                                      line => $self->{line_prev},
2169                                      column => $self->{column_prev} - 7};
2170            $self->{state} = CDATA_SECTION_STATE;
2171            !!!next-input-character;
2172            redo A;
2173          } else {
2174            !!!cp (135.3);
2175            !!!parse-error (type => 'bogus comment',
2176                            line => $self->{line_prev},
2177                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2178            $self->{state} = BOGUS_COMMENT_STATE;
2179            ## Reconsume.
2180            $self->{current_token} = {type => COMMENT_TOKEN,
2181                                      data => $self->{state_keyword},
2182                                      line => $self->{line_prev},
2183                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2184                                     };
2185            redo A;
2186          }
2187        } elsif ($self->{state} == COMMENT_START_STATE) {
2188          if ($self->{next_char} == 0x002D) { # -
2189            !!!cp (137);
2190            $self->{state} = COMMENT_START_DASH_STATE;
2191            !!!next-input-character;
2192            redo A;
2193          } elsif ($self->{next_char} == 0x003E) { # >
2194            !!!cp (138);
2195            !!!parse-error (type => 'bogus comment');
2196            $self->{state} = DATA_STATE;
2197          !!!next-input-character;          !!!next-input-character;
2198    
2199            !!!emit ($self->{current_token}); # comment
2200    
2201          redo A;          redo A;
2202        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2203            !!!cp (139);
2204          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2205          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2206          ## reconsume          ## reconsume
2207    
2208          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2209    
2210          redo A;          redo A;
2211        } else {        } else {
2212          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (140);
2213            $self->{current_token}->{data} # comment
2214                .= chr ($self->{next_char});
2215            $self->{state} = COMMENT_STATE;
2216            !!!next-input-character;
2217            redo A;
2218          }
2219        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2220          if ($self->{next_char} == 0x002D) { # -
2221            !!!cp (141);
2222            $self->{state} = COMMENT_END_STATE;
2223            !!!next-input-character;
2224            redo A;
2225          } elsif ($self->{next_char} == 0x003E) { # >
2226            !!!cp (142);
2227            !!!parse-error (type => 'bogus comment');
2228            $self->{state} = DATA_STATE;
2229            !!!next-input-character;
2230    
2231            !!!emit ($self->{current_token}); # comment
2232    
2233            redo A;
2234          } elsif ($self->{next_char} == -1) {
2235            !!!cp (143);
2236            !!!parse-error (type => 'unclosed comment');
2237            $self->{state} = DATA_STATE;
2238            ## reconsume
2239    
2240            !!!emit ($self->{current_token}); # comment
2241    
2242            redo A;
2243          } else {
2244            !!!cp (144);
2245            $self->{current_token}->{data} # comment
2246                .= '-' . chr ($self->{next_char});
2247            $self->{state} = COMMENT_STATE;
2248            !!!next-input-character;
2249            redo A;
2250          }
2251        } elsif ($self->{state} == COMMENT_STATE) {
2252          if ($self->{next_char} == 0x002D) { # -
2253            !!!cp (145);
2254            $self->{state} = COMMENT_END_DASH_STATE;
2255            !!!next-input-character;
2256            redo A;
2257          } elsif ($self->{next_char} == -1) {
2258            !!!cp (146);
2259            !!!parse-error (type => 'unclosed comment');
2260            $self->{state} = DATA_STATE;
2261            ## reconsume
2262    
2263            !!!emit ($self->{current_token}); # comment
2264    
2265            redo A;
2266          } else {
2267            !!!cp (147);
2268            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2269            $self->{read_until}->($self->{current_token}->{data},
2270                                  q[-],
2271                                  length $self->{current_token}->{data});
2272    
2273          ## Stay in the state          ## Stay in the state
2274          !!!next-input-character;          !!!next-input-character;
2275          redo A;          redo A;
2276        }        }
2277      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2278        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2279          $self->{state} = 'comment end';          !!!cp (148);
2280            $self->{state} = COMMENT_END_STATE;
2281          !!!next-input-character;          !!!next-input-character;
2282          redo A;          redo A;
2283        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2284            !!!cp (149);
2285          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2286          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2287          ## reconsume          ## reconsume
2288    
2289          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2290    
2291          redo A;          redo A;
2292        } else {        } else {
2293          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2294          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2295            $self->{state} = COMMENT_STATE;
2296          !!!next-input-character;          !!!next-input-character;
2297          redo A;          redo A;
2298        }        }
2299      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2300        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2301          $self->{state} = 'data';          !!!cp (151);
2302            $self->{state} = DATA_STATE;
2303          !!!next-input-character;          !!!next-input-character;
2304    
2305          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2306    
2307          redo A;          redo A;
2308        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2309          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2310            !!!parse-error (type => 'dash in comment',
2311                            line => $self->{line_prev},
2312                            column => $self->{column_prev});
2313          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2314          ## Stay in the state          ## Stay in the state
2315          !!!next-input-character;          !!!next-input-character;
2316          redo A;          redo A;
2317        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2318            !!!cp (153);
2319          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2320          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2321          ## reconsume          ## reconsume
2322    
2323          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2324    
2325          redo A;          redo A;
2326        } else {        } else {
2327          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2328          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2329          $self->{state} = 'comment';                          line => $self->{line_prev},
2330                            column => $self->{column_prev});
2331            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2332            $self->{state} = COMMENT_STATE;
2333          !!!next-input-character;          !!!next-input-character;
2334          redo A;          redo A;
2335        }        }
2336      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2337        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2338            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2339            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2340            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2341            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2342          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2343            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2344          !!!next-input-character;          !!!next-input-character;
2345          redo A;          redo A;
2346        } else {        } else {
2347            !!!cp (156);
2348          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2349          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2350          ## reconsume          ## reconsume
2351          redo A;          redo A;
2352        }        }
2353      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2354        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2355            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2356            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2357            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2358            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2359            !!!cp (157);
2360          ## Stay in the state          ## Stay in the state
2361          !!!next-input-character;          !!!next-input-character;
2362          redo A;          redo A;
2363        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
2364                 $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) { # >  
2365          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2366          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2367          !!!next-input-character;          !!!next-input-character;
2368    
2369          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2370    
2371          redo A;          redo A;
2372        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2373            !!!cp (159);
2374          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2375          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2376          ## reconsume          ## reconsume
2377    
2378          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2379    
2380          redo A;          redo A;
2381        } else {        } else {
2382          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (160);
2383                            name => chr ($self->{next_input_character}),          $self->{current_token}->{name} = chr $self->{next_char};
2384                            error => 1};          delete $self->{current_token}->{quirks};
2385  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2386          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2387          !!!next-input-character;          !!!next-input-character;
2388          redo A;          redo A;
2389        }        }
2390      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2391        if ($self->{next_input_character} == 0x0009 or # HT  ## ISSUE: Redundant "First," in the spec.
2392            $self->{next_input_character} == 0x000A or # LF        if ($self->{next_char} == 0x0009 or # HT
2393            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000A or # LF
2394            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000B or # VT
2395            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x000C or # FF
2396          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE            $self->{next_char} == 0x0020) { # SP
2397          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2398            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2399          !!!next-input-character;          !!!next-input-character;
2400          redo A;          redo A;
2401        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2402          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (162);
2403          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2404          !!!next-input-character;          !!!next-input-character;
2405    
2406          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2407    
2408          redo A;          redo A;
2409        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
2410                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (163);
2411          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed DOCTYPE');
2412          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{state} = DATA_STATE;
2413            ## reconsume
2414    
2415            $self->{current_token}->{quirks} = 1;
2416            !!!emit ($self->{current_token}); # DOCTYPE
2417    
2418            redo A;
2419          } else {
2420            !!!cp (164);
2421            $self->{current_token}->{name}
2422              .= chr ($self->{next_char}); # DOCTYPE
2423            ## Stay in the state
2424            !!!next-input-character;
2425            redo A;
2426          }
2427        } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2428          if ($self->{next_char} == 0x0009 or # HT
2429              $self->{next_char} == 0x000A or # LF
2430              $self->{next_char} == 0x000B or # VT
2431              $self->{next_char} == 0x000C or # FF
2432              $self->{next_char} == 0x0020) { # SP
2433            !!!cp (165);
2434          ## Stay in the state          ## Stay in the state
2435          !!!next-input-character;          !!!next-input-character;
2436          redo A;          redo A;
2437        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2438            !!!cp (166);
2439            $self->{state} = DATA_STATE;
2440            !!!next-input-character;
2441    
2442            !!!emit ($self->{current_token}); # DOCTYPE
2443    
2444            redo A;
2445          } elsif ($self->{next_char} == -1) {
2446            !!!cp (167);
2447          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2448          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
2449          ## reconsume          ## reconsume
2450    
2451          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
2452          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
2453    
2454          redo A;          redo A;
2455          } elsif ($self->{next_char} == 0x0050 or # P
2456                   $self->{next_char} == 0x0070) { # p
2457            $self->{state} = PUBLIC_STATE;
2458            $self->{state_keyword} = chr $self->{next_char};
2459            !!!next-input-character;
2460            redo A;
2461          } elsif ($self->{next_char} == 0x0053 or # S
2462                   $self->{next_char} == 0x0073) { # s
2463            $self->{state} = SYSTEM_STATE;
2464            $self->{state_keyword} = chr $self->{next_char};
2465            !!!next-input-character;
2466            redo A;
2467        } else {        } else {
2468          $self->{current_token}->{name}          !!!cp (180);
2469            .= chr ($self->{next_input_character}); # DOCTYPE          !!!parse-error (type => 'string after DOCTYPE name');
2470          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{current_token}->{quirks} = 1;
2471    
2472            $self->{state} = BOGUS_DOCTYPE_STATE;
2473            !!!next-input-character;
2474            redo A;
2475          }
2476        } elsif ($self->{state} == PUBLIC_STATE) {
2477          ## ASCII case-insensitive
2478          if ($self->{next_char} == [
2479                undef,
2480                0x0055, # U
2481                0x0042, # B
2482                0x004C, # L
2483                0x0049, # I
2484              ]->[length $self->{state_keyword}] or
2485              $self->{next_char} == [
2486                undef,
2487                0x0075, # u
2488                0x0062, # b
2489                0x006C, # l
2490                0x0069, # i
2491              ]->[length $self->{state_keyword}]) {
2492            !!!cp (175);
2493            ## Stay in the state.
2494            $self->{state_keyword} .= chr $self->{next_char};
2495            !!!next-input-character;
2496            redo A;
2497          } elsif ((length $self->{state_keyword}) == 5 and
2498                   ($self->{next_char} == 0x0043 or # C
2499                    $self->{next_char} == 0x0063)) { # c
2500            !!!cp (168);
2501            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2502            !!!next-input-character;
2503            redo A;
2504          } else {
2505            !!!cp (169);
2506            !!!parse-error (type => 'string after DOCTYPE name',
2507                            line => $self->{line_prev},
2508                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2509            $self->{current_token}->{quirks} = 1;
2510    
2511            $self->{state} = BOGUS_DOCTYPE_STATE;
2512            ## Reconsume.
2513            redo A;
2514          }
2515        } elsif ($self->{state} == SYSTEM_STATE) {
2516          ## ASCII case-insensitive
2517          if ($self->{next_char} == [
2518                undef,
2519                0x0059, # Y
2520                0x0053, # S
2521                0x0054, # T
2522                0x0045, # E
2523              ]->[length $self->{state_keyword}] or
2524              $self->{next_char} == [
2525                undef,
2526                0x0079, # y
2527                0x0073, # s
2528                0x0074, # t
2529                0x0065, # e
2530              ]->[length $self->{state_keyword}]) {
2531            !!!cp (170);
2532            ## Stay in the state.
2533            $self->{state_keyword} .= chr $self->{next_char};
2534            !!!next-input-character;
2535            redo A;
2536          } elsif ((length $self->{state_keyword}) == 5 and
2537                   ($self->{next_char} == 0x004D or # M
2538                    $self->{next_char} == 0x006D)) { # m
2539            !!!cp (171);
2540            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2541            !!!next-input-character;
2542            redo A;
2543          } else {
2544            !!!cp (172);
2545            !!!parse-error (type => 'string after DOCTYPE name',
2546                            line => $self->{line_prev},
2547                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2548            $self->{current_token}->{quirks} = 1;
2549    
2550            $self->{state} = BOGUS_DOCTYPE_STATE;
2551            ## Reconsume.
2552            redo A;
2553          }
2554        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2555          if ({
2556                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2557                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2558              }->{$self->{next_char}}) {
2559            !!!cp (181);
2560          ## Stay in the state          ## Stay in the state
2561          !!!next-input-character;          !!!next-input-character;
2562          redo A;          redo A;
2563          } elsif ($self->{next_char} eq 0x0022) { # "
2564            !!!cp (182);
2565            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2566            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2567            !!!next-input-character;
2568            redo A;
2569          } elsif ($self->{next_char} eq 0x0027) { # '
2570            !!!cp (183);
2571            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2572            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2573            !!!next-input-character;
2574            redo A;
2575          } elsif ($self->{next_char} eq 0x003E) { # >
2576            !!!cp (184);
2577            !!!parse-error (type => 'no PUBLIC literal');
2578    
2579            $self->{state} = DATA_STATE;
2580            !!!next-input-character;
2581    
2582            $self->{current_token}->{quirks} = 1;
2583            !!!emit ($self->{current_token}); # DOCTYPE
2584    
2585            redo A;
2586          } elsif ($self->{next_char} == -1) {
2587            !!!cp (185);
2588            !!!parse-error (type => 'unclosed DOCTYPE');
2589    
2590            $self->{state} = DATA_STATE;
2591            ## reconsume
2592    
2593            $self->{current_token}->{quirks} = 1;
2594            !!!emit ($self->{current_token}); # DOCTYPE
2595    
2596            redo A;
2597          } else {
2598            !!!cp (186);
2599            !!!parse-error (type => 'string after PUBLIC');
2600            $self->{current_token}->{quirks} = 1;
2601    
2602            $self->{state} = BOGUS_DOCTYPE_STATE;
2603            !!!next-input-character;
2604            redo A;
2605        }        }
2606      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2607        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0022) { # "
2608            $self->{next_input_character} == 0x000A or # LF          !!!cp (187);
2609            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2610            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
2611            $self->{next_input_character} == 0x0020) { # SP          redo A;
2612          } elsif ($self->{next_char} == 0x003E) { # >
2613            !!!cp (188);
2614            !!!parse-error (type => 'unclosed PUBLIC literal');
2615    
2616            $self->{state} = DATA_STATE;
2617            !!!next-input-character;
2618    
2619            $self->{current_token}->{quirks} = 1;
2620            !!!emit ($self->{current_token}); # DOCTYPE
2621    
2622            redo A;
2623          } elsif ($self->{next_char} == -1) {
2624            !!!cp (189);
2625            !!!parse-error (type => 'unclosed PUBLIC literal');
2626    
2627            $self->{state} = DATA_STATE;
2628            ## reconsume
2629    
2630            $self->{current_token}->{quirks} = 1;
2631            !!!emit ($self->{current_token}); # DOCTYPE
2632    
2633            redo A;
2634          } else {
2635            !!!cp (190);
2636            $self->{current_token}->{public_identifier} # DOCTYPE
2637                .= chr $self->{next_char};
2638            $self->{read_until}->($self->{current_token}->{public_identifier},
2639                                  q[">],
2640                                  length $self->{current_token}->{public_identifier});
2641    
2642          ## Stay in the state          ## Stay in the state
2643          !!!next-input-character;          !!!next-input-character;
2644          redo A;          redo A;
2645        } elsif ($self->{next_input_character} == 0x003E) { # >        }
2646          $self->{state} = 'data';      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2647          if ($self->{next_char} == 0x0027) { # '
2648            !!!cp (191);
2649            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2650            !!!next-input-character;
2651            redo A;
2652          } elsif ($self->{next_char} == 0x003E) { # >
2653            !!!cp (192);
2654            !!!parse-error (type => 'unclosed PUBLIC literal');
2655    
2656            $self->{state} = DATA_STATE;
2657            !!!next-input-character;
2658    
2659            $self->{current_token}->{quirks} = 1;
2660            !!!emit ($self->{current_token}); # DOCTYPE
2661    
2662            redo A;
2663          } elsif ($self->{next_char} == -1) {
2664            !!!cp (193);
2665            !!!parse-error (type => 'unclosed PUBLIC literal');
2666    
2667            $self->{state} = DATA_STATE;
2668            ## reconsume
2669    
2670            $self->{current_token}->{quirks} = 1;
2671            !!!emit ($self->{current_token}); # DOCTYPE
2672    
2673            redo A;
2674          } else {
2675            !!!cp (194);
2676            $self->{current_token}->{public_identifier} # DOCTYPE
2677                .= chr $self->{next_char};
2678            $self->{read_until}->($self->{current_token}->{public_identifier},
2679                                  q['>],
2680                                  length $self->{current_token}->{public_identifier});
2681    
2682            ## Stay in the state
2683            !!!next-input-character;
2684            redo A;
2685          }
2686        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2687          if ({
2688                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2689                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2690              }->{$self->{next_char}}) {
2691            !!!cp (195);
2692            ## Stay in the state
2693            !!!next-input-character;
2694            redo A;
2695          } elsif ($self->{next_char} == 0x0022) { # "
2696            !!!cp (196);
2697            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2698            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2699            !!!next-input-character;
2700            redo A;
2701          } elsif ($self->{next_char} == 0x0027) { # '
2702            !!!cp (197);
2703            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2704            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2705            !!!next-input-character;
2706            redo A;
2707          } elsif ($self->{next_char} == 0x003E) { # >
2708            !!!cp (198);
2709            $self->{state} = DATA_STATE;
2710          !!!next-input-character;          !!!next-input-character;
2711    
2712          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2713    
2714          redo A;          redo A;
2715        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2716            !!!cp (199);
2717          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2718          $self->{state} = 'data';  
2719            $self->{state} = DATA_STATE;
2720          ## reconsume          ## reconsume
2721    
2722            $self->{current_token}->{quirks} = 1;
2723          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2724    
2725          redo A;          redo A;
2726        } else {        } else {
2727          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (200);
2728          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after PUBLIC literal');
2729          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2730    
2731            $self->{state} = BOGUS_DOCTYPE_STATE;
2732          !!!next-input-character;          !!!next-input-character;
2733          redo A;          redo A;
2734        }        }
2735      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2736        if ($self->{next_input_character} == 0x003E) { # >        if ({
2737          $self->{state} = 'data';              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2738                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2739              }->{$self->{next_char}}) {
2740            !!!cp (201);
2741            ## Stay in the state
2742            !!!next-input-character;
2743            redo A;
2744          } elsif ($self->{next_char} == 0x0022) { # "
2745            !!!cp (202);
2746            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2747            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2748            !!!next-input-character;
2749            redo A;
2750          } elsif ($self->{next_char} == 0x0027) { # '
2751            !!!cp (203);
2752            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2753            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2754            !!!next-input-character;
2755            redo A;
2756          } elsif ($self->{next_char} == 0x003E) { # >
2757            !!!cp (204);
2758            !!!parse-error (type => 'no SYSTEM literal');
2759            $self->{state} = DATA_STATE;
2760          !!!next-input-character;          !!!next-input-character;
2761    
2762            $self->{current_token}->{quirks} = 1;
2763          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2764    
2765          redo A;          redo A;
2766        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2767            !!!cp (205);
2768          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2769          $self->{state} = 'data';  
2770            $self->{state} = DATA_STATE;
2771          ## reconsume          ## reconsume
2772    
2773            $self->{current_token}->{quirks} = 1;
2774          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2775    
2776          redo A;          redo A;
2777        } else {        } else {
2778            !!!cp (206);
2779            !!!parse-error (type => 'string after SYSTEM');
2780            $self->{current_token}->{quirks} = 1;
2781    
2782            $self->{state} = BOGUS_DOCTYPE_STATE;
2783            !!!next-input-character;
2784            redo A;
2785          }
2786        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2787          if ($self->{next_char} == 0x0022) { # "
2788            !!!cp (207);
2789            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2790            !!!next-input-character;
2791            redo A;
2792          } elsif ($self->{next_char} == 0x003E) { # >
2793            !!!cp (208);
2794            !!!parse-error (type => 'unclosed SYSTEM literal');
2795    
2796            $self->{state} = DATA_STATE;
2797            !!!next-input-character;
2798    
2799            $self->{current_token}->{quirks} = 1;
2800            !!!emit ($self->{current_token}); # DOCTYPE
2801    
2802            redo A;
2803          } elsif ($self->{next_char} == -1) {
2804            !!!cp (209);
2805            !!!parse-error (type => 'unclosed SYSTEM literal');
2806    
2807            $self->{state} = DATA_STATE;
2808            ## reconsume
2809    
2810            $self->{current_token}->{quirks} = 1;
2811            !!!emit ($self->{current_token}); # DOCTYPE
2812    
2813            redo A;
2814          } else {
2815            !!!cp (210);
2816            $self->{current_token}->{system_identifier} # DOCTYPE
2817                .= chr $self->{next_char};
2818            $self->{read_until}->($self->{current_token}->{system_identifier},
2819                                  q[">],
2820                                  length $self->{current_token}->{system_identifier});
2821    
2822          ## Stay in the state          ## Stay in the state
2823          !!!next-input-character;          !!!next-input-character;
2824          redo A;          redo A;
2825        }        }
2826      } else {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2827        die "$0: $self->{state}: Unknown state";        if ($self->{next_char} == 0x0027) { # '
2828      }          !!!cp (211);
2829    } # A            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2830            !!!next-input-character;
2831            redo A;
2832          } elsif ($self->{next_char} == 0x003E) { # >
2833            !!!cp (212);
2834            !!!parse-error (type => 'unclosed SYSTEM literal');
2835    
2836    die "$0: _get_next_token: unexpected case";          $self->{state} = DATA_STATE;
2837  } # _get_next_token          !!!next-input-character;
2838    
2839  sub _tokenize_attempt_to_consume_an_entity ($) {          $self->{current_token}->{quirks} = 1;
2840    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');  
         }  
2841    
2842          ## TODO: check the definition for |a valid Unicode character|.          redo A;
2843          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>        } elsif ($self->{next_char} == -1) {
2844          if ($num > 1114111 or $num == 0) {          !!!cp (213);
2845            $num = 0xFFFD; # REPLACEMENT CHARACTER          !!!parse-error (type => 'unclosed SYSTEM literal');
2846            ## ISSUE: Why this is not an error?  
2847          } elsif (0x80 <= $num and $num <= 0x9F) {          $self->{state} = DATA_STATE;
2848            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          ## reconsume
2849            $num = $c1_entity_char->{$num};  
2850          }          $self->{current_token}->{quirks} = 1;
2851            !!!emit ($self->{current_token}); # DOCTYPE
2852          return {type => 'character', data => chr $num};  
2853        } # X          redo A;
2854      } elsif (0x0030 <= $self->{next_input_character} and        } else {
2855               $self->{next_input_character} <= 0x0039) { # 0..9          !!!cp (214);
2856        my $code = $self->{next_input_character} - 0x0030;          $self->{current_token}->{system_identifier} # DOCTYPE
2857        !!!next-input-character;              .= chr $self->{next_char};
2858            $self->{read_until}->($self->{current_token}->{system_identifier},
2859                                  q['>],
2860                                  length $self->{current_token}->{system_identifier});
2861    
2862            ## Stay in the state
2863            !!!next-input-character;
2864            redo A;
2865          }
2866        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2867          if ({
2868                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2869                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2870              }->{$self->{next_char}}) {
2871            !!!cp (215);
2872            ## Stay in the state
2873            !!!next-input-character;
2874            redo A;
2875          } elsif ($self->{next_char} == 0x003E) { # >
2876            !!!cp (216);
2877            $self->{state} = DATA_STATE;
2878            !!!next-input-character;
2879    
2880            !!!emit ($self->{current_token}); # DOCTYPE
2881    
2882            redo A;
2883          } elsif ($self->{next_char} == -1) {
2884            !!!cp (217);
2885            !!!parse-error (type => 'unclosed DOCTYPE');
2886            $self->{state} = DATA_STATE;
2887            ## reconsume
2888    
2889            $self->{current_token}->{quirks} = 1;
2890            !!!emit ($self->{current_token}); # DOCTYPE
2891    
2892            redo A;
2893          } else {
2894            !!!cp (218);
2895            !!!parse-error (type => 'string after SYSTEM literal');
2896            #$self->{current_token}->{quirks} = 1;
2897    
2898            $self->{state} = BOGUS_DOCTYPE_STATE;
2899            !!!next-input-character;
2900            redo A;
2901          }
2902        } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2903          if ($self->{next_char} == 0x003E) { # >
2904            !!!cp (219);
2905            $self->{state} = DATA_STATE;
2906            !!!next-input-character;
2907    
2908            !!!emit ($self->{current_token}); # DOCTYPE
2909    
2910            redo A;
2911          } elsif ($self->{next_char} == -1) {
2912            !!!cp (220);
2913            !!!parse-error (type => 'unclosed DOCTYPE');
2914            $self->{state} = DATA_STATE;
2915            ## reconsume
2916    
2917            !!!emit ($self->{current_token}); # DOCTYPE
2918    
2919            redo A;
2920          } else {
2921            !!!cp (221);
2922            my $s = '';
2923            $self->{read_until}->($s, q[>], 0);
2924    
2925            ## Stay in the state
2926            !!!next-input-character;
2927            redo A;
2928          }
2929        } elsif ($self->{state} == CDATA_SECTION_STATE) {
2930          ## NOTE: "CDATA section state" in the state is jointly implemented
2931          ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2932          ## and |CDATA_SECTION_MSE2_STATE|.
2933                
2934        while (0x0030 <= $self->{next_input_character} and        if ($self->{next_char} == 0x005D) { # ]
2935                  $self->{next_input_character} <= 0x0039) { # 0..9          !!!cp (221.1);
2936          $code *= 10;          $self->{state} = CDATA_SECTION_MSE1_STATE;
         $code += $self->{next_input_character} - 0x0030;  
           
2937          !!!next-input-character;          !!!next-input-character;
2938            redo A;
2939          } elsif ($self->{next_char} == -1) {
2940            $self->{state} = DATA_STATE;
2941            !!!next-input-character;
2942            if (length $self->{current_token}->{data}) { # character
2943              !!!cp (221.2);
2944              !!!emit ($self->{current_token}); # character
2945            } else {
2946              !!!cp (221.3);
2947              ## No token to emit. $self->{current_token} is discarded.
2948            }        
2949            redo A;
2950          } else {
2951            !!!cp (221.4);
2952            $self->{current_token}->{data} .= chr $self->{next_char};
2953            $self->{read_until}->($self->{current_token}->{data},
2954                                  q<]>,
2955                                  length $self->{current_token}->{data});
2956    
2957            ## Stay in the state.
2958            !!!next-input-character;
2959            redo A;
2960        }        }
2961    
2962        if ($self->{next_input_character} == 0x003B) { # ;        ## ISSUE: "text tokens" in spec.
2963        } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2964          if ($self->{next_char} == 0x005D) { # ]
2965            !!!cp (221.5);
2966            $self->{state} = CDATA_SECTION_MSE2_STATE;
2967          !!!next-input-character;          !!!next-input-character;
2968            redo A;
2969        } else {        } else {
2970            !!!cp (221.6);
2971            $self->{current_token}->{data} .= ']';
2972            $self->{state} = CDATA_SECTION_STATE;
2973            ## Reconsume.
2974            redo A;
2975          }
2976        } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2977          if ($self->{next_char} == 0x003E) { # >
2978            $self->{state} = DATA_STATE;
2979            !!!next-input-character;
2980            if (length $self->{current_token}->{data}) { # character
2981              !!!cp (221.7);
2982              !!!emit ($self->{current_token}); # character
2983            } else {
2984              !!!cp (221.8);
2985              ## No token to emit. $self->{current_token} is discarded.
2986            }
2987            redo A;
2988          } elsif ($self->{next_char} == 0x005D) { # ]
2989            !!!cp (221.9); # character
2990            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
2991            ## Stay in the state.
2992            !!!next-input-character;
2993            redo A;
2994          } else {
2995            !!!cp (221.11);
2996            $self->{current_token}->{data} .= ']]'; # character
2997            $self->{state} = CDATA_SECTION_STATE;
2998            ## Reconsume.
2999            redo A;
3000          }
3001        } elsif ($self->{state} == ENTITY_STATE) {
3002          if ({
3003            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3004            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3005            $self->{entity_additional} => 1,
3006          }->{$self->{next_char}}) {
3007            !!!cp (1001);
3008            ## Don't consume
3009            ## No error
3010            ## Return nothing.
3011            #
3012          } elsif ($self->{next_char} == 0x0023) { # #
3013            !!!cp (999);
3014            $self->{state} = ENTITY_HASH_STATE;
3015            $self->{state_keyword} = '#';
3016            !!!next-input-character;
3017            redo A;
3018          } elsif ((0x0041 <= $self->{next_char} and
3019                    $self->{next_char} <= 0x005A) or # A..Z
3020                   (0x0061 <= $self->{next_char} and
3021                    $self->{next_char} <= 0x007A)) { # a..z
3022            !!!cp (998);
3023            require Whatpm::_NamedEntityList;
3024            $self->{state} = ENTITY_NAME_STATE;
3025            $self->{state_keyword} = chr $self->{next_char};
3026            $self->{entity__value} = $self->{state_keyword};
3027            $self->{entity__match} = 0;
3028            !!!next-input-character;
3029            redo A;
3030          } else {
3031            !!!cp (1027);
3032            !!!parse-error (type => 'bare ero');
3033            ## Return nothing.
3034            #
3035          }
3036    
3037          ## NOTE: No character is consumed by the "consume a character
3038          ## reference" algorithm.  In other word, there is an "&" character
3039          ## that does not introduce a character reference, which would be
3040          ## appended to the parent element or the attribute value in later
3041          ## process of the tokenizer.
3042    
3043          if ($self->{prev_state} == DATA_STATE) {
3044            !!!cp (997);
3045            $self->{state} = $self->{prev_state};
3046            ## Reconsume.
3047            !!!emit ({type => CHARACTER_TOKEN, data => '&',
3048                      line => $self->{line_prev},
3049                      column => $self->{column_prev},
3050                     });
3051            redo A;
3052          } else {
3053            !!!cp (996);
3054            $self->{current_attribute}->{value} .= '&';
3055            $self->{state} = $self->{prev_state};
3056            ## Reconsume.
3057            redo A;
3058          }
3059        } elsif ($self->{state} == ENTITY_HASH_STATE) {
3060          if ($self->{next_char} == 0x0078 or # x
3061              $self->{next_char} == 0x0058) { # X
3062            !!!cp (995);
3063            $self->{state} = HEXREF_X_STATE;
3064            $self->{state_keyword} .= chr $self->{next_char};
3065            !!!next-input-character;
3066            redo A;
3067          } elsif (0x0030 <= $self->{next_char} and
3068                   $self->{next_char} <= 0x0039) { # 0..9
3069            !!!cp (994);
3070            $self->{state} = NCR_NUM_STATE;
3071            $self->{state_keyword} = $self->{next_char} - 0x0030;
3072            !!!next-input-character;
3073            redo A;
3074          } else {
3075            !!!parse-error (type => 'bare nero',
3076                            line => $self->{line_prev},
3077                            column => $self->{column_prev} - 1);
3078    
3079            ## NOTE: According to the spec algorithm, nothing is returned,
3080            ## and then "&#" is appended to the parent element or the attribute
3081            ## value in the later processing.
3082    
3083            if ($self->{prev_state} == DATA_STATE) {
3084              !!!cp (1019);
3085              $self->{state} = $self->{prev_state};
3086              ## Reconsume.
3087              !!!emit ({type => CHARACTER_TOKEN,
3088                        data => '&#',
3089                        line => $self->{line_prev},
3090                        column => $self->{column_prev} - 1,
3091                       });
3092              redo A;
3093            } else {
3094              !!!cp (993);
3095              $self->{current_attribute}->{value} .= '&#';
3096              $self->{state} = $self->{prev_state};
3097              ## Reconsume.
3098              redo A;
3099            }
3100          }
3101        } elsif ($self->{state} == NCR_NUM_STATE) {
3102          if (0x0030 <= $self->{next_char} and
3103              $self->{next_char} <= 0x0039) { # 0..9
3104            !!!cp (1012);
3105            $self->{state_keyword} *= 10;
3106            $self->{state_keyword} += $self->{next_char} - 0x0030;
3107            
3108            ## Stay in the state.
3109            !!!next-input-character;
3110            redo A;
3111          } elsif ($self->{next_char} == 0x003B) { # ;
3112            !!!cp (1013);
3113            !!!next-input-character;
3114            #
3115          } else {
3116            !!!cp (1014);
3117          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
3118            ## Reconsume.
3119            #
3120        }        }
3121    
3122        ## TODO: check the definition for |a valid Unicode character|.        my $code = $self->{state_keyword};
3123        if ($code > 1114111 or $code == 0) {        my $l = $self->{line_prev};
3124          $code = 0xFFFD; # REPLACEMENT CHARACTER        my $c = $self->{column_prev};
3125          ## ISSUE: Why this is not an error?        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3126            !!!cp (1015);
3127            !!!parse-error (type => 'invalid character reference',
3128                            text => (sprintf 'U+%04X', $code),
3129                            line => $l, column => $c);
3130            $code = 0xFFFD;
3131          } elsif ($code > 0x10FFFF) {
3132            !!!cp (1016);
3133            !!!parse-error (type => 'invalid character reference',
3134                            text => (sprintf 'U-%08X', $code),
3135                            line => $l, column => $c);
3136            $code = 0xFFFD;
3137          } elsif ($code == 0x000D) {
3138            !!!cp (1017);
3139            !!!parse-error (type => 'CR character reference',
3140                            line => $l, column => $c);
3141            $code = 0x000A;
3142        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3143          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!cp (1018);
3144            !!!parse-error (type => 'C1 character reference',
3145                            text => (sprintf 'U+%04X', $code),
3146                            line => $l, column => $c);
3147          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3148        }        }
3149          
3150        return {type => 'character', data => chr $code};        if ($self->{prev_state} == DATA_STATE) {
3151      } else {          !!!cp (992);
3152        !!!parse-error (type => 'bare nero');          $self->{state} = $self->{prev_state};
3153        !!!back-next-input-character ($self->{next_input_character});          ## Reconsume.
3154        $self->{next_input_character} = 0x0023; # #          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3155        return undef;                    line => $l, column => $c,
3156      }                   });
3157    } elsif ((0x0041 <= $self->{next_input_character} and          redo A;
3158              $self->{next_input_character} <= 0x005A) or        } else {
3159             (0x0061 <= $self->{next_input_character} and          !!!cp (991);
3160              $self->{next_input_character} <= 0x007A)) {          $self->{current_attribute}->{value} .= chr $code;
3161      my $entity_name = chr $self->{next_input_character};          $self->{current_attribute}->{has_reference} = 1;
3162      !!!next-input-character;          $self->{state} = $self->{prev_state};
3163            ## Reconsume.
3164      my $value = $entity_name;          redo A;
3165      my $match;        }
3166        } elsif ($self->{state} == HEXREF_X_STATE) {
3167      while (length $entity_name < 10 and        if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3168             ## NOTE: Some number greater than the maximum length of entity name            (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3169             ((0x0041 <= $self->{next_input_character} and            (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3170               $self->{next_input_character} <= 0x005A) or          # 0..9, A..F, a..f
3171              (0x0061 <= $self->{next_input_character} and          !!!cp (990);
3172               $self->{next_input_character} <= 0x007A) or          $self->{state} = HEXREF_HEX_STATE;
3173              (0x0030 <= $self->{next_input_character} and          $self->{state_keyword} = 0;
3174               $self->{next_input_character} <= 0x0039))) {          ## Reconsume.
3175        $entity_name .= chr $self->{next_input_character};          redo A;
       if (defined $entity_char->{$entity_name}) {  
         $value = $entity_char->{$entity_name};  
         $match = 1;  
3176        } else {        } else {
3177          $value .= chr $self->{next_input_character};          !!!parse-error (type => 'bare hcro',
3178                            line => $self->{line_prev},
3179                            column => $self->{column_prev} - 2);
3180    
3181            ## NOTE: According to the spec algorithm, nothing is returned,
3182            ## and then "&#" followed by "X" or "x" is appended to the parent
3183            ## element or the attribute value in the later processing.
3184    
3185            if ($self->{prev_state} == DATA_STATE) {
3186              !!!cp (1005);
3187              $self->{state} = $self->{prev_state};
3188              ## Reconsume.
3189              !!!emit ({type => CHARACTER_TOKEN,
3190                        data => '&' . $self->{state_keyword},
3191                        line => $self->{line_prev},
3192                        column => $self->{column_prev} - length $self->{state_keyword},
3193                       });
3194              redo A;
3195            } else {
3196              !!!cp (989);
3197              $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3198              $self->{state} = $self->{prev_state};
3199              ## Reconsume.
3200              redo A;
3201            }
3202        }        }
3203        !!!next-input-character;      } elsif ($self->{state} == HEXREF_HEX_STATE) {
3204      }        if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3205                # 0..9
3206      if ($match) {          !!!cp (1002);
3207        if ($self->{next_input_character} == 0x003B) { # ;          $self->{state_keyword} *= 0x10;
3208            $self->{state_keyword} += $self->{next_char} - 0x0030;
3209            ## Stay in the state.
3210            !!!next-input-character;
3211            redo A;
3212          } elsif (0x0061 <= $self->{next_char} and
3213                   $self->{next_char} <= 0x0066) { # a..f
3214            !!!cp (1003);
3215            $self->{state_keyword} *= 0x10;
3216            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3217            ## Stay in the state.
3218            !!!next-input-character;
3219            redo A;
3220          } elsif (0x0041 <= $self->{next_char} and
3221                   $self->{next_char} <= 0x0046) { # A..F
3222            !!!cp (1004);
3223            $self->{state_keyword} *= 0x10;
3224            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3225            ## Stay in the state.
3226          !!!next-input-character;          !!!next-input-character;
3227            redo A;
3228          } elsif ($self->{next_char} == 0x003B) { # ;
3229            !!!cp (1006);
3230            !!!next-input-character;
3231            #
3232          } else {
3233            !!!cp (1007);
3234            !!!parse-error (type => 'no refc',
3235                            line => $self->{line},
3236                            column => $self->{column});
3237            ## Reconsume.
3238            #
3239          }
3240    
3241          my $code = $self->{state_keyword};
3242          my $l = $self->{line_prev};
3243          my $c = $self->{column_prev};
3244          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3245            !!!cp (1008);
3246            !!!parse-error (type => 'invalid character reference',
3247                            text => (sprintf 'U+%04X', $code),
3248                            line => $l, column => $c);
3249            $code = 0xFFFD;
3250          } elsif ($code > 0x10FFFF) {
3251            !!!cp (1009);
3252            !!!parse-error (type => 'invalid character reference',
3253                            text => (sprintf 'U-%08X', $code),
3254                            line => $l, column => $c);
3255            $code = 0xFFFD;
3256          } elsif ($code == 0x000D) {
3257            !!!cp (1010);
3258            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3259            $code = 0x000A;
3260          } elsif (0x80 <= $code and $code <= 0x9F) {
3261            !!!cp (1011);
3262            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3263            $code = $c1_entity_char->{$code};
3264          }
3265    
3266          if ($self->{prev_state} == DATA_STATE) {
3267            !!!cp (988);
3268            $self->{state} = $self->{prev_state};
3269            ## Reconsume.
3270            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3271                      line => $l, column => $c,
3272                     });
3273            redo A;
3274        } else {        } else {
3275          !!!parse-error (type => 'refc');          !!!cp (987);
3276            $self->{current_attribute}->{value} .= chr $code;
3277            $self->{current_attribute}->{has_reference} = 1;
3278            $self->{state} = $self->{prev_state};
3279            ## Reconsume.
3280            redo A;
3281          }
3282        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3283          if (length $self->{state_keyword} < 30 and
3284              ## NOTE: Some number greater than the maximum length of entity name
3285              ((0x0041 <= $self->{next_char} and # a
3286                $self->{next_char} <= 0x005A) or # x
3287               (0x0061 <= $self->{next_char} and # a
3288                $self->{next_char} <= 0x007A) or # z
3289               (0x0030 <= $self->{next_char} and # 0
3290                $self->{next_char} <= 0x0039) or # 9
3291               $self->{next_char} == 0x003B)) { # ;
3292            our $EntityChar;
3293            $self->{state_keyword} .= chr $self->{next_char};
3294            if (defined $EntityChar->{$self->{state_keyword}}) {
3295              if ($self->{next_char} == 0x003B) { # ;
3296                !!!cp (1020);
3297                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3298                $self->{entity__match} = 1;
3299                !!!next-input-character;
3300                #
3301              } else {
3302                !!!cp (1021);
3303                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3304                $self->{entity__match} = -1;
3305                ## Stay in the state.
3306                !!!next-input-character;
3307                redo A;
3308              }
3309            } else {
3310              !!!cp (1022);
3311              $self->{entity__value} .= chr $self->{next_char};
3312              $self->{entity__match} *= 2;
3313              ## Stay in the state.
3314              !!!next-input-character;
3315              redo A;
3316            }
3317        }        }
3318    
3319        return {type => 'character', data => $value};        my $data;
3320          my $has_ref;
3321          if ($self->{entity__match} > 0) {
3322            !!!cp (1023);
3323            $data = $self->{entity__value};
3324            $has_ref = 1;
3325            #
3326          } elsif ($self->{entity__match} < 0) {
3327            !!!parse-error (type => 'no refc');
3328            if ($self->{prev_state} != DATA_STATE and # in attribute
3329                $self->{entity__match} < -1) {
3330              !!!cp (1024);
3331              $data = '&' . $self->{state_keyword};
3332              #
3333            } else {
3334              !!!cp (1025);
3335              $data = $self->{entity__value};
3336              $has_ref = 1;
3337              #
3338            }
3339          } else {
3340            !!!cp (1026);
3341            !!!parse-error (type => 'bare ero',
3342                            line => $self->{line_prev},
3343                            column => $self->{column_prev} - length $self->{state_keyword});
3344            $data = '&' . $self->{state_keyword};
3345            #
3346          }
3347      
3348          ## NOTE: In these cases, when a character reference is found,
3349          ## it is consumed and a character token is returned, or, otherwise,
3350          ## nothing is consumed and returned, according to the spec algorithm.
3351          ## In this implementation, anything that has been examined by the
3352          ## tokenizer is appended to the parent element or the attribute value
3353          ## as string, either literal string when no character reference or
3354          ## entity-replaced string otherwise, in this stage, since any characters
3355          ## that would not be consumed are appended in the data state or in an
3356          ## appropriate attribute value state anyway.
3357    
3358          if ($self->{prev_state} == DATA_STATE) {
3359            !!!cp (986);
3360            $self->{state} = $self->{prev_state};
3361            ## Reconsume.
3362            !!!emit ({type => CHARACTER_TOKEN,
3363                      data => $data,
3364                      line => $self->{line_prev},
3365                      column => $self->{column_prev} + 1 - length $self->{state_keyword},
3366                     });
3367            redo A;
3368          } else {
3369            !!!cp (985);
3370            $self->{current_attribute}->{value} .= $data;
3371            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3372            $self->{state} = $self->{prev_state};
3373            ## Reconsume.
3374            redo A;
3375          }
3376      } else {      } else {
3377        !!!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;  
3378      }      }
3379    } else {    } # A  
3380      ## no characters are consumed  
3381      !!!parse-error (type => 'bare ero');    die "$0: _get_next_token: unexpected case";
3382      return undef;  } # _get_next_token
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3383    
3384  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3385    my $self = shift;    my $self = shift;
# Line 1667  sub _initialize_tree_constructor ($) { Line 3387  sub _initialize_tree_constructor ($) {
3387    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
3388    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3389    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3390    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
3391      $self->{document}->set_user_data (manakai_source_line => 1);
3392      $self->{document}->set_user_data (manakai_source_column => 1);
3393  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3394    
3395  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1694  sub _construct_tree ($) { Line 3416  sub _construct_tree ($) {
3416        
3417    !!!next-token;    !!!next-token;
3418    
   $self->{insertion_mode} = 'before head';  
3419    undef $self->{form_element};    undef $self->{form_element};
3420    undef $self->{head_element};    undef $self->{head_element};
3421    $self->{open_elements} = [];    $self->{open_elements} = [];
3422    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3423    
3424      ## NOTE: The "initial" insertion mode.
3425    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3426    
3427      ## NOTE: The "before html" insertion mode.
3428    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3429      $self->{insertion_mode} = BEFORE_HEAD_IM;
3430    
3431      ## NOTE: The "before head" insertion mode and so on.
3432    $self->_tree_construction_main;    $self->_tree_construction_main;
3433  } # _construct_tree  } # _construct_tree
3434    
3435  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3436    my $self = shift;    my $self = shift;
3437    B: {  
3438        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
3439          if ($token->{error}) {  
3440            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
3441            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
3442          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3443          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
3444            ($token->{name});        ## language.
3445          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
3446          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
3447          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3448          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
3449          return;            defined $token->{system_identifier}) {
3450        } elsif ({          !!!cp ('t1');
3451                  comment => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3452                  'start tag' => 1,        } elsif ($doctype_name ne 'HTML') {
3453                  'end tag' => 1,          !!!cp ('t2');
3454                  'end-of-file' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3455                 }->{$token->{type}}) {        } elsif (defined $token->{public_identifier}) {
3456          ## ISSUE: Spec currently left this case undefined.          if ($token->{public_identifier} eq 'XSLT-compat') {
3457          !!!parse-error (type => 'missing DOCTYPE');            !!!cp ('t1.2');
3458          #$phase = 'root element';            !!!parse-error (type => 'XSLT-compat', token => $token,
3459          ## reprocess                            level => $self->{level}->{should});
3460          #redo B;          } else {
3461          return;            !!!parse-error (type => 'not HTML5', token => $token);
3462        } elsif ($token->{type} eq 'character') {          }
3463          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } else {
3464            $self->{document}->manakai_append_text ($1);          !!!cp ('t3');
3465            ## ISSUE: DOM3 Core does not allow Document > Text          #
3466            unless (length $token->{data}) {        }
3467              ## Stay in the phase        
3468              !!!next-token;        my $doctype = $self->{document}->create_document_type_definition
3469              redo B;          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3470          ## NOTE: Default value for both |public_id| and |system_id| attributes
3471          ## are empty strings, so that we don't set any value in missing cases.
3472          $doctype->public_id ($token->{public_identifier})
3473              if defined $token->{public_identifier};
3474          $doctype->system_id ($token->{system_identifier})
3475              if defined $token->{system_identifier};
3476          ## NOTE: Other DocumentType attributes are null or empty lists.
3477          ## ISSUE: internalSubset = null??
3478          $self->{document}->append_child ($doctype);
3479          
3480          if ($token->{quirks} or $doctype_name ne 'HTML') {
3481            !!!cp ('t4');
3482            $self->{document}->manakai_compat_mode ('quirks');
3483          } elsif (defined $token->{public_identifier}) {
3484            my $pubid = $token->{public_identifier};
3485            $pubid =~ tr/a-z/A-z/;
3486            my $prefix = [
3487              "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3488              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3489              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3490              "-//IETF//DTD HTML 2.0 LEVEL 1//",
3491              "-//IETF//DTD HTML 2.0 LEVEL 2//",
3492              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3493              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3494              "-//IETF//DTD HTML 2.0 STRICT//",
3495              "-//IETF//DTD HTML 2.0//",
3496              "-//IETF//DTD HTML 2.1E//",
3497              "-//IETF//DTD HTML 3.0//",
3498              "-//IETF//DTD HTML 3.2 FINAL//",
3499              "-//IETF//DTD HTML 3.2//",
3500              "-//IETF//DTD HTML 3//",
3501              "-//IETF//DTD HTML LEVEL 0//",
3502              "-//IETF//DTD HTML LEVEL 1//",
3503              "-//IETF//DTD HTML LEVEL 2//",
3504              "-//IETF//DTD HTML LEVEL 3//",
3505              "-//IETF//DTD HTML STRICT LEVEL 0//",
3506              "-//IETF//DTD HTML STRICT LEVEL 1//",
3507              "-//IETF//DTD HTML STRICT LEVEL 2//",
3508              "-//IETF//DTD HTML STRICT LEVEL 3//",
3509              "-//IETF//DTD HTML STRICT//",
3510              "-//IETF//DTD HTML//",
3511              "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3512              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3513              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3514              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3515              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3516              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3517              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3518              "-//NETSCAPE COMM. CORP.//DTD HTML//",
3519              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3520              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3521              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3522              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3523              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3524              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3525              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3526              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3527              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3528              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3529              "-//W3C//DTD HTML 3 1995-03-24//",
3530              "-//W3C//DTD HTML 3.2 DRAFT//",
3531              "-//W3C//DTD HTML 3.2 FINAL//",
3532              "-//W3C//DTD HTML 3.2//",
3533              "-//W3C//DTD HTML 3.2S DRAFT//",
3534              "-//W3C//DTD HTML 4.0 FRAMESET//",
3535              "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3536              "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3537              "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3538              "-//W3C//DTD W3 HTML//",
3539              "-//W3O//DTD W3 HTML 3.0//",
3540              "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3541              "-//WEBTECHS//DTD MOZILLA HTML//",
3542            ]; # $prefix
3543            my $match;
3544            for (@$prefix) {
3545              if (substr ($prefix, 0, length $_) eq $_) {
3546                $match = 1;
3547                last;
3548              }
3549            }
3550            if ($match or
3551                $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3552                $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3553                $pubid eq "HTML") {
3554              !!!cp ('t5');
3555              $self->{document}->manakai_compat_mode ('quirks');
3556            } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3557                     $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3558              if (defined $token->{system_identifier}) {
3559                !!!cp ('t6');
3560                $self->{document}->manakai_compat_mode ('quirks');
3561              } else {
3562                !!!cp ('t7');
3563                $self->{document}->manakai_compat_mode ('limited quirks');
3564            }            }
3565            } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3566                     $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3567              !!!cp ('t8');
3568              $self->{document}->manakai_compat_mode ('limited quirks');
3569            } else {
3570              !!!cp ('t9');
3571            }
3572          } else {
3573            !!!cp ('t10');
3574          }
3575          if (defined $token->{system_identifier}) {
3576            my $sysid = $token->{system_identifier};
3577            $sysid =~ tr/A-Z/a-z/;
3578            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3579              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3580              ## marked as quirks.
3581              $self->{document}->manakai_compat_mode ('quirks');
3582              !!!cp ('t11');
3583            } else {
3584              !!!cp ('t12');
3585          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
3586        } else {        } else {
3587          die "$0: $token->{type}: Unknown token";          !!!cp ('t13');
3588        }        }
3589      } # B        
3590          ## Go to the "before html" insertion mode.
3591          !!!next-token;
3592          return;
3593        } elsif ({
3594                  START_TAG_TOKEN, 1,
3595                  END_TAG_TOKEN, 1,
3596                  END_OF_FILE_TOKEN, 1,
3597                 }->{$token->{type}}) {
3598          !!!cp ('t14');
3599          !!!parse-error (type => 'no DOCTYPE', token => $token);
3600          $self->{document}->manakai_compat_mode ('quirks');
3601          ## Go to the "before html" insertion mode.
3602          ## reprocess
3603          !!!ack-later;
3604          return;
3605        } elsif ($token->{type} == CHARACTER_TOKEN) {
3606          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3607            ## Ignore the token
3608    
3609            unless (length $token->{data}) {
3610              !!!cp ('t15');
3611              ## Stay in the insertion mode.
3612              !!!next-token;
3613              redo INITIAL;
3614            } else {
3615              !!!cp ('t16');
3616            }
3617          } else {
3618            !!!cp ('t17');
3619          }
3620    
3621          !!!parse-error (type => 'no DOCTYPE', token => $token);
3622          $self->{document}->manakai_compat_mode ('quirks');
3623          ## Go to the "before html" insertion mode.
3624          ## reprocess
3625          return;
3626        } elsif ($token->{type} == COMMENT_TOKEN) {
3627          !!!cp ('t18');
3628          my $comment = $self->{document}->create_comment ($token->{data});
3629          $self->{document}->append_child ($comment);
3630          
3631          ## Stay in the insertion mode.
3632          !!!next-token;
3633          redo INITIAL;
3634        } else {
3635          die "$0: $token->{type}: Unknown token type";
3636        }
3637      } # INITIAL
3638    
3639      die "$0: _tree_construction_initial: This should be never reached";
3640  } # _tree_construction_initial  } # _tree_construction_initial
3641    
3642  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3643    my $self = shift;    my $self = shift;
3644    
3645      ## NOTE: "before html" insertion mode.
3646        
3647    B: {    B: {
3648        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3649          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3650            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3651          ## Ignore the token          ## Ignore the token
3652          ## Stay in the phase          ## Stay in the insertion mode.
3653          !!!next-token;          !!!next-token;
3654          redo B;          redo B;
3655        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3656            !!!cp ('t20');
3657          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3658          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3659          ## Stay in the phase          ## Stay in the insertion mode.
3660          !!!next-token;          !!!next-token;
3661          redo B;          redo B;
3662        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3663          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3664            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
3665            ## ISSUE: DOM3 Core does not allow Document > Text  
3666            unless (length $token->{data}) {            unless (length $token->{data}) {
3667              ## Stay in the phase              !!!cp ('t21');
3668                ## Stay in the insertion mode.
3669              !!!next-token;              !!!next-token;
3670              redo B;              redo B;
3671              } else {
3672                !!!cp ('t22');
3673            }            }
3674            } else {
3675              !!!cp ('t23');
3676          }          }
3677    
3678            $self->{application_cache_selection}->(undef);
3679    
3680          #          #
3681          } elsif ($token->{type} == START_TAG_TOKEN) {
3682            if ($token->{tag_name} eq 'html') {
3683              my $root_element;
3684              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3685              $self->{document}->append_child ($root_element);
3686              push @{$self->{open_elements}},
3687                  [$root_element, $el_category->{html}];
3688    
3689              if ($token->{attributes}->{manifest}) {
3690                !!!cp ('t24');
3691                $self->{application_cache_selection}
3692                    ->($token->{attributes}->{manifest}->{value});
3693                ## ISSUE: Spec is unclear on relative references.
3694                ## According to Hixie (#whatwg 2008-03-19), it should be
3695                ## resolved against the base URI of the document in HTML
3696                ## or xml:base of the element in XHTML.
3697              } else {
3698                !!!cp ('t25');
3699                $self->{application_cache_selection}->(undef);
3700              }
3701    
3702              !!!nack ('t25c');
3703    
3704              !!!next-token;
3705              return; ## Go to the "before head" insertion mode.
3706            } else {
3707              !!!cp ('t25.1');
3708              #
3709            }
3710        } elsif ({        } elsif ({
3711                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3712                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3713                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3714          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3715          #          #
3716        } else {        } else {
3717          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3718        }        }
3719        my $root_element; !!!create-element ($root_element, 'html');  
3720        $self->{document}->append_child ($root_element);      my $root_element;
3721        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3722        #$phase = 'main';      $self->{document}->append_child ($root_element);
3723        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3724        #redo B;  
3725        return;      $self->{application_cache_selection}->(undef);
3726    
3727        ## NOTE: Reprocess the token.
3728        !!!ack-later;
3729        return; ## Go to the "before head" insertion mode.
3730    
3731        ## ISSUE: There is an issue in the spec
3732    } # B    } # B
3733    
3734      die "$0: _tree_construction_root_element: This should never be reached";
3735  } # _tree_construction_root_element  } # _tree_construction_root_element
3736    
3737  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1813  sub _reset_insertion_mode ($) { Line 3746  sub _reset_insertion_mode ($) {
3746            
3747      ## Step 3      ## Step 3
3748      S3: {      S3: {
3749        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3750        if (defined $self->{inner_html_node}) {          $last = 1;
3751          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
3752              $self->{inner_html_node}->[1] eq 'th') {            !!!cp ('t28');
3753              $node = $self->{inner_html_node};
3754            } else {
3755              die "_reset_insertion_mode: t27";
3756            }
3757          }
3758          
3759          ## Step 4..14
3760          my $new_mode;
3761          if ($node->[1] & FOREIGN_EL) {
3762            !!!cp ('t28.1');
3763            ## NOTE: Strictly spaking, the line below only applies to MathML and
3764            ## SVG elements.  Currently the HTML syntax supports only MathML and
3765            ## SVG elements as foreigners.
3766            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3767          } elsif ($node->[1] & TABLE_CELL_EL) {
3768            if ($last) {
3769              !!!cp ('t28.2');
3770            #            #
3771          } else {          } else {
3772            $node = $self->{inner_html_node};            !!!cp ('t28.3');
3773              $new_mode = IN_CELL_IM;
3774          }          }
3775          } else {
3776            !!!cp ('t28.4');
3777            $new_mode = {
3778                          select => IN_SELECT_IM,
3779                          ## NOTE: |option| and |optgroup| do not set
3780                          ## insertion mode to "in select" by themselves.
3781                          tr => IN_ROW_IM,
3782                          tbody => IN_TABLE_BODY_IM,
3783                          thead => IN_TABLE_BODY_IM,
3784                          tfoot => IN_TABLE_BODY_IM,
3785                          caption => IN_CAPTION_IM,
3786                          colgroup => IN_COLUMN_GROUP_IM,
3787                          table => IN_TABLE_IM,
3788                          head => IN_BODY_IM, # not in head!
3789                          body => IN_BODY_IM,
3790                          frameset => IN_FRAMESET_IM,
3791                         }->{$node->[0]->manakai_local_name};
3792        }        }
       
       ## 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]};  
3793        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3794                
3795        ## Step 14        ## Step 15
3796        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3797          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3798            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3799              $self->{insertion_mode} = BEFORE_HEAD_IM;
3800          } else {          } else {
3801            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3802              !!!cp ('t30');
3803              $self->{insertion_mode} = AFTER_HEAD_IM;
3804          }          }
3805          return;          return;
3806          } else {
3807            !!!cp ('t31');
3808        }        }
3809                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3810        ## Step 16        ## Step 16
3811          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3812          
3813          ## Step 17
3814        $i--;        $i--;
3815        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3816                
3817        ## Step 17        ## Step 18
3818        redo S3;        redo S3;
3819      } # S3      } # S3
3820    
3821      die "$0: _reset_insertion_mode: This line should never be reached";
3822  } # _reset_insertion_mode  } # _reset_insertion_mode
3823    
3824  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3825    my $self = shift;    my $self = shift;
3826    
   my $phase = 'main';  
   
3827    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3828    
3829    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1884  sub _tree_construction_main ($) { Line 3840  sub _tree_construction_main ($) {
3840      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3841      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3842        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3843            !!!cp ('t32');
3844          return;          return;
3845        }        }
3846      }      }
# Line 1898  sub _tree_construction_main ($) { Line 3855  sub _tree_construction_main ($) {
3855    
3856        ## Step 6        ## Step 6
3857        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3858            !!!cp ('t33_1');
3859          #          #
3860        } else {        } else {
3861          my $in_open_elements;          my $in_open_elements;
3862          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3863            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3864                !!!cp ('t33');
3865              $in_open_elements = 1;              $in_open_elements = 1;
3866              last OE;              last OE;
3867            }            }
3868          }          }
3869          if ($in_open_elements) {          if ($in_open_elements) {
3870              !!!cp ('t34');
3871            #            #
3872          } else {          } else {
3873              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3874              !!!cp ('t35');
3875            redo S4;            redo S4;
3876          }          }
3877        }        }
# Line 1932  sub _tree_construction_main ($) { Line 3894  sub _tree_construction_main ($) {
3894    
3895        ## Step 11        ## Step 11
3896        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3897            !!!cp ('t36');
3898          ## Step 7'          ## Step 7'
3899          $i++;          $i++;
3900          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3901                    
3902          redo S7;          redo S7;
3903        }        }
3904    
3905          !!!cp ('t37');
3906      } # S7      } # S7
3907    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3908    
3909    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3910      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3911        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3912            !!!cp ('t38');
3913          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3914          return;          return;
3915        }        }
3916      }      }
3917    
3918        !!!cp ('t39');
3919    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3920    
3921    my $style_start_tag = sub {    my $insert;
3922      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});  
3923      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
3924      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
3925       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
3926        ->append_child ($style_el);      ## Step 1
3927      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
3928        my $el;
3929        !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3930    
3931        ## Step 2
3932        $insert->($el);
3933    
3934        ## Step 3
3935        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3936      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3937                  
3938        ## Step 4
3939      my $text = '';      my $text = '';
3940        !!!nack ('t40.1');
3941      !!!next-token;      !!!next-token;
3942      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3943          !!!cp ('t40');
3944        $text .= $token->{data};        $text .= $token->{data};
3945        !!!next-token;        !!!next-token;
3946      } # stop if non-character token or tokenizer stops tokenising      }
3947    
3948        ## Step 5
3949      if (length $text) {      if (length $text) {
3950        $style_el->manakai_append_text ($text);        !!!cp ('t41');
3951          my $text = $self->{document}->create_text_node ($text);
3952          $el->append_child ($text);
3953      }      }
3954        
3955      $self->{content_model_flag} = 'PCDATA';      ## Step 6
3956                      $self->{content_model} = PCDATA_CONTENT_MODEL;
3957      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
3958        ## Step 7
3959        if ($token->{type} == END_TAG_TOKEN and
3960            $token->{tag_name} eq $start_tag_name) {
3961          !!!cp ('t42');
3962        ## Ignore the token        ## Ignore the token
3963      } else {      } else {
3964        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
3965        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3966            !!!cp ('t43');
3967            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3968          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3969            !!!cp ('t44');
3970            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3971          } else {
3972            die "$0: $content_model_flag in parse_rcdata";
3973          }
3974      }      }
3975      !!!next-token;      !!!next-token;
3976    }; # $style_start_tag    }; # $parse_rcdata
3977    
3978    my $script_start_tag = sub {    my $script_start_tag = sub () {
3979      my $script_el;      my $script_el;
3980      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3981      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3982    
3983      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3984      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3985            
3986      my $text = '';      my $text = '';
3987        !!!nack ('t45.1');
3988      !!!next-token;      !!!next-token;
3989      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3990          !!!cp ('t45');
3991        $text .= $token->{data};        $text .= $token->{data};
3992        !!!next-token;        !!!next-token;
3993      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3994      if (length $text) {      if (length $text) {
3995          !!!cp ('t46');
3996        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3997      }      }
3998                                
3999      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
4000    
4001      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
4002          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
4003          !!!cp ('t47');
4004        ## Ignore the token        ## Ignore the token
4005      } else {      } else {
4006        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
4007          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4008        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4009        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4010      }      }
4011            
4012      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
4013          !!!cp ('t49');
4014        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4015      } else {      } else {
4016          !!!cp ('t50');
4017        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
4018        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
4019          
4020        (($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);  
4021                
4022        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
4023                
# Line 2026  sub _tree_construction_main ($) { Line 4027  sub _tree_construction_main ($) {
4027      !!!next-token;      !!!next-token;
4028    }; # $script_start_tag    }; # $script_start_tag
4029    
4030      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
4031      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
4032      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4033    
4034    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4035      my $tag_name = shift;      my $end_tag_token = shift;
4036        my $tag_name = $end_tag_token->{tag_name};
4037    
4038        ## NOTE: The adoption agency algorithm (AAA).
4039    
4040      FET: {      FET: {
4041        ## Step 1        ## Step 1
4042        my $formatting_element;        my $formatting_element;
4043        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4044        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4045          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4046              !!!cp ('t52');
4047              last AFE;
4048            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4049                         eq $tag_name) {
4050              !!!cp ('t51');
4051            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4052            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4053            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
4054          }          }
4055        } # AFE        } # AFE
4056        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4057          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
4058            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4059          ## Ignore the token          ## Ignore the token
4060          !!!next-token;          !!!next-token;
4061          return;          return;
# Line 2055  sub _tree_construction_main ($) { Line 4067  sub _tree_construction_main ($) {
4067          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4068          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
4069            if ($in_scope) {            if ($in_scope) {
4070                !!!cp ('t54');
4071              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
4072              last INSCOPE;              last INSCOPE;
4073            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4074              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
4075                !!!parse-error (type => 'unmatched end tag',
4076                                text => $token->{tag_name},
4077                                token => $end_tag_token);
4078              ## Ignore the token              ## Ignore the token
4079              !!!next-token;              !!!next-token;
4080              return;              return;
4081            }            }
4082          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
4083                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4084            $in_scope = 0;            $in_scope = 0;
4085          }          }
4086        } # INSCOPE        } # INSCOPE
4087        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4088          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
4089            !!!parse-error (type => 'unmatched end tag',
4090                            text => $token->{tag_name},
4091                            token => $end_tag_token);
4092          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4093          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4094          return;          return;
4095        }        }
4096        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4097          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
4098            !!!parse-error (type => 'not closed',
4099                            text => $self->{open_elements}->[-1]->[0]
4100                                ->manakai_local_name,
4101                            token => $end_tag_token);
4102        }        }
4103                
4104        ## Step 2        ## Step 2
# Line 2085  sub _tree_construction_main ($) { Line 4106  sub _tree_construction_main ($) {
4106        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4107        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4108          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4109          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4110              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4111              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4112               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4113              !!!cp ('t59');
4114            $furthest_block = $node;            $furthest_block = $node;
4115            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
4116          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
4117              !!!cp ('t60');
4118            last OE;            last OE;
4119          }          }
4120        } # OE        } # OE
4121                
4122        ## Step 3        ## Step 3
4123        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
4124            !!!cp ('t61');
4125          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
4126          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
4127          !!!next-token;          !!!next-token;
# Line 2110  sub _tree_construction_main ($) { Line 4134  sub _tree_construction_main ($) {
4134        ## Step 5        ## Step 5
4135        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
4136        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
4137            !!!cp ('t62');
4138          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
4139        }        }
4140                
# Line 2132  sub _tree_construction_main ($) { Line 4157  sub _tree_construction_main ($) {
4157          S7S2: {          S7S2: {
4158            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
4159              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4160                  !!!cp ('t63');
4161                $node_i_in_active = $_;                $node_i_in_active = $_;
4162                last S7S2;                last S7S2;
4163              }              }
# Line 2145  sub _tree_construction_main ($) { Line 4171  sub _tree_construction_main ($) {
4171                    
4172          ## Step 4          ## Step 4
4173          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
4174              !!!cp ('t64');
4175            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
4176          }          }
4177                    
4178          ## Step 5          ## Step 5
4179          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
4180              !!!cp ('t65');
4181            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
4182            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
4183            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2167  sub _tree_construction_main ($) { Line 4195  sub _tree_construction_main ($) {
4195        } # S7          } # S7  
4196                
4197        ## Step 8        ## Step 8
4198        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4199            my $foster_parent_element;
4200            my $next_sibling;
4201            OE: for (reverse 0..$#{$self->{open_elements}}) {
4202              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4203                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4204                                 if (defined $parent and $parent->node_type == 1) {
4205                                   !!!cp ('t65.1');
4206                                   $foster_parent_element = $parent;
4207                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4208                                 } else {
4209                                   !!!cp ('t65.2');
4210                                   $foster_parent_element
4211                                     = $self->{open_elements}->[$_ - 1]->[0];
4212                                 }
4213                                 last OE;
4214                               }
4215                             } # OE
4216                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4217                               unless defined $foster_parent_element;
4218            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4219            $open_tables->[-1]->[1] = 1; # tainted
4220          } else {
4221            !!!cp ('t65.3');
4222            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4223          }
4224                
4225        ## Step 9        ## Step 9
4226        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2184  sub _tree_construction_main ($) { Line 4237  sub _tree_construction_main ($) {
4237        my $i;        my $i;
4238        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4239          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4240              !!!cp ('t66');
4241            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
4242            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
4243          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4244              !!!cp ('t67');
4245            $i = $_;            $i = $_;
4246          }          }
4247        } # AFE        } # AFE
# Line 2196  sub _tree_construction_main ($) { Line 4251  sub _tree_construction_main ($) {
4251        undef $i;        undef $i;
4252        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4253          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4254              !!!cp ('t68');
4255            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4256            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4257          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4258              !!!cp ('t69');
4259            $i = $_;            $i = $_;
4260          }          }
4261        } # OE        } # OE
# Line 2209  sub _tree_construction_main ($) { Line 4266  sub _tree_construction_main ($) {
4266      } # FET      } # FET
4267    }; # $formatting_end_tag    }; # $formatting_end_tag
4268    
4269    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4270      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4271    }; # $insert_to_current    }; # $insert_to_current
4272    
4273    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4274                         my $child = shift;      my $child = shift;
4275                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4276                              table => 1, tbody => 1, tfoot => 1,        # MUST
4277                              thead => 1, tr => 1,        my $foster_parent_element;
4278                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4279                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4280                           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') {  
4281                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4282                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4283                                   !!!cp ('t70');
4284                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4285                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4286                               } else {                               } else {
4287                                   !!!cp ('t71');
4288                                 $foster_parent_element                                 $foster_parent_element
4289                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4290                               }                               }
# Line 2239  sub _tree_construction_main ($) { Line 4295  sub _tree_construction_main ($) {
4295                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4296                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4297                             ($child, $next_sibling);                             ($child, $next_sibling);
4298                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4299                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4300                         }        !!!cp ('t72');
4301          $self->{open_elements}->[-1]->[0]->append_child ($child);
4302        }
4303    }; # $insert_to_foster    }; # $insert_to_foster
4304    
4305    my $in_body = sub {    B: while (1) {
4306      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
4307      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
4308        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4309          $script_start_tag->();        ## Ignore the token
4310          return;        ## Stay in the phase
4311        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
4312          $style_start_tag->();        next B;
4313          return;      } elsif ($token->{type} == START_TAG_TOKEN and
4314        } elsif ({               $token->{tag_name} eq 'html') {
4315                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4316                 }->{$token->{tag_name}}) {          !!!cp ('t79');
4317          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html', text => 'html', token => $token);
4318          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
4319          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4320          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
4321          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html', text => 'html', token => $token);
4322            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4323          } else {        } else {
4324            $insert->($el);          !!!cp ('t81');
4325          }        }
4326            
4327          !!!next-token;        !!!cp ('t82');
4328          return;        !!!parse-error (type => 'not first start tag', token => $token);
4329        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
4330          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
4331          ## NOTE: There is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4332          my $title_el;            !!!cp ('t84');
4333          !!!create-element ($title_el, 'title', $token->{attributes});            $top_el->set_attribute_ns
4334          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              (undef, [undef, $attr_name],
4335            ->append_child ($title_el);               $token->{attributes}->{$attr_name}->{value});
         $self->{content_model_flag} = 'RCDATA';  
         delete $self->{escape}; # MUST  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
4336          }          }
4337                    }
4338          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!nack ('t84.1');
4339                    !!!next-token;
4340          next B;
4341        } elsif ($token->{type} == COMMENT_TOKEN) {
4342          my $comment = $self->{document}->create_comment ($token->{data});
4343          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4344            !!!cp ('t85');
4345            $self->{document}->append_child ($comment);
4346          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4347            !!!cp ('t86');
4348            $self->{open_elements}->[0]->[0]->append_child ($comment);
4349          } else {
4350            !!!cp ('t87');
4351            $self->{open_elements}->[-1]->[0]->append_child ($comment);
4352          }
4353          !!!next-token;
4354          next B;
4355        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4356          if ($token->{type} == CHARACTER_TOKEN) {
4357            !!!cp ('t87.1');
4358            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4359          !!!next-token;          !!!next-token;
4360          return;          next B;
4361        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4362          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4363            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4364            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4365              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4366                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4367              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4368              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4369              $formatting_end_tag->($token->{tag_name});            #
4370                        } elsif ({
4371              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4372                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4373                  splice @$active_formatting_elements, $_, 1;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4374                  last AFE2;                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4375                }                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4376              } # AFE2                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4377              OE: for (reverse 0..$#{$self->{open_elements}}) {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4378                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4379                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4380                  last OE;            !!!cp ('t87.2');
4381                }            !!!parse-error (type => 'not closed',
4382              } # OE                            text => $self->{open_elements}->[-1]->[0]
4383              last AFE;                                ->manakai_local_name,
4384            } elsif ($node->[0] eq '#marker') {                            token => $token);
4385              last AFE;  
4386              pop @{$self->{open_elements}}
4387                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4388    
4389              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4390              ## Reprocess.
4391              next B;
4392            } else {
4393              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4394              my $tag_name = $token->{tag_name};
4395              if ($nsuri eq $SVG_NS) {
4396                $tag_name = {
4397                   altglyph => 'altGlyph',
4398                   altglyphdef => 'altGlyphDef',
4399                   altglyphitem => 'altGlyphItem',
4400                   animatecolor => 'animateColor',
4401                   animatemotion => 'animateMotion',
4402                   animatetransform => 'animateTransform',
4403                   clippath => 'clipPath',
4404                   feblend => 'feBlend',
4405                   fecolormatrix => 'feColorMatrix',
4406                   fecomponenttransfer => 'feComponentTransfer',
4407                   fecomposite => 'feComposite',
4408                   feconvolvematrix => 'feConvolveMatrix',
4409                   fediffuselighting => 'feDiffuseLighting',
4410                   fedisplacementmap => 'feDisplacementMap',
4411                   fedistantlight => 'feDistantLight',
4412                   feflood => 'feFlood',
4413                   fefunca => 'feFuncA',
4414                   fefuncb => 'feFuncB',
4415                   fefuncg => 'feFuncG',
4416                   fefuncr => 'feFuncR',
4417                   fegaussianblur => 'feGaussianBlur',
4418                   feimage => 'feImage',
4419                   femerge => 'feMerge',
4420                   femergenode => 'feMergeNode',
4421                   femorphology => 'feMorphology',
4422                   feoffset => 'feOffset',
4423                   fepointlight => 'fePointLight',
4424                   fespecularlighting => 'feSpecularLighting',
4425                   fespotlight => 'feSpotLight',
4426                   fetile => 'feTile',
4427                   feturbulence => 'feTurbulence',
4428                   foreignobject => 'foreignObject',
4429                   glyphref => 'glyphRef',
4430                   lineargradient => 'linearGradient',
4431                   radialgradient => 'radialGradient',
4432                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4433                   textpath => 'textPath',  
4434                }->{$tag_name} || $tag_name;
4435            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4436    
4437          !!!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];  
4438    
4439          !!!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', ''];  
4440    
4441          !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4442          return;  
4443        } elsif ($token->{tag_name} eq 'marquee' or            if ($self->{self_closing}) {
4444                 $token->{tag_name} eq 'object') {              pop @{$self->{open_elements}};
4445          $reconstruct_active_formatting_elements->($insert_to_current);              !!!ack ('t87.3');
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
         delete $self->{escape}; # MUST  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
           
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                         {type => 'character',  
                          data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD  
                         ## TODO: make this configurable  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'},  
                        );  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ({  
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: $self->{form_element} if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         if ($token->{tag_name} eq 'textarea') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
4446            } else {            } else {
4447              !!!parse-error (type => 'in CDATA:#'.$token->{type});              !!!cp ('t87.4');
4448            }            }
4449            ## ISSUE: And ignore?  
4450              !!!next-token;
4451              next B;
4452          }          }
4453          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4454          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4455        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
4456          $reconstruct_active_formatting_elements->($insert_to_current);          #
4457                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4458          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t87.6');
4459                    !!!parse-error (type => 'not closed',
4460          $self->{insertion_mode} = 'in select';                          text => $self->{open_elements}->[-1]->[0]
4461          !!!next-token;                              ->manakai_local_name,
4462          return;                          token => $token);
4463        } elsif ({  
4464                  caption => 1, col => 1, colgroup => 1, frame => 1,          pop @{$self->{open_elements}}
4465                  frameset => 1, head => 1, option => 1, optgroup => 1,              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4466                  tbody => 1, td => 1, tfoot => 1, th => 1,  
4467                  thead => 1, tr => 1,          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4468                 }->{$token->{tag_name}}) {          ## Reprocess.
4469          !!!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.  
4470        } else {        } else {
4471          $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;  
4472        }        }
4473      } elsif ($token->{type} eq 'end tag') {      }
4474        if ($token->{tag_name} eq 'body') {  
4475          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      if ($self->{insertion_mode} & HEAD_IMS) {
4476            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
4477            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4478              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4479            }              !!!cp ('t88.2');
4480            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4481            !!!next-token;              #
4482            return;            } else {
4483          } else {              !!!cp ('t88.1');
4484            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              ## Ignore the token.
4485            ## Ignore the token              #
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4486            }            }
4487          } # INSCOPE            unless (length $token->{data}) {
4488                        !!!cp ('t88');
4489          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
4490            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              next B;
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4491            }            }
4492          } # INSCOPE  ## TODO: set $token->{column} appropriately
           
         if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {  
           pop @{$self->{open_elements}};  
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4493          }          }
4494    
4495          undef $self->{form_element};          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4496          !!!next-token;            !!!cp ('t89');
4497          return;            ## As if <head>
4498        } elsif ({            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4499                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4500                 }->{$token->{tag_name}}) {            push @{$self->{open_elements}},
4501          ## has an element in scope                [$self->{head_element}, $el_category->{head}];
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
 ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
4502    
4503          ## Step 2            ## Reprocess in the "in head" insertion mode...
4504          S2: {            pop @{$self->{open_elements}};
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
4505    
4506              !!!next-token;            ## Reprocess in the "after head" insertion mode...
4507              last S2;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4508            } else {            !!!cp ('t90');
4509              ## Step 3            ## As if </noscript>
4510              if (not $formatting_category->{$node->[1]} and            pop @{$self->{open_elements}};
4511                  #not $phrasing_category->{$node->[1]} and            !!!parse-error (type => 'in noscript:#text', token => $token);
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'not closed:'.$node->[1]);  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
4512                        
4513            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4514            redo S2;            ## As if </head>
4515          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
4516    
4517    B: {            ## Reprocess in the "after head" insertion mode...
4518      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4519        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
4520          !!!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]);  
         }  
4521    
4522          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4523          last B;          } else {
4524              !!!cp ('t92');
4525            }
4526    
4527          ## ISSUE: There is an issue in the spec.          ## "after head" insertion mode
4528        } else {          ## As if <body>
4529          if ($self->{insertion_mode} eq 'before head') {          !!!insert-element ('body',, $token);
4530            if ($token->{type} eq 'character') {          $self->{insertion_mode} = IN_BODY_IM;
4531              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## reprocess
4532                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          next B;
4533                unless (length $token->{data}) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4534                  !!!next-token;          if ($token->{tag_name} eq 'head') {
4535                  redo B;            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4536                }              !!!cp ('t93');
4537              }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4538              ## As if <head>              $self->{open_elements}->[-1]->[0]->append_child
4539              !!!create-element ($self->{head_element}, 'head');                  ($self->{head_element});
4540              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              push @{$self->{open_elements}},
4541              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  [$self->{head_element}, $el_category->{head}];
4542              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = IN_HEAD_IM;
4543              ## 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);  
4544              !!!next-token;              !!!next-token;
4545              redo B;              next B;
4546            } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4547              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              !!!cp ('t93.2');
4548              !!!create-element ($self->{head_element}, 'head', $attr);              !!!parse-error (type => 'after head', text => 'head',
4549              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token);
4550              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4551              $self->{insertion_mode} = 'in head';              !!!nack ('t93.3');
4552              if ($token->{tag_name} eq 'head') {              !!!next-token;
4553                !!!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;  
             }  
4554            } else {            } else {
4555              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4556            }              !!!parse-error (type => 'in head:head',
4557          } elsif ($self->{insertion_mode} eq 'in head') {                              token => $token); # or in head noscript
4558            if ($token->{type} eq 'character') {              ## Ignore the token
4559              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);  
4560              !!!next-token;              !!!next-token;
4561              redo B;              next B;
4562            } elsif ($token->{type} eq 'start tag') {            }
4563              if ($token->{tag_name} eq 'title') {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4564                ## NOTE: There is an "as if in head" code clone            !!!cp ('t96');
4565                my $title_el;            ## As if <head>
4566                !!!create-element ($title_el, 'title', $token->{attributes});            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4567                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4568                  ->append_child ($title_el);            push @{$self->{open_elements}},
4569                $self->{content_model_flag} = 'RCDATA';                [$self->{head_element}, $el_category->{head}];
4570                delete $self->{escape}; # MUST  
4571              $self->{insertion_mode} = IN_HEAD_IM;
4572                my $text = '';            ## Reprocess in the "in head" insertion mode...
4573                !!!next-token;          } else {
4574                while ($token->{type} eq 'character') {            !!!cp ('t97');
4575                  $text .= $token->{data};          }
4576                  !!!next-token;  
4577                }              if ($token->{tag_name} eq 'base') {
4578                if (length $text) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4579                  $title_el->manakai_append_text ($text);                  !!!cp ('t98');
4580                }                  ## As if </noscript>
4581                                  pop @{$self->{open_elements}};
4582                $self->{content_model_flag} = 'PCDATA';                  !!!parse-error (type => 'in noscript', text => 'base',
4583                                    token => $token);
4584                                
4585                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
4586                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
4587                } else {                } else {
4588                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
4589                }                }
               !!!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);  
4590    
4591                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
4592                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4593              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
4594                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head',
4595                ## Ignore the token                                  text => $token->{tag_name}, token => $token);
4596                !!!next-token;                  push @{$self->{open_elements}},
4597                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}};  
4598                } else {                } else {
4599                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
4600                }                }
4601                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4602                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4603                  pop @{$self->{open_elements}} # <head>
4604                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4605                  !!!nack ('t101.1');
4606                !!!next-token;                !!!next-token;
4607                redo B;                next B;
4608              } elsif ($token->{tag_name} eq 'html') {              } elsif ($token->{tag_name} eq 'link') {
4609                #                ## NOTE: There is a "as if in head" code clone.
4610              } else {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4611                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t102');
4612                ## Ignore the token                  !!!parse-error (type => 'after head',
4613                !!!next-token;                                  text => $token->{tag_name}, token => $token);
4614                redo B;                  push @{$self->{open_elements}},
4615              }                      [$self->{head_element}, $el_category->{head}];
4616            } else {                } else {
4617              #                  !!!cp ('t103');
           }  
   
           if ($self->{open_elements}->[-1]->[1] eq 'head') {  
             ## As if </head>  
             pop @{$self->{open_elements}};  
           }  
           $self->{insertion_mode} = 'after head';  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
4618                }                }
4619              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4620                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4621              #                pop @{$self->{open_elements}} # <head>
4622            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4623              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';  
4624                !!!next-token;                !!!next-token;
4625                redo B;                next B;
4626              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
4627                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
4628                        script => 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4629                       }->{$token->{tag_name}}) {                  !!!cp ('t104');
4630                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4631                $self->{insertion_mode} = 'in head';                                  text => $token->{tag_name}, token => $token);
4632                ## reprocess                  push @{$self->{open_elements}},
4633                redo B;                      [$self->{head_element}, $el_category->{head}];
4634              } else {                } else {
4635                #                  !!!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;  
4636                }                }
4637              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4638                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
             !!!parse-error (type => 'in table:#character');  
4639    
4640              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
4641              ## ISSUE: Spec says that "whenever a node would be inserted                  if ($token->{attributes}->{charset}) {
4642              ## into the current node" while characters might not be                    !!!cp ('t106');
4643              ## result in a new Text node.                    ## NOTE: Whether the encoding is supported or not is handled
4644              $reconstruct_active_formatting_elements->($insert_to_foster);                    ## in the {change_encoding} callback.
4645                                  $self->{change_encoding}
4646              if ({                        ->($self, $token->{attributes}->{charset}->{value},
4647                   table => 1, tbody => 1, tfoot => 1,                           $token);
4648                   thead => 1, tr => 1,                    
4649                  }->{$self->{open_elements}->[-1]->[1]}) {                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4650                # MUST                        ->set_user_data (manakai_has_reference =>
4651                my $foster_parent_element;                                             $token->{attributes}->{charset}
4652                my $next_sibling;                                                 ->{has_reference});
4653                my $prev_sibling;                  } elsif ($token->{attributes}->{content}) {
4654                OE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($token->{attributes}->{content}->{value}
4655                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4656                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            [\x09-\x0D\x20]*=
4657                    if (defined $parent and $parent->node_type == 1) {                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4658                      $foster_parent_element = $parent;                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4659                      $next_sibling = $self->{open_elements}->[$_]->[0];                      !!!cp ('t107');
4660                      $prev_sibling = $next_sibling->previous_sibling;                      ## NOTE: Whether the encoding is supported or not is handled
4661                        ## in the {change_encoding} callback.
4662                        $self->{change_encoding}
4663                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4664                               $token);
4665                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4666                            ->set_user_data (manakai_has_reference =>
4667                                                 $token->{attributes}->{content}
4668                                                       ->{has_reference});
4669                    } else {                    } else {
4670                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
4671                    }                    }
                   last OE;  
4672                  }                  }
               } # 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});  
4673                } else {                } else {
4674                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
4675                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
4676                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4677                }                        ->set_user_data (manakai_has_reference =>
4678              } else {                                             $token->{attributes}->{charset}
4679                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
4680              }                  }
4681                                if ($token->{attributes}->{content}) {
4682              !!!next-token;                    !!!cp ('t110');
4683              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4684            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
4685              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
4686              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
4687              !!!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}};  
4688                }                }
4689    
4690                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
4691                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4692                  !!!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}};  
4693                !!!next-token;                !!!next-token;
4694                redo B;                next B;
4695              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
4696                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4697                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
4698                       }->{$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]);  
4699                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4700                    !!!parse-error (type => 'in noscript', text => 'title',
4701                                    token => $token);
4702                  
4703                    $self->{insertion_mode} = IN_HEAD_IM;
4704                    ## Reprocess in the "in head" insertion mode...
4705                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4706                    !!!cp ('t112');
4707                    !!!parse-error (type => 'after head',
4708                                    text => $token->{tag_name}, token => $token);
4709                    push @{$self->{open_elements}},
4710                        [$self->{head_element}, $el_category->{head}];
4711                  } else {
4712                    !!!cp ('t113');
4713                }                }
4714    
4715                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
4716                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
4717                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
4718                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4719                redo B;                pop @{$self->{open_elements}} # <head>
4720              } elsif ($token->{tag_name} eq 'table') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4721                ## NOTE: There are code clones for this "table in table"                next B;
4722                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } elsif ($token->{tag_name} eq 'style' or
4723                         $token->{tag_name} eq 'noframes') {
4724                ## As if </table>                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4725                ## have a table element in table scope                ## insertion mode IN_HEAD_IM)
4726                my $i;                ## NOTE: There is a "as if in head" code clone.
4727                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4728                  my $node = $self->{open_elements}->[$_];                  !!!cp ('t114');
4729                  if ($node->[1] eq 'table') {                  !!!parse-error (type => 'after head',
4730                    $i = $_;                                  text => $token->{tag_name}, token => $token);
4731                    last INSCOPE;                  push @{$self->{open_elements}},
4732                  } elsif ({                      [$self->{head_element}, $el_category->{head}];
4733                            table => 1, html => 1,                } else {
4734                           }->{$node->[1]}) {                  !!!cp ('t115');
4735                    last INSCOPE;                }
4736                  }                $parse_rcdata->(CDATA_CONTENT_MODEL);
4737                } # INSCOPE                pop @{$self->{open_elements}} # <head>
4738                unless (defined $i) {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4739                  !!!parse-error (type => 'unmatched end tag:table');                next B;
4740                  ## Ignore tokens </table><table>              } elsif ($token->{tag_name} eq 'noscript') {
4741                  if ($self->{insertion_mode} == IN_HEAD_IM) {
4742                    !!!cp ('t116');
4743                    ## NOTE: and scripting is disalbed
4744                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4745                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4746                    !!!nack ('t116.1');
4747                  !!!next-token;                  !!!next-token;
4748                  redo B;                  next B;
4749                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4750                    !!!cp ('t117');
4751                    !!!parse-error (type => 'in noscript', text => 'noscript',
4752                                    token => $token);
4753                    ## Ignore the token
4754                    !!!nack ('t117.1');
4755                    !!!next-token;
4756                    next B;
4757                  } else {
4758                    !!!cp ('t118');
4759                    #
4760                }                }
4761                } elsif ($token->{tag_name} eq 'script') {
4762                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4763                    !!!cp ('t119');
4764                    ## As if </noscript>
4765                    pop @{$self->{open_elements}};
4766                    !!!parse-error (type => 'in noscript', text => 'script',
4767                                    token => $token);
4768                                
4769                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
4770                if ({                  ## Reprocess in the "in head" insertion mode...
4771                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4772                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
4773                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head',
4774                  !!!back-token; # <table>                                  text => $token->{tag_name}, token => $token);
4775                  $token = {type => 'end tag', tag_name => 'table'};                  push @{$self->{open_elements}},
4776                  !!!back-token;                      [$self->{head_element}, $el_category->{head}];
4777                  $token = {type => 'end tag',                } else {
4778                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!cp ('t121');
                 redo B;  
4779                }                }
4780    
4781                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
4782                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
4783                  pop @{$self->{open_elements}} # <head>
4784                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4785                  next B;
4786                } elsif ($token->{tag_name} eq 'body' or
4787                         $token->{tag_name} eq 'frameset') {
4788                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4789                    !!!cp ('t122');
4790                    ## As if </noscript>
4791                    pop @{$self->{open_elements}};
4792                    !!!parse-error (type => 'in noscript',
4793                                    text => $token->{tag_name}, token => $token);
4794                    
4795                    ## Reprocess in the "in head" insertion mode...
4796                    ## As if </head>
4797                    pop @{$self->{open_elements}};
4798                    
4799                    ## Reprocess in the "after head" insertion mode...
4800                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4801                    !!!cp ('t124');
4802                    pop @{$self->{open_elements}};
4803                    
4804                    ## Reprocess in the "after head" insertion mode...
4805                  } else {
4806                    !!!cp ('t125');
4807                }                }
4808    
4809                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
4810                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4811                  if ($token->{tag_name} eq 'body') {
4812                    !!!cp ('t126');
4813                    $self->{insertion_mode} = IN_BODY_IM;
4814                  } elsif ($token->{tag_name} eq 'frameset') {
4815                    !!!cp ('t127');
4816                    $self->{insertion_mode} = IN_FRAMESET_IM;
4817                  } else {
4818                    die "$0: tag name: $self->{tag_name}";
4819                  }
4820                  !!!nack ('t127.1');
4821                  !!!next-token;
4822                  next B;
4823                } else {
4824                  !!!cp ('t128');
4825                  #
4826                }
4827    
4828                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4829                  !!!cp ('t129');
4830                  ## As if </noscript>
4831                  pop @{$self->{open_elements}};
4832                  !!!parse-error (type => 'in noscript:/',
4833                                  text => $token->{tag_name}, token => $token);
4834                  
4835                  ## Reprocess in the "in head" insertion mode...
4836                  ## As if </head>
4837                  pop @{$self->{open_elements}};
4838    
4839                ## reprocess                ## Reprocess in the "after head" insertion mode...
4840                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4841                  !!!cp ('t130');
4842                  ## As if </head>
4843                  pop @{$self->{open_elements}};
4844    
4845                  ## Reprocess in the "after head" insertion mode...
4846              } else {              } else {
4847                #                !!!cp ('t131');
4848              }              }
4849            } elsif ($token->{type} eq 'end tag') {  
4850              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
4851                ## have a table element in table scope              ## As if <body>
4852                my $i;              !!!insert-element ('body',, $token);
4853                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
4854                  my $node = $self->{open_elements}->[$_];              ## reprocess
4855                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
4856                    $i = $_;              next B;
4857                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
4858                  } elsif ({              if ($token->{tag_name} eq 'head') {
4859                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4860                           }->{$node->[1]}) {                  !!!cp ('t132');
4861                    last INSCOPE;                  ## As if <head>
4862                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4863                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4864                unless (defined $i) {                  push @{$self->{open_elements}},
4865                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
4866    
4867                    ## Reprocess in the "in head" insertion mode...
4868                    pop @{$self->{open_elements}};
4869                    $self->{insertion_mode} = AFTER_HEAD_IM;
4870                    !!!next-token;
4871                    next B;
4872                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4873                    !!!cp ('t133');
4874                    ## As if </noscript>
4875                    pop @{$self->{open_elements}};
4876                    !!!parse-error (type => 'in noscript:/',
4877                                    text => 'head', token => $token);
4878                    
4879                    ## Reprocess in the "in head" insertion mode...
4880                    pop @{$self->{open_elements}};
4881                    $self->{insertion_mode} = AFTER_HEAD_IM;
4882                    !!!next-token;
4883                    next B;
4884                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4885                    !!!cp ('t134');
4886                    pop @{$self->{open_elements}};
4887                    $self->{insertion_mode} = AFTER_HEAD_IM;
4888                    !!!next-token;
4889                    next B;
4890                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4891                    !!!cp ('t134.1');
4892                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4893                                    token => $token);
4894                  ## Ignore the token                  ## Ignore the token
4895                  !!!next-token;                  !!!next-token;
4896                  redo B;                  next B;
4897                  } else {
4898                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4899                }                }
4900                              } elsif ($token->{tag_name} eq 'noscript') {
4901                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4902                if ({                  !!!cp ('t136');
4903                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}};
4904                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
4905                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!next-token;
4906                  !!!back-token;                  next B;
4907                  $token = {type => 'end tag',                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4908                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4909                  redo B;                  !!!cp ('t137');
4910                    !!!parse-error (type => 'unmatched end tag',
4911                                    text => 'noscript', token => $token);
4912                    ## Ignore the token ## ISSUE: An issue in the spec.
4913                    !!!next-token;
4914                    next B;
4915                  } else {
4916                    !!!cp ('t138');
4917                    #
4918                }                }
4919                } elsif ({
4920                if ($self->{open_elements}->[-1]->[1] ne 'table') {                        body => 1, html => 1,
4921                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                       }->{$token->{tag_name}}) {
4922                  if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4923                      $self->{insertion_mode} == IN_HEAD_IM or
4924                      $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4925                    !!!cp ('t140');
4926                    !!!parse-error (type => 'unmatched end tag',
4927                                    text => $token->{tag_name}, token => $token);
4928                    ## Ignore the token
4929                    !!!next-token;
4930                    next B;
4931                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4932                    !!!cp ('t140.1');
4933                    !!!parse-error (type => 'unmatched end tag',
4934                                    text => $token->{tag_name}, token => $token);
4935                    ## Ignore the token
4936                    !!!next-token;
4937                    next B;
4938                  } else {
4939                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4940                }                }
4941                } elsif ($token->{tag_name} eq 'p') {
4942                  !!!cp ('t142');
4943                  !!!parse-error (type => 'unmatched end tag',
4944                                  text => $token->{tag_name}, token => $token);
4945                  ## Ignore the token
4946                  !!!next-token;
4947                  next B;
4948                } elsif ($token->{tag_name} eq 'br') {
4949                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4950                    !!!cp ('t142.2');
4951                    ## (before head) as if <head>, (in head) as if </head>
4952                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4953                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4954                    $self->{insertion_mode} = AFTER_HEAD_IM;
4955      
4956                    ## Reprocess in the "after head" insertion mode...
4957                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4958                    !!!cp ('t143.2');
4959                    ## As if </head>
4960                    pop @{$self->{open_elements}};
4961                    $self->{insertion_mode} = AFTER_HEAD_IM;
4962      
4963                    ## Reprocess in the "after head" insertion mode...
4964                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4965                    !!!cp ('t143.3');
4966                    ## ISSUE: Two parse errors for <head><noscript></br>
4967                    !!!parse-error (type => 'unmatched end tag',
4968                                    text => 'br', token => $token);
4969                    ## As if </noscript>
4970                    pop @{$self->{open_elements}};
4971                    $self->{insertion_mode} = IN_HEAD_IM;
4972    
4973                splice @{$self->{open_elements}}, $i;                  ## Reprocess in the "in head" insertion mode...
4974                    ## As if </head>
4975                    pop @{$self->{open_elements}};
4976                    $self->{insertion_mode} = AFTER_HEAD_IM;
4977    
4978                $self->_reset_insertion_mode;                  ## Reprocess in the "after head" insertion mode...
4979                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4980                    !!!cp ('t143.4');
4981                    #
4982                  } else {
4983                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4984                  }
4985    
4986                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4987                  !!!parse-error (type => 'unmatched end tag',
4988                                  text => 'br', token => $token);
4989                  ## Ignore the token
4990                !!!next-token;                !!!next-token;
4991                redo B;                next B;
4992              } elsif ({              } else {
4993                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t145');
4994                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                !!!parse-error (type => 'unmatched end tag',
4995                        thead => 1, tr => 1,                                text => $token->{tag_name}, token => $token);
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4996                ## Ignore the token                ## Ignore the token
4997                !!!next-token;                !!!next-token;
4998                redo B;                next B;
4999                }
5000    
5001                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5002                  !!!cp ('t146');
5003                  ## As if </noscript>
5004                  pop @{$self->{open_elements}};
5005                  !!!parse-error (type => 'in noscript:/',
5006                                  text => $token->{tag_name}, token => $token);
5007                  
5008                  ## Reprocess in the "in head" insertion mode...
5009                  ## As if </head>
5010                  pop @{$self->{open_elements}};
5011    
5012                  ## Reprocess in the "after head" insertion mode...
5013                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5014                  !!!cp ('t147');
5015                  ## As if </head>
5016                  pop @{$self->{open_elements}};
5017    
5018                  ## Reprocess in the "after head" insertion mode...
5019                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5020    ## ISSUE: This case cannot be reached?
5021                  !!!cp ('t148');
5022                  !!!parse-error (type => 'unmatched end tag',
5023                                  text => $token->{tag_name}, token => $token);
5024                  ## Ignore the token ## ISSUE: An issue in the spec.
5025                  !!!next-token;
5026                  next B;
5027              } else {              } else {
5028                #                !!!cp ('t149');
5029              }              }
           } else {  
             #  
           }  
5030    
5031            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
5032            $in_body->($insert_to_foster);              ## As if <body>
5033            redo B;              !!!insert-element ('body',, $token);
5034          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
5035            if ($token->{type} eq 'character') {              ## reprocess
5036              ## NOTE: This is a code clone of "character in body".              next B;
5037          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5038            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5039              !!!cp ('t149.1');
5040    
5041              ## NOTE: As if <head>
5042              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5043              $self->{open_elements}->[-1]->[0]->append_child
5044                  ($self->{head_element});
5045              #push @{$self->{open_elements}},
5046              #    [$self->{head_element}, $el_category->{head}];
5047              #$self->{insertion_mode} = IN_HEAD_IM;
5048              ## NOTE: Reprocess.
5049    
5050              ## NOTE: As if </head>
5051              #pop @{$self->{open_elements}};
5052              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5053              ## NOTE: Reprocess.
5054              
5055              #
5056            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5057              !!!cp ('t149.2');
5058    
5059              ## NOTE: As if </head>
5060              pop @{$self->{open_elements}};
5061              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5062              ## NOTE: Reprocess.
5063    
5064              #
5065            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5066              !!!cp ('t149.3');
5067    
5068              !!!parse-error (type => 'in noscript:#eof', token => $token);
5069    
5070              ## As if </noscript>
5071              pop @{$self->{open_elements}};
5072              #$self->{insertion_mode} = IN_HEAD_IM;
5073              ## NOTE: Reprocess.
5074    
5075              ## NOTE: As if </head>
5076              pop @{$self->{open_elements}};
5077              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5078              ## NOTE: Reprocess.
5079    
5080              #
5081            } else {
5082              !!!cp ('t149.4');
5083              #
5084            }
5085    
5086            ## NOTE: As if <body>
5087            !!!insert-element ('body',, $token);
5088            $self->{insertion_mode} = IN_BODY_IM;
5089            ## NOTE: Reprocess.
5090            next B;
5091          } else {
5092            die "$0: $token->{type}: Unknown token type";
5093          }
5094    
5095              ## ISSUE: An issue in the spec.
5096        } elsif ($self->{insertion_mode} & BODY_IMS) {
5097              if ($token->{type} == CHARACTER_TOKEN) {
5098                !!!cp ('t150');
5099                ## NOTE: There is a code clone of "character in body".
5100              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
5101                            
5102              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5103    
5104              !!!next-token;              !!!next-token;
5105              redo B;              next B;
5106            } 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') {  
5107              if ({              if ({
5108                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
5109                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
5110                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5111                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
5112                    ## have an element in table scope
5113                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
5114                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
5115                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
5116                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
5117                  my $node = $self->{open_elements}->[$_];  
5118                  if ($node->[1] eq 'caption') {                      ## Close the cell
5119                    $i = $_;                      !!!back-token; # <x>
5120                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
5121                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
5122                            table => 1, html => 1,                                line => $token->{line},
5123                           }->{$node->[1]}) {                                column => $token->{column}};
5124                    last INSCOPE;                      next B;
5125                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5126                        !!!cp ('t152');
5127                        ## ISSUE: This case can never be reached, maybe.
5128                        last;
5129                      }
5130                  }                  }
5131                } # INSCOPE  
5132                unless (defined $i) {                  !!!cp ('t153');
5133                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
5134                        text => $token->{tag_name}, token => $token);
5135                  ## Ignore the token                  ## Ignore the token
5136                    !!!nack ('t153.1');
5137                  !!!next-token;                  !!!next-token;
5138                  redo B;                  next B;
5139                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5140                                  !!!parse-error (type => 'not closed', text => 'caption',
5141                ## generate implied end tags                                  token => $token);
5142                if ({                  
5143                     dd => 1, dt => 1, li => 1, p => 1,                  ## NOTE: As if </caption>.
5144                     td => 1, th => 1, tr => 1,                  ## have a table element in table scope
5145                    }->{$self->{open_elements}->[-1]->[1]}) {                  my $i;
5146                  !!!back-token; # <?>                  INSCOPE: {
5147                  $token = {type => 'end tag', tag_name => 'caption'};                    for (reverse 0..$#{$self->{open_elements}}) {
5148                  !!!back-token;                      my $node = $self->{open_elements}->[$_];
5149                  $token = {type => 'end tag',                      if ($node->[1] & CAPTION_EL) {
5150                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        !!!cp ('t155');
5151                  redo B;                        $i = $_;
5152                }                        last INSCOPE;
5153                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5154                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        !!!cp ('t156');
5155                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        last;
5156                }                      }
5157                      }
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
5158    
5159                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
5160                      !!!parse-error (type => 'start tag not allowed',
5161                                      text => $token->{tag_name}, token => $token);
5162                      ## Ignore the token
5163                      !!!nack ('t157.1');
5164                      !!!next-token;
5165                      next B;
5166                    } # INSCOPE
5167                    
5168                    ## generate implied end tags
5169                    while ($self->{open_elements}->[-1]->[1]
5170                               & END_TAG_OPTIONAL_EL) {
5171                      !!!cp ('t158');
5172                      pop @{$self->{open_elements}};
5173                    }
5174    
5175                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5176                redo B;                    !!!cp ('t159');
5177                      !!!parse-error (type => 'not closed',
5178                                      text => $self->{open_elements}->[-1]->[0]
5179                                          ->manakai_local_name,
5180                                      token => $token);
5181                    } else {
5182                      !!!cp ('t160');
5183                    }
5184                    
5185                    splice @{$self->{open_elements}}, $i;
5186                    
5187                    $clear_up_to_marker->();
5188                    
5189                    $self->{insertion_mode} = IN_TABLE_IM;
5190                    
5191                    ## reprocess
5192                    !!!ack-later;
5193                    next B;
5194                  } else {
5195                    !!!cp ('t161');
5196                    #
5197                  }
5198              } else {              } else {
5199                  !!!cp ('t162');
5200                #                #
5201              }              }
5202            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5203              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
5204                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
5205                my $i;                  ## have an element in table scope
5206                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
5207                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5208                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5209                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5210                    last INSCOPE;                      !!!cp ('t163');
5211                  } elsif ({                      $i = $_;
5212                            table => 1, html => 1,                      last INSCOPE;
5213                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5214                    last INSCOPE;                      !!!cp ('t164');
5215                        last INSCOPE;
5216                      }
5217                    } # INSCOPE
5218                      unless (defined $i) {
5219                        !!!cp ('t165');
5220                        !!!parse-error (type => 'unmatched end tag',
5221                                        text => $token->{tag_name},
5222                                        token => $token);
5223                        ## Ignore the token
5224                        !!!next-token;
5225                        next B;
5226                      }
5227                    
5228                    ## generate implied end tags
5229                    while ($self->{open_elements}->[-1]->[1]
5230                               & END_TAG_OPTIONAL_EL) {
5231                      !!!cp ('t166');
5232                      pop @{$self->{open_elements}};
5233                  }                  }
5234                } # INSCOPE  
5235                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5236                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
5237                      !!!cp ('t167');
5238                      !!!parse-error (type => 'not closed',
5239                                      text => $self->{open_elements}->[-1]->[0]
5240                                          ->manakai_local_name,
5241                                      token => $token);
5242                    } else {
5243                      !!!cp ('t168');
5244                    }
5245                    
5246                    splice @{$self->{open_elements}}, $i;
5247                    
5248                    $clear_up_to_marker->();
5249                    
5250                    $self->{insertion_mode} = IN_ROW_IM;
5251                    
5252                    !!!next-token;
5253                    next B;
5254                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5255                    !!!cp ('t169');
5256                    !!!parse-error (type => 'unmatched end tag',
5257                                    text => $token->{tag_name}, token => $token);
5258                  ## Ignore the token                  ## Ignore the token
5259                  !!!next-token;                  !!!next-token;
5260                  redo B;                  next B;
5261                }                } else {
5262                                  !!!cp ('t170');
5263                ## 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;  
5264                }                }
5265                } elsif ($token->{tag_name} eq 'caption') {
5266                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
5267                    ## have a table element in table scope
5268                    my $i;
5269                    INSCOPE: {
5270                      for (reverse 0..$#{$self->{open_elements}}) {
5271                        my $node = $self->{open_elements}->[$_];
5272                        if ($node->[1] & CAPTION_EL) {
5273                          !!!cp ('t171');
5274                          $i = $_;
5275                          last INSCOPE;
5276                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5277                          !!!cp ('t172');
5278                          last;
5279                        }
5280                      }
5281    
5282                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
5283                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
5284                                      text => $token->{tag_name}, token => $token);
5285                      ## Ignore the token
5286                      !!!next-token;
5287                      next B;
5288                    } # INSCOPE
5289                    
5290                    ## generate implied end tags
5291                    while ($self->{open_elements}->[-1]->[1]
5292                               & END_TAG_OPTIONAL_EL) {
5293                      !!!cp ('t174');
5294                      pop @{$self->{open_elements}};
5295                    }
5296                    
5297                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5298                      !!!cp ('t175');
5299                      !!!parse-error (type => 'not closed',
5300                                      text => $self->{open_elements}->[-1]->[0]
5301                                          ->manakai_local_name,
5302                                      token => $token);
5303                    } else {
5304                      !!!cp ('t176');
5305                    }
5306                    
5307                    splice @{$self->{open_elements}}, $i;
5308                    
5309                    $clear_up_to_marker->();
5310                    
5311                    $self->{insertion_mode} = IN_TABLE_IM;
5312                    
5313                    !!!next-token;
5314                    next B;
5315                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5316                    !!!cp ('t177');
5317                    !!!parse-error (type => 'unmatched end tag',
5318                                    text => $token->{tag_name}, token => $token);
5319                    ## Ignore the token
5320                    !!!next-token;
5321                    next B;
5322                  } else {
5323                    !!!cp ('t178');
5324                    #
5325                }                }
5326                } elsif ({
5327                          table => 1, tbody => 1, tfoot => 1,
5328                          thead => 1, tr => 1,
5329                         }->{$token->{tag_name}} and
5330                         $self->{insertion_mode} == IN_CELL_IM) {
5331                  ## have an element in table scope
5332                  my $i;
5333                  my $tn;
5334                  INSCOPE: {
5335                    for (reverse 0..$#{$self->{open_elements}}) {
5336                      my $node = $self->{open_elements}->[$_];
5337                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5338                        !!!cp ('t179');
5339                        $i = $_;
5340    
5341                        ## Close the cell
5342                        !!!back-token; # </x>
5343                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
5344                                  line => $token->{line},
5345                                  column => $token->{column}};
5346                        next B;
5347                      } elsif ($node->[1] & TABLE_CELL_EL) {
5348                        !!!cp ('t180');
5349                        $tn = $node->[0]->manakai_local_name;
5350                        ## NOTE: There is exactly one |td| or |th| element
5351                        ## in scope in the stack of open elements by definition.
5352                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5353                        ## ISSUE: Can this be reached?
5354                        !!!cp ('t181');
5355                        last;
5356                      }
5357                    }
5358    
5359                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
5360                    !!!parse-error (type => 'unmatched end tag',
5361                $clear_up_to_marker->();                      text => $token->{tag_name}, token => $token);
5362                    ## Ignore the token
5363                $self->{insertion_mode} = 'in table';                  !!!next-token;
5364                    next B;
5365                !!!next-token;                } # INSCOPE
5366                redo B;              } elsif ($token->{tag_name} eq 'table' and
5367              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5368                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5369                                  token => $token);
5370    
5371                ## As if </caption>                ## As if </caption>
5372                ## have a table element in table scope                ## have a table element in table scope
5373                my $i;                my $i;
5374                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5375                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5376                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5377                      !!!cp ('t184');
5378                    $i = $_;                    $i = $_;
5379                    last INSCOPE;                    last INSCOPE;
5380                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5381                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5382                    last INSCOPE;                    last INSCOPE;
5383                  }                  }
5384                } # INSCOPE                } # INSCOPE
5385                unless (defined $i) {                unless (defined $i) {
5386                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5387                    !!!parse-error (type => 'unmatched end tag',
5388                                    text => 'caption', token => $token);
5389                  ## Ignore the token                  ## Ignore the token
5390                  !!!next-token;                  !!!next-token;
5391                  redo B;                  next B;
5392                }                }
5393                                
5394                ## generate implied end tags                ## generate implied end tags
5395                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5396                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5397                     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;  
5398                }                }
5399    
5400                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5401                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5402                    !!!parse-error (type => 'not closed',
5403                                    text => $self->{open_elements}->[-1]->[0]
5404                                        ->manakai_local_name,
5405                                    token => $token);
5406                  } else {
5407                    !!!cp ('t189');
5408                }                }
5409    
5410                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5411    
5412                $clear_up_to_marker->();                $clear_up_to_marker->();
5413    
5414                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5415    
5416                ## reprocess                ## reprocess
5417                redo B;                next B;
5418              } elsif ({              } elsif ({
5419                        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,  
5420                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5421                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5422                ## Ignore the token                  !!!cp ('t190');
5423                redo B;                  !!!parse-error (type => 'unmatched end tag',
5424              } 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');  
5425                  ## Ignore the token                  ## Ignore the token
5426                  !!!next-token;                  !!!next-token;
5427                  redo B;                  next B;
5428                } else {                } else {
5429                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
5430                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
5431                }                }
5432              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
5433                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
5434                          thead => 1, tr => 1,
5435                         }->{$token->{tag_name}} and
5436                         $self->{insertion_mode} == IN_CAPTION_IM) {
5437                  !!!cp ('t192');
5438                  !!!parse-error (type => 'unmatched end tag',
5439                                  text => $token->{tag_name}, token => $token);
5440                ## Ignore the token                ## Ignore the token
5441                !!!next-token;                !!!next-token;
5442                redo B;                next B;
5443              } else {              } else {
5444                #                !!!cp ('t193');
5445                  #
5446              }              }
5447            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5448              #          for my $entry (@{$self->{open_elements}}) {
5449              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5450                !!!cp ('t75');
5451                !!!parse-error (type => 'in body:#eof', token => $token);
5452                last;
5453            }            }
5454            }
5455    
5456            ## As if </colgroup>          ## Stop parsing.
5457            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
5458              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
5459              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
5460          }
5461    
5462          $insert = $insert_to_current;
5463          #
5464        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5465          if ($token->{type} == CHARACTER_TOKEN) {
5466            if (not $open_tables->[-1]->[1] and # tainted
5467                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5468              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5469                  
5470              unless (length $token->{data}) {
5471                !!!cp ('t194');
5472              !!!next-token;              !!!next-token;
5473              redo B;              next B;
5474            } else {            } else {
5475              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5476            }            }
5477          } 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;  
               }  
             }  
5478    
5479              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5480    
5481              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5482              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5483              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5484              ## result in a new Text node.              ## result in a new Text node.
5485              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5486                
5487              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]}) {  
5488                # MUST                # MUST
5489                my $foster_parent_element;                my $foster_parent_element;
5490                my $next_sibling;                my $next_sibling;
5491                my $prev_sibling;                my $prev_sibling;
5492                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5493                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5494                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5495                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5496                        !!!cp ('t196');
5497                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5498                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5499                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5500                    } else {                    } else {
5501                        !!!cp ('t197');
5502                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5503                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5504                    }                    }
# Line 3816  sub _tree_construction_main ($) { Line 5510  sub _tree_construction_main ($) {
5510                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5511                if (defined $prev_sibling and                if (defined $prev_sibling and
5512                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5513                    !!!cp ('t198');
5514                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5515                } else {                } else {
5516                    !!!cp ('t199');
5517                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5518                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5519                     $next_sibling);                     $next_sibling);
5520                }                }
5521              } else {            $open_tables->[-1]->[1] = 1; # tainted
5522                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5523              !!!cp ('t200');
5524              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5525            }
5526                
5527            !!!next-token;
5528            next B;
5529          } elsif ($token->{type} == START_TAG_TOKEN) {
5530            if ({
5531                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5532                 th => 1, td => 1,
5533                }->{$token->{tag_name}}) {
5534              if ($self->{insertion_mode} == IN_TABLE_IM) {
5535                ## Clear back to table context
5536                while (not ($self->{open_elements}->[-1]->[1]
5537                                & TABLE_SCOPING_EL)) {
5538                  !!!cp ('t201');
5539                  pop @{$self->{open_elements}};
5540              }              }
5541                            
5542              !!!next-token;              !!!insert-element ('tbody',, $token);
5543              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5544            } elsif ($token->{type} eq 'comment') {              ## reprocess in the "in table body" insertion mode...
5545              ## Copied from 'in table'            }
5546              my $comment = $self->{document}->create_comment ($token->{data});            
5547              $self->{open_elements}->[-1]->[0]->append_child ($comment);            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5548              !!!next-token;              unless ($token->{tag_name} eq 'tr') {
5549              redo B;                !!!cp ('t202');
5550            } elsif ($token->{type} eq 'start tag') {                !!!parse-error (type => 'missing start tag:tr', token => $token);
5551              if ({              }
5552                   tr => 1,                  
5553                   th => 1, td => 1,              ## Clear back to table body context
5554                  }->{$token->{tag_name}}) {              while (not ($self->{open_elements}->[-1]->[1]
5555                unless ($token->{tag_name} eq 'tr') {                              & TABLE_ROWS_SCOPING_EL)) {
5556                  !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t203');
5557                  ## ISSUE: Can this case be reached?
5558                  pop @{$self->{open_elements}};
5559                }
5560                    
5561                    $self->{insertion_mode} = IN_ROW_IM;
5562                    if ($token->{tag_name} eq 'tr') {
5563                      !!!cp ('t204');
5564                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5565                      !!!nack ('t204');
5566                      !!!next-token;
5567                      next B;
5568                    } else {
5569                      !!!cp ('t205');
5570                      !!!insert-element ('tr',, $token);
5571                      ## reprocess in the "in row" insertion mode
5572                    }
5573                  } else {
5574                    !!!cp ('t206');
5575                }                }
5576    
5577                ## Clear back to table body context                ## Clear back to table row context
5578                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5579                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5580                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5581                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5582                }                }
5583                                
5584                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5585                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5586                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5587                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5588                } else {                
5589                  !!!insert-element ('tr');                !!!nack ('t207.1');
5590                  ## reprocess                !!!next-token;
5591                }                next B;
               redo B;  
5592              } elsif ({              } elsif ({
5593                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5594                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5595                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5596                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5597                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5598                my $i;                  ## As if </tr>
5599                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5600                  my $node = $self->{open_elements}->[$_];                  my $i;
5601                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5602                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5603                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5604                    $i = $_;                      !!!cp ('t208');
5605                    last INSCOPE;                      $i = $_;
5606                  } elsif ({                      last INSCOPE;
5607                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5608                           }->{$node->[1]}) {                      !!!cp ('t209');
5609                    last INSCOPE;                      last INSCOPE;
5610                      }
5611                    } # INSCOPE
5612                    unless (defined $i) {
5613                      !!!cp ('t210');
5614    ## TODO: This type is wrong.
5615                      !!!parse-error (type => 'unmacthed end tag',
5616                                      text => $token->{tag_name}, token => $token);
5617                      ## Ignore the token
5618                      !!!nack ('t210.1');
5619                      !!!next-token;
5620                      next B;
5621                    }
5622                    
5623                    ## Clear back to table row context
5624                    while (not ($self->{open_elements}->[-1]->[1]
5625                                    & TABLE_ROW_SCOPING_EL)) {
5626                      !!!cp ('t211');
5627                      ## ISSUE: Can this case be reached?
5628                      pop @{$self->{open_elements}};
5629                    }
5630                    
5631                    pop @{$self->{open_elements}}; # tr
5632                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5633                    if ($token->{tag_name} eq 'tr') {
5634                      !!!cp ('t212');
5635                      ## reprocess
5636                      !!!ack-later;
5637                      next B;
5638                    } else {
5639                      !!!cp ('t213');
5640                      ## reprocess in the "in table body" insertion mode...
5641                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5642                }                }
5643    
5644                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5645                while (not {                  ## have an element in table scope
5646                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5647                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5648                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5649                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5650                        !!!cp ('t214');
5651                        $i = $_;
5652                        last INSCOPE;
5653                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5654                        !!!cp ('t215');
5655                        last INSCOPE;
5656                      }
5657                    } # INSCOPE
5658                    unless (defined $i) {
5659                      !!!cp ('t216');
5660    ## TODO: This erorr type is wrong.
5661                      !!!parse-error (type => 'unmatched end tag',
5662                                      text => $token->{tag_name}, token => $token);
5663                      ## Ignore the token
5664                      !!!nack ('t216.1');
5665                      !!!next-token;
5666                      next B;
5667                    }
5668    
5669                    ## Clear back to table body context
5670                    while (not ($self->{open_elements}->[-1]->[1]
5671                                    & TABLE_ROWS_SCOPING_EL)) {
5672                      !!!cp ('t217');
5673                      ## ISSUE: Can this state be reached?
5674                      pop @{$self->{open_elements}};
5675                    }
5676                    
5677                    ## As if <{current node}>
5678                    ## have an element in table scope
5679                    ## true by definition
5680                    
5681                    ## Clear back to table body context
5682                    ## nop by definition
5683                    
5684                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5685                    $self->{insertion_mode} = IN_TABLE_IM;
5686                    ## reprocess in "in table" insertion mode...
5687                  } else {
5688                    !!!cp ('t218');
5689                }                }
5690    
5691                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5692                ## have an element in table scope                  ## Clear back to table context
5693                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5694                                    & TABLE_SCOPING_EL)) {
5695                ## Clear back to table body context                    !!!cp ('t219');
5696                ## nop by definition                    ## ISSUE: Can this state be reached?
5697                      pop @{$self->{open_elements}};
5698                pop @{$self->{open_elements}};                  }
5699                $self->{insertion_mode} = 'in table';                  
5700                ## reprocess                  !!!insert-element ('colgroup',, $token);
5701                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5702                    ## reprocess
5703                    !!!ack-later;
5704                    next B;
5705                  } elsif ({
5706                            caption => 1,
5707                            colgroup => 1,
5708                            tbody => 1, tfoot => 1, thead => 1,
5709                           }->{$token->{tag_name}}) {
5710                    ## Clear back to table context
5711                    while (not ($self->{open_elements}->[-1]->[1]
5712                                    & TABLE_SCOPING_EL)) {
5713                      !!!cp ('t220');
5714                      ## ISSUE: Can this state be reached?
5715                      pop @{$self->{open_elements}};
5716                    }
5717                    
5718                    push @$active_formatting_elements, ['#marker', '']
5719                        if $token->{tag_name} eq 'caption';
5720                    
5721                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5722                    $self->{insertion_mode} = {
5723                                               caption => IN_CAPTION_IM,
5724                                               colgroup => IN_COLUMN_GROUP_IM,
5725                                               tbody => IN_TABLE_BODY_IM,
5726                                               tfoot => IN_TABLE_BODY_IM,
5727                                               thead => IN_TABLE_BODY_IM,
5728                                              }->{$token->{tag_name}};
5729                    !!!next-token;
5730                    !!!nack ('t220.1');
5731                    next B;
5732                  } else {
5733                    die "$0: in table: <>: $token->{tag_name}";
5734                  }
5735              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5736                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5737                !!!parse-error (type => 'not closed:table');                                text => $self->{open_elements}->[-1]->[0]
5738                                      ->manakai_local_name,
5739                                  token => $token);
5740    
5741                ## As if </table>                ## As if </table>
5742                ## have a table element in table scope                ## have a table element in table scope
5743                my $i;                my $i;
5744                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5745                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5746                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5747                      !!!cp ('t221');
5748                    $i = $_;                    $i = $_;
5749                    last INSCOPE;                    last INSCOPE;
5750                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5751                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5752                    last INSCOPE;                    last INSCOPE;
5753                  }                  }
5754                } # INSCOPE                } # INSCOPE
5755                unless (defined $i) {                unless (defined $i) {
5756                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5757    ## TODO: The following is wrong, maybe.
5758                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5759                                    token => $token);
5760                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5761                    !!!nack ('t223.1');
5762                  !!!next-token;                  !!!next-token;
5763                  redo B;                  next B;
5764                }                }
5765                                
5766    ## TODO: Followings are removed from the latest spec.
5767                ## generate implied end tags                ## generate implied end tags
5768                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5769                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5770                     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;  
5771                }                }
5772    
5773                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5774                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5775                    ## NOTE: |<table><tr><table>|
5776                    !!!parse-error (type => 'not closed',
5777                                    text => $self->{open_elements}->[-1]->[0]
5778                                        ->manakai_local_name,
5779                                    token => $token);
5780                  } else {
5781                    !!!cp ('t226');
5782                }                }
5783    
5784                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5785                  pop @{$open_tables};
5786    
5787                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5788    
5789                ## reprocess            ## reprocess
5790                redo B;            !!!ack-later;
5791              } else {            next B;
5792                #          } elsif ($token->{tag_name} eq 'style') {
5793              }            if (not $open_tables->[-1]->[1]) { # tainted
5794            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5795              if ({              ## NOTE: This is a "as if in head" code clone.
5796                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5797                  }->{$token->{tag_name}}) {              next B;
5798                ## have an element in table scope            } else {
5799                my $i;              !!!cp ('t227.7');
5800                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5801                  my $node = $self->{open_elements}->[$_];            }
5802                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5803                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5804                    last INSCOPE;              !!!cp ('t227.6');
5805                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5806                            table => 1, html => 1,              $script_start_tag->();
5807                           }->{$node->[1]}) {              next B;
5808                    last INSCOPE;            } else {
5809                  }              !!!cp ('t227.5');
5810                } # INSCOPE              #
5811                unless (defined $i) {            }
5812                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5813                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5814                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5815                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5816                }                if ($type eq 'hidden') {
5817                    !!!cp ('t227.3');
5818                    !!!parse-error (type => 'in table',
5819                                    text => $token->{tag_name}, token => $token);
5820    
5821                ## 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}};  
               }  
5822    
5823                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;  
               }  
5824    
               ## 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]);  
5825                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
5826    
5827                ## As if <{current node}>                  !!!next-token;
5828                ## have an element in table scope                  !!!ack ('t227.2.1');
5829                ## true by definition                  next B;
5830                  } else {
5831                ## Clear back to table body context                  !!!cp ('t227.2');
5832                ## nop by definition                  #
5833                  }
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
5834              } else {              } else {
5835                  !!!cp ('t227.1');
5836                #                #
5837              }              }
5838            } else {            } else {
5839                !!!cp ('t227.4');
5840              #              #
5841            }            }
5842                      } else {
5843            ## As if in table            !!!cp ('t227');
5844            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5845            $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;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
5846    
5847              ## As if in body, but insert into foster parent element          !!!parse-error (type => 'in table', text => $token->{tag_name},
5848              ## ISSUE: Spec says that "whenever a node would be inserted                          token => $token);
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             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';  
5849    
5850                push @$active_formatting_elements, ['#marker', ''];          $insert = $insert_to_foster;
5851                          #
5852                !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
5853                redo B;              if ($token->{tag_name} eq 'tr' and
5854              } elsif ({                  $self->{insertion_mode} == IN_ROW_IM) {
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
5855                ## have an element in table scope                ## have an element in table scope
5856                my $i;                my $i;
5857                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5858                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5859                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5860                      !!!cp ('t228');
5861                    $i = $_;                    $i = $_;
5862                    last INSCOPE;                    last INSCOPE;
5863                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5864                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5865                    last INSCOPE;                    last INSCOPE;
5866                  }                  }
5867                } # INSCOPE                } # INSCOPE
5868                unless (defined $i) {                unless (defined $i) {
5869                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5870                    !!!parse-error (type => 'unmatched end tag',
5871                                    text => $token->{tag_name}, token => $token);
5872                  ## Ignore the token                  ## Ignore the token
5873                    !!!nack ('t230.1');
5874                  !!!next-token;                  !!!next-token;
5875                  redo B;                  next B;
5876                  } else {
5877                    !!!cp ('t232');
5878                }                }
5879    
5880                ## Clear back to table row context                ## Clear back to table row context
5881                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5882                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5883                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5884                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5885                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5886                }                }
5887    
5888                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5889                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5890                ## reprocess                !!!next-token;
5891                redo B;                !!!nack ('t231.1');
5892                  next B;
5893              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5894                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5895                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5896                    ## have an element in table scope
5897                ## As if </table>                  my $i;
5898                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5899                my $i;                    my $node = $self->{open_elements}->[$_];
5900                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5901                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5902                  if ($node->[1] eq 'table') {                      $i = $_;
5903                    $i = $_;                      last INSCOPE;
5904                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5905                  } elsif ({                      !!!cp ('t234');
5906                            table => 1, html => 1,                      last INSCOPE;
5907                           }->{$node->[1]}) {                    }
5908                    last INSCOPE;                  } # INSCOPE
5909                    unless (defined $i) {
5910                      !!!cp ('t235');
5911    ## TODO: The following is wrong.
5912                      !!!parse-error (type => 'unmatched end tag',
5913                                      text => $token->{type}, token => $token);
5914                      ## Ignore the token
5915                      !!!nack ('t236.1');
5916                      !!!next-token;
5917                      next B;
5918                  }                  }
5919                } # INSCOPE                  
5920                unless (defined $i) {                  ## Clear back to table row context
5921                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5922                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5923                  !!!next-token;                    !!!cp ('t236');
5924                  redo B;  ## ISSUE: Can this state be reached?
5925                }                    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;  
5926                  }                  }
5927                } # INSCOPE                  
5928                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5929                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5930                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5931                  !!!next-token;                }
5932                  redo B;  
5933                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5934                    ## have an element in table scope
5935                ## Clear back to table row context                  my $i;
5936                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5937                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5938                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5939                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5940                        $i = $_;
5941                        last INSCOPE;
5942                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5943                        !!!cp ('t238');
5944                        last INSCOPE;
5945                      }
5946                    } # INSCOPE
5947                    unless (defined $i) {
5948                      !!!cp ('t239');
5949                      !!!parse-error (type => 'unmatched end tag',
5950                                      text => $token->{tag_name}, token => $token);
5951                      ## Ignore the token
5952                      !!!nack ('t239.1');
5953                      !!!next-token;
5954                      next B;
5955                    }
5956                    
5957                    ## Clear back to table body context
5958                    while (not ($self->{open_elements}->[-1]->[1]
5959                                    & TABLE_ROWS_SCOPING_EL)) {
5960                      !!!cp ('t240');
5961                      pop @{$self->{open_elements}};
5962                    }
5963                    
5964                    ## As if <{current node}>
5965                    ## have an element in table scope
5966                    ## true by definition
5967                    
5968                    ## Clear back to table body context
5969                    ## nop by definition
5970                    
5971                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5972                    $self->{insertion_mode} = IN_TABLE_IM;
5973                    ## reprocess in the "in table" insertion mode...
5974                }                }
5975    
5976                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
5977                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
5978                !!!next-token;                ## the code for <table> in the "in table" insertion mode
5979                redo B;                ## is synced with it.
5980              } elsif ($token->{tag_name} eq 'table') {  
5981                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
5982                my $i;                my $i;
5983                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5984                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5985                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
5986                      !!!cp ('t241');
5987                    $i = $_;                    $i = $_;
5988                    last INSCOPE;                    last INSCOPE;
5989                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5990                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5991                    last INSCOPE;                    last INSCOPE;
5992                  }                  }
5993                } # INSCOPE                } # INSCOPE
5994                unless (defined $i) {                unless (defined $i) {
5995                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
5996                    !!!parse-error (type => 'unmatched end tag',
5997                                    text => $token->{tag_name}, token => $token);
5998                  ## Ignore the token                  ## Ignore the token
5999                    !!!nack ('t243.1');
6000                  !!!next-token;                  !!!next-token;
6001                  redo B;                  next B;
6002                }                }
6003                    
6004                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
6005                while (not {                pop @{$open_tables};
6006                  tr => 1, html => 1,                
6007                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
6008                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
6009                  pop @{$self->{open_elements}};                !!!next-token;
6010                }                next B;
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
6011              } elsif ({              } elsif ({
6012                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6013                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
6014                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
6015                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
6016                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
6017                  my $node = $self->{open_elements}->[$_];                  my $i;
6018                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6019                    $i = $_;                    my $node = $self->{open_elements}->[$_];
6020                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6021                  } elsif ({                      !!!cp ('t247');
6022                            table => 1, html => 1,                      $i = $_;
6023                           }->{$node->[1]}) {                      last INSCOPE;
6024                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6025                        !!!cp ('t248');
6026                        last INSCOPE;
6027                      }
6028                    } # INSCOPE
6029                      unless (defined $i) {
6030                        !!!cp ('t249');
6031                        !!!parse-error (type => 'unmatched end tag',
6032                                        text => $token->{tag_name}, token => $token);
6033                        ## Ignore the token
6034                        !!!nack ('t249.1');
6035                        !!!next-token;
6036                        next B;
6037                      }
6038                    
6039                    ## As if </tr>
6040                    ## have an element in table scope
6041                    my $i;
6042                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6043                      my $node = $self->{open_elements}->[$_];
6044                      if ($node->[1] & TABLE_ROW_EL) {
6045                        !!!cp ('t250');
6046                        $i = $_;
6047                        last INSCOPE;
6048                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
6049                        !!!cp ('t251');
6050                        last INSCOPE;
6051                      }
6052                    } # INSCOPE
6053                      unless (defined $i) {
6054                        !!!cp ('t252');
6055                        !!!parse-error (type => 'unmatched end tag',
6056                                        text => 'tr', token => $token);
6057                        ## Ignore the token
6058                        !!!nack ('t252.1');
6059                        !!!next-token;
6060                        next B;
6061                      }
6062                    
6063                    ## Clear back to table row context
6064                    while (not ($self->{open_elements}->[-1]->[1]
6065                                    & TABLE_ROW_SCOPING_EL)) {
6066                      !!!cp ('t253');
6067    ## ISSUE: Can this case be reached?
6068                      pop @{$self->{open_elements}};
6069                  }                  }
6070                } # INSCOPE                  
6071                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
6072                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
6073                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
6074                }                }
6075    
               ## As if </tr>  
6076                ## have an element in table scope                ## have an element in table scope
6077                my $i;                my $i;
6078                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6079                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6080                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6081                      !!!cp ('t254');
6082                    $i = $_;                    $i = $_;
6083                    last INSCOPE;                    last INSCOPE;
6084                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6085                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
6086                    last INSCOPE;                    last INSCOPE;
6087                  }                  }
6088                } # INSCOPE                } # INSCOPE
6089                unless (defined $i) {                unless (defined $i) {
6090                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
6091                    !!!parse-error (type => 'unmatched end tag',
6092                                    text => $token->{tag_name}, token => $token);
6093                  ## Ignore the token                  ## Ignore the token
6094                    !!!nack ('t256.1');
6095                  !!!next-token;                  !!!next-token;
6096                  redo B;                  next B;
6097                }                }
6098    
6099                ## Clear back to table row context                ## Clear back to table body context
6100                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6101                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
6102                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
6103                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6104                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6105                }                }
6106    
6107                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
6108                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
6109                ## reprocess                !!!nack ('t257.1');
6110                redo B;                !!!next-token;
6111                  next B;
6112              } elsif ({              } elsif ({
6113                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6114                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6115                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6116                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6117                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6118                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
6119                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
6120                !!!next-token;                            text => $token->{tag_name}, token => $token);
6121                redo B;            ## Ignore the token
6122              } else {            !!!nack ('t258.1');
6123                #             !!!next-token;
6124              }            next B;
6125            } else {          } else {
6126              #            !!!cp ('t259');
6127            }            !!!parse-error (type => 'in table:/',
6128                              text => $token->{tag_name}, token => $token);
6129    
6130            ## As if in table            $insert = $insert_to_foster;
6131            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
6132            $in_body->($insert_to_foster);          }
6133            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6134          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6135            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
6136              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
6137              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
6138                          #
6139              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6140              !!!cp ('t259.2');
6141              #
6142            }
6143    
6144              !!!next-token;          ## Stop parsing
6145              redo B;          last B;
6146            } elsif ($token->{type} eq 'comment') {        } else {
6147              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
6148              my $comment = $self->{document}->create_comment ($token->{data});        }
6149              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6150              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
6151              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6152            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6153              if ({                unless (length $token->{data}) {
6154                   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  
6155                  !!!next-token;                  !!!next-token;
6156                  redo B;                  next B;
6157                }                }
6158                }
6159                ## Close the cell              
6160                !!!back-token; # <?>              !!!cp ('t261');
6161                $token = {type => 'end tag', tag_name => $tn};              #
6162                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
6163              } else {              if ($token->{tag_name} eq 'col') {
6164                  !!!cp ('t262');
6165                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6166                  pop @{$self->{open_elements}};
6167                  !!!ack ('t262.1');
6168                  !!!next-token;
6169                  next B;
6170                } else {
6171                  !!!cp ('t263');
6172                #                #
6173              }              }
6174            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
6175              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
6176                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6177                my $i;                  !!!cp ('t264');
6178                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag',
6179                  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});  
6180                  ## Ignore the token                  ## Ignore the token
6181                  !!!next-token;                  !!!next-token;
6182                  redo B;                  next B;
6183                }                } else {
6184                                  !!!cp ('t265');
6185                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
6186                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
6187                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
6188                     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]);  
6189                }                }
6190                } elsif ($token->{tag_name} eq 'col') {
6191                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
6192                  !!!parse-error (type => 'unmatched end tag',
6193                $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});  
6194                ## Ignore the token                ## Ignore the token
6195                !!!next-token;                !!!next-token;
6196                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;  
6197              } else {              } else {
6198                #                !!!cp ('t267');
6199                  #
6200              }              }
6201          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6202            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6203                @{$self->{open_elements}} == 1) { # redundant, maybe
6204              !!!cp ('t270.2');
6205              ## Stop parsing.
6206              last B;
6207            } else {
6208              ## NOTE: As if </colgroup>.
6209              !!!cp ('t270.1');
6210              pop @{$self->{open_elements}}; # colgroup
6211              $self->{insertion_mode} = IN_TABLE_IM;
6212              ## Reprocess.
6213              next B;
6214            }
6215          } else {
6216            die "$0: $token->{type}: Unknown token type";
6217          }
6218    
6219              ## As if </colgroup>
6220              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6221                !!!cp ('t269');
6222    ## TODO: Wrong error type?
6223                !!!parse-error (type => 'unmatched end tag',
6224                                text => 'colgroup', token => $token);
6225                ## Ignore the token
6226                !!!nack ('t269.1');
6227                !!!next-token;
6228                next B;
6229            } else {            } else {
6230              #              !!!cp ('t270');
6231                pop @{$self->{open_elements}}; # colgroup
6232                $self->{insertion_mode} = IN_TABLE_IM;
6233                !!!ack-later;
6234                ## reprocess
6235                next B;
6236              }
6237        } elsif ($self->{insertion_mode} & SELECT_IMS) {
6238          if ($token->{type} == CHARACTER_TOKEN) {
6239            !!!cp ('t271');
6240            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6241            !!!next-token;
6242            next B;
6243          } elsif ($token->{type} == START_TAG_TOKEN) {
6244            if ($token->{tag_name} eq 'option') {
6245              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6246                !!!cp ('t272');
6247                ## As if </option>
6248                pop @{$self->{open_elements}};
6249              } else {
6250                !!!cp ('t273');
6251            }            }
             
           $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}};  
               }  
6252    
6253                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6254                !!!next-token;            !!!nack ('t273.1');
6255                redo B;            !!!next-token;
6256              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6257                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6258                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6259                  pop @{$self->{open_elements}};              !!!cp ('t274');
6260                }              ## As if </option>
6261                pop @{$self->{open_elements}};
6262              } else {
6263                !!!cp ('t275');
6264              }
6265    
6266                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6267                  ## As if </optgroup>              !!!cp ('t276');
6268                  pop @{$self->{open_elements}};              ## As if </optgroup>
6269                }              pop @{$self->{open_elements}};
6270              } else {
6271                !!!cp ('t277');
6272              }
6273    
6274                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6275                !!!next-token;            !!!nack ('t277.1');
6276                redo B;            !!!next-token;
6277              } elsif ($token->{tag_name} eq 'select') {            next B;
6278                !!!parse-error (type => 'not closed:select');          } elsif ({
6279                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6280                ## have an element in table scope                   }->{$token->{tag_name}} or
6281                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6282                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6283                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6284                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6285                    $i = $_;                     tr => 1, td => 1, th => 1,
6286                    last INSCOPE;                    }->{$token->{tag_name}})) {
6287                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6288                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6289                           }->{$node->[1]}) {                            token => $token);
6290                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6291                  }            ## as if there were </select> (otherwise).
6292                } # INSCOPE            ## have an element in table scope
6293                unless (defined $i) {            my $i;
6294                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6295                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6296                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6297                  redo B;                !!!cp ('t278');
6298                }                $i = $_;
6299                  last INSCOPE;
6300                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6301                  !!!cp ('t279');
6302                  last INSCOPE;
6303                }
6304              } # INSCOPE
6305              unless (defined $i) {
6306                !!!cp ('t280');
6307                !!!parse-error (type => 'unmatched end tag',
6308                                text => 'select', token => $token);
6309                ## Ignore the token
6310                !!!nack ('t280.1');
6311                !!!next-token;
6312                next B;
6313              }
6314                                
6315                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6316              splice @{$self->{open_elements}}, $i;
6317    
6318                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6319    
6320                !!!next-token;            if ($token->{tag_name} eq 'select') {
6321                redo B;              !!!nack ('t281.2');
6322              } else {              !!!next-token;
6323                #              next B;
6324              } else {
6325                !!!cp ('t281.1');
6326                !!!ack-later;
6327                ## Reprocess the token.
6328                next B;
6329              }
6330            } else {
6331              !!!cp ('t282');
6332              !!!parse-error (type => 'in select',
6333                              text => $token->{tag_name}, token => $token);
6334              ## Ignore the token
6335              !!!nack ('t282.1');
6336              !!!next-token;
6337              next B;
6338            }
6339          } elsif ($token->{type} == END_TAG_TOKEN) {
6340            if ($token->{tag_name} eq 'optgroup') {
6341              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6342                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6343                !!!cp ('t283');
6344                ## As if </option>
6345                splice @{$self->{open_elements}}, -2;
6346              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6347                !!!cp ('t284');
6348                pop @{$self->{open_elements}};
6349              } else {
6350                !!!cp ('t285');
6351                !!!parse-error (type => 'unmatched end tag',
6352                                text => $token->{tag_name}, token => $token);
6353                ## Ignore the token
6354              }
6355              !!!nack ('t285.1');
6356              !!!next-token;
6357              next B;
6358            } elsif ($token->{tag_name} eq 'option') {
6359              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6360                !!!cp ('t286');
6361                pop @{$self->{open_elements}};
6362              } else {
6363                !!!cp ('t287');
6364                !!!parse-error (type => 'unmatched end tag',
6365                                text => $token->{tag_name}, token => $token);
6366                ## Ignore the token
6367              }
6368              !!!nack ('t287.1');
6369              !!!next-token;
6370              next B;
6371            } elsif ($token->{tag_name} eq 'select') {
6372              ## have an element in table scope
6373              my $i;
6374              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6375                my $node = $self->{open_elements}->[$_];
6376                if ($node->[1] & SELECT_EL) {
6377                  !!!cp ('t288');
6378                  $i = $_;
6379                  last INSCOPE;
6380                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6381                  !!!cp ('t289');
6382                  last INSCOPE;
6383              }              }
6384            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6385              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6386                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6387                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6388                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6389                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6390                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6391                  pop @{$self->{open_elements}};              !!!next-token;
6392                } else {              next B;
6393                  !!!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;  
               }  
6394                                
6395                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6396              splice @{$self->{open_elements}}, $i;
6397    
6398                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6399    
6400                !!!next-token;            !!!nack ('t291.1');
6401                redo B;            !!!next-token;
6402              } elsif ({            next B;
6403                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6404                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6405                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6406                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6407                                   }->{$token->{tag_name}}) {
6408                ## have an element in table scope  ## TODO: The following is wrong?
6409                my $i;            !!!parse-error (type => 'unmatched end tag',
6410                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;  
               }  
6411                                
6412                ## As if </select>            ## have an element in table scope
6413                ## have an element in table scope            my $i;
6414                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6415                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6416                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6417                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6418                    $i = $_;                $i = $_;
6419                    last INSCOPE;                last INSCOPE;
6420                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6421                            table => 1, html => 1,                !!!cp ('t293');
6422                           }->{$node->[1]}) {                last INSCOPE;
6423                    last INSCOPE;              }
6424                  }            } # INSCOPE
6425                } # INSCOPE            unless (defined $i) {
6426                unless (defined $i) {              !!!cp ('t294');
6427                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6428                  ## Ignore the </select> token              !!!nack ('t294.1');
6429                  !!!next-token; ## TODO: ok?              !!!next-token;
6430                  redo B;              next B;
6431                }            }
6432                                
6433                splice @{$self->{open_elements}}, $i;            ## As if </select>
6434              ## have an element in table scope
6435                $self->_reset_insertion_mode;            undef $i;
6436              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6437                ## reprocess              my $node = $self->{open_elements}->[$_];
6438                redo B;              if ($node->[1] & SELECT_EL) {
6439              } else {                !!!cp ('t295');
6440                #                $i = $_;
6441                  last INSCOPE;
6442                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6443    ## ISSUE: Can this state be reached?
6444                  !!!cp ('t296');
6445                  last INSCOPE;
6446              }              }
6447            } else {            } # INSCOPE
6448              #            unless (defined $i) {
6449                !!!cp ('t297');
6450    ## TODO: The following error type is correct?
6451                !!!parse-error (type => 'unmatched end tag',
6452                                text => 'select', token => $token);
6453                ## Ignore the </select> token
6454                !!!nack ('t297.1');
6455                !!!next-token; ## TODO: ok?
6456                next B;
6457            }            }
6458                  
6459              !!!cp ('t298');
6460              splice @{$self->{open_elements}}, $i;
6461    
6462            !!!parse-error (type => 'in select:'.$token->{tag_name});            $self->_reset_insertion_mode;
6463    
6464              !!!ack-later;
6465              ## reprocess
6466              next B;
6467            } else {
6468              !!!cp ('t299');
6469              !!!parse-error (type => 'in select:/',
6470                              text => $token->{tag_name}, token => $token);
6471            ## Ignore the token            ## Ignore the token
6472              !!!nack ('t299.3');
6473            !!!next-token;            !!!next-token;
6474            redo B;            next B;
6475          } elsif ($self->{insertion_mode} eq 'after body') {          }
6476            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6477              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6478                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
6479                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
6480                            !!!parse-error (type => 'in body:#eof', token => $token);
6481                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6482              !!!cp ('t299.2');
6483            }
6484    
6485                unless (length $token->{data}) {          ## Stop parsing.
6486                  !!!next-token;          last B;
6487                  redo B;        } else {
6488                }          die "$0: $token->{type}: Unknown token type";
6489              }        }
6490                    } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6491              #        if ($token->{type} == CHARACTER_TOKEN) {
6492              !!!parse-error (type => 'after body:#'.$token->{type});          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6493            } elsif ($token->{type} eq 'comment') {            my $data = $1;
6494              my $comment = $self->{document}->create_comment ($token->{data});            ## As if in body
6495              $self->{open_elements}->[0]->[0]->append_child ($comment);            $reconstruct_active_formatting_elements->($insert_to_current);
6496                  
6497              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6498              
6499              unless (length $token->{data}) {
6500                !!!cp ('t300');
6501              !!!next-token;              !!!next-token;
6502              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});  
6503            }            }
6504            }
6505            
6506            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6507              !!!cp ('t301');
6508              !!!parse-error (type => 'after html:#text', token => $token);
6509    
6510            $self->{insertion_mode} = 'in body';            ## Reprocess in the "after body" insertion mode.
6511            ## reprocess          } else {
6512            redo B;            !!!cp ('t302');
6513          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6514            if ($token->{type} eq 'character') {          
6515              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## "after body" insertion mode
6516                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          !!!parse-error (type => 'after body:#text', token => $token);
6517    
6518                unless (length $token->{data}) {          $self->{insertion_mode} = IN_BODY_IM;
6519                  !!!next-token;          ## reprocess
6520                  redo B;          next B;
6521                }        } elsif ($token->{type} == START_TAG_TOKEN) {
6522              }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6523              !!!cp ('t303');
6524              !!!parse-error (type => 'after html',
6525                              text => $token->{tag_name}, token => $token);
6526              
6527              ## Reprocess in the "after body" insertion mode.
6528            } else {
6529              !!!cp ('t304');
6530            }
6531    
6532              #          ## "after body" insertion mode
6533            } elsif ($token->{type} eq 'comment') {          !!!parse-error (type => 'after body',
6534              my $comment = $self->{document}->create_comment ($token->{data});                          text => $token->{tag_name}, token => $token);
6535              $self->{open_elements}->[-1]->[0]->append_child ($comment);  
6536            $self->{insertion_mode} = IN_BODY_IM;
6537            !!!ack-later;
6538            ## reprocess
6539            next B;
6540          } elsif ($token->{type} == END_TAG_TOKEN) {
6541            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6542              !!!cp ('t305');
6543              !!!parse-error (type => 'after html:/',
6544                              text => $token->{tag_name}, token => $token);
6545              
6546              $self->{insertion_mode} = AFTER_BODY_IM;
6547              ## Reprocess in the "after body" insertion mode.
6548            } else {
6549              !!!cp ('t306');
6550            }
6551    
6552            ## "after body" insertion mode
6553            if ($token->{tag_name} eq 'html') {
6554              if (defined $self->{inner_html_node}) {
6555                !!!cp ('t307');
6556                !!!parse-error (type => 'unmatched end tag',
6557                                text => 'html', token => $token);
6558                ## Ignore the token
6559              !!!next-token;              !!!next-token;
6560              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 {  
               #  
             }  
6561            } else {            } else {
6562              #              !!!cp ('t308');
6563                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6564                !!!next-token;
6565                next B;
6566              }
6567            } else {
6568              !!!cp ('t309');
6569              !!!parse-error (type => 'after body:/',
6570                              text => $token->{tag_name}, token => $token);
6571    
6572              $self->{insertion_mode} = IN_BODY_IM;
6573              ## reprocess
6574              next B;
6575            }
6576          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6577            !!!cp ('t309.2');
6578            ## Stop parsing
6579            last B;
6580          } else {
6581            die "$0: $token->{type}: Unknown token type";
6582          }
6583        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6584          if ($token->{type} == CHARACTER_TOKEN) {
6585            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6586              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6587              
6588              unless (length $token->{data}) {
6589                !!!cp ('t310');
6590                !!!next-token;
6591                next B;
6592              }
6593            }
6594            
6595            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6596              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6597                !!!cp ('t311');
6598                !!!parse-error (type => 'in frameset:#text', token => $token);
6599              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6600                !!!cp ('t312');
6601                !!!parse-error (type => 'after frameset:#text', token => $token);
6602              } else { # "after after frameset"
6603                !!!cp ('t313');
6604                !!!parse-error (type => 'after html:#text', token => $token);
6605            }            }
6606                        
6607            if (defined $token->{tag_name}) {            ## Ignore the token.
6608              !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if (length $token->{data}) {
6609                !!!cp ('t314');
6610                ## reprocess the rest of characters
6611              } else {
6612                !!!cp ('t315');
6613                !!!next-token;
6614              }
6615              next B;
6616            }
6617            
6618            die qq[$0: Character "$token->{data}"];
6619          } elsif ($token->{type} == START_TAG_TOKEN) {
6620            if ($token->{tag_name} eq 'frameset' and
6621                $self->{insertion_mode} == IN_FRAMESET_IM) {
6622              !!!cp ('t318');
6623              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6624              !!!nack ('t318.1');
6625              !!!next-token;
6626              next B;
6627            } elsif ($token->{tag_name} eq 'frame' and
6628                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6629              !!!cp ('t319');
6630              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6631              pop @{$self->{open_elements}};
6632              !!!ack ('t319.1');
6633              !!!next-token;
6634              next B;
6635            } elsif ($token->{tag_name} eq 'noframes') {
6636              !!!cp ('t320');
6637              ## NOTE: As if in head.
6638              $parse_rcdata->(CDATA_CONTENT_MODEL);
6639              next B;
6640    
6641              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6642              ## has no parse error.
6643            } else {
6644              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6645                !!!cp ('t321');
6646                !!!parse-error (type => 'in frameset',
6647                                text => $token->{tag_name}, token => $token);
6648              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6649                !!!cp ('t322');
6650                !!!parse-error (type => 'after frameset',
6651                                text => $token->{tag_name}, token => $token);
6652              } else { # "after after frameset"
6653                !!!cp ('t322.2');
6654                !!!parse-error (type => 'after after frameset',
6655                                text => $token->{tag_name}, token => $token);
6656              }
6657              ## Ignore the token
6658              !!!nack ('t322.1');
6659              !!!next-token;
6660              next B;
6661            }
6662          } elsif ($token->{type} == END_TAG_TOKEN) {
6663            if ($token->{tag_name} eq 'frameset' and
6664                $self->{insertion_mode} == IN_FRAMESET_IM) {
6665              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6666                  @{$self->{open_elements}} == 1) {
6667                !!!cp ('t325');
6668                !!!parse-error (type => 'unmatched end tag',
6669                                text => $token->{tag_name}, token => $token);
6670                ## Ignore the token
6671                !!!next-token;
6672              } else {
6673                !!!cp ('t326');
6674                pop @{$self->{open_elements}};
6675                !!!next-token;
6676              }
6677    
6678              if (not defined $self->{inner_html_node} and
6679                  not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6680                !!!cp ('t327');
6681                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6682            } else {            } else {
6683              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t328');
6684              }
6685              next B;
6686            } elsif ($token->{tag_name} eq 'html' and
6687                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6688              !!!cp ('t329');
6689              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6690              !!!next-token;
6691              next B;
6692            } else {
6693              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6694                !!!cp ('t330');
6695                !!!parse-error (type => 'in frameset:/',
6696                                text => $token->{tag_name}, token => $token);
6697              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6698                !!!cp ('t330.1');
6699                !!!parse-error (type => 'after frameset:/',
6700                                text => $token->{tag_name}, token => $token);
6701              } else { # "after after html"
6702                !!!cp ('t331');
6703                !!!parse-error (type => 'after after frameset:/',
6704                                text => $token->{tag_name}, token => $token);
6705            }            }
6706            ## Ignore the token            ## Ignore the token
6707            !!!next-token;            !!!next-token;
6708            redo B;            next B;
6709          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6710            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6711              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6712                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                  @{$self->{open_elements}} == 1) { # redundant, maybe
6713              !!!cp ('t331.1');
6714              !!!parse-error (type => 'in body:#eof', token => $token);
6715            } else {
6716              !!!cp ('t331.2');
6717            }
6718            
6719            ## Stop parsing
6720            last B;
6721          } else {
6722            die "$0: $token->{type}: Unknown token type";
6723          }
6724    
6725                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
6726                  !!!next-token;      } else {
6727                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
6728                }      }
6729    
6730        ## "in body" insertion mode
6731        if ($token->{type} == START_TAG_TOKEN) {
6732          if ($token->{tag_name} eq 'script') {
6733            !!!cp ('t332');
6734            ## NOTE: This is an "as if in head" code clone
6735            $script_start_tag->();
6736            next B;
6737          } elsif ($token->{tag_name} eq 'style') {
6738            !!!cp ('t333');
6739            ## NOTE: This is an "as if in head" code clone
6740            $parse_rcdata->(CDATA_CONTENT_MODEL);
6741            next B;
6742          } elsif ({
6743                    base => 1, link => 1,
6744                   }->{$token->{tag_name}}) {
6745            !!!cp ('t334');
6746            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6747            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6748            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6749            !!!ack ('t334.1');
6750            !!!next-token;
6751            next B;
6752          } elsif ($token->{tag_name} eq 'meta') {
6753            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6754            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6755            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6756    
6757            unless ($self->{confident}) {
6758              if ($token->{attributes}->{charset}) {
6759                !!!cp ('t335');
6760                ## NOTE: Whether the encoding is supported or not is handled
6761                ## in the {change_encoding} callback.
6762                $self->{change_encoding}
6763                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6764                
6765                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6766                    ->set_user_data (manakai_has_reference =>
6767                                         $token->{attributes}->{charset}
6768                                             ->{has_reference});
6769              } elsif ($token->{attributes}->{content}) {
6770                if ($token->{attributes}->{content}->{value}
6771                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6772                        [\x09-\x0D\x20]*=
6773                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6774                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6775                  !!!cp ('t336');
6776                  ## NOTE: Whether the encoding is supported or not is handled
6777                  ## in the {change_encoding} callback.
6778                  $self->{change_encoding}
6779                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6780                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6781                      ->set_user_data (manakai_has_reference =>
6782                                           $token->{attributes}->{content}
6783                                                 ->{has_reference});
6784              }              }
6785              }
6786            } else {
6787              if ($token->{attributes}->{charset}) {
6788                !!!cp ('t337');
6789                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6790                    ->set_user_data (manakai_has_reference =>
6791                                         $token->{attributes}->{charset}
6792                                             ->{has_reference});
6793              }
6794              if ($token->{attributes}->{content}) {
6795                !!!cp ('t338');
6796                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6797                    ->set_user_data (manakai_has_reference =>
6798                                         $token->{attributes}->{content}
6799                                             ->{has_reference});
6800              }
6801            }
6802    
6803              #          !!!ack ('t338.1');
6804            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6805              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6806              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6807              !!!next-token;          !!!cp ('t341');
6808              redo B;          ## NOTE: This is an "as if in head" code clone
6809            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6810              if ($token->{tag_name} eq 'noframes') {          next B;
6811                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6812                redo B;          !!!parse-error (type => 'in body', text => 'body', token => $token);
6813              } else {                
6814                #          if (@{$self->{open_elements}} == 1 or
6815                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6816              !!!cp ('t342');
6817              ## Ignore the token
6818            } else {
6819              my $body_el = $self->{open_elements}->[1]->[0];
6820              for my $attr_name (keys %{$token->{attributes}}) {
6821                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6822                  !!!cp ('t343');
6823                  $body_el->set_attribute_ns
6824                    (undef, [undef, $attr_name],
6825                     $token->{attributes}->{$attr_name}->{value});
6826              }              }
6827            } elsif ($token->{type} eq 'end tag') {            }
6828              if ($token->{tag_name} eq 'html') {          }
6829                $phase = 'trailing end';          !!!nack ('t343.1');
6830            !!!next-token;
6831            next B;
6832          } elsif ({
6833                    address => 1, blockquote => 1, center => 1, dir => 1,
6834                    div => 1, dl => 1, fieldset => 1,
6835                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6836                    menu => 1, ol => 1, p => 1, ul => 1,
6837                    pre => 1, listing => 1,
6838                    form => 1,
6839                    table => 1,
6840                    hr => 1,
6841                   }->{$token->{tag_name}}) {
6842            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6843              !!!cp ('t350');
6844              !!!parse-error (type => 'in form:form', token => $token);
6845              ## Ignore the token
6846              !!!nack ('t350.1');
6847              !!!next-token;
6848              next B;
6849            }
6850    
6851            ## has a p element in scope
6852            INSCOPE: for (reverse @{$self->{open_elements}}) {
6853              if ($_->[1] & P_EL) {
6854                !!!cp ('t344');
6855                !!!back-token; # <form>
6856                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6857                          line => $token->{line}, column => $token->{column}};
6858                next B;
6859              } elsif ($_->[1] & SCOPING_EL) {
6860                !!!cp ('t345');
6861                last INSCOPE;
6862              }
6863            } # INSCOPE
6864              
6865            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6866            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6867              !!!nack ('t346.1');
6868              !!!next-token;
6869              if ($token->{type} == CHARACTER_TOKEN) {
6870                $token->{data} =~ s/^\x0A//;
6871                unless (length $token->{data}) {
6872                  !!!cp ('t346');
6873                !!!next-token;                !!!next-token;
               redo B;  
6874              } else {              } else {
6875                #                !!!cp ('t349');
6876              }              }
6877            } else {            } else {
6878              #              !!!cp ('t348');
6879            }            }
6880            } elsif ($token->{tag_name} eq 'form') {
6881              !!!cp ('t347.1');
6882              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6883    
6884              !!!nack ('t347.2');
6885              !!!next-token;
6886            } elsif ($token->{tag_name} eq 'table') {
6887              !!!cp ('t382');
6888              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6889                        
6890            if (defined $token->{tag_name}) {            $self->{insertion_mode} = IN_TABLE_IM;
6891              !!!parse-error (type => 'after frameset:'.$token->{tag_name});  
6892              !!!nack ('t382.1');
6893              !!!next-token;
6894            } elsif ($token->{tag_name} eq 'hr') {
6895              !!!cp ('t386');
6896              pop @{$self->{open_elements}};
6897            
6898              !!!nack ('t386.1');
6899              !!!next-token;
6900            } else {
6901              !!!nack ('t347.1');
6902              !!!next-token;
6903            }
6904            next B;
6905          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6906            ## has a p element in scope
6907            INSCOPE: for (reverse @{$self->{open_elements}}) {
6908              if ($_->[1] & P_EL) {
6909                !!!cp ('t353');
6910                !!!back-token; # <x>
6911                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6912                          line => $token->{line}, column => $token->{column}};
6913                next B;
6914              } elsif ($_->[1] & SCOPING_EL) {
6915                !!!cp ('t354');
6916                last INSCOPE;
6917              }
6918            } # INSCOPE
6919              
6920            ## Step 1
6921            my $i = -1;
6922            my $node = $self->{open_elements}->[$i];
6923            my $li_or_dtdd = {li => {li => 1},
6924                              dt => {dt => 1, dd => 1},
6925                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6926            LI: {
6927              ## Step 2
6928              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6929                if ($i != -1) {
6930                  !!!cp ('t355');
6931                  !!!parse-error (type => 'not closed',
6932                                  text => $self->{open_elements}->[-1]->[0]
6933                                      ->manakai_local_name,
6934                                  token => $token);
6935                } else {
6936                  !!!cp ('t356');
6937                }
6938                splice @{$self->{open_elements}}, $i;
6939                last LI;
6940            } else {            } else {
6941              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
6942              }
6943              
6944              ## Step 3
6945              if (not ($node->[1] & FORMATTING_EL) and
6946                  #not $phrasing_category->{$node->[1]} and
6947                  ($node->[1] & SPECIAL_EL or
6948                   $node->[1] & SCOPING_EL) and
6949                  not ($node->[1] & ADDRESS_EL) and
6950                  not ($node->[1] & DIV_EL)) {
6951                !!!cp ('t358');
6952                last LI;
6953              }
6954              
6955              !!!cp ('t359');
6956              ## Step 4
6957              $i--;
6958              $node = $self->{open_elements}->[$i];
6959              redo LI;
6960            } # LI
6961              
6962            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6963            !!!nack ('t359.1');
6964            !!!next-token;
6965            next B;
6966          } elsif ($token->{tag_name} eq 'plaintext') {
6967            ## has a p element in scope
6968            INSCOPE: for (reverse @{$self->{open_elements}}) {
6969              if ($_->[1] & P_EL) {
6970                !!!cp ('t367');
6971                !!!back-token; # <plaintext>
6972                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6973                          line => $token->{line}, column => $token->{column}};
6974                next B;
6975              } elsif ($_->[1] & SCOPING_EL) {
6976                !!!cp ('t368');
6977                last INSCOPE;
6978            }            }
6979            } # INSCOPE
6980              
6981            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6982              
6983            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6984              
6985            !!!nack ('t368.1');
6986            !!!next-token;
6987            next B;
6988          } elsif ($token->{tag_name} eq 'a') {
6989            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6990              my $node = $active_formatting_elements->[$i];
6991              if ($node->[1] & A_EL) {
6992                !!!cp ('t371');
6993                !!!parse-error (type => 'in a:a', token => $token);
6994                
6995                !!!back-token; # <a>
6996                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6997                          line => $token->{line}, column => $token->{column}};
6998                $formatting_end_tag->($token);
6999                
7000                AFE2: for (reverse 0..$#$active_formatting_elements) {
7001                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
7002                    !!!cp ('t372');
7003                    splice @$active_formatting_elements, $_, 1;
7004                    last AFE2;
7005                  }
7006                } # AFE2
7007                OE: for (reverse 0..$#{$self->{open_elements}}) {
7008                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
7009                    !!!cp ('t373');
7010                    splice @{$self->{open_elements}}, $_, 1;
7011                    last OE;
7012                  }
7013                } # OE
7014                last AFE;
7015              } elsif ($node->[0] eq '#marker') {
7016                !!!cp ('t374');
7017                last AFE;
7018              }
7019            } # AFE
7020              
7021            $reconstruct_active_formatting_elements->($insert_to_current);
7022    
7023            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7024            push @$active_formatting_elements, $self->{open_elements}->[-1];
7025    
7026            !!!nack ('t374.1');
7027            !!!next-token;
7028            next B;
7029          } elsif ($token->{tag_name} eq 'nobr') {
7030            $reconstruct_active_formatting_elements->($insert_to_current);
7031    
7032            ## has a |nobr| element in scope
7033            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7034              my $node = $self->{open_elements}->[$_];
7035              if ($node->[1] & NOBR_EL) {
7036                !!!cp ('t376');
7037                !!!parse-error (type => 'in nobr:nobr', token => $token);
7038                !!!back-token; # <nobr>
7039                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7040                          line => $token->{line}, column => $token->{column}};
7041                next B;
7042              } elsif ($node->[1] & SCOPING_EL) {
7043                !!!cp ('t377');
7044                last INSCOPE;
7045              }
7046            } # INSCOPE
7047            
7048            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7049            push @$active_formatting_elements, $self->{open_elements}->[-1];
7050            
7051            !!!nack ('t377.1');
7052            !!!next-token;
7053            next B;
7054          } elsif ($token->{tag_name} eq 'button') {
7055            ## has a button element in scope
7056            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7057              my $node = $self->{open_elements}->[$_];
7058              if ($node->[1] & BUTTON_EL) {
7059                !!!cp ('t378');
7060                !!!parse-error (type => 'in button:button', token => $token);
7061                !!!back-token; # <button>
7062                $token = {type => END_TAG_TOKEN, tag_name => 'button',
7063                          line => $token->{line}, column => $token->{column}};
7064                next B;
7065              } elsif ($node->[1] & SCOPING_EL) {
7066                !!!cp ('t379');
7067                last INSCOPE;
7068              }
7069            } # INSCOPE
7070              
7071            $reconstruct_active_formatting_elements->($insert_to_current);
7072              
7073            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7074    
7075            ## TODO: associate with $self->{form_element} if defined
7076    
7077            push @$active_formatting_elements, ['#marker', ''];
7078    
7079            !!!nack ('t379.1');
7080            !!!next-token;
7081            next B;
7082          } elsif ({
7083                    xmp => 1,
7084                    iframe => 1,
7085                    noembed => 1,
7086                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7087                    noscript => 0, ## TODO: 1 if scripting is enabled
7088                   }->{$token->{tag_name}}) {
7089            if ($token->{tag_name} eq 'xmp') {
7090              !!!cp ('t381');
7091              $reconstruct_active_formatting_elements->($insert_to_current);
7092            } else {
7093              !!!cp ('t399');
7094            }
7095            ## NOTE: There is an "as if in body" code clone.
7096            $parse_rcdata->(CDATA_CONTENT_MODEL);
7097            next B;
7098          } elsif ($token->{tag_name} eq 'isindex') {
7099            !!!parse-error (type => 'isindex', token => $token);
7100            
7101            if (defined $self->{form_element}) {
7102              !!!cp ('t389');
7103            ## Ignore the token            ## Ignore the token
7104              !!!nack ('t389'); ## NOTE: Not acknowledged.
7105            !!!next-token;            !!!next-token;
7106            redo B;            next B;
7107            } else {
7108              !!!ack ('t391.1');
7109    
7110            ## ISSUE: An issue in spec there            my $at = $token->{attributes};
7111              my $form_attrs;
7112              $form_attrs->{action} = $at->{action} if $at->{action};
7113              my $prompt_attr = $at->{prompt};
7114              $at->{name} = {name => 'name', value => 'isindex'};
7115              delete $at->{action};
7116              delete $at->{prompt};
7117              my @tokens = (
7118                            {type => START_TAG_TOKEN, tag_name => 'form',
7119                             attributes => $form_attrs,
7120                             line => $token->{line}, column => $token->{column}},
7121                            {type => START_TAG_TOKEN, tag_name => 'hr',
7122                             line => $token->{line}, column => $token->{column}},
7123                            {type => START_TAG_TOKEN, tag_name => 'p',
7124                             line => $token->{line}, column => $token->{column}},
7125                            {type => START_TAG_TOKEN, tag_name => 'label',
7126                             line => $token->{line}, column => $token->{column}},
7127                           );
7128              if ($prompt_attr) {
7129                !!!cp ('t390');
7130                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7131                               #line => $token->{line}, column => $token->{column},
7132                              };
7133              } else {
7134                !!!cp ('t391');
7135                push @tokens, {type => CHARACTER_TOKEN,
7136                               data => 'This is a searchable index. Insert your search keywords here: ',
7137                               #line => $token->{line}, column => $token->{column},
7138                              }; # SHOULD
7139                ## TODO: make this configurable
7140              }
7141              push @tokens,
7142                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7143                             line => $token->{line}, column => $token->{column}},
7144                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7145                            {type => END_TAG_TOKEN, tag_name => 'label',
7146                             line => $token->{line}, column => $token->{column}},
7147                            {type => END_TAG_TOKEN, tag_name => 'p',
7148                             line => $token->{line}, column => $token->{column}},
7149                            {type => START_TAG_TOKEN, tag_name => 'hr',
7150                             line => $token->{line}, column => $token->{column}},
7151                            {type => END_TAG_TOKEN, tag_name => 'form',
7152                             line => $token->{line}, column => $token->{column}};
7153              !!!back-token (@tokens);
7154              !!!next-token;
7155              next B;
7156            }
7157          } elsif ($token->{tag_name} eq 'textarea') {
7158            my $tag_name = $token->{tag_name};
7159            my $el;
7160            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7161            
7162            ## TODO: $self->{form_element} if defined
7163            $self->{content_model} = RCDATA_CONTENT_MODEL;
7164            delete $self->{escape}; # MUST
7165            
7166            $insert->($el);
7167            
7168            my $text = '';
7169            !!!nack ('t392.1');
7170            !!!next-token;
7171            if ($token->{type} == CHARACTER_TOKEN) {
7172              $token->{data} =~ s/^\x0A//;
7173              unless (length $token->{data}) {
7174                !!!cp ('t392');
7175                !!!next-token;
7176              } else {
7177                !!!cp ('t393');
7178              }
7179          } else {          } else {
7180            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t394');
7181            }
7182            while ($token->{type} == CHARACTER_TOKEN) {
7183              !!!cp ('t395');
7184              $text .= $token->{data};
7185              !!!next-token;
7186            }
7187            if (length $text) {
7188              !!!cp ('t396');
7189              $el->manakai_append_text ($text);
7190            }
7191            
7192            $self->{content_model} = PCDATA_CONTENT_MODEL;
7193            
7194            if ($token->{type} == END_TAG_TOKEN and
7195                $token->{tag_name} eq $tag_name) {
7196              !!!cp ('t397');
7197              ## Ignore the token
7198            } else {
7199              !!!cp ('t398');
7200              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7201          }          }
       }  
     } 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  
7202          !!!next-token;          !!!next-token;
7203          redo B;          next B;
7204        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'rt' or
7205          my $comment = $self->{document}->create_comment ($token->{data});                 $token->{tag_name} eq 'rp') {
7206          $self->{document}->append_child ($comment);          ## has a |ruby| element in scope
7207            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7208              my $node = $self->{open_elements}->[$_];
7209              if ($node->[1] & RUBY_EL) {
7210                !!!cp ('t398.1');
7211                ## generate implied end tags
7212                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7213                  !!!cp ('t398.2');
7214                  pop @{$self->{open_elements}};
7215                }
7216                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7217                  !!!cp ('t398.3');
7218                  !!!parse-error (type => 'not closed',
7219                                  text => $self->{open_elements}->[-1]->[0]
7220                                      ->manakai_local_name,
7221                                  token => $token);
7222                  pop @{$self->{open_elements}}
7223                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7224                }
7225                last INSCOPE;
7226              } elsif ($node->[1] & SCOPING_EL) {
7227                !!!cp ('t398.4');
7228                last INSCOPE;
7229              }
7230            } # INSCOPE
7231    
7232            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7233    
7234            !!!nack ('t398.5');
7235          !!!next-token;          !!!next-token;
7236          redo B;          redo B;
7237        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'math' or
7238          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                 $token->{tag_name} eq 'svg') {
7239            my $data = $1;          $reconstruct_active_formatting_elements->($insert_to_current);
7240            ## As if in the main phase.  
7241            ## NOTE: The insertion mode in the main phase          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7242            ## just before the phase has been changed to the trailing  
7243            ## end phase is either "after body" or "after frameset".          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7244            $reconstruct_active_formatting_elements->($insert_to_current)  
7245              if $phase eq 'main';          ## "adjust foreign attributes" - done in insert-element-f
7246            
7247            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7248            
7249            if ($self->{self_closing}) {
7250              pop @{$self->{open_elements}};
7251              !!!ack ('t398.1');
7252            } else {
7253              !!!cp ('t398.2');
7254              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7255              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7256              ## mode, "in body" (not "in foreign content") secondary insertion
7257              ## mode, maybe.
7258            }
7259    
7260            !!!next-token;
7261            next B;
7262          } elsif ({
7263                    caption => 1, col => 1, colgroup => 1, frame => 1,
7264                    frameset => 1, head => 1, option => 1, optgroup => 1,
7265                    tbody => 1, td => 1, tfoot => 1, th => 1,
7266                    thead => 1, tr => 1,
7267                   }->{$token->{tag_name}}) {
7268            !!!cp ('t401');
7269            !!!parse-error (type => 'in body',
7270                            text => $token->{tag_name}, token => $token);
7271            ## Ignore the token
7272            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7273            !!!next-token;
7274            next B;
7275            
7276            ## ISSUE: An issue on HTML5 new elements in the spec.
7277          } else {
7278            if ($token->{tag_name} eq 'image') {
7279              !!!cp ('t384');
7280              !!!parse-error (type => 'image', token => $token);
7281              $token->{tag_name} = 'img';
7282            } else {
7283              !!!cp ('t385');
7284            }
7285    
7286            ## NOTE: There is an "as if <br>" code clone.
7287            $reconstruct_active_formatting_elements->($insert_to_current);
7288            
7289            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7290    
7291            if ({
7292                 applet => 1, marquee => 1, object => 1,
7293                }->{$token->{tag_name}}) {
7294              !!!cp ('t380');
7295              push @$active_formatting_elements, ['#marker', ''];
7296              !!!nack ('t380.1');
7297            } elsif ({
7298                      b => 1, big => 1, em => 1, font => 1, i => 1,
7299                      s => 1, small => 1, strile => 1,
7300                      strong => 1, tt => 1, u => 1,
7301                     }->{$token->{tag_name}}) {
7302              !!!cp ('t375');
7303              push @$active_formatting_elements, $self->{open_elements}->[-1];
7304              !!!nack ('t375.1');
7305            } elsif ($token->{tag_name} eq 'input') {
7306              !!!cp ('t388');
7307              ## TODO: associate with $self->{form_element} if defined
7308              pop @{$self->{open_elements}};
7309              !!!ack ('t388.2');
7310            } elsif ({
7311                      area => 1, basefont => 1, bgsound => 1, br => 1,
7312                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7313                      #image => 1,
7314                     }->{$token->{tag_name}}) {
7315              !!!cp ('t388.1');
7316              pop @{$self->{open_elements}};
7317              !!!ack ('t388.3');
7318            } elsif ($token->{tag_name} eq 'select') {
7319              ## TODO: associate with $self->{form_element} if defined
7320            
7321              if ($self->{insertion_mode} & TABLE_IMS or
7322                  $self->{insertion_mode} & BODY_TABLE_IMS or
7323                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7324                !!!cp ('t400.1');
7325                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7326              } else {
7327                !!!cp ('t400.2');
7328                $self->{insertion_mode} = IN_SELECT_IM;
7329              }
7330              !!!nack ('t400.3');
7331            } else {
7332              !!!nack ('t402');
7333            }
7334            
7335            !!!next-token;
7336            next B;
7337          }
7338        } elsif ($token->{type} == END_TAG_TOKEN) {
7339          if ($token->{tag_name} eq 'body') {
7340            ## has a |body| element in scope
7341            my $i;
7342            INSCOPE: {
7343              for (reverse @{$self->{open_elements}}) {
7344                if ($_->[1] & BODY_EL) {
7345                  !!!cp ('t405');
7346                  $i = $_;
7347                  last INSCOPE;
7348                } elsif ($_->[1] & SCOPING_EL) {
7349                  !!!cp ('t405.1');
7350                  last;
7351                }
7352              }
7353    
7354              !!!parse-error (type => 'start tag not allowed',
7355                              text => $token->{tag_name}, token => $token);
7356              ## NOTE: Ignore the token.
7357              !!!next-token;
7358              next B;
7359            } # INSCOPE
7360    
7361            for (@{$self->{open_elements}}) {
7362              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7363                !!!cp ('t403');
7364                !!!parse-error (type => 'not closed',
7365                                text => $_->[0]->manakai_local_name,
7366                                token => $token);
7367                last;
7368              } else {
7369                !!!cp ('t404');
7370              }
7371            }
7372    
7373            $self->{insertion_mode} = AFTER_BODY_IM;
7374            !!!next-token;
7375            next B;
7376          } elsif ($token->{tag_name} eq 'html') {
7377            ## TODO: Update this code.  It seems that the code below is not
7378            ## up-to-date, though it has same effect as speced.
7379            if (@{$self->{open_elements}} > 1 and
7380                $self->{open_elements}->[1]->[1] & BODY_EL) {
7381              ## ISSUE: There is an issue in the spec.
7382              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7383                !!!cp ('t406');
7384                !!!parse-error (type => 'not closed',
7385                                text => $self->{open_elements}->[1]->[0]
7386                                    ->manakai_local_name,
7387                                token => $token);
7388              } else {
7389                !!!cp ('t407');
7390              }
7391              $self->{insertion_mode} = AFTER_BODY_IM;
7392              ## reprocess
7393              next B;
7394            } else {
7395              !!!cp ('t408');
7396              !!!parse-error (type => 'unmatched end tag',
7397                              text => $token->{tag_name}, token => $token);
7398              ## Ignore the token
7399              !!!next-token;
7400              next B;
7401            }
7402          } elsif ({
7403                    address => 1, blockquote => 1, center => 1, dir => 1,
7404                    div => 1, dl => 1, fieldset => 1, listing => 1,
7405                    menu => 1, ol => 1, pre => 1, ul => 1,
7406                    dd => 1, dt => 1, li => 1,
7407                    applet => 1, button => 1, marquee => 1, object => 1,
7408                   }->{$token->{tag_name}}) {
7409            ## has an element in scope
7410            my $i;
7411            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7412              my $node = $self->{open_elements}->[$_];
7413              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7414                !!!cp ('t410');
7415                $i = $_;
7416                last INSCOPE;
7417              } elsif ($node->[1] & SCOPING_EL) {
7418                !!!cp ('t411');
7419                last INSCOPE;
7420              }
7421            } # INSCOPE
7422    
7423            unless (defined $i) { # has an element in scope
7424              !!!cp ('t413');
7425              !!!parse-error (type => 'unmatched end tag',
7426                              text => $token->{tag_name}, token => $token);
7427              ## NOTE: Ignore the token.
7428            } else {
7429              ## Step 1. generate implied end tags
7430              while ({
7431                      ## END_TAG_OPTIONAL_EL
7432                      dd => ($token->{tag_name} ne 'dd'),
7433                      dt => ($token->{tag_name} ne 'dt'),
7434                      li => ($token->{tag_name} ne 'li'),
7435                      p => 1,
7436                      rt => 1,
7437                      rp => 1,
7438                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7439                !!!cp ('t409');
7440                pop @{$self->{open_elements}};
7441              }
7442    
7443              ## Step 2.
7444              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7445                      ne $token->{tag_name}) {
7446                !!!cp ('t412');
7447                !!!parse-error (type => 'not closed',
7448                                text => $self->{open_elements}->[-1]->[0]
7449                                    ->manakai_local_name,
7450                                token => $token);
7451              } else {
7452                !!!cp ('t414');
7453              }
7454    
7455              ## Step 3.
7456              splice @{$self->{open_elements}}, $i;
7457    
7458              ## Step 4.
7459              $clear_up_to_marker->()
7460                  if {
7461                    applet => 1, button => 1, marquee => 1, object => 1,
7462                  }->{$token->{tag_name}};
7463            }
7464            !!!next-token;
7465            next B;
7466          } elsif ($token->{tag_name} eq 'form') {
7467            undef $self->{form_element};
7468    
7469            ## has an element in scope
7470            my $i;
7471            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7472              my $node = $self->{open_elements}->[$_];
7473              if ($node->[1] & FORM_EL) {
7474                !!!cp ('t418');
7475                $i = $_;
7476                last INSCOPE;
7477              } elsif ($node->[1] & SCOPING_EL) {
7478                !!!cp ('t419');
7479                last INSCOPE;
7480              }
7481            } # INSCOPE
7482    
7483            unless (defined $i) { # has an element in scope
7484              !!!cp ('t421');
7485              !!!parse-error (type => 'unmatched end tag',
7486                              text => $token->{tag_name}, token => $token);
7487              ## NOTE: Ignore the token.
7488            } else {
7489              ## Step 1. generate implied end tags
7490              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7491                !!!cp ('t417');
7492                pop @{$self->{open_elements}};
7493              }
7494                        
7495            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
7496              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7497                      ne $token->{tag_name}) {
7498                !!!cp ('t417.1');
7499                !!!parse-error (type => 'not closed',
7500                                text => $self->{open_elements}->[-1]->[0]
7501                                    ->manakai_local_name,
7502                                token => $token);
7503              } else {
7504                !!!cp ('t420');
7505              }  
7506                        
7507            unless (length $token->{data}) {            ## Step 3.
7508              !!!next-token;            splice @{$self->{open_elements}}, $i;
7509              redo B;          }
7510    
7511            !!!next-token;
7512            next B;
7513          } elsif ({
7514                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7515                   }->{$token->{tag_name}}) {
7516            ## has an element in scope
7517            my $i;
7518            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7519              my $node = $self->{open_elements}->[$_];
7520              if ($node->[1] & HEADING_EL) {
7521                !!!cp ('t423');
7522                $i = $_;
7523                last INSCOPE;
7524              } elsif ($node->[1] & SCOPING_EL) {
7525                !!!cp ('t424');
7526                last INSCOPE;
7527            }            }
7528            } # INSCOPE
7529    
7530            unless (defined $i) { # has an element in scope
7531              !!!cp ('t425.1');
7532              !!!parse-error (type => 'unmatched end tag',
7533                              text => $token->{tag_name}, token => $token);
7534              ## NOTE: Ignore the token.
7535            } else {
7536              ## Step 1. generate implied end tags
7537              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7538                !!!cp ('t422');
7539                pop @{$self->{open_elements}};
7540              }
7541              
7542              ## Step 2.
7543              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7544                      ne $token->{tag_name}) {
7545                !!!cp ('t425');
7546                !!!parse-error (type => 'unmatched end tag',
7547                                text => $token->{tag_name}, token => $token);
7548              } else {
7549                !!!cp ('t426');
7550              }
7551    
7552              ## Step 3.
7553              splice @{$self->{open_elements}}, $i;
7554          }          }
7555            
7556            !!!next-token;
7557            next B;
7558          } elsif ($token->{tag_name} eq 'p') {
7559            ## has an element in scope
7560            my $i;
7561            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7562              my $node = $self->{open_elements}->[$_];
7563              if ($node->[1] & P_EL) {
7564                !!!cp ('t410.1');
7565                $i = $_;
7566                last INSCOPE;
7567              } elsif ($node->[1] & SCOPING_EL) {
7568                !!!cp ('t411.1');
7569                last INSCOPE;
7570              }
7571            } # INSCOPE
7572    
7573          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7574          $phase = 'main';            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7575          ## reprocess                    ne $token->{tag_name}) {
7576          redo B;              !!!cp ('t412.1');
7577        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
7578                 $token->{type} eq 'end tag') {                              text => $self->{open_elements}->[-1]->[0]
7579          !!!parse-error (type => 'after html:'.$token->{tag_name});                                  ->manakai_local_name,
7580          $phase = 'main';                              token => $token);
7581          ## reprocess            } else {
7582          redo B;              !!!cp ('t414.1');
7583        } elsif ($token->{type} eq 'end-of-file') {            }
7584          ## Stop parsing  
7585          last B;            splice @{$self->{open_elements}}, $i;
7586            } else {
7587              !!!cp ('t413.1');
7588              !!!parse-error (type => 'unmatched end tag',
7589                              text => $token->{tag_name}, token => $token);
7590    
7591              !!!cp ('t415.1');
7592              ## As if <p>, then reprocess the current token
7593              my $el;
7594              !!!create-element ($el, $HTML_NS, 'p',, $token);
7595              $insert->($el);
7596              ## NOTE: Not inserted into |$self->{open_elements}|.
7597            }
7598    
7599            !!!next-token;
7600            next B;
7601          } elsif ({
7602                    a => 1,
7603                    b => 1, big => 1, em => 1, font => 1, i => 1,
7604                    nobr => 1, s => 1, small => 1, strile => 1,
7605                    strong => 1, tt => 1, u => 1,
7606                   }->{$token->{tag_name}}) {
7607            !!!cp ('t427');
7608            $formatting_end_tag->($token);
7609            next B;
7610          } elsif ($token->{tag_name} eq 'br') {
7611            !!!cp ('t428');
7612            !!!parse-error (type => 'unmatched end tag',
7613                            text => 'br', token => $token);
7614    
7615            ## As if <br>
7616            $reconstruct_active_formatting_elements->($insert_to_current);
7617            
7618            my $el;
7619            !!!create-element ($el, $HTML_NS, 'br',, $token);
7620            $insert->($el);
7621            
7622            ## Ignore the token.
7623            !!!next-token;
7624            next B;
7625          } elsif ({
7626                    caption => 1, col => 1, colgroup => 1, frame => 1,
7627                    frameset => 1, head => 1, option => 1, optgroup => 1,
7628                    tbody => 1, td => 1, tfoot => 1, th => 1,
7629                    thead => 1, tr => 1,
7630                    area => 1, basefont => 1, bgsound => 1,
7631                    embed => 1, hr => 1, iframe => 1, image => 1,
7632                    img => 1, input => 1, isindex => 1, noembed => 1,
7633                    noframes => 1, param => 1, select => 1, spacer => 1,
7634                    table => 1, textarea => 1, wbr => 1,
7635                    noscript => 0, ## TODO: if scripting is enabled
7636                   }->{$token->{tag_name}}) {
7637            !!!cp ('t429');
7638            !!!parse-error (type => 'unmatched end tag',
7639                            text => $token->{tag_name}, token => $token);
7640            ## Ignore the token
7641            !!!next-token;
7642            next B;
7643            
7644            ## ISSUE: Issue on HTML5 new elements in spec
7645            
7646        } else {        } else {
7647          die "$0: $token->{type}: Unknown token";          ## Step 1
7648            my $node_i = -1;
7649            my $node = $self->{open_elements}->[$node_i];
7650    
7651            ## Step 2
7652            S2: {
7653              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7654                ## Step 1
7655                ## generate implied end tags
7656                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7657                  !!!cp ('t430');
7658                  ## NOTE: |<ruby><rt></ruby>|.
7659                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7660                  ## which seems wrong.
7661                  pop @{$self->{open_elements}};
7662                  $node_i++;
7663                }
7664            
7665                ## Step 2
7666                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7667                        ne $token->{tag_name}) {
7668                  !!!cp ('t431');
7669                  ## NOTE: <x><y></x>
7670                  !!!parse-error (type => 'not closed',
7671                                  text => $self->{open_elements}->[-1]->[0]
7672                                      ->manakai_local_name,
7673                                  token => $token);
7674                } else {
7675                  !!!cp ('t432');
7676                }
7677                
7678                ## Step 3
7679                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7680    
7681                !!!next-token;
7682                last S2;
7683              } else {
7684                ## Step 3
7685                if (not ($node->[1] & FORMATTING_EL) and
7686                    #not $phrasing_category->{$node->[1]} and
7687                    ($node->[1] & SPECIAL_EL or
7688                     $node->[1] & SCOPING_EL)) {
7689                  !!!cp ('t433');
7690                  !!!parse-error (type => 'unmatched end tag',
7691                                  text => $token->{tag_name}, token => $token);
7692                  ## Ignore the token
7693                  !!!next-token;
7694                  last S2;
7695                }
7696    
7697                !!!cp ('t434');
7698              }
7699              
7700              ## Step 4
7701              $node_i--;
7702              $node = $self->{open_elements}->[$node_i];
7703              
7704              ## Step 5;
7705              redo S2;
7706            } # S2
7707            next B;
7708        }        }
7709      }      }
7710        next B;
7711      } continue { # B
7712        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7713          ## NOTE: The code below is executed in cases where it does not have
7714          ## to be, but it it is harmless even in those cases.
7715          ## has an element in scope
7716          INSCOPE: {
7717            for (reverse 0..$#{$self->{open_elements}}) {
7718              my $node = $self->{open_elements}->[$_];
7719              if ($node->[1] & FOREIGN_EL) {
7720                last INSCOPE;
7721              } elsif ($node->[1] & SCOPING_EL) {
7722                last;
7723              }
7724            }
7725            
7726            ## NOTE: No foreign element in scope.
7727            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7728          } # INSCOPE
7729        }
7730    } # B    } # B
7731    
7732    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4907  sub _tree_construction_main ($) { Line 7734  sub _tree_construction_main ($) {
7734    ## TODO: script stuffs    ## TODO: script stuffs
7735  } # _tree_construct_main  } # _tree_construct_main
7736    
7737  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7738    my $class = shift;    my $class = shift;
7739    my $node = shift;    my $node = shift;
7740    my $s = \$_[0];    #my $s = \$_[0];
7741    my $onerror = $_[1];    my $onerror = $_[1];
7742      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7743    
7744      ## ISSUE: Should {confident} be true?
7745    
7746    my $nt = $node->node_type;    my $nt = $node->node_type;
7747    if ($nt == 9) {    if ($nt == 9) {
# Line 4928  sub set_inner_html ($$$) { Line 7758  sub set_inner_html ($$$) {
7758      }      }
7759    
7760      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7761      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7762    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7763      ## TODO: If non-html element      ## TODO: If non-html element
7764    
7765      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7766    
7767    ## TODO: Support for $get_wrapper
7768    
7769      ## Step 1 # MUST      ## Step 1 # MUST
7770      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
7771      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
7772        $doc->manakai_is_html (1);
7773      my $p = $class->new;      my $p = $class->new;
7774      $p->{document} = $doc;      $p->{document} = $doc;
7775    
7776      ## Step 9 # MUST      ## Step 8 # MUST
7777      my $i = 0;      my $i = 0;
7778      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7779      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7780      $p->{set_next_input_character} = sub {      require Whatpm::Charset::DecodeHandle;
7781        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7782        $input = $get_wrapper->($input);
7783        $p->{set_next_char} = sub {
7784        my $self = shift;        my $self = shift;
7785        $self->{next_input_character} = -1 and return if $i >= length $$s;  
7786        $self->{next_input_character} = ord substr $$s, $i++, 1;        pop @{$self->{prev_char}};
7787        $column++;        unshift @{$self->{prev_char}}, $self->{next_char};
7788    
7789        if ($self->{next_input_character} == 0x000A) { # LF        my $char;
7790          $line++;        if (defined $self->{next_next_char}) {
7791          $column = 0;          $char = $self->{next_next_char};
7792        } elsif ($self->{next_input_character} == 0x000D) { # CR          delete $self->{next_next_char};
7793          if ($i >= length $$s) {        } else {
7794            #          $char = $input->getc;
7795          }
7796          $self->{next_char} = -1 and return unless defined $char;
7797          $self->{next_char} = ord $char;
7798    
7799          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7800          $p->{column}++;
7801    
7802          if ($self->{next_char} == 0x000A) { # LF
7803            $p->{line}++;
7804            $p->{column} = 0;
7805            !!!cp ('i1');
7806          } elsif ($self->{next_char} == 0x000D) { # CR
7807    ## TODO: support for abort/streaming
7808            my $next = $input->getc;
7809            if (defined $next and $next ne "\x0A") {
7810              $self->{next_next_char} = $next;
7811            }
7812            $self->{next_char} = 0x000A; # LF # MUST
7813            $p->{line}++;
7814            $p->{column} = 0;
7815            !!!cp ('i2');
7816          } elsif ($self->{next_char} > 0x10FFFF) {
7817            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7818            !!!cp ('i3');
7819          } elsif ($self->{next_char} == 0x0000) { # NULL
7820            !!!cp ('i4');
7821            !!!parse-error (type => 'NULL');
7822            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7823          } elsif ($self->{next_char} <= 0x0008 or
7824                   (0x000E <= $self->{next_char} and
7825                    $self->{next_char} <= 0x001F) or
7826                   (0x007F <= $self->{next_char} and
7827                    $self->{next_char} <= 0x009F) or
7828                   (0xD800 <= $self->{next_char} and
7829                    $self->{next_char} <= 0xDFFF) or
7830                   (0xFDD0 <= $self->{next_char} and
7831                    $self->{next_char} <= 0xFDDF) or
7832                   {
7833                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7834                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7835                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7836                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7837                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7838                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7839                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7840                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7841                    0x10FFFE => 1, 0x10FFFF => 1,
7842                   }->{$self->{next_char}}) {
7843            !!!cp ('i4.1');
7844            if ($self->{next_char} < 0x10000) {
7845              !!!parse-error (type => 'control char',
7846                              text => (sprintf 'U+%04X', $self->{next_char}));
7847          } else {          } else {
7848            my $next_char = ord substr $$s, $i++, 1;            !!!parse-error (type => 'control char',
7849            if ($next_char == 0x000A) { # LF                            text => (sprintf 'U-%08X', $self->{next_char}));
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
7850          }          }
         $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  
7851        }        }
7852      };      };
7853            $p->{prev_char} = [-1, -1, -1];
7854        $p->{next_char} = -1;
7855    
7856        $p->{read_until} = sub {
7857          #my ($scalar, $specials_range, $offset) = @_;
7858          my $specials_range = $_[1];
7859          return 0 if defined $p->{next_next_char};
7860          my $count = $input->manakai_read_until
7861            ($_[0],
7862             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}]/,
7863             $_[2]);
7864          if ($count) {
7865            $p->{column} += $count;
7866            $p->{column_prev} += $count;
7867            $p->{prev_char} = [-1, -1, -1];
7868            $p->{next_char} = -1;
7869          }
7870          return $count;
7871        }; # $p->{read_until}
7872    
7873      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7874        my (%opt) = @_;        my (%opt) = @_;
7875        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7876          my $column = $opt{column};
7877          if (defined $opt{token} and defined $opt{token}->{line}) {
7878            $line = $opt{token}->{line};
7879            $column = $opt{token}->{column};
7880          }
7881          warn "Parse error ($opt{type}) at line $line column $column\n";
7882      };      };
7883      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7884        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7885      };      };
7886            
7887      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7888      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7889    
7890      ## Step 2      ## Step 2
7891      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7892      $p->{content_model_flag} = {      $p->{content_model} = {
7893        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7894        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7895        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7896        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7897        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7898        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7899        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7900        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7901        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7902        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7903      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7904         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7905            unless defined $p->{content_model};
7906            ## ISSUE: What is "the name of the element"? local name?
7907    
7908      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7909          ## TODO: Foreign element OK?
7910    
7911      ## Step 4      ## Step 3
7912      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7913        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7914    
7915      ## Step 5 # MUST      ## Step 4 # MUST
7916      $doc->append_child ($root);      $doc->append_child ($root);
7917    
7918      ## Step 6 # MUST      ## Step 5 # MUST
7919      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7920    
7921      undef $p->{head_element};      undef $p->{head_element};
7922    
7923      ## Step 7 # MUST      ## Step 6 # MUST
7924      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7925    
7926      ## Step 8 # MUST      ## Step 7 # MUST
7927      my $anode = $node;      my $anode = $node;
7928      AN: while (defined $anode) {      AN: while (defined $anode) {
7929        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7930          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7931          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7932            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7933                !!!cp ('i5');
7934              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7935              last AN;              last AN;
7936            }            }
# Line 5033  sub set_inner_html ($$$) { Line 7939  sub set_inner_html ($$$) {
7939        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7940      } # AN      } # AN
7941            
7942      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7943      {      {
7944        my $self = $p;        my $self = $p;
7945        !!!next-token;        !!!next-token;
7946      }      }
7947      $p->_tree_construction_main;      $p->_tree_construction_main;
7948    
7949      ## Step 11 # MUST      ## Step 10 # MUST
7950      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7951      for (@cn) {      for (@cn) {
7952        $node->remove_child ($_);        $node->remove_child ($_);
7953      }      }
7954      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7955    
7956      ## Step 12 # MUST      ## Step 11 # MUST
7957      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7958      for (@cn) {      for (@cn) {
7959          $this_doc->adopt_node ($_);
7960        $node->append_child ($_);        $node->append_child ($_);
7961      }      }
7962      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
7963    
7964      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7965    
7966        delete $p->{parse_error}; # delete loop
7967    } else {    } else {
7968      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";
7969    }    }
# Line 5063  sub set_inner_html ($$$) { Line 7971  sub set_inner_html ($$$) {
7971    
7972  } # tree construction stage  } # tree construction stage
7973    
7974  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7975    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  
7976    
7977  1;  1;
7978  # $Date$  # $Date$

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.177

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24