/[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.3 by wakaba, Wed May 2 13:44:34 2007 UTC revision 1.163 by wakaba, Sat Sep 13 04:19:56 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  my $permitted_slash_tag_name = {  ## doc.write ('');
9    base => 1,  ## alert (doc.compatMode);
10    link => 1,  
11    meta => 1,  require IO::Handle;
12    hr => 1,  
13    br => 1,  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    img=> 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15    embed => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    param => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    area => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    col => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    input => 1,  
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 $entity_char = {  my $el_category_f = {
217    AElig => "\x{00C6}",    $MML_NS => {
218    Aacute => "\x{00C1}",      'annotation-xml' => MML_AXML_EL,
219    Acirc => "\x{00C2}",      mi => FOREIGN_FLOW_CONTENT_EL,
220    Agrave => "\x{00C0}",      mo => FOREIGN_FLOW_CONTENT_EL,
221    Alpha => "\x{0391}",      mn => FOREIGN_FLOW_CONTENT_EL,
222    Aring => "\x{00C5}",      ms => FOREIGN_FLOW_CONTENT_EL,
223    Atilde => "\x{00C3}",      mtext => FOREIGN_FLOW_CONTENT_EL,
224    Auml => "\x{00C4}",    },
225    Beta => "\x{0392}",    $SVG_NS => {
226    Ccedil => "\x{00C7}",      foreignObject => FOREIGN_FLOW_CONTENT_EL,
227    Chi => "\x{03A7}",      desc => FOREIGN_FLOW_CONTENT_EL,
228    Dagger => "\x{2021}",      title => FOREIGN_FLOW_CONTENT_EL,
229    Delta => "\x{0394}",    },
230    ETH => "\x{00D0}",    ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
   Eacute => "\x{00C9}",  
   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}",  
231  };  };
232    
233  my $special_category = {  my $svg_attr_name = {
234    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    attributename => 'attributeName',
235    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    attributetype => 'attributeType',
236    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    basefrequency => 'baseFrequency',
237    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    baseprofile => 'baseProfile',
238    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,    calcmode => 'calcMode',
239    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,    clippathunits => 'clipPathUnits',
240    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,    contentscripttype => 'contentScriptType',
241    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    contentstyletype => 'contentStyleType',
242    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    diffuseconstant => 'diffuseConstant',
243    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    edgemode => 'edgeMode',
244  };    externalresourcesrequired => 'externalResourcesRequired',
245  my $scoping_category = {    filterres => 'filterRes',
246    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    filterunits => 'filterUnits',
247    table => 1, td => 1, th => 1,    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  my $formatting_category = {  
298    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  my $foreign_attr_xname = {
299    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,    'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311  };  };
 # $phrasing_category: all other elements  
312    
313  sub parse_string ($$$;$) {  ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    my $self = shift->new;  
315    my $s = \$_[0];  my $c1_entity_char = {
316      0x80 => 0x20AC,
317      0x81 => 0xFFFD,
318      0x82 => 0x201A,
319      0x83 => 0x0192,
320      0x84 => 0x201E,
321      0x85 => 0x2026,
322      0x86 => 0x2020,
323      0x87 => 0x2021,
324      0x88 => 0x02C6,
325      0x89 => 0x2030,
326      0x8A => 0x0160,
327      0x8B => 0x2039,
328      0x8C => 0x0152,
329      0x8D => 0xFFFD,
330      0x8E => 0x017D,
331      0x8F => 0xFFFD,
332      0x90 => 0xFFFD,
333      0x91 => 0x2018,
334      0x92 => 0x2019,
335      0x93 => 0x201C,
336      0x94 => 0x201D,
337      0x95 => 0x2022,
338      0x96 => 0x2013,
339      0x97 => 0x2014,
340      0x98 => 0x02DC,
341      0x99 => 0x2122,
342      0x9A => 0x0161,
343      0x9B => 0x203A,
344      0x9C => 0x0153,
345      0x9D => 0xFFFD,
346      0x9E => 0x017E,
347      0x9F => 0x0178,
348    }; # $c1_entity_char
349    
350    sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$$) {
358      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359      my $self = ref $_[0] ? shift : shift->new;
360      my $charset_name = shift;
361      my $byte_stream = $_[0];
362    
363      my $onerror = $_[2] || sub {
364        my (%opt) = @_;
365        warn "Parse error ($opt{type})\n";
366      };
367      $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      };
372    
373      ## HTML5 encoding sniffing algorithm
374      require Message::Charset::Info;
375      my $charset;
376      my $buffer;
377      my ($char_stream, $e_status);
378    
379      SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387        ## Step 1
388        if (defined $charset_name) {
389          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393          ## ISSUE: Unsupported encoding is not ignored according to the spec.
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1);
397          if ($char_stream) {
398            $self->{confident} = 1;
399            last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402          }
403        }
404    
405        ## Step 2
406        my $byte_buffer = '';
407        for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413        ## Step 3
414        if ($byte_buffer =~ /^\xFE\xFF/) {
415          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416          ($char_stream, $e_status) = $charset->get_decode_handle
417              ($byte_stream, allow_error_reporting => 1,
418               allow_fallback => 1, byte_buffer => \$byte_buffer);
419          $self->{confident} = 1;
420          last SNIFFING;
421        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435        }
436    
437        ## Step 4
438        ## TODO: <meta charset>
439    
440        ## Step 5
441        ## TODO: from history
442    
443        ## Step 6
444        require Whatpm::Charset::UniversalCharDet;
445        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446            ($byte_buffer);
447        if (defined $charset_name) {
448          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450          ## ISSUE: Unsupported encoding is not ignored according to the spec.
451          require Whatpm::Charset::DecodeHandle;
452          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453              ($byte_stream);
454          ($char_stream, $e_status) = $charset->get_decode_handle
455              ($buffer, allow_error_reporting => 1,
456               allow_fallback => 1, byte_buffer => \$byte_buffer);
457          if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            level => $self->{level}->{info},
462                            layer => 'encode',
463                            line => 1, column => 1);
464            $self->{confident} = 0;
465            last SNIFFING;
466          }
467        }
468    
469        ## Step 7: default
470        ## TODO: Make this configurable.
471        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473            ## detectable in the step 6.
474        require Whatpm::Charset::DecodeHandle;
475        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476            ($byte_stream);
477        ($char_stream, $e_status)
478            = $charset->get_decode_handle ($buffer,
479                                           allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        line => 1, column => 1,
487                        layer => 'encode');
488        $self->{confident} = 0;
489      } # SNIFFING
490    
491      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493        !!!parse-error (type => 'chardecode:fallback',
494                        #text => $self->{input_encoding},
495                        level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498      } elsif (not ($e_status &
499                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
500        $self->{input_encoding} = $charset->get_iana_name;
501        !!!parse-error (type => 'chardecode:no error',
502                        text => $self->{input_encoding},
503                        level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508      }
509    
510      $self->{change_encoding} = sub {
511        my $self = shift;
512        $charset_name = shift;
513        my $token = shift;
514    
515        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516        ($char_stream, $e_status) = $charset->get_decode_handle
517            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519        
520        if ($char_stream) { # if supported
521          ## "Change the encoding" algorithm:
522    
523          ## Step 1    
524          if ($charset->{category} &
525              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530          }
531          $charset_name = $charset->get_iana_name;
532          
533          ## Step 2
534          if (defined $self->{input_encoding} and
535              $self->{input_encoding} eq $charset_name) {
536            !!!parse-error (type => 'charset label:matching',
537                            text => $charset_name,
538                            level => $self->{level}->{info});
539            $self->{confident} = 1;
540            return;
541          }
542    
543          !!!parse-error (type => 'charset label detected',
544                          text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          token => $token);
548          
549          ## Step 3
550          # if (can) {
551            ## change the encoding on the fly.
552            #$self->{confident} = 1;
553            #return;
554          # }
555          
556          ## Step 4
557          throw Whatpm::HTML::RestartParser ();
558        }
559      }; # $self->{change_encoding}
560    
561      my $char_onerror = sub {
562        my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564                        %opt, type => $type,
565                        line => $self->{line}, column => $self->{column} + 1);
566        if ($opt{octets}) {
567          ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        }
569      };
570    
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574      my @args = @_; shift @args; # $s
575      my $return;
576      try {
577        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578      } catch Whatpm::HTML::RestartParser with {
579        ## NOTE: Invoked after {change_encoding}.
580    
581        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583          !!!parse-error (type => 'chardecode:fallback',
584                          level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588        } elsif (not ($e_status &
589                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
590          $self->{input_encoding} = $charset->get_iana_name;
591          !!!parse-error (type => 'chardecode:no error',
592                          text => $self->{input_encoding},
593                          level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598        }
599        $self->{confident} = 1;
600    
601        $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605      };
606      return $return;
607    } # parse_byte_stream
608    
609    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
611    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
612    ## because the core part of our HTML parser expects a string of character,
613    ## not a string of bytes or code units or anything which might contain a BOM.
614    ## Therefore, any parser interface that accepts a string of bytes,
615    ## such as |parse_byte_string| in this module, must ensure that it does
616    ## strip the BOM and never strip any ZWNBSP.
617    
618    sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      require utf8;
622      my $s = ref $_[0] ? $_[0] : \($_[0]);
623      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631    sub parse_char_stream ($$$;$) {
632      my $self = ref $_[0] ? shift : shift->new;
633      my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635      @{$self->{document}->child_nodes} = ();
636    
637    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
638    
639      $self->{confident} = 1 unless exists $self->{confident};
640      $self->{document}->input_encoding ($self->{input_encoding})
641          if defined $self->{input_encoding};
642    
643    my $i = 0;    my $i = 0;
644    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
645    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
646    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
647      my $self = shift;      my $self = shift;
648      $self->{next_input_character} = -1 and return if $i >= length $$s;  
649      $self->{next_input_character} = ord substr $$s, $i++, 1;      pop @{$self->{prev_char}};
650      $column++;      unshift @{$self->{prev_char}}, $self->{next_char};
651    
652        my $char;
653        if (defined $self->{next_next_char}) {
654          $char = $self->{next_next_char};
655          delete $self->{next_next_char};
656        } else {
657          $char = $input->getc;
658        }
659        $self->{next_char} = -1 and return unless defined $char;
660        $self->{next_char} = ord $char;
661    
662        ($self->{line_prev}, $self->{column_prev})
663            = ($self->{line}, $self->{column});
664        $self->{column}++;
665            
666      if ($self->{next_input_character} == 0x000D) { # CR      if ($self->{next_char} == 0x000A) { # LF
667        if ($i >= length $$s) {        !!!cp ('j1');
668          #        $self->{line}++;
669          $self->{column} = 0;
670        } elsif ($self->{next_char} == 0x000D) { # CR
671          !!!cp ('j2');
672          my $next = $input->getc;
673          if (defined $next and $next ne "\x0A") {
674            $self->{next_next_char} = $next;
675          }
676          $self->{next_char} = 0x000A; # LF # MUST
677          $self->{line}++;
678          $self->{column} = 0;
679        } elsif ($self->{next_char} > 0x10FFFF) {
680          !!!cp ('j3');
681          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
682        } elsif ($self->{next_char} == 0x0000) { # NULL
683          !!!cp ('j4');
684          !!!parse-error (type => 'NULL');
685          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
686        } elsif ($self->{next_char} <= 0x0008 or
687                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
688                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
689                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
690                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
691                 {
692                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
693                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
694                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
695                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
696                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
697                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
698                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
699                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
700                  0x10FFFE => 1, 0x10FFFF => 1,
701                 }->{$self->{next_char}}) {
702          !!!cp ('j5');
703          if ($self->{next_char} < 0x10000) {
704            !!!parse-error (type => 'control char',
705                            text => (sprintf 'U+%04X', $self->{next_char}));
706        } else {        } else {
707          my $next_char = ord substr $$s, $i++, 1;          !!!parse-error (type => 'control char',
708          if ($next_char == 0x000A) { # LF                          text => (sprintf 'U-%08X', $self->{next_char}));
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
709        }        }
       $self->{next_input_character} = 0x000A; # LF # MUST  
       $line++;  
       $column = -1;  
     } elsif ($self->{next_input_character} > 0x10FFFF) {  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
     } elsif ($self->{next_input_character} == 0x0000) { # NULL  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
710      }      }
711    };    };
712      $self->{prev_char} = [-1, -1, -1];
713      $self->{next_char} = -1;
714    
715    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
716      my (%opt) = @_;      my (%opt) = @_;
717      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
718        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
719        warn "Parse error ($opt{type}) at line $line column $column\n";
720    };    };
721    $self->{parse_error} = sub {    $self->{parse_error} = sub {
722      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
723    };    };
724    
725    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 352  sub parse_string ($$$;$) { Line 727  sub parse_string ($$$;$) {
727    $self->_construct_tree;    $self->_construct_tree;
728    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
729    
730      delete $self->{parse_error}; # remove loop
731    
732    return $self->{document};    return $self->{document};
733  } # parse_string  } # parse_char_stream
734    
735  sub new ($) {  sub new ($) {
736    my $class = shift;    my $class = shift;
737    my $self = bless {}, $class;    my $self = bless {
738    $self->{set_next_input_character} = sub {      level => {must => 'm',
739      $self->{next_input_character} = -1;                should => 's',
740                  warn => 'w',
741                  info => 'i',
742                  uncertain => 'u'},
743      }, $class;
744      $self->{set_next_char} = sub {
745        $self->{next_char} = -1;
746    };    };
747    $self->{parse_error} = sub {    $self->{parse_error} = sub {
748      #      #
749    };    };
750      $self->{change_encoding} = sub {
751        # if ($_[0] is a supported encoding) {
752        #   run "change the encoding" algorithm;
753        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
754        # }
755      };
756      $self->{application_cache_selection} = sub {
757        #
758      };
759    return $self;    return $self;
760  } # new  } # new
761    
762    sub CM_ENTITY () { 0b001 } # & markup in data
763    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
764    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
765    
766    sub PLAINTEXT_CONTENT_MODEL () { 0 }
767    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
768    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
769    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
770    
771    sub DATA_STATE () { 0 }
772    sub ENTITY_DATA_STATE () { 1 }
773    sub TAG_OPEN_STATE () { 2 }
774    sub CLOSE_TAG_OPEN_STATE () { 3 }
775    sub TAG_NAME_STATE () { 4 }
776    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
777    sub ATTRIBUTE_NAME_STATE () { 6 }
778    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
779    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
780    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
781    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
782    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
783    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
784    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
785    sub COMMENT_START_STATE () { 14 }
786    sub COMMENT_START_DASH_STATE () { 15 }
787    sub COMMENT_STATE () { 16 }
788    sub COMMENT_END_STATE () { 17 }
789    sub COMMENT_END_DASH_STATE () { 18 }
790    sub BOGUS_COMMENT_STATE () { 19 }
791    sub DOCTYPE_STATE () { 20 }
792    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
793    sub DOCTYPE_NAME_STATE () { 22 }
794    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
795    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
796    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
797    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
798    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
799    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
800    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
801    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
802    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
803    sub BOGUS_DOCTYPE_STATE () { 32 }
804    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
805    sub SELF_CLOSING_START_TAG_STATE () { 34 }
806    sub CDATA_BLOCK_STATE () { 35 }
807    sub MD_HYPHEN_STATE () { 36 }
808    sub MD_DOCTYPE_STATE () { 37 }
809    sub MD_CDATA_STATE () { 38 }
810    
811    sub DOCTYPE_TOKEN () { 1 }
812    sub COMMENT_TOKEN () { 2 }
813    sub START_TAG_TOKEN () { 3 }
814    sub END_TAG_TOKEN () { 4 }
815    sub END_OF_FILE_TOKEN () { 5 }
816    sub CHARACTER_TOKEN () { 6 }
817    
818    sub AFTER_HTML_IMS () { 0b100 }
819    sub HEAD_IMS ()       { 0b1000 }
820    sub BODY_IMS ()       { 0b10000 }
821    sub BODY_TABLE_IMS () { 0b100000 }
822    sub TABLE_IMS ()      { 0b1000000 }
823    sub ROW_IMS ()        { 0b10000000 }
824    sub BODY_AFTER_IMS () { 0b100000000 }
825    sub FRAME_IMS ()      { 0b1000000000 }
826    sub SELECT_IMS ()     { 0b10000000000 }
827    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
828        ## NOTE: "in foreign content" insertion mode is special; it is combined
829        ## with the secondary insertion mode.  In this parser, they are stored
830        ## together in the bit-or'ed form.
831    
832    ## NOTE: "initial" and "before html" insertion modes have no constants.
833    
834    ## NOTE: "after after body" insertion mode.
835    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
836    
837    ## NOTE: "after after frameset" insertion mode.
838    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
839    
840    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
841    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
842    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
843    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
844    sub IN_BODY_IM () { BODY_IMS }
845    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
846    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
847    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
848    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
849    sub IN_TABLE_IM () { TABLE_IMS }
850    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
851    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
852    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
853    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
854    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
855    sub IN_COLUMN_GROUP_IM () { 0b10 }
856    
857  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
858    
859  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
860    my $self = shift;    my $self = shift;
861    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
862    $self->{content_model_flag} = 'PCDATA'; # be    #$self->{state_keyword}; # initialized when used
863      $self->{content_model} = PCDATA_CONTENT_MODEL; # be
864    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
865    undef $self->{current_attribute};    undef $self->{current_attribute};
866    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
867    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
868      delete $self->{self_closing};
869    $self->{char} = [];    $self->{char} = [];
870    # $self->{next_input_character}    # $self->{next_char}
871    !!!next-input-character;    !!!next-input-character;
872    $self->{token} = [];    $self->{token} = [];
873      # $self->{escape}
874  } # _initialize_tokenizer  } # _initialize_tokenizer
875    
876  ## A token has:  ## A token has:
877  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
878  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
879  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
880      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
881  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
882  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
883  ##   ->{data} (comment, character)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
884    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
885  ## Macros  ##        ->{name}
886  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
887  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
888  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
889    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
890    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
891    ##     while the token is pushed back to the stack.
892    
893  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
894    
# Line 405  sub _initialize_tokenizer ($) { Line 898  sub _initialize_tokenizer ($) {
898  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
899  ## and removed from the list.  ## and removed from the list.
900    
901    ## NOTE: HTML5 "Writing HTML documents" section, applied to
902    ## documents and not to user agents and conformance checkers,
903    ## contains some requirements that are not detected by the
904    ## parsing algorithm:
905    ## - Some requirements on character encoding declarations. ## TODO
906    ## - "Elements MUST NOT contain content that their content model disallows."
907    ##   ... Some are parse error, some are not (will be reported by c.c.).
908    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
909    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
910    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
911    
912    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
913    ## be detected by the HTML5 parsing algorithm:
914    ## - Text,
915    
916  sub _get_next_token ($) {  sub _get_next_token ($) {
917    my $self = shift;    my $self = shift;
918    
919      if ($self->{self_closing}) {
920        !!!parse-error (type => 'nestc', token => $self->{current_token});
921        ## NOTE: The |self_closing| flag is only set by start tag token.
922        ## In addition, when a start tag token is emitted, it is always set to
923        ## |current_token|.
924        delete $self->{self_closing};
925      }
926    
927    if (@{$self->{token}}) {    if (@{$self->{token}}) {
928        $self->{self_closing} = $self->{token}->[0]->{self_closing};
929      return shift @{$self->{token}};      return shift @{$self->{token}};
930    }    }
931    
932    A: {    A: {
933      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
934        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
935          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
936              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
937            $self->{state} = 'entity data';            !!!cp (1);
938              $self->{state} = ENTITY_DATA_STATE;
939            !!!next-input-character;            !!!next-input-character;
940            redo A;            redo A;
941          } else {          } else {
942              !!!cp (2);
943            #            #
944          }          }
945        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x002D) { # -
946          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
947            $self->{state} = 'tag open';            unless ($self->{escape}) {
948                if ($self->{prev_char}->[0] == 0x002D and # -
949                    $self->{prev_char}->[1] == 0x0021 and # !
950                    $self->{prev_char}->[2] == 0x003C) { # <
951                  !!!cp (3);
952                  $self->{escape} = 1;
953                } else {
954                  !!!cp (4);
955                }
956              } else {
957                !!!cp (5);
958              }
959            }
960            
961            #
962          } elsif ($self->{next_char} == 0x003C) { # <
963            if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
964                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
965                 not $self->{escape})) {
966              !!!cp (6);
967              $self->{state} = TAG_OPEN_STATE;
968            !!!next-input-character;            !!!next-input-character;
969            redo A;            redo A;
970          } else {          } else {
971              !!!cp (7);
972            #            #
973          }          }
974        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
975          !!!emit ({type => 'end-of-file'});          if ($self->{escape} and
976                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
977              if ($self->{prev_char}->[0] == 0x002D and # -
978                  $self->{prev_char}->[1] == 0x002D) { # -
979                !!!cp (8);
980                delete $self->{escape};
981              } else {
982                !!!cp (9);
983              }
984            } else {
985              !!!cp (10);
986            }
987            
988            #
989          } elsif ($self->{next_char} == -1) {
990            !!!cp (11);
991            !!!emit ({type => END_OF_FILE_TOKEN,
992                      line => $self->{line}, column => $self->{column}});
993          last A; ## TODO: ok?          last A; ## TODO: ok?
994          } else {
995            !!!cp (12);
996        }        }
997        # Anything else        # Anything else
998        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
999                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
1000                       line => $self->{line}, column => $self->{column},
1001                      };
1002        ## Stay in the data state        ## Stay in the data state
1003        !!!next-input-character;        !!!next-input-character;
1004    
1005        !!!emit ($token);        !!!emit ($token);
1006    
1007        redo A;        redo A;
1008      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
1009        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
1010    
1011          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
1012                
1013        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
1014    
1015        $self->{state} = 'data';        $self->{state} = DATA_STATE;
1016        # next-input-character is already done        # next-input-character is already done
1017    
1018        unless (defined $token) {        unless (defined $token) {
1019          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
1020            !!!emit ({type => CHARACTER_TOKEN, data => '&',
1021                      line => $l, column => $c,
1022                     });
1023        } else {        } else {
1024            !!!cp (14);
1025          !!!emit ($token);          !!!emit ($token);
1026        }        }
1027    
1028        redo A;        redo A;
1029      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1030        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1031            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
1032          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
1033            !!!next-input-character;            !!!next-input-character;
1034            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1035            redo A;            redo A;
1036          } else {          } else {
1037              !!!cp (16);
1038            ## reconsume            ## reconsume
1039            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1040    
1041            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1042                        line => $self->{line_prev},
1043                        column => $self->{column_prev},
1044                       });
1045    
1046            redo A;            redo A;
1047          }          }
1048        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1049          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1050            $self->{state} = 'markup declaration open';            !!!cp (17);
1051              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1052            !!!next-input-character;            !!!next-input-character;
1053            redo A;            redo A;
1054          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1055            $self->{state} = 'close tag open';            !!!cp (18);
1056              $self->{state} = CLOSE_TAG_OPEN_STATE;
1057            !!!next-input-character;            !!!next-input-character;
1058            redo A;            redo A;
1059          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1060                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1061              !!!cp (19);
1062            $self->{current_token}            $self->{current_token}
1063              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1064                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1065            $self->{state} = 'tag name';                 line => $self->{line_prev},
1066                   column => $self->{column_prev}};
1067              $self->{state} = TAG_NAME_STATE;
1068            !!!next-input-character;            !!!next-input-character;
1069            redo A;            redo A;
1070          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1071                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1072            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1073                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1074            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1075                                        line => $self->{line_prev},
1076                                        column => $self->{column_prev}};
1077              $self->{state} = TAG_NAME_STATE;
1078            !!!next-input-character;            !!!next-input-character;
1079            redo A;            redo A;
1080          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1081            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1082            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1083                              line => $self->{line_prev},
1084                              column => $self->{column_prev});
1085              $self->{state} = DATA_STATE;
1086            !!!next-input-character;            !!!next-input-character;
1087    
1088            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1089                        line => $self->{line_prev},
1090                        column => $self->{column_prev},
1091                       });
1092    
1093            redo A;            redo A;
1094          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1095            !!!parse-error (type => 'pio');            !!!cp (22);
1096            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1097            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1098                              column => $self->{column_prev});
1099              $self->{state} = BOGUS_COMMENT_STATE;
1100              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1101                                        line => $self->{line_prev},
1102                                        column => $self->{column_prev},
1103                                       };
1104              ## $self->{next_char} is intentionally left as is
1105            redo A;            redo A;
1106          } else {          } else {
1107            !!!parse-error (type => 'bare stago');            !!!cp (23);
1108            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1109                              line => $self->{line_prev},
1110                              column => $self->{column_prev});
1111              $self->{state} = DATA_STATE;
1112            ## reconsume            ## reconsume
1113    
1114            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1115                        line => $self->{line_prev},
1116                        column => $self->{column_prev},
1117                       });
1118    
1119            redo A;            redo A;
1120          }          }
1121        } else {        } else {
1122          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1123        }        }
1124      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1125        if ($self->{content_model_flag} eq 'RCDATA' or        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1126            $self->{content_model_flag} eq 'CDATA') {        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1127          my @next_char;          if (defined $self->{last_emitted_start_tag_name}) {
1128          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
1129            push @next_char, $self->{next_input_character};            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1130            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);            my @next_char;
1131            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
1132            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              push @next_char, $self->{next_char};
1133              !!!next-input-character;              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1134              next TAGNAME;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1135            } else {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1136              !!!parse-error (type => 'unmatched end tag');                !!!cp (24);
1137              $self->{next_input_character} = shift @next_char; # reconsume                !!!next-input-character;
1138                  next TAGNAME;
1139                } else {
1140                  !!!cp (25);
1141                  $self->{next_char} = shift @next_char; # reconsume
1142                  !!!back-next-input-character (@next_char);
1143                  $self->{state} = DATA_STATE;
1144    
1145                  !!!emit ({type => CHARACTER_TOKEN, data => '</',
1146                            line => $l, column => $c,
1147                           });
1148      
1149                  redo A;
1150                }
1151              }
1152              push @next_char, $self->{next_char};
1153          
1154              unless ($self->{next_char} == 0x0009 or # HT
1155                      $self->{next_char} == 0x000A or # LF
1156                      $self->{next_char} == 0x000B or # VT
1157                      $self->{next_char} == 0x000C or # FF
1158                      $self->{next_char} == 0x0020 or # SP
1159                      $self->{next_char} == 0x003E or # >
1160                      $self->{next_char} == 0x002F or # /
1161                      $self->{next_char} == -1) {
1162                !!!cp (26);
1163                $self->{next_char} = shift @next_char; # reconsume
1164              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1165              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1166                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1167              !!!emit ({type => 'character', data => '</'});                        line => $l, column => $c,
1168                         });
1169              redo A;              redo A;
1170              } else {
1171                !!!cp (27);
1172                $self->{next_char} = shift @next_char;
1173                !!!back-next-input-character (@next_char);
1174                # and consume...
1175            }            }
         }  
         push @next_char, $self->{next_input_character};  
       
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == 0x003C or # <  
                 $self->{next_input_character} == -1) {  
           !!!parse-error (type => 'unmatched end tag');  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
           $self->{state} = 'data';  
   
           !!!emit ({type => 'character', data => '</'});  
   
           redo A;  
1176          } else {          } else {
1177            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
1178            !!!back-next-input-character (@next_char);            !!!cp (28);
1179            # and consume...            # next-input-character is already done
1180              $self->{state} = DATA_STATE;
1181              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1182                        line => $l, column => $c,
1183                       });
1184              redo A;
1185          }          }
1186        }        }
1187                
1188        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1189            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1190          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1191                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1192          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1193                   tag_name => chr ($self->{next_char} + 0x0020),
1194                   line => $l, column => $c};
1195            $self->{state} = TAG_NAME_STATE;
1196            !!!next-input-character;
1197            redo A;
1198          } elsif (0x0061 <= $self->{next_char} and
1199                   $self->{next_char} <= 0x007A) { # a..z
1200            !!!cp (30);
1201            $self->{current_token} = {type => END_TAG_TOKEN,
1202                                      tag_name => chr ($self->{next_char}),
1203                                      line => $l, column => $c};
1204            $self->{state} = TAG_NAME_STATE;
1205            !!!next-input-character;
1206            redo A;
1207          } elsif ($self->{next_char} == 0x003E) { # >
1208            !!!cp (31);
1209            !!!parse-error (type => 'empty end tag',
1210                            line => $self->{line_prev}, ## "<" in "</>"
1211                            column => $self->{column_prev} - 1);
1212            $self->{state} = DATA_STATE;
1213          !!!next-input-character;          !!!next-input-character;
1214          redo A;          redo A;
1215        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
1216                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (32);
         $self->{current_token} = {type => 'end tag',  
                           tag_name => chr ($self->{next_input_character})};  
         $self->{state} = 'tag name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         !!!parse-error (type => 'empty end tag');  
         $self->{state} = 'data';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1217          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1218          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1219          # reconsume          # reconsume
1220    
1221          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1222                      line => $l, column => $c,
1223                     });
1224    
1225          redo A;          redo A;
1226        } else {        } else {
1227            !!!cp (33);
1228          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1229          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1230          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1231          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1232        }                                    column => $self->{column_prev} - 1,
1233      } elsif ($self->{state} eq 'tag name') {                                   };
1234        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1235            $self->{next_input_character} == 0x000A or # LF          redo A;
1236            $self->{next_input_character} == 0x000B or # VT        }
1237            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1238            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1239          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1240          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1241          redo A;            $self->{next_char} == 0x000C or # FF
1242        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1243          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1244            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1245            !!!next-input-character;
1246            redo A;
1247          } elsif ($self->{next_char} == 0x003E) { # >
1248            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1249              !!!cp (35);
1250            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1251          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1252            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1253            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1254              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1255            }            #  !!! cp (36);
1256              #  !!! parse-error (type => 'end tag attribute');
1257              #} else {
1258                !!!cp (37);
1259              #}
1260          } else {          } else {
1261            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1262          }          }
1263          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1264          !!!next-input-character;          !!!next-input-character;
1265    
1266          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1267    
1268          redo A;          redo A;
1269        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1270                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1271          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1272            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1273            # start tag or end tag            # start tag or end tag
1274          ## Stay in this state          ## Stay in this state
1275          !!!next-input-character;          !!!next-input-character;
1276          redo A;          redo A;
1277        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1278          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1279          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1280              !!!cp (39);
1281            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1282          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1283            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1284            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1285              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1286            }            #  !!! cp (40);
1287              #  !!! parse-error (type => 'end tag attribute');
1288              #} else {
1289                !!!cp (41);
1290              #}
1291          } else {          } else {
1292            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1293          }          }
1294          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1295          # reconsume          # reconsume
1296    
1297          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1298    
1299          redo A;          redo A;
1300        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1301            !!!cp (42);
1302            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1303          !!!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  
1304          redo A;          redo A;
1305        } else {        } else {
1306          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1307            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1308            # start tag or end tag            # start tag or end tag
1309          ## Stay in the state          ## Stay in the state
1310          !!!next-input-character;          !!!next-input-character;
1311          redo A;          redo A;
1312        }        }
1313      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1314        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1315            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1316            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1317            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1318            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1319            !!!cp (45);
1320          ## Stay in the state          ## Stay in the state
1321          !!!next-input-character;          !!!next-input-character;
1322          redo A;          redo A;
1323        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1324          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1325              !!!cp (46);
1326            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1327          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1328            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1329            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1330                !!!cp (47);
1331              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1332              } else {
1333                !!!cp (48);
1334            }            }
1335          } else {          } else {
1336            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1337          }          }
1338          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1339          !!!next-input-character;          !!!next-input-character;
1340    
1341          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1342    
1343          redo A;          redo A;
1344        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1345                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1346          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1347                                value => ''};          $self->{current_attribute}
1348          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1349                   value => '',
1350                   line => $self->{line}, column => $self->{column}};
1351            $self->{state} = ATTRIBUTE_NAME_STATE;
1352            !!!next-input-character;
1353            redo A;
1354          } elsif ($self->{next_char} == 0x002F) { # /
1355            !!!cp (50);
1356            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1357          !!!next-input-character;          !!!next-input-character;
1358          redo A;          redo A;
1359        } 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) {  
1360          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1361          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1362              !!!cp (52);
1363            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1364          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1365            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1366            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1367                !!!cp (53);
1368              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1369              } else {
1370                !!!cp (54);
1371            }            }
1372          } else {          } else {
1373            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1374          }          }
1375          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1376          # reconsume          # reconsume
1377    
1378          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1379    
1380          redo A;          redo A;
1381        } else {        } else {
1382          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1383                                value => ''};               0x0022 => 1, # "
1384          $self->{state} = 'attribute name';               0x0027 => 1, # '
1385                 0x003D => 1, # =
1386                }->{$self->{next_char}}) {
1387              !!!cp (55);
1388              !!!parse-error (type => 'bad attribute name');
1389            } else {
1390              !!!cp (56);
1391            }
1392            $self->{current_attribute}
1393                = {name => chr ($self->{next_char}),
1394                   value => '',
1395                   line => $self->{line}, column => $self->{column}};
1396            $self->{state} = ATTRIBUTE_NAME_STATE;
1397          !!!next-input-character;          !!!next-input-character;
1398          redo A;          redo A;
1399        }        }
1400      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1401        my $before_leave = sub {        my $before_leave = sub {
1402          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1403              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1404            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1405              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1406            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1407          } else {          } else {
1408              !!!cp (58);
1409            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1410              = $self->{current_attribute};              = $self->{current_attribute};
1411          }          }
1412        }; # $before_leave        }; # $before_leave
1413    
1414        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1415            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1416            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1417            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1418            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1419            !!!cp (59);
1420          $before_leave->();          $before_leave->();
1421          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1422          !!!next-input-character;          !!!next-input-character;
1423          redo A;          redo A;
1424        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1425            !!!cp (60);
1426          $before_leave->();          $before_leave->();
1427          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1428          !!!next-input-character;          !!!next-input-character;
1429          redo A;          redo A;
1430        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1431          $before_leave->();          $before_leave->();
1432          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1433              !!!cp (61);
1434            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1435          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1436            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
1437              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1438            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1439              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1440            }            }
1441          } else {          } else {
1442            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1443          }          }
1444          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1445          !!!next-input-character;          !!!next-input-character;
1446    
1447          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1448    
1449          redo A;          redo A;
1450        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1451                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1452          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1453            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1454          ## Stay in the state          ## Stay in the state
1455          !!!next-input-character;          !!!next-input-character;
1456          redo A;          redo A;
1457        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1458            !!!cp (64);
1459          $before_leave->();          $before_leave->();
1460            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1461          !!!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  
1462          redo A;          redo A;
1463        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1464          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1465          $before_leave->();          $before_leave->();
1466          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1467              !!!cp (66);
1468            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1469          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1470            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1471            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1472                !!!cp (67);
1473              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1474              } else {
1475                ## NOTE: This state should never be reached.
1476                !!!cp (68);
1477            }            }
1478          } else {          } else {
1479            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1480          }          }
1481          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1482          # reconsume          # reconsume
1483    
1484          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1485    
1486          redo A;          redo A;
1487        } else {        } else {
1488          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1489                $self->{next_char} == 0x0027) { # '
1490              !!!cp (69);
1491              !!!parse-error (type => 'bad attribute name');
1492            } else {
1493              !!!cp (70);
1494            }
1495            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1496          ## Stay in the state          ## Stay in the state
1497          !!!next-input-character;          !!!next-input-character;
1498          redo A;          redo A;
1499        }        }
1500      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1501        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1502            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1503            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1504            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1505            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1506            !!!cp (71);
1507          ## Stay in the state          ## Stay in the state
1508          !!!next-input-character;          !!!next-input-character;
1509          redo A;          redo A;
1510        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1511          $self->{state} = 'before attribute value';          !!!cp (72);
1512            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1513          !!!next-input-character;          !!!next-input-character;
1514          redo A;          redo A;
1515        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1516          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1517              !!!cp (73);
1518            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1519          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1520            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1521            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1522                !!!cp (74);
1523              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1524              } else {
1525                ## NOTE: This state should never be reached.
1526                !!!cp (75);
1527            }            }
1528          } else {          } else {
1529            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1530          }          }
1531          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1532          !!!next-input-character;          !!!next-input-character;
1533    
1534          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1535    
1536          redo A;          redo A;
1537        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1538                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1539          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1540                                value => ''};          $self->{current_attribute}
1541          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1542                   value => '',
1543                   line => $self->{line}, column => $self->{column}};
1544            $self->{state} = ATTRIBUTE_NAME_STATE;
1545            !!!next-input-character;
1546            redo A;
1547          } elsif ($self->{next_char} == 0x002F) { # /
1548            !!!cp (77);
1549            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1550          !!!next-input-character;          !!!next-input-character;
1551          redo A;          redo A;
1552        } 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) {  
1553          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1554          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1555              !!!cp (79);
1556            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1557          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1558            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1559            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1560                !!!cp (80);
1561              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1562              } else {
1563                ## NOTE: This state should never be reached.
1564                !!!cp (81);
1565            }            }
1566          } else {          } else {
1567            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1568          }          }
1569          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1570          # reconsume          # reconsume
1571    
1572          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1573    
1574          redo A;          redo A;
1575        } else {        } else {
1576          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{next_char} == 0x0022 or # "
1577                                value => ''};              $self->{next_char} == 0x0027) { # '
1578          $self->{state} = 'attribute name';            !!!cp (78);
1579              !!!parse-error (type => 'bad attribute name');
1580            } else {
1581              !!!cp (82);
1582            }
1583            $self->{current_attribute}
1584                = {name => chr ($self->{next_char}),
1585                   value => '',
1586                   line => $self->{line}, column => $self->{column}};
1587            $self->{state} = ATTRIBUTE_NAME_STATE;
1588          !!!next-input-character;          !!!next-input-character;
1589          redo A;                  redo A;        
1590        }        }
1591      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1592        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1593            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1594            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1595            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1596            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1597            !!!cp (83);
1598          ## Stay in the state          ## Stay in the state
1599          !!!next-input-character;          !!!next-input-character;
1600          redo A;          redo A;
1601        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1602          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1603            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1604          !!!next-input-character;          !!!next-input-character;
1605          redo A;          redo A;
1606        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1607          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1608            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1609          ## reconsume          ## reconsume
1610          redo A;          redo A;
1611        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1612          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1613            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1614          !!!next-input-character;          !!!next-input-character;
1615          redo A;          redo A;
1616        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1617          if ($self->{current_token}->{type} eq 'start tag') {          !!!parse-error (type => 'empty unquoted attribute value');
1618            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1619              !!!cp (87);
1620            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1621          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1622            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1623            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1624                !!!cp (88);
1625              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1626              } else {
1627                ## NOTE: This state should never be reached.
1628                !!!cp (89);
1629            }            }
1630          } else {          } else {
1631            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1632          }          }
1633          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1634          !!!next-input-character;          !!!next-input-character;
1635    
1636          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1637    
1638          redo A;          redo A;
1639        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1640          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1641          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1642              !!!cp (90);
1643            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1644          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1645            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1646            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1647                !!!cp (91);
1648              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1649              } else {
1650                ## NOTE: This state should never be reached.
1651                !!!cp (92);
1652            }            }
1653          } else {          } else {
1654            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1655          }          }
1656          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1657          ## reconsume          ## reconsume
1658    
1659          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1660    
1661          redo A;          redo A;
1662        } else {        } else {
1663          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1664          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1665              !!!parse-error (type => 'bad attribute value');
1666            } else {
1667              !!!cp (94);
1668            }
1669            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1670            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1671          !!!next-input-character;          !!!next-input-character;
1672          redo A;          redo A;
1673        }        }
1674      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1675        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1676          $self->{state} = 'before attribute name';          !!!cp (95);
1677            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1678          !!!next-input-character;          !!!next-input-character;
1679          redo A;          redo A;
1680        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1681          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1682          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1683            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1684          !!!next-input-character;          !!!next-input-character;
1685          redo A;          redo A;
1686        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1687          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1688          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1689              !!!cp (97);
1690            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1691          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1692            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1693            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1694                !!!cp (98);
1695              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1696              } else {
1697                ## NOTE: This state should never be reached.
1698                !!!cp (99);
1699            }            }
1700          } else {          } else {
1701            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1702          }          }
1703          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1704          ## reconsume          ## reconsume
1705    
1706          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1707    
1708          redo A;          redo A;
1709        } else {        } else {
1710          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1711            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1712          ## Stay in the state          ## Stay in the state
1713          !!!next-input-character;          !!!next-input-character;
1714          redo A;          redo A;
1715        }        }
1716      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1717        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1718          $self->{state} = 'before attribute name';          !!!cp (101);
1719            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1720          !!!next-input-character;          !!!next-input-character;
1721          redo A;          redo A;
1722        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1723          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1724          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1725            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1726          !!!next-input-character;          !!!next-input-character;
1727          redo A;          redo A;
1728        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1729          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1730          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1731              !!!cp (103);
1732            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1733          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1734            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1735            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1736                !!!cp (104);
1737              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1738              } else {
1739                ## NOTE: This state should never be reached.
1740                !!!cp (105);
1741            }            }
1742          } else {          } else {
1743            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1744          }          }
1745          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1746          ## reconsume          ## reconsume
1747    
1748          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1749    
1750          redo A;          redo A;
1751        } else {        } else {
1752          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1753            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1754          ## Stay in the state          ## Stay in the state
1755          !!!next-input-character;          !!!next-input-character;
1756          redo A;          redo A;
1757        }        }
1758      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1759        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1760            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1761            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1762            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1763            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1764          $self->{state} = 'before attribute name';          !!!cp (107);
1765          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1766          redo A;          !!!next-input-character;
1767        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1768          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1769          $self->{state} = 'entity in attribute value';          !!!cp (108);
1770          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1771          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1772        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1773          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1774          } elsif ($self->{next_char} == 0x003E) { # >
1775            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1776              !!!cp (109);
1777            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1778          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1779            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1780            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1781                !!!cp (110);
1782              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1783              } else {
1784                ## NOTE: This state should never be reached.
1785                !!!cp (111);
1786            }            }
1787          } else {          } else {
1788            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1789          }          }
1790          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1791          !!!next-input-character;          !!!next-input-character;
1792    
1793          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1794    
1795          redo A;          redo A;
1796        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1797          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1798          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1799              !!!cp (112);
1800            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1801          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1802            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1803            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1804                !!!cp (113);
1805              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1806              } else {
1807                ## NOTE: This state should never be reached.
1808                !!!cp (114);
1809            }            }
1810          } else {          } else {
1811            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1812          }          }
1813          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1814          ## reconsume          ## reconsume
1815    
1816          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1817    
1818          redo A;          redo A;
1819        } else {        } else {
1820          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1821                 0x0022 => 1, # "
1822                 0x0027 => 1, # '
1823                 0x003D => 1, # =
1824                }->{$self->{next_char}}) {
1825              !!!cp (115);
1826              !!!parse-error (type => 'bad attribute value');
1827            } else {
1828              !!!cp (116);
1829            }
1830            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1831          ## Stay in the state          ## Stay in the state
1832          !!!next-input-character;          !!!next-input-character;
1833          redo A;          redo A;
1834        }        }
1835      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1836        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity
1837              (1,
1838               $self->{last_attribute_value_state}
1839                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1840               $self->{last_attribute_value_state}
1841                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1842               -1);
1843    
1844        unless (defined $token) {        unless (defined $token) {
1845            !!!cp (117);
1846          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1847        } else {        } else {
1848            !!!cp (118);
1849          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1850            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1851          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1852        }        }
1853    
1854        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1855        # next-input-character is already done        # next-input-character is already done
1856        redo A;        redo A;
1857      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1858          if ($self->{next_char} == 0x0009 or # HT
1859              $self->{next_char} == 0x000A or # LF
1860              $self->{next_char} == 0x000B or # VT
1861              $self->{next_char} == 0x000C or # FF
1862              $self->{next_char} == 0x0020) { # SP
1863            !!!cp (118);
1864            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1865            !!!next-input-character;
1866            redo A;
1867          } elsif ($self->{next_char} == 0x003E) { # >
1868            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1869              !!!cp (119);
1870              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1871            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1872              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1873              if ($self->{current_token}->{attributes}) {
1874                !!!cp (120);
1875                !!!parse-error (type => 'end tag attribute');
1876              } else {
1877                ## NOTE: This state should never be reached.
1878                !!!cp (121);
1879              }
1880            } else {
1881              die "$0: $self->{current_token}->{type}: Unknown token type";
1882            }
1883            $self->{state} = DATA_STATE;
1884            !!!next-input-character;
1885    
1886            !!!emit ($self->{current_token}); # start tag or end tag
1887    
1888            redo A;
1889          } elsif ($self->{next_char} == 0x002F) { # /
1890            !!!cp (122);
1891            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1892            !!!next-input-character;
1893            redo A;
1894          } elsif ($self->{next_char} == -1) {
1895            !!!parse-error (type => 'unclosed tag');
1896            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1897              !!!cp (122.3);
1898              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1899            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1900              if ($self->{current_token}->{attributes}) {
1901                !!!cp (122.1);
1902                !!!parse-error (type => 'end tag attribute');
1903              } else {
1904                ## NOTE: This state should never be reached.
1905                !!!cp (122.2);
1906              }
1907            } else {
1908              die "$0: $self->{current_token}->{type}: Unknown token type";
1909            }
1910            $self->{state} = DATA_STATE;
1911            ## Reconsume.
1912            !!!emit ($self->{current_token}); # start tag or end tag
1913            redo A;
1914          } else {
1915            !!!cp ('124.1');
1916            !!!parse-error (type => 'no space between attributes');
1917            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1918            ## reconsume
1919            redo A;
1920          }
1921        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1922          if ($self->{next_char} == 0x003E) { # >
1923            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1924              !!!cp ('124.2');
1925              !!!parse-error (type => 'nestc', token => $self->{current_token});
1926              ## TODO: Different type than slash in start tag
1927              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1928              if ($self->{current_token}->{attributes}) {
1929                !!!cp ('124.4');
1930                !!!parse-error (type => 'end tag attribute');
1931              } else {
1932                !!!cp ('124.5');
1933              }
1934              ## TODO: Test |<title></title/>|
1935            } else {
1936              !!!cp ('124.3');
1937              $self->{self_closing} = 1;
1938            }
1939    
1940            $self->{state} = DATA_STATE;
1941            !!!next-input-character;
1942    
1943            !!!emit ($self->{current_token}); # start tag or end tag
1944    
1945            redo A;
1946          } elsif ($self->{next_char} == -1) {
1947            !!!parse-error (type => 'unclosed tag');
1948            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1949              !!!cp (124.7);
1950              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1951            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1952              if ($self->{current_token}->{attributes}) {
1953                !!!cp (124.5);
1954                !!!parse-error (type => 'end tag attribute');
1955              } else {
1956                ## NOTE: This state should never be reached.
1957                !!!cp (124.6);
1958              }
1959            } else {
1960              die "$0: $self->{current_token}->{type}: Unknown token type";
1961            }
1962            $self->{state} = DATA_STATE;
1963            ## Reconsume.
1964            !!!emit ($self->{current_token}); # start tag or end tag
1965            redo A;
1966          } else {
1967            !!!cp ('124.4');
1968            !!!parse-error (type => 'nestc');
1969            ## TODO: This error type is wrong.
1970            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1971            ## Reconsume.
1972            redo A;
1973          }
1974        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1975        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1976                
1977        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1978          #my $token = {type => COMMENT_TOKEN, data => ''};
1979    
1980        BC: {        BC: {
1981          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1982            $self->{state} = 'data';            !!!cp (124);
1983              $self->{state} = DATA_STATE;
1984            !!!next-input-character;            !!!next-input-character;
1985    
1986            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1987    
1988            redo A;            redo A;
1989          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1990            $self->{state} = 'data';            !!!cp (125);
1991              $self->{state} = DATA_STATE;
1992            ## reconsume            ## reconsume
1993    
1994            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1995    
1996            redo A;            redo A;
1997          } else {          } else {
1998            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1999              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2000            !!!next-input-character;            !!!next-input-character;
2001            redo BC;            redo BC;
2002          }          }
2003        } # BC        } # BC
     } elsif ($self->{state} eq 'markup declaration open') {  
       ## (only happen if PCDATA state)  
2004    
2005        my @next_char;        die "$0: _get_next_token: unexpected case [BC]";
2006        push @next_char, $self->{next_input_character};      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2007          ## (only happen if PCDATA state)
2008                
2009        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2010          !!!next-input-character;          !!!cp (133);
2011          push @next_char, $self->{next_input_character};          $self->{state} = MD_HYPHEN_STATE;
2012          if ($self->{next_input_character} == 0x002D) { # -          !!!next-input-character;
2013            $self->{current_token} = {type => 'comment', data => ''};          redo A;
2014            $self->{state} = 'comment';        } elsif ($self->{next_char} == 0x0044 or # D
2015            !!!next-input-character;                 $self->{next_char} == 0x0064) { # d
2016            redo A;          ## ASCII case-insensitive.
2017          }          !!!cp (130);
2018        } elsif ($self->{next_input_character} == 0x0044 or # D          $self->{state} = MD_DOCTYPE_STATE;
2019                 $self->{next_input_character} == 0x0064) { # d          $self->{state_keyword} = chr $self->{next_char};
2020            !!!next-input-character;
2021            redo A;
2022          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2023                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2024                   $self->{next_char} == 0x005B) { # [
2025            !!!cp (135.4);                
2026            $self->{state} = MD_CDATA_STATE;
2027            $self->{state_keyword} = '[';
2028          !!!next-input-character;          !!!next-input-character;
2029          push @next_char, $self->{next_input_character};          redo A;
2030          if ($self->{next_input_character} == 0x004F or # O        } else {
2031              $self->{next_input_character} == 0x006F) { # o          !!!cp (136);
           !!!next-input-character;  
           push @next_char, $self->{next_input_character};  
           if ($self->{next_input_character} == 0x0043 or # C  
               $self->{next_input_character} == 0x0063) { # c  
             !!!next-input-character;  
             push @next_char, $self->{next_input_character};  
             if ($self->{next_input_character} == 0x0054 or # T  
                 $self->{next_input_character} == 0x0074) { # t  
               !!!next-input-character;  
               push @next_char, $self->{next_input_character};  
               if ($self->{next_input_character} == 0x0059 or # Y  
                   $self->{next_input_character} == 0x0079) { # y  
                 !!!next-input-character;  
                 push @next_char, $self->{next_input_character};  
                 if ($self->{next_input_character} == 0x0050 or # P  
                     $self->{next_input_character} == 0x0070) { # p  
                   !!!next-input-character;  
                   push @next_char, $self->{next_input_character};  
                   if ($self->{next_input_character} == 0x0045 or # E  
                       $self->{next_input_character} == 0x0065) { # e  
                     ## ISSUE: What a stupid code this is!  
                     $self->{state} = 'DOCTYPE';  
                     !!!next-input-character;  
                     redo A;  
                   }  
                 }  
               }  
             }  
           }  
         }  
2032        }        }
2033    
2034        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment',
2035        $self->{next_input_character} = shift @next_char;                        line => $self->{line_prev},
2036        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2037        $self->{state} = 'bogus comment';        ## Reconsume.
2038          $self->{state} = BOGUS_COMMENT_STATE;
2039          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2040                                    line => $self->{line_prev},
2041                                    column => $self->{column_prev} - 1,
2042                                   };
2043        redo A;        redo A;
2044              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2045        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2046        ## 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);
2047      } elsif ($self->{state} eq 'comment') {          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2048        if ($self->{next_input_character} == 0x002D) { # -                                    line => $self->{line_prev},
2049          $self->{state} = 'comment dash';                                    column => $self->{column_prev} - 2,
2050                                     };
2051            $self->{state} = COMMENT_START_STATE;
2052            !!!next-input-character;
2053            redo A;
2054          } else {
2055            !!!cp (128);
2056            !!!parse-error (type => 'bogus comment',
2057                            line => $self->{line_prev},
2058                            column => $self->{column_prev} - 2);
2059            $self->{state} = BOGUS_COMMENT_STATE;
2060            ## Reconsume.
2061            $self->{current_token} = {type => COMMENT_TOKEN,
2062                                      data => '-',
2063                                      line => $self->{line_prev},
2064                                      column => $self->{column_prev} - 2,
2065                                     };
2066            redo A;
2067          }
2068        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2069          ## ASCII case-insensitive.
2070          if ($self->{next_char} == [
2071                undef,
2072                0x004F, # O
2073                0x0043, # C
2074                0x0054, # T
2075                0x0059, # Y
2076                0x0050, # P
2077              ]->[length $self->{state_keyword}] or
2078              $self->{next_char} == [
2079                undef,
2080                0x006F, # o
2081                0x0063, # c
2082                0x0074, # t
2083                0x0079, # y
2084                0x0070, # p
2085              ]->[length $self->{state_keyword}]) {
2086            !!!cp (131);
2087            ## Stay in the state.
2088            $self->{state_keyword} .= chr $self->{next_char};
2089            !!!next-input-character;
2090            redo A;
2091          } elsif ((length $self->{state_keyword}) == 6 and
2092                   ($self->{next_char} == 0x0045 or # E
2093                    $self->{next_char} == 0x0065)) { # e
2094            !!!cp (129);
2095            $self->{state} = DOCTYPE_STATE;
2096            $self->{current_token} = {type => DOCTYPE_TOKEN,
2097                                      quirks => 1,
2098                                      line => $self->{line_prev},
2099                                      column => $self->{column_prev} - 7,
2100                                     };
2101          !!!next-input-character;          !!!next-input-character;
2102          redo A;          redo A;
2103        } elsif ($self->{next_input_character} == -1) {        } else {
2104            !!!cp (132);        
2105            !!!parse-error (type => 'bogus comment',
2106                            line => $self->{line_prev},
2107                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2108            $self->{state} = BOGUS_COMMENT_STATE;
2109            ## Reconsume.
2110            $self->{current_token} = {type => COMMENT_TOKEN,
2111                                      data => $self->{state_keyword},
2112                                      line => $self->{line_prev},
2113                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2114                                     };
2115            redo A;
2116          }
2117        } elsif ($self->{state} == MD_CDATA_STATE) {
2118          if ($self->{next_char} == {
2119                '[' => 0x0043, # C
2120                '[C' => 0x0044, # D
2121                '[CD' => 0x0041, # A
2122                '[CDA' => 0x0054, # T
2123                '[CDAT' => 0x0041, # A
2124              }->{$self->{state_keyword}}) {
2125            !!!cp (135.1);
2126            ## Stay in the state.
2127            $self->{state_keyword} .= chr $self->{next_char};
2128            !!!next-input-character;
2129            redo A;
2130          } elsif ($self->{state_keyword} eq '[CDATA' and
2131                   $self->{next_char} == 0x005B) { # [
2132            !!!cp (135.2);
2133            $self->{state} = CDATA_BLOCK_STATE;
2134            !!!next-input-character;
2135            redo A;
2136          } else {
2137            !!!cp (135.3);
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} == COMMENT_START_STATE) {
2151          if ($self->{next_char} == 0x002D) { # -
2152            !!!cp (137);
2153            $self->{state} = COMMENT_START_DASH_STATE;
2154            !!!next-input-character;
2155            redo A;
2156          } elsif ($self->{next_char} == 0x003E) { # >
2157            !!!cp (138);
2158            !!!parse-error (type => 'bogus comment');
2159            $self->{state} = DATA_STATE;
2160            !!!next-input-character;
2161    
2162            !!!emit ($self->{current_token}); # comment
2163    
2164            redo A;
2165          } elsif ($self->{next_char} == -1) {
2166            !!!cp (139);
2167          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2168          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2169          ## reconsume          ## reconsume
2170    
2171          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2172    
2173          redo A;          redo A;
2174        } else {        } else {
2175          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (140);
2176            $self->{current_token}->{data} # comment
2177                .= chr ($self->{next_char});
2178            $self->{state} = COMMENT_STATE;
2179            !!!next-input-character;
2180            redo A;
2181          }
2182        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2183          if ($self->{next_char} == 0x002D) { # -
2184            !!!cp (141);
2185            $self->{state} = COMMENT_END_STATE;
2186            !!!next-input-character;
2187            redo A;
2188          } elsif ($self->{next_char} == 0x003E) { # >
2189            !!!cp (142);
2190            !!!parse-error (type => 'bogus comment');
2191            $self->{state} = DATA_STATE;
2192            !!!next-input-character;
2193    
2194            !!!emit ($self->{current_token}); # comment
2195    
2196            redo A;
2197          } elsif ($self->{next_char} == -1) {
2198            !!!cp (143);
2199            !!!parse-error (type => 'unclosed comment');
2200            $self->{state} = DATA_STATE;
2201            ## reconsume
2202    
2203            !!!emit ($self->{current_token}); # comment
2204    
2205            redo A;
2206          } else {
2207            !!!cp (144);
2208            $self->{current_token}->{data} # comment
2209                .= '-' . chr ($self->{next_char});
2210            $self->{state} = COMMENT_STATE;
2211            !!!next-input-character;
2212            redo A;
2213          }
2214        } elsif ($self->{state} == COMMENT_STATE) {
2215          if ($self->{next_char} == 0x002D) { # -
2216            !!!cp (145);
2217            $self->{state} = COMMENT_END_DASH_STATE;
2218            !!!next-input-character;
2219            redo A;
2220          } elsif ($self->{next_char} == -1) {
2221            !!!cp (146);
2222            !!!parse-error (type => 'unclosed comment');
2223            $self->{state} = DATA_STATE;
2224            ## reconsume
2225    
2226            !!!emit ($self->{current_token}); # comment
2227    
2228            redo A;
2229          } else {
2230            !!!cp (147);
2231            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2232          ## Stay in the state          ## Stay in the state
2233          !!!next-input-character;          !!!next-input-character;
2234          redo A;          redo A;
2235        }        }
2236      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2237        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2238          $self->{state} = 'comment end';          !!!cp (148);
2239            $self->{state} = COMMENT_END_STATE;
2240          !!!next-input-character;          !!!next-input-character;
2241          redo A;          redo A;
2242        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2243            !!!cp (149);
2244          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2245          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2246          ## reconsume          ## reconsume
2247    
2248          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2249    
2250          redo A;          redo A;
2251        } else {        } else {
2252          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2253          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2254            $self->{state} = COMMENT_STATE;
2255          !!!next-input-character;          !!!next-input-character;
2256          redo A;          redo A;
2257        }        }
2258      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2259        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2260          $self->{state} = 'data';          !!!cp (151);
2261            $self->{state} = DATA_STATE;
2262          !!!next-input-character;          !!!next-input-character;
2263    
2264          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2265    
2266          redo A;          redo A;
2267        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2268          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2269            !!!parse-error (type => 'dash in comment',
2270                            line => $self->{line_prev},
2271                            column => $self->{column_prev});
2272          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2273          ## Stay in the state          ## Stay in the state
2274          !!!next-input-character;          !!!next-input-character;
2275          redo A;          redo A;
2276        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2277            !!!cp (153);
2278          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2279          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2280          ## reconsume          ## reconsume
2281    
2282          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2283    
2284          redo A;          redo A;
2285        } else {        } else {
2286          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2287          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2288          $self->{state} = 'comment';                          line => $self->{line_prev},
2289                            column => $self->{column_prev});
2290            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2291            $self->{state} = COMMENT_STATE;
2292          !!!next-input-character;          !!!next-input-character;
2293          redo A;          redo A;
2294        }        }
2295      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2296        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2297            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2298            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2299            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2300            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2301          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2302            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2303          !!!next-input-character;          !!!next-input-character;
2304          redo A;          redo A;
2305        } else {        } else {
2306            !!!cp (156);
2307          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2308          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2309          ## reconsume          ## reconsume
2310          redo A;          redo A;
2311        }        }
2312      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2313        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2314            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2315            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2316            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2317            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2318            !!!cp (157);
2319          ## Stay in the state          ## Stay in the state
2320          !!!next-input-character;          !!!next-input-character;
2321          redo A;          redo A;
2322        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
2323                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (158);
2324          $self->{current_token} = {type => 'DOCTYPE',          !!!parse-error (type => 'no DOCTYPE name');
2325                            name => chr ($self->{next_input_character} - 0x0020),          $self->{state} = DATA_STATE;
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
2326          !!!next-input-character;          !!!next-input-character;
2327    
2328            !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2329    
2330          redo A;          redo A;
2331        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == -1) {
2332            !!!cp (159);
2333          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2334          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2335            ## reconsume
2336    
2337            !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2338    
2339            redo A;
2340          } else {
2341            !!!cp (160);
2342            $self->{current_token}->{name} = chr $self->{next_char};
2343            delete $self->{current_token}->{quirks};
2344    ## ISSUE: "Set the token's name name to the" in the spec
2345            $self->{state} = DOCTYPE_NAME_STATE;
2346            !!!next-input-character;
2347            redo A;
2348          }
2349        } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2350    ## ISSUE: Redundant "First," in the spec.
2351          if ($self->{next_char} == 0x0009 or # HT
2352              $self->{next_char} == 0x000A or # LF
2353              $self->{next_char} == 0x000B or # VT
2354              $self->{next_char} == 0x000C or # FF
2355              $self->{next_char} == 0x0020) { # SP
2356            !!!cp (161);
2357            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2358            !!!next-input-character;
2359            redo A;
2360          } elsif ($self->{next_char} == 0x003E) { # >
2361            !!!cp (162);
2362            $self->{state} = DATA_STATE;
2363          !!!next-input-character;          !!!next-input-character;
2364    
2365          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE
2366    
2367          redo A;          redo A;
2368        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2369          !!!parse-error (type => 'no DOCTYPE name');          !!!cp (163);
2370          $self->{state} = 'data';          !!!parse-error (type => 'unclosed DOCTYPE');
2371            $self->{state} = DATA_STATE;
2372            ## reconsume
2373    
2374            $self->{current_token}->{quirks} = 1;
2375            !!!emit ($self->{current_token}); # DOCTYPE
2376    
2377            redo A;
2378          } else {
2379            !!!cp (164);
2380            $self->{current_token}->{name}
2381              .= chr ($self->{next_char}); # DOCTYPE
2382            ## Stay in the state
2383            !!!next-input-character;
2384            redo A;
2385          }
2386        } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2387          if ($self->{next_char} == 0x0009 or # HT
2388              $self->{next_char} == 0x000A or # LF
2389              $self->{next_char} == 0x000B or # VT
2390              $self->{next_char} == 0x000C or # FF
2391              $self->{next_char} == 0x0020) { # SP
2392            !!!cp (165);
2393            ## Stay in the state
2394            !!!next-input-character;
2395            redo A;
2396          } elsif ($self->{next_char} == 0x003E) { # >
2397            !!!cp (166);
2398            $self->{state} = DATA_STATE;
2399            !!!next-input-character;
2400    
2401            !!!emit ($self->{current_token}); # DOCTYPE
2402    
2403            redo A;
2404          } elsif ($self->{next_char} == -1) {
2405            !!!cp (167);
2406            !!!parse-error (type => 'unclosed DOCTYPE');
2407            $self->{state} = DATA_STATE;
2408            ## reconsume
2409    
2410            $self->{current_token}->{quirks} = 1;
2411            !!!emit ($self->{current_token}); # DOCTYPE
2412    
2413            redo A;
2414          } elsif ($self->{next_char} == 0x0050 or # P
2415                   $self->{next_char} == 0x0070) { # p
2416            !!!next-input-character;
2417            if ($self->{next_char} == 0x0055 or # U
2418                $self->{next_char} == 0x0075) { # u
2419              !!!next-input-character;
2420              if ($self->{next_char} == 0x0042 or # B
2421                  $self->{next_char} == 0x0062) { # b
2422                !!!next-input-character;
2423                if ($self->{next_char} == 0x004C or # L
2424                    $self->{next_char} == 0x006C) { # l
2425                  !!!next-input-character;
2426                  if ($self->{next_char} == 0x0049 or # I
2427                      $self->{next_char} == 0x0069) { # i
2428                    !!!next-input-character;
2429                    if ($self->{next_char} == 0x0043 or # C
2430                        $self->{next_char} == 0x0063) { # c
2431                      !!!cp (168);
2432                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2433                      !!!next-input-character;
2434                      redo A;
2435                    } else {
2436                      !!!cp (169);
2437                    }
2438                  } else {
2439                    !!!cp (170);
2440                  }
2441                } else {
2442                  !!!cp (171);
2443                }
2444              } else {
2445                !!!cp (172);
2446              }
2447            } else {
2448              !!!cp (173);
2449            }
2450    
2451            #
2452          } elsif ($self->{next_char} == 0x0053 or # S
2453                   $self->{next_char} == 0x0073) { # s
2454            !!!next-input-character;
2455            if ($self->{next_char} == 0x0059 or # Y
2456                $self->{next_char} == 0x0079) { # y
2457              !!!next-input-character;
2458              if ($self->{next_char} == 0x0053 or # S
2459                  $self->{next_char} == 0x0073) { # s
2460                !!!next-input-character;
2461                if ($self->{next_char} == 0x0054 or # T
2462                    $self->{next_char} == 0x0074) { # t
2463                  !!!next-input-character;
2464                  if ($self->{next_char} == 0x0045 or # E
2465                      $self->{next_char} == 0x0065) { # e
2466                    !!!next-input-character;
2467                    if ($self->{next_char} == 0x004D or # M
2468                        $self->{next_char} == 0x006D) { # m
2469                      !!!cp (174);
2470                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2471                      !!!next-input-character;
2472                      redo A;
2473                    } else {
2474                      !!!cp (175);
2475                    }
2476                  } else {
2477                    !!!cp (176);
2478                  }
2479                } else {
2480                  !!!cp (177);
2481                }
2482              } else {
2483                !!!cp (178);
2484              }
2485            } else {
2486              !!!cp (179);
2487            }
2488    
2489            #
2490          } else {
2491            !!!cp (180);
2492            !!!next-input-character;
2493            #
2494          }
2495    
2496          !!!parse-error (type => 'string after DOCTYPE name');
2497          $self->{current_token}->{quirks} = 1;
2498    
2499          $self->{state} = BOGUS_DOCTYPE_STATE;
2500          # next-input-character is already done
2501          redo A;
2502        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2503          if ({
2504                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2505                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2506              }->{$self->{next_char}}) {
2507            !!!cp (181);
2508            ## Stay in the state
2509            !!!next-input-character;
2510            redo A;
2511          } elsif ($self->{next_char} eq 0x0022) { # "
2512            !!!cp (182);
2513            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2514            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2515            !!!next-input-character;
2516            redo A;
2517          } elsif ($self->{next_char} eq 0x0027) { # '
2518            !!!cp (183);
2519            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2520            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2521            !!!next-input-character;
2522            redo A;
2523          } elsif ($self->{next_char} eq 0x003E) { # >
2524            !!!cp (184);
2525            !!!parse-error (type => 'no PUBLIC literal');
2526    
2527            $self->{state} = DATA_STATE;
2528            !!!next-input-character;
2529    
2530            $self->{current_token}->{quirks} = 1;
2531            !!!emit ($self->{current_token}); # DOCTYPE
2532    
2533            redo A;
2534          } elsif ($self->{next_char} == -1) {
2535            !!!cp (185);
2536            !!!parse-error (type => 'unclosed DOCTYPE');
2537    
2538            $self->{state} = DATA_STATE;
2539            ## reconsume
2540    
2541            $self->{current_token}->{quirks} = 1;
2542            !!!emit ($self->{current_token}); # DOCTYPE
2543    
2544            redo A;
2545          } else {
2546            !!!cp (186);
2547            !!!parse-error (type => 'string after PUBLIC');
2548            $self->{current_token}->{quirks} = 1;
2549    
2550            $self->{state} = BOGUS_DOCTYPE_STATE;
2551            !!!next-input-character;
2552            redo A;
2553          }
2554        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2555          if ($self->{next_char} == 0x0022) { # "
2556            !!!cp (187);
2557            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2558            !!!next-input-character;
2559            redo A;
2560          } elsif ($self->{next_char} == 0x003E) { # >
2561            !!!cp (188);
2562            !!!parse-error (type => 'unclosed PUBLIC literal');
2563    
2564            $self->{state} = DATA_STATE;
2565            !!!next-input-character;
2566    
2567            $self->{current_token}->{quirks} = 1;
2568            !!!emit ($self->{current_token}); # DOCTYPE
2569    
2570            redo A;
2571          } elsif ($self->{next_char} == -1) {
2572            !!!cp (189);
2573            !!!parse-error (type => 'unclosed PUBLIC literal');
2574    
2575            $self->{state} = DATA_STATE;
2576          ## reconsume          ## reconsume
2577    
2578          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          $self->{current_token}->{quirks} = 1;
2579            !!!emit ($self->{current_token}); # DOCTYPE
2580    
2581          redo A;          redo A;
2582        } else {        } else {
2583          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (190);
2584                            name => chr ($self->{next_input_character}),          $self->{current_token}->{public_identifier} # DOCTYPE
2585                            error => 1};              .= chr $self->{next_char};
2586          $self->{state} = 'DOCTYPE name';          ## Stay in the state
2587          !!!next-input-character;          !!!next-input-character;
2588          redo A;          redo A;
2589        }        }
2590      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2591        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0027) { # '
2592            $self->{next_input_character} == 0x000A or # LF          !!!cp (191);
2593            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
         $self->{state} = 'after DOCTYPE name';  
2594          !!!next-input-character;          !!!next-input-character;
2595          redo A;          redo A;
2596        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2597          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (192);
2598          $self->{state} = 'data';          !!!parse-error (type => 'unclosed PUBLIC literal');
2599    
2600            $self->{state} = DATA_STATE;
2601          !!!next-input-character;          !!!next-input-character;
2602    
2603            $self->{current_token}->{quirks} = 1;
2604          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2605    
2606          redo A;          redo A;
2607        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
2608                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (193);
2609          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed PUBLIC literal');
2610          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
2611            $self->{state} = DATA_STATE;
2612            ## reconsume
2613    
2614            $self->{current_token}->{quirks} = 1;
2615            !!!emit ($self->{current_token}); # DOCTYPE
2616    
2617            redo A;
2618          } else {
2619            !!!cp (194);
2620            $self->{current_token}->{public_identifier} # DOCTYPE
2621                .= chr $self->{next_char};
2622          ## Stay in the state          ## Stay in the state
2623          !!!next-input-character;          !!!next-input-character;
2624          redo A;          redo A;
2625        } elsif ($self->{next_input_character} == -1) {        }
2626        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2627          if ({
2628                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2629                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2630              }->{$self->{next_char}}) {
2631            !!!cp (195);
2632            ## Stay in the state
2633            !!!next-input-character;
2634            redo A;
2635          } elsif ($self->{next_char} == 0x0022) { # "
2636            !!!cp (196);
2637            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2638            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2639            !!!next-input-character;
2640            redo A;
2641          } elsif ($self->{next_char} == 0x0027) { # '
2642            !!!cp (197);
2643            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2644            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2645            !!!next-input-character;
2646            redo A;
2647          } elsif ($self->{next_char} == 0x003E) { # >
2648            !!!cp (198);
2649            $self->{state} = DATA_STATE;
2650            !!!next-input-character;
2651    
2652            !!!emit ($self->{current_token}); # DOCTYPE
2653    
2654            redo A;
2655          } elsif ($self->{next_char} == -1) {
2656            !!!cp (199);
2657          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2658          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
2659          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2660          ## reconsume          ## reconsume
2661    
2662          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
2663          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
2664    
2665          redo A;          redo A;
2666        } else {        } else {
2667          $self->{current_token}->{name}          !!!cp (200);
2668            .= chr ($self->{next_input_character}); # DOCTYPE          !!!parse-error (type => 'string after PUBLIC literal');
2669          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{current_token}->{quirks} = 1;
2670    
2671            $self->{state} = BOGUS_DOCTYPE_STATE;
2672            !!!next-input-character;
2673            redo A;
2674          }
2675        } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2676          if ({
2677                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2678                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2679              }->{$self->{next_char}}) {
2680            !!!cp (201);
2681            ## Stay in the state
2682            !!!next-input-character;
2683            redo A;
2684          } elsif ($self->{next_char} == 0x0022) { # "
2685            !!!cp (202);
2686            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2687            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2688            !!!next-input-character;
2689            redo A;
2690          } elsif ($self->{next_char} == 0x0027) { # '
2691            !!!cp (203);
2692            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2693            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2694            !!!next-input-character;
2695            redo A;
2696          } elsif ($self->{next_char} == 0x003E) { # >
2697            !!!cp (204);
2698            !!!parse-error (type => 'no SYSTEM literal');
2699            $self->{state} = DATA_STATE;
2700            !!!next-input-character;
2701    
2702            $self->{current_token}->{quirks} = 1;
2703            !!!emit ($self->{current_token}); # DOCTYPE
2704    
2705            redo A;
2706          } elsif ($self->{next_char} == -1) {
2707            !!!cp (205);
2708            !!!parse-error (type => 'unclosed DOCTYPE');
2709    
2710            $self->{state} = DATA_STATE;
2711            ## reconsume
2712    
2713            $self->{current_token}->{quirks} = 1;
2714            !!!emit ($self->{current_token}); # DOCTYPE
2715    
2716            redo A;
2717          } else {
2718            !!!cp (206);
2719            !!!parse-error (type => 'string after SYSTEM');
2720            $self->{current_token}->{quirks} = 1;
2721    
2722            $self->{state} = BOGUS_DOCTYPE_STATE;
2723            !!!next-input-character;
2724            redo A;
2725          }
2726        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2727          if ($self->{next_char} == 0x0022) { # "
2728            !!!cp (207);
2729            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2730            !!!next-input-character;
2731            redo A;
2732          } elsif ($self->{next_char} == 0x003E) { # >
2733            !!!cp (208);
2734            !!!parse-error (type => 'unclosed SYSTEM literal');
2735    
2736            $self->{state} = DATA_STATE;
2737            !!!next-input-character;
2738    
2739            $self->{current_token}->{quirks} = 1;
2740            !!!emit ($self->{current_token}); # DOCTYPE
2741    
2742            redo A;
2743          } elsif ($self->{next_char} == -1) {
2744            !!!cp (209);
2745            !!!parse-error (type => 'unclosed SYSTEM literal');
2746    
2747            $self->{state} = DATA_STATE;
2748            ## reconsume
2749    
2750            $self->{current_token}->{quirks} = 1;
2751            !!!emit ($self->{current_token}); # DOCTYPE
2752    
2753            redo A;
2754          } else {
2755            !!!cp (210);
2756            $self->{current_token}->{system_identifier} # DOCTYPE
2757                .= chr $self->{next_char};
2758            ## Stay in the state
2759            !!!next-input-character;
2760            redo A;
2761          }
2762        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2763          if ($self->{next_char} == 0x0027) { # '
2764            !!!cp (211);
2765            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2766            !!!next-input-character;
2767            redo A;
2768          } elsif ($self->{next_char} == 0x003E) { # >
2769            !!!cp (212);
2770            !!!parse-error (type => 'unclosed SYSTEM literal');
2771    
2772            $self->{state} = DATA_STATE;
2773            !!!next-input-character;
2774    
2775            $self->{current_token}->{quirks} = 1;
2776            !!!emit ($self->{current_token}); # DOCTYPE
2777    
2778            redo A;
2779          } elsif ($self->{next_char} == -1) {
2780            !!!cp (213);
2781            !!!parse-error (type => 'unclosed SYSTEM literal');
2782    
2783            $self->{state} = DATA_STATE;
2784            ## reconsume
2785    
2786            $self->{current_token}->{quirks} = 1;
2787            !!!emit ($self->{current_token}); # DOCTYPE
2788    
2789            redo A;
2790          } else {
2791            !!!cp (214);
2792            $self->{current_token}->{system_identifier} # DOCTYPE
2793                .= chr $self->{next_char};
2794          ## Stay in the state          ## Stay in the state
2795          !!!next-input-character;          !!!next-input-character;
2796          redo A;          redo A;
2797        }        }
2798      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2799        if ($self->{next_input_character} == 0x0009 or # HT        if ({
2800            $self->{next_input_character} == 0x000A or # LF              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2801            $self->{next_input_character} == 0x000B or # VT              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2802            $self->{next_input_character} == 0x000C or # FF            }->{$self->{next_char}}) {
2803            $self->{next_input_character} == 0x0020) { # SP          !!!cp (215);
2804          ## Stay in the state          ## Stay in the state
2805          !!!next-input-character;          !!!next-input-character;
2806          redo A;          redo A;
2807        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2808          $self->{state} = 'data';          !!!cp (216);
2809            $self->{state} = DATA_STATE;
2810          !!!next-input-character;          !!!next-input-character;
2811    
2812          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2813    
2814          redo A;          redo A;
2815        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2816            !!!cp (217);
2817          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2818          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2819          ## reconsume          ## reconsume
2820    
2821            $self->{current_token}->{quirks} = 1;
2822          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2823    
2824          redo A;          redo A;
2825        } else {        } else {
2826          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (218);
2827          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after SYSTEM literal');
2828          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2829    
2830            $self->{state} = BOGUS_DOCTYPE_STATE;
2831          !!!next-input-character;          !!!next-input-character;
2832          redo A;          redo A;
2833        }        }
2834      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2835        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2836          $self->{state} = 'data';          !!!cp (219);
2837            $self->{state} = DATA_STATE;
2838          !!!next-input-character;          !!!next-input-character;
2839    
2840          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2841    
2842          redo A;          redo A;
2843        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2844            !!!cp (220);
2845          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2846          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2847          ## reconsume          ## reconsume
2848    
2849          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2850    
2851          redo A;          redo A;
2852        } else {        } else {
2853            !!!cp (221);
2854          ## Stay in the state          ## Stay in the state
2855          !!!next-input-character;          !!!next-input-character;
2856          redo A;          redo A;
2857        }        }
2858        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2859          my $s = '';
2860          
2861          my ($l, $c) = ($self->{line}, $self->{column});
2862    
2863          CS: while ($self->{next_char} != -1) {
2864            if ($self->{next_char} == 0x005D) { # ]
2865              !!!next-input-character;
2866              if ($self->{next_char} == 0x005D) { # ]
2867                !!!next-input-character;
2868                MDC: {
2869                  if ($self->{next_char} == 0x003E) { # >
2870                    !!!cp (221.1);
2871                    !!!next-input-character;
2872                    last CS;
2873                  } elsif ($self->{next_char} == 0x005D) { # ]
2874                    !!!cp (221.2);
2875                    $s .= ']';
2876                    !!!next-input-character;
2877                    redo MDC;
2878                  } else {
2879                    !!!cp (221.3);
2880                    $s .= ']]';
2881                    #
2882                  }
2883                } # MDC
2884              } else {
2885                !!!cp (221.4);
2886                $s .= ']';
2887                #
2888              }
2889            } else {
2890              !!!cp (221.5);
2891              #
2892            }
2893            $s .= chr $self->{next_char};
2894            !!!next-input-character;
2895          } # CS
2896    
2897          $self->{state} = DATA_STATE;
2898          ## next-input-character done or EOF, which is reconsumed.
2899    
2900          if (length $s) {
2901            !!!cp (221.6);
2902            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2903                      line => $l, column => $c});
2904          } else {
2905            !!!cp (221.7);
2906          }
2907    
2908          redo A;
2909    
2910          ## ISSUE: "text tokens" in spec.
2911          ## TODO: Streaming support
2912      } else {      } else {
2913        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2914      }      }
# Line 1449  sub _get_next_token ($) { Line 2917  sub _get_next_token ($) {
2917    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2918  } # _get_next_token  } # _get_next_token
2919    
2920  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2921    my $self = shift;    my ($self, $in_attr, $additional) = @_;
2922      
2923    if ($self->{next_input_character} == 0x0023) { # #    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2924    
2925      if ({
2926           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2927           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2928           $additional => 1,
2929          }->{$self->{next_char}}) {
2930        !!!cp (1001);
2931        ## Don't consume
2932        ## No error
2933        return undef;
2934      } elsif ($self->{next_char} == 0x0023) { # #
2935      !!!next-input-character;      !!!next-input-character;
2936      my $num;      if ($self->{next_char} == 0x0078 or # x
2937      if ($self->{next_input_character} == 0x0078 or # x          $self->{next_char} == 0x0058) { # X
2938          $self->{next_input_character} == 0x0058) { # X        my $code;
2939        X: {        X: {
2940          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2941          !!!next-input-character;          !!!next-input-character;
2942          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2943              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2944            $num ||= 0;            !!!cp (1002);
2945            $num *= 0x10;            $code ||= 0;
2946            $num += $self->{next_input_character} - 0x0030;            $code *= 0x10;
2947              $code += $self->{next_char} - 0x0030;
2948            redo X;            redo X;
2949          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2950                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2951            ## ISSUE: the spec says U+0078, which is apparently incorrect            !!!cp (1003);
2952            $num ||= 0;            $code ||= 0;
2953            $num *= 0x10;            $code *= 0x10;
2954            $num += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2955            redo X;            redo X;
2956          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2957                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2958            ## ISSUE: the spec says U+0058, which is apparently incorrect            !!!cp (1004);
2959            $num ||= 0;            $code ||= 0;
2960            $num *= 0x10;            $code *= 0x10;
2961            $num += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2962            redo X;            redo X;
2963          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2964            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2965            $self->{next_input_character} = 0x0023; # #            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2966            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char, $self->{next_char});
2967              $self->{next_char} = 0x0023; # #
2968            return undef;            return undef;
2969          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2970              !!!cp (1006);
2971            !!!next-input-character;            !!!next-input-character;
2972          } else {          } else {
2973            !!!parse-error (type => 'no refc');            !!!cp (1007);
2974          }            !!!parse-error (type => 'no refc', line => $l, column => $c);
   
         ## TODO: check the definition for |a valid Unicode character|.  
         if ($num > 1114111 or $num == 0) {  
           $num = 0xFFFD; # REPLACEMENT CHARACTER  
           ## ISSUE: Why this is not an error?  
2975          }          }
2976    
2977          return {type => 'character', data => chr $num};          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2978              !!!cp (1008);
2979              !!!parse-error (type => 'invalid character reference',
2980                              text => (sprintf 'U+%04X', $code),
2981                              line => $l, column => $c);
2982              $code = 0xFFFD;
2983            } elsif ($code > 0x10FFFF) {
2984              !!!cp (1009);
2985              !!!parse-error (type => 'invalid character reference',
2986                              text => (sprintf 'U-%08X', $code),
2987                              line => $l, column => $c);
2988              $code = 0xFFFD;
2989            } elsif ($code == 0x000D) {
2990              !!!cp (1010);
2991              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2992              $code = 0x000A;
2993            } elsif (0x80 <= $code and $code <= 0x9F) {
2994              !!!cp (1011);
2995              !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
2996              $code = $c1_entity_char->{$code};
2997            }
2998    
2999            return {type => CHARACTER_TOKEN, data => chr $code,
3000                    has_reference => 1,
3001                    line => $l, column => $c,
3002                   };
3003        } # X        } # X
3004      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
3005               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
3006        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
3007        !!!next-input-character;        !!!next-input-character;
3008                
3009        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
3010                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
3011            !!!cp (1012);
3012          $code *= 10;          $code *= 10;
3013          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
3014                    
3015          !!!next-input-character;          !!!next-input-character;
3016        }        }
3017    
3018        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
3019            !!!cp (1013);
3020          !!!next-input-character;          !!!next-input-character;
3021        } else {        } else {
3022          !!!parse-error (type => 'no refc');          !!!cp (1014);
3023            !!!parse-error (type => 'no refc', line => $l, column => $c);
3024        }        }
3025    
3026        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3027        if ($code > 1114111 or $code == 0) {          !!!cp (1015);
3028          $code = 0xFFFD; # REPLACEMENT CHARACTER          !!!parse-error (type => 'invalid character reference',
3029          ## ISSUE: Why this is not an error?                          text => (sprintf 'U+%04X', $code),
3030                            line => $l, column => $c);
3031            $code = 0xFFFD;
3032          } elsif ($code > 0x10FFFF) {
3033            !!!cp (1016);
3034            !!!parse-error (type => 'invalid character reference',
3035                            text => (sprintf 'U-%08X', $code),
3036                            line => $l, column => $c);
3037            $code = 0xFFFD;
3038          } elsif ($code == 0x000D) {
3039            !!!cp (1017);
3040            !!!parse-error (type => 'CR character reference',
3041                            line => $l, column => $c);
3042            $code = 0x000A;
3043          } elsif (0x80 <= $code and $code <= 0x9F) {
3044            !!!cp (1018);
3045            !!!parse-error (type => 'C1 character reference',
3046                            text => (sprintf 'U+%04X', $code),
3047                            line => $l, column => $c);
3048            $code = $c1_entity_char->{$code};
3049        }        }
3050                
3051        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3052                  line => $l, column => $c,
3053                 };
3054      } else {      } else {
3055        !!!parse-error (type => 'bare nero');        !!!cp (1019);
3056        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
3057        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
3058          $self->{next_char} = 0x0023; # #
3059        return undef;        return undef;
3060      }      }
3061    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
3062              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
3063             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
3064              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
3065      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
3066      !!!next-input-character;      !!!next-input-character;
3067    
3068      my $value = $entity_name;      my $value = $entity_name;
3069      my $match;      my $match = 0;
3070        require Whatpm::_NamedEntityList;
3071        our $EntityChar;
3072    
3073      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3074             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3075             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_char} and # a
3076               $self->{next_input_character} <= 0x005A) or               $self->{next_char} <= 0x005A) or # x
3077              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_char} and # a
3078               $self->{next_input_character} <= 0x007A) or               $self->{next_char} <= 0x007A) or # z
3079              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_char} and # 0
3080               $self->{next_input_character} <= 0x0039))) {               $self->{next_char} <= 0x0039) or # 9
3081        $entity_name .= chr $self->{next_input_character};              $self->{next_char} == 0x003B)) { # ;
3082        if (defined $entity_char->{$entity_name}) {        $entity_name .= chr $self->{next_char};
3083          $value = $entity_char->{$entity_name};        if (defined $EntityChar->{$entity_name}) {
3084          $match = 1;          if ($self->{next_char} == 0x003B) { # ;
3085              !!!cp (1020);
3086              $value = $EntityChar->{$entity_name};
3087              $match = 1;
3088              !!!next-input-character;
3089              last;
3090            } else {
3091              !!!cp (1021);
3092              $value = $EntityChar->{$entity_name};
3093              $match = -1;
3094              !!!next-input-character;
3095            }
3096        } else {        } else {
3097          $value .= chr $self->{next_input_character};          !!!cp (1022);
3098            $value .= chr $self->{next_char};
3099            $match *= 2;
3100            !!!next-input-character;
3101        }        }
       !!!next-input-character;  
3102      }      }
3103            
3104      if ($match) {      if ($match > 0) {
3105        if ($self->{next_input_character} == 0x003B) { # ;        !!!cp (1023);
3106          !!!next-input-character;        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3107                  line => $l, column => $c,
3108                 };
3109        } elsif ($match < 0) {
3110          !!!parse-error (type => 'no refc', line => $l, column => $c);
3111          if ($in_attr and $match < -1) {
3112            !!!cp (1024);
3113            return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3114                    line => $l, column => $c,
3115                   };
3116        } else {        } else {
3117          !!!parse-error (type => 'refc');          !!!cp (1025);
3118            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3119                    line => $l, column => $c,
3120                   };
3121        }        }
   
       return {type => 'character', data => $value};  
3122      } else {      } else {
3123        !!!parse-error (type => 'bare ero');        !!!cp (1026);
3124        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3125        !!!back-token ({type => 'character', data => $value});        ## NOTE: "No characters are consumed" in the spec.
3126        return undef;        return {type => CHARACTER_TOKEN, data => '&'.$value,
3127                  line => $l, column => $c,
3128                 };
3129      }      }
3130    } else {    } else {
3131        !!!cp (1027);
3132      ## no characters are consumed      ## no characters are consumed
3133      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3134      return undef;      return undef;
3135    }    }
3136  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1586  sub _initialize_tree_constructor ($) { Line 3141  sub _initialize_tree_constructor ($) {
3141    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
3142    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3143    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3144    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
3145      $self->{document}->set_user_data (manakai_source_line => 1);
3146      $self->{document}->set_user_data (manakai_source_column => 1);
3147  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3148    
3149  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1613  sub _construct_tree ($) { Line 3170  sub _construct_tree ($) {
3170        
3171    !!!next-token;    !!!next-token;
3172    
   $self->{insertion_mode} = 'before head';  
3173    undef $self->{form_element};    undef $self->{form_element};
3174    undef $self->{head_element};    undef $self->{head_element};
3175    $self->{open_elements} = [];    $self->{open_elements} = [];
3176    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3177    
3178      ## NOTE: The "initial" insertion mode.
3179    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3180    
3181      ## NOTE: The "before html" insertion mode.
3182    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3183      $self->{insertion_mode} = BEFORE_HEAD_IM;
3184    
3185      ## NOTE: The "before head" insertion mode and so on.
3186    $self->_tree_construction_main;    $self->_tree_construction_main;
3187  } # _construct_tree  } # _construct_tree
3188    
3189  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3190    my $self = shift;    my $self = shift;
3191    B: {  
3192        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
3193          if ($token->{error}) {  
3194            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
3195            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
3196          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3197          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
3198            ($token->{name});        ## language.
3199          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
3200          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
3201          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3202          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
3203          return;            defined $token->{system_identifier}) {
3204        } elsif ({          !!!cp ('t1');
3205                  comment => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3206                  'start tag' => 1,        } elsif ($doctype_name ne 'HTML') {
3207                  'end tag' => 1,          !!!cp ('t2');
3208                  'end-of-file' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3209                 }->{$token->{type}}) {        } elsif (defined $token->{public_identifier}) {
3210          ## ISSUE: Spec currently left this case undefined.          if ($token->{public_identifier} eq 'XSLT-compat') {
3211          !!!parse-error (type => 'missing DOCTYPE');            !!!cp ('t1.2');
3212          #$phase = 'root element';            !!!parse-error (type => 'XSLT-compat', token => $token,
3213          ## reprocess                            level => $self->{level}->{should});
3214          #redo B;          } else {
3215          return;            !!!parse-error (type => 'not HTML5', token => $token);
3216        } elsif ($token->{type} eq 'character') {          }
3217          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } else {
3218            $self->{document}->manakai_append_text ($1);          !!!cp ('t3');
3219            ## ISSUE: DOM3 Core does not allow Document > Text          #
3220            unless (length $token->{data}) {        }
3221              ## Stay in the phase        
3222              !!!next-token;        my $doctype = $self->{document}->create_document_type_definition
3223              redo B;          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3224          ## NOTE: Default value for both |public_id| and |system_id| attributes
3225          ## are empty strings, so that we don't set any value in missing cases.
3226          $doctype->public_id ($token->{public_identifier})
3227              if defined $token->{public_identifier};
3228          $doctype->system_id ($token->{system_identifier})
3229              if defined $token->{system_identifier};
3230          ## NOTE: Other DocumentType attributes are null or empty lists.
3231          ## ISSUE: internalSubset = null??
3232          $self->{document}->append_child ($doctype);
3233          
3234          if ($token->{quirks} or $doctype_name ne 'HTML') {
3235            !!!cp ('t4');
3236            $self->{document}->manakai_compat_mode ('quirks');
3237          } elsif (defined $token->{public_identifier}) {
3238            my $pubid = $token->{public_identifier};
3239            $pubid =~ tr/a-z/A-z/;
3240            my $prefix = [
3241              "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3242              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3243              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3244              "-//IETF//DTD HTML 2.0 LEVEL 1//",
3245              "-//IETF//DTD HTML 2.0 LEVEL 2//",
3246              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3247              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3248              "-//IETF//DTD HTML 2.0 STRICT//",
3249              "-//IETF//DTD HTML 2.0//",
3250              "-//IETF//DTD HTML 2.1E//",
3251              "-//IETF//DTD HTML 3.0//",
3252              "-//IETF//DTD HTML 3.2 FINAL//",
3253              "-//IETF//DTD HTML 3.2//",
3254              "-//IETF//DTD HTML 3//",
3255              "-//IETF//DTD HTML LEVEL 0//",
3256              "-//IETF//DTD HTML LEVEL 1//",
3257              "-//IETF//DTD HTML LEVEL 2//",
3258              "-//IETF//DTD HTML LEVEL 3//",
3259              "-//IETF//DTD HTML STRICT LEVEL 0//",
3260              "-//IETF//DTD HTML STRICT LEVEL 1//",
3261              "-//IETF//DTD HTML STRICT LEVEL 2//",
3262              "-//IETF//DTD HTML STRICT LEVEL 3//",
3263              "-//IETF//DTD HTML STRICT//",
3264              "-//IETF//DTD HTML//",
3265              "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3266              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3267              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3268              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3269              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3270              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3271              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3272              "-//NETSCAPE COMM. CORP.//DTD HTML//",
3273              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3274              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3275              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3276              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3277              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3278              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3279              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3280              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3281              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3282              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3283              "-//W3C//DTD HTML 3 1995-03-24//",
3284              "-//W3C//DTD HTML 3.2 DRAFT//",
3285              "-//W3C//DTD HTML 3.2 FINAL//",
3286              "-//W3C//DTD HTML 3.2//",
3287              "-//W3C//DTD HTML 3.2S DRAFT//",
3288              "-//W3C//DTD HTML 4.0 FRAMESET//",
3289              "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3290              "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3291              "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3292              "-//W3C//DTD W3 HTML//",
3293              "-//W3O//DTD W3 HTML 3.0//",
3294              "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3295              "-//WEBTECHS//DTD MOZILLA HTML//",
3296            ]; # $prefix
3297            my $match;
3298            for (@$prefix) {
3299              if (substr ($prefix, 0, length $_) eq $_) {
3300                $match = 1;
3301                last;
3302              }
3303            }
3304            if ($match or
3305                $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3306                $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3307                $pubid eq "HTML") {
3308              !!!cp ('t5');
3309              $self->{document}->manakai_compat_mode ('quirks');
3310            } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3311                     $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3312              if (defined $token->{system_identifier}) {
3313                !!!cp ('t6');
3314                $self->{document}->manakai_compat_mode ('quirks');
3315              } else {
3316                !!!cp ('t7');
3317                $self->{document}->manakai_compat_mode ('limited quirks');
3318            }            }
3319            } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3320                     $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3321              !!!cp ('t8');
3322              $self->{document}->manakai_compat_mode ('limited quirks');
3323            } else {
3324              !!!cp ('t9');
3325            }
3326          } else {
3327            !!!cp ('t10');
3328          }
3329          if (defined $token->{system_identifier}) {
3330            my $sysid = $token->{system_identifier};
3331            $sysid =~ tr/A-Z/a-z/;
3332            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3333              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3334              ## marked as quirks.
3335              $self->{document}->manakai_compat_mode ('quirks');
3336              !!!cp ('t11');
3337            } else {
3338              !!!cp ('t12');
3339          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
3340        } else {        } else {
3341          die "$0: $token->{type}: Unknown token";          !!!cp ('t13');
3342        }        }
3343      } # B        
3344          ## Go to the "before html" insertion mode.
3345          !!!next-token;
3346          return;
3347        } elsif ({
3348                  START_TAG_TOKEN, 1,
3349                  END_TAG_TOKEN, 1,
3350                  END_OF_FILE_TOKEN, 1,
3351                 }->{$token->{type}}) {
3352          !!!cp ('t14');
3353          !!!parse-error (type => 'no DOCTYPE', token => $token);
3354          $self->{document}->manakai_compat_mode ('quirks');
3355          ## Go to the "before html" insertion mode.
3356          ## reprocess
3357          !!!ack-later;
3358          return;
3359        } elsif ($token->{type} == CHARACTER_TOKEN) {
3360          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3361            ## Ignore the token
3362    
3363            unless (length $token->{data}) {
3364              !!!cp ('t15');
3365              ## Stay in the insertion mode.
3366              !!!next-token;
3367              redo INITIAL;
3368            } else {
3369              !!!cp ('t16');
3370            }
3371          } else {
3372            !!!cp ('t17');
3373          }
3374    
3375          !!!parse-error (type => 'no DOCTYPE', token => $token);
3376          $self->{document}->manakai_compat_mode ('quirks');
3377          ## Go to the "before html" insertion mode.
3378          ## reprocess
3379          return;
3380        } elsif ($token->{type} == COMMENT_TOKEN) {
3381          !!!cp ('t18');
3382          my $comment = $self->{document}->create_comment ($token->{data});
3383          $self->{document}->append_child ($comment);
3384          
3385          ## Stay in the insertion mode.
3386          !!!next-token;
3387          redo INITIAL;
3388        } else {
3389          die "$0: $token->{type}: Unknown token type";
3390        }
3391      } # INITIAL
3392    
3393      die "$0: _tree_construction_initial: This should be never reached";
3394  } # _tree_construction_initial  } # _tree_construction_initial
3395    
3396  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3397    my $self = shift;    my $self = shift;
3398    
3399      ## NOTE: "before html" insertion mode.
3400        
3401    B: {    B: {
3402        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3403          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3404            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3405          ## Ignore the token          ## Ignore the token
3406          ## Stay in the phase          ## Stay in the insertion mode.
3407          !!!next-token;          !!!next-token;
3408          redo B;          redo B;
3409        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3410            !!!cp ('t20');
3411          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3412          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3413          ## Stay in the phase          ## Stay in the insertion mode.
3414          !!!next-token;          !!!next-token;
3415          redo B;          redo B;
3416        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3417          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3418            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
3419            ## ISSUE: DOM3 Core does not allow Document > Text  
3420            unless (length $token->{data}) {            unless (length $token->{data}) {
3421              ## Stay in the phase              !!!cp ('t21');
3422                ## Stay in the insertion mode.
3423              !!!next-token;              !!!next-token;
3424              redo B;              redo B;
3425              } else {
3426                !!!cp ('t22');
3427            }            }
3428            } else {
3429              !!!cp ('t23');
3430          }          }
3431    
3432            $self->{application_cache_selection}->(undef);
3433    
3434          #          #
3435          } elsif ($token->{type} == START_TAG_TOKEN) {
3436            if ($token->{tag_name} eq 'html') {
3437              my $root_element;
3438              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3439              $self->{document}->append_child ($root_element);
3440              push @{$self->{open_elements}},
3441                  [$root_element, $el_category->{html}];
3442    
3443              if ($token->{attributes}->{manifest}) {
3444                !!!cp ('t24');
3445                $self->{application_cache_selection}
3446                    ->($token->{attributes}->{manifest}->{value});
3447                ## ISSUE: Spec is unclear on relative references.
3448                ## According to Hixie (#whatwg 2008-03-19), it should be
3449                ## resolved against the base URI of the document in HTML
3450                ## or xml:base of the element in XHTML.
3451              } else {
3452                !!!cp ('t25');
3453                $self->{application_cache_selection}->(undef);
3454              }
3455    
3456              !!!nack ('t25c');
3457    
3458              !!!next-token;
3459              return; ## Go to the "before head" insertion mode.
3460            } else {
3461              !!!cp ('t25.1');
3462              #
3463            }
3464        } elsif ({        } elsif ({
3465                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3466                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3467                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3468          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3469          #          #
3470        } else {        } else {
3471          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3472        }        }
3473        my $root_element; !!!create-element ($root_element, 'html');  
3474        $self->{document}->append_child ($root_element);      my $root_element;
3475        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3476        #$phase = 'main';      $self->{document}->append_child ($root_element);
3477        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3478        #redo B;  
3479        return;      $self->{application_cache_selection}->(undef);
3480    
3481        ## NOTE: Reprocess the token.
3482        !!!ack-later;
3483        return; ## Go to the "before head" insertion mode.
3484    
3485        ## ISSUE: There is an issue in the spec
3486    } # B    } # B
3487    
3488      die "$0: _tree_construction_root_element: This should never be reached";
3489  } # _tree_construction_root_element  } # _tree_construction_root_element
3490    
3491  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1732  sub _reset_insertion_mode ($) { Line 3500  sub _reset_insertion_mode ($) {
3500            
3501      ## Step 3      ## Step 3
3502      S3: {      S3: {
3503        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3504        if (defined $self->{inner_html_node}) {          $last = 1;
3505          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
3506              $self->{inner_html_node}->[1] eq 'th') {            !!!cp ('t28');
3507              $node = $self->{inner_html_node};
3508            } else {
3509              die "_reset_insertion_mode: t27";
3510            }
3511          }
3512          
3513          ## Step 4..14
3514          my $new_mode;
3515          if ($node->[1] & FOREIGN_EL) {
3516            !!!cp ('t28.1');
3517            ## NOTE: Strictly spaking, the line below only applies to MathML and
3518            ## SVG elements.  Currently the HTML syntax supports only MathML and
3519            ## SVG elements as foreigners.
3520            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3521          } elsif ($node->[1] & TABLE_CELL_EL) {
3522            if ($last) {
3523              !!!cp ('t28.2');
3524            #            #
3525          } else {          } else {
3526            $node = $self->{inner_html_node};            !!!cp ('t28.3');
3527              $new_mode = IN_CELL_IM;
3528          }          }
3529          } else {
3530            !!!cp ('t28.4');
3531            $new_mode = {
3532                          select => IN_SELECT_IM,
3533                          ## NOTE: |option| and |optgroup| do not set
3534                          ## insertion mode to "in select" by themselves.
3535                          tr => IN_ROW_IM,
3536                          tbody => IN_TABLE_BODY_IM,
3537                          thead => IN_TABLE_BODY_IM,
3538                          tfoot => IN_TABLE_BODY_IM,
3539                          caption => IN_CAPTION_IM,
3540                          colgroup => IN_COLUMN_GROUP_IM,
3541                          table => IN_TABLE_IM,
3542                          head => IN_BODY_IM, # not in head!
3543                          body => IN_BODY_IM,
3544                          frameset => IN_FRAMESET_IM,
3545                         }->{$node->[0]->manakai_local_name};
3546        }        }
       
       ## 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]};  
3547        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3548                
3549        ## Step 14        ## Step 15
3550        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3551          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3552            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3553              $self->{insertion_mode} = BEFORE_HEAD_IM;
3554          } else {          } else {
3555            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3556              !!!cp ('t30');
3557              $self->{insertion_mode} = AFTER_HEAD_IM;
3558          }          }
3559          return;          return;
3560          } else {
3561            !!!cp ('t31');
3562        }        }
3563                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3564        ## Step 16        ## Step 16
3565          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3566          
3567          ## Step 17
3568        $i--;        $i--;
3569        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3570                
3571        ## Step 17        ## Step 18
3572        redo S3;        redo S3;
3573      } # S3      } # S3
3574    
3575      die "$0: _reset_insertion_mode: This line should never be reached";
3576  } # _reset_insertion_mode  } # _reset_insertion_mode
3577    
3578  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3579    my $self = shift;    my $self = shift;
3580    
   my $phase = 'main';  
   
3581    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3582    
3583    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1803  sub _tree_construction_main ($) { Line 3594  sub _tree_construction_main ($) {
3594      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3595      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3596        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3597            !!!cp ('t32');
3598          return;          return;
3599        }        }
3600      }      }
# Line 1817  sub _tree_construction_main ($) { Line 3609  sub _tree_construction_main ($) {
3609    
3610        ## Step 6        ## Step 6
3611        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3612            !!!cp ('t33_1');
3613          #          #
3614        } else {        } else {
3615          my $in_open_elements;          my $in_open_elements;
3616          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3617            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3618                !!!cp ('t33');
3619              $in_open_elements = 1;              $in_open_elements = 1;
3620              last OE;              last OE;
3621            }            }
3622          }          }
3623          if ($in_open_elements) {          if ($in_open_elements) {
3624              !!!cp ('t34');
3625            #            #
3626          } else {          } else {
3627              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3628              !!!cp ('t35');
3629            redo S4;            redo S4;
3630          }          }
3631        }        }
# Line 1851  sub _tree_construction_main ($) { Line 3648  sub _tree_construction_main ($) {
3648    
3649        ## Step 11        ## Step 11
3650        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3651            !!!cp ('t36');
3652          ## Step 7'          ## Step 7'
3653          $i++;          $i++;
3654          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3655                    
3656          redo S7;          redo S7;
3657        }        }
3658    
3659          !!!cp ('t37');
3660      } # S7      } # S7
3661    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3662    
3663    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3664      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3665        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3666            !!!cp ('t38');
3667          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3668          return;          return;
3669        }        }
3670      }      }
3671    
3672        !!!cp ('t39');
3673    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3674    
3675    my $style_start_tag = sub {    my $insert;
3676      my $style_el; !!!create-element ($style_el, 'style');  
3677      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
3678      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
3679       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
3680        ->append_child ($style_el);      ## Step 1
3681      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
3682                      my $el;
3683        !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3684    
3685        ## Step 2
3686        $insert->($el);
3687    
3688        ## Step 3
3689        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3690        delete $self->{escape}; # MUST
3691    
3692        ## Step 4
3693      my $text = '';      my $text = '';
3694        !!!nack ('t40.1');
3695      !!!next-token;      !!!next-token;
3696      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3697          !!!cp ('t40');
3698        $text .= $token->{data};        $text .= $token->{data};
3699        !!!next-token;        !!!next-token;
3700      } # stop if non-character token or tokenizer stops tokenising      }
3701    
3702        ## Step 5
3703      if (length $text) {      if (length $text) {
3704        $style_el->manakai_append_text ($text);        !!!cp ('t41');
3705          my $text = $self->{document}->create_text_node ($text);
3706          $el->append_child ($text);
3707      }      }
3708        
3709      $self->{content_model_flag} = 'PCDATA';      ## Step 6
3710                      $self->{content_model} = PCDATA_CONTENT_MODEL;
3711      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
3712        ## Step 7
3713        if ($token->{type} == END_TAG_TOKEN and
3714            $token->{tag_name} eq $start_tag_name) {
3715          !!!cp ('t42');
3716        ## Ignore the token        ## Ignore the token
3717      } else {      } else {
3718        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
3719        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3720            !!!cp ('t43');
3721            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3722          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3723            !!!cp ('t44');
3724            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3725          } else {
3726            die "$0: $content_model_flag in parse_rcdata";
3727          }
3728      }      }
3729      !!!next-token;      !!!next-token;
3730    }; # $style_start_tag    }; # $parse_rcdata
3731    
3732    my $script_start_tag = sub {    my $script_start_tag = sub () {
3733      my $script_el;      my $script_el;
3734      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3735      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3736    
3737      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3738        delete $self->{escape}; # MUST
3739            
3740      my $text = '';      my $text = '';
3741        !!!nack ('t45.1');
3742      !!!next-token;      !!!next-token;
3743      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3744          !!!cp ('t45');
3745        $text .= $token->{data};        $text .= $token->{data};
3746        !!!next-token;        !!!next-token;
3747      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3748      if (length $text) {      if (length $text) {
3749          !!!cp ('t46');
3750        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3751      }      }
3752                                
3753      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3754    
3755      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3756          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3757          !!!cp ('t47');
3758        ## Ignore the token        ## Ignore the token
3759      } else {      } else {
3760        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3761          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3762        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3763        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3764      }      }
3765            
3766      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3767          !!!cp ('t49');
3768        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3769      } else {      } else {
3770          !!!cp ('t50');
3771        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3772        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3773          
3774        (($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);  
3775                
3776        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
3777                
# Line 1943  sub _tree_construction_main ($) { Line 3781  sub _tree_construction_main ($) {
3781      !!!next-token;      !!!next-token;
3782    }; # $script_start_tag    }; # $script_start_tag
3783    
3784      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3785      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3786      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3787    
3788    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3789      my $tag_name = shift;      my $end_tag_token = shift;
3790        my $tag_name = $end_tag_token->{tag_name};
3791    
3792        ## NOTE: The adoption agency algorithm (AAA).
3793    
3794      FET: {      FET: {
3795        ## Step 1        ## Step 1
3796        my $formatting_element;        my $formatting_element;
3797        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3798        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3799          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3800              !!!cp ('t52');
3801              last AFE;
3802            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3803                         eq $tag_name) {
3804              !!!cp ('t51');
3805            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3806            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3807            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3808          }          }
3809        } # AFE        } # AFE
3810        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3811          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3812            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3813          ## Ignore the token          ## Ignore the token
3814          !!!next-token;          !!!next-token;
3815          return;          return;
# Line 1972  sub _tree_construction_main ($) { Line 3821  sub _tree_construction_main ($) {
3821          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3822          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3823            if ($in_scope) {            if ($in_scope) {
3824                !!!cp ('t54');
3825              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3826              last INSCOPE;              last INSCOPE;
3827            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3828              !!!parse-error;              !!!cp ('t55');
3829                !!!parse-error (type => 'unmatched end tag',
3830                                text => $token->{tag_name},
3831                                token => $end_tag_token);
3832              ## Ignore the token              ## Ignore the token
3833              !!!next-token;              !!!next-token;
3834              return;              return;
3835            }            }
3836          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3837                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3838            $in_scope = 0;            $in_scope = 0;
3839          }          }
3840        } # INSCOPE        } # INSCOPE
3841        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3842          !!!parse-error;          !!!cp ('t57');
3843            !!!parse-error (type => 'unmatched end tag',
3844                            text => $token->{tag_name},
3845                            token => $end_tag_token);
3846          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3847          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3848          return;          return;
3849        }        }
3850        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3851          !!!parse-error;          !!!cp ('t58');
3852            !!!parse-error (type => 'not closed',
3853                            text => $self->{open_elements}->[-1]->[0]
3854                                ->manakai_local_name,
3855                            token => $end_tag_token);
3856        }        }
3857                
3858        ## Step 2        ## Step 2
# Line 2002  sub _tree_construction_main ($) { Line 3860  sub _tree_construction_main ($) {
3860        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3861        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3862          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3863          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3864              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3865              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3866               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3867              !!!cp ('t59');
3868            $furthest_block = $node;            $furthest_block = $node;
3869            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3870          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3871              !!!cp ('t60');
3872            last OE;            last OE;
3873          }          }
3874        } # OE        } # OE
3875                
3876        ## Step 3        ## Step 3
3877        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3878            !!!cp ('t61');
3879          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3880          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3881          !!!next-token;          !!!next-token;
# Line 2027  sub _tree_construction_main ($) { Line 3888  sub _tree_construction_main ($) {
3888        ## Step 5        ## Step 5
3889        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3890        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3891            !!!cp ('t62');
3892          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3893        }        }
3894                
# Line 2049  sub _tree_construction_main ($) { Line 3911  sub _tree_construction_main ($) {
3911          S7S2: {          S7S2: {
3912            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3913              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3914                  !!!cp ('t63');
3915                $node_i_in_active = $_;                $node_i_in_active = $_;
3916                last S7S2;                last S7S2;
3917              }              }
# Line 2062  sub _tree_construction_main ($) { Line 3925  sub _tree_construction_main ($) {
3925                    
3926          ## Step 4          ## Step 4
3927          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3928              !!!cp ('t64');
3929            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3930          }          }
3931                    
3932          ## Step 5          ## Step 5
3933          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3934              !!!cp ('t65');
3935            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3936            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3937            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2084  sub _tree_construction_main ($) { Line 3949  sub _tree_construction_main ($) {
3949        } # S7          } # S7  
3950                
3951        ## Step 8        ## Step 8
3952        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3953            my $foster_parent_element;
3954            my $next_sibling;
3955            OE: for (reverse 0..$#{$self->{open_elements}}) {
3956              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3957                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3958                                 if (defined $parent and $parent->node_type == 1) {
3959                                   !!!cp ('t65.1');
3960                                   $foster_parent_element = $parent;
3961                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3962                                 } else {
3963                                   !!!cp ('t65.2');
3964                                   $foster_parent_element
3965                                     = $self->{open_elements}->[$_ - 1]->[0];
3966                                 }
3967                                 last OE;
3968                               }
3969                             } # OE
3970                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3971                               unless defined $foster_parent_element;
3972            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3973            $open_tables->[-1]->[1] = 1; # tainted
3974          } else {
3975            !!!cp ('t65.3');
3976            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3977          }
3978                
3979        ## Step 9        ## Step 9
3980        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2101  sub _tree_construction_main ($) { Line 3991  sub _tree_construction_main ($) {
3991        my $i;        my $i;
3992        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3993          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3994              !!!cp ('t66');
3995            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3996            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3997          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3998              !!!cp ('t67');
3999            $i = $_;            $i = $_;
4000          }          }
4001        } # AFE        } # AFE
# Line 2113  sub _tree_construction_main ($) { Line 4005  sub _tree_construction_main ($) {
4005        undef $i;        undef $i;
4006        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4007          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4008              !!!cp ('t68');
4009            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4010            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4011          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4012              !!!cp ('t69');
4013            $i = $_;            $i = $_;
4014          }          }
4015        } # OE        } # OE
# Line 2126  sub _tree_construction_main ($) { Line 4020  sub _tree_construction_main ($) {
4020      } # FET      } # FET
4021    }; # $formatting_end_tag    }; # $formatting_end_tag
4022    
4023    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4024      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4025    }; # $insert_to_current    }; # $insert_to_current
4026    
4027    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4028                         my $child = shift;      my $child = shift;
4029                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4030                              table => 1, tbody => 1, tfoot => 1,        # MUST
4031                              thead => 1, tr => 1,        my $foster_parent_element;
4032                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4033                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4034                           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') {  
4035                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4036                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4037                                   !!!cp ('t70');
4038                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4039                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4040                               } else {                               } else {
4041                                   !!!cp ('t71');
4042                                 $foster_parent_element                                 $foster_parent_element
4043                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4044                               }                               }
# Line 2156  sub _tree_construction_main ($) { Line 4049  sub _tree_construction_main ($) {
4049                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4050                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4051                             ($child, $next_sibling);                             ($child, $next_sibling);
4052                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4053                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4054                         }        !!!cp ('t72');
4055          $self->{open_elements}->[-1]->[0]->append_child ($child);
4056        }
4057    }; # $insert_to_foster    }; # $insert_to_foster
4058    
4059    my $in_body = sub {    B: while (1) {
4060      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
4061      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
4062        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4063          $script_start_tag->();        ## Ignore the token
4064          return;        ## Stay in the phase
4065        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
4066          $style_start_tag->();        next B;
4067          return;      } elsif ($token->{type} == START_TAG_TOKEN and
4068        } elsif ({               $token->{tag_name} eq 'html') {
4069                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4070                 }->{$token->{tag_name}}) {          !!!cp ('t79');
4071          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html', text => 'html', token => $token);
4072          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
4073          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4074          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
4075          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html', text => 'html', token => $token);
4076            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4077          } else {        } else {
4078            $insert->($el);          !!!cp ('t81');
4079          }        }
4080            
4081          !!!next-token;        !!!cp ('t82');
4082          return;        !!!parse-error (type => 'not first start tag', token => $token);
4083        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
4084          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
4085          ## NOTE: There is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4086          my $title_el;            !!!cp ('t84');
4087          !!!create-element ($title_el, 'title', $token->{attributes});            $top_el->set_attribute_ns
4088          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              (undef, [undef, $attr_name],
4089            ->append_child ($title_el);               $token->{attributes}->{$attr_name}->{value});
         $self->{content_model_flag} = 'RCDATA';  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
4090          }          }
4091                    }
4092          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!nack ('t84.1');
4093                    !!!next-token;
4094          next B;
4095        } elsif ($token->{type} == COMMENT_TOKEN) {
4096          my $comment = $self->{document}->create_comment ($token->{data});
4097          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4098            !!!cp ('t85');
4099            $self->{document}->append_child ($comment);
4100          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4101            !!!cp ('t86');
4102            $self->{open_elements}->[0]->[0]->append_child ($comment);
4103          } else {
4104            !!!cp ('t87');
4105            $self->{open_elements}->[-1]->[0]->append_child ($comment);
4106          }
4107          !!!next-token;
4108          next B;
4109        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4110          if ($token->{type} == CHARACTER_TOKEN) {
4111            !!!cp ('t87.1');
4112            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4113          !!!next-token;          !!!next-token;
4114          return;          next B;
4115        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4116          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4117            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4118            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4119              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4120                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4121              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4122              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4123              $formatting_end_tag->($token->{tag_name});            #
4124                        } elsif ({
4125              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4126                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4127                  splice @$active_formatting_elements, $_, 1;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4128                  last AFE2;                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4129                }                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4130              } # AFE2                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4131              OE: for (reverse 0..$#{$self->{open_elements}}) {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4132                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4133                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4134                  last OE;            !!!cp ('t87.2');
4135                }            !!!parse-error (type => 'not closed',
4136              } # OE                            text => $self->{open_elements}->[-1]->[0]
4137              last AFE;                                ->manakai_local_name,
4138            } elsif ($node->[0] eq '#marker') {                            token => $token);
4139              last AFE;  
4140              pop @{$self->{open_elements}}
4141                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4142    
4143              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4144              ## Reprocess.
4145              next B;
4146            } else {
4147              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4148              my $tag_name = $token->{tag_name};
4149              if ($nsuri eq $SVG_NS) {
4150                $tag_name = {
4151                   altglyph => 'altGlyph',
4152                   altglyphdef => 'altGlyphDef',
4153                   altglyphitem => 'altGlyphItem',
4154                   animatecolor => 'animateColor',
4155                   animatemotion => 'animateMotion',
4156                   animatetransform => 'animateTransform',
4157                   clippath => 'clipPath',
4158                   feblend => 'feBlend',
4159                   fecolormatrix => 'feColorMatrix',
4160                   fecomponenttransfer => 'feComponentTransfer',
4161                   fecomposite => 'feComposite',
4162                   feconvolvematrix => 'feConvolveMatrix',
4163                   fediffuselighting => 'feDiffuseLighting',
4164                   fedisplacementmap => 'feDisplacementMap',
4165                   fedistantlight => 'feDistantLight',
4166                   feflood => 'feFlood',
4167                   fefunca => 'feFuncA',
4168                   fefuncb => 'feFuncB',
4169                   fefuncg => 'feFuncG',
4170                   fefuncr => 'feFuncR',
4171                   fegaussianblur => 'feGaussianBlur',
4172                   feimage => 'feImage',
4173                   femerge => 'feMerge',
4174                   femergenode => 'feMergeNode',
4175                   femorphology => 'feMorphology',
4176                   feoffset => 'feOffset',
4177                   fepointlight => 'fePointLight',
4178                   fespecularlighting => 'feSpecularLighting',
4179                   fespotlight => 'feSpotLight',
4180                   fetile => 'feTile',
4181                   feturbulence => 'feTurbulence',
4182                   foreignobject => 'foreignObject',
4183                   glyphref => 'glyphRef',
4184                   lineargradient => 'linearGradient',
4185                   radialgradient => 'radialGradient',
4186                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4187                   textpath => 'textPath',  
4188                }->{$tag_name} || $tag_name;
4189            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4190    
4191          !!!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];  
4192    
4193          !!!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', ''];  
4194    
4195          !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4196          return;  
4197        } elsif ($token->{tag_name} eq 'marquee' or            if ($self->{self_closing}) {
4198                 $token->{tag_name} eq 'object') {              pop @{$self->{open_elements}};
4199          $reconstruct_active_formatting_elements->($insert_to_current);              !!!ack ('t87.3');
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
           
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                         {type => 'character',  
                          data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD  
                         ## TODO: make this configurable  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'},  
                        );  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ({  
                 textarea => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: $self->{form_element} if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
4200            } else {            } else {
4201              !!!parse-error (type => 'in RCDATA:#'.$token->{type});              !!!cp ('t87.4');
4202            }            }
4203            ## ISSUE: And ignore?  
4204              !!!next-token;
4205              next B;
4206          }          }
4207          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4208          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4209        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
4210          $reconstruct_active_formatting_elements->($insert_to_current);          #
4211                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4212          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t87.6');
4213                    !!!parse-error (type => 'not closed',
4214          $self->{insertion_mode} = 'in select';                          text => $self->{open_elements}->[-1]->[0]
4215          !!!next-token;                              ->manakai_local_name,
4216          return;                          token => $token);
4217        } elsif ({  
4218                  caption => 1, col => 1, colgroup => 1, frame => 1,          pop @{$self->{open_elements}}
4219                  frameset => 1, head => 1, option => 1, optgroup => 1,              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4220                  tbody => 1, td => 1, tfoot => 1, th => 1,  
4221                  thead => 1, tr => 1,          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4222                 }->{$token->{tag_name}}) {          ## Reprocess.
4223          !!!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.  
4224        } else {        } else {
4225          $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;  
4226        }        }
4227      } elsif ($token->{type} eq 'end tag') {      }
4228        if ($token->{tag_name} eq 'body') {  
4229          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      if ($self->{insertion_mode} & HEAD_IMS) {
4230            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
4231            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4232              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4233            }              !!!cp ('t88.2');
4234            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4235            !!!next-token;            } else {
4236            return;              !!!cp ('t88.1');
4237          } else {              ## Ignore the token.
4238            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
4239            ## Ignore the token              next B;
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 form => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4240            }            }
4241          } # INSCOPE            unless (length $token->{data}) {
4242                        !!!cp ('t88');
4243          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
4244            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              next B;
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         undef $self->{form_element} if $token->{tag_name} eq 'form';  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4245            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4246          }          }
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex=> 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
4247    
4248          ## Step 2          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4249          S2: {            !!!cp ('t89');
4250            if ($node->[1] eq $token->{tag_name}) {            ## As if <head>
4251              ## Step 1            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4252              ## generate implied end tags            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4253              if ({            push @{$self->{open_elements}},
4254                   dd => 1, dt => 1, li => 1, p => 1,                [$self->{head_element}, $el_category->{head}];
4255                   td => 1, th => 1, tr => 1,  
4256                  }->{$self->{open_elements}->[-1]->[1]}) {            ## Reprocess in the "in head" insertion mode...
4257                !!!back-token;            pop @{$self->{open_elements}};
4258                $token = {type => 'end tag',  
4259                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST            ## Reprocess in the "after head" insertion mode...
4260                return;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4261              }            !!!cp ('t90');
4262                      ## As if </noscript>
4263              ## Step 2            pop @{$self->{open_elements}};
4264              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {            !!!parse-error (type => 'in noscript:#text', token => $token);
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
   
             !!!next-token;  
             last S2;  
           } else {  
             ## Step 3  
             if (not $formatting_category->{$node->[1]} and  
                 #not $phrasing_category->{$node->[1]} and  
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'not closed:'.$node->[1]);  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
4265                        
4266            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4267            redo S2;            ## As if </head>
4268          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
4269    
4270    B: {            ## Reprocess in the "after head" insertion mode...
4271      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4272        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
4273          !!!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]);  
         }  
4274    
4275          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4276          last B;          } else {
4277              !!!cp ('t92');
4278            }
4279    
4280          ## ISSUE: There is an issue in the spec.          ## "after head" insertion mode
4281        } else {          ## As if <body>
4282          if ($self->{insertion_mode} eq 'before head') {          !!!insert-element ('body',, $token);
4283            if ($token->{type} eq 'character') {          $self->{insertion_mode} = IN_BODY_IM;
4284              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## reprocess
4285                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          next B;
4286                unless (length $token->{data}) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4287                  !!!next-token;          if ($token->{tag_name} eq 'head') {
4288                  redo B;            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4289                }              !!!cp ('t93');
4290              }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4291              ## As if <head>              $self->{open_elements}->[-1]->[0]->append_child
4292              !!!create-element ($self->{head_element}, 'head');                  ($self->{head_element});
4293              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              push @{$self->{open_elements}},
4294              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  [$self->{head_element}, $el_category->{head}];
4295              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = IN_HEAD_IM;
4296              ## 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);  
4297              !!!next-token;              !!!next-token;
4298              redo B;              next B;
4299            } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4300              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              !!!cp ('t93.2');
4301              !!!create-element ($self->{head_element}, 'head', $attr);              !!!parse-error (type => 'after head', text => 'head',
4302              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token);
4303              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4304              $self->{insertion_mode} = 'in head';              !!!nack ('t93.3');
4305              if ($token->{tag_name} eq 'head') {              !!!next-token;
4306                !!!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;  
             }  
4307            } else {            } else {
4308              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4309            }              !!!parse-error (type => 'in head:head',
4310          } elsif ($self->{insertion_mode} eq 'in head') {                              token => $token); # or in head noscript
4311            if ($token->{type} eq 'character') {              ## Ignore the token
4312              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);  
4313              !!!next-token;              !!!next-token;
4314              redo B;              next B;
4315            } elsif ($token->{type} eq 'start tag') {            }
4316              if ($token->{tag_name} eq 'title') {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4317                ## NOTE: There is an "as if in head" code clone            !!!cp ('t96');
4318                my $title_el;            ## As if <head>
4319                !!!create-element ($title_el, 'title', $token->{attributes});            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4320                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4321                  ->append_child ($title_el);            push @{$self->{open_elements}},
4322                $self->{content_model_flag} = 'RCDATA';                [$self->{head_element}, $el_category->{head}];
4323    
4324                my $text = '';            $self->{insertion_mode} = IN_HEAD_IM;
4325                !!!next-token;            ## Reprocess in the "in head" insertion mode...
4326                while ($token->{type} eq 'character') {          } else {
4327                  $text .= $token->{data};            !!!cp ('t97');
4328                  !!!next-token;          }
4329                }  
4330                if (length $text) {              if ($token->{tag_name} eq 'base') {
4331                  $title_el->manakai_append_text ($text);                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4332                }                  !!!cp ('t98');
4333                                  ## As if </noscript>
4334                $self->{content_model_flag} = 'PCDATA';                  pop @{$self->{open_elements}};
4335                    !!!parse-error (type => 'in noscript', text => 'base',
4336                                    token => $token);
4337                                
4338                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
4339                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
4340                } else {                } else {
4341                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
4342                }                }
               !!!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);  
4343    
4344                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
4345                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4346              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
4347                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head',
4348                ## Ignore the token                                  text => $token->{tag_name}, token => $token);
4349                !!!next-token;                  push @{$self->{open_elements}},
4350                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}};  
4351                } else {                } else {
4352                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
4353                }                }
4354                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4355                !!!next-token;                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4356                redo B;                pop @{$self->{open_elements}} # <head>
4357              } elsif ($token->{tag_name} eq 'html') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4358                #                !!!nack ('t101.1');
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
4359                !!!next-token;                !!!next-token;
4360                redo B;                next B;
4361              }              } elsif ($token->{tag_name} eq 'link') {
4362            } else {                ## NOTE: There is a "as if in head" code clone.
4363              #                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4364            }                  !!!cp ('t102');
4365                    !!!parse-error (type => 'after head',
4366            if ($self->{open_elements}->[-1]->[1] eq 'head') {                                  text => $token->{tag_name}, token => $token);
4367              ## As if </head>                  push @{$self->{open_elements}},
4368              pop @{$self->{open_elements}};                      [$self->{head_element}, $el_category->{head}];
4369            }                } else {
4370            $self->{insertion_mode} = 'after head';                  !!!cp ('t103');
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
4371                }                }
4372              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4373                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4374              #                pop @{$self->{open_elements}} # <head>
4375            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4376              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';  
4377                !!!next-token;                !!!next-token;
4378                redo B;                next B;
4379              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
4380                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
4381                        script => 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4382                       }->{$token->{tag_name}}) {                  !!!cp ('t104');
4383                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4384                $self->{insertion_mode} = 'in head';                                  text => $token->{tag_name}, token => $token);
4385                ## reprocess                  push @{$self->{open_elements}},
4386                redo B;                      [$self->{head_element}, $el_category->{head}];
4387              } else {                } else {
4388                #                  !!!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;  
4389                }                }
4390              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4391                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
             !!!parse-error (type => 'in table:#character');  
4392    
4393              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
4394              ## ISSUE: Spec says that "whenever a node would be inserted                  if ($token->{attributes}->{charset}) {
4395              ## into the current node" while characters might not be                    !!!cp ('t106');
4396              ## result in a new Text node.                    ## NOTE: Whether the encoding is supported or not is handled
4397              $reconstruct_active_formatting_elements->($insert_to_foster);                    ## in the {change_encoding} callback.
4398                                  $self->{change_encoding}
4399              if ({                        ->($self, $token->{attributes}->{charset}->{value},
4400                   table => 1, tbody => 1, tfoot => 1,                           $token);
4401                   thead => 1, tr => 1,                    
4402                  }->{$self->{open_elements}->[-1]->[1]}) {                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4403                # MUST                        ->set_user_data (manakai_has_reference =>
4404                my $foster_parent_element;                                             $token->{attributes}->{charset}
4405                my $next_sibling;                                                 ->{has_reference});
4406                my $prev_sibling;                  } elsif ($token->{attributes}->{content}) {
4407                OE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($token->{attributes}->{content}->{value}
4408                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4409                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            [\x09-\x0D\x20]*=
4410                    if (defined $parent and $parent->node_type == 1) {                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4411                      $foster_parent_element = $parent;                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4412                      $next_sibling = $self->{open_elements}->[$_]->[0];                      !!!cp ('t107');
4413                      $prev_sibling = $next_sibling->previous_sibling;                      ## NOTE: Whether the encoding is supported or not is handled
4414                        ## in the {change_encoding} callback.
4415                        $self->{change_encoding}
4416                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4417                               $token);
4418                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4419                            ->set_user_data (manakai_has_reference =>
4420                                                 $token->{attributes}->{content}
4421                                                       ->{has_reference});
4422                    } else {                    } else {
4423                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
4424                    }                    }
                   last OE;  
4425                  }                  }
               } # 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});  
4426                } else {                } else {
4427                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
4428                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
4429                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4430                }                        ->set_user_data (manakai_has_reference =>
4431              } else {                                             $token->{attributes}->{charset}
4432                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
4433              }                  }
4434                                if ($token->{attributes}->{content}) {
4435              !!!next-token;                    !!!cp ('t110');
4436              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4437            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
4438              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
4439              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
4440              !!!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}};  
4441                }                }
4442    
4443                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
4444                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4445                  !!!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}};  
4446                !!!next-token;                !!!next-token;
4447                redo B;                next B;
4448              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
4449                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4450                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
4451                       }->{$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]);  
4452                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4453                    !!!parse-error (type => 'in noscript', text => 'title',
4454                                    token => $token);
4455                  
4456                    $self->{insertion_mode} = IN_HEAD_IM;
4457                    ## Reprocess in the "in head" insertion mode...
4458                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4459                    !!!cp ('t112');
4460                    !!!parse-error (type => 'after head',
4461                                    text => $token->{tag_name}, token => $token);
4462                    push @{$self->{open_elements}},
4463                        [$self->{head_element}, $el_category->{head}];
4464                  } else {
4465                    !!!cp ('t113');
4466                }                }
4467    
4468                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
4469                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
4470                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
4471                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4472                redo B;                pop @{$self->{open_elements}} # <head>
4473              } elsif ($token->{tag_name} eq 'table') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4474                ## NOTE: There are code clones for this "table in table"                next B;
4475                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } elsif ($token->{tag_name} eq 'style' or
4476                         $token->{tag_name} eq 'noframes') {
4477                ## As if </table>                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4478                ## have a table element in table scope                ## insertion mode IN_HEAD_IM)
4479                my $i;                ## NOTE: There is a "as if in head" code clone.
4480                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4481                  my $node = $self->{open_elements}->[$_];                  !!!cp ('t114');
4482                  if ($node->[1] eq 'table') {                  !!!parse-error (type => 'after head',
4483                    $i = $_;                                  text => $token->{tag_name}, token => $token);
4484                    last INSCOPE;                  push @{$self->{open_elements}},
4485                  } elsif ({                      [$self->{head_element}, $el_category->{head}];
4486                            table => 1, html => 1,                } else {
4487                           }->{$node->[1]}) {                  !!!cp ('t115');
4488                    last INSCOPE;                }
4489                  }                $parse_rcdata->(CDATA_CONTENT_MODEL);
4490                } # INSCOPE                pop @{$self->{open_elements}} # <head>
4491                unless (defined $i) {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4492                  !!!parse-error (type => 'unmatched end tag:table');                next B;
4493                  ## Ignore tokens </table><table>              } elsif ($token->{tag_name} eq 'noscript') {
4494                  if ($self->{insertion_mode} == IN_HEAD_IM) {
4495                    !!!cp ('t116');
4496                    ## NOTE: and scripting is disalbed
4497                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4498                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4499                    !!!nack ('t116.1');
4500                    !!!next-token;
4501                    next B;
4502                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4503                    !!!cp ('t117');
4504                    !!!parse-error (type => 'in noscript', text => 'noscript',
4505                                    token => $token);
4506                    ## Ignore the token
4507                    !!!nack ('t117.1');
4508                  !!!next-token;                  !!!next-token;
4509                  redo B;                  next B;
4510                  } else {
4511                    !!!cp ('t118');
4512                    #
4513                }                }
4514                } elsif ($token->{tag_name} eq 'script') {
4515                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4516                    !!!cp ('t119');
4517                    ## As if </noscript>
4518                    pop @{$self->{open_elements}};
4519                    !!!parse-error (type => 'in noscript', text => 'script',
4520                                    token => $token);
4521                                
4522                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
4523                if ({                  ## Reprocess in the "in head" insertion mode...
4524                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4525                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
4526                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head',
4527                  !!!back-token; # <table>                                  text => $token->{tag_name}, token => $token);
4528                  $token = {type => 'end tag', tag_name => 'table'};                  push @{$self->{open_elements}},
4529                  !!!back-token;                      [$self->{head_element}, $el_category->{head}];
4530                  $token = {type => 'end tag',                } else {
4531                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!cp ('t121');
                 redo B;  
4532                }                }
4533    
4534                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
4535                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
4536                  pop @{$self->{open_elements}} # <head>
4537                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4538                  next B;
4539                } elsif ($token->{tag_name} eq 'body' or
4540                         $token->{tag_name} eq 'frameset') {
4541                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4542                    !!!cp ('t122');
4543                    ## As if </noscript>
4544                    pop @{$self->{open_elements}};
4545                    !!!parse-error (type => 'in noscript',
4546                                    text => $token->{tag_name}, token => $token);
4547                    
4548                    ## Reprocess in the "in head" insertion mode...
4549                    ## As if </head>
4550                    pop @{$self->{open_elements}};
4551                    
4552                    ## Reprocess in the "after head" insertion mode...
4553                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4554                    !!!cp ('t124');
4555                    pop @{$self->{open_elements}};
4556                    
4557                    ## Reprocess in the "after head" insertion mode...
4558                  } else {
4559                    !!!cp ('t125');
4560                }                }
4561    
4562                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
4563                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4564                  if ($token->{tag_name} eq 'body') {
4565                    !!!cp ('t126');
4566                    $self->{insertion_mode} = IN_BODY_IM;
4567                  } elsif ($token->{tag_name} eq 'frameset') {
4568                    !!!cp ('t127');
4569                    $self->{insertion_mode} = IN_FRAMESET_IM;
4570                  } else {
4571                    die "$0: tag name: $self->{tag_name}";
4572                  }
4573                  !!!nack ('t127.1');
4574                  !!!next-token;
4575                  next B;
4576                } else {
4577                  !!!cp ('t128');
4578                  #
4579                }
4580    
4581                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4582                  !!!cp ('t129');
4583                  ## As if </noscript>
4584                  pop @{$self->{open_elements}};
4585                  !!!parse-error (type => 'in noscript:/',
4586                                  text => $token->{tag_name}, token => $token);
4587                  
4588                  ## Reprocess in the "in head" insertion mode...
4589                  ## As if </head>
4590                  pop @{$self->{open_elements}};
4591    
4592                ## reprocess                ## Reprocess in the "after head" insertion mode...
4593                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4594                  !!!cp ('t130');
4595                  ## As if </head>
4596                  pop @{$self->{open_elements}};
4597    
4598                  ## Reprocess in the "after head" insertion mode...
4599              } else {              } else {
4600                #                !!!cp ('t131');
4601              }              }
4602            } elsif ($token->{type} eq 'end tag') {  
4603              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
4604                ## have a table element in table scope              ## As if <body>
4605                my $i;              !!!insert-element ('body',, $token);
4606                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
4607                  my $node = $self->{open_elements}->[$_];              ## reprocess
4608                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
4609                    $i = $_;              next B;
4610                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
4611                  } elsif ({              if ($token->{tag_name} eq 'head') {
4612                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4613                           }->{$node->[1]}) {                  !!!cp ('t132');
4614                    last INSCOPE;                  ## As if <head>
4615                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4616                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4617                unless (defined $i) {                  push @{$self->{open_elements}},
4618                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
4619    
4620                    ## Reprocess in the "in head" insertion mode...
4621                    pop @{$self->{open_elements}};
4622                    $self->{insertion_mode} = AFTER_HEAD_IM;
4623                    !!!next-token;
4624                    next B;
4625                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4626                    !!!cp ('t133');
4627                    ## As if </noscript>
4628                    pop @{$self->{open_elements}};
4629                    !!!parse-error (type => 'in noscript:/',
4630                                    text => 'head', token => $token);
4631                    
4632                    ## Reprocess in the "in head" insertion mode...
4633                    pop @{$self->{open_elements}};
4634                    $self->{insertion_mode} = AFTER_HEAD_IM;
4635                    !!!next-token;
4636                    next B;
4637                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4638                    !!!cp ('t134');
4639                    pop @{$self->{open_elements}};
4640                    $self->{insertion_mode} = AFTER_HEAD_IM;
4641                    !!!next-token;
4642                    next B;
4643                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4644                    !!!cp ('t134.1');
4645                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4646                                    token => $token);
4647                  ## Ignore the token                  ## Ignore the token
4648                  !!!next-token;                  !!!next-token;
4649                  redo B;                  next B;
4650                  } else {
4651                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4652                }                }
4653                              } elsif ($token->{tag_name} eq 'noscript') {
4654                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4655                if ({                  !!!cp ('t136');
4656                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}};
4657                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
4658                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!next-token;
4659                  !!!back-token;                  next B;
4660                  $token = {type => 'end tag',                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4661                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4662                  redo B;                  !!!cp ('t137');
4663                    !!!parse-error (type => 'unmatched end tag',
4664                                    text => 'noscript', token => $token);
4665                    ## Ignore the token ## ISSUE: An issue in the spec.
4666                    !!!next-token;
4667                    next B;
4668                  } else {
4669                    !!!cp ('t138');
4670                    #
4671                }                }
4672                } elsif ({
4673                if ($self->{open_elements}->[-1]->[1] ne 'table') {                        body => 1, html => 1,
4674                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                       }->{$token->{tag_name}}) {
4675                  if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4676                      $self->{insertion_mode} == IN_HEAD_IM or
4677                      $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4678                    !!!cp ('t140');
4679                    !!!parse-error (type => 'unmatched end tag',
4680                                    text => $token->{tag_name}, token => $token);
4681                    ## Ignore the token
4682                    !!!next-token;
4683                    next B;
4684                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4685                    !!!cp ('t140.1');
4686                    !!!parse-error (type => 'unmatched end tag',
4687                                    text => $token->{tag_name}, token => $token);
4688                    ## Ignore the token
4689                    !!!next-token;
4690                    next B;
4691                  } else {
4692                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4693                }                }
4694                } elsif ($token->{tag_name} eq 'p') {
4695                  !!!cp ('t142');
4696                  !!!parse-error (type => 'unmatched end tag',
4697                                  text => $token->{tag_name}, token => $token);
4698                  ## Ignore the token
4699                  !!!next-token;
4700                  next B;
4701                } elsif ($token->{tag_name} eq 'br') {
4702                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4703                    !!!cp ('t142.2');
4704                    ## (before head) as if <head>, (in head) as if </head>
4705                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4706                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4707                    $self->{insertion_mode} = AFTER_HEAD_IM;
4708      
4709                    ## Reprocess in the "after head" insertion mode...
4710                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4711                    !!!cp ('t143.2');
4712                    ## As if </head>
4713                    pop @{$self->{open_elements}};
4714                    $self->{insertion_mode} = AFTER_HEAD_IM;
4715      
4716                    ## Reprocess in the "after head" insertion mode...
4717                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4718                    !!!cp ('t143.3');
4719                    ## ISSUE: Two parse errors for <head><noscript></br>
4720                    !!!parse-error (type => 'unmatched end tag',
4721                                    text => 'br', token => $token);
4722                    ## As if </noscript>
4723                    pop @{$self->{open_elements}};
4724                    $self->{insertion_mode} = IN_HEAD_IM;
4725    
4726                splice @{$self->{open_elements}}, $i;                  ## Reprocess in the "in head" insertion mode...
4727                    ## As if </head>
4728                    pop @{$self->{open_elements}};
4729                    $self->{insertion_mode} = AFTER_HEAD_IM;
4730    
4731                $self->_reset_insertion_mode;                  ## Reprocess in the "after head" insertion mode...
4732                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4733                    !!!cp ('t143.4');
4734                    #
4735                  } else {
4736                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4737                  }
4738    
4739                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4740                  !!!parse-error (type => 'unmatched end tag',
4741                                  text => 'br', token => $token);
4742                  ## Ignore the token
4743                !!!next-token;                !!!next-token;
4744                redo B;                next B;
4745              } elsif ({              } else {
4746                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t145');
4747                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                !!!parse-error (type => 'unmatched end tag',
4748                        thead => 1, tr => 1,                                text => $token->{tag_name}, token => $token);
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4749                ## Ignore the token                ## Ignore the token
4750                !!!next-token;                !!!next-token;
4751                redo B;                next B;
4752                }
4753    
4754                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4755                  !!!cp ('t146');
4756                  ## As if </noscript>
4757                  pop @{$self->{open_elements}};
4758                  !!!parse-error (type => 'in noscript:/',
4759                                  text => $token->{tag_name}, token => $token);
4760                  
4761                  ## Reprocess in the "in head" insertion mode...
4762                  ## As if </head>
4763                  pop @{$self->{open_elements}};
4764    
4765                  ## Reprocess in the "after head" insertion mode...
4766                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4767                  !!!cp ('t147');
4768                  ## As if </head>
4769                  pop @{$self->{open_elements}};
4770    
4771                  ## Reprocess in the "after head" insertion mode...
4772                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4773    ## ISSUE: This case cannot be reached?
4774                  !!!cp ('t148');
4775                  !!!parse-error (type => 'unmatched end tag',
4776                                  text => $token->{tag_name}, token => $token);
4777                  ## Ignore the token ## ISSUE: An issue in the spec.
4778                  !!!next-token;
4779                  next B;
4780              } else {              } else {
4781                #                !!!cp ('t149');
4782              }              }
           } else {  
             #  
           }  
4783    
4784            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
4785            $in_body->($insert_to_foster);              ## As if <body>
4786            redo B;              !!!insert-element ('body',, $token);
4787          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
4788            if ($token->{type} eq 'character') {              ## reprocess
4789              ## NOTE: This is a code clone of "character in body".              next B;
4790          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4791            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4792              !!!cp ('t149.1');
4793    
4794              ## NOTE: As if <head>
4795              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4796              $self->{open_elements}->[-1]->[0]->append_child
4797                  ($self->{head_element});
4798              #push @{$self->{open_elements}},
4799              #    [$self->{head_element}, $el_category->{head}];
4800              #$self->{insertion_mode} = IN_HEAD_IM;
4801              ## NOTE: Reprocess.
4802    
4803              ## NOTE: As if </head>
4804              #pop @{$self->{open_elements}};
4805              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4806              ## NOTE: Reprocess.
4807              
4808              #
4809            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4810              !!!cp ('t149.2');
4811    
4812              ## NOTE: As if </head>
4813              pop @{$self->{open_elements}};
4814              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4815              ## NOTE: Reprocess.
4816    
4817              #
4818            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4819              !!!cp ('t149.3');
4820    
4821              !!!parse-error (type => 'in noscript:#eof', token => $token);
4822    
4823              ## As if </noscript>
4824              pop @{$self->{open_elements}};
4825              #$self->{insertion_mode} = IN_HEAD_IM;
4826              ## NOTE: Reprocess.
4827    
4828              ## NOTE: As if </head>
4829              pop @{$self->{open_elements}};
4830              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4831              ## NOTE: Reprocess.
4832    
4833              #
4834            } else {
4835              !!!cp ('t149.4');
4836              #
4837            }
4838    
4839            ## NOTE: As if <body>
4840            !!!insert-element ('body',, $token);
4841            $self->{insertion_mode} = IN_BODY_IM;
4842            ## NOTE: Reprocess.
4843            next B;
4844          } else {
4845            die "$0: $token->{type}: Unknown token type";
4846          }
4847    
4848              ## ISSUE: An issue in the spec.
4849        } elsif ($self->{insertion_mode} & BODY_IMS) {
4850              if ($token->{type} == CHARACTER_TOKEN) {
4851                !!!cp ('t150');
4852                ## NOTE: There is a code clone of "character in body".
4853              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4854                            
4855              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4856    
4857              !!!next-token;              !!!next-token;
4858              redo B;              next B;
4859            } 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') {  
4860              if ({              if ({
4861                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4862                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4863                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4864                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
4865                    ## have an element in table scope
4866                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
4867                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
4868                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
4869                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
4870                  my $node = $self->{open_elements}->[$_];  
4871                  if ($node->[1] eq 'caption') {                      ## Close the cell
4872                    $i = $_;                      !!!back-token; # <x>
4873                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
4874                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
4875                            table => 1, html => 1,                                line => $token->{line},
4876                           }->{$node->[1]}) {                                column => $token->{column}};
4877                    last INSCOPE;                      next B;
4878                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4879                        !!!cp ('t152');
4880                        ## ISSUE: This case can never be reached, maybe.
4881                        last;
4882                      }
4883                  }                  }
4884                } # INSCOPE  
4885                unless (defined $i) {                  !!!cp ('t153');
4886                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
4887                        text => $token->{tag_name}, token => $token);
4888                  ## Ignore the token                  ## Ignore the token
4889                    !!!nack ('t153.1');
4890                  !!!next-token;                  !!!next-token;
4891                  redo B;                  next B;
4892                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4893                                  !!!parse-error (type => 'not closed', text => 'caption',
4894                ## generate implied end tags                                  token => $token);
4895                if ({                  
4896                     dd => 1, dt => 1, li => 1, p => 1,                  ## NOTE: As if </caption>.
4897                     td => 1, th => 1, tr => 1,                  ## have a table element in table scope
4898                    }->{$self->{open_elements}->[-1]->[1]}) {                  my $i;
4899                  !!!back-token; # <?>                  INSCOPE: {
4900                  $token = {type => 'end tag', tag_name => 'caption'};                    for (reverse 0..$#{$self->{open_elements}}) {
4901                  !!!back-token;                      my $node = $self->{open_elements}->[$_];
4902                  $token = {type => 'end tag',                      if ($node->[1] & CAPTION_EL) {
4903                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        !!!cp ('t155');
4904                  redo B;                        $i = $_;
4905                }                        last INSCOPE;
4906                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
4907                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        !!!cp ('t156');
4908                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        last;
4909                }                      }
4910                      }
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
4911    
4912                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
4913                      !!!parse-error (type => 'start tag not allowed',
4914                                      text => $token->{tag_name}, token => $token);
4915                      ## Ignore the token
4916                      !!!nack ('t157.1');
4917                      !!!next-token;
4918                      next B;
4919                    } # INSCOPE
4920                    
4921                    ## generate implied end tags
4922                    while ($self->{open_elements}->[-1]->[1]
4923                               & END_TAG_OPTIONAL_EL) {
4924                      !!!cp ('t158');
4925                      pop @{$self->{open_elements}};
4926                    }
4927    
4928                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4929                redo B;                    !!!cp ('t159');
4930                      !!!parse-error (type => 'not closed',
4931                                      text => $self->{open_elements}->[-1]->[0]
4932                                          ->manakai_local_name,
4933                                      token => $token);
4934                    } else {
4935                      !!!cp ('t160');
4936                    }
4937                    
4938                    splice @{$self->{open_elements}}, $i;
4939                    
4940                    $clear_up_to_marker->();
4941                    
4942                    $self->{insertion_mode} = IN_TABLE_IM;
4943                    
4944                    ## reprocess
4945                    !!!ack-later;
4946                    next B;
4947                  } else {
4948                    !!!cp ('t161');
4949                    #
4950                  }
4951              } else {              } else {
4952                  !!!cp ('t162');
4953                #                #
4954              }              }
4955            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4956              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4957                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
4958                my $i;                  ## have an element in table scope
4959                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
4960                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4961                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4962                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4963                    last INSCOPE;                      !!!cp ('t163');
4964                  } elsif ({                      $i = $_;
4965                            table => 1, html => 1,                      last INSCOPE;
4966                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4967                    last INSCOPE;                      !!!cp ('t164');
4968                        last INSCOPE;
4969                      }
4970                    } # INSCOPE
4971                      unless (defined $i) {
4972                        !!!cp ('t165');
4973                        !!!parse-error (type => 'unmatched end tag',
4974                                        text => $token->{tag_name},
4975                                        token => $token);
4976                        ## Ignore the token
4977                        !!!next-token;
4978                        next B;
4979                      }
4980                    
4981                    ## generate implied end tags
4982                    while ($self->{open_elements}->[-1]->[1]
4983                               & END_TAG_OPTIONAL_EL) {
4984                      !!!cp ('t166');
4985                      pop @{$self->{open_elements}};
4986                  }                  }
4987                } # INSCOPE  
4988                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4989                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
4990                      !!!cp ('t167');
4991                      !!!parse-error (type => 'not closed',
4992                                      text => $self->{open_elements}->[-1]->[0]
4993                                          ->manakai_local_name,
4994                                      token => $token);
4995                    } else {
4996                      !!!cp ('t168');
4997                    }
4998                    
4999                    splice @{$self->{open_elements}}, $i;
5000                    
5001                    $clear_up_to_marker->();
5002                    
5003                    $self->{insertion_mode} = IN_ROW_IM;
5004                    
5005                    !!!next-token;
5006                    next B;
5007                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5008                    !!!cp ('t169');
5009                    !!!parse-error (type => 'unmatched end tag',
5010                                    text => $token->{tag_name}, token => $token);
5011                  ## Ignore the token                  ## Ignore the token
5012                  !!!next-token;                  !!!next-token;
5013                  redo B;                  next B;
5014                }                } else {
5015                                  !!!cp ('t170');
5016                ## 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;  
5017                }                }
5018                } elsif ($token->{tag_name} eq 'caption') {
5019                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
5020                    ## have a table element in table scope
5021                    my $i;
5022                    INSCOPE: {
5023                      for (reverse 0..$#{$self->{open_elements}}) {
5024                        my $node = $self->{open_elements}->[$_];
5025                        if ($node->[1] & CAPTION_EL) {
5026                          !!!cp ('t171');
5027                          $i = $_;
5028                          last INSCOPE;
5029                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5030                          !!!cp ('t172');
5031                          last;
5032                        }
5033                      }
5034    
5035                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
5036                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
5037                                      text => $token->{tag_name}, token => $token);
5038                      ## Ignore the token
5039                      !!!next-token;
5040                      next B;
5041                    } # INSCOPE
5042                    
5043                    ## generate implied end tags
5044                    while ($self->{open_elements}->[-1]->[1]
5045                               & END_TAG_OPTIONAL_EL) {
5046                      !!!cp ('t174');
5047                      pop @{$self->{open_elements}};
5048                    }
5049                    
5050                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5051                      !!!cp ('t175');
5052                      !!!parse-error (type => 'not closed',
5053                                      text => $self->{open_elements}->[-1]->[0]
5054                                          ->manakai_local_name,
5055                                      token => $token);
5056                    } else {
5057                      !!!cp ('t176');
5058                    }
5059                    
5060                    splice @{$self->{open_elements}}, $i;
5061                    
5062                    $clear_up_to_marker->();
5063                    
5064                    $self->{insertion_mode} = IN_TABLE_IM;
5065                    
5066                    !!!next-token;
5067                    next B;
5068                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5069                    !!!cp ('t177');
5070                    !!!parse-error (type => 'unmatched end tag',
5071                                    text => $token->{tag_name}, token => $token);
5072                    ## Ignore the token
5073                    !!!next-token;
5074                    next B;
5075                  } else {
5076                    !!!cp ('t178');
5077                    #
5078                }                }
5079                } elsif ({
5080                          table => 1, tbody => 1, tfoot => 1,
5081                          thead => 1, tr => 1,
5082                         }->{$token->{tag_name}} and
5083                         $self->{insertion_mode} == IN_CELL_IM) {
5084                  ## have an element in table scope
5085                  my $i;
5086                  my $tn;
5087                  INSCOPE: {
5088                    for (reverse 0..$#{$self->{open_elements}}) {
5089                      my $node = $self->{open_elements}->[$_];
5090                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5091                        !!!cp ('t179');
5092                        $i = $_;
5093    
5094                        ## Close the cell
5095                        !!!back-token; # </x>
5096                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
5097                                  line => $token->{line},
5098                                  column => $token->{column}};
5099                        next B;
5100                      } elsif ($node->[1] & TABLE_CELL_EL) {
5101                        !!!cp ('t180');
5102                        $tn = $node->[0]->manakai_local_name;
5103                        ## NOTE: There is exactly one |td| or |th| element
5104                        ## in scope in the stack of open elements by definition.
5105                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5106                        ## ISSUE: Can this be reached?
5107                        !!!cp ('t181');
5108                        last;
5109                      }
5110                    }
5111    
5112                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
5113                    !!!parse-error (type => 'unmatched end tag',
5114                $clear_up_to_marker->();                      text => $token->{tag_name}, token => $token);
5115                    ## Ignore the token
5116                $self->{insertion_mode} = 'in table';                  !!!next-token;
5117                    next B;
5118                !!!next-token;                } # INSCOPE
5119                redo B;              } elsif ($token->{tag_name} eq 'table' and
5120              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5121                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5122                                  token => $token);
5123    
5124                ## As if </caption>                ## As if </caption>
5125                ## have a table element in table scope                ## have a table element in table scope
5126                my $i;                my $i;
5127                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5128                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5129                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5130                      !!!cp ('t184');
5131                    $i = $_;                    $i = $_;
5132                    last INSCOPE;                    last INSCOPE;
5133                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5134                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5135                    last INSCOPE;                    last INSCOPE;
5136                  }                  }
5137                } # INSCOPE                } # INSCOPE
5138                unless (defined $i) {                unless (defined $i) {
5139                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5140                    !!!parse-error (type => 'unmatched end tag',
5141                                    text => 'caption', token => $token);
5142                  ## Ignore the token                  ## Ignore the token
5143                  !!!next-token;                  !!!next-token;
5144                  redo B;                  next B;
5145                }                }
5146                                
5147                ## generate implied end tags                ## generate implied end tags
5148                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5149                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5150                     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;  
5151                }                }
5152    
5153                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5154                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5155                    !!!parse-error (type => 'not closed',
5156                                    text => $self->{open_elements}->[-1]->[0]
5157                                        ->manakai_local_name,
5158                                    token => $token);
5159                  } else {
5160                    !!!cp ('t189');
5161                }                }
5162    
5163                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5164    
5165                $clear_up_to_marker->();                $clear_up_to_marker->();
5166    
5167                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5168    
5169                ## reprocess                ## reprocess
5170                redo B;                next B;
5171              } elsif ({              } elsif ({
5172                        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,  
5173                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5174                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5175                ## Ignore the token                  !!!cp ('t190');
5176                redo B;                  !!!parse-error (type => 'unmatched end tag',
5177              } 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');  
5178                  ## Ignore the token                  ## Ignore the token
5179                  !!!next-token;                  !!!next-token;
5180                  redo B;                  next B;
5181                } else {                } else {
5182                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
5183                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
5184                }                }
5185              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
5186                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
5187                          thead => 1, tr => 1,
5188                         }->{$token->{tag_name}} and
5189                         $self->{insertion_mode} == IN_CAPTION_IM) {
5190                  !!!cp ('t192');
5191                  !!!parse-error (type => 'unmatched end tag',
5192                                  text => $token->{tag_name}, token => $token);
5193                ## Ignore the token                ## Ignore the token
5194                !!!next-token;                !!!next-token;
5195                redo B;                next B;
5196              } else {              } else {
5197                #                !!!cp ('t193');
5198                  #
5199              }              }
5200            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5201              #          for my $entry (@{$self->{open_elements}}) {
5202              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5203                !!!cp ('t75');
5204                !!!parse-error (type => 'in body:#eof', token => $token);
5205                last;
5206            }            }
5207            }
5208    
5209            ## As if </colgroup>          ## Stop parsing.
5210            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
5211              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
5212              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
5213          }
5214    
5215          $insert = $insert_to_current;
5216          #
5217        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5218          if ($token->{type} == CHARACTER_TOKEN) {
5219            if (not $open_tables->[-1]->[1] and # tainted
5220                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5221              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5222                  
5223              unless (length $token->{data}) {
5224                !!!cp ('t194');
5225              !!!next-token;              !!!next-token;
5226              redo B;              next B;
5227            } else {            } else {
5228              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5229            }            }
5230          } 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;  
               }  
             }  
5231    
5232              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5233    
5234              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5235              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5236              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5237              ## result in a new Text node.              ## result in a new Text node.
5238              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5239                
5240              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]}) {  
5241                # MUST                # MUST
5242                my $foster_parent_element;                my $foster_parent_element;
5243                my $next_sibling;                my $next_sibling;
5244                my $prev_sibling;                my $prev_sibling;
5245                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5246                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5247                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5248                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5249                        !!!cp ('t196');
5250                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5251                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5252                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5253                    } else {                    } else {
5254                        !!!cp ('t197');
5255                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5256                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5257                    }                    }
# Line 3674  sub _tree_construction_main ($) { Line 5263  sub _tree_construction_main ($) {
5263                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5264                if (defined $prev_sibling and                if (defined $prev_sibling and
5265                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5266                    !!!cp ('t198');
5267                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5268                } else {                } else {
5269                    !!!cp ('t199');
5270                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5271                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5272                     $next_sibling);                     $next_sibling);
5273                }                }
5274              } else {            $open_tables->[-1]->[1] = 1; # tainted
5275                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5276              !!!cp ('t200');
5277              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5278            }
5279                
5280            !!!next-token;
5281            next B;
5282          } elsif ($token->{type} == START_TAG_TOKEN) {
5283            if ({
5284                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5285                 th => 1, td => 1,
5286                }->{$token->{tag_name}}) {
5287              if ($self->{insertion_mode} == IN_TABLE_IM) {
5288                ## Clear back to table context
5289                while (not ($self->{open_elements}->[-1]->[1]
5290                                & TABLE_SCOPING_EL)) {
5291                  !!!cp ('t201');
5292                  pop @{$self->{open_elements}};
5293              }              }
5294                            
5295              !!!next-token;              !!!insert-element ('tbody',, $token);
5296              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5297            } elsif ($token->{type} eq 'comment') {              ## reprocess in the "in table body" insertion mode...
5298              ## Copied from 'in table'            }
5299              my $comment = $self->{document}->create_comment ($token->{data});            
5300              $self->{open_elements}->[-1]->[0]->append_child ($comment);            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5301              !!!next-token;              unless ($token->{tag_name} eq 'tr') {
5302              redo B;                !!!cp ('t202');
5303            } elsif ($token->{type} eq 'start tag') {                !!!parse-error (type => 'missing start tag:tr', token => $token);
5304              if ({              }
5305                   tr => 1,                  
5306                   th => 1, td => 1,              ## Clear back to table body context
5307                  }->{$token->{tag_name}}) {              while (not ($self->{open_elements}->[-1]->[1]
5308                unless ($token->{tag_name} eq 'tr') {                              & TABLE_ROWS_SCOPING_EL)) {
5309                  !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t203');
5310                  ## ISSUE: Can this case be reached?
5311                  pop @{$self->{open_elements}};
5312                }
5313                    
5314                    $self->{insertion_mode} = IN_ROW_IM;
5315                    if ($token->{tag_name} eq 'tr') {
5316                      !!!cp ('t204');
5317                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5318                      !!!nack ('t204');
5319                      !!!next-token;
5320                      next B;
5321                    } else {
5322                      !!!cp ('t205');
5323                      !!!insert-element ('tr',, $token);
5324                      ## reprocess in the "in row" insertion mode
5325                    }
5326                  } else {
5327                    !!!cp ('t206');
5328                }                }
5329    
5330                ## Clear back to table body context                ## Clear back to table row context
5331                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5332                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5333                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5334                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5335                }                }
5336                                
5337                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5338                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5339                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5340                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5341                } else {                
5342                  !!!insert-element ('tr');                !!!nack ('t207.1');
5343                  ## reprocess                !!!next-token;
5344                }                next B;
               redo B;  
5345              } elsif ({              } elsif ({
5346                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5347                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5348                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5349                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5350                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5351                my $i;                  ## As if </tr>
5352                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5353                  my $node = $self->{open_elements}->[$_];                  my $i;
5354                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5355                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5356                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5357                    $i = $_;                      !!!cp ('t208');
5358                    last INSCOPE;                      $i = $_;
5359                  } elsif ({                      last INSCOPE;
5360                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5361                           }->{$node->[1]}) {                      !!!cp ('t209');
5362                    last INSCOPE;                      last INSCOPE;
5363                      }
5364                    } # INSCOPE
5365                    unless (defined $i) {
5366                      !!!cp ('t210');
5367    ## TODO: This type is wrong.
5368                      !!!parse-error (type => 'unmacthed end tag',
5369                                      text => $token->{tag_name}, token => $token);
5370                      ## Ignore the token
5371                      !!!nack ('t210.1');
5372                      !!!next-token;
5373                      next B;
5374                    }
5375                    
5376                    ## Clear back to table row context
5377                    while (not ($self->{open_elements}->[-1]->[1]
5378                                    & TABLE_ROW_SCOPING_EL)) {
5379                      !!!cp ('t211');
5380                      ## ISSUE: Can this case be reached?
5381                      pop @{$self->{open_elements}};
5382                    }
5383                    
5384                    pop @{$self->{open_elements}}; # tr
5385                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5386                    if ($token->{tag_name} eq 'tr') {
5387                      !!!cp ('t212');
5388                      ## reprocess
5389                      !!!ack-later;
5390                      next B;
5391                    } else {
5392                      !!!cp ('t213');
5393                      ## reprocess in the "in table body" insertion mode...
5394                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5395                }                }
5396    
5397                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5398                while (not {                  ## have an element in table scope
5399                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5400                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5401                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5402                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5403                        !!!cp ('t214');
5404                        $i = $_;
5405                        last INSCOPE;
5406                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5407                        !!!cp ('t215');
5408                        last INSCOPE;
5409                      }
5410                    } # INSCOPE
5411                    unless (defined $i) {
5412                      !!!cp ('t216');
5413    ## TODO: This erorr type is wrong.
5414                      !!!parse-error (type => 'unmatched end tag',
5415                                      text => $token->{tag_name}, token => $token);
5416                      ## Ignore the token
5417                      !!!nack ('t216.1');
5418                      !!!next-token;
5419                      next B;
5420                    }
5421    
5422                    ## Clear back to table body context
5423                    while (not ($self->{open_elements}->[-1]->[1]
5424                                    & TABLE_ROWS_SCOPING_EL)) {
5425                      !!!cp ('t217');
5426                      ## ISSUE: Can this state be reached?
5427                      pop @{$self->{open_elements}};
5428                    }
5429                    
5430                    ## As if <{current node}>
5431                    ## have an element in table scope
5432                    ## true by definition
5433                    
5434                    ## Clear back to table body context
5435                    ## nop by definition
5436                    
5437                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5438                    $self->{insertion_mode} = IN_TABLE_IM;
5439                    ## reprocess in "in table" insertion mode...
5440                  } else {
5441                    !!!cp ('t218');
5442                }                }
5443    
5444                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5445                ## have an element in table scope                  ## Clear back to table context
5446                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5447                                    & TABLE_SCOPING_EL)) {
5448                ## Clear back to table body context                    !!!cp ('t219');
5449                ## nop by definition                    ## ISSUE: Can this state be reached?
5450                      pop @{$self->{open_elements}};
5451                pop @{$self->{open_elements}};                  }
5452                $self->{insertion_mode} = 'in table';                  
5453                ## reprocess                  !!!insert-element ('colgroup',, $token);
5454                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5455                    ## reprocess
5456                    !!!ack-later;
5457                    next B;
5458                  } elsif ({
5459                            caption => 1,
5460                            colgroup => 1,
5461                            tbody => 1, tfoot => 1, thead => 1,
5462                           }->{$token->{tag_name}}) {
5463                    ## Clear back to table context
5464                    while (not ($self->{open_elements}->[-1]->[1]
5465                                    & TABLE_SCOPING_EL)) {
5466                      !!!cp ('t220');
5467                      ## ISSUE: Can this state be reached?
5468                      pop @{$self->{open_elements}};
5469                    }
5470                    
5471                    push @$active_formatting_elements, ['#marker', '']
5472                        if $token->{tag_name} eq 'caption';
5473                    
5474                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5475                    $self->{insertion_mode} = {
5476                                               caption => IN_CAPTION_IM,
5477                                               colgroup => IN_COLUMN_GROUP_IM,
5478                                               tbody => IN_TABLE_BODY_IM,
5479                                               tfoot => IN_TABLE_BODY_IM,
5480                                               thead => IN_TABLE_BODY_IM,
5481                                              }->{$token->{tag_name}};
5482                    !!!next-token;
5483                    !!!nack ('t220.1');
5484                    next B;
5485                  } else {
5486                    die "$0: in table: <>: $token->{tag_name}";
5487                  }
5488              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5489                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5490                !!!parse-error (type => 'not closed:table');                                text => $self->{open_elements}->[-1]->[0]
5491                                      ->manakai_local_name,
5492                                  token => $token);
5493    
5494                ## As if </table>                ## As if </table>
5495                ## have a table element in table scope                ## have a table element in table scope
5496                my $i;                my $i;
5497                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5498                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5499                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5500                      !!!cp ('t221');
5501                    $i = $_;                    $i = $_;
5502                    last INSCOPE;                    last INSCOPE;
5503                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5504                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5505                    last INSCOPE;                    last INSCOPE;
5506                  }                  }
5507                } # INSCOPE                } # INSCOPE
5508                unless (defined $i) {                unless (defined $i) {
5509                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5510    ## TODO: The following is wrong, maybe.
5511                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5512                                    token => $token);
5513                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5514                    !!!nack ('t223.1');
5515                  !!!next-token;                  !!!next-token;
5516                  redo B;                  next B;
5517                }                }
5518                                
5519    ## TODO: Followings are removed from the latest spec.
5520                ## generate implied end tags                ## generate implied end tags
5521                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5522                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5523                     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;  
5524                }                }
5525    
5526                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5527                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5528                    ## NOTE: |<table><tr><table>|
5529                    !!!parse-error (type => 'not closed',
5530                                    text => $self->{open_elements}->[-1]->[0]
5531                                        ->manakai_local_name,
5532                                    token => $token);
5533                  } else {
5534                    !!!cp ('t226');
5535                }                }
5536    
5537                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5538                  pop @{$open_tables};
5539    
5540                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5541    
5542                ## reprocess            ## reprocess
5543                redo B;            !!!ack-later;
5544              } else {            next B;
5545                #          } elsif ($token->{tag_name} eq 'style') {
5546              }            if (not $open_tables->[-1]->[1]) { # tainted
5547            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5548              if ({              ## NOTE: This is a "as if in head" code clone.
5549                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5550                  }->{$token->{tag_name}}) {              next B;
5551                ## have an element in table scope            } else {
5552                my $i;              !!!cp ('t227.7');
5553                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5554                  my $node = $self->{open_elements}->[$_];            }
5555                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5556                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5557                    last INSCOPE;              !!!cp ('t227.6');
5558                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5559                            table => 1, html => 1,              $script_start_tag->();
5560                           }->{$node->[1]}) {              next B;
5561                    last INSCOPE;            } else {
5562                  }              !!!cp ('t227.5');
5563                } # INSCOPE              #
5564                unless (defined $i) {            }
5565                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5566                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5567                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5568                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5569                }                if ($type eq 'hidden') {
5570                    !!!cp ('t227.3');
5571                    !!!parse-error (type => 'in table',
5572                                    text => $token->{tag_name}, token => $token);
5573    
5574                ## 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}};  
               }  
5575    
5576                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;  
               }  
5577    
               ## 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]);  
5578                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
5579    
5580                ## Clear back to table body context                  !!!next-token;
5581                ## nop by definition                  !!!ack ('t227.2.1');
5582                    next B;
5583                pop @{$self->{open_elements}};                } else {
5584                $self->{insertion_mode} = 'in table';                  !!!cp ('t227.2');
5585                ## reprocess                  #
5586                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;  
5587              } else {              } else {
5588                  !!!cp ('t227.1');
5589                #                #
5590              }              }
5591            } else {            } else {
5592                !!!cp ('t227.4');
5593              #              #
5594            }            }
5595                      } else {
5596            ## As if in table            !!!cp ('t227');
5597            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5598            $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');  
5599    
5600              ## As if in body, but insert into foster parent element          !!!parse-error (type => 'in table', text => $token->{tag_name},
5601              ## 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';  
5602    
5603                push @$active_formatting_elements, ['#marker', ''];          $insert = $insert_to_foster;
5604                          #
5605                !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
5606                redo B;              if ($token->{tag_name} eq 'tr' and
5607              } 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>  
5608                ## have an element in table scope                ## have an element in table scope
5609                my $i;                my $i;
5610                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5611                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5612                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5613                      !!!cp ('t228');
5614                    $i = $_;                    $i = $_;
5615                    last INSCOPE;                    last INSCOPE;
5616                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5617                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5618                    last INSCOPE;                    last INSCOPE;
5619                  }                  }
5620                } # INSCOPE                } # INSCOPE
5621                unless (defined $i) {                unless (defined $i) {
5622                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5623                    !!!parse-error (type => 'unmatched end tag',
5624                                    text => $token->{tag_name}, token => $token);
5625                  ## Ignore the token                  ## Ignore the token
5626                    !!!nack ('t230.1');
5627                  !!!next-token;                  !!!next-token;
5628                  redo B;                  next B;
5629                  } else {
5630                    !!!cp ('t232');
5631                }                }
5632    
5633                ## Clear back to table row context                ## Clear back to table row context
5634                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5635                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5636                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5637                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5638                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5639                }                }
5640    
5641                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5642                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5643                ## reprocess                !!!next-token;
5644                redo B;                !!!nack ('t231.1');
5645                  next B;
5646              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5647                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5648                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5649                    ## have an element in table scope
5650                ## As if </table>                  my $i;
5651                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5652                my $i;                    my $node = $self->{open_elements}->[$_];
5653                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5654                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5655                  if ($node->[1] eq 'table') {                      $i = $_;
5656                    $i = $_;                      last INSCOPE;
5657                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5658                  } elsif ({                      !!!cp ('t234');
5659                            table => 1, html => 1,                      last INSCOPE;
5660                           }->{$node->[1]}) {                    }
5661                    last INSCOPE;                  } # INSCOPE
5662                    unless (defined $i) {
5663                      !!!cp ('t235');
5664    ## TODO: The following is wrong.
5665                      !!!parse-error (type => 'unmatched end tag',
5666                                      text => $token->{type}, token => $token);
5667                      ## Ignore the token
5668                      !!!nack ('t236.1');
5669                      !!!next-token;
5670                      next B;
5671                  }                  }
5672                } # INSCOPE                  
5673                unless (defined $i) {                  ## Clear back to table row context
5674                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5675                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5676                  !!!next-token;                    !!!cp ('t236');
5677                  redo B;  ## ISSUE: Can this state be reached?
5678                }                    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;  
5679                  }                  }
5680                } # INSCOPE                  
5681                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5682                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5683                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5684                  !!!next-token;                }
5685                  redo B;  
5686                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5687                    ## have an element in table scope
5688                ## Clear back to table row context                  my $i;
5689                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5690                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5691                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5692                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5693                        $i = $_;
5694                        last INSCOPE;
5695                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5696                        !!!cp ('t238');
5697                        last INSCOPE;
5698                      }
5699                    } # INSCOPE
5700                    unless (defined $i) {
5701                      !!!cp ('t239');
5702                      !!!parse-error (type => 'unmatched end tag',
5703                                      text => $token->{tag_name}, token => $token);
5704                      ## Ignore the token
5705                      !!!nack ('t239.1');
5706                      !!!next-token;
5707                      next B;
5708                    }
5709                    
5710                    ## Clear back to table body context
5711                    while (not ($self->{open_elements}->[-1]->[1]
5712                                    & TABLE_ROWS_SCOPING_EL)) {
5713                      !!!cp ('t240');
5714                      pop @{$self->{open_elements}};
5715                    }
5716                    
5717                    ## As if <{current node}>
5718                    ## have an element in table scope
5719                    ## true by definition
5720                    
5721                    ## Clear back to table body context
5722                    ## nop by definition
5723                    
5724                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5725                    $self->{insertion_mode} = IN_TABLE_IM;
5726                    ## reprocess in the "in table" insertion mode...
5727                }                }
5728    
5729                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
5730                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
5731                !!!next-token;                ## the code for <table> in the "in table" insertion mode
5732                redo B;                ## is synced with it.
5733              } elsif ($token->{tag_name} eq 'table') {  
5734                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
5735                my $i;                my $i;
5736                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5737                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5738                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
5739                      !!!cp ('t241');
5740                    $i = $_;                    $i = $_;
5741                    last INSCOPE;                    last INSCOPE;
5742                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5743                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5744                    last INSCOPE;                    last INSCOPE;
5745                  }                  }
5746                } # INSCOPE                } # INSCOPE
5747                unless (defined $i) {                unless (defined $i) {
5748                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
5749                    !!!parse-error (type => 'unmatched end tag',
5750                                    text => $token->{tag_name}, token => $token);
5751                  ## Ignore the token                  ## Ignore the token
5752                    !!!nack ('t243.1');
5753                  !!!next-token;                  !!!next-token;
5754                  redo B;                  next B;
5755                }                }
5756                    
5757                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
5758                while (not {                pop @{$open_tables};
5759                  tr => 1, html => 1,                
5760                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
5761                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
5762                  pop @{$self->{open_elements}};                !!!next-token;
5763                }                next B;
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
5764              } elsif ({              } elsif ({
5765                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5766                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
5767                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
5768                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
5769                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5770                  my $node = $self->{open_elements}->[$_];                  my $i;
5771                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5772                    $i = $_;                    my $node = $self->{open_elements}->[$_];
5773                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5774                  } elsif ({                      !!!cp ('t247');
5775                            table => 1, html => 1,                      $i = $_;
5776                           }->{$node->[1]}) {                      last INSCOPE;
5777                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5778                        !!!cp ('t248');
5779                        last INSCOPE;
5780                      }
5781                    } # INSCOPE
5782                      unless (defined $i) {
5783                        !!!cp ('t249');
5784                        !!!parse-error (type => 'unmatched end tag',
5785                                        text => $token->{tag_name}, token => $token);
5786                        ## Ignore the token
5787                        !!!nack ('t249.1');
5788                        !!!next-token;
5789                        next B;
5790                      }
5791                    
5792                    ## As if </tr>
5793                    ## have an element in table scope
5794                    my $i;
5795                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5796                      my $node = $self->{open_elements}->[$_];
5797                      if ($node->[1] & TABLE_ROW_EL) {
5798                        !!!cp ('t250');
5799                        $i = $_;
5800                        last INSCOPE;
5801                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5802                        !!!cp ('t251');
5803                        last INSCOPE;
5804                      }
5805                    } # INSCOPE
5806                      unless (defined $i) {
5807                        !!!cp ('t252');
5808                        !!!parse-error (type => 'unmatched end tag',
5809                                        text => 'tr', token => $token);
5810                        ## Ignore the token
5811                        !!!nack ('t252.1');
5812                        !!!next-token;
5813                        next B;
5814                      }
5815                    
5816                    ## Clear back to table row context
5817                    while (not ($self->{open_elements}->[-1]->[1]
5818                                    & TABLE_ROW_SCOPING_EL)) {
5819                      !!!cp ('t253');
5820    ## ISSUE: Can this case be reached?
5821                      pop @{$self->{open_elements}};
5822                  }                  }
5823                } # INSCOPE                  
5824                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5825                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5826                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
5827                }                }
5828    
               ## As if </tr>  
5829                ## have an element in table scope                ## have an element in table scope
5830                my $i;                my $i;
5831                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5832                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5833                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5834                      !!!cp ('t254');
5835                    $i = $_;                    $i = $_;
5836                    last INSCOPE;                    last INSCOPE;
5837                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5838                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5839                    last INSCOPE;                    last INSCOPE;
5840                  }                  }
5841                } # INSCOPE                } # INSCOPE
5842                unless (defined $i) {                unless (defined $i) {
5843                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
5844                    !!!parse-error (type => 'unmatched end tag',
5845                                    text => $token->{tag_name}, token => $token);
5846                  ## Ignore the token                  ## Ignore the token
5847                    !!!nack ('t256.1');
5848                  !!!next-token;                  !!!next-token;
5849                  redo B;                  next B;
5850                }                }
5851    
5852                ## Clear back to table row context                ## Clear back to table body context
5853                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5854                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5855                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5856                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5857                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5858                }                }
5859    
5860                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
5861                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
5862                ## reprocess                !!!nack ('t257.1');
5863                redo B;                !!!next-token;
5864                  next B;
5865              } elsif ({              } elsif ({
5866                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5867                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5868                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5869                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5870                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5871                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5872                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
5873                !!!next-token;                            text => $token->{tag_name}, token => $token);
5874                redo B;            ## Ignore the token
5875              } else {            !!!nack ('t258.1');
5876                #             !!!next-token;
5877              }            next B;
5878            } else {          } else {
5879              #            !!!cp ('t259');
5880            }            !!!parse-error (type => 'in table:/',
5881                              text => $token->{tag_name}, token => $token);
5882    
5883            ## As if in table            $insert = $insert_to_foster;
5884            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5885            $in_body->($insert_to_foster);          }
5886            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5887          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5888            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
5889              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
5890              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
5891                          #
5892              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5893              !!!cp ('t259.2');
5894              #
5895            }
5896    
5897              !!!next-token;          ## Stop parsing
5898              redo B;          last B;
5899            } elsif ($token->{type} eq 'comment') {        } else {
5900              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
5901              my $comment = $self->{document}->create_comment ($token->{data});        }
5902              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5903              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
5904              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5905            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5906              if ({                unless (length $token->{data}) {
5907                   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  
5908                  !!!next-token;                  !!!next-token;
5909                  redo B;                  next B;
5910                }                }
5911                }
5912                ## Close the cell              
5913                !!!back-token; # <?>              !!!cp ('t261');
5914                $token = {type => 'end tag', tag_name => $tn};              #
5915                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
5916              } else {              if ($token->{tag_name} eq 'col') {
5917                  !!!cp ('t262');
5918                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5919                  pop @{$self->{open_elements}};
5920                  !!!ack ('t262.1');
5921                  !!!next-token;
5922                  next B;
5923                } else {
5924                  !!!cp ('t263');
5925                #                #
5926              }              }
5927            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5928              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
5929                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5930                my $i;                  !!!cp ('t264');
5931                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag',
5932                  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});  
5933                  ## Ignore the token                  ## Ignore the token
5934                  !!!next-token;                  !!!next-token;
5935                  redo B;                  next B;
5936                }                } else {
5937                                  !!!cp ('t265');
5938                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
5939                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
5940                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
5941                     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]);  
5942                }                }
5943                } elsif ($token->{tag_name} eq 'col') {
5944                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
5945                  !!!parse-error (type => 'unmatched end tag',
5946                $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});  
5947                ## Ignore the token                ## Ignore the token
5948                !!!next-token;                !!!next-token;
5949                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;  
5950              } else {              } else {
5951                #                !!!cp ('t267');
5952                  #
5953              }              }
5954          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5955            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5956                @{$self->{open_elements}} == 1) { # redundant, maybe
5957              !!!cp ('t270.2');
5958              ## Stop parsing.
5959              last B;
5960            } else {
5961              ## NOTE: As if </colgroup>.
5962              !!!cp ('t270.1');
5963              pop @{$self->{open_elements}}; # colgroup
5964              $self->{insertion_mode} = IN_TABLE_IM;
5965              ## Reprocess.
5966              next B;
5967            }
5968          } else {
5969            die "$0: $token->{type}: Unknown token type";
5970          }
5971    
5972              ## As if </colgroup>
5973              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5974                !!!cp ('t269');
5975    ## TODO: Wrong error type?
5976                !!!parse-error (type => 'unmatched end tag',
5977                                text => 'colgroup', token => $token);
5978                ## Ignore the token
5979                !!!nack ('t269.1');
5980                !!!next-token;
5981                next B;
5982            } else {            } else {
5983              #              !!!cp ('t270');
5984                pop @{$self->{open_elements}}; # colgroup
5985                $self->{insertion_mode} = IN_TABLE_IM;
5986                !!!ack-later;
5987                ## reprocess
5988                next B;
5989              }
5990        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5991          if ($token->{type} == CHARACTER_TOKEN) {
5992            !!!cp ('t271');
5993            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5994            !!!next-token;
5995            next B;
5996          } elsif ($token->{type} == START_TAG_TOKEN) {
5997            if ($token->{tag_name} eq 'option') {
5998              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5999                !!!cp ('t272');
6000                ## As if </option>
6001                pop @{$self->{open_elements}};
6002              } else {
6003                !!!cp ('t273');
6004            }            }
             
           $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}};  
               }  
6005    
6006                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6007                !!!next-token;            !!!nack ('t273.1');
6008                redo B;            !!!next-token;
6009              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6010                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6011                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6012                  pop @{$self->{open_elements}};              !!!cp ('t274');
6013                }              ## As if </option>
6014                pop @{$self->{open_elements}};
6015              } else {
6016                !!!cp ('t275');
6017              }
6018    
6019                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6020                  ## As if </optgroup>              !!!cp ('t276');
6021                  pop @{$self->{open_elements}};              ## As if </optgroup>
6022                }              pop @{$self->{open_elements}};
6023              } else {
6024                !!!cp ('t277');
6025              }
6026    
6027                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6028                !!!next-token;            !!!nack ('t277.1');
6029                redo B;            !!!next-token;
6030              } elsif ($token->{tag_name} eq 'select') {            next B;
6031                !!!parse-error (type => 'not closed:select');          } elsif ({
6032                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6033                ## have an element in table scope                   }->{$token->{tag_name}} or
6034                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6035                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6036                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6037                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6038                    $i = $_;                     tr => 1, td => 1, th => 1,
6039                    last INSCOPE;                    }->{$token->{tag_name}})) {
6040                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6041                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6042                           }->{$node->[1]}) {                            token => $token);
6043                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6044                  }            ## as if there were </select> (otherwise).
6045                } # INSCOPE            ## have an element in table scope
6046                unless (defined $i) {            my $i;
6047                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6048                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6049                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6050                  redo B;                !!!cp ('t278');
6051                }                $i = $_;
6052                  last INSCOPE;
6053                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6054                  !!!cp ('t279');
6055                  last INSCOPE;
6056                }
6057              } # INSCOPE
6058              unless (defined $i) {
6059                !!!cp ('t280');
6060                !!!parse-error (type => 'unmatched end tag',
6061                                text => 'select', token => $token);
6062                ## Ignore the token
6063                !!!nack ('t280.1');
6064                !!!next-token;
6065                next B;
6066              }
6067                                
6068                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6069              splice @{$self->{open_elements}}, $i;
6070    
6071                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6072    
6073                !!!next-token;            if ($token->{tag_name} eq 'select') {
6074                redo B;              !!!nack ('t281.2');
6075              } else {              !!!next-token;
6076                #              next B;
6077              } else {
6078                !!!cp ('t281.1');
6079                !!!ack-later;
6080                ## Reprocess the token.
6081                next B;
6082              }
6083            } else {
6084              !!!cp ('t282');
6085              !!!parse-error (type => 'in select',
6086                              text => $token->{tag_name}, token => $token);
6087              ## Ignore the token
6088              !!!nack ('t282.1');
6089              !!!next-token;
6090              next B;
6091            }
6092          } elsif ($token->{type} == END_TAG_TOKEN) {
6093            if ($token->{tag_name} eq 'optgroup') {
6094              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6095                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6096                !!!cp ('t283');
6097                ## As if </option>
6098                splice @{$self->{open_elements}}, -2;
6099              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6100                !!!cp ('t284');
6101                pop @{$self->{open_elements}};
6102              } else {
6103                !!!cp ('t285');
6104                !!!parse-error (type => 'unmatched end tag',
6105                                text => $token->{tag_name}, token => $token);
6106                ## Ignore the token
6107              }
6108              !!!nack ('t285.1');
6109              !!!next-token;
6110              next B;
6111            } elsif ($token->{tag_name} eq 'option') {
6112              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6113                !!!cp ('t286');
6114                pop @{$self->{open_elements}};
6115              } else {
6116                !!!cp ('t287');
6117                !!!parse-error (type => 'unmatched end tag',
6118                                text => $token->{tag_name}, token => $token);
6119                ## Ignore the token
6120              }
6121              !!!nack ('t287.1');
6122              !!!next-token;
6123              next B;
6124            } elsif ($token->{tag_name} eq 'select') {
6125              ## have an element in table scope
6126              my $i;
6127              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6128                my $node = $self->{open_elements}->[$_];
6129                if ($node->[1] & SELECT_EL) {
6130                  !!!cp ('t288');
6131                  $i = $_;
6132                  last INSCOPE;
6133                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6134                  !!!cp ('t289');
6135                  last INSCOPE;
6136              }              }
6137            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6138              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6139                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6140                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6141                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6142                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6143                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6144                  pop @{$self->{open_elements}};              !!!next-token;
6145                } else {              next B;
6146                  !!!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;  
               }  
6147                                
6148                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6149              splice @{$self->{open_elements}}, $i;
6150    
6151                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6152    
6153                !!!next-token;            !!!nack ('t291.1');
6154                redo B;            !!!next-token;
6155              } elsif ({            next B;
6156                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6157                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6158                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6159                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6160                                   }->{$token->{tag_name}}) {
6161                ## have an element in table scope  ## TODO: The following is wrong?
6162                my $i;            !!!parse-error (type => 'unmatched end tag',
6163                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;  
               }  
6164                                
6165                ## As if </select>            ## have an element in table scope
6166                ## have an element in table scope            my $i;
6167                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6168                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6169                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6170                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6171                    $i = $_;                $i = $_;
6172                    last INSCOPE;                last INSCOPE;
6173                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6174                            table => 1, html => 1,                !!!cp ('t293');
6175                           }->{$node->[1]}) {                last INSCOPE;
6176                    last INSCOPE;              }
6177                  }            } # INSCOPE
6178                } # INSCOPE            unless (defined $i) {
6179                unless (defined $i) {              !!!cp ('t294');
6180                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6181                  ## Ignore the </select> token              !!!nack ('t294.1');
6182                  !!!next-token; ## TODO: ok?              !!!next-token;
6183                  redo B;              next B;
6184                }            }
6185                                
6186                splice @{$self->{open_elements}}, $i;            ## As if </select>
6187              ## have an element in table scope
6188                $self->_reset_insertion_mode;            undef $i;
6189              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6190                ## reprocess              my $node = $self->{open_elements}->[$_];
6191                redo B;              if ($node->[1] & SELECT_EL) {
6192              } else {                !!!cp ('t295');
6193                #                $i = $_;
6194                  last INSCOPE;
6195                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6196    ## ISSUE: Can this state be reached?
6197                  !!!cp ('t296');
6198                  last INSCOPE;
6199              }              }
6200            } else {            } # INSCOPE
6201              #            unless (defined $i) {
6202                !!!cp ('t297');
6203    ## TODO: The following error type is correct?
6204                !!!parse-error (type => 'unmatched end tag',
6205                                text => 'select', token => $token);
6206                ## Ignore the </select> token
6207                !!!nack ('t297.1');
6208                !!!next-token; ## TODO: ok?
6209                next B;
6210            }            }
6211                  
6212              !!!cp ('t298');
6213              splice @{$self->{open_elements}}, $i;
6214    
6215            !!!parse-error (type => 'in select:'.$token->{tag_name});            $self->_reset_insertion_mode;
6216    
6217              !!!ack-later;
6218              ## reprocess
6219              next B;
6220            } else {
6221              !!!cp ('t299');
6222              !!!parse-error (type => 'in select:/',
6223                              text => $token->{tag_name}, token => $token);
6224            ## Ignore the token            ## Ignore the token
6225              !!!nack ('t299.3');
6226            !!!next-token;            !!!next-token;
6227            redo B;            next B;
6228          } elsif ($self->{insertion_mode} eq 'after body') {          }
6229            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6230              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6231                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
6232                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
6233                            !!!parse-error (type => 'in body:#eof', token => $token);
6234                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6235              !!!cp ('t299.2');
6236            }
6237    
6238                unless (length $token->{data}) {          ## Stop parsing.
6239                  !!!next-token;          last B;
6240                  redo B;        } else {
6241                }          die "$0: $token->{type}: Unknown token type";
6242              }        }
6243                    } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6244              #        if ($token->{type} == CHARACTER_TOKEN) {
6245              !!!parse-error (type => 'after body:#'.$token->{type});          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6246            } elsif ($token->{type} eq 'comment') {            my $data = $1;
6247              my $comment = $self->{document}->create_comment ($token->{data});            ## As if in body
6248              $self->{open_elements}->[0]->[0]->append_child ($comment);            $reconstruct_active_formatting_elements->($insert_to_current);
6249                  
6250              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6251              
6252              unless (length $token->{data}) {
6253                !!!cp ('t300');
6254              !!!next-token;              !!!next-token;
6255              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});  
6256            }            }
6257            }
6258            
6259            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6260              !!!cp ('t301');
6261              !!!parse-error (type => 'after html:#text', token => $token);
6262    
6263            $self->{insertion_mode} = 'in body';            ## Reprocess in the "after body" insertion mode.
6264            ## reprocess          } else {
6265            redo B;            !!!cp ('t302');
6266          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6267            if ($token->{type} eq 'character') {          
6268              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## "after body" insertion mode
6269                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          !!!parse-error (type => 'after body:#text', token => $token);
6270    
6271                unless (length $token->{data}) {          $self->{insertion_mode} = IN_BODY_IM;
6272                  !!!next-token;          ## reprocess
6273                  redo B;          next B;
6274                }        } elsif ($token->{type} == START_TAG_TOKEN) {
6275              }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6276              !!!cp ('t303');
6277              !!!parse-error (type => 'after html',
6278                              text => $token->{tag_name}, token => $token);
6279              
6280              ## Reprocess in the "after body" insertion mode.
6281            } else {
6282              !!!cp ('t304');
6283            }
6284    
6285              #          ## "after body" insertion mode
6286            } elsif ($token->{type} eq 'comment') {          !!!parse-error (type => 'after body',
6287              my $comment = $self->{document}->create_comment ($token->{data});                          text => $token->{tag_name}, token => $token);
6288              $self->{open_elements}->[-1]->[0]->append_child ($comment);  
6289            $self->{insertion_mode} = IN_BODY_IM;
6290            !!!ack-later;
6291            ## reprocess
6292            next B;
6293          } elsif ($token->{type} == END_TAG_TOKEN) {
6294            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6295              !!!cp ('t305');
6296              !!!parse-error (type => 'after html:/',
6297                              text => $token->{tag_name}, token => $token);
6298              
6299              $self->{insertion_mode} = AFTER_BODY_IM;
6300              ## Reprocess in the "after body" insertion mode.
6301            } else {
6302              !!!cp ('t306');
6303            }
6304    
6305            ## "after body" insertion mode
6306            if ($token->{tag_name} eq 'html') {
6307              if (defined $self->{inner_html_node}) {
6308                !!!cp ('t307');
6309                !!!parse-error (type => 'unmatched end tag',
6310                                text => 'html', token => $token);
6311                ## Ignore the token
6312              !!!next-token;              !!!next-token;
6313              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 {  
               #  
             }  
6314            } else {            } else {
6315              #              !!!cp ('t308');
6316                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6317                !!!next-token;
6318                next B;
6319            }            }
6320            } else {
6321              !!!cp ('t309');
6322              !!!parse-error (type => 'after body:/',
6323                              text => $token->{tag_name}, token => $token);
6324    
6325              $self->{insertion_mode} = IN_BODY_IM;
6326              ## reprocess
6327              next B;
6328            }
6329          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6330            !!!cp ('t309.2');
6331            ## Stop parsing
6332            last B;
6333          } else {
6334            die "$0: $token->{type}: Unknown token type";
6335          }
6336        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6337          if ($token->{type} == CHARACTER_TOKEN) {
6338            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6339              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6340                        
6341            if (defined $token->{tag_name}) {            unless (length $token->{data}) {
6342              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t310');
6343                !!!next-token;
6344                next B;
6345              }
6346            }
6347            
6348            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6349              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6350                !!!cp ('t311');
6351                !!!parse-error (type => 'in frameset:#text', token => $token);
6352              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6353                !!!cp ('t312');
6354                !!!parse-error (type => 'after frameset:#text', token => $token);
6355              } else { # "after after frameset"
6356                !!!cp ('t313');
6357                !!!parse-error (type => 'after html:#text', token => $token);
6358              }
6359              
6360              ## Ignore the token.
6361              if (length $token->{data}) {
6362                !!!cp ('t314');
6363                ## reprocess the rest of characters
6364            } else {            } else {
6365              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t315');
6366                !!!next-token;
6367              }
6368              next B;
6369            }
6370            
6371            die qq[$0: Character "$token->{data}"];
6372          } elsif ($token->{type} == START_TAG_TOKEN) {
6373            if ($token->{tag_name} eq 'frameset' and
6374                $self->{insertion_mode} == IN_FRAMESET_IM) {
6375              !!!cp ('t318');
6376              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6377              !!!nack ('t318.1');
6378              !!!next-token;
6379              next B;
6380            } elsif ($token->{tag_name} eq 'frame' and
6381                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6382              !!!cp ('t319');
6383              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6384              pop @{$self->{open_elements}};
6385              !!!ack ('t319.1');
6386              !!!next-token;
6387              next B;
6388            } elsif ($token->{tag_name} eq 'noframes') {
6389              !!!cp ('t320');
6390              ## NOTE: As if in head.
6391              $parse_rcdata->(CDATA_CONTENT_MODEL);
6392              next B;
6393    
6394              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6395              ## has no parse error.
6396            } else {
6397              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6398                !!!cp ('t321');
6399                !!!parse-error (type => 'in frameset',
6400                                text => $token->{tag_name}, token => $token);
6401              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6402                !!!cp ('t322');
6403                !!!parse-error (type => 'after frameset',
6404                                text => $token->{tag_name}, token => $token);
6405              } else { # "after after frameset"
6406                !!!cp ('t322.2');
6407                !!!parse-error (type => 'after after frameset',
6408                                text => $token->{tag_name}, token => $token);
6409            }            }
6410            ## Ignore the token            ## Ignore the token
6411              !!!nack ('t322.1');
6412            !!!next-token;            !!!next-token;
6413            redo B;            next B;
6414          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6415            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6416              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{tag_name} eq 'frameset' and
6417                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{insertion_mode} == IN_FRAMESET_IM) {
6418              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6419                  @{$self->{open_elements}} == 1) {
6420                !!!cp ('t325');
6421                !!!parse-error (type => 'unmatched end tag',
6422                                text => $token->{tag_name}, token => $token);
6423                ## Ignore the token
6424                !!!next-token;
6425              } else {
6426                !!!cp ('t326');
6427                pop @{$self->{open_elements}};
6428                !!!next-token;
6429              }
6430    
6431                unless (length $token->{data}) {            if (not defined $self->{inner_html_node} and
6432                  !!!next-token;                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6433                  redo B;              !!!cp ('t327');
6434                }              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6435              } else {
6436                !!!cp ('t328');
6437              }
6438              next B;
6439            } elsif ($token->{tag_name} eq 'html' and
6440                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6441              !!!cp ('t329');
6442              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6443              !!!next-token;
6444              next B;
6445            } else {
6446              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6447                !!!cp ('t330');
6448                !!!parse-error (type => 'in frameset:/',
6449                                text => $token->{tag_name}, token => $token);
6450              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6451                !!!cp ('t330.1');
6452                !!!parse-error (type => 'after frameset:/',
6453                                text => $token->{tag_name}, token => $token);
6454              } else { # "after after html"
6455                !!!cp ('t331');
6456                !!!parse-error (type => 'after after frameset:/',
6457                                text => $token->{tag_name}, token => $token);
6458              }
6459              ## Ignore the token
6460              !!!next-token;
6461              next B;
6462            }
6463          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6464            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6465                    @{$self->{open_elements}} == 1) { # redundant, maybe
6466              !!!cp ('t331.1');
6467              !!!parse-error (type => 'in body:#eof', token => $token);
6468            } else {
6469              !!!cp ('t331.2');
6470            }
6471            
6472            ## Stop parsing
6473            last B;
6474          } else {
6475            die "$0: $token->{type}: Unknown token type";
6476          }
6477    
6478          ## ISSUE: An issue in spec here
6479        } else {
6480          die "$0: $self->{insertion_mode}: Unknown insertion mode";
6481        }
6482    
6483        ## "in body" insertion mode
6484        if ($token->{type} == START_TAG_TOKEN) {
6485          if ($token->{tag_name} eq 'script') {
6486            !!!cp ('t332');
6487            ## NOTE: This is an "as if in head" code clone
6488            $script_start_tag->();
6489            next B;
6490          } elsif ($token->{tag_name} eq 'style') {
6491            !!!cp ('t333');
6492            ## NOTE: This is an "as if in head" code clone
6493            $parse_rcdata->(CDATA_CONTENT_MODEL);
6494            next B;
6495          } elsif ({
6496                    base => 1, link => 1,
6497                   }->{$token->{tag_name}}) {
6498            !!!cp ('t334');
6499            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6500            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6501            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6502            !!!ack ('t334.1');
6503            !!!next-token;
6504            next B;
6505          } elsif ($token->{tag_name} eq 'meta') {
6506            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6507            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6508            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6509    
6510            unless ($self->{confident}) {
6511              if ($token->{attributes}->{charset}) {
6512                !!!cp ('t335');
6513                ## NOTE: Whether the encoding is supported or not is handled
6514                ## in the {change_encoding} callback.
6515                $self->{change_encoding}
6516                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6517                
6518                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6519                    ->set_user_data (manakai_has_reference =>
6520                                         $token->{attributes}->{charset}
6521                                             ->{has_reference});
6522              } elsif ($token->{attributes}->{content}) {
6523                if ($token->{attributes}->{content}->{value}
6524                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6525                        [\x09-\x0D\x20]*=
6526                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6527                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6528                  !!!cp ('t336');
6529                  ## NOTE: Whether the encoding is supported or not is handled
6530                  ## in the {change_encoding} callback.
6531                  $self->{change_encoding}
6532                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6533                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6534                      ->set_user_data (manakai_has_reference =>
6535                                           $token->{attributes}->{content}
6536                                                 ->{has_reference});
6537              }              }
6538              }
6539            } else {
6540              if ($token->{attributes}->{charset}) {
6541                !!!cp ('t337');
6542                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6543                    ->set_user_data (manakai_has_reference =>
6544                                         $token->{attributes}->{charset}
6545                                             ->{has_reference});
6546              }
6547              if ($token->{attributes}->{content}) {
6548                !!!cp ('t338');
6549                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6550                    ->set_user_data (manakai_has_reference =>
6551                                         $token->{attributes}->{content}
6552                                             ->{has_reference});
6553              }
6554            }
6555    
6556              #          !!!ack ('t338.1');
6557            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6558              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6559              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6560              !!!next-token;          !!!cp ('t341');
6561              redo B;          ## NOTE: This is an "as if in head" code clone
6562            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6563              if ($token->{tag_name} eq 'noframes') {          next B;
6564                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6565                redo B;          !!!parse-error (type => 'in body', text => 'body', token => $token);
6566              } else {                
6567                #          if (@{$self->{open_elements}} == 1 or
6568                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6569              !!!cp ('t342');
6570              ## Ignore the token
6571            } else {
6572              my $body_el = $self->{open_elements}->[1]->[0];
6573              for my $attr_name (keys %{$token->{attributes}}) {
6574                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6575                  !!!cp ('t343');
6576                  $body_el->set_attribute_ns
6577                    (undef, [undef, $attr_name],
6578                     $token->{attributes}->{$attr_name}->{value});
6579              }              }
6580            } elsif ($token->{type} eq 'end tag') {            }
6581              if ($token->{tag_name} eq 'html') {          }
6582                $phase = 'trailing end';          !!!nack ('t343.1');
6583            !!!next-token;
6584            next B;
6585          } elsif ({
6586                    address => 1, blockquote => 1, center => 1, dir => 1,
6587                    div => 1, dl => 1, fieldset => 1,
6588                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6589                    menu => 1, ol => 1, p => 1, ul => 1,
6590                    pre => 1, listing => 1,
6591                    form => 1,
6592                    table => 1,
6593                    hr => 1,
6594                   }->{$token->{tag_name}}) {
6595            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6596              !!!cp ('t350');
6597              !!!parse-error (type => 'in form:form', token => $token);
6598              ## Ignore the token
6599              !!!nack ('t350.1');
6600              !!!next-token;
6601              next B;
6602            }
6603    
6604            ## has a p element in scope
6605            INSCOPE: for (reverse @{$self->{open_elements}}) {
6606              if ($_->[1] & P_EL) {
6607                !!!cp ('t344');
6608                !!!back-token; # <form>
6609                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6610                          line => $token->{line}, column => $token->{column}};
6611                next B;
6612              } elsif ($_->[1] & SCOPING_EL) {
6613                !!!cp ('t345');
6614                last INSCOPE;
6615              }
6616            } # INSCOPE
6617              
6618            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6619            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6620              !!!nack ('t346.1');
6621              !!!next-token;
6622              if ($token->{type} == CHARACTER_TOKEN) {
6623                $token->{data} =~ s/^\x0A//;
6624                unless (length $token->{data}) {
6625                  !!!cp ('t346');
6626                !!!next-token;                !!!next-token;
               redo B;  
6627              } else {              } else {
6628                #                !!!cp ('t349');
6629              }              }
6630            } else {            } else {
6631              #              !!!cp ('t348');
6632            }            }
6633            } elsif ($token->{tag_name} eq 'form') {
6634              !!!cp ('t347.1');
6635              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6636    
6637              !!!nack ('t347.2');
6638              !!!next-token;
6639            } elsif ($token->{tag_name} eq 'table') {
6640              !!!cp ('t382');
6641              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6642                        
6643            if (defined $token->{tag_name}) {            $self->{insertion_mode} = IN_TABLE_IM;
6644              !!!parse-error (type => 'after frameset:'.$token->{tag_name});  
6645              !!!nack ('t382.1');
6646              !!!next-token;
6647            } elsif ($token->{tag_name} eq 'hr') {
6648              !!!cp ('t386');
6649              pop @{$self->{open_elements}};
6650            
6651              !!!nack ('t386.1');
6652              !!!next-token;
6653            } else {
6654              !!!nack ('t347.1');
6655              !!!next-token;
6656            }
6657            next B;
6658          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6659            ## has a p element in scope
6660            INSCOPE: for (reverse @{$self->{open_elements}}) {
6661              if ($_->[1] & P_EL) {
6662                !!!cp ('t353');
6663                !!!back-token; # <x>
6664                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6665                          line => $token->{line}, column => $token->{column}};
6666                next B;
6667              } elsif ($_->[1] & SCOPING_EL) {
6668                !!!cp ('t354');
6669                last INSCOPE;
6670              }
6671            } # INSCOPE
6672              
6673            ## Step 1
6674            my $i = -1;
6675            my $node = $self->{open_elements}->[$i];
6676            my $li_or_dtdd = {li => {li => 1},
6677                              dt => {dt => 1, dd => 1},
6678                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6679            LI: {
6680              ## Step 2
6681              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6682                if ($i != -1) {
6683                  !!!cp ('t355');
6684                  !!!parse-error (type => 'not closed',
6685                                  text => $self->{open_elements}->[-1]->[0]
6686                                      ->manakai_local_name,
6687                                  token => $token);
6688                } else {
6689                  !!!cp ('t356');
6690                }
6691                splice @{$self->{open_elements}}, $i;
6692                last LI;
6693            } else {            } else {
6694              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
6695            }            }
6696              
6697              ## Step 3
6698              if (not ($node->[1] & FORMATTING_EL) and
6699                  #not $phrasing_category->{$node->[1]} and
6700                  ($node->[1] & SPECIAL_EL or
6701                   $node->[1] & SCOPING_EL) and
6702                  not ($node->[1] & ADDRESS_EL) and
6703                  not ($node->[1] & DIV_EL)) {
6704                !!!cp ('t358');
6705                last LI;
6706              }
6707              
6708              !!!cp ('t359');
6709              ## Step 4
6710              $i--;
6711              $node = $self->{open_elements}->[$i];
6712              redo LI;
6713            } # LI
6714              
6715            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6716            !!!nack ('t359.1');
6717            !!!next-token;
6718            next B;
6719          } elsif ($token->{tag_name} eq 'plaintext') {
6720            ## has a p element in scope
6721            INSCOPE: for (reverse @{$self->{open_elements}}) {
6722              if ($_->[1] & P_EL) {
6723                !!!cp ('t367');
6724                !!!back-token; # <plaintext>
6725                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6726                          line => $token->{line}, column => $token->{column}};
6727                next B;
6728              } elsif ($_->[1] & SCOPING_EL) {
6729                !!!cp ('t368');
6730                last INSCOPE;
6731              }
6732            } # INSCOPE
6733              
6734            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6735              
6736            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6737              
6738            !!!nack ('t368.1');
6739            !!!next-token;
6740            next B;
6741          } elsif ($token->{tag_name} eq 'a') {
6742            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6743              my $node = $active_formatting_elements->[$i];
6744              if ($node->[1] & A_EL) {
6745                !!!cp ('t371');
6746                !!!parse-error (type => 'in a:a', token => $token);
6747                
6748                !!!back-token; # <a>
6749                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6750                          line => $token->{line}, column => $token->{column}};
6751                $formatting_end_tag->($token);
6752                
6753                AFE2: for (reverse 0..$#$active_formatting_elements) {
6754                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6755                    !!!cp ('t372');
6756                    splice @$active_formatting_elements, $_, 1;
6757                    last AFE2;
6758                  }
6759                } # AFE2
6760                OE: for (reverse 0..$#{$self->{open_elements}}) {
6761                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6762                    !!!cp ('t373');
6763                    splice @{$self->{open_elements}}, $_, 1;
6764                    last OE;
6765                  }
6766                } # OE
6767                last AFE;
6768              } elsif ($node->[0] eq '#marker') {
6769                !!!cp ('t374');
6770                last AFE;
6771              }
6772            } # AFE
6773              
6774            $reconstruct_active_formatting_elements->($insert_to_current);
6775    
6776            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6777            push @$active_formatting_elements, $self->{open_elements}->[-1];
6778    
6779            !!!nack ('t374.1');
6780            !!!next-token;
6781            next B;
6782          } elsif ($token->{tag_name} eq 'nobr') {
6783            $reconstruct_active_formatting_elements->($insert_to_current);
6784    
6785            ## has a |nobr| element in scope
6786            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6787              my $node = $self->{open_elements}->[$_];
6788              if ($node->[1] & NOBR_EL) {
6789                !!!cp ('t376');
6790                !!!parse-error (type => 'in nobr:nobr', token => $token);
6791                !!!back-token; # <nobr>
6792                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6793                          line => $token->{line}, column => $token->{column}};
6794                next B;
6795              } elsif ($node->[1] & SCOPING_EL) {
6796                !!!cp ('t377');
6797                last INSCOPE;
6798              }
6799            } # INSCOPE
6800            
6801            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6802            push @$active_formatting_elements, $self->{open_elements}->[-1];
6803            
6804            !!!nack ('t377.1');
6805            !!!next-token;
6806            next B;
6807          } elsif ($token->{tag_name} eq 'button') {
6808            ## has a button element in scope
6809            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6810              my $node = $self->{open_elements}->[$_];
6811              if ($node->[1] & BUTTON_EL) {
6812                !!!cp ('t378');
6813                !!!parse-error (type => 'in button:button', token => $token);
6814                !!!back-token; # <button>
6815                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6816                          line => $token->{line}, column => $token->{column}};
6817                next B;
6818              } elsif ($node->[1] & SCOPING_EL) {
6819                !!!cp ('t379');
6820                last INSCOPE;
6821              }
6822            } # INSCOPE
6823              
6824            $reconstruct_active_formatting_elements->($insert_to_current);
6825              
6826            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6827    
6828            ## TODO: associate with $self->{form_element} if defined
6829    
6830            push @$active_formatting_elements, ['#marker', ''];
6831    
6832            !!!nack ('t379.1');
6833            !!!next-token;
6834            next B;
6835          } elsif ({
6836                    xmp => 1,
6837                    iframe => 1,
6838                    noembed => 1,
6839                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6840                    noscript => 0, ## TODO: 1 if scripting is enabled
6841                   }->{$token->{tag_name}}) {
6842            if ($token->{tag_name} eq 'xmp') {
6843              !!!cp ('t381');
6844              $reconstruct_active_formatting_elements->($insert_to_current);
6845            } else {
6846              !!!cp ('t399');
6847            }
6848            ## NOTE: There is an "as if in body" code clone.
6849            $parse_rcdata->(CDATA_CONTENT_MODEL);
6850            next B;
6851          } elsif ($token->{tag_name} eq 'isindex') {
6852            !!!parse-error (type => 'isindex', token => $token);
6853            
6854            if (defined $self->{form_element}) {
6855              !!!cp ('t389');
6856            ## Ignore the token            ## Ignore the token
6857              !!!nack ('t389'); ## NOTE: Not acknowledged.
6858            !!!next-token;            !!!next-token;
6859            redo B;            next B;
6860            } else {
6861              !!!ack ('t391.1');
6862    
6863            ## ISSUE: An issue in spec there            my $at = $token->{attributes};
6864              my $form_attrs;
6865              $form_attrs->{action} = $at->{action} if $at->{action};
6866              my $prompt_attr = $at->{prompt};
6867              $at->{name} = {name => 'name', value => 'isindex'};
6868              delete $at->{action};
6869              delete $at->{prompt};
6870              my @tokens = (
6871                            {type => START_TAG_TOKEN, tag_name => 'form',
6872                             attributes => $form_attrs,
6873                             line => $token->{line}, column => $token->{column}},
6874                            {type => START_TAG_TOKEN, tag_name => 'hr',
6875                             line => $token->{line}, column => $token->{column}},
6876                            {type => START_TAG_TOKEN, tag_name => 'p',
6877                             line => $token->{line}, column => $token->{column}},
6878                            {type => START_TAG_TOKEN, tag_name => 'label',
6879                             line => $token->{line}, column => $token->{column}},
6880                           );
6881              if ($prompt_attr) {
6882                !!!cp ('t390');
6883                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6884                               #line => $token->{line}, column => $token->{column},
6885                              };
6886              } else {
6887                !!!cp ('t391');
6888                push @tokens, {type => CHARACTER_TOKEN,
6889                               data => 'This is a searchable index. Insert your search keywords here: ',
6890                               #line => $token->{line}, column => $token->{column},
6891                              }; # SHOULD
6892                ## TODO: make this configurable
6893              }
6894              push @tokens,
6895                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6896                             line => $token->{line}, column => $token->{column}},
6897                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6898                            {type => END_TAG_TOKEN, tag_name => 'label',
6899                             line => $token->{line}, column => $token->{column}},
6900                            {type => END_TAG_TOKEN, tag_name => 'p',
6901                             line => $token->{line}, column => $token->{column}},
6902                            {type => START_TAG_TOKEN, tag_name => 'hr',
6903                             line => $token->{line}, column => $token->{column}},
6904                            {type => END_TAG_TOKEN, tag_name => 'form',
6905                             line => $token->{line}, column => $token->{column}};
6906              !!!back-token (@tokens);
6907              !!!next-token;
6908              next B;
6909            }
6910          } elsif ($token->{tag_name} eq 'textarea') {
6911            my $tag_name = $token->{tag_name};
6912            my $el;
6913            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6914            
6915            ## TODO: $self->{form_element} if defined
6916            $self->{content_model} = RCDATA_CONTENT_MODEL;
6917            delete $self->{escape}; # MUST
6918            
6919            $insert->($el);
6920            
6921            my $text = '';
6922            !!!nack ('t392.1');
6923            !!!next-token;
6924            if ($token->{type} == CHARACTER_TOKEN) {
6925              $token->{data} =~ s/^\x0A//;
6926              unless (length $token->{data}) {
6927                !!!cp ('t392');
6928                !!!next-token;
6929              } else {
6930                !!!cp ('t393');
6931              }
6932          } else {          } else {
6933            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t394');
6934            }
6935            while ($token->{type} == CHARACTER_TOKEN) {
6936              !!!cp ('t395');
6937              $text .= $token->{data};
6938              !!!next-token;
6939            }
6940            if (length $text) {
6941              !!!cp ('t396');
6942              $el->manakai_append_text ($text);
6943            }
6944            
6945            $self->{content_model} = PCDATA_CONTENT_MODEL;
6946            
6947            if ($token->{type} == END_TAG_TOKEN and
6948                $token->{tag_name} eq $tag_name) {
6949              !!!cp ('t397');
6950              ## Ignore the token
6951            } else {
6952              !!!cp ('t398');
6953              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6954          }          }
       }  
     } 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  
6955          !!!next-token;          !!!next-token;
6956          redo B;          next B;
6957        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'rt' or
6958          my $comment = $self->{document}->create_comment ($token->{data});                 $token->{tag_name} eq 'rp') {
6959          $self->{document}->append_child ($comment);          ## has a |ruby| element in scope
6960            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6961              my $node = $self->{open_elements}->[$_];
6962              if ($node->[1] & RUBY_EL) {
6963                !!!cp ('t398.1');
6964                ## generate implied end tags
6965                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6966                  !!!cp ('t398.2');
6967                  pop @{$self->{open_elements}};
6968                }
6969                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6970                  !!!cp ('t398.3');
6971                  !!!parse-error (type => 'not closed',
6972                                  text => $self->{open_elements}->[-1]->[0]
6973                                      ->manakai_local_name,
6974                                  token => $token);
6975                  pop @{$self->{open_elements}}
6976                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6977                }
6978                last INSCOPE;
6979              } elsif ($node->[1] & SCOPING_EL) {
6980                !!!cp ('t398.4');
6981                last INSCOPE;
6982              }
6983            } # INSCOPE
6984    
6985            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6986    
6987            !!!nack ('t398.5');
6988          !!!next-token;          !!!next-token;
6989          redo B;          redo B;
6990        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'math' or
6991          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                 $token->{tag_name} eq 'svg') {
6992            my $data = $1;          $reconstruct_active_formatting_elements->($insert_to_current);
6993            ## As if in the main phase.  
6994            ## NOTE: The insertion mode in the main phase          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
6995            ## just before the phase has been changed to the trailing  
6996            ## end phase is either "after body" or "after frameset".          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6997            $reconstruct_active_formatting_elements->($insert_to_current)  
6998              if $phase eq 'main';          ## "adjust foreign attributes" - done in insert-element-f
6999            
7000            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7001            
7002            if ($self->{self_closing}) {
7003              pop @{$self->{open_elements}};
7004              !!!ack ('t398.1');
7005            } else {
7006              !!!cp ('t398.2');
7007              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7008              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7009              ## mode, "in body" (not "in foreign content") secondary insertion
7010              ## mode, maybe.
7011            }
7012    
7013            !!!next-token;
7014            next B;
7015          } elsif ({
7016                    caption => 1, col => 1, colgroup => 1, frame => 1,
7017                    frameset => 1, head => 1, option => 1, optgroup => 1,
7018                    tbody => 1, td => 1, tfoot => 1, th => 1,
7019                    thead => 1, tr => 1,
7020                   }->{$token->{tag_name}}) {
7021            !!!cp ('t401');
7022            !!!parse-error (type => 'in body',
7023                            text => $token->{tag_name}, token => $token);
7024            ## Ignore the token
7025            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7026            !!!next-token;
7027            next B;
7028            
7029            ## ISSUE: An issue on HTML5 new elements in the spec.
7030          } else {
7031            if ($token->{tag_name} eq 'image') {
7032              !!!cp ('t384');
7033              !!!parse-error (type => 'image', token => $token);
7034              $token->{tag_name} = 'img';
7035            } else {
7036              !!!cp ('t385');
7037            }
7038    
7039            ## NOTE: There is an "as if <br>" code clone.
7040            $reconstruct_active_formatting_elements->($insert_to_current);
7041            
7042            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7043    
7044            if ({
7045                 applet => 1, marquee => 1, object => 1,
7046                }->{$token->{tag_name}}) {
7047              !!!cp ('t380');
7048              push @$active_formatting_elements, ['#marker', ''];
7049              !!!nack ('t380.1');
7050            } elsif ({
7051                      b => 1, big => 1, em => 1, font => 1, i => 1,
7052                      s => 1, small => 1, strile => 1,
7053                      strong => 1, tt => 1, u => 1,
7054                     }->{$token->{tag_name}}) {
7055              !!!cp ('t375');
7056              push @$active_formatting_elements, $self->{open_elements}->[-1];
7057              !!!nack ('t375.1');
7058            } elsif ($token->{tag_name} eq 'input') {
7059              !!!cp ('t388');
7060              ## TODO: associate with $self->{form_element} if defined
7061              pop @{$self->{open_elements}};
7062              !!!ack ('t388.2');
7063            } elsif ({
7064                      area => 1, basefont => 1, bgsound => 1, br => 1,
7065                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7066                      #image => 1,
7067                     }->{$token->{tag_name}}) {
7068              !!!cp ('t388.1');
7069              pop @{$self->{open_elements}};
7070              !!!ack ('t388.3');
7071            } elsif ($token->{tag_name} eq 'select') {
7072              ## TODO: associate with $self->{form_element} if defined
7073            
7074              if ($self->{insertion_mode} & TABLE_IMS or
7075                  $self->{insertion_mode} & BODY_TABLE_IMS or
7076                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7077                !!!cp ('t400.1');
7078                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7079              } else {
7080                !!!cp ('t400.2');
7081                $self->{insertion_mode} = IN_SELECT_IM;
7082              }
7083              !!!nack ('t400.3');
7084            } else {
7085              !!!nack ('t402');
7086            }
7087            
7088            !!!next-token;
7089            next B;
7090          }
7091        } elsif ($token->{type} == END_TAG_TOKEN) {
7092          if ($token->{tag_name} eq 'body') {
7093            ## has a |body| element in scope
7094            my $i;
7095            INSCOPE: {
7096              for (reverse @{$self->{open_elements}}) {
7097                if ($_->[1] & BODY_EL) {
7098                  !!!cp ('t405');
7099                  $i = $_;
7100                  last INSCOPE;
7101                } elsif ($_->[1] & SCOPING_EL) {
7102                  !!!cp ('t405.1');
7103                  last;
7104                }
7105              }
7106    
7107              !!!parse-error (type => 'start tag not allowed',
7108                              text => $token->{tag_name}, token => $token);
7109              ## NOTE: Ignore the token.
7110              !!!next-token;
7111              next B;
7112            } # INSCOPE
7113    
7114            for (@{$self->{open_elements}}) {
7115              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7116                !!!cp ('t403');
7117                !!!parse-error (type => 'not closed',
7118                                text => $_->[0]->manakai_local_name,
7119                                token => $token);
7120                last;
7121              } else {
7122                !!!cp ('t404');
7123              }
7124            }
7125    
7126            $self->{insertion_mode} = AFTER_BODY_IM;
7127            !!!next-token;
7128            next B;
7129          } elsif ($token->{tag_name} eq 'html') {
7130            ## TODO: Update this code.  It seems that the code below is not
7131            ## up-to-date, though it has same effect as speced.
7132            if (@{$self->{open_elements}} > 1 and
7133                $self->{open_elements}->[1]->[1] & BODY_EL) {
7134              ## ISSUE: There is an issue in the spec.
7135              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7136                !!!cp ('t406');
7137                !!!parse-error (type => 'not closed',
7138                                text => $self->{open_elements}->[1]->[0]
7139                                    ->manakai_local_name,
7140                                token => $token);
7141              } else {
7142                !!!cp ('t407');
7143              }
7144              $self->{insertion_mode} = AFTER_BODY_IM;
7145              ## reprocess
7146              next B;
7147            } else {
7148              !!!cp ('t408');
7149              !!!parse-error (type => 'unmatched end tag',
7150                              text => $token->{tag_name}, token => $token);
7151              ## Ignore the token
7152              !!!next-token;
7153              next B;
7154            }
7155          } elsif ({
7156                    address => 1, blockquote => 1, center => 1, dir => 1,
7157                    div => 1, dl => 1, fieldset => 1, listing => 1,
7158                    menu => 1, ol => 1, pre => 1, ul => 1,
7159                    dd => 1, dt => 1, li => 1,
7160                    applet => 1, button => 1, marquee => 1, object => 1,
7161                   }->{$token->{tag_name}}) {
7162            ## has an element in scope
7163            my $i;
7164            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7165              my $node = $self->{open_elements}->[$_];
7166              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7167                !!!cp ('t410');
7168                $i = $_;
7169                last INSCOPE;
7170              } elsif ($node->[1] & SCOPING_EL) {
7171                !!!cp ('t411');
7172                last INSCOPE;
7173              }
7174            } # INSCOPE
7175    
7176            unless (defined $i) { # has an element in scope
7177              !!!cp ('t413');
7178              !!!parse-error (type => 'unmatched end tag',
7179                              text => $token->{tag_name}, token => $token);
7180              ## NOTE: Ignore the token.
7181            } else {
7182              ## Step 1. generate implied end tags
7183              while ({
7184                      ## END_TAG_OPTIONAL_EL
7185                      dd => ($token->{tag_name} ne 'dd'),
7186                      dt => ($token->{tag_name} ne 'dt'),
7187                      li => ($token->{tag_name} ne 'li'),
7188                      p => 1,
7189                      rt => 1,
7190                      rp => 1,
7191                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7192                !!!cp ('t409');
7193                pop @{$self->{open_elements}};
7194              }
7195    
7196              ## Step 2.
7197              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7198                      ne $token->{tag_name}) {
7199                !!!cp ('t412');
7200                !!!parse-error (type => 'not closed',
7201                                text => $self->{open_elements}->[-1]->[0]
7202                                    ->manakai_local_name,
7203                                token => $token);
7204              } else {
7205                !!!cp ('t414');
7206              }
7207    
7208              ## Step 3.
7209              splice @{$self->{open_elements}}, $i;
7210    
7211              ## Step 4.
7212              $clear_up_to_marker->()
7213                  if {
7214                    applet => 1, button => 1, marquee => 1, object => 1,
7215                  }->{$token->{tag_name}};
7216            }
7217            !!!next-token;
7218            next B;
7219          } elsif ($token->{tag_name} eq 'form') {
7220            undef $self->{form_element};
7221    
7222            ## has an element in scope
7223            my $i;
7224            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7225              my $node = $self->{open_elements}->[$_];
7226              if ($node->[1] & FORM_EL) {
7227                !!!cp ('t418');
7228                $i = $_;
7229                last INSCOPE;
7230              } elsif ($node->[1] & SCOPING_EL) {
7231                !!!cp ('t419');
7232                last INSCOPE;
7233              }
7234            } # INSCOPE
7235    
7236            unless (defined $i) { # has an element in scope
7237              !!!cp ('t421');
7238              !!!parse-error (type => 'unmatched end tag',
7239                              text => $token->{tag_name}, token => $token);
7240              ## NOTE: Ignore the token.
7241            } else {
7242              ## Step 1. generate implied end tags
7243              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7244                !!!cp ('t417');
7245                pop @{$self->{open_elements}};
7246              }
7247                        
7248            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
7249              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7250                      ne $token->{tag_name}) {
7251                !!!cp ('t417.1');
7252                !!!parse-error (type => 'not closed',
7253                                text => $self->{open_elements}->[-1]->[0]
7254                                    ->manakai_local_name,
7255                                token => $token);
7256              } else {
7257                !!!cp ('t420');
7258              }  
7259                        
7260            unless (length $token->{data}) {            ## Step 3.
7261              !!!next-token;            splice @{$self->{open_elements}}, $i;
7262              redo B;          }
7263    
7264            !!!next-token;
7265            next B;
7266          } elsif ({
7267                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7268                   }->{$token->{tag_name}}) {
7269            ## has an element in scope
7270            my $i;
7271            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7272              my $node = $self->{open_elements}->[$_];
7273              if ($node->[1] & HEADING_EL) {
7274                !!!cp ('t423');
7275                $i = $_;
7276                last INSCOPE;
7277              } elsif ($node->[1] & SCOPING_EL) {
7278                !!!cp ('t424');
7279                last INSCOPE;
7280            }            }
7281            } # INSCOPE
7282    
7283            unless (defined $i) { # has an element in scope
7284              !!!cp ('t425.1');
7285              !!!parse-error (type => 'unmatched end tag',
7286                              text => $token->{tag_name}, token => $token);
7287              ## NOTE: Ignore the token.
7288            } else {
7289              ## Step 1. generate implied end tags
7290              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7291                !!!cp ('t422');
7292                pop @{$self->{open_elements}};
7293              }
7294              
7295              ## Step 2.
7296              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7297                      ne $token->{tag_name}) {
7298                !!!cp ('t425');
7299                !!!parse-error (type => 'unmatched end tag',
7300                                text => $token->{tag_name}, token => $token);
7301              } else {
7302                !!!cp ('t426');
7303              }
7304    
7305              ## Step 3.
7306              splice @{$self->{open_elements}}, $i;
7307          }          }
7308            
7309            !!!next-token;
7310            next B;
7311          } elsif ($token->{tag_name} eq 'p') {
7312            ## has an element in scope
7313            my $i;
7314            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7315              my $node = $self->{open_elements}->[$_];
7316              if ($node->[1] & P_EL) {
7317                !!!cp ('t410.1');
7318                $i = $_;
7319                last INSCOPE;
7320              } elsif ($node->[1] & SCOPING_EL) {
7321                !!!cp ('t411.1');
7322                last INSCOPE;
7323              }
7324            } # INSCOPE
7325    
7326          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7327          $phase = 'main';            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7328          ## reprocess                    ne $token->{tag_name}) {
7329          redo B;              !!!cp ('t412.1');
7330        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
7331                 $token->{type} eq 'end tag') {                              text => $self->{open_elements}->[-1]->[0]
7332          !!!parse-error (type => 'after html:'.$token->{tag_name});                                  ->manakai_local_name,
7333          $phase = 'main';                              token => $token);
7334          ## reprocess            } else {
7335          redo B;              !!!cp ('t414.1');
7336        } elsif ($token->{type} eq 'end-of-file') {            }
7337          ## Stop parsing  
7338          last B;            splice @{$self->{open_elements}}, $i;
7339            } else {
7340              !!!cp ('t413.1');
7341              !!!parse-error (type => 'unmatched end tag',
7342                              text => $token->{tag_name}, token => $token);
7343    
7344              !!!cp ('t415.1');
7345              ## As if <p>, then reprocess the current token
7346              my $el;
7347              !!!create-element ($el, $HTML_NS, 'p',, $token);
7348              $insert->($el);
7349              ## NOTE: Not inserted into |$self->{open_elements}|.
7350            }
7351    
7352            !!!next-token;
7353            next B;
7354          } elsif ({
7355                    a => 1,
7356                    b => 1, big => 1, em => 1, font => 1, i => 1,
7357                    nobr => 1, s => 1, small => 1, strile => 1,
7358                    strong => 1, tt => 1, u => 1,
7359                   }->{$token->{tag_name}}) {
7360            !!!cp ('t427');
7361            $formatting_end_tag->($token);
7362            next B;
7363          } elsif ($token->{tag_name} eq 'br') {
7364            !!!cp ('t428');
7365            !!!parse-error (type => 'unmatched end tag',
7366                            text => 'br', token => $token);
7367    
7368            ## As if <br>
7369            $reconstruct_active_formatting_elements->($insert_to_current);
7370            
7371            my $el;
7372            !!!create-element ($el, $HTML_NS, 'br',, $token);
7373            $insert->($el);
7374            
7375            ## Ignore the token.
7376            !!!next-token;
7377            next B;
7378          } elsif ({
7379                    caption => 1, col => 1, colgroup => 1, frame => 1,
7380                    frameset => 1, head => 1, option => 1, optgroup => 1,
7381                    tbody => 1, td => 1, tfoot => 1, th => 1,
7382                    thead => 1, tr => 1,
7383                    area => 1, basefont => 1, bgsound => 1,
7384                    embed => 1, hr => 1, iframe => 1, image => 1,
7385                    img => 1, input => 1, isindex => 1, noembed => 1,
7386                    noframes => 1, param => 1, select => 1, spacer => 1,
7387                    table => 1, textarea => 1, wbr => 1,
7388                    noscript => 0, ## TODO: if scripting is enabled
7389                   }->{$token->{tag_name}}) {
7390            !!!cp ('t429');
7391            !!!parse-error (type => 'unmatched end tag',
7392                            text => $token->{tag_name}, token => $token);
7393            ## Ignore the token
7394            !!!next-token;
7395            next B;
7396            
7397            ## ISSUE: Issue on HTML5 new elements in spec
7398            
7399        } else {        } else {
7400          die "$0: $token->{type}: Unknown token";          ## Step 1
7401            my $node_i = -1;
7402            my $node = $self->{open_elements}->[$node_i];
7403    
7404            ## Step 2
7405            S2: {
7406              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7407                ## Step 1
7408                ## generate implied end tags
7409                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7410                  !!!cp ('t430');
7411                  ## NOTE: |<ruby><rt></ruby>|.
7412                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7413                  ## which seems wrong.
7414                  pop @{$self->{open_elements}};
7415                  $node_i++;
7416                }
7417            
7418                ## Step 2
7419                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7420                        ne $token->{tag_name}) {
7421                  !!!cp ('t431');
7422                  ## NOTE: <x><y></x>
7423                  !!!parse-error (type => 'not closed',
7424                                  text => $self->{open_elements}->[-1]->[0]
7425                                      ->manakai_local_name,
7426                                  token => $token);
7427                } else {
7428                  !!!cp ('t432');
7429                }
7430                
7431                ## Step 3
7432                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7433    
7434                !!!next-token;
7435                last S2;
7436              } else {
7437                ## Step 3
7438                if (not ($node->[1] & FORMATTING_EL) and
7439                    #not $phrasing_category->{$node->[1]} and
7440                    ($node->[1] & SPECIAL_EL or
7441                     $node->[1] & SCOPING_EL)) {
7442                  !!!cp ('t433');
7443                  !!!parse-error (type => 'unmatched end tag',
7444                                  text => $token->{tag_name}, token => $token);
7445                  ## Ignore the token
7446                  !!!next-token;
7447                  last S2;
7448                }
7449    
7450                !!!cp ('t434');
7451              }
7452              
7453              ## Step 4
7454              $node_i--;
7455              $node = $self->{open_elements}->[$node_i];
7456              
7457              ## Step 5;
7458              redo S2;
7459            } # S2
7460            next B;
7461        }        }
7462      }      }
7463        next B;
7464      } continue { # B
7465        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7466          ## NOTE: The code below is executed in cases where it does not have
7467          ## to be, but it it is harmless even in those cases.
7468          ## has an element in scope
7469          INSCOPE: {
7470            for (reverse 0..$#{$self->{open_elements}}) {
7471              my $node = $self->{open_elements}->[$_];
7472              if ($node->[1] & FOREIGN_EL) {
7473                last INSCOPE;
7474              } elsif ($node->[1] & SCOPING_EL) {
7475                last;
7476              }
7477            }
7478            
7479            ## NOTE: No foreign element in scope.
7480            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7481          } # INSCOPE
7482        }
7483    } # B    } # B
7484    
7485    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4765  sub _tree_construction_main ($) { Line 7487  sub _tree_construction_main ($) {
7487    ## TODO: script stuffs    ## TODO: script stuffs
7488  } # _tree_construct_main  } # _tree_construct_main
7489    
7490  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7491    my $class = shift;    my $class = shift;
7492    my $node = shift;    my $node = shift;
7493    my $s = \$_[0];    my $s = \$_[0];
7494    my $onerror = $_[1];    my $onerror = $_[1];
7495      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7496    
7497      ## ISSUE: Should {confident} be true?
7498    
7499    my $nt = $node->node_type;    my $nt = $node->node_type;
7500    if ($nt == 9) {    if ($nt == 9) {
# Line 4786  sub set_inner_html ($$$) { Line 7511  sub set_inner_html ($$$) {
7511      }      }
7512    
7513      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7514      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7515    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7516      ## TODO: If non-html element      ## TODO: If non-html element
7517    
7518      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7519    
7520    ## TODO: Support for $get_wrapper
7521    
7522      ## Step 1 # MUST      ## Step 1 # MUST
7523      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
7524      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
7525        $doc->manakai_is_html (1);
7526      my $p = $class->new;      my $p = $class->new;
7527      $p->{document} = $doc;      $p->{document} = $doc;
7528    
7529      ## Step 9 # MUST      ## Step 8 # MUST
7530      my $i = 0;      my $i = 0;
7531      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7532      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7533      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7534        my $self = shift;        my $self = shift;
7535        $self->{next_input_character} = -1 and return if $i >= length $$s;  
7536        $self->{next_input_character} = ord substr $$s, $i++, 1;        pop @{$self->{prev_char}};
7537        $column++;        unshift @{$self->{prev_char}}, $self->{next_char};
7538          
7539        if ($self->{next_input_character} == 0x000D) { # CR        $self->{next_char} = -1 and return if $i >= length $$s;
7540          if ($i >= length $$s) {        $self->{next_char} = ord substr $$s, $i++, 1;
7541            #  
7542          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7543          $p->{column}++;
7544    
7545          if ($self->{next_char} == 0x000A) { # LF
7546            $p->{line}++;
7547            $p->{column} = 0;
7548            !!!cp ('i1');
7549          } elsif ($self->{next_char} == 0x000D) { # CR
7550            $i++ if substr ($$s, $i, 1) eq "\x0A";
7551            $self->{next_char} = 0x000A; # LF # MUST
7552            $p->{line}++;
7553            $p->{column} = 0;
7554            !!!cp ('i2');
7555          } elsif ($self->{next_char} > 0x10FFFF) {
7556            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7557            !!!cp ('i3');
7558          } elsif ($self->{next_char} == 0x0000) { # NULL
7559            !!!cp ('i4');
7560            !!!parse-error (type => 'NULL');
7561            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7562          } elsif ($self->{next_char} <= 0x0008 or
7563                   (0x000E <= $self->{next_char} and
7564                    $self->{next_char} <= 0x001F) or
7565                   (0x007F <= $self->{next_char} and
7566                    $self->{next_char} <= 0x009F) or
7567                   (0xD800 <= $self->{next_char} and
7568                    $self->{next_char} <= 0xDFFF) or
7569                   (0xFDD0 <= $self->{next_char} and
7570                    $self->{next_char} <= 0xFDDF) or
7571                   {
7572                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7573                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7574                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7575                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7576                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7577                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7578                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7579                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7580                    0x10FFFE => 1, 0x10FFFF => 1,
7581                   }->{$self->{next_char}}) {
7582            !!!cp ('i4.1');
7583            if ($self->{next_char} < 0x10000) {
7584              !!!parse-error (type => 'control char',
7585                              text => (sprintf 'U+%04X', $self->{next_char}));
7586          } else {          } else {
7587            my $next_char = ord substr $$s, $i++, 1;            !!!parse-error (type => 'control char',
7588            if ($next_char == 0x000A) { # LF                            text => (sprintf 'U-%08X', $self->{next_char}));
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
7589          }          }
         $self->{next_input_character} = 0x000A; # LF # MUST  
         $line++;  
         $column = -1;  
       } 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  
7590        }        }
7591      };      };
7592        $p->{prev_char} = [-1, -1, -1];
7593        $p->{next_char} = -1;
7594            
7595      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7596        my (%opt) = @_;        my (%opt) = @_;
7597        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7598          my $column = $opt{column};
7599          if (defined $opt{token} and defined $opt{token}->{line}) {
7600            $line = $opt{token}->{line};
7601            $column = $opt{token}->{column};
7602          }
7603          warn "Parse error ($opt{type}) at line $line column $column\n";
7604      };      };
7605      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7606        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7607      };      };
7608            
7609      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7610      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7611    
7612      ## Step 2      ## Step 2
7613      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7614      $p->{content_model_flag} = {      $p->{content_model} = {
7615        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7616        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7617        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7618        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7619        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7620        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7621        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7622        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7623        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7624        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7625      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7626         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7627            unless defined $p->{content_model};
7628            ## ISSUE: What is "the name of the element"? local name?
7629    
7630      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7631          ## TODO: Foreign element OK?
7632    
7633      ## Step 4      ## Step 3
7634      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7635        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7636    
7637      ## Step 5 # MUST      ## Step 4 # MUST
7638      $doc->append_child ($root);      $doc->append_child ($root);
7639    
7640      ## Step 6 # MUST      ## Step 5 # MUST
7641      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7642    
7643      undef $p->{head_element};      undef $p->{head_element};
7644    
7645      ## Step 7 # MUST      ## Step 6 # MUST
7646      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7647    
7648      ## Step 8 # MUST      ## Step 7 # MUST
7649      my $anode = $node;      my $anode = $node;
7650      AN: while (defined $anode) {      AN: while (defined $anode) {
7651        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7652          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7653          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7654            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7655                !!!cp ('i5');
7656              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7657              last AN;              last AN;
7658            }            }
# Line 4888  sub set_inner_html ($$$) { Line 7661  sub set_inner_html ($$$) {
7661        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7662      } # AN      } # AN
7663            
7664      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7665      {      {
7666        my $self = $p;        my $self = $p;
7667        !!!next-token;        !!!next-token;
7668      }      }
7669      $p->_tree_construction_main;      $p->_tree_construction_main;
7670    
7671      ## Step 11 # MUST      ## Step 10 # MUST
7672      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7673      for (@cn) {      for (@cn) {
7674        $node->remove_child ($_);        $node->remove_child ($_);
7675      }      }
7676      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7677    
7678      ## Step 12 # MUST      ## Step 11 # MUST
7679      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7680      for (@cn) {      for (@cn) {
7681          $this_doc->adopt_node ($_);
7682        $node->append_child ($_);        $node->append_child ($_);
7683      }      }
7684      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
7685    
7686      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7687    
7688        delete $p->{parse_error}; # delete loop
7689    } else {    } else {
7690      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";
7691    }    }
# Line 4918  sub set_inner_html ($$$) { Line 7693  sub set_inner_html ($$$) {
7693    
7694  } # tree construction stage  } # tree construction stage
7695    
7696  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7697    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  
7698    
7699  1;  1;
7700  # $Date$  # $Date$

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.163

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24