/[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.137 by wakaba, Sun May 18 03:46:27 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## This is an early version of an HTML parser.  ## ISSUE:
7    ## var doc = implementation.createDocument (null, null, null);
8  my $permitted_slash_tag_name = {  ## doc.write ('');
9    base => 1,  ## alert (doc.compatMode);
10    link => 1,  
11    meta => 1,  ## TODO: 1252 parse error (revision 1264)
12    hr => 1,  ## TODO: 8859-11 = 874 (revision 1271)
13    br => 1,  
14    img=> 1,  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
15    embed => 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
16    param => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
17    area => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
18    col => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
19    input => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
20    
21    sub A_EL () { 0b1 }
22    sub ADDRESS_EL () { 0b10 }
23    sub BODY_EL () { 0b100 }
24    sub BUTTON_EL () { 0b1000 }
25    sub CAPTION_EL () { 0b10000 }
26    sub DD_EL () { 0b100000 }
27    sub DIV_EL () { 0b1000000 }
28    sub DT_EL () { 0b10000000 }
29    sub FORM_EL () { 0b100000000 }
30    sub FORMATTING_EL () { 0b1000000000 }
31    sub FRAMESET_EL () { 0b10000000000 }
32    sub HEADING_EL () { 0b100000000000 }
33    sub HTML_EL () { 0b1000000000000 }
34    sub LI_EL () { 0b10000000000000 }
35    sub NOBR_EL () { 0b100000000000000 }
36    sub OPTION_EL () { 0b1000000000000000 }
37    sub OPTGROUP_EL () { 0b10000000000000000 }
38    sub P_EL () { 0b100000000000000000 }
39    sub SELECT_EL () { 0b1000000000000000000 }
40    sub TABLE_EL () { 0b10000000000000000000 }
41    sub TABLE_CELL_EL () { 0b100000000000000000000 }
42    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
43    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
44    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
45    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
46    sub FOREIGN_EL () { 0b10000000000000000000000000 }
47    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
48    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
49    
50    sub TABLE_ROWS_EL () {
51      TABLE_EL |
52      TABLE_ROW_EL |
53      TABLE_ROW_GROUP_EL
54    }
55    
56    sub END_TAG_OPTIONAL_EL () {
57      DD_EL |
58      DT_EL |
59      LI_EL |
60      P_EL
61    }
62    
63    sub ALL_END_TAG_OPTIONAL_EL () {
64      END_TAG_OPTIONAL_EL |
65      BODY_EL |
66      HTML_EL |
67      TABLE_CELL_EL |
68      TABLE_ROW_EL |
69      TABLE_ROW_GROUP_EL
70    }
71    
72    sub SCOPING_EL () {
73      BUTTON_EL |
74      CAPTION_EL |
75      HTML_EL |
76      TABLE_EL |
77      TABLE_CELL_EL |
78      MISC_SCOPING_EL
79    }
80    
81    sub TABLE_SCOPING_EL () {
82      HTML_EL |
83      TABLE_EL
84    }
85    
86    sub TABLE_ROWS_SCOPING_EL () {
87      HTML_EL |
88      TABLE_ROW_GROUP_EL
89    }
90    
91    sub TABLE_ROW_SCOPING_EL () {
92      HTML_EL |
93      TABLE_ROW_EL
94    }
95    
96    sub SPECIAL_EL () {
97      ADDRESS_EL |
98      BODY_EL |
99      DIV_EL |
100      END_TAG_OPTIONAL_EL |
101      FORM_EL |
102      FRAMESET_EL |
103      HEADING_EL |
104      OPTION_EL |
105      OPTGROUP_EL |
106      SELECT_EL |
107      TABLE_ROW_EL |
108      TABLE_ROW_GROUP_EL |
109      MISC_SPECIAL_EL
110    }
111    
112    my $el_category = {
113      a => A_EL | FORMATTING_EL,
114      address => ADDRESS_EL,
115      applet => MISC_SCOPING_EL,
116      area => MISC_SPECIAL_EL,
117      b => FORMATTING_EL,
118      base => MISC_SPECIAL_EL,
119      basefont => MISC_SPECIAL_EL,
120      bgsound => MISC_SPECIAL_EL,
121      big => FORMATTING_EL,
122      blockquote => MISC_SPECIAL_EL,
123      body => BODY_EL,
124      br => MISC_SPECIAL_EL,
125      button => BUTTON_EL,
126      caption => CAPTION_EL,
127      center => MISC_SPECIAL_EL,
128      col => MISC_SPECIAL_EL,
129      colgroup => MISC_SPECIAL_EL,
130      dd => DD_EL,
131      dir => MISC_SPECIAL_EL,
132      div => DIV_EL,
133      dl => MISC_SPECIAL_EL,
134      dt => DT_EL,
135      em => FORMATTING_EL,
136      embed => MISC_SPECIAL_EL,
137      fieldset => MISC_SPECIAL_EL,
138      font => FORMATTING_EL,
139      form => FORM_EL,
140      frame => MISC_SPECIAL_EL,
141      frameset => FRAMESET_EL,
142      h1 => HEADING_EL,
143      h2 => HEADING_EL,
144      h3 => HEADING_EL,
145      h4 => HEADING_EL,
146      h5 => HEADING_EL,
147      h6 => HEADING_EL,
148      head => MISC_SPECIAL_EL,
149      hr => MISC_SPECIAL_EL,
150      html => HTML_EL,
151      i => FORMATTING_EL,
152      iframe => MISC_SPECIAL_EL,
153      img => MISC_SPECIAL_EL,
154      input => MISC_SPECIAL_EL,
155      isindex => MISC_SPECIAL_EL,
156      li => LI_EL,
157      link => MISC_SPECIAL_EL,
158      listing => MISC_SPECIAL_EL,
159      marquee => MISC_SCOPING_EL,
160      menu => MISC_SPECIAL_EL,
161      meta => MISC_SPECIAL_EL,
162      nobr => NOBR_EL | FORMATTING_EL,
163      noembed => MISC_SPECIAL_EL,
164      noframes => MISC_SPECIAL_EL,
165      noscript => MISC_SPECIAL_EL,
166      object => MISC_SCOPING_EL,
167      ol => MISC_SPECIAL_EL,
168      optgroup => OPTGROUP_EL,
169      option => OPTION_EL,
170      p => P_EL,
171      param => MISC_SPECIAL_EL,
172      plaintext => MISC_SPECIAL_EL,
173      pre => MISC_SPECIAL_EL,
174      s => FORMATTING_EL,
175      script => MISC_SPECIAL_EL,
176      select => SELECT_EL,
177      small => FORMATTING_EL,
178      spacer => MISC_SPECIAL_EL,
179      strike => FORMATTING_EL,
180      strong => FORMATTING_EL,
181      style => MISC_SPECIAL_EL,
182      table => TABLE_EL,
183      tbody => TABLE_ROW_GROUP_EL,
184      td => TABLE_CELL_EL,
185      textarea => MISC_SPECIAL_EL,
186      tfoot => TABLE_ROW_GROUP_EL,
187      th => TABLE_CELL_EL,
188      thead => TABLE_ROW_GROUP_EL,
189      title => MISC_SPECIAL_EL,
190      tr => TABLE_ROW_EL,
191      tt => FORMATTING_EL,
192      u => FORMATTING_EL,
193      ul => MISC_SPECIAL_EL,
194      wbr => MISC_SPECIAL_EL,
195  };  };
196    
197  my $entity_char = {  my $el_category_f = {
198    AElig => "\x{00C6}",    $MML_NS => {
199    Aacute => "\x{00C1}",      'annotation-xml' => MML_AXML_EL,
200    Acirc => "\x{00C2}",      mi => FOREIGN_FLOW_CONTENT_EL,
201    Agrave => "\x{00C0}",      mo => FOREIGN_FLOW_CONTENT_EL,
202    Alpha => "\x{0391}",      mn => FOREIGN_FLOW_CONTENT_EL,
203    Aring => "\x{00C5}",      ms => FOREIGN_FLOW_CONTENT_EL,
204    Atilde => "\x{00C3}",      mtext => FOREIGN_FLOW_CONTENT_EL,
205    Auml => "\x{00C4}",    },
206    Beta => "\x{0392}",    $SVG_NS => {
207    Ccedil => "\x{00C7}",      foreignObject => FOREIGN_FLOW_CONTENT_EL,
208    Chi => "\x{03A7}",      desc => FOREIGN_FLOW_CONTENT_EL,
209    Dagger => "\x{2021}",      title => FOREIGN_FLOW_CONTENT_EL,
210    Delta => "\x{0394}",    },
211    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}",  
212  };  };
213    
214  my $special_category = {  my $svg_attr_name = {
215    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    attributetype => 'attributeType',
216    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    basefrequency => 'baseFrequency',
217    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    baseprofile => 'baseProfile',
218    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    calcmode => 'calcMode',
219    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,    clippathunits => 'clipPathUnits',
220    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,    contentscripttype => 'contentScriptType',
221    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,    contentstyletype => 'contentStyleType',
222    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    diffuseconstant => 'diffuseConstant',
223    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    edgemode => 'edgeMode',
224    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    externalresourcesrequired => 'externalResourcesRequired',
225  };    fecolormatrix => 'feColorMatrix',
226  my $scoping_category = {    fecomposite => 'feComposite',
227    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    fegaussianblur => 'feGaussianBlur',
228    table => 1, td => 1, th => 1,    femorphology => 'feMorphology',
229      fetile => 'feTile',
230      filterres => 'filterRes',
231      filterunits => 'filterUnits',
232      glyphref => 'glyphRef',
233      gradienttransform => 'gradientTransform',
234      gradientunits => 'gradientUnits',
235      kernelmatrix => 'kernelMatrix',
236      kernelunitlength => 'kernelUnitLength',
237      keypoints => 'keyPoints',
238      keysplines => 'keySplines',
239      keytimes => 'keyTimes',
240      lengthadjust => 'lengthAdjust',
241      limitingconeangle => 'limitingConeAngle',
242      markerheight => 'markerHeight',
243      markerunits => 'markerUnits',
244      markerwidth => 'markerWidth',
245      maskcontentunits => 'maskContentUnits',
246      maskunits => 'maskUnits',
247      numoctaves => 'numOctaves',
248      pathlength => 'pathLength',
249      patterncontentunits => 'patternContentUnits',
250      patterntransform => 'patternTransform',
251      patternunits => 'patternUnits',
252      pointsatx => 'pointsAtX',
253      pointsaty => 'pointsAtY',
254      pointsatz => 'pointsAtZ',
255      preservealpha => 'preserveAlpha',
256      preserveaspectratio => 'preserveAspectRatio',
257      primitiveunits => 'primitiveUnits',
258      refx => 'refX',
259      refy => 'refY',
260      repeatcount => 'repeatCount',
261      repeatdur => 'repeatDur',
262      requiredextensions => 'requiredExtensions',
263      specularconstant => 'specularConstant',
264      specularexponent => 'specularExponent',
265      spreadmethod => 'spreadMethod',
266      startoffset => 'startOffset',
267      stddeviation => 'stdDeviation',
268      stitchtiles => 'stitchTiles',
269      surfacescale => 'surfaceScale',
270      systemlanguage => 'systemLanguage',
271      tablevalues => 'tableValues',
272      targetx => 'targetX',
273      targety => 'targetY',
274      textlength => 'textLength',
275      viewbox => 'viewBox',
276      viewtarget => 'viewTarget',
277      xchannelselector => 'xChannelSelector',
278      ychannelselector => 'yChannelSelector',
279      zoomandpan => 'zoomAndPan',
280  };  };
281  my $formatting_category = {  
282    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  my $foreign_attr_xname = {
283    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,    'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
284      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
285      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
286      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
287      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
288      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
289      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
290      'xml:base' => [$XML_NS, ['xml', 'base']],
291      'xml:lang' => [$XML_NS, ['xml', 'lang']],
292      'xml:space' => [$XML_NS, ['xml', 'space']],
293      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
294      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
295  };  };
 # $phrasing_category: all other elements  
296    
297  sub parse_string ($$$;$) {  ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
298    my $self = shift->new;  
299    my $s = \$_[0];  my $c1_entity_char = {
300      0x80 => 0x20AC,
301      0x81 => 0xFFFD,
302      0x82 => 0x201A,
303      0x83 => 0x0192,
304      0x84 => 0x201E,
305      0x85 => 0x2026,
306      0x86 => 0x2020,
307      0x87 => 0x2021,
308      0x88 => 0x02C6,
309      0x89 => 0x2030,
310      0x8A => 0x0160,
311      0x8B => 0x2039,
312      0x8C => 0x0152,
313      0x8D => 0xFFFD,
314      0x8E => 0x017D,
315      0x8F => 0xFFFD,
316      0x90 => 0xFFFD,
317      0x91 => 0x2018,
318      0x92 => 0x2019,
319      0x93 => 0x201C,
320      0x94 => 0x201D,
321      0x95 => 0x2022,
322      0x96 => 0x2013,
323      0x97 => 0x2014,
324      0x98 => 0x02DC,
325      0x99 => 0x2122,
326      0x9A => 0x0161,
327      0x9B => 0x203A,
328      0x9C => 0x0153,
329      0x9D => 0xFFFD,
330      0x9E => 0x017E,
331      0x9F => 0x0178,
332    }; # $c1_entity_char
333    
334    sub parse_byte_string ($$$$;$) {
335      my $self = ref $_[0] ? shift : shift->new;
336      my $charset_name = shift;
337      open my $byte_stream, '<', ref $_[0] ? $_[0] : \($_[0]);
338    
339      my $onerror = $_[2] || sub {
340        my (%opt) = @_;
341        warn "Parse error ($opt{type})\n";
342      };
343      $self->{parse_error} = $onerror; # updated later by parse_char_string
344    
345      ## HTML5 encoding sniffing algorithm
346      require Message::Charset::Info;
347      my $charset;
348      my $buffer;
349      my ($char_stream, $e_status);
350    
351      SNIFFING: {
352    
353        ## Step 1
354        if (defined $charset_name) {
355          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
356    
357          ## ISSUE: Unsupported encoding is not ignored according to the spec.
358          ($char_stream, $e_status) = $charset->get_decode_handle
359              ($byte_stream, allow_error_reporting => 1,
360               allow_fallback => 1);
361          if ($char_stream) {
362            $self->{confident} = 1;
363            last SNIFFING;
364          } else {
365            ## TODO: unsupported error
366          }
367        }
368    
369        ## Step 2
370        my $byte_buffer = '';
371        for (1..1024) {
372          my $char = $byte_stream->getc;
373          last unless defined $char;
374          $byte_buffer .= $char;
375        } ## TODO: timeout
376    
377        ## Step 3
378        if ($byte_buffer =~ /^\xFE\xFF/) {
379          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
380          ($char_stream, $e_status) = $charset->get_decode_handle
381              ($byte_stream, allow_error_reporting => 1,
382               allow_fallback => 1, byte_buffer => \$byte_buffer);
383          $self->{confident} = 1;
384          last SNIFFING;
385        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
386          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
387          ($char_stream, $e_status) = $charset->get_decode_handle
388              ($byte_stream, allow_error_reporting => 1,
389               allow_fallback => 1, byte_buffer => \$byte_buffer);
390          $self->{confident} = 1;
391          last SNIFFING;
392        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
393          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1, byte_buffer => \$byte_buffer);
397          $self->{confident} = 1;
398          last SNIFFING;
399        }
400    
401        ## Step 4
402        ## TODO: <meta charset>
403    
404        ## Step 5
405        ## TODO: from history
406    
407        ## Step 6
408        require Whatpm::Charset::UniversalCharDet;
409        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
410            ($byte_buffer);
411        if (defined $charset_name) {
412          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
413    
414          ## ISSUE: Unsupported encoding is not ignored according to the spec.
415          require Whatpm::Charset::DecodeHandle;
416          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
417              ($byte_stream);
418          ($char_stream, $e_status) = $charset->get_decode_handle
419              ($buffer, allow_error_reporting => 1,
420               allow_fallback => 1, byte_buffer => \$byte_buffer);
421          if ($char_stream) {
422            $buffer->{buffer} = $byte_buffer;
423            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
424                            value => $charset_name,
425                            level => $self->{info_level},
426                            line => 1, column => 1);
427            $self->{confident} = 0;
428            last SNIFFING;
429          }
430        }
431    
432        ## Step 7: default
433        ## TODO: Make this configurable.
434        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
435            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
436            ## detectable in the step 6.
437        require Whatpm::Charset::DecodeHandle;
438        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
439            ($byte_stream);
440        ($char_stream, $e_status)
441            = $charset->get_decode_handle ($buffer,
442                                           allow_error_reporting => 1,
443                                           allow_fallback => 1,
444                                           byte_buffer => \$byte_buffer);
445        $buffer->{buffer} = $byte_buffer;
446        !!!parse-error (type => 'sniffing:default', ## TODO: type name
447                        value => 'windows-1252',
448                        level => $self->{info_level},
449                        line => 1, column => 1);
450        $self->{confident} = 0;
451      } # SNIFFING
452    
453      $self->{input_encoding} = $charset->get_iana_name;
454      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
455        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
456                        value => $self->{input_encoding},
457                        level => $self->{unsupported_level},
458                        line => 1, column => 1);
459      } elsif (not ($e_status &
460                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
461        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
462                        value => $self->{input_encoding},
463                        level => $self->{unsupported_level},
464                        line => 1, column => 1);
465      }
466    
467      $self->{change_encoding} = sub {
468        my $self = shift;
469        $charset_name = shift;
470        my $token = shift;
471    
472        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
473        ($char_stream, $e_status) = $charset->get_decode_handle
474            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
475             byte_buffer => \ $buffer->{buffer});
476        
477        if ($char_stream) { # if supported
478          ## "Change the encoding" algorithm:
479    
480          ## Step 1    
481          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
482            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
483            ($char_stream, $e_status) = $charset->get_decode_handle
484                ($byte_stream,
485                 byte_buffer => \ $buffer->{buffer});
486          }
487          $charset_name = $charset->get_iana_name;
488          
489          ## Step 2
490          if (defined $self->{input_encoding} and
491              $self->{input_encoding} eq $charset_name) {
492            !!!parse-error (type => 'charset label:matching', ## TODO: type
493                            value => $charset_name,
494                            level => $self->{info_level});
495            $self->{confident} = 1;
496            return;
497          }
498    
499          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
500              ':'.$charset_name, level => 'w', token => $token);
501          
502          ## Step 3
503          # if (can) {
504            ## change the encoding on the fly.
505            #$self->{confident} = 1;
506            #return;
507          # }
508          
509          ## Step 4
510          throw Whatpm::HTML::RestartParser ();
511        }
512      }; # $self->{change_encoding}
513    
514      my $char_onerror = sub {
515        my (undef, $type, %opt) = @_;
516        !!!parse-error (%opt, type => $type,
517                        line => $self->{line}, column => $self->{column} + 1);
518        if ($opt{octets}) {
519          ${$opt{octets}} = "\x{FFFD}"; # relacement character
520        }
521      };
522      $char_stream->onerror ($char_onerror);
523    
524      my @args = @_; shift @args; # $s
525      my $return;
526      try {
527        $return = $self->parse_char_stream ($char_stream, @args);  
528      } catch Whatpm::HTML::RestartParser with {
529        ## NOTE: Invoked after {change_encoding}.
530    
531        $self->{input_encoding} = $charset->get_iana_name;
532        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
533          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
534                          value => $self->{input_encoding},
535                          level => $self->{unsupported_level},
536                          line => 1, column => 1);
537        } elsif (not ($e_status &
538                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
539          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
540                          value => $self->{input_encoding},
541                          level => $self->{unsupported_level},
542                          line => 1, column => 1);
543        }
544        $self->{confident} = 1;
545        $char_stream->onerror ($char_onerror);
546        $return = $self->parse_char_stream ($char_stream, @args);
547      };
548      return $return;
549    } # parse_byte_string
550    
551    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
552    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
553    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
554    ## because the core part of our HTML parser expects a string of character,
555    ## not a string of bytes or code units or anything which might contain a BOM.
556    ## Therefore, any parser interface that accepts a string of bytes,
557    ## such as |parse_byte_string| in this module, must ensure that it does
558    ## strip the BOM and never strip any ZWNBSP.
559    
560    sub parse_char_string ($$$;$) {
561      my $self = shift;
562      open my $input, '<:utf8', ref $_[0] ? $_[0] : \($_[0]);
563      return $self->parse_char_stream ($input, @_[1..$#_]);
564    } # parse_char_string
565    *parse_string = \&parse_char_string;
566    
567    sub parse_char_stream ($$$;$) {
568      my $self = ref $_[0] ? shift : shift->new;
569      my $input = $_[0];
570    $self->{document} = $_[1];    $self->{document} = $_[1];
571      @{$self->{document}->child_nodes} = ();
572    
573    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
574    
575      $self->{confident} = 1 unless exists $self->{confident};
576      $self->{document}->input_encoding ($self->{input_encoding})
577          if defined $self->{input_encoding};
578    
579    my $i = 0;    my $i = 0;
580    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
581    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
582    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
583      my $self = shift;      my $self = shift;
584      $self->{next_input_character} = -1 and return if $i >= length $$s;  
585      $self->{next_input_character} = ord substr $$s, $i++, 1;      pop @{$self->{prev_char}};
586      $column++;      unshift @{$self->{prev_char}}, $self->{next_char};
587    
588        my $char = $input->getc;
589        $self->{next_char} = -1 and return unless defined $char;
590        $self->{next_char} = ord $char;
591    
592        ($self->{line_prev}, $self->{column_prev})
593            = ($self->{line}, $self->{column});
594        $self->{column}++;
595            
596      if ($self->{next_input_character} == 0x000D) { # CR      if ($self->{next_char} == 0x000A) { # LF
597        if ($i >= length $$s) {        !!!cp ('j1');
598          #        $self->{line}++;
599        } else {        $self->{column} = 0;
600          my $next_char = ord substr $$s, $i++, 1;      } elsif ($self->{next_char} == 0x000D) { # CR
601          if ($next_char == 0x000A) { # LF        !!!cp ('j2');
602            #        my $next = $input->getc;
603          } else {        if ($next ne "\x0A") {
604            push @{$self->{char}}, $next_char;          $input->ungetc ($next);
605          }        }
606        }        $self->{next_char} = 0x000A; # LF # MUST
607        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{line}++;
608        $line++;        $self->{column} = 0;
609        $column = -1;      } elsif ($self->{next_char} > 0x10FFFF) {
610      } elsif ($self->{next_input_character} > 0x10FFFF) {        !!!cp ('j3');
611        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
612      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
613        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        !!!cp ('j4');
614          !!!parse-error (type => 'NULL');
615          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
616        } elsif ($self->{next_char} <= 0x0008 or
617                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
618                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
619                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
620                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
621                 {
622                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
623                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
624                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
625                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
626                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
627                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
628                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
629                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
630                  0x10FFFE => 1, 0x10FFFF => 1,
631                 }->{$self->{next_char}}) {
632          !!!cp ('j5');
633          !!!parse-error (type => 'control char', level => $self->{must_level});
634    ## TODO: error type documentation
635      }      }
636    };    };
637      $self->{prev_char} = [-1, -1, -1];
638      $self->{next_char} = -1;
639    
640    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
641      my (%opt) = @_;      my (%opt) = @_;
642      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
643        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
644        warn "Parse error ($opt{type}) at line $line column $column\n";
645    };    };
646    $self->{parse_error} = sub {    $self->{parse_error} = sub {
647      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
648    };    };
649    
650    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 352  sub parse_string ($$$;$) { Line 652  sub parse_string ($$$;$) {
652    $self->_construct_tree;    $self->_construct_tree;
653    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
654    
655      delete $self->{parse_error}; # remove loop
656    
657    return $self->{document};    return $self->{document};
658  } # parse_string  } # parse_char_stream
659    
660  sub new ($) {  sub new ($) {
661    my $class = shift;    my $class = shift;
662    my $self = bless {}, $class;    my $self = bless {
663    $self->{set_next_input_character} = sub {      must_level => 'm',
664      $self->{next_input_character} = -1;      should_level => 's',
665        good_level => 'w',
666        warn_level => 'w',
667        info_level => 'i',
668        unsupported_level => 'u',
669      }, $class;
670      $self->{set_next_char} = sub {
671        $self->{next_char} = -1;
672    };    };
673    $self->{parse_error} = sub {    $self->{parse_error} = sub {
674      #      #
675    };    };
676      $self->{change_encoding} = sub {
677        # if ($_[0] is a supported encoding) {
678        #   run "change the encoding" algorithm;
679        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
680        # }
681      };
682      $self->{application_cache_selection} = sub {
683        #
684      };
685    return $self;    return $self;
686  } # new  } # new
687    
688    sub CM_ENTITY () { 0b001 } # & markup in data
689    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
690    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
691    
692    sub PLAINTEXT_CONTENT_MODEL () { 0 }
693    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
694    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
695    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
696    
697    sub DATA_STATE () { 0 }
698    sub ENTITY_DATA_STATE () { 1 }
699    sub TAG_OPEN_STATE () { 2 }
700    sub CLOSE_TAG_OPEN_STATE () { 3 }
701    sub TAG_NAME_STATE () { 4 }
702    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
703    sub ATTRIBUTE_NAME_STATE () { 6 }
704    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
705    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
706    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
707    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
708    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
709    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
710    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
711    sub COMMENT_START_STATE () { 14 }
712    sub COMMENT_START_DASH_STATE () { 15 }
713    sub COMMENT_STATE () { 16 }
714    sub COMMENT_END_STATE () { 17 }
715    sub COMMENT_END_DASH_STATE () { 18 }
716    sub BOGUS_COMMENT_STATE () { 19 }
717    sub DOCTYPE_STATE () { 20 }
718    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
719    sub DOCTYPE_NAME_STATE () { 22 }
720    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
721    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
722    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
723    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
724    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
725    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
726    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
727    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
728    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
729    sub BOGUS_DOCTYPE_STATE () { 32 }
730    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
731    sub SELF_CLOSING_START_TAG_STATE () { 34 }
732    sub CDATA_BLOCK_STATE () { 35 }
733    
734    sub DOCTYPE_TOKEN () { 1 }
735    sub COMMENT_TOKEN () { 2 }
736    sub START_TAG_TOKEN () { 3 }
737    sub END_TAG_TOKEN () { 4 }
738    sub END_OF_FILE_TOKEN () { 5 }
739    sub CHARACTER_TOKEN () { 6 }
740    
741    sub AFTER_HTML_IMS () { 0b100 }
742    sub HEAD_IMS ()       { 0b1000 }
743    sub BODY_IMS ()       { 0b10000 }
744    sub BODY_TABLE_IMS () { 0b100000 }
745    sub TABLE_IMS ()      { 0b1000000 }
746    sub ROW_IMS ()        { 0b10000000 }
747    sub BODY_AFTER_IMS () { 0b100000000 }
748    sub FRAME_IMS ()      { 0b1000000000 }
749    sub SELECT_IMS ()     { 0b10000000000 }
750    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
751        ## NOTE: "in foreign content" insertion mode is special; it is combined
752        ## with the secondary insertion mode.  In this parser, they are stored
753        ## together in the bit-or'ed form.
754    
755    ## NOTE: "initial" and "before html" insertion modes have no constants.
756    
757    ## NOTE: "after after body" insertion mode.
758    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
759    
760    ## NOTE: "after after frameset" insertion mode.
761    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
762    
763    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
764    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
765    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
766    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
767    sub IN_BODY_IM () { BODY_IMS }
768    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
769    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
770    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
771    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
772    sub IN_TABLE_IM () { TABLE_IMS }
773    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
774    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
775    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
776    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
777    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
778    sub IN_COLUMN_GROUP_IM () { 0b10 }
779    
780  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
781    
782  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
783    my $self = shift;    my $self = shift;
784    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
785    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
786    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
787    undef $self->{current_attribute};    undef $self->{current_attribute};
788    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
789    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
790      delete $self->{self_closing};
791    $self->{char} = [];    $self->{char} = [];
792    # $self->{next_input_character}    # $self->{next_char}
793    !!!next-input-character;    !!!next-input-character;
794    $self->{token} = [];    $self->{token} = [];
795      # $self->{escape}
796  } # _initialize_tokenizer  } # _initialize_tokenizer
797    
798  ## A token has:  ## A token has:
799  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
800  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
801  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
802      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
803  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
804  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
805  ##   ->{data} (comment, character)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
806    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
807  ## Macros  ##        ->{name}
808  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
809  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
810  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
811    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
812    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
813    ##     while the token is pushed back to the stack.
814    
815    ## ISSUE: "When a DOCTYPE token is created, its
816    ## <i>self-closing flag</i> must be unset (its other state is that it
817    ## be set), and its attributes list must be empty.": Wrong subject?
818    
819  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
820    
# Line 405  sub _initialize_tokenizer ($) { Line 824  sub _initialize_tokenizer ($) {
824  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
825  ## and removed from the list.  ## and removed from the list.
826    
827    ## NOTE: HTML5 "Writing HTML documents" section, applied to
828    ## documents and not to user agents and conformance checkers,
829    ## contains some requirements that are not detected by the
830    ## parsing algorithm:
831    ## - Some requirements on character encoding declarations. ## TODO
832    ## - "Elements MUST NOT contain content that their content model disallows."
833    ##   ... Some are parse error, some are not (will be reported by c.c.).
834    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
835    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
836    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
837    
838    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
839    ## be detected by the HTML5 parsing algorithm:
840    ## - Text,
841    
842  sub _get_next_token ($) {  sub _get_next_token ($) {
843    my $self = shift;    my $self = shift;
844    
845      if ($self->{self_closing}) {
846        !!!parse-error (type => 'nestc', token => $self->{current_token});
847        ## NOTE: The |self_closing| flag is only set by start tag token.
848        ## In addition, when a start tag token is emitted, it is always set to
849        ## |current_token|.
850        delete $self->{self_closing};
851      }
852    
853    if (@{$self->{token}}) {    if (@{$self->{token}}) {
854        $self->{self_closing} = $self->{token}->[0]->{self_closing};
855      return shift @{$self->{token}};      return shift @{$self->{token}};
856    }    }
857    
858    A: {    A: {
859      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
860        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
861          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
862              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
863            $self->{state} = 'entity data';            !!!cp (1);
864              $self->{state} = ENTITY_DATA_STATE;
865            !!!next-input-character;            !!!next-input-character;
866            redo A;            redo A;
867          } else {          } else {
868              !!!cp (2);
869            #            #
870          }          }
871        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x002D) { # -
872          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
873            $self->{state} = 'tag open';            unless ($self->{escape}) {
874                if ($self->{prev_char}->[0] == 0x002D and # -
875                    $self->{prev_char}->[1] == 0x0021 and # !
876                    $self->{prev_char}->[2] == 0x003C) { # <
877                  !!!cp (3);
878                  $self->{escape} = 1;
879                } else {
880                  !!!cp (4);
881                }
882              } else {
883                !!!cp (5);
884              }
885            }
886            
887            #
888          } elsif ($self->{next_char} == 0x003C) { # <
889            if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
890                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
891                 not $self->{escape})) {
892              !!!cp (6);
893              $self->{state} = TAG_OPEN_STATE;
894            !!!next-input-character;            !!!next-input-character;
895            redo A;            redo A;
896          } else {          } else {
897              !!!cp (7);
898            #            #
899          }          }
900        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
901          !!!emit ({type => 'end-of-file'});          if ($self->{escape} and
902                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
903              if ($self->{prev_char}->[0] == 0x002D and # -
904                  $self->{prev_char}->[1] == 0x002D) { # -
905                !!!cp (8);
906                delete $self->{escape};
907              } else {
908                !!!cp (9);
909              }
910            } else {
911              !!!cp (10);
912            }
913            
914            #
915          } elsif ($self->{next_char} == -1) {
916            !!!cp (11);
917            !!!emit ({type => END_OF_FILE_TOKEN,
918                      line => $self->{line}, column => $self->{column}});
919          last A; ## TODO: ok?          last A; ## TODO: ok?
920          } else {
921            !!!cp (12);
922        }        }
923        # Anything else        # Anything else
924        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
925                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
926                       line => $self->{line}, column => $self->{column},
927                      };
928        ## Stay in the data state        ## Stay in the data state
929        !!!next-input-character;        !!!next-input-character;
930    
931        !!!emit ($token);        !!!emit ($token);
932    
933        redo A;        redo A;
934      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
935        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
936    
937          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
938                
939        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
940    
941        $self->{state} = 'data';        $self->{state} = DATA_STATE;
942        # next-input-character is already done        # next-input-character is already done
943    
944        unless (defined $token) {        unless (defined $token) {
945          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
946            !!!emit ({type => CHARACTER_TOKEN, data => '&',
947                      line => $l, column => $c,
948                     });
949        } else {        } else {
950            !!!cp (14);
951          !!!emit ($token);          !!!emit ($token);
952        }        }
953    
954        redo A;        redo A;
955      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
956        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
957            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
958          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
959            !!!next-input-character;            !!!next-input-character;
960            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
961            redo A;            redo A;
962          } else {          } else {
963              !!!cp (16);
964            ## reconsume            ## reconsume
965            $self->{state} = 'data';            $self->{state} = DATA_STATE;
966    
967            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
968                        line => $self->{line_prev},
969                        column => $self->{column_prev},
970                       });
971    
972            redo A;            redo A;
973          }          }
974        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
975          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
976            $self->{state} = 'markup declaration open';            !!!cp (17);
977              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
978            !!!next-input-character;            !!!next-input-character;
979            redo A;            redo A;
980          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
981            $self->{state} = 'close tag open';            !!!cp (18);
982              $self->{state} = CLOSE_TAG_OPEN_STATE;
983            !!!next-input-character;            !!!next-input-character;
984            redo A;            redo A;
985          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
986                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
987              !!!cp (19);
988            $self->{current_token}            $self->{current_token}
989              = {type => 'start tag',              = {type => START_TAG_TOKEN,
990                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
991            $self->{state} = 'tag name';                 line => $self->{line_prev},
992                   column => $self->{column_prev}};
993              $self->{state} = TAG_NAME_STATE;
994            !!!next-input-character;            !!!next-input-character;
995            redo A;            redo A;
996          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
997                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
998            $self->{current_token} = {type => 'start tag',            !!!cp (20);
999                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1000            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1001                                        line => $self->{line_prev},
1002                                        column => $self->{column_prev}};
1003              $self->{state} = TAG_NAME_STATE;
1004            !!!next-input-character;            !!!next-input-character;
1005            redo A;            redo A;
1006          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1007            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1008            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1009                              line => $self->{line_prev},
1010                              column => $self->{column_prev});
1011              $self->{state} = DATA_STATE;
1012            !!!next-input-character;            !!!next-input-character;
1013    
1014            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1015                        line => $self->{line_prev},
1016                        column => $self->{column_prev},
1017                       });
1018    
1019            redo A;            redo A;
1020          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1021            !!!parse-error (type => 'pio');            !!!cp (22);
1022            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1023            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1024                              column => $self->{column_prev});
1025              $self->{state} = BOGUS_COMMENT_STATE;
1026              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1027                                        line => $self->{line_prev},
1028                                        column => $self->{column_prev},
1029                                       };
1030              ## $self->{next_char} is intentionally left as is
1031            redo A;            redo A;
1032          } else {          } else {
1033            !!!parse-error (type => 'bare stago');            !!!cp (23);
1034            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1035                              line => $self->{line_prev},
1036                              column => $self->{column_prev});
1037              $self->{state} = DATA_STATE;
1038            ## reconsume            ## reconsume
1039    
1040            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1041                        line => $self->{line_prev},
1042                        column => $self->{column_prev},
1043                       });
1044    
1045            redo A;            redo A;
1046          }          }
1047        } else {        } else {
1048          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1049        }        }
1050      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1051        if ($self->{content_model_flag} eq 'RCDATA' or        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1052            $self->{content_model_flag} eq 'CDATA') {        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1053          my @next_char;          if (defined $self->{last_emitted_start_tag_name}) {
1054          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
1055            push @next_char, $self->{next_input_character};            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1056            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);            my @next_char;
1057            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
1058            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              push @next_char, $self->{next_char};
1059              !!!next-input-character;              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1060              next TAGNAME;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1061            } else {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1062              !!!parse-error (type => 'unmatched end tag');                !!!cp (24);
1063              $self->{next_input_character} = shift @next_char; # reconsume                !!!next-input-character;
1064                  next TAGNAME;
1065                } else {
1066                  !!!cp (25);
1067                  $self->{next_char} = shift @next_char; # reconsume
1068                  !!!back-next-input-character (@next_char);
1069                  $self->{state} = DATA_STATE;
1070    
1071                  !!!emit ({type => CHARACTER_TOKEN, data => '</',
1072                            line => $l, column => $c,
1073                           });
1074      
1075                  redo A;
1076                }
1077              }
1078              push @next_char, $self->{next_char};
1079          
1080              unless ($self->{next_char} == 0x0009 or # HT
1081                      $self->{next_char} == 0x000A or # LF
1082                      $self->{next_char} == 0x000B or # VT
1083                      $self->{next_char} == 0x000C or # FF
1084                      $self->{next_char} == 0x0020 or # SP
1085                      $self->{next_char} == 0x003E or # >
1086                      $self->{next_char} == 0x002F or # /
1087                      $self->{next_char} == -1) {
1088                !!!cp (26);
1089                $self->{next_char} = shift @next_char; # reconsume
1090              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1091              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1092                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1093              !!!emit ({type => 'character', data => '</'});                        line => $l, column => $c,
1094                         });
1095              redo A;              redo A;
1096              } else {
1097                !!!cp (27);
1098                $self->{next_char} = shift @next_char;
1099                !!!back-next-input-character (@next_char);
1100                # and consume...
1101            }            }
         }  
         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;  
1102          } else {          } else {
1103            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
1104            !!!back-next-input-character (@next_char);            !!!cp (28);
1105            # and consume...            # next-input-character is already done
1106              $self->{state} = DATA_STATE;
1107              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1108                        line => $l, column => $c,
1109                       });
1110              redo A;
1111          }          }
1112        }        }
1113                
1114        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1115            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1116          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1117                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1118          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1119          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1120          redo A;                 line => $l, column => $c};
1121        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1122                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1123          $self->{current_token} = {type => 'end tag',          redo A;
1124                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1125          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1126          !!!next-input-character;          !!!cp (30);
1127          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1128        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1129          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1130          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1131            !!!next-input-character;
1132            redo A;
1133          } elsif ($self->{next_char} == 0x003E) { # >
1134            !!!cp (31);
1135            !!!parse-error (type => 'empty end tag',
1136                            line => $self->{line_prev}, ## "<" in "</>"
1137                            column => $self->{column_prev} - 1);
1138            $self->{state} = DATA_STATE;
1139          !!!next-input-character;          !!!next-input-character;
1140          redo A;          redo A;
1141        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1142            !!!cp (32);
1143          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1144          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1145          # reconsume          # reconsume
1146    
1147          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1148                      line => $l, column => $c,
1149                     });
1150    
1151          redo A;          redo A;
1152        } else {        } else {
1153            !!!cp (33);
1154          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1155          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1156          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1157          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1158        }                                    column => $self->{column_prev} - 1,
1159      } elsif ($self->{state} eq 'tag name') {                                   };
1160        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1161            $self->{next_input_character} == 0x000A or # LF          redo A;
1162            $self->{next_input_character} == 0x000B or # VT        }
1163            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1164            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1165          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1166          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1167          redo A;            $self->{next_char} == 0x000C or # FF
1168        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1169          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1170            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1171            !!!next-input-character;
1172            redo A;
1173          } elsif ($self->{next_char} == 0x003E) { # >
1174            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1175              !!!cp (35);
1176            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1177          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1178            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1179            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1180              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1181            }            #  !!! cp (36);
1182              #  !!! parse-error (type => 'end tag attribute');
1183              #} else {
1184                !!!cp (37);
1185              #}
1186          } else {          } else {
1187            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1188          }          }
1189          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1190          !!!next-input-character;          !!!next-input-character;
1191    
1192          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1193    
1194          redo A;          redo A;
1195        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1196                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1197          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1198            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1199            # start tag or end tag            # start tag or end tag
1200          ## Stay in this state          ## Stay in this state
1201          !!!next-input-character;          !!!next-input-character;
1202          redo A;          redo A;
1203        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1204          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1205          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1206              !!!cp (39);
1207            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1208          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1209            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1210            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1211              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1212            }            #  !!! cp (40);
1213              #  !!! parse-error (type => 'end tag attribute');
1214              #} else {
1215                !!!cp (41);
1216              #}
1217          } else {          } else {
1218            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1219          }          }
1220          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1221          # reconsume          # reconsume
1222    
1223          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1224    
1225          redo A;          redo A;
1226        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1227            !!!cp (42);
1228            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1229          !!!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  
1230          redo A;          redo A;
1231        } else {        } else {
1232          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1233            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1234            # start tag or end tag            # start tag or end tag
1235          ## Stay in the state          ## Stay in the state
1236          !!!next-input-character;          !!!next-input-character;
1237          redo A;          redo A;
1238        }        }
1239      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1240        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1241            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1242            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1243            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1244            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1245            !!!cp (45);
1246          ## Stay in the state          ## Stay in the state
1247          !!!next-input-character;          !!!next-input-character;
1248          redo A;          redo A;
1249        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1250          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1251              !!!cp (46);
1252            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1253          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1254            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1255            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1256                !!!cp (47);
1257              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1258              } else {
1259                !!!cp (48);
1260            }            }
1261          } else {          } else {
1262            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1263          }          }
1264          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1265          !!!next-input-character;          !!!next-input-character;
1266    
1267          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1268    
1269          redo A;          redo A;
1270        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1271                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1272          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1273                                value => ''};          $self->{current_attribute}
1274          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1275          !!!next-input-character;                 value => '',
1276          redo A;                 line => $self->{line}, column => $self->{column}};
1277        } elsif ($self->{next_input_character} == 0x002F) { # /          $self->{state} = ATTRIBUTE_NAME_STATE;
1278            !!!next-input-character;
1279            redo A;
1280          } elsif ($self->{next_char} == 0x002F) { # /
1281            !!!cp (50);
1282            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1283          !!!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');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
1284          redo A;          redo A;
1285        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1286          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1287          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1288              !!!cp (52);
1289            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1290          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1291            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1292            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1293                !!!cp (53);
1294              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1295              } else {
1296                !!!cp (54);
1297            }            }
1298          } else {          } else {
1299            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1300          }          }
1301          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1302          # reconsume          # reconsume
1303    
1304          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1305    
1306          redo A;          redo A;
1307        } else {        } else {
1308          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1309                                value => ''};               0x0022 => 1, # "
1310          $self->{state} = 'attribute name';               0x0027 => 1, # '
1311                 0x003D => 1, # =
1312                }->{$self->{next_char}}) {
1313              !!!cp (55);
1314              !!!parse-error (type => 'bad attribute name');
1315            } else {
1316              !!!cp (56);
1317            }
1318            $self->{current_attribute}
1319                = {name => chr ($self->{next_char}),
1320                   value => '',
1321                   line => $self->{line}, column => $self->{column}};
1322            $self->{state} = ATTRIBUTE_NAME_STATE;
1323          !!!next-input-character;          !!!next-input-character;
1324          redo A;          redo A;
1325        }        }
1326      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1327        my $before_leave = sub {        my $before_leave = sub {
1328          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1329              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1330            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1331              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1332            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1333          } else {          } else {
1334              !!!cp (58);
1335            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1336              = $self->{current_attribute};              = $self->{current_attribute};
1337          }          }
1338        }; # $before_leave        }; # $before_leave
1339    
1340        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1341            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1342            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1343            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1344            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1345            !!!cp (59);
1346          $before_leave->();          $before_leave->();
1347          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1348          !!!next-input-character;          !!!next-input-character;
1349          redo A;          redo A;
1350        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1351            !!!cp (60);
1352          $before_leave->();          $before_leave->();
1353          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1354          !!!next-input-character;          !!!next-input-character;
1355          redo A;          redo A;
1356        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1357          $before_leave->();          $before_leave->();
1358          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1359              !!!cp (61);
1360            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1361          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1362            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
1363              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1364            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1365              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1366            }            }
1367          } else {          } else {
1368            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1369          }          }
1370          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1371          !!!next-input-character;          !!!next-input-character;
1372    
1373          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1374    
1375          redo A;          redo A;
1376        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1377                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1378          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1379            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1380          ## Stay in the state          ## Stay in the state
1381          !!!next-input-character;          !!!next-input-character;
1382          redo A;          redo A;
1383        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1384            !!!cp (64);
1385          $before_leave->();          $before_leave->();
1386            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1387          !!!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  
1388          redo A;          redo A;
1389        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1390          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1391          $before_leave->();          $before_leave->();
1392          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1393              !!!cp (66);
1394            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1395          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1396            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1397            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1398                !!!cp (67);
1399              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1400              } else {
1401                ## NOTE: This state should never be reached.
1402                !!!cp (68);
1403            }            }
1404          } else {          } else {
1405            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1406          }          }
1407          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1408          # reconsume          # reconsume
1409    
1410          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1411    
1412          redo A;          redo A;
1413        } else {        } else {
1414          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1415                $self->{next_char} == 0x0027) { # '
1416              !!!cp (69);
1417              !!!parse-error (type => 'bad attribute name');
1418            } else {
1419              !!!cp (70);
1420            }
1421            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1422          ## Stay in the state          ## Stay in the state
1423          !!!next-input-character;          !!!next-input-character;
1424          redo A;          redo A;
1425        }        }
1426      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1427        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1428            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1429            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1430            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1431            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1432            !!!cp (71);
1433          ## Stay in the state          ## Stay in the state
1434          !!!next-input-character;          !!!next-input-character;
1435          redo A;          redo A;
1436        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1437          $self->{state} = 'before attribute value';          !!!cp (72);
1438            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1439          !!!next-input-character;          !!!next-input-character;
1440          redo A;          redo A;
1441        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1442          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1443              !!!cp (73);
1444            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1445          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1446            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1447            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1448                !!!cp (74);
1449              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1450              } else {
1451                ## NOTE: This state should never be reached.
1452                !!!cp (75);
1453            }            }
1454          } else {          } else {
1455            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1456          }          }
1457          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1458          !!!next-input-character;          !!!next-input-character;
1459    
1460          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1461    
1462          redo A;          redo A;
1463        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1464                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1465          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1466                                value => ''};          $self->{current_attribute}
1467          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1468                   value => '',
1469                   line => $self->{line}, column => $self->{column}};
1470            $self->{state} = ATTRIBUTE_NAME_STATE;
1471            !!!next-input-character;
1472            redo A;
1473          } elsif ($self->{next_char} == 0x002F) { # /
1474            !!!cp (77);
1475            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1476          !!!next-input-character;          !!!next-input-character;
1477          redo A;          redo A;
1478        } 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) {  
1479          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1480          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1481              !!!cp (79);
1482            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1483          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1484            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1485            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1486                !!!cp (80);
1487              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1488              } else {
1489                ## NOTE: This state should never be reached.
1490                !!!cp (81);
1491            }            }
1492          } else {          } else {
1493            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1494          }          }
1495          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1496          # reconsume          # reconsume
1497    
1498          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1499    
1500          redo A;          redo A;
1501        } else {        } else {
1502          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1503                                value => ''};          $self->{current_attribute}
1504          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char}),
1505                   value => '',
1506                   line => $self->{line}, column => $self->{column}};
1507            $self->{state} = ATTRIBUTE_NAME_STATE;
1508          !!!next-input-character;          !!!next-input-character;
1509          redo A;                  redo A;        
1510        }        }
1511      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1512        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1513            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1514            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1515            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1516            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1517            !!!cp (83);
1518          ## Stay in the state          ## Stay in the state
1519          !!!next-input-character;          !!!next-input-character;
1520          redo A;          redo A;
1521        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1522          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1523            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1524          !!!next-input-character;          !!!next-input-character;
1525          redo A;          redo A;
1526        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1527          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1528            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1529          ## reconsume          ## reconsume
1530          redo A;          redo A;
1531        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1532          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1533            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1534          !!!next-input-character;          !!!next-input-character;
1535          redo A;          redo A;
1536        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1537          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1538              !!!cp (87);
1539            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1540          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1541            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1542            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1543                !!!cp (88);
1544              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1545              } else {
1546                ## NOTE: This state should never be reached.
1547                !!!cp (89);
1548            }            }
1549          } else {          } else {
1550            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1551          }          }
1552          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1553          !!!next-input-character;          !!!next-input-character;
1554    
1555          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1556    
1557          redo A;          redo A;
1558        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1559          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1560          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1561              !!!cp (90);
1562            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1563          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1564            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1565            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1566                !!!cp (91);
1567              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1568              } else {
1569                ## NOTE: This state should never be reached.
1570                !!!cp (92);
1571            }            }
1572          } else {          } else {
1573            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1574          }          }
1575          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1576          ## reconsume          ## reconsume
1577    
1578          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1579    
1580          redo A;          redo A;
1581        } else {        } else {
1582          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1583          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1584              !!!parse-error (type => 'bad attribute value');
1585            } else {
1586              !!!cp (94);
1587            }
1588            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1589            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1590          !!!next-input-character;          !!!next-input-character;
1591          redo A;          redo A;
1592        }        }
1593      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1594        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1595          $self->{state} = 'before attribute name';          !!!cp (95);
1596            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1597          !!!next-input-character;          !!!next-input-character;
1598          redo A;          redo A;
1599        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1600          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1601          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1602            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1603          !!!next-input-character;          !!!next-input-character;
1604          redo A;          redo A;
1605        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1606          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1607          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1608              !!!cp (97);
1609            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1610          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1611            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1612            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1613                !!!cp (98);
1614              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1615              } else {
1616                ## NOTE: This state should never be reached.
1617                !!!cp (99);
1618            }            }
1619          } else {          } else {
1620            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1621          }          }
1622          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1623          ## reconsume          ## reconsume
1624    
1625          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1626    
1627          redo A;          redo A;
1628        } else {        } else {
1629          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1630            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1631          ## Stay in the state          ## Stay in the state
1632          !!!next-input-character;          !!!next-input-character;
1633          redo A;          redo A;
1634        }        }
1635      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1636        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1637          $self->{state} = 'before attribute name';          !!!cp (101);
1638            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1639          !!!next-input-character;          !!!next-input-character;
1640          redo A;          redo A;
1641        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1642          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1643          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1644            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1645          !!!next-input-character;          !!!next-input-character;
1646          redo A;          redo A;
1647        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1648          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1649          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1650              !!!cp (103);
1651            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1652          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1653            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1654            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1655                !!!cp (104);
1656              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1657              } else {
1658                ## NOTE: This state should never be reached.
1659                !!!cp (105);
1660            }            }
1661          } else {          } else {
1662            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1663          }          }
1664          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1665          ## reconsume          ## reconsume
1666    
1667          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1668    
1669          redo A;          redo A;
1670        } else {        } else {
1671          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1672            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1673          ## Stay in the state          ## Stay in the state
1674          !!!next-input-character;          !!!next-input-character;
1675          redo A;          redo A;
1676        }        }
1677      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1678        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1679            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1680            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1681            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1682            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1683          $self->{state} = 'before attribute name';          !!!cp (107);
1684          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1685          redo A;          !!!next-input-character;
1686        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1687          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1688          $self->{state} = 'entity in attribute value';          !!!cp (108);
1689          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1690          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1691        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1692          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1693          } elsif ($self->{next_char} == 0x003E) { # >
1694            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1695              !!!cp (109);
1696            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1697          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1698            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1699            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1700                !!!cp (110);
1701              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1702              } else {
1703                ## NOTE: This state should never be reached.
1704                !!!cp (111);
1705            }            }
1706          } else {          } else {
1707            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1708          }          }
1709          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1710          !!!next-input-character;          !!!next-input-character;
1711    
1712          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1713    
1714          redo A;          redo A;
1715        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1716          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1717          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1718              !!!cp (112);
1719            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1720          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1721            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1722            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1723                !!!cp (113);
1724              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1725              } else {
1726                ## NOTE: This state should never be reached.
1727                !!!cp (114);
1728            }            }
1729          } else {          } else {
1730            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1731          }          }
1732          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1733          ## reconsume          ## reconsume
1734    
1735          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1736    
1737          redo A;          redo A;
1738        } else {        } else {
1739          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1740                 0x0022 => 1, # "
1741                 0x0027 => 1, # '
1742                 0x003D => 1, # =
1743                }->{$self->{next_char}}) {
1744              !!!cp (115);
1745              !!!parse-error (type => 'bad attribute value');
1746            } else {
1747              !!!cp (116);
1748            }
1749            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1750          ## Stay in the state          ## Stay in the state
1751          !!!next-input-character;          !!!next-input-character;
1752          redo A;          redo A;
1753        }        }
1754      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1755        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity
1756              (1,
1757               $self->{last_attribute_value_state}
1758                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1759               $self->{last_attribute_value_state}
1760                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1761               -1);
1762    
1763        unless (defined $token) {        unless (defined $token) {
1764            !!!cp (117);
1765          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1766        } else {        } else {
1767            !!!cp (118);
1768          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1769            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1770          ## 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"
1771        }        }
1772    
1773        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1774        # next-input-character is already done        # next-input-character is already done
1775        redo A;        redo A;
1776      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1777          if ($self->{next_char} == 0x0009 or # HT
1778              $self->{next_char} == 0x000A or # LF
1779              $self->{next_char} == 0x000B or # VT
1780              $self->{next_char} == 0x000C or # FF
1781              $self->{next_char} == 0x0020) { # SP
1782            !!!cp (118);
1783            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1784            !!!next-input-character;
1785            redo A;
1786          } elsif ($self->{next_char} == 0x003E) { # >
1787            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1788              !!!cp (119);
1789              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1790            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1791              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1792              if ($self->{current_token}->{attributes}) {
1793                !!!cp (120);
1794                !!!parse-error (type => 'end tag attribute');
1795              } else {
1796                ## NOTE: This state should never be reached.
1797                !!!cp (121);
1798              }
1799            } else {
1800              die "$0: $self->{current_token}->{type}: Unknown token type";
1801            }
1802            $self->{state} = DATA_STATE;
1803            !!!next-input-character;
1804    
1805            !!!emit ($self->{current_token}); # start tag or end tag
1806    
1807            redo A;
1808          } elsif ($self->{next_char} == 0x002F) { # /
1809            !!!cp (122);
1810            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1811            !!!next-input-character;
1812            redo A;
1813          } else {
1814            !!!cp ('124.1');
1815            !!!parse-error (type => 'no space between attributes');
1816            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1817            ## reconsume
1818            redo A;
1819          }
1820        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1821          if ($self->{next_char} == 0x003E) { # >
1822            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1823              !!!cp ('124.2');
1824              !!!parse-error (type => 'nestc', token => $self->{current_token});
1825              ## TODO: Different type than slash in start tag
1826              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1827              if ($self->{current_token}->{attributes}) {
1828                !!!cp ('124.4');
1829                !!!parse-error (type => 'end tag attribute');
1830              } else {
1831                !!!cp ('124.5');
1832              }
1833              ## TODO: Test |<title></title/>|
1834            } else {
1835              !!!cp ('124.3');
1836              $self->{self_closing} = 1;
1837            }
1838    
1839            $self->{state} = DATA_STATE;
1840            !!!next-input-character;
1841    
1842            !!!emit ($self->{current_token}); # start tag or end tag
1843    
1844            redo A;
1845          } else {
1846            !!!cp ('124.4');
1847            !!!parse-error (type => 'nestc');
1848            ## TODO: This error type is wrong.
1849            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1850            ## Reconsume.
1851            redo A;
1852          }
1853        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1854        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1855                
1856        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1857          #my $token = {type => COMMENT_TOKEN, data => ''};
1858    
1859        BC: {        BC: {
1860          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1861            $self->{state} = 'data';            !!!cp (124);
1862              $self->{state} = DATA_STATE;
1863            !!!next-input-character;            !!!next-input-character;
1864    
1865            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1866    
1867            redo A;            redo A;
1868          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1869            $self->{state} = 'data';            !!!cp (125);
1870              $self->{state} = DATA_STATE;
1871            ## reconsume            ## reconsume
1872    
1873            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1874    
1875            redo A;            redo A;
1876          } else {          } else {
1877            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1878              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1879            !!!next-input-character;            !!!next-input-character;
1880            redo BC;            redo BC;
1881          }          }
1882        } # BC        } # BC
1883      } elsif ($self->{state} eq 'markup declaration open') {  
1884          die "$0: _get_next_token: unexpected case [BC]";
1885        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1886        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1887    
1888          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1889    
1890        my @next_char;        my @next_char;
1891        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1892                
1893        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1894          !!!next-input-character;          !!!next-input-character;
1895          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1896          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1897            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1898            $self->{state} = 'comment';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1899                                        line => $l, column => $c,
1900                                       };
1901              $self->{state} = COMMENT_START_STATE;
1902            !!!next-input-character;            !!!next-input-character;
1903            redo A;            redo A;
1904            } else {
1905              !!!cp (128);
1906            }
1907          } elsif ($self->{next_char} == 0x0044 or # D
1908                   $self->{next_char} == 0x0064) { # d
1909            !!!next-input-character;
1910            push @next_char, $self->{next_char};
1911            if ($self->{next_char} == 0x004F or # O
1912                $self->{next_char} == 0x006F) { # o
1913              !!!next-input-character;
1914              push @next_char, $self->{next_char};
1915              if ($self->{next_char} == 0x0043 or # C
1916                  $self->{next_char} == 0x0063) { # c
1917                !!!next-input-character;
1918                push @next_char, $self->{next_char};
1919                if ($self->{next_char} == 0x0054 or # T
1920                    $self->{next_char} == 0x0074) { # t
1921                  !!!next-input-character;
1922                  push @next_char, $self->{next_char};
1923                  if ($self->{next_char} == 0x0059 or # Y
1924                      $self->{next_char} == 0x0079) { # y
1925                    !!!next-input-character;
1926                    push @next_char, $self->{next_char};
1927                    if ($self->{next_char} == 0x0050 or # P
1928                        $self->{next_char} == 0x0070) { # p
1929                      !!!next-input-character;
1930                      push @next_char, $self->{next_char};
1931                      if ($self->{next_char} == 0x0045 or # E
1932                          $self->{next_char} == 0x0065) { # e
1933                        !!!cp (129);
1934                        ## TODO: What a stupid code this is!
1935                        $self->{state} = DOCTYPE_STATE;
1936                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1937                                                  quirks => 1,
1938                                                  line => $l, column => $c,
1939                                                 };
1940                        !!!next-input-character;
1941                        redo A;
1942                      } else {
1943                        !!!cp (130);
1944                      }
1945                    } else {
1946                      !!!cp (131);
1947                    }
1948                  } else {
1949                    !!!cp (132);
1950                  }
1951                } else {
1952                  !!!cp (133);
1953                }
1954              } else {
1955                !!!cp (134);
1956              }
1957            } else {
1958              !!!cp (135);
1959          }          }
1960        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1961                 $self->{next_input_character} == 0x0064) { # d                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1962                   $self->{next_char} == 0x005B) { # [
1963          !!!next-input-character;          !!!next-input-character;
1964          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1965          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x0043) { # C
             $self->{next_input_character} == 0x006F) { # o  
1966            !!!next-input-character;            !!!next-input-character;
1967            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1968            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0044) { # D
               $self->{next_input_character} == 0x0063) { # c  
1969              !!!next-input-character;              !!!next-input-character;
1970              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1971              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0041) { # A
                 $self->{next_input_character} == 0x0074) { # t  
1972                !!!next-input-character;                !!!next-input-character;
1973                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1974                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0054) { # T
                   $self->{next_input_character} == 0x0079) { # y  
1975                  !!!next-input-character;                  !!!next-input-character;
1976                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1977                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0041) { # A
                     $self->{next_input_character} == 0x0070) { # p  
1978                    !!!next-input-character;                    !!!next-input-character;
1979                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1980                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x005B) { # [
1981                        $self->{next_input_character} == 0x0065) { # e                      !!!cp (135.1);
1982                      ## ISSUE: What a stupid code this is!                      $self->{state} = CDATA_BLOCK_STATE;
                     $self->{state} = 'DOCTYPE';  
1983                      !!!next-input-character;                      !!!next-input-character;
1984                      redo A;                      redo A;
1985                      } else {
1986                        !!!cp (135.2);
1987                    }                    }
1988                    } else {
1989                      !!!cp (135.3);
1990                  }                  }
1991                  } else {
1992                    !!!cp (135.4);                
1993                }                }
1994                } else {
1995                  !!!cp (135.5);
1996              }              }
1997              } else {
1998                !!!cp (135.6);
1999            }            }
2000            } else {
2001              !!!cp (135.7);
2002          }          }
2003          } else {
2004            !!!cp (136);
2005        }        }
2006    
2007        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
2008        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2009        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2010        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
2011          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2012                                    line => $l, column => $c,
2013                                   };
2014        redo A;        redo A;
2015                
2016        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2017        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
2018      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
2019        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2020          $self->{state} = 'comment dash';          !!!cp (137);
2021            $self->{state} = COMMENT_START_DASH_STATE;
2022            !!!next-input-character;
2023            redo A;
2024          } elsif ($self->{next_char} == 0x003E) { # >
2025            !!!cp (138);
2026            !!!parse-error (type => 'bogus comment');
2027            $self->{state} = DATA_STATE;
2028            !!!next-input-character;
2029    
2030            !!!emit ($self->{current_token}); # comment
2031    
2032            redo A;
2033          } elsif ($self->{next_char} == -1) {
2034            !!!cp (139);
2035            !!!parse-error (type => 'unclosed comment');
2036            $self->{state} = DATA_STATE;
2037            ## reconsume
2038    
2039            !!!emit ($self->{current_token}); # comment
2040    
2041            redo A;
2042          } else {
2043            !!!cp (140);
2044            $self->{current_token}->{data} # comment
2045                .= chr ($self->{next_char});
2046            $self->{state} = COMMENT_STATE;
2047            !!!next-input-character;
2048            redo A;
2049          }
2050        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2051          if ($self->{next_char} == 0x002D) { # -
2052            !!!cp (141);
2053            $self->{state} = COMMENT_END_STATE;
2054            !!!next-input-character;
2055            redo A;
2056          } elsif ($self->{next_char} == 0x003E) { # >
2057            !!!cp (142);
2058            !!!parse-error (type => 'bogus comment');
2059            $self->{state} = DATA_STATE;
2060            !!!next-input-character;
2061    
2062            !!!emit ($self->{current_token}); # comment
2063    
2064            redo A;
2065          } elsif ($self->{next_char} == -1) {
2066            !!!cp (143);
2067            !!!parse-error (type => 'unclosed comment');
2068            $self->{state} = DATA_STATE;
2069            ## reconsume
2070    
2071            !!!emit ($self->{current_token}); # comment
2072    
2073            redo A;
2074          } else {
2075            !!!cp (144);
2076            $self->{current_token}->{data} # comment
2077                .= '-' . chr ($self->{next_char});
2078            $self->{state} = COMMENT_STATE;
2079            !!!next-input-character;
2080            redo A;
2081          }
2082        } elsif ($self->{state} == COMMENT_STATE) {
2083          if ($self->{next_char} == 0x002D) { # -
2084            !!!cp (145);
2085            $self->{state} = COMMENT_END_DASH_STATE;
2086          !!!next-input-character;          !!!next-input-character;
2087          redo A;          redo A;
2088        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2089            !!!cp (146);
2090          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2091          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2092          ## reconsume          ## reconsume
2093    
2094          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2095    
2096          redo A;          redo A;
2097        } else {        } else {
2098          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2099            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2100          ## Stay in the state          ## Stay in the state
2101          !!!next-input-character;          !!!next-input-character;
2102          redo A;          redo A;
2103        }        }
2104      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2105        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2106          $self->{state} = 'comment end';          !!!cp (148);
2107            $self->{state} = COMMENT_END_STATE;
2108          !!!next-input-character;          !!!next-input-character;
2109          redo A;          redo A;
2110        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2111            !!!cp (149);
2112          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2113          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2114          ## reconsume          ## reconsume
2115    
2116          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2117    
2118          redo A;          redo A;
2119        } else {        } else {
2120          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2121          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2122            $self->{state} = COMMENT_STATE;
2123          !!!next-input-character;          !!!next-input-character;
2124          redo A;          redo A;
2125        }        }
2126      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2127        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2128          $self->{state} = 'data';          !!!cp (151);
2129            $self->{state} = DATA_STATE;
2130          !!!next-input-character;          !!!next-input-character;
2131    
2132          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2133    
2134          redo A;          redo A;
2135        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2136          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2137            !!!parse-error (type => 'dash in comment',
2138                            line => $self->{line_prev},
2139                            column => $self->{column_prev});
2140          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2141          ## Stay in the state          ## Stay in the state
2142          !!!next-input-character;          !!!next-input-character;
2143          redo A;          redo A;
2144        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2145            !!!cp (153);
2146          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2147          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2148          ## reconsume          ## reconsume
2149    
2150          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2151    
2152          redo A;          redo A;
2153        } else {        } else {
2154          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2155          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2156          $self->{state} = 'comment';                          line => $self->{line_prev},
2157                            column => $self->{column_prev});
2158            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2159            $self->{state} = COMMENT_STATE;
2160          !!!next-input-character;          !!!next-input-character;
2161          redo A;          redo A;
2162        }        }
2163      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2164        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2165            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2166            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2167            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2168            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2169          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2170            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2171          !!!next-input-character;          !!!next-input-character;
2172          redo A;          redo A;
2173        } else {        } else {
2174            !!!cp (156);
2175          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2176          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2177          ## reconsume          ## reconsume
2178          redo A;          redo A;
2179        }        }
2180      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2181        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2182            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2183            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2184            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2185            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2186            !!!cp (157);
2187          ## Stay in the state          ## Stay in the state
2188          !!!next-input-character;          !!!next-input-character;
2189          redo A;          redo A;
2190        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
2191                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (158);
2192          $self->{current_token} = {type => 'DOCTYPE',          !!!parse-error (type => 'no DOCTYPE name');
2193                            name => chr ($self->{next_input_character} - 0x0020),          $self->{state} = DATA_STATE;
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
2194          !!!next-input-character;          !!!next-input-character;
2195    
2196            !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2197    
2198          redo A;          redo A;
2199        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == -1) {
2200            !!!cp (159);
2201          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2202          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2203            ## reconsume
2204    
2205            !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2206    
2207            redo A;
2208          } else {
2209            !!!cp (160);
2210            $self->{current_token}->{name} = chr $self->{next_char};
2211            delete $self->{current_token}->{quirks};
2212    ## ISSUE: "Set the token's name name to the" in the spec
2213            $self->{state} = DOCTYPE_NAME_STATE;
2214            !!!next-input-character;
2215            redo A;
2216          }
2217        } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2218    ## ISSUE: Redundant "First," in the spec.
2219          if ($self->{next_char} == 0x0009 or # HT
2220              $self->{next_char} == 0x000A or # LF
2221              $self->{next_char} == 0x000B or # VT
2222              $self->{next_char} == 0x000C or # FF
2223              $self->{next_char} == 0x0020) { # SP
2224            !!!cp (161);
2225            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2226            !!!next-input-character;
2227            redo A;
2228          } elsif ($self->{next_char} == 0x003E) { # >
2229            !!!cp (162);
2230            $self->{state} = DATA_STATE;
2231          !!!next-input-character;          !!!next-input-character;
2232    
2233          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE
2234    
2235          redo A;          redo A;
2236        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2237          !!!parse-error (type => 'no DOCTYPE name');          !!!cp (163);
2238          $self->{state} = 'data';          !!!parse-error (type => 'unclosed DOCTYPE');
2239            $self->{state} = DATA_STATE;
2240          ## reconsume          ## reconsume
2241    
2242          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          $self->{current_token}->{quirks} = 1;
2243            !!!emit ($self->{current_token}); # DOCTYPE
2244    
2245          redo A;          redo A;
2246        } else {        } else {
2247          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (164);
2248                            name => chr ($self->{next_input_character}),          $self->{current_token}->{name}
2249                            error => 1};            .= chr ($self->{next_char}); # DOCTYPE
2250          $self->{state} = 'DOCTYPE name';          ## Stay in the state
2251          !!!next-input-character;          !!!next-input-character;
2252          redo A;          redo A;
2253        }        }
2254      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2255        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2256            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2257            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2258            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2259            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2260          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (165);
2261          $self->{state} = 'after DOCTYPE name';          ## Stay in the state
2262          !!!next-input-character;          !!!next-input-character;
2263          redo A;          redo A;
2264        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2265          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (166);
2266          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2267          !!!next-input-character;          !!!next-input-character;
2268    
2269          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2270    
2271          redo A;          redo A;
2272        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
2273                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (167);
2274          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed DOCTYPE');
2275          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{state} = DATA_STATE;
2276            ## reconsume
2277    
2278            $self->{current_token}->{quirks} = 1;
2279            !!!emit ($self->{current_token}); # DOCTYPE
2280    
2281            redo A;
2282          } elsif ($self->{next_char} == 0x0050 or # P
2283                   $self->{next_char} == 0x0070) { # p
2284            !!!next-input-character;
2285            if ($self->{next_char} == 0x0055 or # U
2286                $self->{next_char} == 0x0075) { # u
2287              !!!next-input-character;
2288              if ($self->{next_char} == 0x0042 or # B
2289                  $self->{next_char} == 0x0062) { # b
2290                !!!next-input-character;
2291                if ($self->{next_char} == 0x004C or # L
2292                    $self->{next_char} == 0x006C) { # l
2293                  !!!next-input-character;
2294                  if ($self->{next_char} == 0x0049 or # I
2295                      $self->{next_char} == 0x0069) { # i
2296                    !!!next-input-character;
2297                    if ($self->{next_char} == 0x0043 or # C
2298                        $self->{next_char} == 0x0063) { # c
2299                      !!!cp (168);
2300                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2301                      !!!next-input-character;
2302                      redo A;
2303                    } else {
2304                      !!!cp (169);
2305                    }
2306                  } else {
2307                    !!!cp (170);
2308                  }
2309                } else {
2310                  !!!cp (171);
2311                }
2312              } else {
2313                !!!cp (172);
2314              }
2315            } else {
2316              !!!cp (173);
2317            }
2318    
2319            #
2320          } elsif ($self->{next_char} == 0x0053 or # S
2321                   $self->{next_char} == 0x0073) { # s
2322            !!!next-input-character;
2323            if ($self->{next_char} == 0x0059 or # Y
2324                $self->{next_char} == 0x0079) { # y
2325              !!!next-input-character;
2326              if ($self->{next_char} == 0x0053 or # S
2327                  $self->{next_char} == 0x0073) { # s
2328                !!!next-input-character;
2329                if ($self->{next_char} == 0x0054 or # T
2330                    $self->{next_char} == 0x0074) { # t
2331                  !!!next-input-character;
2332                  if ($self->{next_char} == 0x0045 or # E
2333                      $self->{next_char} == 0x0065) { # e
2334                    !!!next-input-character;
2335                    if ($self->{next_char} == 0x004D or # M
2336                        $self->{next_char} == 0x006D) { # m
2337                      !!!cp (174);
2338                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2339                      !!!next-input-character;
2340                      redo A;
2341                    } else {
2342                      !!!cp (175);
2343                    }
2344                  } else {
2345                    !!!cp (176);
2346                  }
2347                } else {
2348                  !!!cp (177);
2349                }
2350              } else {
2351                !!!cp (178);
2352              }
2353            } else {
2354              !!!cp (179);
2355            }
2356    
2357            #
2358          } else {
2359            !!!cp (180);
2360            !!!next-input-character;
2361            #
2362          }
2363    
2364          !!!parse-error (type => 'string after DOCTYPE name');
2365          $self->{current_token}->{quirks} = 1;
2366    
2367          $self->{state} = BOGUS_DOCTYPE_STATE;
2368          # next-input-character is already done
2369          redo A;
2370        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2371          if ({
2372                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2373                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2374              }->{$self->{next_char}}) {
2375            !!!cp (181);
2376          ## Stay in the state          ## Stay in the state
2377          !!!next-input-character;          !!!next-input-character;
2378          redo A;          redo A;
2379        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} eq 0x0022) { # "
2380            !!!cp (182);
2381            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2382            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2383            !!!next-input-character;
2384            redo A;
2385          } elsif ($self->{next_char} eq 0x0027) { # '
2386            !!!cp (183);
2387            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2388            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2389            !!!next-input-character;
2390            redo A;
2391          } elsif ($self->{next_char} eq 0x003E) { # >
2392            !!!cp (184);
2393            !!!parse-error (type => 'no PUBLIC literal');
2394    
2395            $self->{state} = DATA_STATE;
2396            !!!next-input-character;
2397    
2398            $self->{current_token}->{quirks} = 1;
2399            !!!emit ($self->{current_token}); # DOCTYPE
2400    
2401            redo A;
2402          } elsif ($self->{next_char} == -1) {
2403            !!!cp (185);
2404          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2405          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
2406          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2407          ## reconsume          ## reconsume
2408    
2409          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
2410          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
2411    
2412          redo A;          redo A;
2413        } else {        } else {
2414          $self->{current_token}->{name}          !!!cp (186);
2415            .= chr ($self->{next_input_character}); # DOCTYPE          !!!parse-error (type => 'string after PUBLIC');
2416          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{current_token}->{quirks} = 1;
2417    
2418            $self->{state} = BOGUS_DOCTYPE_STATE;
2419            !!!next-input-character;
2420            redo A;
2421          }
2422        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2423          if ($self->{next_char} == 0x0022) { # "
2424            !!!cp (187);
2425            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2426            !!!next-input-character;
2427            redo A;
2428          } elsif ($self->{next_char} == 0x003E) { # >
2429            !!!cp (188);
2430            !!!parse-error (type => 'unclosed PUBLIC literal');
2431    
2432            $self->{state} = DATA_STATE;
2433            !!!next-input-character;
2434    
2435            $self->{current_token}->{quirks} = 1;
2436            !!!emit ($self->{current_token}); # DOCTYPE
2437    
2438            redo A;
2439          } elsif ($self->{next_char} == -1) {
2440            !!!cp (189);
2441            !!!parse-error (type => 'unclosed PUBLIC literal');
2442    
2443            $self->{state} = DATA_STATE;
2444            ## reconsume
2445    
2446            $self->{current_token}->{quirks} = 1;
2447            !!!emit ($self->{current_token}); # DOCTYPE
2448    
2449            redo A;
2450          } else {
2451            !!!cp (190);
2452            $self->{current_token}->{public_identifier} # DOCTYPE
2453                .= chr $self->{next_char};
2454          ## Stay in the state          ## Stay in the state
2455          !!!next-input-character;          !!!next-input-character;
2456          redo A;          redo A;
2457        }        }
2458      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2459        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0027) { # '
2460            $self->{next_input_character} == 0x000A or # LF          !!!cp (191);
2461            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2462            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
2463            $self->{next_input_character} == 0x0020) { # SP          redo A;
2464          } elsif ($self->{next_char} == 0x003E) { # >
2465            !!!cp (192);
2466            !!!parse-error (type => 'unclosed PUBLIC literal');
2467    
2468            $self->{state} = DATA_STATE;
2469            !!!next-input-character;
2470    
2471            $self->{current_token}->{quirks} = 1;
2472            !!!emit ($self->{current_token}); # DOCTYPE
2473    
2474            redo A;
2475          } elsif ($self->{next_char} == -1) {
2476            !!!cp (193);
2477            !!!parse-error (type => 'unclosed PUBLIC literal');
2478    
2479            $self->{state} = DATA_STATE;
2480            ## reconsume
2481    
2482            $self->{current_token}->{quirks} = 1;
2483            !!!emit ($self->{current_token}); # DOCTYPE
2484    
2485            redo A;
2486          } else {
2487            !!!cp (194);
2488            $self->{current_token}->{public_identifier} # DOCTYPE
2489                .= chr $self->{next_char};
2490          ## Stay in the state          ## Stay in the state
2491          !!!next-input-character;          !!!next-input-character;
2492          redo A;          redo A;
2493        } elsif ($self->{next_input_character} == 0x003E) { # >        }
2494          $self->{state} = 'data';      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2495          if ({
2496                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2497                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2498              }->{$self->{next_char}}) {
2499            !!!cp (195);
2500            ## Stay in the state
2501            !!!next-input-character;
2502            redo A;
2503          } elsif ($self->{next_char} == 0x0022) { # "
2504            !!!cp (196);
2505            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2506            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2507            !!!next-input-character;
2508            redo A;
2509          } elsif ($self->{next_char} == 0x0027) { # '
2510            !!!cp (197);
2511            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2512            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2513            !!!next-input-character;
2514            redo A;
2515          } elsif ($self->{next_char} == 0x003E) { # >
2516            !!!cp (198);
2517            $self->{state} = DATA_STATE;
2518          !!!next-input-character;          !!!next-input-character;
2519    
2520          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2521    
2522          redo A;          redo A;
2523        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2524            !!!cp (199);
2525          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2526          $self->{state} = 'data';  
2527            $self->{state} = DATA_STATE;
2528          ## reconsume          ## reconsume
2529    
2530            $self->{current_token}->{quirks} = 1;
2531          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2532    
2533          redo A;          redo A;
2534        } else {        } else {
2535          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (200);
2536          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after PUBLIC literal');
2537          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2538    
2539            $self->{state} = BOGUS_DOCTYPE_STATE;
2540          !!!next-input-character;          !!!next-input-character;
2541          redo A;          redo A;
2542        }        }
2543      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2544        if ($self->{next_input_character} == 0x003E) { # >        if ({
2545          $self->{state} = 'data';              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2546                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2547              }->{$self->{next_char}}) {
2548            !!!cp (201);
2549            ## Stay in the state
2550            !!!next-input-character;
2551            redo A;
2552          } elsif ($self->{next_char} == 0x0022) { # "
2553            !!!cp (202);
2554            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2555            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2556            !!!next-input-character;
2557            redo A;
2558          } elsif ($self->{next_char} == 0x0027) { # '
2559            !!!cp (203);
2560            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2561            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2562            !!!next-input-character;
2563            redo A;
2564          } elsif ($self->{next_char} == 0x003E) { # >
2565            !!!cp (204);
2566            !!!parse-error (type => 'no SYSTEM literal');
2567            $self->{state} = DATA_STATE;
2568          !!!next-input-character;          !!!next-input-character;
2569    
2570            $self->{current_token}->{quirks} = 1;
2571          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2572    
2573          redo A;          redo A;
2574        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2575            !!!cp (205);
2576          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2577          $self->{state} = 'data';  
2578            $self->{state} = DATA_STATE;
2579            ## reconsume
2580    
2581            $self->{current_token}->{quirks} = 1;
2582            !!!emit ($self->{current_token}); # DOCTYPE
2583    
2584            redo A;
2585          } else {
2586            !!!cp (206);
2587            !!!parse-error (type => 'string after SYSTEM');
2588            $self->{current_token}->{quirks} = 1;
2589    
2590            $self->{state} = BOGUS_DOCTYPE_STATE;
2591            !!!next-input-character;
2592            redo A;
2593          }
2594        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2595          if ($self->{next_char} == 0x0022) { # "
2596            !!!cp (207);
2597            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2598            !!!next-input-character;
2599            redo A;
2600          } elsif ($self->{next_char} == 0x003E) { # >
2601            !!!cp (208);
2602            !!!parse-error (type => 'unclosed PUBLIC literal');
2603    
2604            $self->{state} = DATA_STATE;
2605            !!!next-input-character;
2606    
2607            $self->{current_token}->{quirks} = 1;
2608            !!!emit ($self->{current_token}); # DOCTYPE
2609    
2610            redo A;
2611          } elsif ($self->{next_char} == -1) {
2612            !!!cp (209);
2613            !!!parse-error (type => 'unclosed SYSTEM literal');
2614    
2615            $self->{state} = DATA_STATE;
2616          ## reconsume          ## reconsume
2617    
2618            $self->{current_token}->{quirks} = 1;
2619          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2620    
2621          redo A;          redo A;
2622        } else {        } else {
2623            !!!cp (210);
2624            $self->{current_token}->{system_identifier} # DOCTYPE
2625                .= chr $self->{next_char};
2626          ## Stay in the state          ## Stay in the state
2627          !!!next-input-character;          !!!next-input-character;
2628          redo A;          redo A;
2629        }        }
2630        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2631          if ($self->{next_char} == 0x0027) { # '
2632            !!!cp (211);
2633            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2634            !!!next-input-character;
2635            redo A;
2636          } elsif ($self->{next_char} == 0x003E) { # >
2637            !!!cp (212);
2638            !!!parse-error (type => 'unclosed PUBLIC literal');
2639    
2640            $self->{state} = DATA_STATE;
2641            !!!next-input-character;
2642    
2643            $self->{current_token}->{quirks} = 1;
2644            !!!emit ($self->{current_token}); # DOCTYPE
2645    
2646            redo A;
2647          } elsif ($self->{next_char} == -1) {
2648            !!!cp (213);
2649            !!!parse-error (type => 'unclosed SYSTEM literal');
2650    
2651            $self->{state} = DATA_STATE;
2652            ## reconsume
2653    
2654            $self->{current_token}->{quirks} = 1;
2655            !!!emit ($self->{current_token}); # DOCTYPE
2656    
2657            redo A;
2658          } else {
2659            !!!cp (214);
2660            $self->{current_token}->{system_identifier} # DOCTYPE
2661                .= chr $self->{next_char};
2662            ## Stay in the state
2663            !!!next-input-character;
2664            redo A;
2665          }
2666        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2667          if ({
2668                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2669                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2670              }->{$self->{next_char}}) {
2671            !!!cp (215);
2672            ## Stay in the state
2673            !!!next-input-character;
2674            redo A;
2675          } elsif ($self->{next_char} == 0x003E) { # >
2676            !!!cp (216);
2677            $self->{state} = DATA_STATE;
2678            !!!next-input-character;
2679    
2680            !!!emit ($self->{current_token}); # DOCTYPE
2681    
2682            redo A;
2683          } elsif ($self->{next_char} == -1) {
2684            !!!cp (217);
2685            !!!parse-error (type => 'unclosed DOCTYPE');
2686    
2687            $self->{state} = DATA_STATE;
2688            ## reconsume
2689    
2690            $self->{current_token}->{quirks} = 1;
2691            !!!emit ($self->{current_token}); # DOCTYPE
2692    
2693            redo A;
2694          } else {
2695            !!!cp (218);
2696            !!!parse-error (type => 'string after SYSTEM literal');
2697            #$self->{current_token}->{quirks} = 1;
2698    
2699            $self->{state} = BOGUS_DOCTYPE_STATE;
2700            !!!next-input-character;
2701            redo A;
2702          }
2703        } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2704          if ($self->{next_char} == 0x003E) { # >
2705            !!!cp (219);
2706            $self->{state} = DATA_STATE;
2707            !!!next-input-character;
2708    
2709            !!!emit ($self->{current_token}); # DOCTYPE
2710    
2711            redo A;
2712          } elsif ($self->{next_char} == -1) {
2713            !!!cp (220);
2714            !!!parse-error (type => 'unclosed DOCTYPE');
2715            $self->{state} = DATA_STATE;
2716            ## reconsume
2717    
2718            !!!emit ($self->{current_token}); # DOCTYPE
2719    
2720            redo A;
2721          } else {
2722            !!!cp (221);
2723            ## Stay in the state
2724            !!!next-input-character;
2725            redo A;
2726          }
2727        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2728          my $s = '';
2729          
2730          my ($l, $c) = ($self->{line}, $self->{column});
2731    
2732          CS: while ($self->{next_char} != -1) {
2733            if ($self->{next_char} == 0x005D) { # ]
2734              !!!next-input-character;
2735              if ($self->{next_char} == 0x005D) { # ]
2736                !!!next-input-character;
2737                MDC: {
2738                  if ($self->{next_char} == 0x003E) { # >
2739                    !!!cp (221.1);
2740                    !!!next-input-character;
2741                    last CS;
2742                  } elsif ($self->{next_char} == 0x005D) { # ]
2743                    !!!cp (221.2);
2744                    $s .= ']';
2745                    !!!next-input-character;
2746                    redo MDC;
2747                  } else {
2748                    !!!cp (221.3);
2749                    $s .= ']]';
2750                    #
2751                  }
2752                } # MDC
2753              } else {
2754                !!!cp (221.4);
2755                $s .= ']';
2756                #
2757              }
2758            } else {
2759              !!!cp (221.5);
2760              #
2761            }
2762            $s .= chr $self->{next_char};
2763            !!!next-input-character;
2764          } # CS
2765    
2766          $self->{state} = DATA_STATE;
2767          ## next-input-character done or EOF, which is reconsumed.
2768    
2769          if (length $s) {
2770            !!!cp (221.6);
2771            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2772                      line => $l, column => $c});
2773          } else {
2774            !!!cp (221.7);
2775          }
2776    
2777          redo A;
2778    
2779          ## ISSUE: "text tokens" in spec.
2780          ## TODO: Streaming support
2781      } else {      } else {
2782        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2783      }      }
# Line 1449  sub _get_next_token ($) { Line 2786  sub _get_next_token ($) {
2786    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2787  } # _get_next_token  } # _get_next_token
2788    
2789  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2790    my $self = shift;    my ($self, $in_attr, $additional) = @_;
2791      
2792    if ($self->{next_input_character} == 0x0023) { # #    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2793    
2794      if ({
2795           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2796           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2797           $additional => 1,
2798          }->{$self->{next_char}}) {
2799        !!!cp (1001);
2800        ## Don't consume
2801        ## No error
2802        return undef;
2803      } elsif ($self->{next_char} == 0x0023) { # #
2804      !!!next-input-character;      !!!next-input-character;
2805      my $num;      if ($self->{next_char} == 0x0078 or # x
2806      if ($self->{next_input_character} == 0x0078 or # x          $self->{next_char} == 0x0058) { # X
2807          $self->{next_input_character} == 0x0058) { # X        my $code;
2808        X: {        X: {
2809          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2810          !!!next-input-character;          !!!next-input-character;
2811          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2812              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2813            $num ||= 0;            !!!cp (1002);
2814            $num *= 0x10;            $code ||= 0;
2815            $num += $self->{next_input_character} - 0x0030;            $code *= 0x10;
2816              $code += $self->{next_char} - 0x0030;
2817            redo X;            redo X;
2818          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2819                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2820            ## ISSUE: the spec says U+0078, which is apparently incorrect            !!!cp (1003);
2821            $num ||= 0;            $code ||= 0;
2822            $num *= 0x10;            $code *= 0x10;
2823            $num += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2824            redo X;            redo X;
2825          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2826                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2827            ## ISSUE: the spec says U+0058, which is apparently incorrect            !!!cp (1004);
2828            $num ||= 0;            $code ||= 0;
2829            $num *= 0x10;            $code *= 0x10;
2830            $num += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2831            redo X;            redo X;
2832          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2833            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2834            $self->{next_input_character} = 0x0023; # #            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2835            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char, $self->{next_char});
2836              $self->{next_char} = 0x0023; # #
2837            return undef;            return undef;
2838          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2839              !!!cp (1006);
2840            !!!next-input-character;            !!!next-input-character;
2841          } else {          } else {
2842            !!!parse-error (type => 'no refc');            !!!cp (1007);
2843          }            !!!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?  
2844          }          }
2845    
2846          return {type => 'character', data => chr $num};          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2847              !!!cp (1008);
2848              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2849              $code = 0xFFFD;
2850            } elsif ($code > 0x10FFFF) {
2851              !!!cp (1009);
2852              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2853              $code = 0xFFFD;
2854            } elsif ($code == 0x000D) {
2855              !!!cp (1010);
2856              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2857              $code = 0x000A;
2858            } elsif (0x80 <= $code and $code <= 0x9F) {
2859              !!!cp (1011);
2860              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2861              $code = $c1_entity_char->{$code};
2862            }
2863    
2864            return {type => CHARACTER_TOKEN, data => chr $code,
2865                    has_reference => 1,
2866                    line => $l, column => $c,
2867                   };
2868        } # X        } # X
2869      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2870               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2871        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2872        !!!next-input-character;        !!!next-input-character;
2873                
2874        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2875                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2876            !!!cp (1012);
2877          $code *= 10;          $code *= 10;
2878          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2879                    
2880          !!!next-input-character;          !!!next-input-character;
2881        }        }
2882    
2883        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2884            !!!cp (1013);
2885          !!!next-input-character;          !!!next-input-character;
2886        } else {        } else {
2887          !!!parse-error (type => 'no refc');          !!!cp (1014);
2888            !!!parse-error (type => 'no refc', line => $l, column => $c);
2889        }        }
2890    
2891        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2892        if ($code > 1114111 or $code == 0) {          !!!cp (1015);
2893          $code = 0xFFFD; # REPLACEMENT CHARACTER          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2894          ## ISSUE: Why this is not an error?          $code = 0xFFFD;
2895          } elsif ($code > 0x10FFFF) {
2896            !!!cp (1016);
2897            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2898            $code = 0xFFFD;
2899          } elsif ($code == 0x000D) {
2900            !!!cp (1017);
2901            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2902            $code = 0x000A;
2903          } elsif (0x80 <= $code and $code <= 0x9F) {
2904            !!!cp (1018);
2905            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2906            $code = $c1_entity_char->{$code};
2907        }        }
2908                
2909        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2910                  line => $l, column => $c,
2911                 };
2912      } else {      } else {
2913        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2914        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2915        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2916          $self->{next_char} = 0x0023; # #
2917        return undef;        return undef;
2918      }      }
2919    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2920              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2921             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2922              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2923      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2924      !!!next-input-character;      !!!next-input-character;
2925    
2926      my $value = $entity_name;      my $value = $entity_name;
2927      my $match;      my $match = 0;
2928        require Whatpm::_NamedEntityList;
2929        our $EntityChar;
2930    
2931      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2932             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2933             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_char} and # a
2934               $self->{next_input_character} <= 0x005A) or               $self->{next_char} <= 0x005A) or # x
2935              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_char} and # a
2936               $self->{next_input_character} <= 0x007A) or               $self->{next_char} <= 0x007A) or # z
2937              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_char} and # 0
2938               $self->{next_input_character} <= 0x0039))) {               $self->{next_char} <= 0x0039) or # 9
2939        $entity_name .= chr $self->{next_input_character};              $self->{next_char} == 0x003B)) { # ;
2940        if (defined $entity_char->{$entity_name}) {        $entity_name .= chr $self->{next_char};
2941          $value = $entity_char->{$entity_name};        if (defined $EntityChar->{$entity_name}) {
2942          $match = 1;          if ($self->{next_char} == 0x003B) { # ;
2943              !!!cp (1020);
2944              $value = $EntityChar->{$entity_name};
2945              $match = 1;
2946              !!!next-input-character;
2947              last;
2948            } else {
2949              !!!cp (1021);
2950              $value = $EntityChar->{$entity_name};
2951              $match = -1;
2952              !!!next-input-character;
2953            }
2954        } else {        } else {
2955          $value .= chr $self->{next_input_character};          !!!cp (1022);
2956            $value .= chr $self->{next_char};
2957            $match *= 2;
2958            !!!next-input-character;
2959        }        }
       !!!next-input-character;  
2960      }      }
2961            
2962      if ($match) {      if ($match > 0) {
2963        if ($self->{next_input_character} == 0x003B) { # ;        !!!cp (1023);
2964          !!!next-input-character;        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2965                  line => $l, column => $c,
2966                 };
2967        } elsif ($match < 0) {
2968          !!!parse-error (type => 'no refc', line => $l, column => $c);
2969          if ($in_attr and $match < -1) {
2970            !!!cp (1024);
2971            return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2972                    line => $l, column => $c,
2973                   };
2974        } else {        } else {
2975          !!!parse-error (type => 'refc');          !!!cp (1025);
2976            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2977                    line => $l, column => $c,
2978                   };
2979        }        }
   
       return {type => 'character', data => $value};  
2980      } else {      } else {
2981        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2982        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2983        !!!back-token ({type => 'character', data => $value});        ## NOTE: "No characters are consumed" in the spec.
2984        return undef;        return {type => CHARACTER_TOKEN, data => '&'.$value,
2985                  line => $l, column => $c,
2986                 };
2987      }      }
2988    } else {    } else {
2989        !!!cp (1027);
2990      ## no characters are consumed      ## no characters are consumed
2991      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2992      return undef;      return undef;
2993    }    }
2994  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1586  sub _initialize_tree_constructor ($) { Line 2999  sub _initialize_tree_constructor ($) {
2999    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
3000    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3001    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3002    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
3003  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3004    
3005  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1613  sub _construct_tree ($) { Line 3026  sub _construct_tree ($) {
3026        
3027    !!!next-token;    !!!next-token;
3028    
   $self->{insertion_mode} = 'before head';  
3029    undef $self->{form_element};    undef $self->{form_element};
3030    undef $self->{head_element};    undef $self->{head_element};
3031    $self->{open_elements} = [];    $self->{open_elements} = [];
3032    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3033    
3034      ## NOTE: The "initial" insertion mode.
3035    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3036    
3037      ## NOTE: The "before html" insertion mode.
3038    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3039      $self->{insertion_mode} = BEFORE_HEAD_IM;
3040    
3041      ## NOTE: The "before head" insertion mode and so on.
3042    $self->_tree_construction_main;    $self->_tree_construction_main;
3043  } # _construct_tree  } # _construct_tree
3044    
3045  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3046    my $self = shift;    my $self = shift;
3047    B: {  
3048        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
3049          if ($token->{error}) {  
3050            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
3051            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
3052          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3053          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
3054            ($token->{name});        ## language.
3055          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
3056          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
3057          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/;
3058          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
3059          return;            defined $token->{public_identifier} or
3060        } elsif ({            defined $token->{system_identifier}) {
3061                  comment => 1,          !!!cp ('t1');
3062                  'start tag' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3063                  'end tag' => 1,        } elsif ($doctype_name ne 'HTML') {
3064                  'end-of-file' => 1,          !!!cp ('t2');
3065                 }->{$token->{type}}) {          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3066          ## ISSUE: Spec currently left this case undefined.          !!!parse-error (type => 'not HTML5', token => $token);
3067          !!!parse-error (type => 'missing DOCTYPE');        } else {
3068          #$phase = 'root element';          !!!cp ('t3');
3069          ## reprocess        }
3070          #redo B;        
3071          return;        my $doctype = $self->{document}->create_document_type_definition
3072        } elsif ($token->{type} eq 'character') {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3073          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        ## NOTE: Default value for both |public_id| and |system_id| attributes
3074            $self->{document}->manakai_append_text ($1);        ## are empty strings, so that we don't set any value in missing cases.
3075            ## ISSUE: DOM3 Core does not allow Document > Text        $doctype->public_id ($token->{public_identifier})
3076            unless (length $token->{data}) {            if defined $token->{public_identifier};
3077              ## Stay in the phase        $doctype->system_id ($token->{system_identifier})
3078              !!!next-token;            if defined $token->{system_identifier};
3079              redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
3080          ## ISSUE: internalSubset = null??
3081          $self->{document}->append_child ($doctype);
3082          
3083          if ($token->{quirks} or $doctype_name ne 'HTML') {
3084            !!!cp ('t4');
3085            $self->{document}->manakai_compat_mode ('quirks');
3086          } elsif (defined $token->{public_identifier}) {
3087            my $pubid = $token->{public_identifier};
3088            $pubid =~ tr/a-z/A-z/;
3089            if ({
3090              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
3091              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
3092              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
3093              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
3094              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
3095              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
3096              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
3097              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
3098              "-//IETF//DTD HTML 2.0//EN" => 1,
3099              "-//IETF//DTD HTML 2.1E//EN" => 1,
3100              "-//IETF//DTD HTML 3.0//EN" => 1,
3101              "-//IETF//DTD HTML 3.0//EN//" => 1,
3102              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
3103              "-//IETF//DTD HTML 3.2//EN" => 1,
3104              "-//IETF//DTD HTML 3//EN" => 1,
3105              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
3106              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
3107              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
3108              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
3109              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
3110              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
3111              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
3112              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
3113              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
3114              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
3115              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
3116              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
3117              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
3118              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
3119              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
3120              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
3121              "-//IETF//DTD HTML STRICT//EN" => 1,
3122              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
3123              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
3124              "-//IETF//DTD HTML//EN" => 1,
3125              "-//IETF//DTD HTML//EN//2.0" => 1,
3126              "-//IETF//DTD HTML//EN//3.0" => 1,
3127              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
3128              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
3129              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
3130              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
3131              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
3132              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
3133              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
3134              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
3135              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
3136              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
3137              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
3138              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
3139              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
3140              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
3141              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
3142              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
3143              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
3144              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
3145              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
3146              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
3147              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
3148              "-//W3C//DTD HTML 3.2//EN" => 1,
3149              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
3150              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
3151              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
3152              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
3153              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
3154              "-//W3C//DTD W3 HTML//EN" => 1,
3155              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
3156              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
3157              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
3158              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
3159              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
3160              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
3161              "HTML" => 1,
3162            }->{$pubid}) {
3163              !!!cp ('t5');
3164              $self->{document}->manakai_compat_mode ('quirks');
3165            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
3166                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
3167              if (defined $token->{system_identifier}) {
3168                !!!cp ('t6');
3169                $self->{document}->manakai_compat_mode ('quirks');
3170              } else {
3171                !!!cp ('t7');
3172                $self->{document}->manakai_compat_mode ('limited quirks');
3173            }            }
3174            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
3175                     $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
3176              !!!cp ('t8');
3177              $self->{document}->manakai_compat_mode ('limited quirks');
3178            } else {
3179              !!!cp ('t9');
3180            }
3181          } else {
3182            !!!cp ('t10');
3183          }
3184          if (defined $token->{system_identifier}) {
3185            my $sysid = $token->{system_identifier};
3186            $sysid =~ tr/A-Z/a-z/;
3187            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3188              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
3189              $self->{document}->manakai_compat_mode ('quirks');
3190              !!!cp ('t11');
3191            } else {
3192              !!!cp ('t12');
3193          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
3194        } else {        } else {
3195          die "$0: $token->{type}: Unknown token";          !!!cp ('t13');
3196        }        }
3197      } # B        
3198          ## Go to the "before html" insertion mode.
3199          !!!next-token;
3200          return;
3201        } elsif ({
3202                  START_TAG_TOKEN, 1,
3203                  END_TAG_TOKEN, 1,
3204                  END_OF_FILE_TOKEN, 1,
3205                 }->{$token->{type}}) {
3206          !!!cp ('t14');
3207          !!!parse-error (type => 'no DOCTYPE', token => $token);
3208          $self->{document}->manakai_compat_mode ('quirks');
3209          ## Go to the "before html" insertion mode.
3210          ## reprocess
3211          !!!ack-later;
3212          return;
3213        } elsif ($token->{type} == CHARACTER_TOKEN) {
3214          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3215            ## Ignore the token
3216    
3217            unless (length $token->{data}) {
3218              !!!cp ('t15');
3219              ## Stay in the insertion mode.
3220              !!!next-token;
3221              redo INITIAL;
3222            } else {
3223              !!!cp ('t16');
3224            }
3225          } else {
3226            !!!cp ('t17');
3227          }
3228    
3229          !!!parse-error (type => 'no DOCTYPE', token => $token);
3230          $self->{document}->manakai_compat_mode ('quirks');
3231          ## Go to the "before html" insertion mode.
3232          ## reprocess
3233          return;
3234        } elsif ($token->{type} == COMMENT_TOKEN) {
3235          !!!cp ('t18');
3236          my $comment = $self->{document}->create_comment ($token->{data});
3237          $self->{document}->append_child ($comment);
3238          
3239          ## Stay in the insertion mode.
3240          !!!next-token;
3241          redo INITIAL;
3242        } else {
3243          die "$0: $token->{type}: Unknown token type";
3244        }
3245      } # INITIAL
3246    
3247      die "$0: _tree_construction_initial: This should be never reached";
3248  } # _tree_construction_initial  } # _tree_construction_initial
3249    
3250  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3251    my $self = shift;    my $self = shift;
3252    
3253      ## NOTE: "before html" insertion mode.
3254        
3255    B: {    B: {
3256        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3257          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3258            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3259          ## Ignore the token          ## Ignore the token
3260          ## Stay in the phase          ## Stay in the insertion mode.
3261          !!!next-token;          !!!next-token;
3262          redo B;          redo B;
3263        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3264            !!!cp ('t20');
3265          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3266          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3267          ## Stay in the phase          ## Stay in the insertion mode.
3268          !!!next-token;          !!!next-token;
3269          redo B;          redo B;
3270        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3271          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3272            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
3273            ## ISSUE: DOM3 Core does not allow Document > Text  
3274            unless (length $token->{data}) {            unless (length $token->{data}) {
3275              ## Stay in the phase              !!!cp ('t21');
3276                ## Stay in the insertion mode.
3277              !!!next-token;              !!!next-token;
3278              redo B;              redo B;
3279              } else {
3280                !!!cp ('t22');
3281            }            }
3282            } else {
3283              !!!cp ('t23');
3284          }          }
3285    
3286            $self->{application_cache_selection}->(undef);
3287    
3288          #          #
3289          } elsif ($token->{type} == START_TAG_TOKEN) {
3290            if ($token->{tag_name} eq 'html') {
3291              my $root_element;
3292              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3293              $self->{document}->append_child ($root_element);
3294              push @{$self->{open_elements}},
3295                  [$root_element, $el_category->{html}];
3296    
3297              if ($token->{attributes}->{manifest}) {
3298                !!!cp ('t24');
3299                $self->{application_cache_selection}
3300                    ->($token->{attributes}->{manifest}->{value});
3301                ## ISSUE: Spec is unclear on relative references.
3302                ## According to Hixie (#whatwg 2008-03-19), it should be
3303                ## resolved against the base URI of the document in HTML
3304                ## or xml:base of the element in XHTML.
3305              } else {
3306                !!!cp ('t25');
3307                $self->{application_cache_selection}->(undef);
3308              }
3309    
3310              !!!nack ('t25c');
3311    
3312              !!!next-token;
3313              return; ## Go to the "before head" insertion mode.
3314            } else {
3315              !!!cp ('t25.1');
3316              #
3317            }
3318        } elsif ({        } elsif ({
3319                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3320                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3321                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3322          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3323          #          #
3324        } else {        } else {
3325          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3326        }        }
3327        my $root_element; !!!create-element ($root_element, 'html');  
3328        $self->{document}->append_child ($root_element);      my $root_element;
3329        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3330        #$phase = 'main';      $self->{document}->append_child ($root_element);
3331        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3332        #redo B;  
3333        return;      $self->{application_cache_selection}->(undef);
3334    
3335        ## NOTE: Reprocess the token.
3336        !!!ack-later;
3337        return; ## Go to the "before head" insertion mode.
3338    
3339        ## ISSUE: There is an issue in the spec
3340    } # B    } # B
3341    
3342      die "$0: _tree_construction_root_element: This should never be reached";
3343  } # _tree_construction_root_element  } # _tree_construction_root_element
3344    
3345  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1732  sub _reset_insertion_mode ($) { Line 3354  sub _reset_insertion_mode ($) {
3354            
3355      ## Step 3      ## Step 3
3356      S3: {      S3: {
3357        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3358        if (defined $self->{inner_html_node}) {          $last = 1;
3359          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
3360              $self->{inner_html_node}->[1] eq 'th') {            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
3361            #              !!!cp ('t27');
3362          } else {              #
3363            $node = $self->{inner_html_node};            } else {
3364                !!!cp ('t28');
3365                $node = $self->{inner_html_node};
3366              }
3367          }          }
3368        }        }
3369            
3370        ## Step 4..13      ## Step 4..14
3371        my $new_mode = {      my $new_mode;
3372                        select => 'in select',      if ($node->[1] & FOREIGN_EL) {
3373                        td => 'in cell',        ## NOTE: Strictly spaking, the line below only applies to MathML and
3374                        th => 'in cell',        ## SVG elements.  Currently the HTML syntax supports only MathML and
3375                        tr => 'in row',        ## SVG elements as foreigners.
3376                        tbody => 'in table body',        $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3377                        thead => 'in table head',        ## ISSUE: What is set as the secondary insertion mode?
3378                        tfoot => 'in table foot',      } else {
3379                        caption => 'in caption',        $new_mode = {
3380                        colgroup => 'in column group',                        select => IN_SELECT_IM,
3381                        table => 'in table',                        ## NOTE: |option| and |optgroup| do not set
3382                        head => 'in body', # not in head!                        ## insertion mode to "in select" by themselves.
3383                        body => 'in body',                        td => IN_CELL_IM,
3384                        frameset => 'in frameset',                        th => IN_CELL_IM,
3385                       }->{$node->[1]};                        tr => IN_ROW_IM,
3386        $self->{insertion_mode} = $new_mode and return if defined $new_mode;                        tbody => IN_TABLE_BODY_IM,
3387                          thead => IN_TABLE_BODY_IM,
3388                          tfoot => IN_TABLE_BODY_IM,
3389                          caption => IN_CAPTION_IM,
3390                          colgroup => IN_COLUMN_GROUP_IM,
3391                          table => IN_TABLE_IM,
3392                          head => IN_BODY_IM, # not in head!
3393                          body => IN_BODY_IM,
3394                          frameset => IN_FRAMESET_IM,
3395                         }->{$node->[0]->manakai_local_name};
3396        }
3397        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3398                
3399        ## Step 14        ## Step 15
3400        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3401          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3402            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3403              $self->{insertion_mode} = BEFORE_HEAD_IM;
3404          } else {          } else {
3405            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3406              !!!cp ('t30');
3407              $self->{insertion_mode} = AFTER_HEAD_IM;
3408          }          }
3409          return;          return;
3410          } else {
3411            !!!cp ('t31');
3412        }        }
3413                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3414        ## Step 16        ## Step 16
3415          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3416          
3417          ## Step 17
3418        $i--;        $i--;
3419        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3420                
3421        ## Step 17        ## Step 18
3422        redo S3;        redo S3;
3423      } # S3      } # S3
3424    
3425      die "$0: _reset_insertion_mode: This line should never be reached";
3426  } # _reset_insertion_mode  } # _reset_insertion_mode
3427    
3428  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3429    my $self = shift;    my $self = shift;
3430    
   my $phase = 'main';  
   
3431    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3432    
3433    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1803  sub _tree_construction_main ($) { Line 3444  sub _tree_construction_main ($) {
3444      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3445      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3446        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3447            !!!cp ('t32');
3448          return;          return;
3449        }        }
3450      }      }
# Line 1817  sub _tree_construction_main ($) { Line 3459  sub _tree_construction_main ($) {
3459    
3460        ## Step 6        ## Step 6
3461        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3462            !!!cp ('t33_1');
3463          #          #
3464        } else {        } else {
3465          my $in_open_elements;          my $in_open_elements;
3466          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3467            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3468                !!!cp ('t33');
3469              $in_open_elements = 1;              $in_open_elements = 1;
3470              last OE;              last OE;
3471            }            }
3472          }          }
3473          if ($in_open_elements) {          if ($in_open_elements) {
3474              !!!cp ('t34');
3475            #            #
3476          } else {          } else {
3477              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3478              !!!cp ('t35');
3479            redo S4;            redo S4;
3480          }          }
3481        }        }
# Line 1851  sub _tree_construction_main ($) { Line 3498  sub _tree_construction_main ($) {
3498    
3499        ## Step 11        ## Step 11
3500        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3501            !!!cp ('t36');
3502          ## Step 7'          ## Step 7'
3503          $i++;          $i++;
3504          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3505                    
3506          redo S7;          redo S7;
3507        }        }
3508    
3509          !!!cp ('t37');
3510      } # S7      } # S7
3511    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3512    
3513    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3514      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3515        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3516            !!!cp ('t38');
3517          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3518          return;          return;
3519        }        }
3520      }      }
3521    
3522        !!!cp ('t39');
3523    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3524    
3525    my $style_start_tag = sub {    my $insert;
3526      my $style_el; !!!create-element ($style_el, 'style');  
3527      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
3528      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
3529       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
3530        ->append_child ($style_el);      ## Step 1
3531      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
3532                      my $el;
3533        !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3534    
3535        ## Step 2
3536        $insert->($el);
3537    
3538        ## Step 3
3539        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3540        delete $self->{escape}; # MUST
3541    
3542        ## Step 4
3543      my $text = '';      my $text = '';
3544        !!!nack ('t40.1');
3545      !!!next-token;      !!!next-token;
3546      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3547          !!!cp ('t40');
3548        $text .= $token->{data};        $text .= $token->{data};
3549        !!!next-token;        !!!next-token;
3550      } # stop if non-character token or tokenizer stops tokenising      }
3551    
3552        ## Step 5
3553      if (length $text) {      if (length $text) {
3554        $style_el->manakai_append_text ($text);        !!!cp ('t41');
3555          my $text = $self->{document}->create_text_node ($text);
3556          $el->append_child ($text);
3557      }      }
3558        
3559      $self->{content_model_flag} = 'PCDATA';      ## Step 6
3560                      $self->{content_model} = PCDATA_CONTENT_MODEL;
3561      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
3562        ## Step 7
3563        if ($token->{type} == END_TAG_TOKEN and
3564            $token->{tag_name} eq $start_tag_name) {
3565          !!!cp ('t42');
3566        ## Ignore the token        ## Ignore the token
3567      } else {      } else {
3568        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
3569        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3570            !!!cp ('t43');
3571            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3572          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3573            !!!cp ('t44');
3574            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3575          } else {
3576            die "$0: $content_model_flag in parse_rcdata";
3577          }
3578      }      }
3579      !!!next-token;      !!!next-token;
3580    }; # $style_start_tag    }; # $parse_rcdata
3581    
3582    my $script_start_tag = sub {    my $script_start_tag = sub () {
3583      my $script_el;      my $script_el;
3584      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3585      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3586    
3587      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3588        delete $self->{escape}; # MUST
3589            
3590      my $text = '';      my $text = '';
3591        !!!nack ('t45.1');
3592      !!!next-token;      !!!next-token;
3593      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3594          !!!cp ('t45');
3595        $text .= $token->{data};        $text .= $token->{data};
3596        !!!next-token;        !!!next-token;
3597      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3598      if (length $text) {      if (length $text) {
3599          !!!cp ('t46');
3600        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3601      }      }
3602                                
3603      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3604    
3605      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3606          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3607          !!!cp ('t47');
3608        ## Ignore the token        ## Ignore the token
3609      } else {      } else {
3610        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3611          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3612        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3613        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3614      }      }
3615            
3616      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3617          !!!cp ('t49');
3618        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3619      } else {      } else {
3620          !!!cp ('t50');
3621        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3622        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3623          
3624        (($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);  
3625                
3626        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
3627                
# Line 1943  sub _tree_construction_main ($) { Line 3631  sub _tree_construction_main ($) {
3631      !!!next-token;      !!!next-token;
3632    }; # $script_start_tag    }; # $script_start_tag
3633    
3634      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3635      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3636      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3637    
3638    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3639      my $tag_name = shift;      my $end_tag_token = shift;
3640        my $tag_name = $end_tag_token->{tag_name};
3641    
3642        ## NOTE: The adoption agency algorithm (AAA).
3643    
3644      FET: {      FET: {
3645        ## Step 1        ## Step 1
3646        my $formatting_element;        my $formatting_element;
3647        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3648        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3649          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3650              !!!cp ('t52');
3651              last AFE;
3652            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3653                         eq $tag_name) {
3654              !!!cp ('t51');
3655            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3656            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3657            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3658          }          }
3659        } # AFE        } # AFE
3660        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3661          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3662            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3663          ## Ignore the token          ## Ignore the token
3664          !!!next-token;          !!!next-token;
3665          return;          return;
# Line 1972  sub _tree_construction_main ($) { Line 3671  sub _tree_construction_main ($) {
3671          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3672          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3673            if ($in_scope) {            if ($in_scope) {
3674                !!!cp ('t54');
3675              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3676              last INSCOPE;              last INSCOPE;
3677            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3678              !!!parse-error;              !!!cp ('t55');
3679                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3680                                token => $end_tag_token);
3681              ## Ignore the token              ## Ignore the token
3682              !!!next-token;              !!!next-token;
3683              return;              return;
3684            }            }
3685          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3686                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3687            $in_scope = 0;            $in_scope = 0;
3688          }          }
3689        } # INSCOPE        } # INSCOPE
3690        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3691          !!!parse-error;          !!!cp ('t57');
3692            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3693                            token => $end_tag_token);
3694          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3695          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3696          return;          return;
3697        }        }
3698        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3699          !!!parse-error;          !!!cp ('t58');
3700            !!!parse-error (type => 'not closed',
3701                            value => $self->{open_elements}->[-1]->[0]
3702                                ->manakai_local_name,
3703                            token => $end_tag_token);
3704        }        }
3705                
3706        ## Step 2        ## Step 2
# Line 2002  sub _tree_construction_main ($) { Line 3708  sub _tree_construction_main ($) {
3708        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3709        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3710          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3711          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3712              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3713              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3714               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3715              !!!cp ('t59');
3716            $furthest_block = $node;            $furthest_block = $node;
3717            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3718          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3719              !!!cp ('t60');
3720            last OE;            last OE;
3721          }          }
3722        } # OE        } # OE
3723                
3724        ## Step 3        ## Step 3
3725        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3726            !!!cp ('t61');
3727          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3728          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3729          !!!next-token;          !!!next-token;
# Line 2027  sub _tree_construction_main ($) { Line 3736  sub _tree_construction_main ($) {
3736        ## Step 5        ## Step 5
3737        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3738        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3739            !!!cp ('t62');
3740          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3741        }        }
3742                
# Line 2049  sub _tree_construction_main ($) { Line 3759  sub _tree_construction_main ($) {
3759          S7S2: {          S7S2: {
3760            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3761              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3762                  !!!cp ('t63');
3763                $node_i_in_active = $_;                $node_i_in_active = $_;
3764                last S7S2;                last S7S2;
3765              }              }
# Line 2062  sub _tree_construction_main ($) { Line 3773  sub _tree_construction_main ($) {
3773                    
3774          ## Step 4          ## Step 4
3775          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3776              !!!cp ('t64');
3777            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3778          }          }
3779                    
3780          ## Step 5          ## Step 5
3781          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3782              !!!cp ('t65');
3783            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3784            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3785            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2084  sub _tree_construction_main ($) { Line 3797  sub _tree_construction_main ($) {
3797        } # S7          } # S7  
3798                
3799        ## Step 8        ## Step 8
3800        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3801            my $foster_parent_element;
3802            my $next_sibling;
3803            OE: for (reverse 0..$#{$self->{open_elements}}) {
3804              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3805                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3806                                 if (defined $parent and $parent->node_type == 1) {
3807                                   !!!cp ('t65.1');
3808                                   $foster_parent_element = $parent;
3809                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3810                                 } else {
3811                                   !!!cp ('t65.2');
3812                                   $foster_parent_element
3813                                     = $self->{open_elements}->[$_ - 1]->[0];
3814                                 }
3815                                 last OE;
3816                               }
3817                             } # OE
3818                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3819                               unless defined $foster_parent_element;
3820            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3821            $open_tables->[-1]->[1] = 1; # tainted
3822          } else {
3823            !!!cp ('t65.3');
3824            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3825          }
3826                
3827        ## Step 9        ## Step 9
3828        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2101  sub _tree_construction_main ($) { Line 3839  sub _tree_construction_main ($) {
3839        my $i;        my $i;
3840        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3841          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3842              !!!cp ('t66');
3843            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3844            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3845          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3846              !!!cp ('t67');
3847            $i = $_;            $i = $_;
3848          }          }
3849        } # AFE        } # AFE
# Line 2113  sub _tree_construction_main ($) { Line 3853  sub _tree_construction_main ($) {
3853        undef $i;        undef $i;
3854        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3855          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3856              !!!cp ('t68');
3857            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3858            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3859          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3860              !!!cp ('t69');
3861            $i = $_;            $i = $_;
3862          }          }
3863        } # OE        } # OE
# Line 2126  sub _tree_construction_main ($) { Line 3868  sub _tree_construction_main ($) {
3868      } # FET      } # FET
3869    }; # $formatting_end_tag    }; # $formatting_end_tag
3870    
3871    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3872      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3873    }; # $insert_to_current    }; # $insert_to_current
3874    
3875    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3876                         my $child = shift;      my $child = shift;
3877                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3878                              table => 1, tbody => 1, tfoot => 1,        # MUST
3879                              thead => 1, tr => 1,        my $foster_parent_element;
3880                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3881                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3882                           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') {  
3883                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3884                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3885                                   !!!cp ('t70');
3886                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3887                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3888                               } else {                               } else {
3889                                   !!!cp ('t71');
3890                                 $foster_parent_element                                 $foster_parent_element
3891                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3892                               }                               }
# Line 2156  sub _tree_construction_main ($) { Line 3897  sub _tree_construction_main ($) {
3897                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3898                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3899                             ($child, $next_sibling);                             ($child, $next_sibling);
3900                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3901                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3902                         }        !!!cp ('t72');
3903          $self->{open_elements}->[-1]->[0]->append_child ($child);
3904        }
3905    }; # $insert_to_foster    }; # $insert_to_foster
3906    
3907    my $in_body = sub {    B: while (1) {
3908      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
3909      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
3910        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3911          $script_start_tag->();        ## Ignore the token
3912          return;        ## Stay in the phase
3913        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
3914          $style_start_tag->();        next B;
3915          return;      } elsif ($token->{type} == START_TAG_TOKEN and
3916        } elsif ({               $token->{tag_name} eq 'html') {
3917                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3918                 }->{$token->{tag_name}}) {          !!!cp ('t79');
3919          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html:html', token => $token);
3920          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
3921          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3922          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
3923          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html:html', token => $token);
3924            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3925          } else {        } else {
3926            $insert->($el);          !!!cp ('t81');
3927          }        }
3928            
3929          !!!next-token;        !!!cp ('t82');
3930          return;        !!!parse-error (type => 'not first start tag', token => $token);
3931        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
3932          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
3933          ## NOTE: There is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3934          my $title_el;            !!!cp ('t84');
3935          !!!create-element ($title_el, 'title', $token->{attributes});            $top_el->set_attribute_ns
3936          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              (undef, [undef, $attr_name],
3937            ->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;  
3938          }          }
3939                    }
3940          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!nack ('t84.1');
3941                    !!!next-token;
3942          next B;
3943        } elsif ($token->{type} == COMMENT_TOKEN) {
3944          my $comment = $self->{document}->create_comment ($token->{data});
3945          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3946            !!!cp ('t85');
3947            $self->{document}->append_child ($comment);
3948          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3949            !!!cp ('t86');
3950            $self->{open_elements}->[0]->[0]->append_child ($comment);
3951          } else {
3952            !!!cp ('t87');
3953            $self->{open_elements}->[-1]->[0]->append_child ($comment);
3954          }
3955          !!!next-token;
3956          next B;
3957        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3958          if ($token->{type} == CHARACTER_TOKEN) {
3959            !!!cp ('t87.1');
3960            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3961          !!!next-token;          !!!next-token;
3962          return;          next B;
3963        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3964          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3965            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3966            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3967              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
3968                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3969              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
3970              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
3971              $formatting_end_tag->($token->{tag_name});            #
3972                        } elsif ({
3973              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3974                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3975                  splice @$active_formatting_elements, $_, 1;                    embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3976                  last AFE2;                    h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3977                }                    li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3978              } # AFE2                    ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3979              OE: for (reverse 0..$#{$self->{open_elements}}) {                    sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3980                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    var => 1,
3981                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
3982                  last OE;            !!!cp ('t87.2');
3983                }            !!!parse-error (type => 'not closed',
3984              } # OE                            value => $self->{open_elements}->[-1]->[0]
3985              last AFE;                                ->manakai_local_name,
3986            } elsif ($node->[0] eq '#marker') {                            token => $token);
3987              last AFE;  
3988              pop @{$self->{open_elements}}
3989                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3990    
3991              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3992              ## Reprocess.
3993              next B;
3994            } else {
3995              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3996              my $tag_name = $token->{tag_name};
3997              if ($nsuri eq $SVG_NS) {
3998                $tag_name = {
3999                   altglyph => 'altGlyph',
4000                   altglyphdef => 'altGlyphDef',
4001                   altglyphitem => 'altGlyphItem',
4002                   animatecolor => 'animateColor',
4003                   animatemotion => 'animateMotion',
4004                   animatetransform => 'animateTransform',
4005                   clippath => 'clipPath',
4006                   feblend => 'feBlend',
4007                   fecolormatrix => 'feColorMatrix',
4008                   fecomponenttransfer => 'feComponentTransfer',
4009                   fecomposite => 'feComposite',
4010                   feconvolvematrix => 'feConvolveMatrix',
4011                   fediffuselighting => 'feDiffuseLighting',
4012                   fedisplacementmap => 'feDisplacementMap',
4013                   fedistantlight => 'feDistantLight',
4014                   feflood => 'feFlood',
4015                   fefunca => 'feFuncA',
4016                   fefuncb => 'feFuncB',
4017                   fefuncg => 'feFuncG',
4018                   fefuncr => 'feFuncR',
4019                   fegaussianblur => 'feGaussianBlur',
4020                   feimage => 'feImage',
4021                   femerge => 'feMerge',
4022                   femergenode => 'feMergeNode',
4023                   femorphology => 'feMorphology',
4024                   feoffset => 'feOffset',
4025                   fepointlight => 'fePointLight',
4026                   fespecularlighting => 'feSpecularLighting',
4027                   fespotlight => 'feSpotLight',
4028                   fetile => 'feTile',
4029                   feturbulence => 'feTurbulence',
4030                   foreignobject => 'foreignObject',
4031                   glyphref => 'glyphRef',
4032                   lineargradient => 'linearGradient',
4033                   radialgradient => 'radialGradient',
4034                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4035                   textpath => 'textPath',  
4036                }->{$tag_name} || $tag_name;
4037            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4038    
4039          !!!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];  
4040    
4041          !!!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', ''];  
4042    
4043          !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4044          return;  
4045        } elsif ($token->{tag_name} eq 'marquee' or            if ($self->{self_closing}) {
4046                 $token->{tag_name} eq 'object') {              pop @{$self->{open_elements}};
4047          $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});  
4048            } else {            } else {
4049              !!!parse-error (type => 'in RCDATA:#'.$token->{type});              !!!cp ('t87.4');
4050            }            }
4051            ## ISSUE: And ignore?  
4052              !!!next-token;
4053              next B;
4054          }          }
4055          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4056          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4057        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
4058          $reconstruct_active_formatting_elements->($insert_to_current);          #
4059                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4060          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4061                    !!!cp ('t87.6');
4062          $self->{insertion_mode} = 'in select';          #
4063          !!!next-token;          ## TODO: ...
         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,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
4064        } else {        } else {
4065          $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;  
4066        }        }
4067      } elsif ($token->{type} eq 'end tag') {      }
4068        if ($token->{tag_name} eq 'body') {  
4069          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      if ($self->{insertion_mode} & HEAD_IMS) {
4070            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
4071            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4072              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4073            }              !!!cp ('t88.2');
4074            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4075            !!!next-token;            } else {
4076            return;              !!!cp ('t88.1');
4077          } else {              ## Ignore the token.
4078            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
4079            ## 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;  
4080            }            }
4081          } # INSCOPE            unless (length $token->{data}) {
4082                        !!!cp ('t88');
4083          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
4084            !!!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;  
4085            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4086          }          }
           
         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];  
4087    
4088          ## Step 2          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4089          S2: {            !!!cp ('t89');
4090            if ($node->[1] eq $token->{tag_name}) {            ## As if <head>
4091              ## Step 1            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4092              ## generate implied end tags            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4093              if ({            push @{$self->{open_elements}},
4094                   dd => 1, dt => 1, li => 1, p => 1,                [$self->{head_element}, $el_category->{head}];
4095                   td => 1, th => 1, tr => 1,  
4096                  }->{$self->{open_elements}->[-1]->[1]}) {            ## Reprocess in the "in head" insertion mode...
4097                !!!back-token;            pop @{$self->{open_elements}};
4098                $token = {type => 'end tag',  
4099                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST            ## Reprocess in the "after head" insertion mode...
4100                return;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4101              }            !!!cp ('t90');
4102                      ## As if </noscript>
4103              ## Step 2            pop @{$self->{open_elements}};
4104              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {            !!!parse-error (type => 'in noscript:#character', 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;  
             }  
           }  
4105                        
4106            ## Step 4            ## Reprocess in the "in head" insertion mode...
4107            $node_i--;            ## As if </head>
4108            $node = $self->{open_elements}->[$node_i];            pop @{$self->{open_elements}};
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
4109    
4110    B: {            ## Reprocess in the "after head" insertion mode...
4111      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4112        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
4113          !!!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]);  
         }  
4114    
4115          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4116          last B;          } else {
4117              !!!cp ('t92');
4118            }
4119    
4120          ## ISSUE: There is an issue in the spec.          ## "after head" insertion mode
4121        } else {          ## As if <body>
4122          if ($self->{insertion_mode} eq 'before head') {          !!!insert-element ('body',, $token);
4123            if ($token->{type} eq 'character') {          $self->{insertion_mode} = IN_BODY_IM;
4124              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## reprocess
4125                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          next B;
4126                unless (length $token->{data}) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4127                  !!!next-token;          if ($token->{tag_name} eq 'head') {
4128                  redo B;            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4129                }              !!!cp ('t93');
4130              }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4131              ## As if <head>              $self->{open_elements}->[-1]->[0]->append_child
4132              !!!create-element ($self->{head_element}, 'head');                  ($self->{head_element});
4133              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              push @{$self->{open_elements}},
4134              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  [$self->{head_element}, $el_category->{head}];
4135              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = IN_HEAD_IM;
4136              ## 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);  
4137              !!!next-token;              !!!next-token;
4138              redo B;              next B;
4139            } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4140              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              !!!cp ('t94');
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} 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;  
             }  
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in 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;  
               }  
             }  
               
4141              #              #
4142            } elsif ($token->{type} eq 'comment') {            } else {
4143              my $comment = $self->{document}->create_comment ($token->{data});              !!!cp ('t95');
4144              $self->{open_elements}->[-1]->[0]->append_child ($comment);              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4145                ## Ignore the token
4146                !!!nack ('t95.1');
4147              !!!next-token;              !!!next-token;
4148              redo B;              next B;
4149            } elsif ($token->{type} eq 'start tag') {            }
4150              if ($token->{tag_name} eq 'title') {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4151                ## NOTE: There is an "as if in head" code clone            !!!cp ('t96');
4152                my $title_el;            ## As if <head>
4153                !!!create-element ($title_el, 'title', $token->{attributes});            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4154                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4155                  ->append_child ($title_el);            push @{$self->{open_elements}},
4156                $self->{content_model_flag} = 'RCDATA';                [$self->{head_element}, $el_category->{head}];
4157    
4158                my $text = '';            $self->{insertion_mode} = IN_HEAD_IM;
4159                !!!next-token;            ## Reprocess in the "in head" insertion mode...
4160                while ($token->{type} eq 'character') {          } else {
4161                  $text .= $token->{data};            !!!cp ('t97');
4162                  !!!next-token;          }
4163                }  
4164                if (length $text) {              if ($token->{tag_name} eq 'base') {
4165                  $title_el->manakai_append_text ($text);                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4166                }                  !!!cp ('t98');
4167                                  ## As if </noscript>
4168                $self->{content_model_flag} = 'PCDATA';                  pop @{$self->{open_elements}};
4169                    !!!parse-error (type => 'in noscript:base', token => $token);
4170                                
4171                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
4172                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
4173                } else {                } else {
4174                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
4175                }                }
               !!!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);  
4176    
4177                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
4178                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4179              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
4180                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4181                ## Ignore the token                  push @{$self->{open_elements}},
4182                !!!next-token;                      [$self->{head_element}, $el_category->{head}];
               redo B;  
             } 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}};  
4183                } else {                } else {
4184                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
4185                }                }
4186                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4187                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4188                  pop @{$self->{open_elements}} # <head>
4189                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4190                  !!!nack ('t101.1');
4191                !!!next-token;                !!!next-token;
4192                redo B;                next B;
4193              } elsif ($token->{tag_name} eq 'html') {              } elsif ($token->{tag_name} eq 'link') {
4194                #                ## NOTE: There is a "as if in head" code clone.
4195              } else {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4196                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t102');
4197                ## Ignore the token                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4198                !!!next-token;                  push @{$self->{open_elements}},
4199                redo B;                      [$self->{head_element}, $el_category->{head}];
4200              }                } else {
4201            } else {                  !!!cp ('t103');
             #  
           }  
   
           if ($self->{open_elements}->[-1]->[1] eq 'head') {  
             ## As if </head>  
             pop @{$self->{open_elements}};  
           }  
           $self->{insertion_mode} = 'after head';  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
4202                }                }
4203              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4204                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4205              #                pop @{$self->{open_elements}} # <head>
4206            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4207              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';  
4208                !!!next-token;                !!!next-token;
4209                redo B;                next B;
4210              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
4211                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
4212                        script => 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4213                       }->{$token->{tag_name}}) {                  !!!cp ('t104');
4214                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4215                $self->{insertion_mode} = 'in head';                  push @{$self->{open_elements}},
4216                ## reprocess                      [$self->{head_element}, $el_category->{head}];
4217                redo B;                } else {
4218              } else {                  !!!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;  
4219                }                }
4220              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4221                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
             !!!parse-error (type => 'in table:#character');  
4222    
4223              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
4224              ## ISSUE: Spec says that "whenever a node would be inserted                  if ($token->{attributes}->{charset}) {
4225              ## into the current node" while characters might not be                    !!!cp ('t106');
4226              ## result in a new Text node.                    ## NOTE: Whether the encoding is supported or not is handled
4227              $reconstruct_active_formatting_elements->($insert_to_foster);                    ## in the {change_encoding} callback.
4228                                  $self->{change_encoding}
4229              if ({                        ->($self, $token->{attributes}->{charset}->{value},
4230                   table => 1, tbody => 1, tfoot => 1,                           $token);
4231                   thead => 1, tr => 1,                    
4232                  }->{$self->{open_elements}->[-1]->[1]}) {                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4233                # MUST                        ->set_user_data (manakai_has_reference =>
4234                my $foster_parent_element;                                             $token->{attributes}->{charset}
4235                my $next_sibling;                                                 ->{has_reference});
4236                my $prev_sibling;                  } elsif ($token->{attributes}->{content}) {
4237                OE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($token->{attributes}->{content}->{value}
4238                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4239                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            [\x09-\x0D\x20]*=
4240                    if (defined $parent and $parent->node_type == 1) {                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4241                      $foster_parent_element = $parent;                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4242                      $next_sibling = $self->{open_elements}->[$_]->[0];                      !!!cp ('t107');
4243                      $prev_sibling = $next_sibling->previous_sibling;                      ## NOTE: Whether the encoding is supported or not is handled
4244                        ## in the {change_encoding} callback.
4245                        $self->{change_encoding}
4246                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4247                               $token);
4248                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4249                            ->set_user_data (manakai_has_reference =>
4250                                                 $token->{attributes}->{content}
4251                                                       ->{has_reference});
4252                    } else {                    } else {
4253                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
4254                    }                    }
                   last OE;  
4255                  }                  }
               } # 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});  
4256                } else {                } else {
4257                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
4258                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
4259                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4260                }                        ->set_user_data (manakai_has_reference =>
4261              } else {                                             $token->{attributes}->{charset}
4262                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
4263              }                  }
4264                                if ($token->{attributes}->{content}) {
4265              !!!next-token;                    !!!cp ('t110');
4266              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4267            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
4268              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
4269              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
4270              !!!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}};  
4271                }                }
4272    
4273                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
4274                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4275                  !!!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}};  
4276                !!!next-token;                !!!next-token;
4277                redo B;                next B;
4278              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
4279                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4280                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
4281                       }->{$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]);  
4282                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4283                    !!!parse-error (type => 'in noscript:title', token => $token);
4284                  
4285                    $self->{insertion_mode} = IN_HEAD_IM;
4286                    ## Reprocess in the "in head" insertion mode...
4287                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4288                    !!!cp ('t112');
4289                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4290                    push @{$self->{open_elements}},
4291                        [$self->{head_element}, $el_category->{head}];
4292                  } else {
4293                    !!!cp ('t113');
4294                }                }
4295    
4296                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
4297                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
4298                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
4299                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4300                redo B;                pop @{$self->{open_elements}} # <head>
4301              } elsif ($token->{tag_name} eq 'table') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4302                ## NOTE: There are code clones for this "table in table"                next B;
4303                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } elsif ($token->{tag_name} eq 'style') {
4304                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4305                ## As if </table>                ## insertion mode IN_HEAD_IM)
4306                ## have a table element in table scope                ## NOTE: There is a "as if in head" code clone.
4307                my $i;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4308                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!cp ('t114');
4309                  my $node = $self->{open_elements}->[$_];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4310                  if ($node->[1] eq 'table') {                  push @{$self->{open_elements}},
4311                    $i = $_;                      [$self->{head_element}, $el_category->{head}];
4312                    last INSCOPE;                } else {
4313                  } elsif ({                  !!!cp ('t115');
4314                            table => 1, html => 1,                }
4315                           }->{$node->[1]}) {                $parse_rcdata->(CDATA_CONTENT_MODEL);
4316                    last INSCOPE;                pop @{$self->{open_elements}} # <head>
4317                  }                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4318                } # INSCOPE                next B;
4319                unless (defined $i) {              } elsif ($token->{tag_name} eq 'noscript') {
4320                  !!!parse-error (type => 'unmatched end tag:table');                if ($self->{insertion_mode} == IN_HEAD_IM) {
4321                  ## Ignore tokens </table><table>                  !!!cp ('t116');
4322                    ## NOTE: and scripting is disalbed
4323                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4324                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4325                    !!!nack ('t116.1');
4326                  !!!next-token;                  !!!next-token;
4327                  redo B;                  next B;
4328                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4329                    !!!cp ('t117');
4330                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4331                    ## Ignore the token
4332                    !!!nack ('t117.1');
4333                    !!!next-token;
4334                    next B;
4335                  } else {
4336                    !!!cp ('t118');
4337                    #
4338                }                }
4339                } elsif ($token->{tag_name} eq 'script') {
4340                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4341                    !!!cp ('t119');
4342                    ## As if </noscript>
4343                    pop @{$self->{open_elements}};
4344                    !!!parse-error (type => 'in noscript:script', token => $token);
4345                                
4346                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
4347                if ({                  ## Reprocess in the "in head" insertion mode...
4348                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4349                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
4350                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4351                  !!!back-token; # <table>                  push @{$self->{open_elements}},
4352                  $token = {type => 'end tag', tag_name => 'table'};                      [$self->{head_element}, $el_category->{head}];
4353                  !!!back-token;                } else {
4354                  $token = {type => 'end tag',                  !!!cp ('t121');
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4355                }                }
4356    
4357                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
4358                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
4359                  pop @{$self->{open_elements}} # <head>
4360                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4361                  next B;
4362                } elsif ($token->{tag_name} eq 'body' or
4363                         $token->{tag_name} eq 'frameset') {
4364                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4365                    !!!cp ('t122');
4366                    ## As if </noscript>
4367                    pop @{$self->{open_elements}};
4368                    !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4369                    
4370                    ## Reprocess in the "in head" insertion mode...
4371                    ## As if </head>
4372                    pop @{$self->{open_elements}};
4373                    
4374                    ## Reprocess in the "after head" insertion mode...
4375                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4376                    !!!cp ('t124');
4377                    pop @{$self->{open_elements}};
4378                    
4379                    ## Reprocess in the "after head" insertion mode...
4380                  } else {
4381                    !!!cp ('t125');
4382                }                }
4383    
4384                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
4385                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4386                  if ($token->{tag_name} eq 'body') {
4387                    !!!cp ('t126');
4388                    $self->{insertion_mode} = IN_BODY_IM;
4389                  } elsif ($token->{tag_name} eq 'frameset') {
4390                    !!!cp ('t127');
4391                    $self->{insertion_mode} = IN_FRAMESET_IM;
4392                  } else {
4393                    die "$0: tag name: $self->{tag_name}";
4394                  }
4395                  !!!nack ('t127.1');
4396                  !!!next-token;
4397                  next B;
4398                } else {
4399                  !!!cp ('t128');
4400                  #
4401                }
4402    
4403                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4404                  !!!cp ('t129');
4405                  ## As if </noscript>
4406                  pop @{$self->{open_elements}};
4407                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4408                  
4409                  ## Reprocess in the "in head" insertion mode...
4410                  ## As if </head>
4411                  pop @{$self->{open_elements}};
4412    
4413                ## reprocess                ## Reprocess in the "after head" insertion mode...
4414                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4415                  !!!cp ('t130');
4416                  ## As if </head>
4417                  pop @{$self->{open_elements}};
4418    
4419                  ## Reprocess in the "after head" insertion mode...
4420              } else {              } else {
4421                #                !!!cp ('t131');
4422              }              }
4423            } elsif ($token->{type} eq 'end tag') {  
4424              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
4425                ## have a table element in table scope              ## As if <body>
4426                my $i;              !!!insert-element ('body',, $token);
4427                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
4428                  my $node = $self->{open_elements}->[$_];              ## reprocess
4429                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
4430                    $i = $_;              next B;
4431                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
4432                  } elsif ({              if ($token->{tag_name} eq 'head') {
4433                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4434                           }->{$node->[1]}) {                  !!!cp ('t132');
4435                    last INSCOPE;                  ## As if <head>
4436                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4437                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4438                unless (defined $i) {                  push @{$self->{open_elements}},
4439                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
4440    
4441                    ## Reprocess in the "in head" insertion mode...
4442                    pop @{$self->{open_elements}};
4443                    $self->{insertion_mode} = AFTER_HEAD_IM;
4444                    !!!next-token;
4445                    next B;
4446                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4447                    !!!cp ('t133');
4448                    ## As if </noscript>
4449                    pop @{$self->{open_elements}};
4450                    !!!parse-error (type => 'in noscript:/head', token => $token);
4451                    
4452                    ## Reprocess in the "in head" insertion mode...
4453                    pop @{$self->{open_elements}};
4454                    $self->{insertion_mode} = AFTER_HEAD_IM;
4455                    !!!next-token;
4456                    next B;
4457                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4458                    !!!cp ('t134');
4459                    pop @{$self->{open_elements}};
4460                    $self->{insertion_mode} = AFTER_HEAD_IM;
4461                    !!!next-token;
4462                    next B;
4463                  } else {
4464                    !!!cp ('t135');
4465                    #
4466                  }
4467                } elsif ($token->{tag_name} eq 'noscript') {
4468                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4469                    !!!cp ('t136');
4470                    pop @{$self->{open_elements}};
4471                    $self->{insertion_mode} = IN_HEAD_IM;
4472                    !!!next-token;
4473                    next B;
4474                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4475                    !!!cp ('t137');
4476                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4477                    ## Ignore the token ## ISSUE: An issue in the spec.
4478                    !!!next-token;
4479                    next B;
4480                  } else {
4481                    !!!cp ('t138');
4482                    #
4483                  }
4484                } elsif ({
4485                          body => 1, html => 1,
4486                         }->{$token->{tag_name}}) {
4487                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4488                    !!!cp ('t139');
4489                    ## As if <head>
4490                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4491                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4492                    push @{$self->{open_elements}},
4493                        [$self->{head_element}, $el_category->{head}];
4494    
4495                    $self->{insertion_mode} = IN_HEAD_IM;
4496                    ## Reprocess in the "in head" insertion mode...
4497                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4498                    !!!cp ('t140');
4499                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4500                  ## Ignore the token                  ## Ignore the token
4501                  !!!next-token;                  !!!next-token;
4502                  redo B;                  next B;
4503                  } else {
4504                    !!!cp ('t141');
4505                }                }
4506                                
4507                ## generate implied end tags                #
4508                if ({              } elsif ({
4509                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
4510                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
4511                    }->{$self->{open_elements}->[-1]->[1]}) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4512                  !!!back-token;                  !!!cp ('t142');
4513                  $token = {type => 'end tag',                  ## As if <head>
4514                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4515                  redo B;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4516                    push @{$self->{open_elements}},
4517                        [$self->{head_element}, $el_category->{head}];
4518    
4519                    $self->{insertion_mode} = IN_HEAD_IM;
4520                    ## Reprocess in the "in head" insertion mode...
4521                  } else {
4522                    !!!cp ('t143');
4523                }                }
4524    
4525                if ($self->{open_elements}->[-1]->[1] ne 'table') {                #
4526                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } else {
4527                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4528                    !!!cp ('t144');
4529                    #
4530                  } else {
4531                    !!!cp ('t145');
4532                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4533                    ## Ignore the token
4534                    !!!next-token;
4535                    next B;
4536                }                }
4537                }
4538    
4539                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4540                  !!!cp ('t146');
4541                  ## As if </noscript>
4542                  pop @{$self->{open_elements}};
4543                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4544                  
4545                  ## Reprocess in the "in head" insertion mode...
4546                  ## As if </head>
4547                  pop @{$self->{open_elements}};
4548    
4549                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
4550                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4551                  !!!cp ('t147');
4552                  ## As if </head>
4553                  pop @{$self->{open_elements}};
4554    
4555                  ## Reprocess in the "after head" insertion mode...
4556                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4557    ## ISSUE: This case cannot be reached?
4558                  !!!cp ('t148');
4559                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4560                  ## Ignore the token ## ISSUE: An issue in the spec.
4561                !!!next-token;                !!!next-token;
4562                redo B;                next B;
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
4563              } else {              } else {
4564                #                !!!cp ('t149');
4565              }              }
           } else {  
             #  
           }  
4566    
4567            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
4568            $in_body->($insert_to_foster);              ## As if <body>
4569            redo B;              !!!insert-element ('body',, $token);
4570          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
4571            if ($token->{type} eq 'character') {              ## reprocess
4572              ## NOTE: This is a code clone of "character in body".              next B;
4573          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4574            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4575              !!!cp ('t149.1');
4576    
4577              ## NOTE: As if <head>
4578              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4579              $self->{open_elements}->[-1]->[0]->append_child
4580                  ($self->{head_element});
4581              #push @{$self->{open_elements}},
4582              #    [$self->{head_element}, $el_category->{head}];
4583              #$self->{insertion_mode} = IN_HEAD_IM;
4584              ## NOTE: Reprocess.
4585    
4586              ## NOTE: As if </head>
4587              #pop @{$self->{open_elements}};
4588              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4589              ## NOTE: Reprocess.
4590              
4591              #
4592            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4593              !!!cp ('t149.2');
4594    
4595              ## NOTE: As if </head>
4596              pop @{$self->{open_elements}};
4597              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4598              ## NOTE: Reprocess.
4599    
4600              #
4601            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4602              !!!cp ('t149.3');
4603    
4604              !!!parse-error (type => 'in noscript:#eof', token => $token);
4605    
4606              ## As if </noscript>
4607              pop @{$self->{open_elements}};
4608              #$self->{insertion_mode} = IN_HEAD_IM;
4609              ## NOTE: Reprocess.
4610    
4611              ## NOTE: As if </head>
4612              pop @{$self->{open_elements}};
4613              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4614              ## NOTE: Reprocess.
4615    
4616              #
4617            } else {
4618              !!!cp ('t149.4');
4619              #
4620            }
4621    
4622            ## NOTE: As if <body>
4623            !!!insert-element ('body',, $token);
4624            $self->{insertion_mode} = IN_BODY_IM;
4625            ## NOTE: Reprocess.
4626            next B;
4627          } else {
4628            die "$0: $token->{type}: Unknown token type";
4629          }
4630    
4631              ## ISSUE: An issue in the spec.
4632        } elsif ($self->{insertion_mode} & BODY_IMS) {
4633              if ($token->{type} == CHARACTER_TOKEN) {
4634                !!!cp ('t150');
4635                ## NOTE: There is a code clone of "character in body".
4636              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4637                            
4638              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4639    
4640              !!!next-token;              !!!next-token;
4641              redo B;              next B;
4642            } 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') {  
4643              if ({              if ({
4644                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4645                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4646                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4647                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
4648                    ## have an element in table scope
4649                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
4650                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
4651                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
4652                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
4653                  my $node = $self->{open_elements}->[$_];  
4654                  if ($node->[1] eq 'caption') {                      ## Close the cell
4655                    $i = $_;                      !!!back-token; # <x>
4656                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
4657                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
4658                            table => 1, html => 1,                                line => $token->{line},
4659                           }->{$node->[1]}) {                                column => $token->{column}};
4660                    last INSCOPE;                      next B;
4661                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4662                        !!!cp ('t152');
4663                        ## ISSUE: This case can never be reached, maybe.
4664                        last;
4665                      }
4666                  }                  }
4667                } # INSCOPE  
4668                unless (defined $i) {                  !!!cp ('t153');
4669                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
4670                        value => $token->{tag_name}, token => $token);
4671                  ## Ignore the token                  ## Ignore the token
4672                    !!!nack ('t153.1');
4673                  !!!next-token;                  !!!next-token;
4674                  redo B;                  next B;
4675                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4676                                  !!!parse-error (type => 'not closed:caption', token => $token);
4677                ## generate implied end tags                  
4678                if ({                  ## NOTE: As if </caption>.
4679                     dd => 1, dt => 1, li => 1, p => 1,                  ## have a table element in table scope
4680                     td => 1, th => 1, tr => 1,                  my $i;
4681                    }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: {
4682                  !!!back-token; # <?>                    for (reverse 0..$#{$self->{open_elements}}) {
4683                  $token = {type => 'end tag', tag_name => 'caption'};                      my $node = $self->{open_elements}->[$_];
4684                  !!!back-token;                      if ($node->[1] & CAPTION_EL) {
4685                  $token = {type => 'end tag',                        !!!cp ('t155');
4686                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        $i = $_;
4687                  redo B;                        last INSCOPE;
4688                }                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4689                          !!!cp ('t156');
4690                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        last;
4691                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      }
4692                }                    }
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
4693    
4694                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
4695                      !!!parse-error (type => 'start tag not allowed',
4696                                      value => $token->{tag_name}, token => $token);
4697                      ## Ignore the token
4698                      !!!nack ('t157.1');
4699                      !!!next-token;
4700                      next B;
4701                    } # INSCOPE
4702                    
4703                    ## generate implied end tags
4704                    while ($self->{open_elements}->[-1]->[1]
4705                               & END_TAG_OPTIONAL_EL) {
4706                      !!!cp ('t158');
4707                      pop @{$self->{open_elements}};
4708                    }
4709    
4710                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4711                redo B;                    !!!cp ('t159');
4712                      !!!parse-error (type => 'not closed',
4713                                      value => $self->{open_elements}->[-1]->[0]
4714                                          ->manakai_local_name,
4715                                      token => $token);
4716                    } else {
4717                      !!!cp ('t160');
4718                    }
4719                    
4720                    splice @{$self->{open_elements}}, $i;
4721                    
4722                    $clear_up_to_marker->();
4723                    
4724                    $self->{insertion_mode} = IN_TABLE_IM;
4725                    
4726                    ## reprocess
4727                    !!!ack-later;
4728                    next B;
4729                  } else {
4730                    !!!cp ('t161');
4731                    #
4732                  }
4733              } else {              } else {
4734                  !!!cp ('t162');
4735                #                #
4736              }              }
4737            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4738              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4739                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
4740                my $i;                  ## have an element in table scope
4741                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
4742                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4743                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4744                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4745                    last INSCOPE;                      !!!cp ('t163');
4746                  } elsif ({                      $i = $_;
4747                            table => 1, html => 1,                      last INSCOPE;
4748                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4749                    last INSCOPE;                      !!!cp ('t164');
4750                        last INSCOPE;
4751                      }
4752                    } # INSCOPE
4753                      unless (defined $i) {
4754                        !!!cp ('t165');
4755                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4756                        ## Ignore the token
4757                        !!!next-token;
4758                        next B;
4759                      }
4760                    
4761                    ## generate implied end tags
4762                    while ($self->{open_elements}->[-1]->[1]
4763                               & END_TAG_OPTIONAL_EL) {
4764                      !!!cp ('t166');
4765                      pop @{$self->{open_elements}};
4766                  }                  }
4767                } # INSCOPE  
4768                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4769                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
4770                      !!!cp ('t167');
4771                      !!!parse-error (type => 'not closed',
4772                                      value => $self->{open_elements}->[-1]->[0]
4773                                          ->manakai_local_name,
4774                                      token => $token);
4775                    } else {
4776                      !!!cp ('t168');
4777                    }
4778                    
4779                    splice @{$self->{open_elements}}, $i;
4780                    
4781                    $clear_up_to_marker->();
4782                    
4783                    $self->{insertion_mode} = IN_ROW_IM;
4784                    
4785                    !!!next-token;
4786                    next B;
4787                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4788                    !!!cp ('t169');
4789                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4790                  ## Ignore the token                  ## Ignore the token
4791                  !!!next-token;                  !!!next-token;
4792                  redo B;                  next B;
4793                }                } else {
4794                                  !!!cp ('t170');
4795                ## 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;  
4796                }                }
4797                } elsif ($token->{tag_name} eq 'caption') {
4798                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
4799                    ## have a table element in table scope
4800                    my $i;
4801                    INSCOPE: {
4802                      for (reverse 0..$#{$self->{open_elements}}) {
4803                        my $node = $self->{open_elements}->[$_];
4804                        if ($node->[1] & CAPTION_EL) {
4805                          !!!cp ('t171');
4806                          $i = $_;
4807                          last INSCOPE;
4808                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
4809                          !!!cp ('t172');
4810                          last;
4811                        }
4812                      }
4813    
4814                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
4815                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
4816                                      value => $token->{tag_name}, token => $token);
4817                      ## Ignore the token
4818                      !!!next-token;
4819                      next B;
4820                    } # INSCOPE
4821                    
4822                    ## generate implied end tags
4823                    while ($self->{open_elements}->[-1]->[1]
4824                               & END_TAG_OPTIONAL_EL) {
4825                      !!!cp ('t174');
4826                      pop @{$self->{open_elements}};
4827                    }
4828                    
4829                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4830                      !!!cp ('t175');
4831                      !!!parse-error (type => 'not closed',
4832                                      value => $self->{open_elements}->[-1]->[0]
4833                                          ->manakai_local_name,
4834                                      token => $token);
4835                    } else {
4836                      !!!cp ('t176');
4837                    }
4838                    
4839                    splice @{$self->{open_elements}}, $i;
4840                    
4841                    $clear_up_to_marker->();
4842                    
4843                    $self->{insertion_mode} = IN_TABLE_IM;
4844                    
4845                    !!!next-token;
4846                    next B;
4847                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4848                    !!!cp ('t177');
4849                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4850                    ## Ignore the token
4851                    !!!next-token;
4852                    next B;
4853                  } else {
4854                    !!!cp ('t178');
4855                    #
4856                }                }
4857                } elsif ({
4858                          table => 1, tbody => 1, tfoot => 1,
4859                          thead => 1, tr => 1,
4860                         }->{$token->{tag_name}} and
4861                         $self->{insertion_mode} == IN_CELL_IM) {
4862                  ## have an element in table scope
4863                  my $i;
4864                  my $tn;
4865                  INSCOPE: {
4866                    for (reverse 0..$#{$self->{open_elements}}) {
4867                      my $node = $self->{open_elements}->[$_];
4868                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4869                        !!!cp ('t179');
4870                        $i = $_;
4871    
4872                        ## Close the cell
4873                        !!!back-token; # </x>
4874                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
4875                                  line => $token->{line},
4876                                  column => $token->{column}};
4877                        next B;
4878                      } elsif ($node->[1] & TABLE_CELL_EL) {
4879                        !!!cp ('t180');
4880                        $tn = $node->[0]->manakai_local_name;
4881                        ## NOTE: There is exactly one |td| or |th| element
4882                        ## in scope in the stack of open elements by definition.
4883                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4884                        ## ISSUE: Can this be reached?
4885                        !!!cp ('t181');
4886                        last;
4887                      }
4888                    }
4889    
4890                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
4891                    !!!parse-error (type => 'unmatched end tag',
4892                $clear_up_to_marker->();                      value => $token->{tag_name}, token => $token);
4893                    ## Ignore the token
4894                $self->{insertion_mode} = 'in table';                  !!!next-token;
4895                    next B;
4896                !!!next-token;                } # INSCOPE
4897                redo B;              } elsif ($token->{tag_name} eq 'table' and
4898              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4899                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4900    
4901                ## As if </caption>                ## As if </caption>
4902                ## have a table element in table scope                ## have a table element in table scope
4903                my $i;                my $i;
4904                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4905                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4906                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4907                      !!!cp ('t184');
4908                    $i = $_;                    $i = $_;
4909                    last INSCOPE;                    last INSCOPE;
4910                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4911                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4912                    last INSCOPE;                    last INSCOPE;
4913                  }                  }
4914                } # INSCOPE                } # INSCOPE
4915                unless (defined $i) {                unless (defined $i) {
4916                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4917                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4918                  ## Ignore the token                  ## Ignore the token
4919                  !!!next-token;                  !!!next-token;
4920                  redo B;                  next B;
4921                }                }
4922                                
4923                ## generate implied end tags                ## generate implied end tags
4924                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4925                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4926                     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;  
4927                }                }
4928    
4929                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4930                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4931                    !!!parse-error (type => 'not closed',
4932                                    value => $self->{open_elements}->[-1]->[0]
4933                                        ->manakai_local_name,
4934                                    token => $token);
4935                  } else {
4936                    !!!cp ('t189');
4937                }                }
4938    
4939                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4940    
4941                $clear_up_to_marker->();                $clear_up_to_marker->();
4942    
4943                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4944    
4945                ## reprocess                ## reprocess
4946                redo B;                next B;
4947              } elsif ({              } elsif ({
4948                        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,  
4949                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4950                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4951                ## Ignore the token                  !!!cp ('t190');
4952                redo B;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
             } else {  
               #  
             }  
           } 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');  
4953                  ## Ignore the token                  ## Ignore the token
4954                  !!!next-token;                  !!!next-token;
4955                  redo B;                  next B;
4956                } else {                } else {
4957                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
4958                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
4959                }                }
4960              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
4961                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
4962                          thead => 1, tr => 1,
4963                         }->{$token->{tag_name}} and
4964                         $self->{insertion_mode} == IN_CAPTION_IM) {
4965                  !!!cp ('t192');
4966                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4967                ## Ignore the token                ## Ignore the token
4968                !!!next-token;                !!!next-token;
4969                redo B;                next B;
4970              } else {              } else {
4971                #                !!!cp ('t193');
4972                  #
4973              }              }
4974            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4975              #          for my $entry (@{$self->{open_elements}}) {
4976              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4977                !!!cp ('t75');
4978                !!!parse-error (type => 'in body:#eof', token => $token);
4979                last;
4980            }            }
4981            }
4982    
4983            ## As if </colgroup>          ## Stop parsing.
4984            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
4985              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
4986              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
4987          }
4988    
4989          $insert = $insert_to_current;
4990          #
4991        } elsif ($self->{insertion_mode} & TABLE_IMS) {
4992          if ($token->{type} == CHARACTER_TOKEN) {
4993            if (not $open_tables->[-1]->[1] and # tainted
4994                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4995              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4996                  
4997              unless (length $token->{data}) {
4998                !!!cp ('t194');
4999              !!!next-token;              !!!next-token;
5000              redo B;              next B;
5001            } else {            } else {
5002              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5003            }            }
5004          } 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;  
               }  
             }  
5005    
5006              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5007    
5008              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5009              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5010              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5011              ## result in a new Text node.              ## result in a new Text node.
5012              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5013                
5014              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]}) {  
5015                # MUST                # MUST
5016                my $foster_parent_element;                my $foster_parent_element;
5017                my $next_sibling;                my $next_sibling;
5018                my $prev_sibling;                my $prev_sibling;
5019                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5020                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5021                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5022                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5023                        !!!cp ('t196');
5024                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5025                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5026                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5027                    } else {                    } else {
5028                        !!!cp ('t197');
5029                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5030                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5031                    }                    }
# Line 3674  sub _tree_construction_main ($) { Line 5037  sub _tree_construction_main ($) {
5037                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5038                if (defined $prev_sibling and                if (defined $prev_sibling and
5039                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5040                    !!!cp ('t198');
5041                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5042                } else {                } else {
5043                    !!!cp ('t199');
5044                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5045                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5046                     $next_sibling);                     $next_sibling);
5047                }                }
5048              } else {            $open_tables->[-1]->[1] = 1; # tainted
5049                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5050              }            !!!cp ('t200');
5051              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5052            }
5053                            
5054              !!!next-token;          !!!next-token;
5055              redo B;          next B;
5056            } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == START_TAG_TOKEN) {
             ## 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') {  
5057              if ({              if ({
5058                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
5059                   th => 1, td => 1,                   th => 1, td => 1,
5060                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5061                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5062                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
5063                    while (not ($self->{open_elements}->[-1]->[1]
5064                                    & TABLE_SCOPING_EL)) {
5065                      !!!cp ('t201');
5066                      pop @{$self->{open_elements}};
5067                    }
5068                    
5069                    !!!insert-element ('tbody',, $token);
5070                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5071                    ## reprocess in the "in table body" insertion mode...
5072                }                }
5073    
5074                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5075                while (not {                  unless ($token->{tag_name} eq 'tr') {
5076                  tbody => 1, tfoot => 1, thead => 1, html => 1,                    !!!cp ('t202');
5077                }->{$self->{open_elements}->[-1]->[1]}) {                    !!!parse-error (type => 'missing start tag:tr', token => $token);
5078                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  }
5079                    
5080                    ## Clear back to table body context
5081                    while (not ($self->{open_elements}->[-1]->[1]
5082                                    & TABLE_ROWS_SCOPING_EL)) {
5083                      !!!cp ('t203');
5084                      ## ISSUE: Can this case be reached?
5085                      pop @{$self->{open_elements}};
5086                    }
5087                    
5088                    $self->{insertion_mode} = IN_ROW_IM;
5089                    if ($token->{tag_name} eq 'tr') {
5090                      !!!cp ('t204');
5091                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5092                      !!!nack ('t204');
5093                      !!!next-token;
5094                      next B;
5095                    } else {
5096                      !!!cp ('t205');
5097                      !!!insert-element ('tr',, $token);
5098                      ## reprocess in the "in row" insertion mode
5099                    }
5100                  } else {
5101                    !!!cp ('t206');
5102                  }
5103    
5104                  ## Clear back to table row context
5105                  while (not ($self->{open_elements}->[-1]->[1]
5106                                  & TABLE_ROW_SCOPING_EL)) {
5107                    !!!cp ('t207');
5108                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5109                }                }
5110                                
5111                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5112                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5113                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5114                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5115                } else {                
5116                  !!!insert-element ('tr');                !!!nack ('t207.1');
5117                  ## reprocess                !!!next-token;
5118                }                next B;
               redo B;  
5119              } elsif ({              } elsif ({
5120                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5121                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5122                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5123                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5124                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5125                my $i;                  ## As if </tr>
5126                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5127                  my $node = $self->{open_elements}->[$_];                  my $i;
5128                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5129                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5130                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5131                    $i = $_;                      !!!cp ('t208');
5132                    last INSCOPE;                      $i = $_;
5133                  } elsif ({                      last INSCOPE;
5134                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5135                           }->{$node->[1]}) {                      !!!cp ('t209');
5136                    last INSCOPE;                      last INSCOPE;
5137                      }
5138                    } # INSCOPE
5139                    unless (defined $i) {
5140                      !!!cp ('t210');
5141    ## TODO: This type is wrong.
5142                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5143                      ## Ignore the token
5144                      !!!nack ('t210.1');
5145                      !!!next-token;
5146                      next B;
5147                    }
5148                    
5149                    ## Clear back to table row context
5150                    while (not ($self->{open_elements}->[-1]->[1]
5151                                    & TABLE_ROW_SCOPING_EL)) {
5152                      !!!cp ('t211');
5153                      ## ISSUE: Can this case be reached?
5154                      pop @{$self->{open_elements}};
5155                    }
5156                    
5157                    pop @{$self->{open_elements}}; # tr
5158                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5159                    if ($token->{tag_name} eq 'tr') {
5160                      !!!cp ('t212');
5161                      ## reprocess
5162                      !!!ack-later;
5163                      next B;
5164                    } else {
5165                      !!!cp ('t213');
5166                      ## reprocess in the "in table body" insertion mode...
5167                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5168                }                }
5169    
5170                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5171                while (not {                  ## have an element in table scope
5172                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5173                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5174                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5175                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5176                        !!!cp ('t214');
5177                        $i = $_;
5178                        last INSCOPE;
5179                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5180                        !!!cp ('t215');
5181                        last INSCOPE;
5182                      }
5183                    } # INSCOPE
5184                    unless (defined $i) {
5185                      !!!cp ('t216');
5186    ## TODO: This erorr type ios wrong.
5187                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5188                      ## Ignore the token
5189                      !!!nack ('t216.1');
5190                      !!!next-token;
5191                      next B;
5192                    }
5193    
5194                    ## Clear back to table body context
5195                    while (not ($self->{open_elements}->[-1]->[1]
5196                                    & TABLE_ROWS_SCOPING_EL)) {
5197                      !!!cp ('t217');
5198                      ## ISSUE: Can this state be reached?
5199                      pop @{$self->{open_elements}};
5200                    }
5201                    
5202                    ## As if <{current node}>
5203                    ## have an element in table scope
5204                    ## true by definition
5205                    
5206                    ## Clear back to table body context
5207                    ## nop by definition
5208                    
5209                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5210                    $self->{insertion_mode} = IN_TABLE_IM;
5211                    ## reprocess in "in table" insertion mode...
5212                  } else {
5213                    !!!cp ('t218');
5214                }                }
5215    
5216                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5217                ## have an element in table scope                  ## Clear back to table context
5218                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5219                                    & TABLE_SCOPING_EL)) {
5220                ## Clear back to table body context                    !!!cp ('t219');
5221                ## nop by definition                    ## ISSUE: Can this state be reached?
5222                      pop @{$self->{open_elements}};
5223                pop @{$self->{open_elements}};                  }
5224                $self->{insertion_mode} = 'in table';                  
5225                ## reprocess                  !!!insert-element ('colgroup',, $token);
5226                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5227                    ## reprocess
5228                    !!!ack-later;
5229                    next B;
5230                  } elsif ({
5231                            caption => 1,
5232                            colgroup => 1,
5233                            tbody => 1, tfoot => 1, thead => 1,
5234                           }->{$token->{tag_name}}) {
5235                    ## Clear back to table context
5236                    while (not ($self->{open_elements}->[-1]->[1]
5237                                    & TABLE_SCOPING_EL)) {
5238                      !!!cp ('t220');
5239                      ## ISSUE: Can this state be reached?
5240                      pop @{$self->{open_elements}};
5241                    }
5242                    
5243                    push @$active_formatting_elements, ['#marker', '']
5244                        if $token->{tag_name} eq 'caption';
5245                    
5246                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5247                    $self->{insertion_mode} = {
5248                                               caption => IN_CAPTION_IM,
5249                                               colgroup => IN_COLUMN_GROUP_IM,
5250                                               tbody => IN_TABLE_BODY_IM,
5251                                               tfoot => IN_TABLE_BODY_IM,
5252                                               thead => IN_TABLE_BODY_IM,
5253                                              }->{$token->{tag_name}};
5254                    !!!next-token;
5255                    !!!nack ('t220.1');
5256                    next B;
5257                  } else {
5258                    die "$0: in table: <>: $token->{tag_name}";
5259                  }
5260              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5261                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5262                !!!parse-error (type => 'not closed:table');                                value => $self->{open_elements}->[-1]->[0]
5263                                      ->manakai_local_name,
5264                                  token => $token);
5265    
5266                ## As if </table>                ## As if </table>
5267                ## have a table element in table scope                ## have a table element in table scope
5268                my $i;                my $i;
5269                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5270                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5271                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5272                      !!!cp ('t221');
5273                    $i = $_;                    $i = $_;
5274                    last INSCOPE;                    last INSCOPE;
5275                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5276                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5277                    last INSCOPE;                    last INSCOPE;
5278                  }                  }
5279                } # INSCOPE                } # INSCOPE
5280                unless (defined $i) {                unless (defined $i) {
5281                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5282    ## TODO: The following is wrong, maybe.
5283                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5284                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5285                    !!!nack ('t223.1');
5286                  !!!next-token;                  !!!next-token;
5287                  redo B;                  next B;
5288                }                }
5289                                
5290    ## TODO: Followings are removed from the latest spec.
5291                ## generate implied end tags                ## generate implied end tags
5292                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5293                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5294                     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;  
5295                }                }
5296    
5297                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5298                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5299                    ## NOTE: |<table><tr><table>|
5300                    !!!parse-error (type => 'not closed',
5301                                    value => $self->{open_elements}->[-1]->[0]
5302                                        ->manakai_local_name,
5303                                    token => $token);
5304                  } else {
5305                    !!!cp ('t226');
5306                }                }
5307    
5308                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5309                  pop @{$open_tables};
5310    
5311                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5312    
5313                ## reprocess            ## reprocess
5314                redo B;            !!!ack-later;
5315              } else {            next B;
5316                #          } elsif ($token->{tag_name} eq 'style') {
5317              }            if (not $open_tables->[-1]->[1]) { # tainted
5318            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5319              if ({              ## NOTE: This is a "as if in head" code clone.
5320                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5321                  }->{$token->{tag_name}}) {              next B;
5322                ## have an element in table scope            } else {
5323                my $i;              !!!cp ('t227.7');
5324                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5325                  my $node = $self->{open_elements}->[$_];            }
5326                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5327                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5328                    last INSCOPE;              !!!cp ('t227.6');
5329                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5330                            table => 1, html => 1,              $script_start_tag->();
5331                           }->{$node->[1]}) {              next B;
5332                    last INSCOPE;            } else {
5333                  }              !!!cp ('t227.5');
5334                } # INSCOPE              #
5335                unless (defined $i) {            }
5336                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5337                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5338                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5339                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5340                }                if ($type eq 'hidden') {
5341                    !!!cp ('t227.3');
5342                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5343    
5344                ## 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}};  
               }  
5345    
5346                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;  
               }  
5347    
               ## 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]);  
5348                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
5349    
5350                pop @{$self->{open_elements}};                  !!!next-token;
5351                $self->{insertion_mode} = 'in table';                  !!!ack ('t227.2.1');
5352                ## reprocess                  next B;
5353                redo B;                } else {
5354              } elsif ({                  !!!cp ('t227.2');
5355                        body => 1, caption => 1, col => 1, colgroup => 1,                  #
5356                        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;  
5357              } else {              } else {
5358                  !!!cp ('t227.1');
5359                #                #
5360              }              }
5361            } else {            } else {
5362                !!!cp ('t227.4');
5363              #              #
5364            }            }
5365                      } else {
5366            ## As if in table            !!!cp ('t227');
5367            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5368            $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');  
5369    
5370              ## As if in body, but insert into foster parent element          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## 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';  
5371    
5372                push @$active_formatting_elements, ['#marker', ''];          $insert = $insert_to_foster;
5373                          #
5374                !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
5375                redo B;              if ($token->{tag_name} eq 'tr' and
5376              } 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>  
5377                ## have an element in table scope                ## have an element in table scope
5378                my $i;                my $i;
5379                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5380                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5381                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5382                      !!!cp ('t228');
5383                    $i = $_;                    $i = $_;
5384                    last INSCOPE;                    last INSCOPE;
5385                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5386                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5387                    last INSCOPE;                    last INSCOPE;
5388                  }                  }
5389                } # INSCOPE                } # INSCOPE
5390                unless (defined $i) {                unless (defined $i) {
5391                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5392                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5393                  ## Ignore the token                  ## Ignore the token
5394                    !!!nack ('t230.1');
5395                  !!!next-token;                  !!!next-token;
5396                  redo B;                  next B;
5397                  } else {
5398                    !!!cp ('t232');
5399                }                }
5400    
5401                ## Clear back to table row context                ## Clear back to table row context
5402                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5403                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5404                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5405                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5406                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5407                }                }
5408    
5409                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5410                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5411                ## reprocess                !!!next-token;
5412                redo B;                !!!nack ('t231.1');
5413                  next B;
5414              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5415                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5416                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5417                    ## have an element in table scope
5418                ## As if </table>                  my $i;
5419                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5420                my $i;                    my $node = $self->{open_elements}->[$_];
5421                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5422                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5423                  if ($node->[1] eq 'table') {                      $i = $_;
5424                    $i = $_;                      last INSCOPE;
5425                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5426                  } elsif ({                      !!!cp ('t234');
5427                            table => 1, html => 1,                      last INSCOPE;
5428                           }->{$node->[1]}) {                    }
5429                    last INSCOPE;                  } # INSCOPE
5430                    unless (defined $i) {
5431                      !!!cp ('t235');
5432    ## TODO: The following is wrong.
5433                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5434                      ## Ignore the token
5435                      !!!nack ('t236.1');
5436                      !!!next-token;
5437                      next B;
5438                  }                  }
5439                } # INSCOPE                  
5440                unless (defined $i) {                  ## Clear back to table row context
5441                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5442                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5443                  !!!next-token;                    !!!cp ('t236');
5444                  redo B;  ## ISSUE: Can this state be reached?
5445                }                    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;  
5446                  }                  }
5447                } # INSCOPE                  
5448                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5449                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5450                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5451                  !!!next-token;                }
5452                  redo B;  
5453                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5454                    ## have an element in table scope
5455                ## Clear back to table row context                  my $i;
5456                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5457                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5458                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5459                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5460                        $i = $_;
5461                        last INSCOPE;
5462                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5463                        !!!cp ('t238');
5464                        last INSCOPE;
5465                      }
5466                    } # INSCOPE
5467                    unless (defined $i) {
5468                      !!!cp ('t239');
5469                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5470                      ## Ignore the token
5471                      !!!nack ('t239.1');
5472                      !!!next-token;
5473                      next B;
5474                    }
5475                    
5476                    ## Clear back to table body context
5477                    while (not ($self->{open_elements}->[-1]->[1]
5478                                    & TABLE_ROWS_SCOPING_EL)) {
5479                      !!!cp ('t240');
5480                      pop @{$self->{open_elements}};
5481                    }
5482                    
5483                    ## As if <{current node}>
5484                    ## have an element in table scope
5485                    ## true by definition
5486                    
5487                    ## Clear back to table body context
5488                    ## nop by definition
5489                    
5490                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5491                    $self->{insertion_mode} = IN_TABLE_IM;
5492                    ## reprocess in the "in table" insertion mode...
5493                }                }
5494    
5495                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
5496                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
5497                !!!next-token;                ## the code for <table> in the "in table" insertion mode
5498                redo B;                ## is synced with it.
5499              } elsif ($token->{tag_name} eq 'table') {  
5500                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
5501                my $i;                my $i;
5502                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5503                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5504                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
5505                      !!!cp ('t241');
5506                    $i = $_;                    $i = $_;
5507                    last INSCOPE;                    last INSCOPE;
5508                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5509                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5510                    last INSCOPE;                    last INSCOPE;
5511                  }                  }
5512                } # INSCOPE                } # INSCOPE
5513                unless (defined $i) {                unless (defined $i) {
5514                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
5515                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5516                  ## Ignore the token                  ## Ignore the token
5517                    !!!nack ('t243.1');
5518                  !!!next-token;                  !!!next-token;
5519                  redo B;                  next B;
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
5520                }                }
5521                    
5522                pop @{$self->{open_elements}}; # tr                splice @{$self->{open_elements}}, $i;
5523                $self->{insertion_mode} = 'in table body';                pop @{$open_tables};
5524                ## reprocess                
5525                redo B;                $self->_reset_insertion_mode;
5526                  
5527                  !!!next-token;
5528                  next B;
5529              } elsif ({              } elsif ({
5530                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5531                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
5532                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
5533                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
5534                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5535                  my $node = $self->{open_elements}->[$_];                  my $i;
5536                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5537                    $i = $_;                    my $node = $self->{open_elements}->[$_];
5538                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5539                  } elsif ({                      !!!cp ('t247');
5540                            table => 1, html => 1,                      $i = $_;
5541                           }->{$node->[1]}) {                      last INSCOPE;
5542                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5543                        !!!cp ('t248');
5544                        last INSCOPE;
5545                      }
5546                    } # INSCOPE
5547                      unless (defined $i) {
5548                        !!!cp ('t249');
5549                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5550                        ## Ignore the token
5551                        !!!nack ('t249.1');
5552                        !!!next-token;
5553                        next B;
5554                      }
5555                    
5556                    ## As if </tr>
5557                    ## have an element in table scope
5558                    my $i;
5559                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5560                      my $node = $self->{open_elements}->[$_];
5561                      if ($node->[1] & TABLE_ROW_EL) {
5562                        !!!cp ('t250');
5563                        $i = $_;
5564                        last INSCOPE;
5565                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5566                        !!!cp ('t251');
5567                        last INSCOPE;
5568                      }
5569                    } # INSCOPE
5570                      unless (defined $i) {
5571                        !!!cp ('t252');
5572                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5573                        ## Ignore the token
5574                        !!!nack ('t252.1');
5575                        !!!next-token;
5576                        next B;
5577                      }
5578                    
5579                    ## Clear back to table row context
5580                    while (not ($self->{open_elements}->[-1]->[1]
5581                                    & TABLE_ROW_SCOPING_EL)) {
5582                      !!!cp ('t253');
5583    ## ISSUE: Can this case be reached?
5584                      pop @{$self->{open_elements}};
5585                  }                  }
5586                } # INSCOPE                  
5587                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5588                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5589                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
5590                }                }
5591    
               ## As if </tr>  
5592                ## have an element in table scope                ## have an element in table scope
5593                my $i;                my $i;
5594                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5595                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5596                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5597                      !!!cp ('t254');
5598                    $i = $_;                    $i = $_;
5599                    last INSCOPE;                    last INSCOPE;
5600                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5601                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5602                    last INSCOPE;                    last INSCOPE;
5603                  }                  }
5604                } # INSCOPE                } # INSCOPE
5605                unless (defined $i) {                unless (defined $i) {
5606                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
5607                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5608                  ## Ignore the token                  ## Ignore the token
5609                    !!!nack ('t256.1');
5610                  !!!next-token;                  !!!next-token;
5611                  redo B;                  next B;
5612                }                }
5613    
5614                ## Clear back to table row context                ## Clear back to table body context
5615                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5616                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5617                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5618                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5619                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5620                }                }
5621    
5622                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
5623                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
5624                ## reprocess                !!!nack ('t257.1');
5625                redo B;                !!!next-token;
5626                  next B;
5627              } elsif ({              } elsif ({
5628                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5629                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5630                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5631                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5632                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5633                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5634                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5635                !!!next-token;            ## Ignore the token
5636                redo B;            !!!nack ('t258.1');
5637              } else {             !!!next-token;
5638                #            next B;
5639              }          } else {
5640            } else {            !!!cp ('t259');
5641              #            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
           }  
5642    
5643            ## As if in table            $insert = $insert_to_foster;
5644            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5645            $in_body->($insert_to_foster);          }
5646            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5647          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5648            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
5649              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
5650              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
5651                          #
5652              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5653              !!!cp ('t259.2');
5654              #
5655            }
5656    
5657              !!!next-token;          ## Stop parsing
5658              redo B;          last B;
5659            } elsif ($token->{type} eq 'comment') {        } else {
5660              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
5661              my $comment = $self->{document}->create_comment ($token->{data});        }
5662              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5663              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
5664              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5665            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5666              if ({                unless (length $token->{data}) {
5667                   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  
5668                  !!!next-token;                  !!!next-token;
5669                  redo B;                  next B;
5670                }                }
5671                }
5672                ## Close the cell              
5673                !!!back-token; # <?>              !!!cp ('t261');
5674                $token = {type => 'end tag', tag_name => $tn};              #
5675                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
5676              } else {              if ($token->{tag_name} eq 'col') {
5677                  !!!cp ('t262');
5678                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5679                  pop @{$self->{open_elements}};
5680                  !!!ack ('t262.1');
5681                  !!!next-token;
5682                  next B;
5683                } else {
5684                  !!!cp ('t263');
5685                #                #
5686              }              }
5687            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5688              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
5689                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5690                my $i;                  !!!cp ('t264');
5691                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag:colgroup', 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) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5692                  ## Ignore the token                  ## Ignore the token
5693                  !!!next-token;                  !!!next-token;
5694                  redo B;                  next B;
5695                }                } else {
5696                                  !!!cp ('t265');
5697                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
5698                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
5699                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
5700                     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]);  
5701                }                }
5702                } elsif ($token->{tag_name} eq 'col') {
5703                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
5704                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
               $clear_up_to_marker->();  
   
               $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});  
5705                ## Ignore the token                ## Ignore the token
5706                !!!next-token;                !!!next-token;
5707                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;  
5708              } else {              } else {
5709                #                !!!cp ('t267');
5710                  #
5711              }              }
5712          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5713            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5714                @{$self->{open_elements}} == 1) { # redundant, maybe
5715              !!!cp ('t270.2');
5716              ## Stop parsing.
5717              last B;
5718            } else {
5719              ## NOTE: As if </colgroup>.
5720              !!!cp ('t270.1');
5721              pop @{$self->{open_elements}}; # colgroup
5722              $self->{insertion_mode} = IN_TABLE_IM;
5723              ## Reprocess.
5724              next B;
5725            }
5726          } else {
5727            die "$0: $token->{type}: Unknown token type";
5728          }
5729    
5730              ## As if </colgroup>
5731              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5732                !!!cp ('t269');
5733    ## TODO: Wrong error type?
5734                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5735                ## Ignore the token
5736                !!!nack ('t269.1');
5737                !!!next-token;
5738                next B;
5739            } else {            } else {
5740              #              !!!cp ('t270');
5741                pop @{$self->{open_elements}}; # colgroup
5742                $self->{insertion_mode} = IN_TABLE_IM;
5743                !!!ack-later;
5744                ## reprocess
5745                next B;
5746              }
5747        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5748          if ($token->{type} == CHARACTER_TOKEN) {
5749            !!!cp ('t271');
5750            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5751            !!!next-token;
5752            next B;
5753          } elsif ($token->{type} == START_TAG_TOKEN) {
5754            if ($token->{tag_name} eq 'option') {
5755              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5756                !!!cp ('t272');
5757                ## As if </option>
5758                pop @{$self->{open_elements}};
5759              } else {
5760                !!!cp ('t273');
5761            }            }
             
           $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}};  
               }  
5762    
5763                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5764                !!!next-token;            !!!nack ('t273.1');
5765                redo B;            !!!next-token;
5766              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5767                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5768                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5769                  pop @{$self->{open_elements}};              !!!cp ('t274');
5770                }              ## As if </option>
5771                pop @{$self->{open_elements}};
5772              } else {
5773                !!!cp ('t275');
5774              }
5775    
5776                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5777                  ## As if </optgroup>              !!!cp ('t276');
5778                  pop @{$self->{open_elements}};              ## As if </optgroup>
5779                }              pop @{$self->{open_elements}};
5780              } else {
5781                !!!cp ('t277');
5782              }
5783    
5784                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5785                !!!next-token;            !!!nack ('t277.1');
5786                redo B;            !!!next-token;
5787              } elsif ($token->{tag_name} eq 'select') {            next B;
5788                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5789                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5790                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5791                my $i;                    {
5792                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5793                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5794                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5795                    $i = $_;                    }->{$token->{tag_name}})) {
5796                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5797                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5798                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5799                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5800                    last INSCOPE;            ## have an element in table scope
5801                  }            my $i;
5802                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5803                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5804                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5805                  ## Ignore the token                !!!cp ('t278');
5806                  !!!next-token;                $i = $_;
5807                  redo B;                last INSCOPE;
5808                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5809                  !!!cp ('t279');
5810                  last INSCOPE;
5811                }
5812              } # INSCOPE
5813              unless (defined $i) {
5814                !!!cp ('t280');
5815                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5816                ## Ignore the token
5817                !!!nack ('t280.1');
5818                !!!next-token;
5819                next B;
5820              }
5821                                
5822                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5823              splice @{$self->{open_elements}}, $i;
5824    
5825                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5826    
5827                !!!next-token;            if ($token->{tag_name} eq 'select') {
5828                redo B;              !!!nack ('t281.2');
5829              } else {              !!!next-token;
5830                #              next B;
5831              } else {
5832                !!!cp ('t281.1');
5833                !!!ack-later;
5834                ## Reprocess the token.
5835                next B;
5836              }
5837            } else {
5838              !!!cp ('t282');
5839              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5840              ## Ignore the token
5841              !!!nack ('t282.1');
5842              !!!next-token;
5843              next B;
5844            }
5845          } elsif ($token->{type} == END_TAG_TOKEN) {
5846            if ($token->{tag_name} eq 'optgroup') {
5847              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5848                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5849                !!!cp ('t283');
5850                ## As if </option>
5851                splice @{$self->{open_elements}}, -2;
5852              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5853                !!!cp ('t284');
5854                pop @{$self->{open_elements}};
5855              } else {
5856                !!!cp ('t285');
5857                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5858                ## Ignore the token
5859              }
5860              !!!nack ('t285.1');
5861              !!!next-token;
5862              next B;
5863            } elsif ($token->{tag_name} eq 'option') {
5864              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5865                !!!cp ('t286');
5866                pop @{$self->{open_elements}};
5867              } else {
5868                !!!cp ('t287');
5869                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5870                ## Ignore the token
5871              }
5872              !!!nack ('t287.1');
5873              !!!next-token;
5874              next B;
5875            } elsif ($token->{tag_name} eq 'select') {
5876              ## have an element in table scope
5877              my $i;
5878              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5879                my $node = $self->{open_elements}->[$_];
5880                if ($node->[1] & SELECT_EL) {
5881                  !!!cp ('t288');
5882                  $i = $_;
5883                  last INSCOPE;
5884                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5885                  !!!cp ('t289');
5886                  last INSCOPE;
5887              }              }
5888            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
5889              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
5890                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
5891                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5892                  ## As if </option>              ## Ignore the token
5893                  splice @{$self->{open_elements}}, -2;              !!!nack ('t290.1');
5894                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!next-token;
5895                  pop @{$self->{open_elements}};              next B;
5896                } else {            }
                 !!!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;  
               }  
5897                                
5898                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5899              splice @{$self->{open_elements}}, $i;
5900    
5901                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5902    
5903                !!!next-token;            !!!nack ('t291.1');
5904                redo B;            !!!next-token;
5905              } elsif ({            next B;
5906                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5907                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5908                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5909                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5910                                   }->{$token->{tag_name}}) {
5911                ## have an element in table scope  ## TODO: The following is wrong?
5912                my $i;            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
               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) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
5913                                
5914                ## As if </select>            ## have an element in table scope
5915                ## have an element in table scope            my $i;
5916                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5917                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
5918                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5919                  if ($node->[1] eq 'select') {                !!!cp ('t292');
5920                    $i = $_;                $i = $_;
5921                    last INSCOPE;                last INSCOPE;
5922                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5923                            table => 1, html => 1,                !!!cp ('t293');
5924                           }->{$node->[1]}) {                last INSCOPE;
5925                    last INSCOPE;              }
5926                  }            } # INSCOPE
5927                } # INSCOPE            unless (defined $i) {
5928                unless (defined $i) {              !!!cp ('t294');
5929                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
5930                  ## Ignore the </select> token              !!!nack ('t294.1');
5931                  !!!next-token; ## TODO: ok?              !!!next-token;
5932                  redo B;              next B;
5933                }            }
5934                                
5935                splice @{$self->{open_elements}}, $i;            ## As if </select>
5936              ## have an element in table scope
5937                $self->_reset_insertion_mode;            undef $i;
5938              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5939                ## reprocess              my $node = $self->{open_elements}->[$_];
5940                redo B;              if ($node->[1] & SELECT_EL) {
5941              } else {                !!!cp ('t295');
5942                #                $i = $_;
5943                  last INSCOPE;
5944                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5945    ## ISSUE: Can this state be reached?
5946                  !!!cp ('t296');
5947                  last INSCOPE;
5948              }              }
5949            } else {            } # INSCOPE
5950              #            unless (defined $i) {
5951                !!!cp ('t297');
5952    ## TODO: The following error type is correct?
5953                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5954                ## Ignore the </select> token
5955                !!!nack ('t297.1');
5956                !!!next-token; ## TODO: ok?
5957                next B;
5958            }            }
5959                  
5960              !!!cp ('t298');
5961              splice @{$self->{open_elements}}, $i;
5962    
5963              $self->_reset_insertion_mode;
5964    
5965            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
5966              ## reprocess
5967              next B;
5968            } else {
5969              !!!cp ('t299');
5970              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5971            ## Ignore the token            ## Ignore the token
5972              !!!nack ('t299.3');
5973            !!!next-token;            !!!next-token;
5974            redo B;            next B;
5975          } elsif ($self->{insertion_mode} eq 'after body') {          }
5976            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5977              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5978                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
5979                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
5980              !!!parse-error (type => 'in body:#eof', token => $token);
5981            } else {
5982              !!!cp ('t299.2');
5983            }
5984    
5985            ## Stop parsing.
5986            last B;
5987          } else {
5988            die "$0: $token->{type}: Unknown token type";
5989          }
5990        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5991          if ($token->{type} == CHARACTER_TOKEN) {
5992            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5993              my $data = $1;
5994              ## As if in body
5995              $reconstruct_active_formatting_elements->($insert_to_current);
5996                                
5997                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5998              
5999              unless (length $token->{data}) {
6000                !!!cp ('t300');
6001                !!!next-token;
6002                next B;
6003              }
6004            }
6005            
6006            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6007              !!!cp ('t301');
6008              !!!parse-error (type => 'after html:#character', token => $token);
6009    
6010                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
6011                  !!!next-token;          } else {
6012                  redo B;            !!!cp ('t302');
6013                }          }
6014              }          
6015                        ## "after body" insertion mode
6016              #          !!!parse-error (type => 'after body:#character', token => $token);
6017              !!!parse-error (type => 'after body:#'.$token->{type});  
6018            } elsif ($token->{type} eq 'comment') {          $self->{insertion_mode} = IN_BODY_IM;
6019              my $comment = $self->{document}->create_comment ($token->{data});          ## reprocess
6020              $self->{open_elements}->[0]->[0]->append_child ($comment);          next B;
6021          } elsif ($token->{type} == START_TAG_TOKEN) {
6022            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6023              !!!cp ('t303');
6024              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6025              
6026              ## Reprocess in the "after body" insertion mode.
6027            } else {
6028              !!!cp ('t304');
6029            }
6030    
6031            ## "after body" insertion mode
6032            !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6033    
6034            $self->{insertion_mode} = IN_BODY_IM;
6035            !!!ack-later;
6036            ## reprocess
6037            next B;
6038          } elsif ($token->{type} == END_TAG_TOKEN) {
6039            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6040              !!!cp ('t305');
6041              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6042              
6043              $self->{insertion_mode} = AFTER_BODY_IM;
6044              ## Reprocess in the "after body" insertion mode.
6045            } else {
6046              !!!cp ('t306');
6047            }
6048    
6049            ## "after body" insertion mode
6050            if ($token->{tag_name} eq 'html') {
6051              if (defined $self->{inner_html_node}) {
6052                !!!cp ('t307');
6053                !!!parse-error (type => 'unmatched end tag:html', token => $token);
6054                ## Ignore the token
6055              !!!next-token;              !!!next-token;
6056              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});  
             }  
6057            } else {            } else {
6058              !!!parse-error (type => 'after body:#'.$token->{type});              !!!cp ('t308');
6059                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6060                !!!next-token;
6061                next B;
6062            }            }
6063            } else {
6064              !!!cp ('t309');
6065              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6066    
6067            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
6068            ## reprocess            ## reprocess
6069            redo B;            next B;
6070          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6071            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6072              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          !!!cp ('t309.2');
6073                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          ## Stop parsing
6074            last B;
6075          } else {
6076            die "$0: $token->{type}: Unknown token type";
6077          }
6078        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6079          if ($token->{type} == CHARACTER_TOKEN) {
6080            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6081              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6082              
6083              unless (length $token->{data}) {
6084                !!!cp ('t310');
6085                !!!next-token;
6086                next B;
6087              }
6088            }
6089            
6090            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6091              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6092                !!!cp ('t311');
6093                !!!parse-error (type => 'in frameset:#character', token => $token);
6094              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6095                !!!cp ('t312');
6096                !!!parse-error (type => 'after frameset:#character', token => $token);
6097              } else { # "after html frameset"
6098                !!!cp ('t313');
6099                !!!parse-error (type => 'after html:#character', token => $token);
6100    
6101                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6102                ## Reprocess in the "after frameset" insertion mode.
6103                !!!parse-error (type => 'after frameset:#character', token => $token);
6104              }
6105              
6106              ## Ignore the token.
6107              if (length $token->{data}) {
6108                !!!cp ('t314');
6109                ## reprocess the rest of characters
6110              } else {
6111                !!!cp ('t315');
6112                !!!next-token;
6113              }
6114              next B;
6115            }
6116            
6117            die qq[$0: Character "$token->{data}"];
6118          } elsif ($token->{type} == START_TAG_TOKEN) {
6119            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6120              !!!cp ('t316');
6121              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6122    
6123              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6124              ## Process in the "after frameset" insertion mode.
6125            } else {
6126              !!!cp ('t317');
6127            }
6128    
6129            if ($token->{tag_name} eq 'frameset' and
6130                $self->{insertion_mode} == IN_FRAMESET_IM) {
6131              !!!cp ('t318');
6132              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6133              !!!nack ('t318.1');
6134              !!!next-token;
6135              next B;
6136            } elsif ($token->{tag_name} eq 'frame' and
6137                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6138              !!!cp ('t319');
6139              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6140              pop @{$self->{open_elements}};
6141              !!!ack ('t319.1');
6142              !!!next-token;
6143              next B;
6144            } elsif ($token->{tag_name} eq 'noframes') {
6145              !!!cp ('t320');
6146              ## NOTE: As if in body.
6147              $parse_rcdata->(CDATA_CONTENT_MODEL);
6148              next B;
6149            } else {
6150              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6151                !!!cp ('t321');
6152                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6153              } else {
6154                !!!cp ('t322');
6155                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6156              }
6157              ## Ignore the token
6158              !!!nack ('t322.1');
6159              !!!next-token;
6160              next B;
6161            }
6162          } elsif ($token->{type} == END_TAG_TOKEN) {
6163            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6164              !!!cp ('t323');
6165              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6166    
6167                unless (length $token->{data}) {            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6168                  !!!next-token;            ## Process in the "after frameset" insertion mode.
6169                  redo B;          } else {
6170                }            !!!cp ('t324');
6171              }          }
6172    
6173              #          if ($token->{tag_name} eq 'frameset' and
6174            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6175              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6176              $self->{open_elements}->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
6177                !!!cp ('t325');
6178                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6179                ## Ignore the token
6180              !!!next-token;              !!!next-token;
             redo 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 {  
               #  
             }  
6181            } else {            } else {
6182              #              !!!cp ('t326');
6183                pop @{$self->{open_elements}};
6184                !!!next-token;
6185            }            }
6186              
6187            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
6188              !!!parse-error (type => 'in frameset:'.$token->{tag_name});                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6189                !!!cp ('t327');
6190                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6191              } else {
6192                !!!cp ('t328');
6193              }
6194              next B;
6195            } elsif ($token->{tag_name} eq 'html' and
6196                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6197              !!!cp ('t329');
6198              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6199              !!!next-token;
6200              next B;
6201            } else {
6202              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6203                !!!cp ('t330');
6204                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6205            } else {            } else {
6206              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t331');
6207                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6208            }            }
6209            ## Ignore the token            ## Ignore the token
6210            !!!next-token;            !!!next-token;
6211            redo B;            next B;
6212          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6213            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6214              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6215                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                  @{$self->{open_elements}} == 1) { # redundant, maybe
6216              !!!cp ('t331.1');
6217              !!!parse-error (type => 'in body:#eof', token => $token);
6218            } else {
6219              !!!cp ('t331.2');
6220            }
6221            
6222            ## Stop parsing
6223            last B;
6224          } else {
6225            die "$0: $token->{type}: Unknown token type";
6226          }
6227    
6228                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
6229                  !!!next-token;      } else {
6230                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
6231                }      }
6232    
6233        ## "in body" insertion mode
6234        if ($token->{type} == START_TAG_TOKEN) {
6235          if ($token->{tag_name} eq 'script') {
6236            !!!cp ('t332');
6237            ## NOTE: This is an "as if in head" code clone
6238            $script_start_tag->();
6239            next B;
6240          } elsif ($token->{tag_name} eq 'style') {
6241            !!!cp ('t333');
6242            ## NOTE: This is an "as if in head" code clone
6243            $parse_rcdata->(CDATA_CONTENT_MODEL);
6244            next B;
6245          } elsif ({
6246                    base => 1, link => 1,
6247                   }->{$token->{tag_name}}) {
6248            !!!cp ('t334');
6249            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6250            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6251            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6252            !!!ack ('t334.1');
6253            !!!next-token;
6254            next B;
6255          } elsif ($token->{tag_name} eq 'meta') {
6256            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6257            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6258            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6259    
6260            unless ($self->{confident}) {
6261              if ($token->{attributes}->{charset}) {
6262                !!!cp ('t335');
6263                ## NOTE: Whether the encoding is supported or not is handled
6264                ## in the {change_encoding} callback.
6265                $self->{change_encoding}
6266                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6267                
6268                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6269                    ->set_user_data (manakai_has_reference =>
6270                                         $token->{attributes}->{charset}
6271                                             ->{has_reference});
6272              } elsif ($token->{attributes}->{content}) {
6273                if ($token->{attributes}->{content}->{value}
6274                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6275                        [\x09-\x0D\x20]*=
6276                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6277                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6278                  !!!cp ('t336');
6279                  ## NOTE: Whether the encoding is supported or not is handled
6280                  ## in the {change_encoding} callback.
6281                  $self->{change_encoding}
6282                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6283                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6284                      ->set_user_data (manakai_has_reference =>
6285                                           $token->{attributes}->{content}
6286                                                 ->{has_reference});
6287              }              }
6288              }
6289            } else {
6290              if ($token->{attributes}->{charset}) {
6291                !!!cp ('t337');
6292                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6293                    ->set_user_data (manakai_has_reference =>
6294                                         $token->{attributes}->{charset}
6295                                             ->{has_reference});
6296              }
6297              if ($token->{attributes}->{content}) {
6298                !!!cp ('t338');
6299                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6300                    ->set_user_data (manakai_has_reference =>
6301                                         $token->{attributes}->{content}
6302                                             ->{has_reference});
6303              }
6304            }
6305    
6306              #          !!!ack ('t338.1');
6307            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6308              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6309              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6310              !!!next-token;          !!!cp ('t341');
6311              redo B;          ## NOTE: This is an "as if in head" code clone
6312            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6313              if ($token->{tag_name} eq 'noframes') {          next B;
6314                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6315                redo B;          !!!parse-error (type => 'in body:body', token => $token);
6316              } else {                
6317                #          if (@{$self->{open_elements}} == 1 or
6318                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6319              !!!cp ('t342');
6320              ## Ignore the token
6321            } else {
6322              my $body_el = $self->{open_elements}->[1]->[0];
6323              for my $attr_name (keys %{$token->{attributes}}) {
6324                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6325                  !!!cp ('t343');
6326                  $body_el->set_attribute_ns
6327                    (undef, [undef, $attr_name],
6328                     $token->{attributes}->{$attr_name}->{value});
6329              }              }
6330            } elsif ($token->{type} eq 'end tag') {            }
6331              if ($token->{tag_name} eq 'html') {          }
6332                $phase = 'trailing end';          !!!nack ('t343.1');
6333            !!!next-token;
6334            next B;
6335          } elsif ({
6336                    address => 1, blockquote => 1, center => 1, dir => 1,
6337                    div => 1, dl => 1, fieldset => 1,
6338                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6339                    menu => 1, ol => 1, p => 1, ul => 1,
6340                    pre => 1, listing => 1,
6341                    form => 1,
6342                    table => 1,
6343                    hr => 1,
6344                   }->{$token->{tag_name}}) {
6345            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6346              !!!cp ('t350');
6347              !!!parse-error (type => 'in form:form', token => $token);
6348              ## Ignore the token
6349              !!!nack ('t350.1');
6350              !!!next-token;
6351              next B;
6352            }
6353    
6354            ## has a p element in scope
6355            INSCOPE: for (reverse @{$self->{open_elements}}) {
6356              if ($_->[1] & P_EL) {
6357                !!!cp ('t344');
6358                !!!back-token; # <form>
6359                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6360                          line => $token->{line}, column => $token->{column}};
6361                next B;
6362              } elsif ($_->[1] & SCOPING_EL) {
6363                !!!cp ('t345');
6364                last INSCOPE;
6365              }
6366            } # INSCOPE
6367              
6368            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6369            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6370              !!!nack ('t346.1');
6371              !!!next-token;
6372              if ($token->{type} == CHARACTER_TOKEN) {
6373                $token->{data} =~ s/^\x0A//;
6374                unless (length $token->{data}) {
6375                  !!!cp ('t346');
6376                !!!next-token;                !!!next-token;
               redo B;  
6377              } else {              } else {
6378                #                !!!cp ('t349');
6379              }              }
6380            } else {            } else {
6381              #              !!!cp ('t348');
6382              }
6383            } elsif ($token->{tag_name} eq 'form') {
6384              !!!cp ('t347.1');
6385              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6386    
6387              !!!nack ('t347.2');
6388              !!!next-token;
6389            } elsif ($token->{tag_name} eq 'table') {
6390              !!!cp ('t382');
6391              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6392              
6393              $self->{insertion_mode} = IN_TABLE_IM;
6394    
6395              !!!nack ('t382.1');
6396              !!!next-token;
6397            } elsif ($token->{tag_name} eq 'hr') {
6398              !!!cp ('t386');
6399              pop @{$self->{open_elements}};
6400            
6401              !!!nack ('t386.1');
6402              !!!next-token;
6403            } else {
6404              !!!nack ('t347.1');
6405              !!!next-token;
6406            }
6407            next B;
6408          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6409            ## has a p element in scope
6410            INSCOPE: for (reverse @{$self->{open_elements}}) {
6411              if ($_->[1] & P_EL) {
6412                !!!cp ('t353');
6413                !!!back-token; # <x>
6414                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6415                          line => $token->{line}, column => $token->{column}};
6416                next B;
6417              } elsif ($_->[1] & SCOPING_EL) {
6418                !!!cp ('t354');
6419                last INSCOPE;
6420            }            }
6421            } # INSCOPE
6422                        
6423            if (defined $token->{tag_name}) {          ## Step 1
6424              !!!parse-error (type => 'after frameset:'.$token->{tag_name});          my $i = -1;
6425            my $node = $self->{open_elements}->[$i];
6426            my $li_or_dtdd = {li => {li => 1},
6427                              dt => {dt => 1, dd => 1},
6428                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6429            LI: {
6430              ## Step 2
6431              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6432                if ($i != -1) {
6433                  !!!cp ('t355');
6434                  !!!parse-error (type => 'not closed',
6435                                  value => $self->{open_elements}->[-1]->[0]
6436                                      ->manakai_local_name,
6437                                  token => $token);
6438                } else {
6439                  !!!cp ('t356');
6440                }
6441                splice @{$self->{open_elements}}, $i;
6442                last LI;
6443            } else {            } else {
6444              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
6445              }
6446              
6447              ## Step 3
6448              if (not ($node->[1] & FORMATTING_EL) and
6449                  #not $phrasing_category->{$node->[1]} and
6450                  ($node->[1] & SPECIAL_EL or
6451                   $node->[1] & SCOPING_EL) and
6452                  not ($node->[1] & ADDRESS_EL) and
6453                  not ($node->[1] & DIV_EL)) {
6454                !!!cp ('t358');
6455                last LI;
6456              }
6457              
6458              !!!cp ('t359');
6459              ## Step 4
6460              $i--;
6461              $node = $self->{open_elements}->[$i];
6462              redo LI;
6463            } # LI
6464              
6465            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6466            !!!nack ('t359.1');
6467            !!!next-token;
6468            next B;
6469          } elsif ($token->{tag_name} eq 'plaintext') {
6470            ## has a p element in scope
6471            INSCOPE: for (reverse @{$self->{open_elements}}) {
6472              if ($_->[1] & P_EL) {
6473                !!!cp ('t367');
6474                !!!back-token; # <plaintext>
6475                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6476                          line => $token->{line}, column => $token->{column}};
6477                next B;
6478              } elsif ($_->[1] & SCOPING_EL) {
6479                !!!cp ('t368');
6480                last INSCOPE;
6481              }
6482            } # INSCOPE
6483              
6484            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6485              
6486            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6487              
6488            !!!nack ('t368.1');
6489            !!!next-token;
6490            next B;
6491          } elsif ($token->{tag_name} eq 'a') {
6492            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6493              my $node = $active_formatting_elements->[$i];
6494              if ($node->[1] & A_EL) {
6495                !!!cp ('t371');
6496                !!!parse-error (type => 'in a:a', token => $token);
6497                
6498                !!!back-token; # <a>
6499                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6500                          line => $token->{line}, column => $token->{column}};
6501                $formatting_end_tag->($token);
6502                
6503                AFE2: for (reverse 0..$#$active_formatting_elements) {
6504                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6505                    !!!cp ('t372');
6506                    splice @$active_formatting_elements, $_, 1;
6507                    last AFE2;
6508                  }
6509                } # AFE2
6510                OE: for (reverse 0..$#{$self->{open_elements}}) {
6511                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6512                    !!!cp ('t373');
6513                    splice @{$self->{open_elements}}, $_, 1;
6514                    last OE;
6515                  }
6516                } # OE
6517                last AFE;
6518              } elsif ($node->[0] eq '#marker') {
6519                !!!cp ('t374');
6520                last AFE;
6521            }            }
6522            } # AFE
6523              
6524            $reconstruct_active_formatting_elements->($insert_to_current);
6525    
6526            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6527            push @$active_formatting_elements, $self->{open_elements}->[-1];
6528    
6529            !!!nack ('t374.1');
6530            !!!next-token;
6531            next B;
6532          } elsif ($token->{tag_name} eq 'nobr') {
6533            $reconstruct_active_formatting_elements->($insert_to_current);
6534    
6535            ## has a |nobr| element in scope
6536            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6537              my $node = $self->{open_elements}->[$_];
6538              if ($node->[1] & NOBR_EL) {
6539                !!!cp ('t376');
6540                !!!parse-error (type => 'in nobr:nobr', token => $token);
6541                !!!back-token; # <nobr>
6542                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6543                          line => $token->{line}, column => $token->{column}};
6544                next B;
6545              } elsif ($node->[1] & SCOPING_EL) {
6546                !!!cp ('t377');
6547                last INSCOPE;
6548              }
6549            } # INSCOPE
6550            
6551            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6552            push @$active_formatting_elements, $self->{open_elements}->[-1];
6553            
6554            !!!nack ('t377.1');
6555            !!!next-token;
6556            next B;
6557          } elsif ($token->{tag_name} eq 'button') {
6558            ## has a button element in scope
6559            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6560              my $node = $self->{open_elements}->[$_];
6561              if ($node->[1] & BUTTON_EL) {
6562                !!!cp ('t378');
6563                !!!parse-error (type => 'in button:button', token => $token);
6564                !!!back-token; # <button>
6565                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6566                          line => $token->{line}, column => $token->{column}};
6567                next B;
6568              } elsif ($node->[1] & SCOPING_EL) {
6569                !!!cp ('t379');
6570                last INSCOPE;
6571              }
6572            } # INSCOPE
6573              
6574            $reconstruct_active_formatting_elements->($insert_to_current);
6575              
6576            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6577    
6578            ## TODO: associate with $self->{form_element} if defined
6579    
6580            push @$active_formatting_elements, ['#marker', ''];
6581    
6582            !!!nack ('t379.1');
6583            !!!next-token;
6584            next B;
6585          } elsif ({
6586                    xmp => 1,
6587                    iframe => 1,
6588                    noembed => 1,
6589                    noframes => 1,
6590                    noscript => 0, ## TODO: 1 if scripting is enabled
6591                   }->{$token->{tag_name}}) {
6592            if ($token->{tag_name} eq 'xmp') {
6593              !!!cp ('t381');
6594              $reconstruct_active_formatting_elements->($insert_to_current);
6595            } else {
6596              !!!cp ('t399');
6597            }
6598            ## NOTE: There is an "as if in body" code clone.
6599            $parse_rcdata->(CDATA_CONTENT_MODEL);
6600            next B;
6601          } elsif ($token->{tag_name} eq 'isindex') {
6602            !!!parse-error (type => 'isindex', token => $token);
6603            
6604            if (defined $self->{form_element}) {
6605              !!!cp ('t389');
6606            ## Ignore the token            ## Ignore the token
6607              !!!nack ('t389'); ## NOTE: Not acknowledged.
6608              !!!next-token;
6609              next B;
6610            } else {
6611              my $at = $token->{attributes};
6612              my $form_attrs;
6613              $form_attrs->{action} = $at->{action} if $at->{action};
6614              my $prompt_attr = $at->{prompt};
6615              $at->{name} = {name => 'name', value => 'isindex'};
6616              delete $at->{action};
6617              delete $at->{prompt};
6618              my @tokens = (
6619                            {type => START_TAG_TOKEN, tag_name => 'form',
6620                             attributes => $form_attrs,
6621                             line => $token->{line}, column => $token->{column}},
6622                            {type => START_TAG_TOKEN, tag_name => 'hr',
6623                             line => $token->{line}, column => $token->{column}},
6624                            {type => START_TAG_TOKEN, tag_name => 'p',
6625                             line => $token->{line}, column => $token->{column}},
6626                            {type => START_TAG_TOKEN, tag_name => 'label',
6627                             line => $token->{line}, column => $token->{column}},
6628                           );
6629              if ($prompt_attr) {
6630                !!!cp ('t390');
6631                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6632                               #line => $token->{line}, column => $token->{column},
6633                              };
6634              } else {
6635                !!!cp ('t391');
6636                push @tokens, {type => CHARACTER_TOKEN,
6637                               data => 'This is a searchable index. Insert your search keywords here: ',
6638                               #line => $token->{line}, column => $token->{column},
6639                              }; # SHOULD
6640                ## TODO: make this configurable
6641              }
6642              push @tokens,
6643                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6644                             line => $token->{line}, column => $token->{column}},
6645                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6646                            {type => END_TAG_TOKEN, tag_name => 'label',
6647                             line => $token->{line}, column => $token->{column}},
6648                            {type => END_TAG_TOKEN, tag_name => 'p',
6649                             line => $token->{line}, column => $token->{column}},
6650                            {type => START_TAG_TOKEN, tag_name => 'hr',
6651                             line => $token->{line}, column => $token->{column}},
6652                            {type => END_TAG_TOKEN, tag_name => 'form',
6653                             line => $token->{line}, column => $token->{column}};
6654              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6655              !!!back-token (@tokens);
6656              !!!next-token;
6657              next B;
6658            }
6659          } elsif ($token->{tag_name} eq 'textarea') {
6660            my $tag_name = $token->{tag_name};
6661            my $el;
6662            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6663            
6664            ## TODO: $self->{form_element} if defined
6665            $self->{content_model} = RCDATA_CONTENT_MODEL;
6666            delete $self->{escape}; # MUST
6667            
6668            $insert->($el);
6669            
6670            my $text = '';
6671            !!!nack ('t392.1');
6672            !!!next-token;
6673            if ($token->{type} == CHARACTER_TOKEN) {
6674              $token->{data} =~ s/^\x0A//;
6675              unless (length $token->{data}) {
6676                !!!cp ('t392');
6677                !!!next-token;
6678              } else {
6679                !!!cp ('t393');
6680              }
6681            } else {
6682              !!!cp ('t394');
6683            }
6684            while ($token->{type} == CHARACTER_TOKEN) {
6685              !!!cp ('t395');
6686              $text .= $token->{data};
6687            !!!next-token;            !!!next-token;
6688            redo B;          }
6689            if (length $text) {
6690              !!!cp ('t396');
6691              $el->manakai_append_text ($text);
6692            }
6693            
6694            $self->{content_model} = PCDATA_CONTENT_MODEL;
6695            
6696            if ($token->{type} == END_TAG_TOKEN and
6697                $token->{tag_name} eq $tag_name) {
6698              !!!cp ('t397');
6699              ## Ignore the token
6700            } else {
6701              !!!cp ('t398');
6702              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6703            }
6704            !!!next-token;
6705            next B;
6706          } elsif ($token->{tag_name} eq 'math' or
6707                   $token->{tag_name} eq 'svg') {
6708            $reconstruct_active_formatting_elements->($insert_to_current);
6709    
6710            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6711    
6712            ## ISSUE: An issue in spec there          ## "adjust foreign attributes" - done in insert-element-f
6713            
6714            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6715            
6716            if ($self->{self_closing}) {
6717              pop @{$self->{open_elements}};
6718              !!!ack ('t398.1');
6719          } else {          } else {
6720            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t398.2');
6721              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6722              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6723              ## mode, "in body" (not "in foreign content") secondary insertion
6724              ## mode, maybe.
6725          }          }
6726        }  
6727      } elsif ($phase eq 'trailing end') {          !!!next-token;
6728        ## states in the main stage is preserved yet # MUST          next B;
6729                } elsif ({
6730        if ($token->{type} eq 'DOCTYPE') {                  caption => 1, col => 1, colgroup => 1, frame => 1,
6731          !!!parse-error (type => 'after html:#DOCTYPE');                  frameset => 1, head => 1, option => 1, optgroup => 1,
6732                    tbody => 1, td => 1, tfoot => 1, th => 1,
6733                    thead => 1, tr => 1,
6734                   }->{$token->{tag_name}}) {
6735            !!!cp ('t401');
6736            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6737          ## Ignore the token          ## Ignore the token
6738            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6739          !!!next-token;          !!!next-token;
6740          redo B;          next B;
6741        } elsif ($token->{type} eq 'comment') {          
6742          my $comment = $self->{document}->create_comment ($token->{data});          ## ISSUE: An issue on HTML5 new elements in the spec.
6743          $self->{document}->append_child ($comment);        } else {
6744            if ($token->{tag_name} eq 'image') {
6745              !!!cp ('t384');
6746              !!!parse-error (type => 'image', token => $token);
6747              $token->{tag_name} = 'img';
6748            } else {
6749              !!!cp ('t385');
6750            }
6751    
6752            ## NOTE: There is an "as if <br>" code clone.
6753            $reconstruct_active_formatting_elements->($insert_to_current);
6754            
6755            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6756    
6757            if ({
6758                 applet => 1, marquee => 1, object => 1,
6759                }->{$token->{tag_name}}) {
6760              !!!cp ('t380');
6761              push @$active_formatting_elements, ['#marker', ''];
6762              !!!nack ('t380.1');
6763            } elsif ({
6764                      b => 1, big => 1, em => 1, font => 1, i => 1,
6765                      s => 1, small => 1, strile => 1,
6766                      strong => 1, tt => 1, u => 1,
6767                     }->{$token->{tag_name}}) {
6768              !!!cp ('t375');
6769              push @$active_formatting_elements, $self->{open_elements}->[-1];
6770              !!!nack ('t375.1');
6771            } elsif ($token->{tag_name} eq 'input') {
6772              !!!cp ('t388');
6773              ## TODO: associate with $self->{form_element} if defined
6774              pop @{$self->{open_elements}};
6775              !!!ack ('t388.2');
6776            } elsif ({
6777                      area => 1, basefont => 1, bgsound => 1, br => 1,
6778                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6779                      #image => 1,
6780                     }->{$token->{tag_name}}) {
6781              !!!cp ('t388.1');
6782              pop @{$self->{open_elements}};
6783              !!!ack ('t388.3');
6784            } elsif ($token->{tag_name} eq 'select') {
6785              ## TODO: associate with $self->{form_element} if defined
6786            
6787              if ($self->{insertion_mode} & TABLE_IMS or
6788                  $self->{insertion_mode} & BODY_TABLE_IMS or
6789                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6790                !!!cp ('t400.1');
6791                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6792              } else {
6793                !!!cp ('t400.2');
6794                $self->{insertion_mode} = IN_SELECT_IM;
6795              }
6796              !!!nack ('t400.3');
6797            } else {
6798              !!!nack ('t402');
6799            }
6800            
6801          !!!next-token;          !!!next-token;
6802          redo B;          next B;
6803        } elsif ($token->{type} eq 'character') {        }
6804          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6805            my $data = $1;        if ($token->{tag_name} eq 'body') {
6806            ## As if in the main phase.          ## has a |body| element in scope
6807            ## NOTE: The insertion mode in the main phase          my $i;
6808            ## just before the phase has been changed to the trailing          INSCOPE: {
6809            ## end phase is either "after body" or "after frameset".            for (reverse @{$self->{open_elements}}) {
6810            $reconstruct_active_formatting_elements->($insert_to_current)              if ($_->[1] & BODY_EL) {
6811              if $phase eq 'main';                !!!cp ('t405');
6812                  $i = $_;
6813                  last INSCOPE;
6814                } elsif ($_->[1] & SCOPING_EL) {
6815                  !!!cp ('t405.1');
6816                  last;
6817                }
6818              }
6819    
6820              !!!parse-error (type => 'start tag not allowed',
6821                              value => $token->{tag_name}, token => $token);
6822              ## NOTE: Ignore the token.
6823              !!!next-token;
6824              next B;
6825            } # INSCOPE
6826    
6827            for (@{$self->{open_elements}}) {
6828              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6829                !!!cp ('t403');
6830                !!!parse-error (type => 'not closed',
6831                                value => $_->[0]->manakai_local_name,
6832                                token => $token);
6833                last;
6834              } else {
6835                !!!cp ('t404');
6836              }
6837            }
6838    
6839            $self->{insertion_mode} = AFTER_BODY_IM;
6840            !!!next-token;
6841            next B;
6842          } elsif ($token->{tag_name} eq 'html') {
6843            ## TODO: Update this code.  It seems that the code below is not
6844            ## up-to-date, though it has same effect as speced.
6845            if (@{$self->{open_elements}} > 1 and
6846                $self->{open_elements}->[1]->[1] & BODY_EL) {
6847              ## ISSUE: There is an issue in the spec.
6848              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6849                !!!cp ('t406');
6850                !!!parse-error (type => 'not closed',
6851                                value => $self->{open_elements}->[1]->[0]
6852                                    ->manakai_local_name,
6853                                token => $token);
6854              } else {
6855                !!!cp ('t407');
6856              }
6857              $self->{insertion_mode} = AFTER_BODY_IM;
6858              ## reprocess
6859              next B;
6860            } else {
6861              !!!cp ('t408');
6862              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6863              ## Ignore the token
6864              !!!next-token;
6865              next B;
6866            }
6867          } elsif ({
6868                    address => 1, blockquote => 1, center => 1, dir => 1,
6869                    div => 1, dl => 1, fieldset => 1, listing => 1,
6870                    menu => 1, ol => 1, pre => 1, ul => 1,
6871                    dd => 1, dt => 1, li => 1,
6872                    applet => 1, button => 1, marquee => 1, object => 1,
6873                   }->{$token->{tag_name}}) {
6874            ## has an element in scope
6875            my $i;
6876            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6877              my $node = $self->{open_elements}->[$_];
6878              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6879                !!!cp ('t410');
6880                $i = $_;
6881                last INSCOPE;
6882              } elsif ($node->[1] & SCOPING_EL) {
6883                !!!cp ('t411');
6884                last INSCOPE;
6885              }
6886            } # INSCOPE
6887    
6888            unless (defined $i) { # has an element in scope
6889              !!!cp ('t413');
6890              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6891            } else {
6892              ## Step 1. generate implied end tags
6893              while ({
6894                      dd => ($token->{tag_name} ne 'dd'),
6895                      dt => ($token->{tag_name} ne 'dt'),
6896                      li => ($token->{tag_name} ne 'li'),
6897                      p => 1,
6898                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6899                !!!cp ('t409');
6900                pop @{$self->{open_elements}};
6901              }
6902    
6903              ## Step 2.
6904              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6905                      ne $token->{tag_name}) {
6906                !!!cp ('t412');
6907                !!!parse-error (type => 'not closed',
6908                                value => $self->{open_elements}->[-1]->[0]
6909                                    ->manakai_local_name,
6910                                token => $token);
6911              } else {
6912                !!!cp ('t414');
6913              }
6914    
6915              ## Step 3.
6916              splice @{$self->{open_elements}}, $i;
6917    
6918              ## Step 4.
6919              $clear_up_to_marker->()
6920                  if {
6921                    applet => 1, button => 1, marquee => 1, object => 1,
6922                  }->{$token->{tag_name}};
6923            }
6924            !!!next-token;
6925            next B;
6926          } elsif ($token->{tag_name} eq 'form') {
6927            undef $self->{form_element};
6928    
6929            ## has an element in scope
6930            my $i;
6931            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6932              my $node = $self->{open_elements}->[$_];
6933              if ($node->[1] & FORM_EL) {
6934                !!!cp ('t418');
6935                $i = $_;
6936                last INSCOPE;
6937              } elsif ($node->[1] & SCOPING_EL) {
6938                !!!cp ('t419');
6939                last INSCOPE;
6940              }
6941            } # INSCOPE
6942    
6943            unless (defined $i) { # has an element in scope
6944              !!!cp ('t421');
6945              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6946            } else {
6947              ## Step 1. generate implied end tags
6948              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6949                !!!cp ('t417');
6950                pop @{$self->{open_elements}};
6951              }
6952                        
6953            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
6954              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6955                      ne $token->{tag_name}) {
6956                !!!cp ('t417.1');
6957                !!!parse-error (type => 'not closed',
6958                                value => $self->{open_elements}->[-1]->[0]
6959                                    ->manakai_local_name,
6960                                token => $token);
6961              } else {
6962                !!!cp ('t420');
6963              }  
6964                        
6965            unless (length $token->{data}) {            ## Step 3.
6966              !!!next-token;            splice @{$self->{open_elements}}, $i;
6967              redo B;          }
6968    
6969            !!!next-token;
6970            next B;
6971          } elsif ({
6972                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6973                   }->{$token->{tag_name}}) {
6974            ## has an element in scope
6975            my $i;
6976            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6977              my $node = $self->{open_elements}->[$_];
6978              if ($node->[1] & HEADING_EL) {
6979                !!!cp ('t423');
6980                $i = $_;
6981                last INSCOPE;
6982              } elsif ($node->[1] & SCOPING_EL) {
6983                !!!cp ('t424');
6984                last INSCOPE;
6985              }
6986            } # INSCOPE
6987    
6988            unless (defined $i) { # has an element in scope
6989              !!!cp ('t425.1');
6990              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6991            } else {
6992              ## Step 1. generate implied end tags
6993              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6994                !!!cp ('t422');
6995                pop @{$self->{open_elements}};
6996              }
6997              
6998              ## Step 2.
6999              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7000                      ne $token->{tag_name}) {
7001                !!!cp ('t425');
7002                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7003              } else {
7004                !!!cp ('t426');
7005            }            }
7006    
7007              ## Step 3.
7008              splice @{$self->{open_elements}}, $i;
7009          }          }
7010            
7011            !!!next-token;
7012            next B;
7013          } elsif ($token->{tag_name} eq 'p') {
7014            ## has an element in scope
7015            my $i;
7016            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7017              my $node = $self->{open_elements}->[$_];
7018              if ($node->[1] & P_EL) {
7019                !!!cp ('t410.1');
7020                $i = $_;
7021                last INSCOPE;
7022              } elsif ($node->[1] & SCOPING_EL) {
7023                !!!cp ('t411.1');
7024                last INSCOPE;
7025              }
7026            } # INSCOPE
7027    
7028          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7029          $phase = 'main';            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7030          ## reprocess                    ne $token->{tag_name}) {
7031          redo B;              !!!cp ('t412.1');
7032        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
7033                 $token->{type} eq 'end tag') {                              value => $self->{open_elements}->[-1]->[0]
7034          !!!parse-error (type => 'after html:'.$token->{tag_name});                                  ->manakai_local_name,
7035          $phase = 'main';                              token => $token);
7036          ## reprocess            } else {
7037          redo B;              !!!cp ('t414.1');
7038        } elsif ($token->{type} eq 'end-of-file') {            }
7039          ## Stop parsing  
7040          last B;            splice @{$self->{open_elements}}, $i;
7041            } else {
7042              !!!cp ('t413.1');
7043              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7044    
7045              !!!cp ('t415.1');
7046              ## As if <p>, then reprocess the current token
7047              my $el;
7048              !!!create-element ($el, $HTML_NS, 'p',, $token);
7049              $insert->($el);
7050              ## NOTE: Not inserted into |$self->{open_elements}|.
7051            }
7052    
7053            !!!next-token;
7054            next B;
7055          } elsif ({
7056                    a => 1,
7057                    b => 1, big => 1, em => 1, font => 1, i => 1,
7058                    nobr => 1, s => 1, small => 1, strile => 1,
7059                    strong => 1, tt => 1, u => 1,
7060                   }->{$token->{tag_name}}) {
7061            !!!cp ('t427');
7062            $formatting_end_tag->($token);
7063            next B;
7064          } elsif ($token->{tag_name} eq 'br') {
7065            !!!cp ('t428');
7066            !!!parse-error (type => 'unmatched end tag:br', token => $token);
7067    
7068            ## As if <br>
7069            $reconstruct_active_formatting_elements->($insert_to_current);
7070            
7071            my $el;
7072            !!!create-element ($el, $HTML_NS, 'br',, $token);
7073            $insert->($el);
7074            
7075            ## Ignore the token.
7076            !!!next-token;
7077            next B;
7078          } elsif ({
7079                    caption => 1, col => 1, colgroup => 1, frame => 1,
7080                    frameset => 1, head => 1, option => 1, optgroup => 1,
7081                    tbody => 1, td => 1, tfoot => 1, th => 1,
7082                    thead => 1, tr => 1,
7083                    area => 1, basefont => 1, bgsound => 1,
7084                    embed => 1, hr => 1, iframe => 1, image => 1,
7085                    img => 1, input => 1, isindex => 1, noembed => 1,
7086                    noframes => 1, param => 1, select => 1, spacer => 1,
7087                    table => 1, textarea => 1, wbr => 1,
7088                    noscript => 0, ## TODO: if scripting is enabled
7089                   }->{$token->{tag_name}}) {
7090            !!!cp ('t429');
7091            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7092            ## Ignore the token
7093            !!!next-token;
7094            next B;
7095            
7096            ## ISSUE: Issue on HTML5 new elements in spec
7097            
7098        } else {        } else {
7099          die "$0: $token->{type}: Unknown token";          ## Step 1
7100            my $node_i = -1;
7101            my $node = $self->{open_elements}->[$node_i];
7102    
7103            ## Step 2
7104            S2: {
7105              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7106                ## Step 1
7107                ## generate implied end tags
7108                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7109                  !!!cp ('t430');
7110                  ## ISSUE: Can this case be reached?
7111                  pop @{$self->{open_elements}};
7112                }
7113            
7114                ## Step 2
7115                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7116                        ne $token->{tag_name}) {
7117                  !!!cp ('t431');
7118                  ## NOTE: <x><y></x>
7119                  !!!parse-error (type => 'not closed',
7120                                  value => $self->{open_elements}->[-1]->[0]
7121                                      ->manakai_local_name,
7122                                  token => $token);
7123                } else {
7124                  !!!cp ('t432');
7125                }
7126                
7127                ## Step 3
7128                splice @{$self->{open_elements}}, $node_i;
7129    
7130                !!!next-token;
7131                last S2;
7132              } else {
7133                ## Step 3
7134                if (not ($node->[1] & FORMATTING_EL) and
7135                    #not $phrasing_category->{$node->[1]} and
7136                    ($node->[1] & SPECIAL_EL or
7137                     $node->[1] & SCOPING_EL)) {
7138                  !!!cp ('t433');
7139                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7140                  ## Ignore the token
7141                  !!!next-token;
7142                  last S2;
7143                }
7144    
7145                !!!cp ('t434');
7146              }
7147              
7148              ## Step 4
7149              $node_i--;
7150              $node = $self->{open_elements}->[$node_i];
7151              
7152              ## Step 5;
7153              redo S2;
7154            } # S2
7155            next B;
7156        }        }
7157      }      }
7158        next B;
7159      } continue { # B
7160        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7161          ## NOTE: The code below is executed in cases where it does not have
7162          ## to be, but it it is harmless even in those cases.
7163          ## has an element in scope
7164          INSCOPE: {
7165            for (reverse 0..$#{$self->{open_elements}}) {
7166              my $node = $self->{open_elements}->[$_];
7167              if ($node->[1] & FOREIGN_EL) {
7168                last INSCOPE;
7169              } elsif ($node->[1] & SCOPING_EL) {
7170                last;
7171              }
7172            }
7173            
7174            ## NOTE: No foreign element in scope.
7175            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7176          } # INSCOPE
7177        }
7178    } # B    } # B
7179    
7180    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4771  sub set_inner_html ($$$) { Line 7188  sub set_inner_html ($$$) {
7188    my $s = \$_[0];    my $s = \$_[0];
7189    my $onerror = $_[1];    my $onerror = $_[1];
7190    
7191      ## ISSUE: Should {confident} be true?
7192    
7193    my $nt = $node->node_type;    my $nt = $node->node_type;
7194    if ($nt == 9) {    if ($nt == 9) {
7195      # MUST      # MUST
# Line 4793  sub set_inner_html ($$$) { Line 7212  sub set_inner_html ($$$) {
7212      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7213    
7214      ## Step 1 # MUST      ## Step 1 # MUST
7215      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
7216      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
7217        $doc->manakai_is_html (1);
7218      my $p = $class->new;      my $p = $class->new;
7219      $p->{document} = $doc;      $p->{document} = $doc;
7220    
7221      ## Step 9 # MUST      ## Step 8 # MUST
7222      my $i = 0;      my $i = 0;
7223      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7224      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7225      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7226        my $self = shift;        my $self = shift;
7227        $self->{next_input_character} = -1 and return if $i >= length $$s;  
7228        $self->{next_input_character} = ord substr $$s, $i++, 1;        pop @{$self->{prev_char}};
7229        $column++;        unshift @{$self->{prev_char}}, $self->{next_char};
7230          
7231        if ($self->{next_input_character} == 0x000D) { # CR        $self->{next_char} = -1 and return if $i >= length $$s;
7232          if ($i >= length $$s) {        $self->{next_char} = ord substr $$s, $i++, 1;
7233            #  
7234          } else {        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7235            my $next_char = ord substr $$s, $i++, 1;        $p->{column}++;
7236            if ($next_char == 0x000A) { # LF  
7237              #        if ($self->{next_char} == 0x000A) { # LF
7238            } else {          $p->{line}++;
7239              push @{$self->{char}}, $next_char;          $p->{column} = 0;
7240            }          !!!cp ('i1');
7241          }        } elsif ($self->{next_char} == 0x000D) { # CR
7242          $self->{next_input_character} = 0x000A; # LF # MUST          $i++ if substr ($$s, $i, 1) eq "\x0A";
7243          $line++;          $self->{next_char} = 0x000A; # LF # MUST
7244          $column = -1;          $p->{line}++;
7245        } elsif ($self->{next_input_character} > 0x10FFFF) {          $p->{column} = 0;
7246          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          !!!cp ('i2');
7247        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_char} > 0x10FFFF) {
7248          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7249            !!!cp ('i3');
7250          } elsif ($self->{next_char} == 0x0000) { # NULL
7251            !!!cp ('i4');
7252            !!!parse-error (type => 'NULL');
7253            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7254          } elsif ($self->{next_char} <= 0x0008 or
7255                   (0x000E <= $self->{next_char} and
7256                    $self->{next_char} <= 0x001F) or
7257                   (0x007F <= $self->{next_char} and
7258                    $self->{next_char} <= 0x009F) or
7259                   (0xD800 <= $self->{next_char} and
7260                    $self->{next_char} <= 0xDFFF) or
7261                   (0xFDD0 <= $self->{next_char} and
7262                    $self->{next_char} <= 0xFDDF) or
7263                   {
7264                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7265                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7266                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7267                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7268                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7269                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7270                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7271                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7272                    0x10FFFE => 1, 0x10FFFF => 1,
7273                   }->{$self->{next_char}}) {
7274            !!!cp ('i4.1');
7275            !!!parse-error (type => 'control char', level => $self->{must_level});
7276    ## TODO: error type documentation
7277        }        }
7278      };      };
7279        $p->{prev_char} = [-1, -1, -1];
7280        $p->{next_char} = -1;
7281            
7282      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7283        my (%opt) = @_;        my (%opt) = @_;
7284        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7285          my $column = $opt{column};
7286          if (defined $opt{token} and defined $opt{token}->{line}) {
7287            $line = $opt{token}->{line};
7288            $column = $opt{token}->{column};
7289          }
7290          warn "Parse error ($opt{type}) at line $line column $column\n";
7291      };      };
7292      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7293        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7294      };      };
7295            
7296      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7297      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7298    
7299      ## Step 2      ## Step 2
7300      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7301      $p->{content_model_flag} = {      $p->{content_model} = {
7302        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7303        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7304        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7305        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7306        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7307        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7308        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7309        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7310        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7311        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7312      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7313         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7314            unless defined $p->{content_model};
7315            ## ISSUE: What is "the name of the element"? local name?
7316    
7317      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7318          ## TODO: Foreign element OK?
7319    
7320      ## Step 4      ## Step 3
7321      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7322        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7323    
7324      ## Step 5 # MUST      ## Step 4 # MUST
7325      $doc->append_child ($root);      $doc->append_child ($root);
7326    
7327      ## Step 6 # MUST      ## Step 5 # MUST
7328      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7329    
7330      undef $p->{head_element};      undef $p->{head_element};
7331    
7332      ## Step 7 # MUST      ## Step 6 # MUST
7333      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7334    
7335      ## Step 8 # MUST      ## Step 7 # MUST
7336      my $anode = $node;      my $anode = $node;
7337      AN: while (defined $anode) {      AN: while (defined $anode) {
7338        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7339          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7340          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7341            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7342                !!!cp ('i5');
7343              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7344              last AN;              last AN;
7345            }            }
# Line 4888  sub set_inner_html ($$$) { Line 7348  sub set_inner_html ($$$) {
7348        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7349      } # AN      } # AN
7350            
7351      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7352      {      {
7353        my $self = $p;        my $self = $p;
7354        !!!next-token;        !!!next-token;
7355      }      }
7356      $p->_tree_construction_main;      $p->_tree_construction_main;
7357    
7358      ## Step 11 # MUST      ## Step 10 # MUST
7359      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7360      for (@cn) {      for (@cn) {
7361        $node->remove_child ($_);        $node->remove_child ($_);
7362      }      }
7363      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7364    
7365      ## Step 12 # MUST      ## Step 11 # MUST
7366      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7367      for (@cn) {      for (@cn) {
7368          $this_doc->adopt_node ($_);
7369        $node->append_child ($_);        $node->append_child ($_);
7370      }      }
7371      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
7372    
7373      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7374    
7375        delete $p->{parse_error}; # delete loop
7376    } else {    } else {
7377      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";
7378    }    }
# Line 4918  sub set_inner_html ($$$) { Line 7380  sub set_inner_html ($$$) {
7380    
7381  } # tree construction stage  } # tree construction stage
7382    
7383  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7384    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  
7385    
7386  1;  1;
7387  # $Date$  # $Date$

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24