/[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.153 by wakaba, Fri Aug 15 08:32:41 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## This is an early version of an HTML parser.  ## ISSUE:
7    ## var doc = implementation.createDocument (null, null, null);
8  my $permitted_slash_tag_name = {  ## doc.write ('');
9    base => 1,  ## alert (doc.compatMode);
10    link => 1,  
11    meta => 1,  require IO::Handle;
12    hr => 1,  
13    br => 1,  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    img=> 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15    embed => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    param => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    area => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    col => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    input => 1,  
20    sub A_EL () { 0b1 }
21    sub ADDRESS_EL () { 0b10 }
22    sub BODY_EL () { 0b100 }
23    sub BUTTON_EL () { 0b1000 }
24    sub CAPTION_EL () { 0b10000 }
25    sub DD_EL () { 0b100000 }
26    sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214  };  };
215    
216  my $entity_char = {  my $el_category_f = {
217    AElig => "\x{00C6}",    $MML_NS => {
218    Aacute => "\x{00C1}",      'annotation-xml' => MML_AXML_EL,
219    Acirc => "\x{00C2}",      mi => FOREIGN_FLOW_CONTENT_EL,
220    Agrave => "\x{00C0}",      mo => FOREIGN_FLOW_CONTENT_EL,
221    Alpha => "\x{0391}",      mn => FOREIGN_FLOW_CONTENT_EL,
222    Aring => "\x{00C5}",      ms => FOREIGN_FLOW_CONTENT_EL,
223    Atilde => "\x{00C3}",      mtext => FOREIGN_FLOW_CONTENT_EL,
224    Auml => "\x{00C4}",    },
225    Beta => "\x{0392}",    $SVG_NS => {
226    Ccedil => "\x{00C7}",      foreignObject => FOREIGN_FLOW_CONTENT_EL,
227    Chi => "\x{03A7}",      desc => FOREIGN_FLOW_CONTENT_EL,
228    Dagger => "\x{2021}",      title => FOREIGN_FLOW_CONTENT_EL,
229    Delta => "\x{0394}",    },
230    ETH => "\x{00D0}",    ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
   Eacute => "\x{00C9}",  
   Ecirc => "\x{00CA}",  
   Egrave => "\x{00C8}",  
   Epsilon => "\x{0395}",  
   Eta => "\x{0397}",  
   Euml => "\x{00CB}",  
   Gamma => "\x{0393}",  
   Iacute => "\x{00CD}",  
   Icirc => "\x{00CE}",  
   Igrave => "\x{00CC}",  
   Iota => "\x{0399}",  
   Iuml => "\x{00CF}",  
   Kappa => "\x{039A}",  
   Lambda => "\x{039B}",  
   Mu => "\x{039C}",  
   Ntilde => "\x{00D1}",  
   Nu => "\x{039D}",  
   OElig => "\x{0152}",  
   Oacute => "\x{00D3}",  
   Ocirc => "\x{00D4}",  
   Ograve => "\x{00D2}",  
   Omega => "\x{03A9}",  
   Omicron => "\x{039F}",  
   Oslash => "\x{00D8}",  
   Otilde => "\x{00D5}",  
   Ouml => "\x{00D6}",  
   Phi => "\x{03A6}",  
   Pi => "\x{03A0}",  
   Prime => "\x{2033}",  
   Psi => "\x{03A8}",  
   Rho => "\x{03A1}",  
   Scaron => "\x{0160}",  
   Sigma => "\x{03A3}",  
   THORN => "\x{00DE}",  
   Tau => "\x{03A4}",  
   Theta => "\x{0398}",  
   Uacute => "\x{00DA}",  
   Ucirc => "\x{00DB}",  
   Ugrave => "\x{00D9}",  
   Upsilon => "\x{03A5}",  
   Uuml => "\x{00DC}",  
   Xi => "\x{039E}",  
   Yacute => "\x{00DD}",  
   Yuml => "\x{0178}",  
   Zeta => "\x{0396}",  
   aacute => "\x{00E1}",  
   acirc => "\x{00E2}",  
   acute => "\x{00B4}",  
   aelig => "\x{00E6}",  
   agrave => "\x{00E0}",  
   alefsym => "\x{2135}",  
   alpha => "\x{03B1}",  
   amp => "\x{0026}",  
   AMP => "\x{0026}",  
   and => "\x{2227}",  
   ang => "\x{2220}",  
   apos => "\x{0027}",  
   aring => "\x{00E5}",  
   asymp => "\x{2248}",  
   atilde => "\x{00E3}",  
   auml => "\x{00E4}",  
   bdquo => "\x{201E}",  
   beta => "\x{03B2}",  
   brvbar => "\x{00A6}",  
   bull => "\x{2022}",  
   cap => "\x{2229}",  
   ccedil => "\x{00E7}",  
   cedil => "\x{00B8}",  
   cent => "\x{00A2}",  
   chi => "\x{03C7}",  
   circ => "\x{02C6}",  
   clubs => "\x{2663}",  
   cong => "\x{2245}",  
   copy => "\x{00A9}",  
   COPY => "\x{00A9}",  
   crarr => "\x{21B5}",  
   cup => "\x{222A}",  
   curren => "\x{00A4}",  
   dArr => "\x{21D3}",  
   dagger => "\x{2020}",  
   darr => "\x{2193}",  
   deg => "\x{00B0}",  
   delta => "\x{03B4}",  
   diams => "\x{2666}",  
   divide => "\x{00F7}",  
   eacute => "\x{00E9}",  
   ecirc => "\x{00EA}",  
   egrave => "\x{00E8}",  
   empty => "\x{2205}",  
   emsp => "\x{2003}",  
   ensp => "\x{2002}",  
   epsilon => "\x{03B5}",  
   equiv => "\x{2261}",  
   eta => "\x{03B7}",  
   eth => "\x{00F0}",  
   euml => "\x{00EB}",  
   euro => "\x{20AC}",  
   exist => "\x{2203}",  
   fnof => "\x{0192}",  
   forall => "\x{2200}",  
   frac12 => "\x{00BD}",  
   frac14 => "\x{00BC}",  
   frac34 => "\x{00BE}",  
   frasl => "\x{2044}",  
   gamma => "\x{03B3}",  
   ge => "\x{2265}",  
   gt => "\x{003E}",  
   GT => "\x{003E}",  
   hArr => "\x{21D4}",  
   harr => "\x{2194}",  
   hearts => "\x{2665}",  
   hellip => "\x{2026}",  
   iacute => "\x{00ED}",  
   icirc => "\x{00EE}",  
   iexcl => "\x{00A1}",  
   igrave => "\x{00EC}",  
   image => "\x{2111}",  
   infin => "\x{221E}",  
   int => "\x{222B}",  
   iota => "\x{03B9}",  
   iquest => "\x{00BF}",  
   isin => "\x{2208}",  
   iuml => "\x{00EF}",  
   kappa => "\x{03BA}",  
   lArr => "\x{21D0}",  
   lambda => "\x{03BB}",  
   lang => "\x{2329}",  
   laquo => "\x{00AB}",  
   larr => "\x{2190}",  
   lceil => "\x{2308}",  
   ldquo => "\x{201C}",  
   le => "\x{2264}",  
   lfloor => "\x{230A}",  
   lowast => "\x{2217}",  
   loz => "\x{25CA}",  
   lrm => "\x{200E}",  
   lsaquo => "\x{2039}",  
   lsquo => "\x{2018}",  
   lt => "\x{003C}",  
   LT => "\x{003C}",  
   macr => "\x{00AF}",  
   mdash => "\x{2014}",  
   micro => "\x{00B5}",  
   middot => "\x{00B7}",  
   minus => "\x{2212}",  
   mu => "\x{03BC}",  
   nabla => "\x{2207}",  
   nbsp => "\x{00A0}",  
   ndash => "\x{2013}",  
   ne => "\x{2260}",  
   ni => "\x{220B}",  
   not => "\x{00AC}",  
   notin => "\x{2209}",  
   nsub => "\x{2284}",  
   ntilde => "\x{00F1}",  
   nu => "\x{03BD}",  
   oacute => "\x{00F3}",  
   ocirc => "\x{00F4}",  
   oelig => "\x{0153}",  
   ograve => "\x{00F2}",  
   oline => "\x{203E}",  
   omega => "\x{03C9}",  
   omicron => "\x{03BF}",  
   oplus => "\x{2295}",  
   or => "\x{2228}",  
   ordf => "\x{00AA}",  
   ordm => "\x{00BA}",  
   oslash => "\x{00F8}",  
   otilde => "\x{00F5}",  
   otimes => "\x{2297}",  
   ouml => "\x{00F6}",  
   para => "\x{00B6}",  
   part => "\x{2202}",  
   permil => "\x{2030}",  
   perp => "\x{22A5}",  
   phi => "\x{03C6}",  
   pi => "\x{03C0}",  
   piv => "\x{03D6}",  
   plusmn => "\x{00B1}",  
   pound => "\x{00A3}",  
   prime => "\x{2032}",  
   prod => "\x{220F}",  
   prop => "\x{221D}",  
   psi => "\x{03C8}",  
   quot => "\x{0022}",  
   QUOT => "\x{0022}",  
   rArr => "\x{21D2}",  
   radic => "\x{221A}",  
   rang => "\x{232A}",  
   raquo => "\x{00BB}",  
   rarr => "\x{2192}",  
   rceil => "\x{2309}",  
   rdquo => "\x{201D}",  
   real => "\x{211C}",  
   reg => "\x{00AE}",  
   REG => "\x{00AE}",  
   rfloor => "\x{230B}",  
   rho => "\x{03C1}",  
   rlm => "\x{200F}",  
   rsaquo => "\x{203A}",  
   rsquo => "\x{2019}",  
   sbquo => "\x{201A}",  
   scaron => "\x{0161}",  
   sdot => "\x{22C5}",  
   sect => "\x{00A7}",  
   shy => "\x{00AD}",  
   sigma => "\x{03C3}",  
   sigmaf => "\x{03C2}",  
   sim => "\x{223C}",  
   spades => "\x{2660}",  
   sub => "\x{2282}",  
   sube => "\x{2286}",  
   sum => "\x{2211}",  
   sup => "\x{2283}",  
   sup1 => "\x{00B9}",  
   sup2 => "\x{00B2}",  
   sup3 => "\x{00B3}",  
   supe => "\x{2287}",  
   szlig => "\x{00DF}",  
   tau => "\x{03C4}",  
   there4 => "\x{2234}",  
   theta => "\x{03B8}",  
   thetasym => "\x{03D1}",  
   thinsp => "\x{2009}",  
   thorn => "\x{00FE}",  
   tilde => "\x{02DC}",  
   times => "\x{00D7}",  
   trade => "\x{2122}",  
   uArr => "\x{21D1}",  
   uacute => "\x{00FA}",  
   uarr => "\x{2191}",  
   ucirc => "\x{00FB}",  
   ugrave => "\x{00F9}",  
   uml => "\x{00A8}",  
   upsih => "\x{03D2}",  
   upsilon => "\x{03C5}",  
   uuml => "\x{00FC}",  
   weierp => "\x{2118}",  
   xi => "\x{03BE}",  
   yacute => "\x{00FD}",  
   yen => "\x{00A5}",  
   yuml => "\x{00FF}",  
   zeta => "\x{03B6}",  
   zwj => "\x{200D}",  
   zwnj => "\x{200C}",  
231  };  };
232    
233  my $special_category = {  my $svg_attr_name = {
234    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    attributename => 'attributeName',
235    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    attributetype => 'attributeType',
236    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    basefrequency => 'baseFrequency',
237    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    baseprofile => 'baseProfile',
238    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,    calcmode => 'calcMode',
239    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,    clippathunits => 'clipPathUnits',
240    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,    contentscripttype => 'contentScriptType',
241    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    contentstyletype => 'contentStyleType',
242    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    diffuseconstant => 'diffuseConstant',
243    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    edgemode => 'edgeMode',
244  };    externalresourcesrequired => 'externalResourcesRequired',
245  my $scoping_category = {    filterres => 'filterRes',
246    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    filterunits => 'filterUnits',
247    table => 1, td => 1, th => 1,    glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296  };  };
297  my $formatting_category = {  
298    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  my $foreign_attr_xname = {
299    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,    'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311  };  };
 # $phrasing_category: all other elements  
312    
313  sub parse_string ($$$;$) {  ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    my $self = shift->new;  
315    my $s = \$_[0];  my $c1_entity_char = {
316      0x80 => 0x20AC,
317      0x81 => 0xFFFD,
318      0x82 => 0x201A,
319      0x83 => 0x0192,
320      0x84 => 0x201E,
321      0x85 => 0x2026,
322      0x86 => 0x2020,
323      0x87 => 0x2021,
324      0x88 => 0x02C6,
325      0x89 => 0x2030,
326      0x8A => 0x0160,
327      0x8B => 0x2039,
328      0x8C => 0x0152,
329      0x8D => 0xFFFD,
330      0x8E => 0x017D,
331      0x8F => 0xFFFD,
332      0x90 => 0xFFFD,
333      0x91 => 0x2018,
334      0x92 => 0x2019,
335      0x93 => 0x201C,
336      0x94 => 0x201D,
337      0x95 => 0x2022,
338      0x96 => 0x2013,
339      0x97 => 0x2014,
340      0x98 => 0x02DC,
341      0x99 => 0x2122,
342      0x9A => 0x0161,
343      0x9B => 0x203A,
344      0x9C => 0x0153,
345      0x9D => 0xFFFD,
346      0x9E => 0x017E,
347      0x9F => 0x0178,
348    }; # $c1_entity_char
349    
350    sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$) {
358      my $self = ref $_[0] ? shift : shift->new;
359      my $charset_name = shift;
360      my $byte_stream = $_[0];
361    
362      my $onerror = $_[2] || sub {
363        my (%opt) = @_;
364        warn "Parse error ($opt{type})\n";
365      };
366      $self->{parse_error} = $onerror; # updated later by parse_char_string
367    
368      ## HTML5 encoding sniffing algorithm
369      require Message::Charset::Info;
370      my $charset;
371      my $buffer;
372      my ($char_stream, $e_status);
373    
374      SNIFFING: {
375    
376        ## Step 1
377        if (defined $charset_name) {
378          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
379    
380          ## ISSUE: Unsupported encoding is not ignored according to the spec.
381          ($char_stream, $e_status) = $charset->get_decode_handle
382              ($byte_stream, allow_error_reporting => 1,
383               allow_fallback => 1);
384          if ($char_stream) {
385            $self->{confident} = 1;
386            last SNIFFING;
387          } else {
388            ## TODO: unsupported error
389          }
390        }
391    
392        ## Step 2
393        my $byte_buffer = '';
394        for (1..1024) {
395          my $char = $byte_stream->getc;
396          last unless defined $char;
397          $byte_buffer .= $char;
398        } ## TODO: timeout
399    
400        ## Step 3
401        if ($byte_buffer =~ /^\xFE\xFF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
403          ($char_stream, $e_status) = $charset->get_decode_handle
404              ($byte_stream, allow_error_reporting => 1,
405               allow_fallback => 1, byte_buffer => \$byte_buffer);
406          $self->{confident} = 1;
407          last SNIFFING;
408        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
409          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
410          ($char_stream, $e_status) = $charset->get_decode_handle
411              ($byte_stream, allow_error_reporting => 1,
412               allow_fallback => 1, byte_buffer => \$byte_buffer);
413          $self->{confident} = 1;
414          last SNIFFING;
415        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
416          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
417          ($char_stream, $e_status) = $charset->get_decode_handle
418              ($byte_stream, allow_error_reporting => 1,
419               allow_fallback => 1, byte_buffer => \$byte_buffer);
420          $self->{confident} = 1;
421          last SNIFFING;
422        }
423    
424        ## Step 4
425        ## TODO: <meta charset>
426    
427        ## Step 5
428        ## TODO: from history
429    
430        ## Step 6
431        require Whatpm::Charset::UniversalCharDet;
432        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
433            ($byte_buffer);
434        if (defined $charset_name) {
435          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
436    
437          ## ISSUE: Unsupported encoding is not ignored according to the spec.
438          require Whatpm::Charset::DecodeHandle;
439          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
440              ($byte_stream);
441          ($char_stream, $e_status) = $charset->get_decode_handle
442              ($buffer, allow_error_reporting => 1,
443               allow_fallback => 1, byte_buffer => \$byte_buffer);
444          if ($char_stream) {
445            $buffer->{buffer} = $byte_buffer;
446            !!!parse-error (type => 'sniffing:chardet',
447                            text => $charset_name,
448                            level => $self->{level}->{info},
449                            layer => 'encode',
450                            line => 1, column => 1);
451            $self->{confident} = 0;
452            last SNIFFING;
453          }
454        }
455    
456        ## Step 7: default
457        ## TODO: Make this configurable.
458        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
459            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
460            ## detectable in the step 6.
461        require Whatpm::Charset::DecodeHandle;
462        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
463            ($byte_stream);
464        ($char_stream, $e_status)
465            = $charset->get_decode_handle ($buffer,
466                                           allow_error_reporting => 1,
467                                           allow_fallback => 1,
468                                           byte_buffer => \$byte_buffer);
469        $buffer->{buffer} = $byte_buffer;
470        !!!parse-error (type => 'sniffing:default',
471                        text => 'windows-1252',
472                        level => $self->{level}->{info},
473                        line => 1, column => 1,
474                        layer => 'encode');
475        $self->{confident} = 0;
476      } # SNIFFING
477    
478      $self->{input_encoding} = $charset->get_iana_name;
479      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
480        !!!parse-error (type => 'chardecode:fallback',
481                        text => $self->{input_encoding},
482                        level => $self->{level}->{uncertain},
483                        line => 1, column => 1,
484                        layer => 'encode');
485      } elsif (not ($e_status &
486                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
487        !!!parse-error (type => 'chardecode:no error',
488                        text => $self->{input_encoding},
489                        level => $self->{level}->{uncertain},
490                        line => 1, column => 1,
491                        layer => 'encode');
492      }
493    
494      $self->{change_encoding} = sub {
495        my $self = shift;
496        $charset_name = shift;
497        my $token = shift;
498    
499        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
500        ($char_stream, $e_status) = $charset->get_decode_handle
501            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
502             byte_buffer => \ $buffer->{buffer});
503        
504        if ($char_stream) { # if supported
505          ## "Change the encoding" algorithm:
506    
507          ## Step 1    
508          if ($charset->{category} &
509              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
510            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
511            ($char_stream, $e_status) = $charset->get_decode_handle
512                ($byte_stream,
513                 byte_buffer => \ $buffer->{buffer});
514          }
515          $charset_name = $charset->get_iana_name;
516          
517          ## Step 2
518          if (defined $self->{input_encoding} and
519              $self->{input_encoding} eq $charset_name) {
520            !!!parse-error (type => 'charset label:matching',
521                            text => $charset_name,
522                            level => $self->{level}->{info});
523            $self->{confident} = 1;
524            return;
525          }
526    
527          !!!parse-error (type => 'charset label detected',
528                          text => $self->{input_encoding},
529                          value => $charset_name,
530                          level => $self->{level}->{warn},
531                          token => $token);
532          
533          ## Step 3
534          # if (can) {
535            ## change the encoding on the fly.
536            #$self->{confident} = 1;
537            #return;
538          # }
539          
540          ## Step 4
541          throw Whatpm::HTML::RestartParser ();
542        }
543      }; # $self->{change_encoding}
544    
545      my $char_onerror = sub {
546        my (undef, $type, %opt) = @_;
547        !!!parse-error (layer => 'encode',
548                        %opt, type => $type,
549                        line => $self->{line}, column => $self->{column} + 1);
550        if ($opt{octets}) {
551          ${$opt{octets}} = "\x{FFFD}"; # relacement character
552        }
553      };
554      $char_stream->onerror ($char_onerror);
555    
556      my @args = @_; shift @args; # $s
557      my $return;
558      try {
559        $return = $self->parse_char_stream ($char_stream, @args);  
560      } catch Whatpm::HTML::RestartParser with {
561        ## NOTE: Invoked after {change_encoding}.
562    
563        $self->{input_encoding} = $charset->get_iana_name;
564        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
565          !!!parse-error (type => 'chardecode:fallback',
566                          text => $self->{input_encoding},
567                          level => $self->{level}->{uncertain},
568                          line => 1, column => 1,
569                          layer => 'encode');
570        } elsif (not ($e_status &
571                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
572          !!!parse-error (type => 'chardecode:no error',
573                          text => $self->{input_encoding},
574                          level => $self->{level}->{uncertain},
575                          line => 1, column => 1,
576                          layer => 'encode');
577        }
578        $self->{confident} = 1;
579        $char_stream->onerror ($char_onerror);
580        $return = $self->parse_char_stream ($char_stream, @args);
581      };
582      return $return;
583    } # parse_byte_stream
584    
585    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
586    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
587    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
588    ## because the core part of our HTML parser expects a string of character,
589    ## not a string of bytes or code units or anything which might contain a BOM.
590    ## Therefore, any parser interface that accepts a string of bytes,
591    ## such as |parse_byte_string| in this module, must ensure that it does
592    ## strip the BOM and never strip any ZWNBSP.
593    
594    sub parse_char_string ($$$;$) {
595      my $self = shift;
596      require utf8;
597      my $s = ref $_[0] ? $_[0] : \($_[0]);
598      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
599      return $self->parse_char_stream ($input, @_[1..$#_]);
600    } # parse_char_string
601    *parse_string = \&parse_char_string;
602    
603    sub parse_char_stream ($$$;$) {
604      my $self = ref $_[0] ? shift : shift->new;
605      my $input = $_[0];
606    $self->{document} = $_[1];    $self->{document} = $_[1];
607      @{$self->{document}->child_nodes} = ();
608    
609    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
610    
611      $self->{confident} = 1 unless exists $self->{confident};
612      $self->{document}->input_encoding ($self->{input_encoding})
613          if defined $self->{input_encoding};
614    
615    my $i = 0;    my $i = 0;
616    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
617    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
618    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
619      my $self = shift;      my $self = shift;
620      $self->{next_input_character} = -1 and return if $i >= length $$s;  
621      $self->{next_input_character} = ord substr $$s, $i++, 1;      pop @{$self->{prev_char}};
622      $column++;      unshift @{$self->{prev_char}}, $self->{next_char};
623    
624        my $char;
625        if (defined $self->{next_next_char}) {
626          $char = $self->{next_next_char};
627          delete $self->{next_next_char};
628        } else {
629          $char = $input->getc;
630        }
631        $self->{next_char} = -1 and return unless defined $char;
632        $self->{next_char} = ord $char;
633    
634        ($self->{line_prev}, $self->{column_prev})
635            = ($self->{line}, $self->{column});
636        $self->{column}++;
637            
638      if ($self->{next_input_character} == 0x000D) { # CR      if ($self->{next_char} == 0x000A) { # LF
639        if ($i >= length $$s) {        !!!cp ('j1');
640          #        $self->{line}++;
641          $self->{column} = 0;
642        } elsif ($self->{next_char} == 0x000D) { # CR
643          !!!cp ('j2');
644          my $next = $input->getc;
645          if (defined $next and $next ne "\x0A") {
646            $self->{next_next_char} = $next;
647          }
648          $self->{next_char} = 0x000A; # LF # MUST
649          $self->{line}++;
650          $self->{column} = 0;
651        } elsif ($self->{next_char} > 0x10FFFF) {
652          !!!cp ('j3');
653          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
654        } elsif ($self->{next_char} == 0x0000) { # NULL
655          !!!cp ('j4');
656          !!!parse-error (type => 'NULL');
657          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
658        } elsif ($self->{next_char} <= 0x0008 or
659                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
660                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
661                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
662                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
663                 {
664                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
665                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
666                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
667                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
668                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
669                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
670                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
671                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
672                  0x10FFFE => 1, 0x10FFFF => 1,
673                 }->{$self->{next_char}}) {
674          !!!cp ('j5');
675          if ($self->{next_char} < 0x10000) {
676            !!!parse-error (type => 'control char',
677                            text => (sprintf 'U+%04X', $self->{next_char}));
678        } else {        } else {
679          my $next_char = ord substr $$s, $i++, 1;          !!!parse-error (type => 'control char',
680          if ($next_char == 0x000A) { # LF                          text => (sprintf 'U-%08X', $self->{next_char}));
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
681        }        }
       $self->{next_input_character} = 0x000A; # LF # MUST  
       $line++;  
       $column = -1;  
     } elsif ($self->{next_input_character} > 0x10FFFF) {  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
     } elsif ($self->{next_input_character} == 0x0000) { # NULL  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
682      }      }
683    };    };
684      $self->{prev_char} = [-1, -1, -1];
685      $self->{next_char} = -1;
686    
687    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
688      my (%opt) = @_;      my (%opt) = @_;
689      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
690        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
691        warn "Parse error ($opt{type}) at line $line column $column\n";
692    };    };
693    $self->{parse_error} = sub {    $self->{parse_error} = sub {
694      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
695    };    };
696    
697    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 352  sub parse_string ($$$;$) { Line 699  sub parse_string ($$$;$) {
699    $self->_construct_tree;    $self->_construct_tree;
700    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
701    
702      delete $self->{parse_error}; # remove loop
703    
704    return $self->{document};    return $self->{document};
705  } # parse_string  } # parse_char_stream
706    
707  sub new ($) {  sub new ($) {
708    my $class = shift;    my $class = shift;
709    my $self = bless {}, $class;    my $self = bless {
710    $self->{set_next_input_character} = sub {      level => {must => 'm',
711      $self->{next_input_character} = -1;                warn => 'w',
712                  info => 'i',
713                  uncertain => 'u'},
714      }, $class;
715      $self->{set_next_char} = sub {
716        $self->{next_char} = -1;
717    };    };
718    $self->{parse_error} = sub {    $self->{parse_error} = sub {
719      #      #
720    };    };
721      $self->{change_encoding} = sub {
722        # if ($_[0] is a supported encoding) {
723        #   run "change the encoding" algorithm;
724        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
725        # }
726      };
727      $self->{application_cache_selection} = sub {
728        #
729      };
730    return $self;    return $self;
731  } # new  } # new
732    
733    sub CM_ENTITY () { 0b001 } # & markup in data
734    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
735    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
736    
737    sub PLAINTEXT_CONTENT_MODEL () { 0 }
738    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
739    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
740    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
741    
742    sub DATA_STATE () { 0 }
743    sub ENTITY_DATA_STATE () { 1 }
744    sub TAG_OPEN_STATE () { 2 }
745    sub CLOSE_TAG_OPEN_STATE () { 3 }
746    sub TAG_NAME_STATE () { 4 }
747    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
748    sub ATTRIBUTE_NAME_STATE () { 6 }
749    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
750    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
751    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
752    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
753    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
754    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
755    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
756    sub COMMENT_START_STATE () { 14 }
757    sub COMMENT_START_DASH_STATE () { 15 }
758    sub COMMENT_STATE () { 16 }
759    sub COMMENT_END_STATE () { 17 }
760    sub COMMENT_END_DASH_STATE () { 18 }
761    sub BOGUS_COMMENT_STATE () { 19 }
762    sub DOCTYPE_STATE () { 20 }
763    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
764    sub DOCTYPE_NAME_STATE () { 22 }
765    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
766    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
767    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
768    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
769    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
770    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
771    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
772    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
773    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
774    sub BOGUS_DOCTYPE_STATE () { 32 }
775    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
776    sub SELF_CLOSING_START_TAG_STATE () { 34 }
777    sub CDATA_BLOCK_STATE () { 35 }
778    
779    sub DOCTYPE_TOKEN () { 1 }
780    sub COMMENT_TOKEN () { 2 }
781    sub START_TAG_TOKEN () { 3 }
782    sub END_TAG_TOKEN () { 4 }
783    sub END_OF_FILE_TOKEN () { 5 }
784    sub CHARACTER_TOKEN () { 6 }
785    
786    sub AFTER_HTML_IMS () { 0b100 }
787    sub HEAD_IMS ()       { 0b1000 }
788    sub BODY_IMS ()       { 0b10000 }
789    sub BODY_TABLE_IMS () { 0b100000 }
790    sub TABLE_IMS ()      { 0b1000000 }
791    sub ROW_IMS ()        { 0b10000000 }
792    sub BODY_AFTER_IMS () { 0b100000000 }
793    sub FRAME_IMS ()      { 0b1000000000 }
794    sub SELECT_IMS ()     { 0b10000000000 }
795    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
796        ## NOTE: "in foreign content" insertion mode is special; it is combined
797        ## with the secondary insertion mode.  In this parser, they are stored
798        ## together in the bit-or'ed form.
799    
800    ## NOTE: "initial" and "before html" insertion modes have no constants.
801    
802    ## NOTE: "after after body" insertion mode.
803    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
804    
805    ## NOTE: "after after frameset" insertion mode.
806    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
807    
808    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
809    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
810    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
811    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
812    sub IN_BODY_IM () { BODY_IMS }
813    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
814    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
815    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
816    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
817    sub IN_TABLE_IM () { TABLE_IMS }
818    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
819    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
820    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
821    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
822    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
823    sub IN_COLUMN_GROUP_IM () { 0b10 }
824    
825  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
826    
827  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
828    my $self = shift;    my $self = shift;
829    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
830    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
831    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
832    undef $self->{current_attribute};    undef $self->{current_attribute};
833    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
834    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
835      delete $self->{self_closing};
836    $self->{char} = [];    $self->{char} = [];
837    # $self->{next_input_character}    # $self->{next_char}
838    !!!next-input-character;    !!!next-input-character;
839    $self->{token} = [];    $self->{token} = [];
840      # $self->{escape}
841  } # _initialize_tokenizer  } # _initialize_tokenizer
842    
843  ## A token has:  ## A token has:
844  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
845  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
846  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
847      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
848  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
849  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
850  ##   ->{data} (comment, character)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
851    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
852  ## Macros  ##        ->{name}
853  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
854  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
855  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
856    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
857    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
858    ##     while the token is pushed back to the stack.
859    
860  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
861    
# Line 405  sub _initialize_tokenizer ($) { Line 865  sub _initialize_tokenizer ($) {
865  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
866  ## and removed from the list.  ## and removed from the list.
867    
868    ## NOTE: HTML5 "Writing HTML documents" section, applied to
869    ## documents and not to user agents and conformance checkers,
870    ## contains some requirements that are not detected by the
871    ## parsing algorithm:
872    ## - Some requirements on character encoding declarations. ## TODO
873    ## - "Elements MUST NOT contain content that their content model disallows."
874    ##   ... Some are parse error, some are not (will be reported by c.c.).
875    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
876    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
877    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
878    
879    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
880    ## be detected by the HTML5 parsing algorithm:
881    ## - Text,
882    
883  sub _get_next_token ($) {  sub _get_next_token ($) {
884    my $self = shift;    my $self = shift;
885    
886      if ($self->{self_closing}) {
887        !!!parse-error (type => 'nestc', token => $self->{current_token});
888        ## NOTE: The |self_closing| flag is only set by start tag token.
889        ## In addition, when a start tag token is emitted, it is always set to
890        ## |current_token|.
891        delete $self->{self_closing};
892      }
893    
894    if (@{$self->{token}}) {    if (@{$self->{token}}) {
895        $self->{self_closing} = $self->{token}->[0]->{self_closing};
896      return shift @{$self->{token}};      return shift @{$self->{token}};
897    }    }
898    
899    A: {    A: {
900      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
901        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
902          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
903              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
904            $self->{state} = 'entity data';            !!!cp (1);
905              $self->{state} = ENTITY_DATA_STATE;
906            !!!next-input-character;            !!!next-input-character;
907            redo A;            redo A;
908          } else {          } else {
909              !!!cp (2);
910            #            #
911          }          }
912        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x002D) { # -
913          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
914            $self->{state} = 'tag open';            unless ($self->{escape}) {
915                if ($self->{prev_char}->[0] == 0x002D and # -
916                    $self->{prev_char}->[1] == 0x0021 and # !
917                    $self->{prev_char}->[2] == 0x003C) { # <
918                  !!!cp (3);
919                  $self->{escape} = 1;
920                } else {
921                  !!!cp (4);
922                }
923              } else {
924                !!!cp (5);
925              }
926            }
927            
928            #
929          } elsif ($self->{next_char} == 0x003C) { # <
930            if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
931                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
932                 not $self->{escape})) {
933              !!!cp (6);
934              $self->{state} = TAG_OPEN_STATE;
935            !!!next-input-character;            !!!next-input-character;
936            redo A;            redo A;
937          } else {          } else {
938              !!!cp (7);
939            #            #
940          }          }
941        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
942          !!!emit ({type => 'end-of-file'});          if ($self->{escape} and
943                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
944              if ($self->{prev_char}->[0] == 0x002D and # -
945                  $self->{prev_char}->[1] == 0x002D) { # -
946                !!!cp (8);
947                delete $self->{escape};
948              } else {
949                !!!cp (9);
950              }
951            } else {
952              !!!cp (10);
953            }
954            
955            #
956          } elsif ($self->{next_char} == -1) {
957            !!!cp (11);
958            !!!emit ({type => END_OF_FILE_TOKEN,
959                      line => $self->{line}, column => $self->{column}});
960          last A; ## TODO: ok?          last A; ## TODO: ok?
961          } else {
962            !!!cp (12);
963        }        }
964        # Anything else        # Anything else
965        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
966                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
967                       line => $self->{line}, column => $self->{column},
968                      };
969        ## Stay in the data state        ## Stay in the data state
970        !!!next-input-character;        !!!next-input-character;
971    
972        !!!emit ($token);        !!!emit ($token);
973    
974        redo A;        redo A;
975      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
976        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
977    
978          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
979                
980        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
981    
982        $self->{state} = 'data';        $self->{state} = DATA_STATE;
983        # next-input-character is already done        # next-input-character is already done
984    
985        unless (defined $token) {        unless (defined $token) {
986          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
987            !!!emit ({type => CHARACTER_TOKEN, data => '&',
988                      line => $l, column => $c,
989                     });
990        } else {        } else {
991            !!!cp (14);
992          !!!emit ($token);          !!!emit ($token);
993        }        }
994    
995        redo A;        redo A;
996      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
997        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
998            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
999          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
1000            !!!next-input-character;            !!!next-input-character;
1001            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1002            redo A;            redo A;
1003          } else {          } else {
1004              !!!cp (16);
1005            ## reconsume            ## reconsume
1006            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1007    
1008            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1009                        line => $self->{line_prev},
1010                        column => $self->{column_prev},
1011                       });
1012    
1013            redo A;            redo A;
1014          }          }
1015        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1016          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1017            $self->{state} = 'markup declaration open';            !!!cp (17);
1018              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1019            !!!next-input-character;            !!!next-input-character;
1020            redo A;            redo A;
1021          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1022            $self->{state} = 'close tag open';            !!!cp (18);
1023              $self->{state} = CLOSE_TAG_OPEN_STATE;
1024            !!!next-input-character;            !!!next-input-character;
1025            redo A;            redo A;
1026          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1027                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1028              !!!cp (19);
1029            $self->{current_token}            $self->{current_token}
1030              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1031                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1032            $self->{state} = 'tag name';                 line => $self->{line_prev},
1033                   column => $self->{column_prev}};
1034              $self->{state} = TAG_NAME_STATE;
1035            !!!next-input-character;            !!!next-input-character;
1036            redo A;            redo A;
1037          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1038                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1039            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1040                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1041            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1042                                        line => $self->{line_prev},
1043                                        column => $self->{column_prev}};
1044              $self->{state} = TAG_NAME_STATE;
1045            !!!next-input-character;            !!!next-input-character;
1046            redo A;            redo A;
1047          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1048            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1049            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1050                              line => $self->{line_prev},
1051                              column => $self->{column_prev});
1052              $self->{state} = DATA_STATE;
1053            !!!next-input-character;            !!!next-input-character;
1054    
1055            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1056                        line => $self->{line_prev},
1057                        column => $self->{column_prev},
1058                       });
1059    
1060            redo A;            redo A;
1061          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1062            !!!parse-error (type => 'pio');            !!!cp (22);
1063            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1064            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1065                              column => $self->{column_prev});
1066              $self->{state} = BOGUS_COMMENT_STATE;
1067              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1068                                        line => $self->{line_prev},
1069                                        column => $self->{column_prev},
1070                                       };
1071              ## $self->{next_char} is intentionally left as is
1072            redo A;            redo A;
1073          } else {          } else {
1074            !!!parse-error (type => 'bare stago');            !!!cp (23);
1075            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1076                              line => $self->{line_prev},
1077                              column => $self->{column_prev});
1078              $self->{state} = DATA_STATE;
1079            ## reconsume            ## reconsume
1080    
1081            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1082                        line => $self->{line_prev},
1083                        column => $self->{column_prev},
1084                       });
1085    
1086            redo A;            redo A;
1087          }          }
1088        } else {        } else {
1089          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1090        }        }
1091      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1092        if ($self->{content_model_flag} eq 'RCDATA' or        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1093            $self->{content_model_flag} eq 'CDATA') {        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1094          my @next_char;          if (defined $self->{last_emitted_start_tag_name}) {
1095          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
1096            push @next_char, $self->{next_input_character};            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1097            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);            my @next_char;
1098            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
1099            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              push @next_char, $self->{next_char};
1100              !!!next-input-character;              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1101              next TAGNAME;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1102            } else {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1103              !!!parse-error (type => 'unmatched end tag');                !!!cp (24);
1104              $self->{next_input_character} = shift @next_char; # reconsume                !!!next-input-character;
1105                  next TAGNAME;
1106                } else {
1107                  !!!cp (25);
1108                  $self->{next_char} = shift @next_char; # reconsume
1109                  !!!back-next-input-character (@next_char);
1110                  $self->{state} = DATA_STATE;
1111    
1112                  !!!emit ({type => CHARACTER_TOKEN, data => '</',
1113                            line => $l, column => $c,
1114                           });
1115      
1116                  redo A;
1117                }
1118              }
1119              push @next_char, $self->{next_char};
1120          
1121              unless ($self->{next_char} == 0x0009 or # HT
1122                      $self->{next_char} == 0x000A or # LF
1123                      $self->{next_char} == 0x000B or # VT
1124                      $self->{next_char} == 0x000C or # FF
1125                      $self->{next_char} == 0x0020 or # SP
1126                      $self->{next_char} == 0x003E or # >
1127                      $self->{next_char} == 0x002F or # /
1128                      $self->{next_char} == -1) {
1129                !!!cp (26);
1130                $self->{next_char} = shift @next_char; # reconsume
1131              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1132              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1133                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1134              !!!emit ({type => 'character', data => '</'});                        line => $l, column => $c,
1135                         });
1136              redo A;              redo A;
1137              } else {
1138                !!!cp (27);
1139                $self->{next_char} = shift @next_char;
1140                !!!back-next-input-character (@next_char);
1141                # and consume...
1142            }            }
         }  
         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;  
1143          } else {          } else {
1144            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
1145            !!!back-next-input-character (@next_char);            !!!cp (28);
1146            # and consume...            # next-input-character is already done
1147              $self->{state} = DATA_STATE;
1148              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1149                        line => $l, column => $c,
1150                       });
1151              redo A;
1152          }          }
1153        }        }
1154                
1155        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1156            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1157          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1158                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1159          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1160                   tag_name => chr ($self->{next_char} + 0x0020),
1161                   line => $l, column => $c};
1162            $self->{state} = TAG_NAME_STATE;
1163            !!!next-input-character;
1164            redo A;
1165          } elsif (0x0061 <= $self->{next_char} and
1166                   $self->{next_char} <= 0x007A) { # a..z
1167            !!!cp (30);
1168            $self->{current_token} = {type => END_TAG_TOKEN,
1169                                      tag_name => chr ($self->{next_char}),
1170                                      line => $l, column => $c};
1171            $self->{state} = TAG_NAME_STATE;
1172            !!!next-input-character;
1173            redo A;
1174          } elsif ($self->{next_char} == 0x003E) { # >
1175            !!!cp (31);
1176            !!!parse-error (type => 'empty end tag',
1177                            line => $self->{line_prev}, ## "<" in "</>"
1178                            column => $self->{column_prev} - 1);
1179            $self->{state} = DATA_STATE;
1180          !!!next-input-character;          !!!next-input-character;
1181          redo A;          redo A;
1182        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
1183                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (32);
         $self->{current_token} = {type => 'end tag',  
                           tag_name => chr ($self->{next_input_character})};  
         $self->{state} = 'tag name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         !!!parse-error (type => 'empty end tag');  
         $self->{state} = 'data';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1184          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1185          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1186          # reconsume          # reconsume
1187    
1188          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1189                      line => $l, column => $c,
1190                     });
1191    
1192          redo A;          redo A;
1193        } else {        } else {
1194            !!!cp (33);
1195          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1196          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1197          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1198          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1199        }                                    column => $self->{column_prev} - 1,
1200      } elsif ($self->{state} eq 'tag name') {                                   };
1201        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1202            $self->{next_input_character} == 0x000A or # LF          redo A;
1203            $self->{next_input_character} == 0x000B or # VT        }
1204            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1205            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1206          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1207          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1208          redo A;            $self->{next_char} == 0x000C or # FF
1209        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1210          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1211            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1212            !!!next-input-character;
1213            redo A;
1214          } elsif ($self->{next_char} == 0x003E) { # >
1215            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1216              !!!cp (35);
1217            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1218          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1219            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1220            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1221              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1222            }            #  !!! cp (36);
1223              #  !!! parse-error (type => 'end tag attribute');
1224              #} else {
1225                !!!cp (37);
1226              #}
1227          } else {          } else {
1228            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1229          }          }
1230          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1231          !!!next-input-character;          !!!next-input-character;
1232    
1233          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1234    
1235          redo A;          redo A;
1236        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1237                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1238          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1239            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1240            # start tag or end tag            # start tag or end tag
1241          ## Stay in this state          ## Stay in this state
1242          !!!next-input-character;          !!!next-input-character;
1243          redo A;          redo A;
1244        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1245          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1246          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1247              !!!cp (39);
1248            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1249          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1250            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1251            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1252              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1253            }            #  !!! cp (40);
1254              #  !!! parse-error (type => 'end tag attribute');
1255              #} else {
1256                !!!cp (41);
1257              #}
1258          } else {          } else {
1259            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1260          }          }
1261          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1262          # reconsume          # reconsume
1263    
1264          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1265    
1266          redo A;          redo A;
1267        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1268            !!!cp (42);
1269            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1270          !!!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  
1271          redo A;          redo A;
1272        } else {        } else {
1273          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1274            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1275            # start tag or end tag            # start tag or end tag
1276          ## Stay in the state          ## Stay in the state
1277          !!!next-input-character;          !!!next-input-character;
1278          redo A;          redo A;
1279        }        }
1280      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1281        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1282            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1283            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1284            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1285            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1286            !!!cp (45);
1287          ## Stay in the state          ## Stay in the state
1288          !!!next-input-character;          !!!next-input-character;
1289          redo A;          redo A;
1290        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1291          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1292              !!!cp (46);
1293            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1294          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1295            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1296            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1297                !!!cp (47);
1298              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1299              } else {
1300                !!!cp (48);
1301            }            }
1302          } else {          } else {
1303            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1304          }          }
1305          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1306          !!!next-input-character;          !!!next-input-character;
1307    
1308          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1309    
1310          redo A;          redo A;
1311        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1312                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1313          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1314                                value => ''};          $self->{current_attribute}
1315          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1316                   value => '',
1317                   line => $self->{line}, column => $self->{column}};
1318            $self->{state} = ATTRIBUTE_NAME_STATE;
1319            !!!next-input-character;
1320            redo A;
1321          } elsif ($self->{next_char} == 0x002F) { # /
1322            !!!cp (50);
1323            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1324          !!!next-input-character;          !!!next-input-character;
1325          redo A;          redo A;
1326        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003C or # <  
                $self->{next_input_character} == -1) {  
1327          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1328          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1329              !!!cp (52);
1330            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1331          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1332            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1333            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1334                !!!cp (53);
1335              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1336              } else {
1337                !!!cp (54);
1338            }            }
1339          } else {          } else {
1340            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1341          }          }
1342          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1343          # reconsume          # reconsume
1344    
1345          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1346    
1347          redo A;          redo A;
1348        } else {        } else {
1349          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1350                                value => ''};               0x0022 => 1, # "
1351          $self->{state} = 'attribute name';               0x0027 => 1, # '
1352                 0x003D => 1, # =
1353                }->{$self->{next_char}}) {
1354              !!!cp (55);
1355              !!!parse-error (type => 'bad attribute name');
1356            } else {
1357              !!!cp (56);
1358            }
1359            $self->{current_attribute}
1360                = {name => chr ($self->{next_char}),
1361                   value => '',
1362                   line => $self->{line}, column => $self->{column}};
1363            $self->{state} = ATTRIBUTE_NAME_STATE;
1364          !!!next-input-character;          !!!next-input-character;
1365          redo A;          redo A;
1366        }        }
1367      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1368        my $before_leave = sub {        my $before_leave = sub {
1369          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1370              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1371            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1372              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1373            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1374          } else {          } else {
1375              !!!cp (58);
1376            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1377              = $self->{current_attribute};              = $self->{current_attribute};
1378          }          }
1379        }; # $before_leave        }; # $before_leave
1380    
1381        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1382            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1383            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1384            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1385            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1386            !!!cp (59);
1387          $before_leave->();          $before_leave->();
1388          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1389          !!!next-input-character;          !!!next-input-character;
1390          redo A;          redo A;
1391        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1392            !!!cp (60);
1393          $before_leave->();          $before_leave->();
1394          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1395          !!!next-input-character;          !!!next-input-character;
1396          redo A;          redo A;
1397        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1398          $before_leave->();          $before_leave->();
1399          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1400              !!!cp (61);
1401            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1402          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1403            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
1404              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1405            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1406              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1407            }            }
1408          } else {          } else {
1409            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1410          }          }
1411          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1412          !!!next-input-character;          !!!next-input-character;
1413    
1414          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1415    
1416          redo A;          redo A;
1417        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1418                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1419          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1420            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1421          ## Stay in the state          ## Stay in the state
1422          !!!next-input-character;          !!!next-input-character;
1423          redo A;          redo A;
1424        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1425            !!!cp (64);
1426          $before_leave->();          $before_leave->();
1427            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1428          !!!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  
1429          redo A;          redo A;
1430        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1431          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1432          $before_leave->();          $before_leave->();
1433          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1434              !!!cp (66);
1435            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1436          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1437            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1438            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1439                !!!cp (67);
1440              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1441              } else {
1442                ## NOTE: This state should never be reached.
1443                !!!cp (68);
1444            }            }
1445          } else {          } else {
1446            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1447          }          }
1448          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1449          # reconsume          # reconsume
1450    
1451          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1452    
1453          redo A;          redo A;
1454        } else {        } else {
1455          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1456                $self->{next_char} == 0x0027) { # '
1457              !!!cp (69);
1458              !!!parse-error (type => 'bad attribute name');
1459            } else {
1460              !!!cp (70);
1461            }
1462            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1463          ## Stay in the state          ## Stay in the state
1464          !!!next-input-character;          !!!next-input-character;
1465          redo A;          redo A;
1466        }        }
1467      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1468        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1469            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1470            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1471            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1472            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1473            !!!cp (71);
1474          ## Stay in the state          ## Stay in the state
1475          !!!next-input-character;          !!!next-input-character;
1476          redo A;          redo A;
1477        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1478          $self->{state} = 'before attribute value';          !!!cp (72);
1479            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1480          !!!next-input-character;          !!!next-input-character;
1481          redo A;          redo A;
1482        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1483          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1484              !!!cp (73);
1485            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1486          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1487            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1488            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1489                !!!cp (74);
1490              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1491              } else {
1492                ## NOTE: This state should never be reached.
1493                !!!cp (75);
1494            }            }
1495          } else {          } else {
1496            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1497          }          }
1498          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1499          !!!next-input-character;          !!!next-input-character;
1500    
1501          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1502    
1503          redo A;          redo A;
1504        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1505                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1506          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1507                                value => ''};          $self->{current_attribute}
1508          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1509          !!!next-input-character;                 value => '',
1510          redo A;                 line => $self->{line}, column => $self->{column}};
1511        } elsif ($self->{next_input_character} == 0x002F) { # /          $self->{state} = ATTRIBUTE_NAME_STATE;
1512            !!!next-input-character;
1513            redo A;
1514          } elsif ($self->{next_char} == 0x002F) { # /
1515            !!!cp (77);
1516            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1517          !!!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  
1518          redo A;          redo A;
1519        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1520          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1521          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1522              !!!cp (79);
1523            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1524          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1525            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1526            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1527                !!!cp (80);
1528              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1529              } else {
1530                ## NOTE: This state should never be reached.
1531                !!!cp (81);
1532            }            }
1533          } else {          } else {
1534            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1535          }          }
1536          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1537          # reconsume          # reconsume
1538    
1539          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1540    
1541          redo A;          redo A;
1542        } else {        } else {
1543          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1544                                value => ''};          $self->{current_attribute}
1545          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char}),
1546                   value => '',
1547                   line => $self->{line}, column => $self->{column}};
1548            $self->{state} = ATTRIBUTE_NAME_STATE;
1549          !!!next-input-character;          !!!next-input-character;
1550          redo A;                  redo A;        
1551        }        }
1552      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1553        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1554            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1555            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1556            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1557            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1558            !!!cp (83);
1559          ## Stay in the state          ## Stay in the state
1560          !!!next-input-character;          !!!next-input-character;
1561          redo A;          redo A;
1562        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1563          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1564            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1565          !!!next-input-character;          !!!next-input-character;
1566          redo A;          redo A;
1567        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1568          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1569            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1570          ## reconsume          ## reconsume
1571          redo A;          redo A;
1572        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1573          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1574            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1575          !!!next-input-character;          !!!next-input-character;
1576          redo A;          redo A;
1577        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1578          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1579              !!!cp (87);
1580            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1581          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1582            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1583            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1584                !!!cp (88);
1585              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1586              } else {
1587                ## NOTE: This state should never be reached.
1588                !!!cp (89);
1589            }            }
1590          } else {          } else {
1591            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1592          }          }
1593          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1594          !!!next-input-character;          !!!next-input-character;
1595    
1596          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1597    
1598          redo A;          redo A;
1599        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1600          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1601          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1602              !!!cp (90);
1603            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1604          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1605            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1606            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1607                !!!cp (91);
1608              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1609              } else {
1610                ## NOTE: This state should never be reached.
1611                !!!cp (92);
1612            }            }
1613          } else {          } else {
1614            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1615          }          }
1616          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1617          ## reconsume          ## reconsume
1618    
1619          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1620    
1621          redo A;          redo A;
1622        } else {        } else {
1623          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1624          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1625              !!!parse-error (type => 'bad attribute value');
1626            } else {
1627              !!!cp (94);
1628            }
1629            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1630            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1631          !!!next-input-character;          !!!next-input-character;
1632          redo A;          redo A;
1633        }        }
1634      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1635        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1636          $self->{state} = 'before attribute name';          !!!cp (95);
1637            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1638          !!!next-input-character;          !!!next-input-character;
1639          redo A;          redo A;
1640        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1641          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1642          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1643            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1644          !!!next-input-character;          !!!next-input-character;
1645          redo A;          redo A;
1646        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1647          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1648          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1649              !!!cp (97);
1650            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1651          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1652            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1653            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1654                !!!cp (98);
1655              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1656              } else {
1657                ## NOTE: This state should never be reached.
1658                !!!cp (99);
1659            }            }
1660          } else {          } else {
1661            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1662          }          }
1663          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1664          ## reconsume          ## reconsume
1665    
1666          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1667    
1668          redo A;          redo A;
1669        } else {        } else {
1670          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1671            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1672          ## Stay in the state          ## Stay in the state
1673          !!!next-input-character;          !!!next-input-character;
1674          redo A;          redo A;
1675        }        }
1676      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1677        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1678          $self->{state} = 'before attribute name';          !!!cp (101);
1679            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1680          !!!next-input-character;          !!!next-input-character;
1681          redo A;          redo A;
1682        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1683          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1684          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1685            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1686          !!!next-input-character;          !!!next-input-character;
1687          redo A;          redo A;
1688        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1689          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1690          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1691              !!!cp (103);
1692            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1693          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1694            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1695            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1696                !!!cp (104);
1697              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1698              } else {
1699                ## NOTE: This state should never be reached.
1700                !!!cp (105);
1701            }            }
1702          } else {          } else {
1703            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1704          }          }
1705          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1706          ## reconsume          ## reconsume
1707    
1708          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1709    
1710          redo A;          redo A;
1711        } else {        } else {
1712          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1713            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1714          ## Stay in the state          ## Stay in the state
1715          !!!next-input-character;          !!!next-input-character;
1716          redo A;          redo A;
1717        }        }
1718      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1719        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1720            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1721            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1722            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1723            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1724          $self->{state} = 'before attribute name';          !!!cp (107);
1725          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1726          redo A;          !!!next-input-character;
1727        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1728          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1729          $self->{state} = 'entity in attribute value';          !!!cp (108);
1730          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1731          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1732        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1733          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1734          } elsif ($self->{next_char} == 0x003E) { # >
1735            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1736              !!!cp (109);
1737            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1738          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1739            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1740            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1741                !!!cp (110);
1742              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1743              } else {
1744                ## NOTE: This state should never be reached.
1745                !!!cp (111);
1746            }            }
1747          } else {          } else {
1748            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1749          }          }
1750          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1751          !!!next-input-character;          !!!next-input-character;
1752    
1753          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1754    
1755          redo A;          redo A;
1756        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1757          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1758          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1759              !!!cp (112);
1760            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1761          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1762            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1763            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1764                !!!cp (113);
1765              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1766              } else {
1767                ## NOTE: This state should never be reached.
1768                !!!cp (114);
1769            }            }
1770          } else {          } else {
1771            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1772          }          }
1773          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1774          ## reconsume          ## reconsume
1775    
1776          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1777    
1778          redo A;          redo A;
1779        } else {        } else {
1780          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1781                 0x0022 => 1, # "
1782                 0x0027 => 1, # '
1783                 0x003D => 1, # =
1784                }->{$self->{next_char}}) {
1785              !!!cp (115);
1786              !!!parse-error (type => 'bad attribute value');
1787            } else {
1788              !!!cp (116);
1789            }
1790            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1791          ## Stay in the state          ## Stay in the state
1792          !!!next-input-character;          !!!next-input-character;
1793          redo A;          redo A;
1794        }        }
1795      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1796        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity
1797              (1,
1798               $self->{last_attribute_value_state}
1799                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1800               $self->{last_attribute_value_state}
1801                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1802               -1);
1803    
1804        unless (defined $token) {        unless (defined $token) {
1805            !!!cp (117);
1806          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1807        } else {        } else {
1808            !!!cp (118);
1809          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1810            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1811          ## 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"
1812        }        }
1813    
1814        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1815        # next-input-character is already done        # next-input-character is already done
1816        redo A;        redo A;
1817      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1818          if ($self->{next_char} == 0x0009 or # HT
1819              $self->{next_char} == 0x000A or # LF
1820              $self->{next_char} == 0x000B or # VT
1821              $self->{next_char} == 0x000C or # FF
1822              $self->{next_char} == 0x0020) { # SP
1823            !!!cp (118);
1824            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1825            !!!next-input-character;
1826            redo A;
1827          } elsif ($self->{next_char} == 0x003E) { # >
1828            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1829              !!!cp (119);
1830              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1831            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1832              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1833              if ($self->{current_token}->{attributes}) {
1834                !!!cp (120);
1835                !!!parse-error (type => 'end tag attribute');
1836              } else {
1837                ## NOTE: This state should never be reached.
1838                !!!cp (121);
1839              }
1840            } else {
1841              die "$0: $self->{current_token}->{type}: Unknown token type";
1842            }
1843            $self->{state} = DATA_STATE;
1844            !!!next-input-character;
1845    
1846            !!!emit ($self->{current_token}); # start tag or end tag
1847    
1848            redo A;
1849          } elsif ($self->{next_char} == 0x002F) { # /
1850            !!!cp (122);
1851            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1852            !!!next-input-character;
1853            redo A;
1854          } elsif ($self->{next_char} == -1) {
1855            !!!parse-error (type => 'unclosed tag');
1856            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1857              !!!cp (122.3);
1858              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1859            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1860              if ($self->{current_token}->{attributes}) {
1861                !!!cp (122.1);
1862                !!!parse-error (type => 'end tag attribute');
1863              } else {
1864                ## NOTE: This state should never be reached.
1865                !!!cp (122.2);
1866              }
1867            } else {
1868              die "$0: $self->{current_token}->{type}: Unknown token type";
1869            }
1870            $self->{state} = DATA_STATE;
1871            ## Reconsume.
1872            !!!emit ($self->{current_token}); # start tag or end tag
1873            redo A;
1874          } else {
1875            !!!cp ('124.1');
1876            !!!parse-error (type => 'no space between attributes');
1877            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1878            ## reconsume
1879            redo A;
1880          }
1881        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1882          if ($self->{next_char} == 0x003E) { # >
1883            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1884              !!!cp ('124.2');
1885              !!!parse-error (type => 'nestc', token => $self->{current_token});
1886              ## TODO: Different type than slash in start tag
1887              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1888              if ($self->{current_token}->{attributes}) {
1889                !!!cp ('124.4');
1890                !!!parse-error (type => 'end tag attribute');
1891              } else {
1892                !!!cp ('124.5');
1893              }
1894              ## TODO: Test |<title></title/>|
1895            } else {
1896              !!!cp ('124.3');
1897              $self->{self_closing} = 1;
1898            }
1899    
1900            $self->{state} = DATA_STATE;
1901            !!!next-input-character;
1902    
1903            !!!emit ($self->{current_token}); # start tag or end tag
1904    
1905            redo A;
1906          } elsif ($self->{next_char} == -1) {
1907            !!!parse-error (type => 'unclosed tag');
1908            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1909              !!!cp (124.7);
1910              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1911            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1912              if ($self->{current_token}->{attributes}) {
1913                !!!cp (124.5);
1914                !!!parse-error (type => 'end tag attribute');
1915              } else {
1916                ## NOTE: This state should never be reached.
1917                !!!cp (124.6);
1918              }
1919            } else {
1920              die "$0: $self->{current_token}->{type}: Unknown token type";
1921            }
1922            $self->{state} = DATA_STATE;
1923            ## Reconsume.
1924            !!!emit ($self->{current_token}); # start tag or end tag
1925            redo A;
1926          } else {
1927            !!!cp ('124.4');
1928            !!!parse-error (type => 'nestc');
1929            ## TODO: This error type is wrong.
1930            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1931            ## Reconsume.
1932            redo A;
1933          }
1934        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1935        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1936                
1937        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1938          #my $token = {type => COMMENT_TOKEN, data => ''};
1939    
1940        BC: {        BC: {
1941          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1942            $self->{state} = 'data';            !!!cp (124);
1943              $self->{state} = DATA_STATE;
1944            !!!next-input-character;            !!!next-input-character;
1945    
1946            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1947    
1948            redo A;            redo A;
1949          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1950            $self->{state} = 'data';            !!!cp (125);
1951              $self->{state} = DATA_STATE;
1952            ## reconsume            ## reconsume
1953    
1954            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1955    
1956            redo A;            redo A;
1957          } else {          } else {
1958            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1959              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1960            !!!next-input-character;            !!!next-input-character;
1961            redo BC;            redo BC;
1962          }          }
1963        } # BC        } # BC
1964      } elsif ($self->{state} eq 'markup declaration open') {  
1965          die "$0: _get_next_token: unexpected case [BC]";
1966        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1967        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1968    
1969          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1970    
1971        my @next_char;        my @next_char;
1972        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1973                
1974        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
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} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1978            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1979            $self->{state} = 'comment';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1980                                        line => $l, column => $c,
1981                                       };
1982              $self->{state} = COMMENT_START_STATE;
1983            !!!next-input-character;            !!!next-input-character;
1984            redo A;            redo A;
1985            } else {
1986              !!!cp (128);
1987          }          }
1988        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1989                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1990          !!!next-input-character;          !!!next-input-character;
1991          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1992          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1993              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1994            !!!next-input-character;            !!!next-input-character;
1995            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1996            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1997                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1998              !!!next-input-character;              !!!next-input-character;
1999              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
2000              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2001                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2002                !!!next-input-character;                !!!next-input-character;
2003                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
2004                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
2005                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
2006                  !!!next-input-character;                  !!!next-input-character;
2007                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
2008                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
2009                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
2010                    !!!next-input-character;                    !!!next-input-character;
2011                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
2012                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
2013                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
2014                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
2015                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
2016                        $self->{state} = DOCTYPE_STATE;
2017                        $self->{current_token} = {type => DOCTYPE_TOKEN,
2018                                                  quirks => 1,
2019                                                  line => $l, column => $c,
2020                                                 };
2021                      !!!next-input-character;                      !!!next-input-character;
2022                      redo A;                      redo A;
2023                      } else {
2024                        !!!cp (130);
2025                    }                    }
2026                    } else {
2027                      !!!cp (131);
2028                  }                  }
2029                  } else {
2030                    !!!cp (132);
2031                }                }
2032                } else {
2033                  !!!cp (133);
2034              }              }
2035              } else {
2036                !!!cp (134);
2037            }            }
2038            } else {
2039              !!!cp (135);
2040          }          }
2041          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2042                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2043                   $self->{next_char} == 0x005B) { # [
2044            !!!next-input-character;
2045            push @next_char, $self->{next_char};
2046            if ($self->{next_char} == 0x0043) { # C
2047              !!!next-input-character;
2048              push @next_char, $self->{next_char};
2049              if ($self->{next_char} == 0x0044) { # D
2050                !!!next-input-character;
2051                push @next_char, $self->{next_char};
2052                if ($self->{next_char} == 0x0041) { # A
2053                  !!!next-input-character;
2054                  push @next_char, $self->{next_char};
2055                  if ($self->{next_char} == 0x0054) { # T
2056                    !!!next-input-character;
2057                    push @next_char, $self->{next_char};
2058                    if ($self->{next_char} == 0x0041) { # A
2059                      !!!next-input-character;
2060                      push @next_char, $self->{next_char};
2061                      if ($self->{next_char} == 0x005B) { # [
2062                        !!!cp (135.1);
2063                        $self->{state} = CDATA_BLOCK_STATE;
2064                        !!!next-input-character;
2065                        redo A;
2066                      } else {
2067                        !!!cp (135.2);
2068                      }
2069                    } else {
2070                      !!!cp (135.3);
2071                    }
2072                  } else {
2073                    !!!cp (135.4);                
2074                  }
2075                } else {
2076                  !!!cp (135.5);
2077                }
2078              } else {
2079                !!!cp (135.6);
2080              }
2081            } else {
2082              !!!cp (135.7);
2083            }
2084          } else {
2085            !!!cp (136);
2086        }        }
2087    
2088        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
2089        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2090        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2091        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
2092          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2093                                    line => $l, column => $c,
2094                                   };
2095        redo A;        redo A;
2096                
2097        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2098        ## 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?
2099      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
2100        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2101          $self->{state} = 'comment dash';          !!!cp (137);
2102            $self->{state} = COMMENT_START_DASH_STATE;
2103          !!!next-input-character;          !!!next-input-character;
2104          redo A;          redo A;
2105        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2106            !!!cp (138);
2107            !!!parse-error (type => 'bogus comment');
2108            $self->{state} = DATA_STATE;
2109            !!!next-input-character;
2110    
2111            !!!emit ($self->{current_token}); # comment
2112    
2113            redo A;
2114          } elsif ($self->{next_char} == -1) {
2115            !!!cp (139);
2116          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2117          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2118          ## reconsume          ## reconsume
2119    
2120          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2121    
2122          redo A;          redo A;
2123        } else {        } else {
2124          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (140);
2125            $self->{current_token}->{data} # comment
2126                .= chr ($self->{next_char});
2127            $self->{state} = COMMENT_STATE;
2128            !!!next-input-character;
2129            redo A;
2130          }
2131        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2132          if ($self->{next_char} == 0x002D) { # -
2133            !!!cp (141);
2134            $self->{state} = COMMENT_END_STATE;
2135            !!!next-input-character;
2136            redo A;
2137          } elsif ($self->{next_char} == 0x003E) { # >
2138            !!!cp (142);
2139            !!!parse-error (type => 'bogus comment');
2140            $self->{state} = DATA_STATE;
2141            !!!next-input-character;
2142    
2143            !!!emit ($self->{current_token}); # comment
2144    
2145            redo A;
2146          } elsif ($self->{next_char} == -1) {
2147            !!!cp (143);
2148            !!!parse-error (type => 'unclosed comment');
2149            $self->{state} = DATA_STATE;
2150            ## reconsume
2151    
2152            !!!emit ($self->{current_token}); # comment
2153    
2154            redo A;
2155          } else {
2156            !!!cp (144);
2157            $self->{current_token}->{data} # comment
2158                .= '-' . chr ($self->{next_char});
2159            $self->{state} = COMMENT_STATE;
2160            !!!next-input-character;
2161            redo A;
2162          }
2163        } elsif ($self->{state} == COMMENT_STATE) {
2164          if ($self->{next_char} == 0x002D) { # -
2165            !!!cp (145);
2166            $self->{state} = COMMENT_END_DASH_STATE;
2167            !!!next-input-character;
2168            redo A;
2169          } elsif ($self->{next_char} == -1) {
2170            !!!cp (146);
2171            !!!parse-error (type => 'unclosed comment');
2172            $self->{state} = DATA_STATE;
2173            ## reconsume
2174    
2175            !!!emit ($self->{current_token}); # comment
2176    
2177            redo A;
2178          } else {
2179            !!!cp (147);
2180            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2181          ## Stay in the state          ## Stay in the state
2182          !!!next-input-character;          !!!next-input-character;
2183          redo A;          redo A;
2184        }        }
2185      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2186        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2187          $self->{state} = 'comment end';          !!!cp (148);
2188            $self->{state} = COMMENT_END_STATE;
2189          !!!next-input-character;          !!!next-input-character;
2190          redo A;          redo A;
2191        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2192            !!!cp (149);
2193          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2194          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2195          ## reconsume          ## reconsume
2196    
2197          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2198    
2199          redo A;          redo A;
2200        } else {        } else {
2201          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2202          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2203            $self->{state} = COMMENT_STATE;
2204          !!!next-input-character;          !!!next-input-character;
2205          redo A;          redo A;
2206        }        }
2207      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2208        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2209          $self->{state} = 'data';          !!!cp (151);
2210            $self->{state} = DATA_STATE;
2211          !!!next-input-character;          !!!next-input-character;
2212    
2213          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2214    
2215          redo A;          redo A;
2216        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2217          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2218            !!!parse-error (type => 'dash in comment',
2219                            line => $self->{line_prev},
2220                            column => $self->{column_prev});
2221          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2222          ## Stay in the state          ## Stay in the state
2223          !!!next-input-character;          !!!next-input-character;
2224          redo A;          redo A;
2225        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2226            !!!cp (153);
2227          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2228          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2229          ## reconsume          ## reconsume
2230    
2231          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2232    
2233          redo A;          redo A;
2234        } else {        } else {
2235          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2236          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2237          $self->{state} = 'comment';                          line => $self->{line_prev},
2238                            column => $self->{column_prev});
2239            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2240            $self->{state} = COMMENT_STATE;
2241          !!!next-input-character;          !!!next-input-character;
2242          redo A;          redo A;
2243        }        }
2244      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2245        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2246            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2247            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2248            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2249            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2250          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2251            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2252          !!!next-input-character;          !!!next-input-character;
2253          redo A;          redo A;
2254        } else {        } else {
2255            !!!cp (156);
2256          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2257          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2258          ## reconsume          ## reconsume
2259          redo A;          redo A;
2260        }        }
2261      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2262        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2263            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2264            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2265            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2266            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2267            !!!cp (157);
2268          ## Stay in the state          ## Stay in the state
2269          !!!next-input-character;          !!!next-input-character;
2270          redo A;          redo A;
2271        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
2272                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (158);
2273          $self->{current_token} = {type => 'DOCTYPE',          !!!parse-error (type => 'no DOCTYPE name');
2274                            name => chr ($self->{next_input_character} - 0x0020),          $self->{state} = DATA_STATE;
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
2275          !!!next-input-character;          !!!next-input-character;
2276    
2277            !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2278    
2279          redo A;          redo A;
2280        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == -1) {
2281            !!!cp (159);
2282          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2283          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2284            ## reconsume
2285    
2286            !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2287    
2288            redo A;
2289          } else {
2290            !!!cp (160);
2291            $self->{current_token}->{name} = chr $self->{next_char};
2292            delete $self->{current_token}->{quirks};
2293    ## ISSUE: "Set the token's name name to the" in the spec
2294            $self->{state} = DOCTYPE_NAME_STATE;
2295            !!!next-input-character;
2296            redo A;
2297          }
2298        } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2299    ## ISSUE: Redundant "First," in the spec.
2300          if ($self->{next_char} == 0x0009 or # HT
2301              $self->{next_char} == 0x000A or # LF
2302              $self->{next_char} == 0x000B or # VT
2303              $self->{next_char} == 0x000C or # FF
2304              $self->{next_char} == 0x0020) { # SP
2305            !!!cp (161);
2306            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2307            !!!next-input-character;
2308            redo A;
2309          } elsif ($self->{next_char} == 0x003E) { # >
2310            !!!cp (162);
2311            $self->{state} = DATA_STATE;
2312          !!!next-input-character;          !!!next-input-character;
2313    
2314          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE
2315    
2316          redo A;          redo A;
2317        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2318          !!!parse-error (type => 'no DOCTYPE name');          !!!cp (163);
2319          $self->{state} = 'data';          !!!parse-error (type => 'unclosed DOCTYPE');
2320            $self->{state} = DATA_STATE;
2321            ## reconsume
2322    
2323            $self->{current_token}->{quirks} = 1;
2324            !!!emit ($self->{current_token}); # DOCTYPE
2325    
2326            redo A;
2327          } else {
2328            !!!cp (164);
2329            $self->{current_token}->{name}
2330              .= chr ($self->{next_char}); # DOCTYPE
2331            ## Stay in the state
2332            !!!next-input-character;
2333            redo A;
2334          }
2335        } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2336          if ($self->{next_char} == 0x0009 or # HT
2337              $self->{next_char} == 0x000A or # LF
2338              $self->{next_char} == 0x000B or # VT
2339              $self->{next_char} == 0x000C or # FF
2340              $self->{next_char} == 0x0020) { # SP
2341            !!!cp (165);
2342            ## Stay in the state
2343            !!!next-input-character;
2344            redo A;
2345          } elsif ($self->{next_char} == 0x003E) { # >
2346            !!!cp (166);
2347            $self->{state} = DATA_STATE;
2348            !!!next-input-character;
2349    
2350            !!!emit ($self->{current_token}); # DOCTYPE
2351    
2352            redo A;
2353          } elsif ($self->{next_char} == -1) {
2354            !!!cp (167);
2355            !!!parse-error (type => 'unclosed DOCTYPE');
2356            $self->{state} = DATA_STATE;
2357            ## reconsume
2358    
2359            $self->{current_token}->{quirks} = 1;
2360            !!!emit ($self->{current_token}); # DOCTYPE
2361    
2362            redo A;
2363          } elsif ($self->{next_char} == 0x0050 or # P
2364                   $self->{next_char} == 0x0070) { # p
2365            !!!next-input-character;
2366            if ($self->{next_char} == 0x0055 or # U
2367                $self->{next_char} == 0x0075) { # u
2368              !!!next-input-character;
2369              if ($self->{next_char} == 0x0042 or # B
2370                  $self->{next_char} == 0x0062) { # b
2371                !!!next-input-character;
2372                if ($self->{next_char} == 0x004C or # L
2373                    $self->{next_char} == 0x006C) { # l
2374                  !!!next-input-character;
2375                  if ($self->{next_char} == 0x0049 or # I
2376                      $self->{next_char} == 0x0069) { # i
2377                    !!!next-input-character;
2378                    if ($self->{next_char} == 0x0043 or # C
2379                        $self->{next_char} == 0x0063) { # c
2380                      !!!cp (168);
2381                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2382                      !!!next-input-character;
2383                      redo A;
2384                    } else {
2385                      !!!cp (169);
2386                    }
2387                  } else {
2388                    !!!cp (170);
2389                  }
2390                } else {
2391                  !!!cp (171);
2392                }
2393              } else {
2394                !!!cp (172);
2395              }
2396            } else {
2397              !!!cp (173);
2398            }
2399    
2400            #
2401          } elsif ($self->{next_char} == 0x0053 or # S
2402                   $self->{next_char} == 0x0073) { # s
2403            !!!next-input-character;
2404            if ($self->{next_char} == 0x0059 or # Y
2405                $self->{next_char} == 0x0079) { # y
2406              !!!next-input-character;
2407              if ($self->{next_char} == 0x0053 or # S
2408                  $self->{next_char} == 0x0073) { # s
2409                !!!next-input-character;
2410                if ($self->{next_char} == 0x0054 or # T
2411                    $self->{next_char} == 0x0074) { # t
2412                  !!!next-input-character;
2413                  if ($self->{next_char} == 0x0045 or # E
2414                      $self->{next_char} == 0x0065) { # e
2415                    !!!next-input-character;
2416                    if ($self->{next_char} == 0x004D or # M
2417                        $self->{next_char} == 0x006D) { # m
2418                      !!!cp (174);
2419                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2420                      !!!next-input-character;
2421                      redo A;
2422                    } else {
2423                      !!!cp (175);
2424                    }
2425                  } else {
2426                    !!!cp (176);
2427                  }
2428                } else {
2429                  !!!cp (177);
2430                }
2431              } else {
2432                !!!cp (178);
2433              }
2434            } else {
2435              !!!cp (179);
2436            }
2437    
2438            #
2439          } else {
2440            !!!cp (180);
2441            !!!next-input-character;
2442            #
2443          }
2444    
2445          !!!parse-error (type => 'string after DOCTYPE name');
2446          $self->{current_token}->{quirks} = 1;
2447    
2448          $self->{state} = BOGUS_DOCTYPE_STATE;
2449          # next-input-character is already done
2450          redo A;
2451        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2452          if ({
2453                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2454                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2455              }->{$self->{next_char}}) {
2456            !!!cp (181);
2457            ## Stay in the state
2458            !!!next-input-character;
2459            redo A;
2460          } elsif ($self->{next_char} eq 0x0022) { # "
2461            !!!cp (182);
2462            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2463            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2464            !!!next-input-character;
2465            redo A;
2466          } elsif ($self->{next_char} eq 0x0027) { # '
2467            !!!cp (183);
2468            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2469            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2470            !!!next-input-character;
2471            redo A;
2472          } elsif ($self->{next_char} eq 0x003E) { # >
2473            !!!cp (184);
2474            !!!parse-error (type => 'no PUBLIC literal');
2475    
2476            $self->{state} = DATA_STATE;
2477            !!!next-input-character;
2478    
2479            $self->{current_token}->{quirks} = 1;
2480            !!!emit ($self->{current_token}); # DOCTYPE
2481    
2482            redo A;
2483          } elsif ($self->{next_char} == -1) {
2484            !!!cp (185);
2485            !!!parse-error (type => 'unclosed DOCTYPE');
2486    
2487            $self->{state} = DATA_STATE;
2488            ## reconsume
2489    
2490            $self->{current_token}->{quirks} = 1;
2491            !!!emit ($self->{current_token}); # DOCTYPE
2492    
2493            redo A;
2494          } else {
2495            !!!cp (186);
2496            !!!parse-error (type => 'string after PUBLIC');
2497            $self->{current_token}->{quirks} = 1;
2498    
2499            $self->{state} = BOGUS_DOCTYPE_STATE;
2500            !!!next-input-character;
2501            redo A;
2502          }
2503        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2504          if ($self->{next_char} == 0x0022) { # "
2505            !!!cp (187);
2506            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2507            !!!next-input-character;
2508            redo A;
2509          } elsif ($self->{next_char} == 0x003E) { # >
2510            !!!cp (188);
2511            !!!parse-error (type => 'unclosed PUBLIC literal');
2512    
2513            $self->{state} = DATA_STATE;
2514            !!!next-input-character;
2515    
2516            $self->{current_token}->{quirks} = 1;
2517            !!!emit ($self->{current_token}); # DOCTYPE
2518    
2519            redo A;
2520          } elsif ($self->{next_char} == -1) {
2521            !!!cp (189);
2522            !!!parse-error (type => 'unclosed PUBLIC literal');
2523    
2524            $self->{state} = DATA_STATE;
2525          ## reconsume          ## reconsume
2526    
2527          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          $self->{current_token}->{quirks} = 1;
2528            !!!emit ($self->{current_token}); # DOCTYPE
2529    
2530          redo A;          redo A;
2531        } else {        } else {
2532          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (190);
2533                            name => chr ($self->{next_input_character}),          $self->{current_token}->{public_identifier} # DOCTYPE
2534                            error => 1};              .= chr $self->{next_char};
2535          $self->{state} = 'DOCTYPE name';          ## Stay in the state
2536          !!!next-input-character;          !!!next-input-character;
2537          redo A;          redo A;
2538        }        }
2539      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2540        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0027) { # '
2541            $self->{next_input_character} == 0x000A or # LF          !!!cp (191);
2542            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
         $self->{state} = 'after DOCTYPE name';  
2543          !!!next-input-character;          !!!next-input-character;
2544          redo A;          redo A;
2545        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2546          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (192);
2547          $self->{state} = 'data';          !!!parse-error (type => 'unclosed PUBLIC literal');
2548    
2549            $self->{state} = DATA_STATE;
2550          !!!next-input-character;          !!!next-input-character;
2551    
2552            $self->{current_token}->{quirks} = 1;
2553          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2554    
2555          redo A;          redo A;
2556        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
2557                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (193);
2558          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed PUBLIC literal');
2559          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
2560            $self->{state} = DATA_STATE;
2561            ## reconsume
2562    
2563            $self->{current_token}->{quirks} = 1;
2564            !!!emit ($self->{current_token}); # DOCTYPE
2565    
2566            redo A;
2567          } else {
2568            !!!cp (194);
2569            $self->{current_token}->{public_identifier} # DOCTYPE
2570                .= chr $self->{next_char};
2571          ## Stay in the state          ## Stay in the state
2572          !!!next-input-character;          !!!next-input-character;
2573          redo A;          redo A;
2574        } elsif ($self->{next_input_character} == -1) {        }
2575        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2576          if ({
2577                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2578                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2579              }->{$self->{next_char}}) {
2580            !!!cp (195);
2581            ## Stay in the state
2582            !!!next-input-character;
2583            redo A;
2584          } elsif ($self->{next_char} == 0x0022) { # "
2585            !!!cp (196);
2586            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2587            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2588            !!!next-input-character;
2589            redo A;
2590          } elsif ($self->{next_char} == 0x0027) { # '
2591            !!!cp (197);
2592            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2593            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2594            !!!next-input-character;
2595            redo A;
2596          } elsif ($self->{next_char} == 0x003E) { # >
2597            !!!cp (198);
2598            $self->{state} = DATA_STATE;
2599            !!!next-input-character;
2600    
2601            !!!emit ($self->{current_token}); # DOCTYPE
2602    
2603            redo A;
2604          } elsif ($self->{next_char} == -1) {
2605            !!!cp (199);
2606          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2607          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
2608          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2609          ## reconsume          ## reconsume
2610    
2611          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
2612          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
2613    
2614          redo A;          redo A;
2615        } else {        } else {
2616          $self->{current_token}->{name}          !!!cp (200);
2617            .= chr ($self->{next_input_character}); # DOCTYPE          !!!parse-error (type => 'string after PUBLIC literal');
2618          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{current_token}->{quirks} = 1;
2619    
2620            $self->{state} = BOGUS_DOCTYPE_STATE;
2621            !!!next-input-character;
2622            redo A;
2623          }
2624        } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2625          if ({
2626                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2627                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2628              }->{$self->{next_char}}) {
2629            !!!cp (201);
2630          ## Stay in the state          ## Stay in the state
2631          !!!next-input-character;          !!!next-input-character;
2632          redo A;          redo A;
2633          } elsif ($self->{next_char} == 0x0022) { # "
2634            !!!cp (202);
2635            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2636            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2637            !!!next-input-character;
2638            redo A;
2639          } elsif ($self->{next_char} == 0x0027) { # '
2640            !!!cp (203);
2641            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2642            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2643            !!!next-input-character;
2644            redo A;
2645          } elsif ($self->{next_char} == 0x003E) { # >
2646            !!!cp (204);
2647            !!!parse-error (type => 'no SYSTEM literal');
2648            $self->{state} = DATA_STATE;
2649            !!!next-input-character;
2650    
2651            $self->{current_token}->{quirks} = 1;
2652            !!!emit ($self->{current_token}); # DOCTYPE
2653    
2654            redo A;
2655          } elsif ($self->{next_char} == -1) {
2656            !!!cp (205);
2657            !!!parse-error (type => 'unclosed DOCTYPE');
2658    
2659            $self->{state} = DATA_STATE;
2660            ## reconsume
2661    
2662            $self->{current_token}->{quirks} = 1;
2663            !!!emit ($self->{current_token}); # DOCTYPE
2664    
2665            redo A;
2666          } else {
2667            !!!cp (206);
2668            !!!parse-error (type => 'string after SYSTEM');
2669            $self->{current_token}->{quirks} = 1;
2670    
2671            $self->{state} = BOGUS_DOCTYPE_STATE;
2672            !!!next-input-character;
2673            redo A;
2674        }        }
2675      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2676        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0022) { # "
2677            $self->{next_input_character} == 0x000A or # LF          !!!cp (207);
2678            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2679            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
2680            $self->{next_input_character} == 0x0020) { # SP          redo A;
2681          } elsif ($self->{next_char} == 0x003E) { # >
2682            !!!cp (208);
2683            !!!parse-error (type => 'unclosed SYSTEM literal');
2684    
2685            $self->{state} = DATA_STATE;
2686            !!!next-input-character;
2687    
2688            $self->{current_token}->{quirks} = 1;
2689            !!!emit ($self->{current_token}); # DOCTYPE
2690    
2691            redo A;
2692          } elsif ($self->{next_char} == -1) {
2693            !!!cp (209);
2694            !!!parse-error (type => 'unclosed SYSTEM literal');
2695    
2696            $self->{state} = DATA_STATE;
2697            ## reconsume
2698    
2699            $self->{current_token}->{quirks} = 1;
2700            !!!emit ($self->{current_token}); # DOCTYPE
2701    
2702            redo A;
2703          } else {
2704            !!!cp (210);
2705            $self->{current_token}->{system_identifier} # DOCTYPE
2706                .= chr $self->{next_char};
2707          ## Stay in the state          ## Stay in the state
2708          !!!next-input-character;          !!!next-input-character;
2709          redo A;          redo A;
2710        } elsif ($self->{next_input_character} == 0x003E) { # >        }
2711          $self->{state} = 'data';      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2712          if ($self->{next_char} == 0x0027) { # '
2713            !!!cp (211);
2714            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2715          !!!next-input-character;          !!!next-input-character;
2716            redo A;
2717          } elsif ($self->{next_char} == 0x003E) { # >
2718            !!!cp (212);
2719            !!!parse-error (type => 'unclosed SYSTEM literal');
2720    
2721            $self->{state} = DATA_STATE;
2722            !!!next-input-character;
2723    
2724            $self->{current_token}->{quirks} = 1;
2725            !!!emit ($self->{current_token}); # DOCTYPE
2726    
2727            redo A;
2728          } elsif ($self->{next_char} == -1) {
2729            !!!cp (213);
2730            !!!parse-error (type => 'unclosed SYSTEM literal');
2731    
2732            $self->{state} = DATA_STATE;
2733            ## reconsume
2734    
2735            $self->{current_token}->{quirks} = 1;
2736          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2737    
2738          redo A;          redo A;
2739        } elsif ($self->{next_input_character} == -1) {        } else {
2740            !!!cp (214);
2741            $self->{current_token}->{system_identifier} # DOCTYPE
2742                .= chr $self->{next_char};
2743            ## Stay in the state
2744            !!!next-input-character;
2745            redo A;
2746          }
2747        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2748          if ({
2749                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2750                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2751              }->{$self->{next_char}}) {
2752            !!!cp (215);
2753            ## Stay in the state
2754            !!!next-input-character;
2755            redo A;
2756          } elsif ($self->{next_char} == 0x003E) { # >
2757            !!!cp (216);
2758            $self->{state} = DATA_STATE;
2759            !!!next-input-character;
2760    
2761            !!!emit ($self->{current_token}); # DOCTYPE
2762    
2763            redo A;
2764          } elsif ($self->{next_char} == -1) {
2765            !!!cp (217);
2766          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2767          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2768          ## reconsume          ## reconsume
2769    
2770            $self->{current_token}->{quirks} = 1;
2771          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2772    
2773          redo A;          redo A;
2774        } else {        } else {
2775          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (218);
2776          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after SYSTEM literal');
2777          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2778    
2779            $self->{state} = BOGUS_DOCTYPE_STATE;
2780          !!!next-input-character;          !!!next-input-character;
2781          redo A;          redo A;
2782        }        }
2783      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2784        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2785          $self->{state} = 'data';          !!!cp (219);
2786            $self->{state} = DATA_STATE;
2787          !!!next-input-character;          !!!next-input-character;
2788    
2789          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2790    
2791          redo A;          redo A;
2792        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2793            !!!cp (220);
2794          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2795          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2796          ## reconsume          ## reconsume
2797    
2798          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2799    
2800          redo A;          redo A;
2801        } else {        } else {
2802            !!!cp (221);
2803          ## Stay in the state          ## Stay in the state
2804          !!!next-input-character;          !!!next-input-character;
2805          redo A;          redo A;
2806        }        }
2807        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2808          my $s = '';
2809          
2810          my ($l, $c) = ($self->{line}, $self->{column});
2811    
2812          CS: while ($self->{next_char} != -1) {
2813            if ($self->{next_char} == 0x005D) { # ]
2814              !!!next-input-character;
2815              if ($self->{next_char} == 0x005D) { # ]
2816                !!!next-input-character;
2817                MDC: {
2818                  if ($self->{next_char} == 0x003E) { # >
2819                    !!!cp (221.1);
2820                    !!!next-input-character;
2821                    last CS;
2822                  } elsif ($self->{next_char} == 0x005D) { # ]
2823                    !!!cp (221.2);
2824                    $s .= ']';
2825                    !!!next-input-character;
2826                    redo MDC;
2827                  } else {
2828                    !!!cp (221.3);
2829                    $s .= ']]';
2830                    #
2831                  }
2832                } # MDC
2833              } else {
2834                !!!cp (221.4);
2835                $s .= ']';
2836                #
2837              }
2838            } else {
2839              !!!cp (221.5);
2840              #
2841            }
2842            $s .= chr $self->{next_char};
2843            !!!next-input-character;
2844          } # CS
2845    
2846          $self->{state} = DATA_STATE;
2847          ## next-input-character done or EOF, which is reconsumed.
2848    
2849          if (length $s) {
2850            !!!cp (221.6);
2851            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2852                      line => $l, column => $c});
2853          } else {
2854            !!!cp (221.7);
2855          }
2856    
2857          redo A;
2858    
2859          ## ISSUE: "text tokens" in spec.
2860          ## TODO: Streaming support
2861      } else {      } else {
2862        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2863      }      }
# Line 1449  sub _get_next_token ($) { Line 2866  sub _get_next_token ($) {
2866    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2867  } # _get_next_token  } # _get_next_token
2868    
2869  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2870    my $self = shift;    my ($self, $in_attr, $additional) = @_;
2871      
2872    if ($self->{next_input_character} == 0x0023) { # #    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2873    
2874      if ({
2875           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2876           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2877           $additional => 1,
2878          }->{$self->{next_char}}) {
2879        !!!cp (1001);
2880        ## Don't consume
2881        ## No error
2882        return undef;
2883      } elsif ($self->{next_char} == 0x0023) { # #
2884      !!!next-input-character;      !!!next-input-character;
2885      my $num;      if ($self->{next_char} == 0x0078 or # x
2886      if ($self->{next_input_character} == 0x0078 or # x          $self->{next_char} == 0x0058) { # X
2887          $self->{next_input_character} == 0x0058) { # X        my $code;
2888        X: {        X: {
2889          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2890          !!!next-input-character;          !!!next-input-character;
2891          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2892              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2893            $num ||= 0;            !!!cp (1002);
2894            $num *= 0x10;            $code ||= 0;
2895            $num += $self->{next_input_character} - 0x0030;            $code *= 0x10;
2896              $code += $self->{next_char} - 0x0030;
2897            redo X;            redo X;
2898          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2899                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2900            ## ISSUE: the spec says U+0078, which is apparently incorrect            !!!cp (1003);
2901            $num ||= 0;            $code ||= 0;
2902            $num *= 0x10;            $code *= 0x10;
2903            $num += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2904            redo X;            redo X;
2905          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2906                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2907            ## ISSUE: the spec says U+0058, which is apparently incorrect            !!!cp (1004);
2908            $num ||= 0;            $code ||= 0;
2909            $num *= 0x10;            $code *= 0x10;
2910            $num += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2911            redo X;            redo X;
2912          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2913            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2914            $self->{next_input_character} = 0x0023; # #            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2915            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char, $self->{next_char});
2916              $self->{next_char} = 0x0023; # #
2917            return undef;            return undef;
2918          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2919              !!!cp (1006);
2920            !!!next-input-character;            !!!next-input-character;
2921          } else {          } else {
2922            !!!parse-error (type => 'no refc');            !!!cp (1007);
2923          }            !!!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?  
2924          }          }
2925    
2926          return {type => 'character', data => chr $num};          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2927              !!!cp (1008);
2928              !!!parse-error (type => 'invalid character reference',
2929                              text => (sprintf 'U+%04X', $code),
2930                              line => $l, column => $c);
2931              $code = 0xFFFD;
2932            } elsif ($code > 0x10FFFF) {
2933              !!!cp (1009);
2934              !!!parse-error (type => 'invalid character reference',
2935                              text => (sprintf 'U-%08X', $code),
2936                              line => $l, column => $c);
2937              $code = 0xFFFD;
2938            } elsif ($code == 0x000D) {
2939              !!!cp (1010);
2940              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2941              $code = 0x000A;
2942            } elsif (0x80 <= $code and $code <= 0x9F) {
2943              !!!cp (1011);
2944              !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
2945              $code = $c1_entity_char->{$code};
2946            }
2947    
2948            return {type => CHARACTER_TOKEN, data => chr $code,
2949                    has_reference => 1,
2950                    line => $l, column => $c,
2951                   };
2952        } # X        } # X
2953      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2954               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2955        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2956        !!!next-input-character;        !!!next-input-character;
2957                
2958        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2959                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2960            !!!cp (1012);
2961          $code *= 10;          $code *= 10;
2962          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2963                    
2964          !!!next-input-character;          !!!next-input-character;
2965        }        }
2966    
2967        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2968            !!!cp (1013);
2969          !!!next-input-character;          !!!next-input-character;
2970        } else {        } else {
2971          !!!parse-error (type => 'no refc');          !!!cp (1014);
2972            !!!parse-error (type => 'no refc', line => $l, column => $c);
2973        }        }
2974    
2975        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2976        if ($code > 1114111 or $code == 0) {          !!!cp (1015);
2977          $code = 0xFFFD; # REPLACEMENT CHARACTER          !!!parse-error (type => 'invalid character reference',
2978          ## ISSUE: Why this is not an error?                          text => (sprintf 'U+%04X', $code),
2979                            line => $l, column => $c);
2980            $code = 0xFFFD;
2981          } elsif ($code > 0x10FFFF) {
2982            !!!cp (1016);
2983            !!!parse-error (type => 'invalid character reference',
2984                            text => (sprintf 'U-%08X', $code),
2985                            line => $l, column => $c);
2986            $code = 0xFFFD;
2987          } elsif ($code == 0x000D) {
2988            !!!cp (1017);
2989            !!!parse-error (type => 'CR character reference',
2990                            line => $l, column => $c);
2991            $code = 0x000A;
2992          } elsif (0x80 <= $code and $code <= 0x9F) {
2993            !!!cp (1018);
2994            !!!parse-error (type => 'C1 character reference',
2995                            text => (sprintf 'U+%04X', $code),
2996                            line => $l, column => $c);
2997            $code = $c1_entity_char->{$code};
2998        }        }
2999                
3000        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3001                  line => $l, column => $c,
3002                 };
3003      } else {      } else {
3004        !!!parse-error (type => 'bare nero');        !!!cp (1019);
3005        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
3006        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
3007          $self->{next_char} = 0x0023; # #
3008        return undef;        return undef;
3009      }      }
3010    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
3011              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
3012             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
3013              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
3014      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
3015      !!!next-input-character;      !!!next-input-character;
3016    
3017      my $value = $entity_name;      my $value = $entity_name;
3018      my $match;      my $match = 0;
3019        require Whatpm::_NamedEntityList;
3020        our $EntityChar;
3021    
3022      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3023             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3024             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_char} and # a
3025               $self->{next_input_character} <= 0x005A) or               $self->{next_char} <= 0x005A) or # x
3026              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_char} and # a
3027               $self->{next_input_character} <= 0x007A) or               $self->{next_char} <= 0x007A) or # z
3028              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_char} and # 0
3029               $self->{next_input_character} <= 0x0039))) {               $self->{next_char} <= 0x0039) or # 9
3030        $entity_name .= chr $self->{next_input_character};              $self->{next_char} == 0x003B)) { # ;
3031        if (defined $entity_char->{$entity_name}) {        $entity_name .= chr $self->{next_char};
3032          $value = $entity_char->{$entity_name};        if (defined $EntityChar->{$entity_name}) {
3033          $match = 1;          if ($self->{next_char} == 0x003B) { # ;
3034              !!!cp (1020);
3035              $value = $EntityChar->{$entity_name};
3036              $match = 1;
3037              !!!next-input-character;
3038              last;
3039            } else {
3040              !!!cp (1021);
3041              $value = $EntityChar->{$entity_name};
3042              $match = -1;
3043              !!!next-input-character;
3044            }
3045        } else {        } else {
3046          $value .= chr $self->{next_input_character};          !!!cp (1022);
3047            $value .= chr $self->{next_char};
3048            $match *= 2;
3049            !!!next-input-character;
3050        }        }
       !!!next-input-character;  
3051      }      }
3052            
3053      if ($match) {      if ($match > 0) {
3054        if ($self->{next_input_character} == 0x003B) { # ;        !!!cp (1023);
3055          !!!next-input-character;        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3056                  line => $l, column => $c,
3057                 };
3058        } elsif ($match < 0) {
3059          !!!parse-error (type => 'no refc', line => $l, column => $c);
3060          if ($in_attr and $match < -1) {
3061            !!!cp (1024);
3062            return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3063                    line => $l, column => $c,
3064                   };
3065        } else {        } else {
3066          !!!parse-error (type => 'refc');          !!!cp (1025);
3067            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3068                    line => $l, column => $c,
3069                   };
3070        }        }
   
       return {type => 'character', data => $value};  
3071      } else {      } else {
3072        !!!parse-error (type => 'bare ero');        !!!cp (1026);
3073        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3074        !!!back-token ({type => 'character', data => $value});        ## NOTE: "No characters are consumed" in the spec.
3075        return undef;        return {type => CHARACTER_TOKEN, data => '&'.$value,
3076                  line => $l, column => $c,
3077                 };
3078      }      }
3079    } else {    } else {
3080        !!!cp (1027);
3081      ## no characters are consumed      ## no characters are consumed
3082      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3083      return undef;      return undef;
3084    }    }
3085  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1586  sub _initialize_tree_constructor ($) { Line 3090  sub _initialize_tree_constructor ($) {
3090    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
3091    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3092    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3093    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
3094  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3095    
3096  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1613  sub _construct_tree ($) { Line 3117  sub _construct_tree ($) {
3117        
3118    !!!next-token;    !!!next-token;
3119    
   $self->{insertion_mode} = 'before head';  
3120    undef $self->{form_element};    undef $self->{form_element};
3121    undef $self->{head_element};    undef $self->{head_element};
3122    $self->{open_elements} = [];    $self->{open_elements} = [];
3123    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3124    
3125      ## NOTE: The "initial" insertion mode.
3126    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3127    
3128      ## NOTE: The "before html" insertion mode.
3129    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3130      $self->{insertion_mode} = BEFORE_HEAD_IM;
3131    
3132      ## NOTE: The "before head" insertion mode and so on.
3133    $self->_tree_construction_main;    $self->_tree_construction_main;
3134  } # _construct_tree  } # _construct_tree
3135    
3136  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3137    my $self = shift;    my $self = shift;
3138    B: {  
3139        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
3140          if ($token->{error}) {  
3141            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
3142            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
3143          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3144          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
3145            ($token->{name});        ## language.
3146          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
3147          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
3148          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/;
3149          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
3150          return;            defined $token->{public_identifier} or
3151        } elsif ({            defined $token->{system_identifier}) {
3152                  comment => 1,          !!!cp ('t1');
3153                  'start tag' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3154                  'end tag' => 1,        } elsif ($doctype_name ne 'HTML') {
3155                  'end-of-file' => 1,          !!!cp ('t2');
3156                 }->{$token->{type}}) {          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3157          ## ISSUE: Spec currently left this case undefined.          !!!parse-error (type => 'not HTML5', token => $token);
3158          !!!parse-error (type => 'missing DOCTYPE');        } else {
3159          #$phase = 'root element';          !!!cp ('t3');
3160          ## reprocess        }
3161          #redo B;        
3162          return;        my $doctype = $self->{document}->create_document_type_definition
3163        } elsif ($token->{type} eq 'character') {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3164          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        ## NOTE: Default value for both |public_id| and |system_id| attributes
3165            $self->{document}->manakai_append_text ($1);        ## are empty strings, so that we don't set any value in missing cases.
3166            ## ISSUE: DOM3 Core does not allow Document > Text        $doctype->public_id ($token->{public_identifier})
3167            unless (length $token->{data}) {            if defined $token->{public_identifier};
3168              ## Stay in the phase        $doctype->system_id ($token->{system_identifier})
3169              !!!next-token;            if defined $token->{system_identifier};
3170              redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
3171          ## ISSUE: internalSubset = null??
3172          $self->{document}->append_child ($doctype);
3173          
3174          if ($token->{quirks} or $doctype_name ne 'HTML') {
3175            !!!cp ('t4');
3176            $self->{document}->manakai_compat_mode ('quirks');
3177          } elsif (defined $token->{public_identifier}) {
3178            my $pubid = $token->{public_identifier};
3179            $pubid =~ tr/a-z/A-z/;
3180            my $prefix = [
3181              "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3182              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3183              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3184              "-//IETF//DTD HTML 2.0 LEVEL 1//",
3185              "-//IETF//DTD HTML 2.0 LEVEL 2//",
3186              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3187              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3188              "-//IETF//DTD HTML 2.0 STRICT//",
3189              "-//IETF//DTD HTML 2.0//",
3190              "-//IETF//DTD HTML 2.1E//",
3191              "-//IETF//DTD HTML 3.0//",
3192              "-//IETF//DTD HTML 3.2 FINAL//",
3193              "-//IETF//DTD HTML 3.2//",
3194              "-//IETF//DTD HTML 3//",
3195              "-//IETF//DTD HTML LEVEL 0//",
3196              "-//IETF//DTD HTML LEVEL 1//",
3197              "-//IETF//DTD HTML LEVEL 2//",
3198              "-//IETF//DTD HTML LEVEL 3//",
3199              "-//IETF//DTD HTML STRICT LEVEL 0//",
3200              "-//IETF//DTD HTML STRICT LEVEL 1//",
3201              "-//IETF//DTD HTML STRICT LEVEL 2//",
3202              "-//IETF//DTD HTML STRICT LEVEL 3//",
3203              "-//IETF//DTD HTML STRICT//",
3204              "-//IETF//DTD HTML//",
3205              "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3206              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3207              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3208              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3209              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3210              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3211              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3212              "-//NETSCAPE COMM. CORP.//DTD HTML//",
3213              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3214              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3215              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3216              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3217              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3218              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3219              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3220              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3221              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3222              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3223              "-//W3C//DTD HTML 3 1995-03-24//",
3224              "-//W3C//DTD HTML 3.2 DRAFT//",
3225              "-//W3C//DTD HTML 3.2 FINAL//",
3226              "-//W3C//DTD HTML 3.2//",
3227              "-//W3C//DTD HTML 3.2S DRAFT//",
3228              "-//W3C//DTD HTML 4.0 FRAMESET//",
3229              "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3230              "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3231              "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3232              "-//W3C//DTD W3 HTML//",
3233              "-//W3O//DTD W3 HTML 3.0//",
3234              "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3235              "-//WEBTECHS//DTD MOZILLA HTML//",
3236            ]; # $prefix
3237            my $match;
3238            for (@$prefix) {
3239              if (substr ($prefix, 0, length $_) eq $_) {
3240                $match = 1;
3241                last;
3242              }
3243            }
3244            if ($match or
3245                $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3246                $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3247                $pubid eq "HTML") {
3248              !!!cp ('t5');
3249              $self->{document}->manakai_compat_mode ('quirks');
3250            } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3251                     $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3252              if (defined $token->{system_identifier}) {
3253                !!!cp ('t6');
3254                $self->{document}->manakai_compat_mode ('quirks');
3255              } else {
3256                !!!cp ('t7');
3257                $self->{document}->manakai_compat_mode ('limited quirks');
3258            }            }
3259            } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3260                     $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3261              !!!cp ('t8');
3262              $self->{document}->manakai_compat_mode ('limited quirks');
3263            } else {
3264              !!!cp ('t9');
3265            }
3266          } else {
3267            !!!cp ('t10');
3268          }
3269          if (defined $token->{system_identifier}) {
3270            my $sysid = $token->{system_identifier};
3271            $sysid =~ tr/A-Z/a-z/;
3272            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3273              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3274              ## marked as quirks.
3275              $self->{document}->manakai_compat_mode ('quirks');
3276              !!!cp ('t11');
3277            } else {
3278              !!!cp ('t12');
3279            }
3280          } else {
3281            !!!cp ('t13');
3282          }
3283          
3284          ## Go to the "before html" insertion mode.
3285          !!!next-token;
3286          return;
3287        } elsif ({
3288                  START_TAG_TOKEN, 1,
3289                  END_TAG_TOKEN, 1,
3290                  END_OF_FILE_TOKEN, 1,
3291                 }->{$token->{type}}) {
3292          !!!cp ('t14');
3293          !!!parse-error (type => 'no DOCTYPE', token => $token);
3294          $self->{document}->manakai_compat_mode ('quirks');
3295          ## Go to the "before html" insertion mode.
3296          ## reprocess
3297          !!!ack-later;
3298          return;
3299        } elsif ($token->{type} == CHARACTER_TOKEN) {
3300          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3301            ## Ignore the token
3302    
3303            unless (length $token->{data}) {
3304              !!!cp ('t15');
3305              ## Stay in the insertion mode.
3306              !!!next-token;
3307              redo INITIAL;
3308            } else {
3309              !!!cp ('t16');
3310          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
3311        } else {        } else {
3312          die "$0: $token->{type}: Unknown token";          !!!cp ('t17');
3313        }        }
3314      } # B  
3315          !!!parse-error (type => 'no DOCTYPE', token => $token);
3316          $self->{document}->manakai_compat_mode ('quirks');
3317          ## Go to the "before html" insertion mode.
3318          ## reprocess
3319          return;
3320        } elsif ($token->{type} == COMMENT_TOKEN) {
3321          !!!cp ('t18');
3322          my $comment = $self->{document}->create_comment ($token->{data});
3323          $self->{document}->append_child ($comment);
3324          
3325          ## Stay in the insertion mode.
3326          !!!next-token;
3327          redo INITIAL;
3328        } else {
3329          die "$0: $token->{type}: Unknown token type";
3330        }
3331      } # INITIAL
3332    
3333      die "$0: _tree_construction_initial: This should be never reached";
3334  } # _tree_construction_initial  } # _tree_construction_initial
3335    
3336  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3337    my $self = shift;    my $self = shift;
3338    
3339      ## NOTE: "before html" insertion mode.
3340        
3341    B: {    B: {
3342        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3343          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3344            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3345          ## Ignore the token          ## Ignore the token
3346          ## Stay in the phase          ## Stay in the insertion mode.
3347          !!!next-token;          !!!next-token;
3348          redo B;          redo B;
3349        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3350            !!!cp ('t20');
3351          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3352          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3353          ## Stay in the phase          ## Stay in the insertion mode.
3354          !!!next-token;          !!!next-token;
3355          redo B;          redo B;
3356        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3357          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3358            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
3359            ## ISSUE: DOM3 Core does not allow Document > Text  
3360            unless (length $token->{data}) {            unless (length $token->{data}) {
3361              ## Stay in the phase              !!!cp ('t21');
3362                ## Stay in the insertion mode.
3363              !!!next-token;              !!!next-token;
3364              redo B;              redo B;
3365              } else {
3366                !!!cp ('t22');
3367            }            }
3368            } else {
3369              !!!cp ('t23');
3370          }          }
3371    
3372            $self->{application_cache_selection}->(undef);
3373    
3374          #          #
3375          } elsif ($token->{type} == START_TAG_TOKEN) {
3376            if ($token->{tag_name} eq 'html') {
3377              my $root_element;
3378              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3379              $self->{document}->append_child ($root_element);
3380              push @{$self->{open_elements}},
3381                  [$root_element, $el_category->{html}];
3382    
3383              if ($token->{attributes}->{manifest}) {
3384                !!!cp ('t24');
3385                $self->{application_cache_selection}
3386                    ->($token->{attributes}->{manifest}->{value});
3387                ## ISSUE: Spec is unclear on relative references.
3388                ## According to Hixie (#whatwg 2008-03-19), it should be
3389                ## resolved against the base URI of the document in HTML
3390                ## or xml:base of the element in XHTML.
3391              } else {
3392                !!!cp ('t25');
3393                $self->{application_cache_selection}->(undef);
3394              }
3395    
3396              !!!nack ('t25c');
3397    
3398              !!!next-token;
3399              return; ## Go to the "before head" insertion mode.
3400            } else {
3401              !!!cp ('t25.1');
3402              #
3403            }
3404        } elsif ({        } elsif ({
3405                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3406                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3407                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3408          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3409          #          #
3410        } else {        } else {
3411          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3412        }        }
3413        my $root_element; !!!create-element ($root_element, 'html');  
3414        $self->{document}->append_child ($root_element);      my $root_element;
3415        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3416        #$phase = 'main';      $self->{document}->append_child ($root_element);
3417        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3418        #redo B;  
3419        return;      $self->{application_cache_selection}->(undef);
3420    
3421        ## NOTE: Reprocess the token.
3422        !!!ack-later;
3423        return; ## Go to the "before head" insertion mode.
3424    
3425        ## ISSUE: There is an issue in the spec
3426    } # B    } # B
3427    
3428      die "$0: _tree_construction_root_element: This should never be reached";
3429  } # _tree_construction_root_element  } # _tree_construction_root_element
3430    
3431  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1732  sub _reset_insertion_mode ($) { Line 3440  sub _reset_insertion_mode ($) {
3440            
3441      ## Step 3      ## Step 3
3442      S3: {      S3: {
3443        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3444        if (defined $self->{inner_html_node}) {          $last = 1;
3445          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
3446              $self->{inner_html_node}->[1] eq 'th') {            !!!cp ('t28');
3447              $node = $self->{inner_html_node};
3448            } else {
3449              die "_reset_insertion_mode: t27";
3450            }
3451          }
3452          
3453          ## Step 4..14
3454          my $new_mode;
3455          if ($node->[1] & FOREIGN_EL) {
3456            !!!cp ('t28.1');
3457            ## NOTE: Strictly spaking, the line below only applies to MathML and
3458            ## SVG elements.  Currently the HTML syntax supports only MathML and
3459            ## SVG elements as foreigners.
3460            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3461          } elsif ($node->[1] & TABLE_CELL_EL) {
3462            if ($last) {
3463              !!!cp ('t28.2');
3464            #            #
3465          } else {          } else {
3466            $node = $self->{inner_html_node};            !!!cp ('t28.3');
3467              $new_mode = IN_CELL_IM;
3468          }          }
3469          } else {
3470            !!!cp ('t28.4');
3471            $new_mode = {
3472                          select => IN_SELECT_IM,
3473                          ## NOTE: |option| and |optgroup| do not set
3474                          ## insertion mode to "in select" by themselves.
3475                          tr => IN_ROW_IM,
3476                          tbody => IN_TABLE_BODY_IM,
3477                          thead => IN_TABLE_BODY_IM,
3478                          tfoot => IN_TABLE_BODY_IM,
3479                          caption => IN_CAPTION_IM,
3480                          colgroup => IN_COLUMN_GROUP_IM,
3481                          table => IN_TABLE_IM,
3482                          head => IN_BODY_IM, # not in head!
3483                          body => IN_BODY_IM,
3484                          frameset => IN_FRAMESET_IM,
3485                         }->{$node->[0]->manakai_local_name};
3486        }        }
       
       ## Step 4..13  
       my $new_mode = {  
                       select => 'in select',  
                       td => 'in cell',  
                       th => 'in cell',  
                       tr => 'in row',  
                       tbody => 'in table body',  
                       thead => 'in table head',  
                       tfoot => 'in table foot',  
                       caption => 'in caption',  
                       colgroup => 'in column group',  
                       table => 'in table',  
                       head => 'in body', # not in head!  
                       body => 'in body',  
                       frameset => 'in frameset',  
                      }->{$node->[1]};  
3487        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3488                
3489        ## Step 14        ## Step 15
3490        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3491          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3492            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3493              $self->{insertion_mode} = BEFORE_HEAD_IM;
3494          } else {          } else {
3495            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3496              !!!cp ('t30');
3497              $self->{insertion_mode} = AFTER_HEAD_IM;
3498          }          }
3499          return;          return;
3500          } else {
3501            !!!cp ('t31');
3502        }        }
3503                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3504        ## Step 16        ## Step 16
3505          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3506          
3507          ## Step 17
3508        $i--;        $i--;
3509        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3510                
3511        ## Step 17        ## Step 18
3512        redo S3;        redo S3;
3513      } # S3      } # S3
3514    
3515      die "$0: _reset_insertion_mode: This line should never be reached";
3516  } # _reset_insertion_mode  } # _reset_insertion_mode
3517    
3518  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3519    my $self = shift;    my $self = shift;
3520    
   my $phase = 'main';  
   
3521    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3522    
3523    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1803  sub _tree_construction_main ($) { Line 3534  sub _tree_construction_main ($) {
3534      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3535      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3536        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3537            !!!cp ('t32');
3538          return;          return;
3539        }        }
3540      }      }
# Line 1817  sub _tree_construction_main ($) { Line 3549  sub _tree_construction_main ($) {
3549    
3550        ## Step 6        ## Step 6
3551        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3552            !!!cp ('t33_1');
3553          #          #
3554        } else {        } else {
3555          my $in_open_elements;          my $in_open_elements;
3556          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3557            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3558                !!!cp ('t33');
3559              $in_open_elements = 1;              $in_open_elements = 1;
3560              last OE;              last OE;
3561            }            }
3562          }          }
3563          if ($in_open_elements) {          if ($in_open_elements) {
3564              !!!cp ('t34');
3565            #            #
3566          } else {          } else {
3567              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3568              !!!cp ('t35');
3569            redo S4;            redo S4;
3570          }          }
3571        }        }
# Line 1851  sub _tree_construction_main ($) { Line 3588  sub _tree_construction_main ($) {
3588    
3589        ## Step 11        ## Step 11
3590        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3591            !!!cp ('t36');
3592          ## Step 7'          ## Step 7'
3593          $i++;          $i++;
3594          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3595                    
3596          redo S7;          redo S7;
3597        }        }
3598    
3599          !!!cp ('t37');
3600      } # S7      } # S7
3601    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3602    
3603    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3604      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3605        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3606            !!!cp ('t38');
3607          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3608          return;          return;
3609        }        }
3610      }      }
3611    
3612        !!!cp ('t39');
3613    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3614    
3615    my $style_start_tag = sub {    my $insert;
3616      my $style_el; !!!create-element ($style_el, 'style');  
3617      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
3618      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
3619       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
3620        ->append_child ($style_el);      ## Step 1
3621      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
3622                      my $el;
3623        !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3624    
3625        ## Step 2
3626        $insert->($el);
3627    
3628        ## Step 3
3629        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3630        delete $self->{escape}; # MUST
3631    
3632        ## Step 4
3633      my $text = '';      my $text = '';
3634        !!!nack ('t40.1');
3635      !!!next-token;      !!!next-token;
3636      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3637          !!!cp ('t40');
3638        $text .= $token->{data};        $text .= $token->{data};
3639        !!!next-token;        !!!next-token;
3640      } # stop if non-character token or tokenizer stops tokenising      }
3641    
3642        ## Step 5
3643      if (length $text) {      if (length $text) {
3644        $style_el->manakai_append_text ($text);        !!!cp ('t41');
3645          my $text = $self->{document}->create_text_node ($text);
3646          $el->append_child ($text);
3647      }      }
3648        
3649      $self->{content_model_flag} = 'PCDATA';      ## Step 6
3650                      $self->{content_model} = PCDATA_CONTENT_MODEL;
3651      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
3652        ## Step 7
3653        if ($token->{type} == END_TAG_TOKEN and
3654            $token->{tag_name} eq $start_tag_name) {
3655          !!!cp ('t42');
3656        ## Ignore the token        ## Ignore the token
3657      } else {      } else {
3658        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
3659        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3660            !!!cp ('t43');
3661            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3662          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3663            !!!cp ('t44');
3664            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3665          } else {
3666            die "$0: $content_model_flag in parse_rcdata";
3667          }
3668      }      }
3669      !!!next-token;      !!!next-token;
3670    }; # $style_start_tag    }; # $parse_rcdata
3671    
3672    my $script_start_tag = sub {    my $script_start_tag = sub () {
3673      my $script_el;      my $script_el;
3674      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3675      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3676    
3677      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3678        delete $self->{escape}; # MUST
3679            
3680      my $text = '';      my $text = '';
3681        !!!nack ('t45.1');
3682      !!!next-token;      !!!next-token;
3683      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3684          !!!cp ('t45');
3685        $text .= $token->{data};        $text .= $token->{data};
3686        !!!next-token;        !!!next-token;
3687      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3688      if (length $text) {      if (length $text) {
3689          !!!cp ('t46');
3690        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3691      }      }
3692                                
3693      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3694    
3695      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3696          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3697          !!!cp ('t47');
3698        ## Ignore the token        ## Ignore the token
3699      } else {      } else {
3700        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3701          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3702        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3703        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3704      }      }
3705            
3706      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3707          !!!cp ('t49');
3708        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3709      } else {      } else {
3710          !!!cp ('t50');
3711        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3712        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3713          
3714        (($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);  
3715                
3716        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
3717                
# Line 1943  sub _tree_construction_main ($) { Line 3721  sub _tree_construction_main ($) {
3721      !!!next-token;      !!!next-token;
3722    }; # $script_start_tag    }; # $script_start_tag
3723    
3724      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3725      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3726      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3727    
3728    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3729      my $tag_name = shift;      my $end_tag_token = shift;
3730        my $tag_name = $end_tag_token->{tag_name};
3731    
3732        ## NOTE: The adoption agency algorithm (AAA).
3733    
3734      FET: {      FET: {
3735        ## Step 1        ## Step 1
3736        my $formatting_element;        my $formatting_element;
3737        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3738        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3739          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3740              !!!cp ('t52');
3741              last AFE;
3742            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3743                         eq $tag_name) {
3744              !!!cp ('t51');
3745            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3746            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3747            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3748          }          }
3749        } # AFE        } # AFE
3750        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3751          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3752            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3753          ## Ignore the token          ## Ignore the token
3754          !!!next-token;          !!!next-token;
3755          return;          return;
# Line 1972  sub _tree_construction_main ($) { Line 3761  sub _tree_construction_main ($) {
3761          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3762          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3763            if ($in_scope) {            if ($in_scope) {
3764                !!!cp ('t54');
3765              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3766              last INSCOPE;              last INSCOPE;
3767            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3768              !!!parse-error;              !!!cp ('t55');
3769                !!!parse-error (type => 'unmatched end tag',
3770                                text => $token->{tag_name},
3771                                token => $end_tag_token);
3772              ## Ignore the token              ## Ignore the token
3773              !!!next-token;              !!!next-token;
3774              return;              return;
3775            }            }
3776          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3777                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3778            $in_scope = 0;            $in_scope = 0;
3779          }          }
3780        } # INSCOPE        } # INSCOPE
3781        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3782          !!!parse-error;          !!!cp ('t57');
3783            !!!parse-error (type => 'unmatched end tag',
3784                            text => $token->{tag_name},
3785                            token => $end_tag_token);
3786          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3787          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3788          return;          return;
3789        }        }
3790        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3791          !!!parse-error;          !!!cp ('t58');
3792            !!!parse-error (type => 'not closed',
3793                            text => $self->{open_elements}->[-1]->[0]
3794                                ->manakai_local_name,
3795                            token => $end_tag_token);
3796        }        }
3797                
3798        ## Step 2        ## Step 2
# Line 2002  sub _tree_construction_main ($) { Line 3800  sub _tree_construction_main ($) {
3800        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3801        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3802          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3803          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3804              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3805              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3806               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3807              !!!cp ('t59');
3808            $furthest_block = $node;            $furthest_block = $node;
3809            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3810          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3811              !!!cp ('t60');
3812            last OE;            last OE;
3813          }          }
3814        } # OE        } # OE
3815                
3816        ## Step 3        ## Step 3
3817        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3818            !!!cp ('t61');
3819          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3820          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3821          !!!next-token;          !!!next-token;
# Line 2027  sub _tree_construction_main ($) { Line 3828  sub _tree_construction_main ($) {
3828        ## Step 5        ## Step 5
3829        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3830        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3831            !!!cp ('t62');
3832          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3833        }        }
3834                
# Line 2049  sub _tree_construction_main ($) { Line 3851  sub _tree_construction_main ($) {
3851          S7S2: {          S7S2: {
3852            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3853              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3854                  !!!cp ('t63');
3855                $node_i_in_active = $_;                $node_i_in_active = $_;
3856                last S7S2;                last S7S2;
3857              }              }
# Line 2062  sub _tree_construction_main ($) { Line 3865  sub _tree_construction_main ($) {
3865                    
3866          ## Step 4          ## Step 4
3867          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3868              !!!cp ('t64');
3869            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3870          }          }
3871                    
3872          ## Step 5          ## Step 5
3873          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3874              !!!cp ('t65');
3875            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3876            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3877            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2084  sub _tree_construction_main ($) { Line 3889  sub _tree_construction_main ($) {
3889        } # S7          } # S7  
3890                
3891        ## Step 8        ## Step 8
3892        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3893            my $foster_parent_element;
3894            my $next_sibling;
3895            OE: for (reverse 0..$#{$self->{open_elements}}) {
3896              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3897                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3898                                 if (defined $parent and $parent->node_type == 1) {
3899                                   !!!cp ('t65.1');
3900                                   $foster_parent_element = $parent;
3901                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3902                                 } else {
3903                                   !!!cp ('t65.2');
3904                                   $foster_parent_element
3905                                     = $self->{open_elements}->[$_ - 1]->[0];
3906                                 }
3907                                 last OE;
3908                               }
3909                             } # OE
3910                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3911                               unless defined $foster_parent_element;
3912            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3913            $open_tables->[-1]->[1] = 1; # tainted
3914          } else {
3915            !!!cp ('t65.3');
3916            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3917          }
3918                
3919        ## Step 9        ## Step 9
3920        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2101  sub _tree_construction_main ($) { Line 3931  sub _tree_construction_main ($) {
3931        my $i;        my $i;
3932        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3933          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3934              !!!cp ('t66');
3935            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3936            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3937          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3938              !!!cp ('t67');
3939            $i = $_;            $i = $_;
3940          }          }
3941        } # AFE        } # AFE
# Line 2113  sub _tree_construction_main ($) { Line 3945  sub _tree_construction_main ($) {
3945        undef $i;        undef $i;
3946        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3947          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3948              !!!cp ('t68');
3949            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3950            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3951          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3952              !!!cp ('t69');
3953            $i = $_;            $i = $_;
3954          }          }
3955        } # OE        } # OE
# Line 2126  sub _tree_construction_main ($) { Line 3960  sub _tree_construction_main ($) {
3960      } # FET      } # FET
3961    }; # $formatting_end_tag    }; # $formatting_end_tag
3962    
3963    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3964      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3965    }; # $insert_to_current    }; # $insert_to_current
3966    
3967    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3968                         my $child = shift;      my $child = shift;
3969                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3970                              table => 1, tbody => 1, tfoot => 1,        # MUST
3971                              thead => 1, tr => 1,        my $foster_parent_element;
3972                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3973                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3974                           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') {  
3975                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3976                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3977                                   !!!cp ('t70');
3978                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3979                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3980                               } else {                               } else {
3981                                   !!!cp ('t71');
3982                                 $foster_parent_element                                 $foster_parent_element
3983                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3984                               }                               }
# Line 2156  sub _tree_construction_main ($) { Line 3989  sub _tree_construction_main ($) {
3989                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3990                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3991                             ($child, $next_sibling);                             ($child, $next_sibling);
3992                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3993                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3994                         }        !!!cp ('t72');
3995          $self->{open_elements}->[-1]->[0]->append_child ($child);
3996        }
3997    }; # $insert_to_foster    }; # $insert_to_foster
3998    
3999    my $in_body = sub {    B: while (1) {
4000      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
4001      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
4002        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4003          $script_start_tag->();        ## Ignore the token
4004          return;        ## Stay in the phase
4005        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
4006          $style_start_tag->();        next B;
4007          return;      } elsif ($token->{type} == START_TAG_TOKEN and
4008        } elsif ({               $token->{tag_name} eq 'html') {
4009                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4010                 }->{$token->{tag_name}}) {          !!!cp ('t79');
4011          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html', text => 'html', token => $token);
4012          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
4013          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4014          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
4015          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html', text => 'html', token => $token);
4016            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4017          } else {        } else {
4018            $insert->($el);          !!!cp ('t81');
4019          }        }
4020            
4021          !!!next-token;        !!!cp ('t82');
4022          return;        !!!parse-error (type => 'not first start tag', token => $token);
4023        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
4024          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
4025          ## NOTE: There is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4026          my $title_el;            !!!cp ('t84');
4027          !!!create-element ($title_el, 'title', $token->{attributes});            $top_el->set_attribute_ns
4028          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              (undef, [undef, $attr_name],
4029            ->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;  
4030          }          }
4031                    }
4032          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!nack ('t84.1');
4033                    !!!next-token;
4034          next B;
4035        } elsif ($token->{type} == COMMENT_TOKEN) {
4036          my $comment = $self->{document}->create_comment ($token->{data});
4037          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4038            !!!cp ('t85');
4039            $self->{document}->append_child ($comment);
4040          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4041            !!!cp ('t86');
4042            $self->{open_elements}->[0]->[0]->append_child ($comment);
4043          } else {
4044            !!!cp ('t87');
4045            $self->{open_elements}->[-1]->[0]->append_child ($comment);
4046          }
4047          !!!next-token;
4048          next B;
4049        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4050          if ($token->{type} == CHARACTER_TOKEN) {
4051            !!!cp ('t87.1');
4052            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4053          !!!next-token;          !!!next-token;
4054          return;          next B;
4055        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4056          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4057            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4058            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4059              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4060                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4061              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4062              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4063              $formatting_end_tag->($token->{tag_name});            #
4064                        } elsif ({
4065              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4066                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4067                  splice @$active_formatting_elements, $_, 1;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4068                  last AFE2;                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4069                }                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4070              } # AFE2                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4071              OE: for (reverse 0..$#{$self->{open_elements}}) {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4072                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4073                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4074                  last OE;            !!!cp ('t87.2');
4075                }            !!!parse-error (type => 'not closed',
4076              } # OE                            text => $self->{open_elements}->[-1]->[0]
4077              last AFE;                                ->manakai_local_name,
4078            } elsif ($node->[0] eq '#marker') {                            token => $token);
4079              last AFE;  
4080              pop @{$self->{open_elements}}
4081                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4082    
4083              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4084              ## Reprocess.
4085              next B;
4086            } else {
4087              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4088              my $tag_name = $token->{tag_name};
4089              if ($nsuri eq $SVG_NS) {
4090                $tag_name = {
4091                   altglyph => 'altGlyph',
4092                   altglyphdef => 'altGlyphDef',
4093                   altglyphitem => 'altGlyphItem',
4094                   animatecolor => 'animateColor',
4095                   animatemotion => 'animateMotion',
4096                   animatetransform => 'animateTransform',
4097                   clippath => 'clipPath',
4098                   feblend => 'feBlend',
4099                   fecolormatrix => 'feColorMatrix',
4100                   fecomponenttransfer => 'feComponentTransfer',
4101                   fecomposite => 'feComposite',
4102                   feconvolvematrix => 'feConvolveMatrix',
4103                   fediffuselighting => 'feDiffuseLighting',
4104                   fedisplacementmap => 'feDisplacementMap',
4105                   fedistantlight => 'feDistantLight',
4106                   feflood => 'feFlood',
4107                   fefunca => 'feFuncA',
4108                   fefuncb => 'feFuncB',
4109                   fefuncg => 'feFuncG',
4110                   fefuncr => 'feFuncR',
4111                   fegaussianblur => 'feGaussianBlur',
4112                   feimage => 'feImage',
4113                   femerge => 'feMerge',
4114                   femergenode => 'feMergeNode',
4115                   femorphology => 'feMorphology',
4116                   feoffset => 'feOffset',
4117                   fepointlight => 'fePointLight',
4118                   fespecularlighting => 'feSpecularLighting',
4119                   fespotlight => 'feSpotLight',
4120                   fetile => 'feTile',
4121                   feturbulence => 'feTurbulence',
4122                   foreignobject => 'foreignObject',
4123                   glyphref => 'glyphRef',
4124                   lineargradient => 'linearGradient',
4125                   radialgradient => 'radialGradient',
4126                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4127                   textpath => 'textPath',  
4128                }->{$tag_name} || $tag_name;
4129            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4130    
4131          !!!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];  
4132    
4133          !!!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', ''];  
4134    
4135          !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4136          return;  
4137        } elsif ($token->{tag_name} eq 'marquee' or            if ($self->{self_closing}) {
4138                 $token->{tag_name} eq 'object') {              pop @{$self->{open_elements}};
4139          $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});  
4140            } else {            } else {
4141              !!!parse-error (type => 'in RCDATA:#'.$token->{type});              !!!cp ('t87.4');
4142            }            }
4143            ## ISSUE: And ignore?  
4144              !!!next-token;
4145              next B;
4146          }          }
4147          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4148          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4149        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
4150          $reconstruct_active_formatting_elements->($insert_to_current);          #
4151                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4152          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t87.6');
4153                    !!!parse-error (type => 'not closed',
4154          $self->{insertion_mode} = 'in select';                          text => $self->{open_elements}->[-1]->[0]
4155          !!!next-token;                              ->manakai_local_name,
4156          return;                          token => $token);
4157        } elsif ({  
4158                  caption => 1, col => 1, colgroup => 1, frame => 1,          pop @{$self->{open_elements}}
4159                  frameset => 1, head => 1, option => 1, optgroup => 1,              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4160                  tbody => 1, td => 1, tfoot => 1, th => 1,  
4161                  thead => 1, tr => 1,          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4162                 }->{$token->{tag_name}}) {          ## Reprocess.
4163          !!!parse-error (type => 'in body:'.$token->{tag_name});          next B;
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
4164        } else {        } else {
4165          $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;  
4166        }        }
4167      } elsif ($token->{type} eq 'end tag') {      }
4168        if ($token->{tag_name} eq 'body') {  
4169          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      if ($self->{insertion_mode} & HEAD_IMS) {
4170            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
4171            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4172              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4173            }              !!!cp ('t88.2');
4174            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4175            !!!next-token;            } else {
4176            return;              !!!cp ('t88.1');
4177          } else {              ## Ignore the token.
4178            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
4179            ## 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;  
4180            }            }
4181          } # INSCOPE            unless (length $token->{data}) {
4182                        !!!cp ('t88');
4183          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
4184            !!!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;  
4185            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4186          }          }
           
         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];  
4187    
4188          ## Step 2          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4189          S2: {            !!!cp ('t89');
4190            if ($node->[1] eq $token->{tag_name}) {            ## As if <head>
4191              ## Step 1            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4192              ## generate implied end tags            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4193              if ({            push @{$self->{open_elements}},
4194                   dd => 1, dt => 1, li => 1, p => 1,                [$self->{head_element}, $el_category->{head}];
4195                   td => 1, th => 1, tr => 1,  
4196                  }->{$self->{open_elements}->[-1]->[1]}) {            ## Reprocess in the "in head" insertion mode...
4197                !!!back-token;            pop @{$self->{open_elements}};
4198                $token = {type => 'end tag',  
4199                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST            ## Reprocess in the "after head" insertion mode...
4200                return;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4201              }            !!!cp ('t90');
4202                      ## As if </noscript>
4203              ## Step 2            pop @{$self->{open_elements}};
4204              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {            !!!parse-error (type => 'in noscript:#text', token => $token);
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
   
             !!!next-token;  
             last S2;  
           } else {  
             ## Step 3  
             if (not $formatting_category->{$node->[1]} and  
                 #not $phrasing_category->{$node->[1]} and  
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'not closed:'.$node->[1]);  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
4205                        
4206            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4207            redo S2;            ## As if </head>
4208          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
4209    
4210    B: {            ## Reprocess in the "after head" insertion mode...
4211      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4212        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
4213          !!!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]);  
         }  
4214    
4215          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4216          last B;          } else {
4217              !!!cp ('t92');
4218            }
4219    
4220          ## ISSUE: There is an issue in the spec.          ## "after head" insertion mode
4221        } else {          ## As if <body>
4222          if ($self->{insertion_mode} eq 'before head') {          !!!insert-element ('body',, $token);
4223            if ($token->{type} eq 'character') {          $self->{insertion_mode} = IN_BODY_IM;
4224              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## reprocess
4225                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          next B;
4226                unless (length $token->{data}) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4227                  !!!next-token;          if ($token->{tag_name} eq 'head') {
4228                  redo B;            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4229                }              !!!cp ('t93');
4230              }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4231              ## As if <head>              $self->{open_elements}->[-1]->[0]->append_child
4232              !!!create-element ($self->{head_element}, 'head');                  ($self->{head_element});
4233              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              push @{$self->{open_elements}},
4234              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  [$self->{head_element}, $el_category->{head}];
4235              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = IN_HEAD_IM;
4236              ## 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);  
4237              !!!next-token;              !!!next-token;
4238              redo B;              next B;
4239            } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4240              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              !!!cp ('t93.2');
4241              !!!create-element ($self->{head_element}, 'head', $attr);              !!!parse-error (type => 'after head', text => 'head',
4242              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token);
4243              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4244              $self->{insertion_mode} = 'in head';              !!!nack ('t93.3');
4245              if ($token->{tag_name} eq 'head') {              !!!next-token;
4246                !!!next-token;              next B;
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             }  
4247            } else {            } else {
4248              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4249            }              !!!parse-error (type => 'in head:head',
4250          } elsif ($self->{insertion_mode} eq 'in head') {                              token => $token); # or in head noscript
4251            if ($token->{type} eq 'character') {              ## Ignore the token
4252              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              !!!nack ('t95.1');
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
4253              !!!next-token;              !!!next-token;
4254              redo B;              next B;
4255            } elsif ($token->{type} eq 'start tag') {            }
4256              if ($token->{tag_name} eq 'title') {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4257                ## NOTE: There is an "as if in head" code clone            !!!cp ('t96');
4258                my $title_el;            ## As if <head>
4259                !!!create-element ($title_el, 'title', $token->{attributes});            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4260                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4261                  ->append_child ($title_el);            push @{$self->{open_elements}},
4262                $self->{content_model_flag} = 'RCDATA';                [$self->{head_element}, $el_category->{head}];
4263    
4264                my $text = '';            $self->{insertion_mode} = IN_HEAD_IM;
4265                !!!next-token;            ## Reprocess in the "in head" insertion mode...
4266                while ($token->{type} eq 'character') {          } else {
4267                  $text .= $token->{data};            !!!cp ('t97');
4268                  !!!next-token;          }
4269                }  
4270                if (length $text) {              if ($token->{tag_name} eq 'base') {
4271                  $title_el->manakai_append_text ($text);                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4272                }                  !!!cp ('t98');
4273                                  ## As if </noscript>
4274                $self->{content_model_flag} = 'PCDATA';                  pop @{$self->{open_elements}};
4275                    !!!parse-error (type => 'in noscript', text => 'base',
4276                                    token => $token);
4277                                
4278                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
4279                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
4280                } else {                } else {
4281                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
4282                }                }
               !!!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);  
4283    
4284                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
4285                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4286              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
4287                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head',
4288                ## Ignore the token                                  text => $token->{tag_name}, token => $token);
4289                !!!next-token;                  push @{$self->{open_elements}},
4290                redo B;                      [$self->{head_element}, $el_category->{head}];
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'head') {  
               if ($self->{open_elements}->[-1]->[1] eq 'head') {  
                 pop @{$self->{open_elements}};  
4291                } else {                } else {
4292                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
4293                }                }
4294                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4295                !!!next-token;                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4296                redo B;                pop @{$self->{open_elements}} # <head>
4297              } elsif ($token->{tag_name} eq 'html') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4298                #                !!!nack ('t101.1');
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
4299                !!!next-token;                !!!next-token;
4300                redo B;                next B;
4301              }              } elsif ($token->{tag_name} eq 'link') {
4302            } else {                ## NOTE: There is a "as if in head" code clone.
4303              #                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4304            }                  !!!cp ('t102');
4305                    !!!parse-error (type => 'after head',
4306            if ($self->{open_elements}->[-1]->[1] eq 'head') {                                  text => $token->{tag_name}, token => $token);
4307              ## As if </head>                  push @{$self->{open_elements}},
4308              pop @{$self->{open_elements}};                      [$self->{head_element}, $el_category->{head}];
4309            }                } else {
4310            $self->{insertion_mode} = 'after head';                  !!!cp ('t103');
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
4311                }                }
4312              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4313                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4314              #                pop @{$self->{open_elements}} # <head>
4315            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4316              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';  
4317                !!!next-token;                !!!next-token;
4318                redo B;                next B;
4319              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
4320                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
4321                        script => 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4322                       }->{$token->{tag_name}}) {                  !!!cp ('t104');
4323                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4324                $self->{insertion_mode} = 'in head';                                  text => $token->{tag_name}, token => $token);
4325                ## reprocess                  push @{$self->{open_elements}},
4326                redo B;                      [$self->{head_element}, $el_category->{head}];
4327              } else {                } else {
4328                #                  !!!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;  
4329                }                }
4330              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4331                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4332    
4333              !!!parse-error (type => 'in table:#character');                unless ($self->{confident}) {
4334                    if ($token->{attributes}->{charset}) {
4335              ## As if in body, but insert into foster parent element                    !!!cp ('t106');
4336              ## ISSUE: Spec says that "whenever a node would be inserted                    ## NOTE: Whether the encoding is supported or not is handled
4337              ## into the current node" while characters might not be                    ## in the {change_encoding} callback.
4338              ## result in a new Text node.                    $self->{change_encoding}
4339              $reconstruct_active_formatting_elements->($insert_to_foster);                        ->($self, $token->{attributes}->{charset}->{value},
4340                                         $token);
4341              if ({                    
4342                   table => 1, tbody => 1, tfoot => 1,                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4343                   thead => 1, tr => 1,                        ->set_user_data (manakai_has_reference =>
4344                  }->{$self->{open_elements}->[-1]->[1]}) {                                             $token->{attributes}->{charset}
4345                # MUST                                                 ->{has_reference});
4346                my $foster_parent_element;                  } elsif ($token->{attributes}->{content}) {
4347                my $next_sibling;                    if ($token->{attributes}->{content}->{value}
4348                my $prev_sibling;                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4349                OE: for (reverse 0..$#{$self->{open_elements}}) {                            [\x09-\x0D\x20]*=
4350                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4351                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4352                    if (defined $parent and $parent->node_type == 1) {                      !!!cp ('t107');
4353                      $foster_parent_element = $parent;                      ## NOTE: Whether the encoding is supported or not is handled
4354                      $next_sibling = $self->{open_elements}->[$_]->[0];                      ## in the {change_encoding} callback.
4355                      $prev_sibling = $next_sibling->previous_sibling;                      $self->{change_encoding}
4356                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4357                               $token);
4358                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4359                            ->set_user_data (manakai_has_reference =>
4360                                                 $token->{attributes}->{content}
4361                                                       ->{has_reference});
4362                    } else {                    } else {
4363                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
4364                    }                    }
                   last OE;  
4365                  }                  }
               } # 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});  
4366                } else {                } else {
4367                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
4368                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
4369                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4370                }                        ->set_user_data (manakai_has_reference =>
4371              } else {                                             $token->{attributes}->{charset}
4372                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
4373              }                  }
4374                                if ($token->{attributes}->{content}) {
4375              !!!next-token;                    !!!cp ('t110');
4376              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4377            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
4378              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
4379              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
4380              !!!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}};  
4381                }                }
4382    
4383                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
4384                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4385                  !!!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}};  
4386                !!!next-token;                !!!next-token;
4387                redo B;                next B;
4388              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
4389                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4390                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
4391                       }->{$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]);  
4392                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4393                    !!!parse-error (type => 'in noscript', text => 'title',
4394                                    token => $token);
4395                  
4396                    $self->{insertion_mode} = IN_HEAD_IM;
4397                    ## Reprocess in the "in head" insertion mode...
4398                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4399                    !!!cp ('t112');
4400                    !!!parse-error (type => 'after head',
4401                                    text => $token->{tag_name}, token => $token);
4402                    push @{$self->{open_elements}},
4403                        [$self->{head_element}, $el_category->{head}];
4404                  } else {
4405                    !!!cp ('t113');
4406                }                }
4407    
4408                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
4409                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
4410                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
4411                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4412                redo B;                pop @{$self->{open_elements}} # <head>
4413              } elsif ($token->{tag_name} eq 'table') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4414                ## NOTE: There are code clones for this "table in table"                next B;
4415                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } elsif ($token->{tag_name} eq 'style' or
4416                         $token->{tag_name} eq 'noframes') {
4417                ## As if </table>                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4418                ## have a table element in table scope                ## insertion mode IN_HEAD_IM)
4419                my $i;                ## NOTE: There is a "as if in head" code clone.
4420                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4421                  my $node = $self->{open_elements}->[$_];                  !!!cp ('t114');
4422                  if ($node->[1] eq 'table') {                  !!!parse-error (type => 'after head',
4423                    $i = $_;                                  text => $token->{tag_name}, token => $token);
4424                    last INSCOPE;                  push @{$self->{open_elements}},
4425                  } elsif ({                      [$self->{head_element}, $el_category->{head}];
4426                            table => 1, html => 1,                } else {
4427                           }->{$node->[1]}) {                  !!!cp ('t115');
4428                    last INSCOPE;                }
4429                  }                $parse_rcdata->(CDATA_CONTENT_MODEL);
4430                } # INSCOPE                pop @{$self->{open_elements}} # <head>
4431                unless (defined $i) {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4432                  !!!parse-error (type => 'unmatched end tag:table');                next B;
4433                  ## Ignore tokens </table><table>              } elsif ($token->{tag_name} eq 'noscript') {
4434                  if ($self->{insertion_mode} == IN_HEAD_IM) {
4435                    !!!cp ('t116');
4436                    ## NOTE: and scripting is disalbed
4437                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4438                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4439                    !!!nack ('t116.1');
4440                  !!!next-token;                  !!!next-token;
4441                  redo B;                  next B;
4442                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4443                    !!!cp ('t117');
4444                    !!!parse-error (type => 'in noscript', text => 'noscript',
4445                                    token => $token);
4446                    ## Ignore the token
4447                    !!!nack ('t117.1');
4448                    !!!next-token;
4449                    next B;
4450                  } else {
4451                    !!!cp ('t118');
4452                    #
4453                }                }
4454                } elsif ($token->{tag_name} eq 'script') {
4455                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4456                    !!!cp ('t119');
4457                    ## As if </noscript>
4458                    pop @{$self->{open_elements}};
4459                    !!!parse-error (type => 'in noscript', text => 'script',
4460                                    token => $token);
4461                                
4462                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
4463                if ({                  ## Reprocess in the "in head" insertion mode...
4464                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4465                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
4466                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head',
4467                  !!!back-token; # <table>                                  text => $token->{tag_name}, token => $token);
4468                  $token = {type => 'end tag', tag_name => 'table'};                  push @{$self->{open_elements}},
4469                  !!!back-token;                      [$self->{head_element}, $el_category->{head}];
4470                  $token = {type => 'end tag',                } else {
4471                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!cp ('t121');
                 redo B;  
4472                }                }
4473    
4474                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
4475                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
4476                  pop @{$self->{open_elements}} # <head>
4477                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4478                  next B;
4479                } elsif ($token->{tag_name} eq 'body' or
4480                         $token->{tag_name} eq 'frameset') {
4481                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4482                    !!!cp ('t122');
4483                    ## As if </noscript>
4484                    pop @{$self->{open_elements}};
4485                    !!!parse-error (type => 'in noscript',
4486                                    text => $token->{tag_name}, token => $token);
4487                    
4488                    ## Reprocess in the "in head" insertion mode...
4489                    ## As if </head>
4490                    pop @{$self->{open_elements}};
4491                    
4492                    ## Reprocess in the "after head" insertion mode...
4493                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4494                    !!!cp ('t124');
4495                    pop @{$self->{open_elements}};
4496                    
4497                    ## Reprocess in the "after head" insertion mode...
4498                  } else {
4499                    !!!cp ('t125');
4500                }                }
4501    
4502                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
4503                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4504                  if ($token->{tag_name} eq 'body') {
4505                    !!!cp ('t126');
4506                    $self->{insertion_mode} = IN_BODY_IM;
4507                  } elsif ($token->{tag_name} eq 'frameset') {
4508                    !!!cp ('t127');
4509                    $self->{insertion_mode} = IN_FRAMESET_IM;
4510                  } else {
4511                    die "$0: tag name: $self->{tag_name}";
4512                  }
4513                  !!!nack ('t127.1');
4514                  !!!next-token;
4515                  next B;
4516                } else {
4517                  !!!cp ('t128');
4518                  #
4519                }
4520    
4521                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4522                  !!!cp ('t129');
4523                  ## As if </noscript>
4524                  pop @{$self->{open_elements}};
4525                  !!!parse-error (type => 'in noscript:/',
4526                                  text => $token->{tag_name}, token => $token);
4527                  
4528                  ## Reprocess in the "in head" insertion mode...
4529                  ## As if </head>
4530                  pop @{$self->{open_elements}};
4531    
4532                ## reprocess                ## Reprocess in the "after head" insertion mode...
4533                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4534                  !!!cp ('t130');
4535                  ## As if </head>
4536                  pop @{$self->{open_elements}};
4537    
4538                  ## Reprocess in the "after head" insertion mode...
4539              } else {              } else {
4540                #                !!!cp ('t131');
4541              }              }
4542            } elsif ($token->{type} eq 'end tag') {  
4543              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
4544                ## have a table element in table scope              ## As if <body>
4545                my $i;              !!!insert-element ('body',, $token);
4546                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
4547                  my $node = $self->{open_elements}->[$_];              ## reprocess
4548                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
4549                    $i = $_;              next B;
4550                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
4551                  } elsif ({              if ($token->{tag_name} eq 'head') {
4552                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4553                           }->{$node->[1]}) {                  !!!cp ('t132');
4554                    last INSCOPE;                  ## As if <head>
4555                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4556                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4557                unless (defined $i) {                  push @{$self->{open_elements}},
4558                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
4559    
4560                    ## Reprocess in the "in head" insertion mode...
4561                    pop @{$self->{open_elements}};
4562                    $self->{insertion_mode} = AFTER_HEAD_IM;
4563                    !!!next-token;
4564                    next B;
4565                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4566                    !!!cp ('t133');
4567                    ## As if </noscript>
4568                    pop @{$self->{open_elements}};
4569                    !!!parse-error (type => 'in noscript:/',
4570                                    text => 'head', token => $token);
4571                    
4572                    ## Reprocess in the "in head" insertion mode...
4573                    pop @{$self->{open_elements}};
4574                    $self->{insertion_mode} = AFTER_HEAD_IM;
4575                    !!!next-token;
4576                    next B;
4577                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4578                    !!!cp ('t134');
4579                    pop @{$self->{open_elements}};
4580                    $self->{insertion_mode} = AFTER_HEAD_IM;
4581                    !!!next-token;
4582                    next B;
4583                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4584                    !!!cp ('t134.1');
4585                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4586                                    token => $token);
4587                  ## Ignore the token                  ## Ignore the token
4588                  !!!next-token;                  !!!next-token;
4589                  redo B;                  next B;
4590                  } else {
4591                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4592                }                }
4593                              } elsif ($token->{tag_name} eq 'noscript') {
4594                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4595                if ({                  !!!cp ('t136');
4596                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}};
4597                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
4598                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!next-token;
4599                  !!!back-token;                  next B;
4600                  $token = {type => 'end tag',                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4601                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4602                  redo B;                  !!!cp ('t137');
4603                    !!!parse-error (type => 'unmatched end tag',
4604                                    text => 'noscript', token => $token);
4605                    ## Ignore the token ## ISSUE: An issue in the spec.
4606                    !!!next-token;
4607                    next B;
4608                  } else {
4609                    !!!cp ('t138');
4610                    #
4611                }                }
4612                } elsif ({
4613                if ($self->{open_elements}->[-1]->[1] ne 'table') {                        body => 1, html => 1,
4614                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                       }->{$token->{tag_name}}) {
4615                  if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4616                      $self->{insertion_mode} == IN_HEAD_IM or
4617                      $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4618                    !!!cp ('t140');
4619                    !!!parse-error (type => 'unmatched end tag',
4620                                    text => $token->{tag_name}, token => $token);
4621                    ## Ignore the token
4622                    !!!next-token;
4623                    next B;
4624                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4625                    !!!cp ('t140.1');
4626                    !!!parse-error (type => 'unmatched end tag',
4627                                    text => $token->{tag_name}, token => $token);
4628                    ## Ignore the token
4629                    !!!next-token;
4630                    next B;
4631                  } else {
4632                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4633                }                }
4634                } elsif ($token->{tag_name} eq 'p') {
4635                  !!!cp ('t142');
4636                  !!!parse-error (type => 'unmatched end tag',
4637                                  text => $token->{tag_name}, token => $token);
4638                  ## Ignore the token
4639                  !!!next-token;
4640                  next B;
4641                } elsif ($token->{tag_name} eq 'br') {
4642                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4643                    !!!cp ('t142.2');
4644                    ## (before head) as if <head>, (in head) as if </head>
4645                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4646                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4647                    $self->{insertion_mode} = AFTER_HEAD_IM;
4648      
4649                    ## Reprocess in the "after head" insertion mode...
4650                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4651                    !!!cp ('t143.2');
4652                    ## As if </head>
4653                    pop @{$self->{open_elements}};
4654                    $self->{insertion_mode} = AFTER_HEAD_IM;
4655      
4656                    ## Reprocess in the "after head" insertion mode...
4657                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4658                    !!!cp ('t143.3');
4659                    ## ISSUE: Two parse errors for <head><noscript></br>
4660                    !!!parse-error (type => 'unmatched end tag',
4661                                    text => 'br', token => $token);
4662                    ## As if </noscript>
4663                    pop @{$self->{open_elements}};
4664                    $self->{insertion_mode} = IN_HEAD_IM;
4665    
4666                splice @{$self->{open_elements}}, $i;                  ## Reprocess in the "in head" insertion mode...
4667                    ## As if </head>
4668                    pop @{$self->{open_elements}};
4669                    $self->{insertion_mode} = AFTER_HEAD_IM;
4670    
4671                $self->_reset_insertion_mode;                  ## Reprocess in the "after head" insertion mode...
4672                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4673                    !!!cp ('t143.4');
4674                    #
4675                  } else {
4676                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4677                  }
4678    
4679                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4680                  !!!parse-error (type => 'unmatched end tag',
4681                                  text => 'br', token => $token);
4682                  ## Ignore the token
4683                !!!next-token;                !!!next-token;
4684                redo B;                next B;
4685              } elsif ({              } else {
4686                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t145');
4687                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                !!!parse-error (type => 'unmatched end tag',
4688                        thead => 1, tr => 1,                                text => $token->{tag_name}, token => $token);
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4689                ## Ignore the token                ## Ignore the token
4690                !!!next-token;                !!!next-token;
4691                redo B;                next B;
4692                }
4693    
4694                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4695                  !!!cp ('t146');
4696                  ## As if </noscript>
4697                  pop @{$self->{open_elements}};
4698                  !!!parse-error (type => 'in noscript:/',
4699                                  text => $token->{tag_name}, token => $token);
4700                  
4701                  ## Reprocess in the "in head" insertion mode...
4702                  ## As if </head>
4703                  pop @{$self->{open_elements}};
4704    
4705                  ## Reprocess in the "after head" insertion mode...
4706                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4707                  !!!cp ('t147');
4708                  ## As if </head>
4709                  pop @{$self->{open_elements}};
4710    
4711                  ## Reprocess in the "after head" insertion mode...
4712                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4713    ## ISSUE: This case cannot be reached?
4714                  !!!cp ('t148');
4715                  !!!parse-error (type => 'unmatched end tag',
4716                                  text => $token->{tag_name}, token => $token);
4717                  ## Ignore the token ## ISSUE: An issue in the spec.
4718                  !!!next-token;
4719                  next B;
4720              } else {              } else {
4721                #                !!!cp ('t149');
4722              }              }
           } else {  
             #  
           }  
4723    
4724            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
4725            $in_body->($insert_to_foster);              ## As if <body>
4726            redo B;              !!!insert-element ('body',, $token);
4727          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
4728            if ($token->{type} eq 'character') {              ## reprocess
4729              ## NOTE: This is a code clone of "character in body".              next B;
4730          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4731            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4732              !!!cp ('t149.1');
4733    
4734              ## NOTE: As if <head>
4735              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4736              $self->{open_elements}->[-1]->[0]->append_child
4737                  ($self->{head_element});
4738              #push @{$self->{open_elements}},
4739              #    [$self->{head_element}, $el_category->{head}];
4740              #$self->{insertion_mode} = IN_HEAD_IM;
4741              ## NOTE: Reprocess.
4742    
4743              ## NOTE: As if </head>
4744              #pop @{$self->{open_elements}};
4745              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4746              ## NOTE: Reprocess.
4747              
4748              #
4749            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4750              !!!cp ('t149.2');
4751    
4752              ## NOTE: As if </head>
4753              pop @{$self->{open_elements}};
4754              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4755              ## NOTE: Reprocess.
4756    
4757              #
4758            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4759              !!!cp ('t149.3');
4760    
4761              !!!parse-error (type => 'in noscript:#eof', token => $token);
4762    
4763              ## As if </noscript>
4764              pop @{$self->{open_elements}};
4765              #$self->{insertion_mode} = IN_HEAD_IM;
4766              ## NOTE: Reprocess.
4767    
4768              ## NOTE: As if </head>
4769              pop @{$self->{open_elements}};
4770              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4771              ## NOTE: Reprocess.
4772    
4773              #
4774            } else {
4775              !!!cp ('t149.4');
4776              #
4777            }
4778    
4779            ## NOTE: As if <body>
4780            !!!insert-element ('body',, $token);
4781            $self->{insertion_mode} = IN_BODY_IM;
4782            ## NOTE: Reprocess.
4783            next B;
4784          } else {
4785            die "$0: $token->{type}: Unknown token type";
4786          }
4787    
4788              ## ISSUE: An issue in the spec.
4789        } elsif ($self->{insertion_mode} & BODY_IMS) {
4790              if ($token->{type} == CHARACTER_TOKEN) {
4791                !!!cp ('t150');
4792                ## NOTE: There is a code clone of "character in body".
4793              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4794                            
4795              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4796    
4797              !!!next-token;              !!!next-token;
4798              redo B;              next B;
4799            } 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') {  
4800              if ({              if ({
4801                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4802                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4803                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4804                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
4805                    ## have an element in table scope
4806                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
4807                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
4808                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
4809                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
4810                  my $node = $self->{open_elements}->[$_];  
4811                  if ($node->[1] eq 'caption') {                      ## Close the cell
4812                    $i = $_;                      !!!back-token; # <x>
4813                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
4814                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
4815                            table => 1, html => 1,                                line => $token->{line},
4816                           }->{$node->[1]}) {                                column => $token->{column}};
4817                    last INSCOPE;                      next B;
4818                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4819                        !!!cp ('t152');
4820                        ## ISSUE: This case can never be reached, maybe.
4821                        last;
4822                      }
4823                  }                  }
4824                } # INSCOPE  
4825                unless (defined $i) {                  !!!cp ('t153');
4826                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
4827                        text => $token->{tag_name}, token => $token);
4828                  ## Ignore the token                  ## Ignore the token
4829                    !!!nack ('t153.1');
4830                  !!!next-token;                  !!!next-token;
4831                  redo B;                  next B;
4832                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4833                                  !!!parse-error (type => 'not closed', text => 'caption',
4834                ## generate implied end tags                                  token => $token);
4835                if ({                  
4836                     dd => 1, dt => 1, li => 1, p => 1,                  ## NOTE: As if </caption>.
4837                     td => 1, th => 1, tr => 1,                  ## have a table element in table scope
4838                    }->{$self->{open_elements}->[-1]->[1]}) {                  my $i;
4839                  !!!back-token; # <?>                  INSCOPE: {
4840                  $token = {type => 'end tag', tag_name => 'caption'};                    for (reverse 0..$#{$self->{open_elements}}) {
4841                  !!!back-token;                      my $node = $self->{open_elements}->[$_];
4842                  $token = {type => 'end tag',                      if ($node->[1] & CAPTION_EL) {
4843                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        !!!cp ('t155');
4844                  redo B;                        $i = $_;
4845                }                        last INSCOPE;
4846                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
4847                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        !!!cp ('t156');
4848                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        last;
4849                }                      }
4850                      }
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
4851    
4852                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
4853                      !!!parse-error (type => 'start tag not allowed',
4854                                      text => $token->{tag_name}, token => $token);
4855                      ## Ignore the token
4856                      !!!nack ('t157.1');
4857                      !!!next-token;
4858                      next B;
4859                    } # INSCOPE
4860                    
4861                    ## generate implied end tags
4862                    while ($self->{open_elements}->[-1]->[1]
4863                               & END_TAG_OPTIONAL_EL) {
4864                      !!!cp ('t158');
4865                      pop @{$self->{open_elements}};
4866                    }
4867    
4868                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4869                redo B;                    !!!cp ('t159');
4870                      !!!parse-error (type => 'not closed',
4871                                      text => $self->{open_elements}->[-1]->[0]
4872                                          ->manakai_local_name,
4873                                      token => $token);
4874                    } else {
4875                      !!!cp ('t160');
4876                    }
4877                    
4878                    splice @{$self->{open_elements}}, $i;
4879                    
4880                    $clear_up_to_marker->();
4881                    
4882                    $self->{insertion_mode} = IN_TABLE_IM;
4883                    
4884                    ## reprocess
4885                    !!!ack-later;
4886                    next B;
4887                  } else {
4888                    !!!cp ('t161');
4889                    #
4890                  }
4891              } else {              } else {
4892                  !!!cp ('t162');
4893                #                #
4894              }              }
4895            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4896              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4897                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
4898                my $i;                  ## have an element in table scope
4899                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
4900                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4901                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4902                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4903                    last INSCOPE;                      !!!cp ('t163');
4904                  } elsif ({                      $i = $_;
4905                            table => 1, html => 1,                      last INSCOPE;
4906                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4907                    last INSCOPE;                      !!!cp ('t164');
4908                        last INSCOPE;
4909                      }
4910                    } # INSCOPE
4911                      unless (defined $i) {
4912                        !!!cp ('t165');
4913                        !!!parse-error (type => 'unmatched end tag',
4914                                        text => $token->{tag_name},
4915                                        token => $token);
4916                        ## Ignore the token
4917                        !!!next-token;
4918                        next B;
4919                      }
4920                    
4921                    ## generate implied end tags
4922                    while ($self->{open_elements}->[-1]->[1]
4923                               & END_TAG_OPTIONAL_EL) {
4924                      !!!cp ('t166');
4925                      pop @{$self->{open_elements}};
4926                  }                  }
4927                } # INSCOPE  
4928                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4929                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
4930                      !!!cp ('t167');
4931                      !!!parse-error (type => 'not closed',
4932                                      text => $self->{open_elements}->[-1]->[0]
4933                                          ->manakai_local_name,
4934                                      token => $token);
4935                    } else {
4936                      !!!cp ('t168');
4937                    }
4938                    
4939                    splice @{$self->{open_elements}}, $i;
4940                    
4941                    $clear_up_to_marker->();
4942                    
4943                    $self->{insertion_mode} = IN_ROW_IM;
4944                    
4945                    !!!next-token;
4946                    next B;
4947                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4948                    !!!cp ('t169');
4949                    !!!parse-error (type => 'unmatched end tag',
4950                                    text => $token->{tag_name}, token => $token);
4951                  ## Ignore the token                  ## Ignore the token
4952                  !!!next-token;                  !!!next-token;
4953                  redo B;                  next B;
4954                }                } else {
4955                                  !!!cp ('t170');
4956                ## 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;  
4957                }                }
4958                } elsif ($token->{tag_name} eq 'caption') {
4959                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
4960                    ## have a table element in table scope
4961                    my $i;
4962                    INSCOPE: {
4963                      for (reverse 0..$#{$self->{open_elements}}) {
4964                        my $node = $self->{open_elements}->[$_];
4965                        if ($node->[1] & CAPTION_EL) {
4966                          !!!cp ('t171');
4967                          $i = $_;
4968                          last INSCOPE;
4969                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
4970                          !!!cp ('t172');
4971                          last;
4972                        }
4973                      }
4974    
4975                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
4976                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
4977                                      text => $token->{tag_name}, token => $token);
4978                      ## Ignore the token
4979                      !!!next-token;
4980                      next B;
4981                    } # INSCOPE
4982                    
4983                    ## generate implied end tags
4984                    while ($self->{open_elements}->[-1]->[1]
4985                               & END_TAG_OPTIONAL_EL) {
4986                      !!!cp ('t174');
4987                      pop @{$self->{open_elements}};
4988                    }
4989                    
4990                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4991                      !!!cp ('t175');
4992                      !!!parse-error (type => 'not closed',
4993                                      text => $self->{open_elements}->[-1]->[0]
4994                                          ->manakai_local_name,
4995                                      token => $token);
4996                    } else {
4997                      !!!cp ('t176');
4998                    }
4999                    
5000                    splice @{$self->{open_elements}}, $i;
5001                    
5002                    $clear_up_to_marker->();
5003                    
5004                    $self->{insertion_mode} = IN_TABLE_IM;
5005                    
5006                    !!!next-token;
5007                    next B;
5008                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5009                    !!!cp ('t177');
5010                    !!!parse-error (type => 'unmatched end tag',
5011                                    text => $token->{tag_name}, token => $token);
5012                    ## Ignore the token
5013                    !!!next-token;
5014                    next B;
5015                  } else {
5016                    !!!cp ('t178');
5017                    #
5018                }                }
5019                } elsif ({
5020                          table => 1, tbody => 1, tfoot => 1,
5021                          thead => 1, tr => 1,
5022                         }->{$token->{tag_name}} and
5023                         $self->{insertion_mode} == IN_CELL_IM) {
5024                  ## have an element in table scope
5025                  my $i;
5026                  my $tn;
5027                  INSCOPE: {
5028                    for (reverse 0..$#{$self->{open_elements}}) {
5029                      my $node = $self->{open_elements}->[$_];
5030                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5031                        !!!cp ('t179');
5032                        $i = $_;
5033    
5034                        ## Close the cell
5035                        !!!back-token; # </x>
5036                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
5037                                  line => $token->{line},
5038                                  column => $token->{column}};
5039                        next B;
5040                      } elsif ($node->[1] & TABLE_CELL_EL) {
5041                        !!!cp ('t180');
5042                        $tn = $node->[0]->manakai_local_name;
5043                        ## NOTE: There is exactly one |td| or |th| element
5044                        ## in scope in the stack of open elements by definition.
5045                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5046                        ## ISSUE: Can this be reached?
5047                        !!!cp ('t181');
5048                        last;
5049                      }
5050                    }
5051    
5052                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
5053                    !!!parse-error (type => 'unmatched end tag',
5054                $clear_up_to_marker->();                      text => $token->{tag_name}, token => $token);
5055                    ## Ignore the token
5056                $self->{insertion_mode} = 'in table';                  !!!next-token;
5057                    next B;
5058                !!!next-token;                } # INSCOPE
5059                redo B;              } elsif ($token->{tag_name} eq 'table' and
5060              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5061                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5062                                  token => $token);
5063    
5064                ## As if </caption>                ## As if </caption>
5065                ## have a table element in table scope                ## have a table element in table scope
5066                my $i;                my $i;
5067                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5068                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5069                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5070                      !!!cp ('t184');
5071                    $i = $_;                    $i = $_;
5072                    last INSCOPE;                    last INSCOPE;
5073                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5074                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5075                    last INSCOPE;                    last INSCOPE;
5076                  }                  }
5077                } # INSCOPE                } # INSCOPE
5078                unless (defined $i) {                unless (defined $i) {
5079                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5080                    !!!parse-error (type => 'unmatched end tag',
5081                                    text => 'caption', token => $token);
5082                  ## Ignore the token                  ## Ignore the token
5083                  !!!next-token;                  !!!next-token;
5084                  redo B;                  next B;
5085                }                }
5086                                
5087                ## generate implied end tags                ## generate implied end tags
5088                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5089                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5090                     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;  
5091                }                }
5092    
5093                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5094                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5095                    !!!parse-error (type => 'not closed',
5096                                    text => $self->{open_elements}->[-1]->[0]
5097                                        ->manakai_local_name,
5098                                    token => $token);
5099                  } else {
5100                    !!!cp ('t189');
5101                }                }
5102    
5103                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5104    
5105                $clear_up_to_marker->();                $clear_up_to_marker->();
5106    
5107                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5108    
5109                ## reprocess                ## reprocess
5110                redo B;                next B;
5111              } elsif ({              } elsif ({
5112                        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,  
5113                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5114                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5115                ## Ignore the token                  !!!cp ('t190');
5116                redo B;                  !!!parse-error (type => 'unmatched end tag',
5117              } else {                                  text => $token->{tag_name}, token => $token);
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
5118                  ## Ignore the token                  ## Ignore the token
5119                  !!!next-token;                  !!!next-token;
5120                  redo B;                  next B;
5121                } else {                } else {
5122                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
5123                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
5124                }                }
5125              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
5126                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
5127                          thead => 1, tr => 1,
5128                         }->{$token->{tag_name}} and
5129                         $self->{insertion_mode} == IN_CAPTION_IM) {
5130                  !!!cp ('t192');
5131                  !!!parse-error (type => 'unmatched end tag',
5132                                  text => $token->{tag_name}, token => $token);
5133                ## Ignore the token                ## Ignore the token
5134                !!!next-token;                !!!next-token;
5135                redo B;                next B;
5136              } else {              } else {
5137                #                !!!cp ('t193');
5138                  #
5139              }              }
5140            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5141              #          for my $entry (@{$self->{open_elements}}) {
5142              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5143                !!!cp ('t75');
5144                !!!parse-error (type => 'in body:#eof', token => $token);
5145                last;
5146            }            }
5147            }
5148    
5149            ## As if </colgroup>          ## Stop parsing.
5150            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
5151              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
5152              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
5153          }
5154    
5155          $insert = $insert_to_current;
5156          #
5157        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5158          if ($token->{type} == CHARACTER_TOKEN) {
5159            if (not $open_tables->[-1]->[1] and # tainted
5160                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5161              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5162                  
5163              unless (length $token->{data}) {
5164                !!!cp ('t194');
5165              !!!next-token;              !!!next-token;
5166              redo B;              next B;
5167            } else {            } else {
5168              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5169            }            }
5170          } 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;  
               }  
             }  
5171    
5172              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5173    
5174              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5175              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5176              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5177              ## result in a new Text node.              ## result in a new Text node.
5178              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5179                
5180              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]}) {  
5181                # MUST                # MUST
5182                my $foster_parent_element;                my $foster_parent_element;
5183                my $next_sibling;                my $next_sibling;
5184                my $prev_sibling;                my $prev_sibling;
5185                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5186                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5187                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5188                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5189                        !!!cp ('t196');
5190                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5191                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5192                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5193                    } else {                    } else {
5194                        !!!cp ('t197');
5195                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5196                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5197                    }                    }
# Line 3674  sub _tree_construction_main ($) { Line 5203  sub _tree_construction_main ($) {
5203                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5204                if (defined $prev_sibling and                if (defined $prev_sibling and
5205                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5206                    !!!cp ('t198');
5207                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5208                } else {                } else {
5209                    !!!cp ('t199');
5210                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5211                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5212                     $next_sibling);                     $next_sibling);
5213                }                }
5214              } else {            $open_tables->[-1]->[1] = 1; # tainted
5215                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5216              !!!cp ('t200');
5217              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5218            }
5219                
5220            !!!next-token;
5221            next B;
5222          } elsif ($token->{type} == START_TAG_TOKEN) {
5223            if ({
5224                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5225                 th => 1, td => 1,
5226                }->{$token->{tag_name}}) {
5227              if ($self->{insertion_mode} == IN_TABLE_IM) {
5228                ## Clear back to table context
5229                while (not ($self->{open_elements}->[-1]->[1]
5230                                & TABLE_SCOPING_EL)) {
5231                  !!!cp ('t201');
5232                  pop @{$self->{open_elements}};
5233              }              }
5234                            
5235              !!!next-token;              !!!insert-element ('tbody',, $token);
5236              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5237            } elsif ($token->{type} eq 'comment') {              ## reprocess in the "in table body" insertion mode...
5238              ## Copied from 'in table'            }
5239              my $comment = $self->{document}->create_comment ($token->{data});            
5240              $self->{open_elements}->[-1]->[0]->append_child ($comment);            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5241              !!!next-token;              unless ($token->{tag_name} eq 'tr') {
5242              redo B;                !!!cp ('t202');
5243            } elsif ($token->{type} eq 'start tag') {                !!!parse-error (type => 'missing start tag:tr', token => $token);
5244              if ({              }
5245                   tr => 1,                  
5246                   th => 1, td => 1,              ## Clear back to table body context
5247                  }->{$token->{tag_name}}) {              while (not ($self->{open_elements}->[-1]->[1]
5248                unless ($token->{tag_name} eq 'tr') {                              & TABLE_ROWS_SCOPING_EL)) {
5249                  !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t203');
5250                  ## ISSUE: Can this case be reached?
5251                  pop @{$self->{open_elements}};
5252                }
5253                    
5254                    $self->{insertion_mode} = IN_ROW_IM;
5255                    if ($token->{tag_name} eq 'tr') {
5256                      !!!cp ('t204');
5257                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5258                      !!!nack ('t204');
5259                      !!!next-token;
5260                      next B;
5261                    } else {
5262                      !!!cp ('t205');
5263                      !!!insert-element ('tr',, $token);
5264                      ## reprocess in the "in row" insertion mode
5265                    }
5266                  } else {
5267                    !!!cp ('t206');
5268                }                }
5269    
5270                ## Clear back to table body context                ## Clear back to table row context
5271                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5272                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5273                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5274                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5275                }                }
5276                                
5277                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5278                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5279                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5280                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5281                } else {                
5282                  !!!insert-element ('tr');                !!!nack ('t207.1');
5283                  ## reprocess                !!!next-token;
5284                }                next B;
               redo B;  
5285              } elsif ({              } elsif ({
5286                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5287                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5288                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5289                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5290                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5291                my $i;                  ## As if </tr>
5292                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5293                  my $node = $self->{open_elements}->[$_];                  my $i;
5294                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5295                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5296                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5297                    $i = $_;                      !!!cp ('t208');
5298                    last INSCOPE;                      $i = $_;
5299                  } elsif ({                      last INSCOPE;
5300                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5301                           }->{$node->[1]}) {                      !!!cp ('t209');
5302                    last INSCOPE;                      last INSCOPE;
5303                      }
5304                    } # INSCOPE
5305                    unless (defined $i) {
5306                      !!!cp ('t210');
5307    ## TODO: This type is wrong.
5308                      !!!parse-error (type => 'unmacthed end tag',
5309                                      text => $token->{tag_name}, token => $token);
5310                      ## Ignore the token
5311                      !!!nack ('t210.1');
5312                      !!!next-token;
5313                      next B;
5314                    }
5315                    
5316                    ## Clear back to table row context
5317                    while (not ($self->{open_elements}->[-1]->[1]
5318                                    & TABLE_ROW_SCOPING_EL)) {
5319                      !!!cp ('t211');
5320                      ## ISSUE: Can this case be reached?
5321                      pop @{$self->{open_elements}};
5322                    }
5323                    
5324                    pop @{$self->{open_elements}}; # tr
5325                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5326                    if ($token->{tag_name} eq 'tr') {
5327                      !!!cp ('t212');
5328                      ## reprocess
5329                      !!!ack-later;
5330                      next B;
5331                    } else {
5332                      !!!cp ('t213');
5333                      ## reprocess in the "in table body" insertion mode...
5334                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5335                }                }
5336    
5337                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5338                while (not {                  ## have an element in table scope
5339                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5340                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5341                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5342                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5343                        !!!cp ('t214');
5344                        $i = $_;
5345                        last INSCOPE;
5346                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5347                        !!!cp ('t215');
5348                        last INSCOPE;
5349                      }
5350                    } # INSCOPE
5351                    unless (defined $i) {
5352                      !!!cp ('t216');
5353    ## TODO: This erorr type is wrong.
5354                      !!!parse-error (type => 'unmatched end tag',
5355                                      text => $token->{tag_name}, token => $token);
5356                      ## Ignore the token
5357                      !!!nack ('t216.1');
5358                      !!!next-token;
5359                      next B;
5360                    }
5361    
5362                    ## Clear back to table body context
5363                    while (not ($self->{open_elements}->[-1]->[1]
5364                                    & TABLE_ROWS_SCOPING_EL)) {
5365                      !!!cp ('t217');
5366                      ## ISSUE: Can this state be reached?
5367                      pop @{$self->{open_elements}};
5368                    }
5369                    
5370                    ## As if <{current node}>
5371                    ## have an element in table scope
5372                    ## true by definition
5373                    
5374                    ## Clear back to table body context
5375                    ## nop by definition
5376                    
5377                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5378                    $self->{insertion_mode} = IN_TABLE_IM;
5379                    ## reprocess in "in table" insertion mode...
5380                  } else {
5381                    !!!cp ('t218');
5382                }                }
5383    
5384                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5385                ## have an element in table scope                  ## Clear back to table context
5386                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5387                                    & TABLE_SCOPING_EL)) {
5388                ## Clear back to table body context                    !!!cp ('t219');
5389                ## nop by definition                    ## ISSUE: Can this state be reached?
5390                      pop @{$self->{open_elements}};
5391                pop @{$self->{open_elements}};                  }
5392                $self->{insertion_mode} = 'in table';                  
5393                ## reprocess                  !!!insert-element ('colgroup',, $token);
5394                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5395                    ## reprocess
5396                    !!!ack-later;
5397                    next B;
5398                  } elsif ({
5399                            caption => 1,
5400                            colgroup => 1,
5401                            tbody => 1, tfoot => 1, thead => 1,
5402                           }->{$token->{tag_name}}) {
5403                    ## Clear back to table context
5404                    while (not ($self->{open_elements}->[-1]->[1]
5405                                    & TABLE_SCOPING_EL)) {
5406                      !!!cp ('t220');
5407                      ## ISSUE: Can this state be reached?
5408                      pop @{$self->{open_elements}};
5409                    }
5410                    
5411                    push @$active_formatting_elements, ['#marker', '']
5412                        if $token->{tag_name} eq 'caption';
5413                    
5414                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5415                    $self->{insertion_mode} = {
5416                                               caption => IN_CAPTION_IM,
5417                                               colgroup => IN_COLUMN_GROUP_IM,
5418                                               tbody => IN_TABLE_BODY_IM,
5419                                               tfoot => IN_TABLE_BODY_IM,
5420                                               thead => IN_TABLE_BODY_IM,
5421                                              }->{$token->{tag_name}};
5422                    !!!next-token;
5423                    !!!nack ('t220.1');
5424                    next B;
5425                  } else {
5426                    die "$0: in table: <>: $token->{tag_name}";
5427                  }
5428              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5429                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5430                !!!parse-error (type => 'not closed:table');                                text => $self->{open_elements}->[-1]->[0]
5431                                      ->manakai_local_name,
5432                                  token => $token);
5433    
5434                ## As if </table>                ## As if </table>
5435                ## have a table element in table scope                ## have a table element in table scope
5436                my $i;                my $i;
5437                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5438                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5439                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5440                      !!!cp ('t221');
5441                    $i = $_;                    $i = $_;
5442                    last INSCOPE;                    last INSCOPE;
5443                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5444                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5445                    last INSCOPE;                    last INSCOPE;
5446                  }                  }
5447                } # INSCOPE                } # INSCOPE
5448                unless (defined $i) {                unless (defined $i) {
5449                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5450    ## TODO: The following is wrong, maybe.
5451                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5452                                    token => $token);
5453                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5454                    !!!nack ('t223.1');
5455                  !!!next-token;                  !!!next-token;
5456                  redo B;                  next B;
5457                }                }
5458                                
5459    ## TODO: Followings are removed from the latest spec.
5460                ## generate implied end tags                ## generate implied end tags
5461                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5462                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5463                     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;  
5464                }                }
5465    
5466                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5467                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5468                    ## NOTE: |<table><tr><table>|
5469                    !!!parse-error (type => 'not closed',
5470                                    text => $self->{open_elements}->[-1]->[0]
5471                                        ->manakai_local_name,
5472                                    token => $token);
5473                  } else {
5474                    !!!cp ('t226');
5475                }                }
5476    
5477                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5478                  pop @{$open_tables};
5479    
5480                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5481    
5482                ## reprocess            ## reprocess
5483                redo B;            !!!ack-later;
5484              } else {            next B;
5485                #          } elsif ($token->{tag_name} eq 'style') {
5486              }            if (not $open_tables->[-1]->[1]) { # tainted
5487            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5488              if ({              ## NOTE: This is a "as if in head" code clone.
5489                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5490                  }->{$token->{tag_name}}) {              next B;
5491                ## have an element in table scope            } else {
5492                my $i;              !!!cp ('t227.7');
5493                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5494                  my $node = $self->{open_elements}->[$_];            }
5495                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5496                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5497                    last INSCOPE;              !!!cp ('t227.6');
5498                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5499                            table => 1, html => 1,              $script_start_tag->();
5500                           }->{$node->[1]}) {              next B;
5501                    last INSCOPE;            } else {
5502                  }              !!!cp ('t227.5');
5503                } # INSCOPE              #
5504                unless (defined $i) {            }
5505                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5506                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5507                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5508                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5509                }                if ($type eq 'hidden') {
5510                    !!!cp ('t227.3');
5511                    !!!parse-error (type => 'in table',
5512                                    text => $token->{tag_name}, token => $token);
5513    
5514                ## 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}};  
               }  
5515    
5516                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;  
               }  
5517    
               ## 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]);  
5518                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
5519    
5520                ## Clear back to table body context                  !!!next-token;
5521                ## nop by definition                  !!!ack ('t227.2.1');
5522                    next B;
5523                pop @{$self->{open_elements}};                } else {
5524                $self->{insertion_mode} = 'in table';                  !!!cp ('t227.2');
5525                ## reprocess                  #
5526                redo B;                }
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
5527              } else {              } else {
5528                  !!!cp ('t227.1');
5529                #                #
5530              }              }
5531            } else {            } else {
5532                !!!cp ('t227.4');
5533              #              #
5534            }            }
5535                      } else {
5536            ## As if in table            !!!cp ('t227');
5537            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5538            $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');  
5539    
5540              ## As if in body, but insert into foster parent element          !!!parse-error (type => 'in table', text => $token->{tag_name},
5541              ## ISSUE: Spec says that "whenever a node would be inserted                          token => $token);
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
5542    
5543                push @$active_formatting_elements, ['#marker', ''];          $insert = $insert_to_foster;
5544                          #
5545                !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
5546                redo B;              if ($token->{tag_name} eq 'tr' and
5547              } 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>  
5548                ## have an element in table scope                ## have an element in table scope
5549                my $i;                my $i;
5550                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5551                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5552                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5553                      !!!cp ('t228');
5554                    $i = $_;                    $i = $_;
5555                    last INSCOPE;                    last INSCOPE;
5556                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5557                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5558                    last INSCOPE;                    last INSCOPE;
5559                  }                  }
5560                } # INSCOPE                } # INSCOPE
5561                unless (defined $i) {                unless (defined $i) {
5562                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5563                    !!!parse-error (type => 'unmatched end tag',
5564                                    text => $token->{tag_name}, token => $token);
5565                  ## Ignore the token                  ## Ignore the token
5566                    !!!nack ('t230.1');
5567                  !!!next-token;                  !!!next-token;
5568                  redo B;                  next B;
5569                  } else {
5570                    !!!cp ('t232');
5571                }                }
5572    
5573                ## Clear back to table row context                ## Clear back to table row context
5574                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5575                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5576                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5577                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5578                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5579                }                }
5580    
5581                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5582                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5583                ## reprocess                !!!next-token;
5584                redo B;                !!!nack ('t231.1');
5585                  next B;
5586              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5587                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5588                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5589                    ## have an element in table scope
5590                ## As if </table>                  my $i;
5591                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5592                my $i;                    my $node = $self->{open_elements}->[$_];
5593                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5594                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5595                  if ($node->[1] eq 'table') {                      $i = $_;
5596                    $i = $_;                      last INSCOPE;
5597                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5598                  } elsif ({                      !!!cp ('t234');
5599                            table => 1, html => 1,                      last INSCOPE;
5600                           }->{$node->[1]}) {                    }
5601                    last INSCOPE;                  } # INSCOPE
5602                    unless (defined $i) {
5603                      !!!cp ('t235');
5604    ## TODO: The following is wrong.
5605                      !!!parse-error (type => 'unmatched end tag',
5606                                      text => $token->{type}, token => $token);
5607                      ## Ignore the token
5608                      !!!nack ('t236.1');
5609                      !!!next-token;
5610                      next B;
5611                  }                  }
5612                } # INSCOPE                  
5613                unless (defined $i) {                  ## Clear back to table row context
5614                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5615                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5616                  !!!next-token;                    !!!cp ('t236');
5617                  redo B;  ## ISSUE: Can this state be reached?
5618                }                    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;  
5619                  }                  }
5620                } # INSCOPE                  
5621                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5622                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5623                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5624                  !!!next-token;                }
5625                  redo B;  
5626                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5627                    ## have an element in table scope
5628                ## Clear back to table row context                  my $i;
5629                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5630                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5631                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5632                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5633                        $i = $_;
5634                        last INSCOPE;
5635                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5636                        !!!cp ('t238');
5637                        last INSCOPE;
5638                      }
5639                    } # INSCOPE
5640                    unless (defined $i) {
5641                      !!!cp ('t239');
5642                      !!!parse-error (type => 'unmatched end tag',
5643                                      text => $token->{tag_name}, token => $token);
5644                      ## Ignore the token
5645                      !!!nack ('t239.1');
5646                      !!!next-token;
5647                      next B;
5648                    }
5649                    
5650                    ## Clear back to table body context
5651                    while (not ($self->{open_elements}->[-1]->[1]
5652                                    & TABLE_ROWS_SCOPING_EL)) {
5653                      !!!cp ('t240');
5654                      pop @{$self->{open_elements}};
5655                    }
5656                    
5657                    ## As if <{current node}>
5658                    ## have an element in table scope
5659                    ## true by definition
5660                    
5661                    ## Clear back to table body context
5662                    ## nop by definition
5663                    
5664                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5665                    $self->{insertion_mode} = IN_TABLE_IM;
5666                    ## reprocess in the "in table" insertion mode...
5667                }                }
5668    
5669                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
5670                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
5671                !!!next-token;                ## the code for <table> in the "in table" insertion mode
5672                redo B;                ## is synced with it.
5673              } elsif ($token->{tag_name} eq 'table') {  
5674                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
5675                my $i;                my $i;
5676                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5677                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5678                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
5679                      !!!cp ('t241');
5680                    $i = $_;                    $i = $_;
5681                    last INSCOPE;                    last INSCOPE;
5682                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5683                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5684                    last INSCOPE;                    last INSCOPE;
5685                  }                  }
5686                } # INSCOPE                } # INSCOPE
5687                unless (defined $i) {                unless (defined $i) {
5688                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
5689                    !!!parse-error (type => 'unmatched end tag',
5690                                    text => $token->{tag_name}, token => $token);
5691                  ## Ignore the token                  ## Ignore the token
5692                    !!!nack ('t243.1');
5693                  !!!next-token;                  !!!next-token;
5694                  redo B;                  next B;
5695                }                }
5696                    
5697                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
5698                while (not {                pop @{$open_tables};
5699                  tr => 1, html => 1,                
5700                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
5701                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
5702                  pop @{$self->{open_elements}};                !!!next-token;
5703                }                next B;
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
5704              } elsif ({              } elsif ({
5705                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5706                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
5707                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
5708                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
5709                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5710                  my $node = $self->{open_elements}->[$_];                  my $i;
5711                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5712                    $i = $_;                    my $node = $self->{open_elements}->[$_];
5713                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5714                  } elsif ({                      !!!cp ('t247');
5715                            table => 1, html => 1,                      $i = $_;
5716                           }->{$node->[1]}) {                      last INSCOPE;
5717                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5718                        !!!cp ('t248');
5719                        last INSCOPE;
5720                      }
5721                    } # INSCOPE
5722                      unless (defined $i) {
5723                        !!!cp ('t249');
5724                        !!!parse-error (type => 'unmatched end tag',
5725                                        text => $token->{tag_name}, token => $token);
5726                        ## Ignore the token
5727                        !!!nack ('t249.1');
5728                        !!!next-token;
5729                        next B;
5730                      }
5731                    
5732                    ## As if </tr>
5733                    ## have an element in table scope
5734                    my $i;
5735                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5736                      my $node = $self->{open_elements}->[$_];
5737                      if ($node->[1] & TABLE_ROW_EL) {
5738                        !!!cp ('t250');
5739                        $i = $_;
5740                        last INSCOPE;
5741                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5742                        !!!cp ('t251');
5743                        last INSCOPE;
5744                      }
5745                    } # INSCOPE
5746                      unless (defined $i) {
5747                        !!!cp ('t252');
5748                        !!!parse-error (type => 'unmatched end tag',
5749                                        text => 'tr', token => $token);
5750                        ## Ignore the token
5751                        !!!nack ('t252.1');
5752                        !!!next-token;
5753                        next B;
5754                      }
5755                    
5756                    ## Clear back to table row context
5757                    while (not ($self->{open_elements}->[-1]->[1]
5758                                    & TABLE_ROW_SCOPING_EL)) {
5759                      !!!cp ('t253');
5760    ## ISSUE: Can this case be reached?
5761                      pop @{$self->{open_elements}};
5762                  }                  }
5763                } # INSCOPE                  
5764                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5765                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5766                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
5767                }                }
5768    
               ## As if </tr>  
5769                ## have an element in table scope                ## have an element in table scope
5770                my $i;                my $i;
5771                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5772                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5773                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5774                      !!!cp ('t254');
5775                    $i = $_;                    $i = $_;
5776                    last INSCOPE;                    last INSCOPE;
5777                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5778                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5779                    last INSCOPE;                    last INSCOPE;
5780                  }                  }
5781                } # INSCOPE                } # INSCOPE
5782                unless (defined $i) {                unless (defined $i) {
5783                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
5784                    !!!parse-error (type => 'unmatched end tag',
5785                                    text => $token->{tag_name}, token => $token);
5786                  ## Ignore the token                  ## Ignore the token
5787                    !!!nack ('t256.1');
5788                  !!!next-token;                  !!!next-token;
5789                  redo B;                  next B;
5790                }                }
5791    
5792                ## Clear back to table row context                ## Clear back to table body context
5793                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5794                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5795                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5796                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5797                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5798                }                }
5799    
5800                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
5801                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
5802                ## reprocess                !!!nack ('t257.1');
5803                redo B;                !!!next-token;
5804                  next B;
5805              } elsif ({              } elsif ({
5806                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5807                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5808                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5809                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5810                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5811                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5812                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
5813                !!!next-token;                            text => $token->{tag_name}, token => $token);
5814                redo B;            ## Ignore the token
5815              } else {            !!!nack ('t258.1');
5816                #             !!!next-token;
5817              }            next B;
5818            } else {          } else {
5819              #            !!!cp ('t259');
5820            }            !!!parse-error (type => 'in table:/',
5821                              text => $token->{tag_name}, token => $token);
5822    
5823            ## As if in table            $insert = $insert_to_foster;
5824            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5825            $in_body->($insert_to_foster);          }
5826            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5827          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5828            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
5829              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
5830              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
5831                          #
5832              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5833              !!!cp ('t259.2');
5834              #
5835            }
5836    
5837              !!!next-token;          ## Stop parsing
5838              redo B;          last B;
5839            } elsif ($token->{type} eq 'comment') {        } else {
5840              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
5841              my $comment = $self->{document}->create_comment ($token->{data});        }
5842              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5843              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
5844              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5845            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5846              if ({                unless (length $token->{data}) {
5847                   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  
5848                  !!!next-token;                  !!!next-token;
5849                  redo B;                  next B;
5850                }                }
5851                }
5852                ## Close the cell              
5853                !!!back-token; # <?>              !!!cp ('t261');
5854                $token = {type => 'end tag', tag_name => $tn};              #
5855                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
5856              } else {              if ($token->{tag_name} eq 'col') {
5857                  !!!cp ('t262');
5858                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5859                  pop @{$self->{open_elements}};
5860                  !!!ack ('t262.1');
5861                  !!!next-token;
5862                  next B;
5863                } else {
5864                  !!!cp ('t263');
5865                #                #
5866              }              }
5867            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5868              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
5869                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5870                my $i;                  !!!cp ('t264');
5871                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag',
5872                  my $node = $self->{open_elements}->[$_];                                  text => 'colgroup', token => $token);
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5873                  ## Ignore the token                  ## Ignore the token
5874                  !!!next-token;                  !!!next-token;
5875                  redo B;                  next B;
5876                }                } else {
5877                                  !!!cp ('t265');
5878                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
5879                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
5880                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
5881                     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]);  
5882                }                }
5883                } elsif ($token->{tag_name} eq 'col') {
5884                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
5885                  !!!parse-error (type => 'unmatched end tag',
5886                $clear_up_to_marker->();                                text => 'col', token => $token);
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5887                ## Ignore the token                ## Ignore the token
5888                !!!next-token;                !!!next-token;
5889                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;  
5890              } else {              } else {
5891                #                !!!cp ('t267');
5892                  #
5893              }              }
5894          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5895            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5896                @{$self->{open_elements}} == 1) { # redundant, maybe
5897              !!!cp ('t270.2');
5898              ## Stop parsing.
5899              last B;
5900            } else {
5901              ## NOTE: As if </colgroup>.
5902              !!!cp ('t270.1');
5903              pop @{$self->{open_elements}}; # colgroup
5904              $self->{insertion_mode} = IN_TABLE_IM;
5905              ## Reprocess.
5906              next B;
5907            }
5908          } else {
5909            die "$0: $token->{type}: Unknown token type";
5910          }
5911    
5912              ## As if </colgroup>
5913              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5914                !!!cp ('t269');
5915    ## TODO: Wrong error type?
5916                !!!parse-error (type => 'unmatched end tag',
5917                                text => 'colgroup', token => $token);
5918                ## Ignore the token
5919                !!!nack ('t269.1');
5920                !!!next-token;
5921                next B;
5922            } else {            } else {
5923              #              !!!cp ('t270');
5924                pop @{$self->{open_elements}}; # colgroup
5925                $self->{insertion_mode} = IN_TABLE_IM;
5926                !!!ack-later;
5927                ## reprocess
5928                next B;
5929              }
5930        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5931          if ($token->{type} == CHARACTER_TOKEN) {
5932            !!!cp ('t271');
5933            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5934            !!!next-token;
5935            next B;
5936          } elsif ($token->{type} == START_TAG_TOKEN) {
5937            if ($token->{tag_name} eq 'option') {
5938              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5939                !!!cp ('t272');
5940                ## As if </option>
5941                pop @{$self->{open_elements}};
5942              } else {
5943                !!!cp ('t273');
5944            }            }
             
           $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}};  
               }  
5945    
5946                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5947                !!!next-token;            !!!nack ('t273.1');
5948                redo B;            !!!next-token;
5949              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5950                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5951                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5952                  pop @{$self->{open_elements}};              !!!cp ('t274');
5953                }              ## As if </option>
5954                pop @{$self->{open_elements}};
5955              } else {
5956                !!!cp ('t275');
5957              }
5958    
5959                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5960                  ## As if </optgroup>              !!!cp ('t276');
5961                  pop @{$self->{open_elements}};              ## As if </optgroup>
5962                }              pop @{$self->{open_elements}};
5963              } else {
5964                !!!cp ('t277');
5965              }
5966    
5967                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5968                !!!next-token;            !!!nack ('t277.1');
5969                redo B;            !!!next-token;
5970              } elsif ($token->{tag_name} eq 'select') {            next B;
5971                !!!parse-error (type => 'not closed:select');          } elsif ({
5972                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
5973                ## have an element in table scope                   }->{$token->{tag_name}} or
5974                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5975                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
5976                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
5977                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
5978                    $i = $_;                     tr => 1, td => 1, th => 1,
5979                    last INSCOPE;                    }->{$token->{tag_name}})) {
5980                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
5981                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
5982                           }->{$node->[1]}) {                            token => $token);
5983                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
5984                  }            ## as if there were </select> (otherwise).
5985                } # INSCOPE            ## have an element in table scope
5986                unless (defined $i) {            my $i;
5987                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5988                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
5989                  !!!next-token;              if ($node->[1] & SELECT_EL) {
5990                  redo B;                !!!cp ('t278');
5991                }                $i = $_;
5992                  last INSCOPE;
5993                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5994                  !!!cp ('t279');
5995                  last INSCOPE;
5996                }
5997              } # INSCOPE
5998              unless (defined $i) {
5999                !!!cp ('t280');
6000                !!!parse-error (type => 'unmatched end tag',
6001                                text => 'select', token => $token);
6002                ## Ignore the token
6003                !!!nack ('t280.1');
6004                !!!next-token;
6005                next B;
6006              }
6007                                
6008                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6009              splice @{$self->{open_elements}}, $i;
6010    
6011                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6012    
6013                !!!next-token;            if ($token->{tag_name} eq 'select') {
6014                redo B;              !!!nack ('t281.2');
6015              } else {              !!!next-token;
6016                #              next B;
6017              } else {
6018                !!!cp ('t281.1');
6019                !!!ack-later;
6020                ## Reprocess the token.
6021                next B;
6022              }
6023            } else {
6024              !!!cp ('t282');
6025              !!!parse-error (type => 'in select',
6026                              text => $token->{tag_name}, token => $token);
6027              ## Ignore the token
6028              !!!nack ('t282.1');
6029              !!!next-token;
6030              next B;
6031            }
6032          } elsif ($token->{type} == END_TAG_TOKEN) {
6033            if ($token->{tag_name} eq 'optgroup') {
6034              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6035                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6036                !!!cp ('t283');
6037                ## As if </option>
6038                splice @{$self->{open_elements}}, -2;
6039              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6040                !!!cp ('t284');
6041                pop @{$self->{open_elements}};
6042              } else {
6043                !!!cp ('t285');
6044                !!!parse-error (type => 'unmatched end tag',
6045                                text => $token->{tag_name}, token => $token);
6046                ## Ignore the token
6047              }
6048              !!!nack ('t285.1');
6049              !!!next-token;
6050              next B;
6051            } elsif ($token->{tag_name} eq 'option') {
6052              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6053                !!!cp ('t286');
6054                pop @{$self->{open_elements}};
6055              } else {
6056                !!!cp ('t287');
6057                !!!parse-error (type => 'unmatched end tag',
6058                                text => $token->{tag_name}, token => $token);
6059                ## Ignore the token
6060              }
6061              !!!nack ('t287.1');
6062              !!!next-token;
6063              next B;
6064            } elsif ($token->{tag_name} eq 'select') {
6065              ## have an element in table scope
6066              my $i;
6067              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6068                my $node = $self->{open_elements}->[$_];
6069                if ($node->[1] & SELECT_EL) {
6070                  !!!cp ('t288');
6071                  $i = $_;
6072                  last INSCOPE;
6073                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6074                  !!!cp ('t289');
6075                  last INSCOPE;
6076              }              }
6077            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6078              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6079                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6080                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6081                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6082                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6083                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6084                  pop @{$self->{open_elements}};              !!!next-token;
6085                } else {              next B;
6086                  !!!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;  
               }  
6087                                
6088                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6089              splice @{$self->{open_elements}}, $i;
6090    
6091                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6092    
6093                !!!next-token;            !!!nack ('t291.1');
6094                redo B;            !!!next-token;
6095              } elsif ({            next B;
6096                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6097                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6098                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6099                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6100                                   }->{$token->{tag_name}}) {
6101                ## have an element in table scope  ## TODO: The following is wrong?
6102                my $i;            !!!parse-error (type => 'unmatched end tag',
6103                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                            text => $token->{tag_name}, token => $token);
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
6104                                
6105                ## As if </select>            ## have an element in table scope
6106                ## have an element in table scope            my $i;
6107                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6108                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6109                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6110                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6111                    $i = $_;                $i = $_;
6112                    last INSCOPE;                last INSCOPE;
6113                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6114                            table => 1, html => 1,                !!!cp ('t293');
6115                           }->{$node->[1]}) {                last INSCOPE;
6116                    last INSCOPE;              }
6117                  }            } # INSCOPE
6118                } # INSCOPE            unless (defined $i) {
6119                unless (defined $i) {              !!!cp ('t294');
6120                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6121                  ## Ignore the </select> token              !!!nack ('t294.1');
6122                  !!!next-token; ## TODO: ok?              !!!next-token;
6123                  redo B;              next B;
6124                }            }
6125                                
6126                splice @{$self->{open_elements}}, $i;            ## As if </select>
6127              ## have an element in table scope
6128                $self->_reset_insertion_mode;            undef $i;
6129              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6130                ## reprocess              my $node = $self->{open_elements}->[$_];
6131                redo B;              if ($node->[1] & SELECT_EL) {
6132              } else {                !!!cp ('t295');
6133                #                $i = $_;
6134                  last INSCOPE;
6135                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6136    ## ISSUE: Can this state be reached?
6137                  !!!cp ('t296');
6138                  last INSCOPE;
6139              }              }
6140            } else {            } # INSCOPE
6141              #            unless (defined $i) {
6142                !!!cp ('t297');
6143    ## TODO: The following error type is correct?
6144                !!!parse-error (type => 'unmatched end tag',
6145                                text => 'select', token => $token);
6146                ## Ignore the </select> token
6147                !!!nack ('t297.1');
6148                !!!next-token; ## TODO: ok?
6149                next B;
6150            }            }
6151                  
6152              !!!cp ('t298');
6153              splice @{$self->{open_elements}}, $i;
6154    
6155              $self->_reset_insertion_mode;
6156    
6157            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
6158              ## reprocess
6159              next B;
6160            } else {
6161              !!!cp ('t299');
6162              !!!parse-error (type => 'in select:/',
6163                              text => $token->{tag_name}, token => $token);
6164            ## Ignore the token            ## Ignore the token
6165              !!!nack ('t299.3');
6166            !!!next-token;            !!!next-token;
6167            redo B;            next B;
6168          } elsif ($self->{insertion_mode} eq 'after body') {          }
6169            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6170              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6171                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
6172                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
6173                            !!!parse-error (type => 'in body:#eof', token => $token);
6174                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6175              !!!cp ('t299.2');
6176            }
6177    
6178                unless (length $token->{data}) {          ## Stop parsing.
6179                  !!!next-token;          last B;
6180                  redo B;        } else {
6181                }          die "$0: $token->{type}: Unknown token type";
6182              }        }
6183                    } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6184              #        if ($token->{type} == CHARACTER_TOKEN) {
6185              !!!parse-error (type => 'after body:#'.$token->{type});          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6186            } elsif ($token->{type} eq 'comment') {            my $data = $1;
6187              my $comment = $self->{document}->create_comment ($token->{data});            ## As if in body
6188              $self->{open_elements}->[0]->[0]->append_child ($comment);            $reconstruct_active_formatting_elements->($insert_to_current);
6189                  
6190              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6191              
6192              unless (length $token->{data}) {
6193                !!!cp ('t300');
6194              !!!next-token;              !!!next-token;
6195              redo B;              next B;
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $phase = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
           } else {  
             !!!parse-error (type => 'after body:#'.$token->{type});  
6196            }            }
6197            }
6198            
6199            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6200              !!!cp ('t301');
6201              !!!parse-error (type => 'after html:#text', token => $token);
6202    
6203            $self->{insertion_mode} = 'in body';            ## Reprocess in the "after body" insertion mode.
6204            ## reprocess          } else {
6205            redo B;            !!!cp ('t302');
6206          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6207            if ($token->{type} eq 'character') {          
6208              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## "after body" insertion mode
6209                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          !!!parse-error (type => 'after body:#text', token => $token);
6210    
6211                unless (length $token->{data}) {          $self->{insertion_mode} = IN_BODY_IM;
6212                  !!!next-token;          ## reprocess
6213                  redo B;          next B;
6214                }        } elsif ($token->{type} == START_TAG_TOKEN) {
6215              }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6216              !!!cp ('t303');
6217              !!!parse-error (type => 'after html',
6218                              text => $token->{tag_name}, token => $token);
6219              
6220              ## Reprocess in the "after body" insertion mode.
6221            } else {
6222              !!!cp ('t304');
6223            }
6224    
6225              #          ## "after body" insertion mode
6226            } elsif ($token->{type} eq 'comment') {          !!!parse-error (type => 'after body',
6227              my $comment = $self->{document}->create_comment ($token->{data});                          text => $token->{tag_name}, token => $token);
6228              $self->{open_elements}->[-1]->[0]->append_child ($comment);  
6229            $self->{insertion_mode} = IN_BODY_IM;
6230            !!!ack-later;
6231            ## reprocess
6232            next B;
6233          } elsif ($token->{type} == END_TAG_TOKEN) {
6234            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6235              !!!cp ('t305');
6236              !!!parse-error (type => 'after html:/',
6237                              text => $token->{tag_name}, token => $token);
6238              
6239              $self->{insertion_mode} = AFTER_BODY_IM;
6240              ## Reprocess in the "after body" insertion mode.
6241            } else {
6242              !!!cp ('t306');
6243            }
6244    
6245            ## "after body" insertion mode
6246            if ($token->{tag_name} eq 'html') {
6247              if (defined $self->{inner_html_node}) {
6248                !!!cp ('t307');
6249                !!!parse-error (type => 'unmatched end tag',
6250                                text => 'html', token => $token);
6251                ## Ignore the token
6252              !!!next-token;              !!!next-token;
6253              redo B;              next B;
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
6254            } else {            } else {
6255              #              !!!cp ('t308');
6256                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6257                !!!next-token;
6258                next B;
6259              }
6260            } else {
6261              !!!cp ('t309');
6262              !!!parse-error (type => 'after body:/',
6263                              text => $token->{tag_name}, token => $token);
6264    
6265              $self->{insertion_mode} = IN_BODY_IM;
6266              ## reprocess
6267              next B;
6268            }
6269          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6270            !!!cp ('t309.2');
6271            ## Stop parsing
6272            last B;
6273          } else {
6274            die "$0: $token->{type}: Unknown token type";
6275          }
6276        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6277          if ($token->{type} == CHARACTER_TOKEN) {
6278            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6279              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6280              
6281              unless (length $token->{data}) {
6282                !!!cp ('t310');
6283                !!!next-token;
6284                next B;
6285              }
6286            }
6287            
6288            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6289              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6290                !!!cp ('t311');
6291                !!!parse-error (type => 'in frameset:#text', token => $token);
6292              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6293                !!!cp ('t312');
6294                !!!parse-error (type => 'after frameset:#text', token => $token);
6295              } else { # "after html frameset"
6296                !!!cp ('t313');
6297                !!!parse-error (type => 'after html:#text', token => $token);
6298    
6299                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6300                ## Reprocess in the "after frameset" insertion mode.
6301                !!!parse-error (type => 'after frameset:#text', token => $token);
6302            }            }
6303                        
6304            if (defined $token->{tag_name}) {            ## Ignore the token.
6305              !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if (length $token->{data}) {
6306                !!!cp ('t314');
6307                ## reprocess the rest of characters
6308            } else {            } else {
6309              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t315');
6310                !!!next-token;
6311              }
6312              next B;
6313            }
6314            
6315            die qq[$0: Character "$token->{data}"];
6316          } elsif ($token->{type} == START_TAG_TOKEN) {
6317            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6318              !!!cp ('t316');
6319              !!!parse-error (type => 'after html',
6320                              text => $token->{tag_name}, token => $token);
6321    
6322              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6323              ## Process in the "after frameset" insertion mode.
6324            } else {
6325              !!!cp ('t317');
6326            }
6327    
6328            if ($token->{tag_name} eq 'frameset' and
6329                $self->{insertion_mode} == IN_FRAMESET_IM) {
6330              !!!cp ('t318');
6331              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6332              !!!nack ('t318.1');
6333              !!!next-token;
6334              next B;
6335            } elsif ($token->{tag_name} eq 'frame' and
6336                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6337              !!!cp ('t319');
6338              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6339              pop @{$self->{open_elements}};
6340              !!!ack ('t319.1');
6341              !!!next-token;
6342              next B;
6343            } elsif ($token->{tag_name} eq 'noframes') {
6344              !!!cp ('t320');
6345              ## NOTE: As if in head.
6346              $parse_rcdata->(CDATA_CONTENT_MODEL);
6347              next B;
6348            } else {
6349              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6350                !!!cp ('t321');
6351                !!!parse-error (type => 'in frameset',
6352                                text => $token->{tag_name}, token => $token);
6353              } else {
6354                !!!cp ('t322');
6355                !!!parse-error (type => 'after frameset',
6356                                text => $token->{tag_name}, token => $token);
6357            }            }
6358            ## Ignore the token            ## Ignore the token
6359              !!!nack ('t322.1');
6360            !!!next-token;            !!!next-token;
6361            redo B;            next B;
6362          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6363            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6364              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6365                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t323');
6366              !!!parse-error (type => 'after html:/',
6367                              text => $token->{tag_name}, token => $token);
6368    
6369              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6370              ## Process in the "after frameset" insertion mode.
6371            } else {
6372              !!!cp ('t324');
6373            }
6374    
6375            if ($token->{tag_name} eq 'frameset' and
6376                $self->{insertion_mode} == IN_FRAMESET_IM) {
6377              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6378                  @{$self->{open_elements}} == 1) {
6379                !!!cp ('t325');
6380                !!!parse-error (type => 'unmatched end tag',
6381                                text => $token->{tag_name}, token => $token);
6382                ## Ignore the token
6383                !!!next-token;
6384              } else {
6385                !!!cp ('t326');
6386                pop @{$self->{open_elements}};
6387                !!!next-token;
6388              }
6389    
6390                unless (length $token->{data}) {            if (not defined $self->{inner_html_node} and
6391                  !!!next-token;                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6392                  redo B;              !!!cp ('t327');
6393                }              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6394              } else {
6395                !!!cp ('t328');
6396              }
6397              next B;
6398            } elsif ($token->{tag_name} eq 'html' and
6399                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6400              !!!cp ('t329');
6401              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6402              !!!next-token;
6403              next B;
6404            } else {
6405              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6406                !!!cp ('t330');
6407                !!!parse-error (type => 'in frameset:/',
6408                                text => $token->{tag_name}, token => $token);
6409              } else {
6410                !!!cp ('t331');
6411                !!!parse-error (type => 'after frameset:/',
6412                                text => $token->{tag_name}, token => $token);
6413              }
6414              ## Ignore the token
6415              !!!next-token;
6416              next B;
6417            }
6418          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6419            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6420                    @{$self->{open_elements}} == 1) { # redundant, maybe
6421              !!!cp ('t331.1');
6422              !!!parse-error (type => 'in body:#eof', token => $token);
6423            } else {
6424              !!!cp ('t331.2');
6425            }
6426            
6427            ## Stop parsing
6428            last B;
6429          } else {
6430            die "$0: $token->{type}: Unknown token type";
6431          }
6432    
6433          ## ISSUE: An issue in spec here
6434        } else {
6435          die "$0: $self->{insertion_mode}: Unknown insertion mode";
6436        }
6437    
6438        ## "in body" insertion mode
6439        if ($token->{type} == START_TAG_TOKEN) {
6440          if ($token->{tag_name} eq 'script') {
6441            !!!cp ('t332');
6442            ## NOTE: This is an "as if in head" code clone
6443            $script_start_tag->();
6444            next B;
6445          } elsif ($token->{tag_name} eq 'style') {
6446            !!!cp ('t333');
6447            ## NOTE: This is an "as if in head" code clone
6448            $parse_rcdata->(CDATA_CONTENT_MODEL);
6449            next B;
6450          } elsif ({
6451                    base => 1, link => 1,
6452                   }->{$token->{tag_name}}) {
6453            !!!cp ('t334');
6454            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6455            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6456            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6457            !!!ack ('t334.1');
6458            !!!next-token;
6459            next B;
6460          } elsif ($token->{tag_name} eq 'meta') {
6461            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6462            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6463            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6464    
6465            unless ($self->{confident}) {
6466              if ($token->{attributes}->{charset}) {
6467                !!!cp ('t335');
6468                ## NOTE: Whether the encoding is supported or not is handled
6469                ## in the {change_encoding} callback.
6470                $self->{change_encoding}
6471                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6472                
6473                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6474                    ->set_user_data (manakai_has_reference =>
6475                                         $token->{attributes}->{charset}
6476                                             ->{has_reference});
6477              } elsif ($token->{attributes}->{content}) {
6478                if ($token->{attributes}->{content}->{value}
6479                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6480                        [\x09-\x0D\x20]*=
6481                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6482                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6483                  !!!cp ('t336');
6484                  ## NOTE: Whether the encoding is supported or not is handled
6485                  ## in the {change_encoding} callback.
6486                  $self->{change_encoding}
6487                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6488                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6489                      ->set_user_data (manakai_has_reference =>
6490                                           $token->{attributes}->{content}
6491                                                 ->{has_reference});
6492              }              }
6493              }
6494            } else {
6495              if ($token->{attributes}->{charset}) {
6496                !!!cp ('t337');
6497                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6498                    ->set_user_data (manakai_has_reference =>
6499                                         $token->{attributes}->{charset}
6500                                             ->{has_reference});
6501              }
6502              if ($token->{attributes}->{content}) {
6503                !!!cp ('t338');
6504                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6505                    ->set_user_data (manakai_has_reference =>
6506                                         $token->{attributes}->{content}
6507                                             ->{has_reference});
6508              }
6509            }
6510    
6511              #          !!!ack ('t338.1');
6512            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6513              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6514              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6515              !!!next-token;          !!!cp ('t341');
6516              redo B;          ## NOTE: This is an "as if in head" code clone
6517            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6518              if ($token->{tag_name} eq 'noframes') {          next B;
6519                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6520                redo B;          !!!parse-error (type => 'in body', text => 'body', token => $token);
6521              } else {                
6522                #          if (@{$self->{open_elements}} == 1 or
6523                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6524              !!!cp ('t342');
6525              ## Ignore the token
6526            } else {
6527              my $body_el = $self->{open_elements}->[1]->[0];
6528              for my $attr_name (keys %{$token->{attributes}}) {
6529                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6530                  !!!cp ('t343');
6531                  $body_el->set_attribute_ns
6532                    (undef, [undef, $attr_name],
6533                     $token->{attributes}->{$attr_name}->{value});
6534              }              }
6535            } elsif ($token->{type} eq 'end tag') {            }
6536              if ($token->{tag_name} eq 'html') {          }
6537                $phase = 'trailing end';          !!!nack ('t343.1');
6538            !!!next-token;
6539            next B;
6540          } elsif ({
6541                    address => 1, blockquote => 1, center => 1, dir => 1,
6542                    div => 1, dl => 1, fieldset => 1,
6543                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6544                    menu => 1, ol => 1, p => 1, ul => 1,
6545                    pre => 1, listing => 1,
6546                    form => 1,
6547                    table => 1,
6548                    hr => 1,
6549                   }->{$token->{tag_name}}) {
6550            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6551              !!!cp ('t350');
6552              !!!parse-error (type => 'in form:form', token => $token);
6553              ## Ignore the token
6554              !!!nack ('t350.1');
6555              !!!next-token;
6556              next B;
6557            }
6558    
6559            ## has a p element in scope
6560            INSCOPE: for (reverse @{$self->{open_elements}}) {
6561              if ($_->[1] & P_EL) {
6562                !!!cp ('t344');
6563                !!!back-token; # <form>
6564                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6565                          line => $token->{line}, column => $token->{column}};
6566                next B;
6567              } elsif ($_->[1] & SCOPING_EL) {
6568                !!!cp ('t345');
6569                last INSCOPE;
6570              }
6571            } # INSCOPE
6572              
6573            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6574            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6575              !!!nack ('t346.1');
6576              !!!next-token;
6577              if ($token->{type} == CHARACTER_TOKEN) {
6578                $token->{data} =~ s/^\x0A//;
6579                unless (length $token->{data}) {
6580                  !!!cp ('t346');
6581                !!!next-token;                !!!next-token;
               redo B;  
6582              } else {              } else {
6583                #                !!!cp ('t349');
6584              }              }
6585            } else {            } else {
6586              #              !!!cp ('t348');
6587            }            }
6588            } elsif ($token->{tag_name} eq 'form') {
6589              !!!cp ('t347.1');
6590              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6591    
6592              !!!nack ('t347.2');
6593              !!!next-token;
6594            } elsif ($token->{tag_name} eq 'table') {
6595              !!!cp ('t382');
6596              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6597                        
6598            if (defined $token->{tag_name}) {            $self->{insertion_mode} = IN_TABLE_IM;
6599              !!!parse-error (type => 'after frameset:'.$token->{tag_name});  
6600              !!!nack ('t382.1');
6601              !!!next-token;
6602            } elsif ($token->{tag_name} eq 'hr') {
6603              !!!cp ('t386');
6604              pop @{$self->{open_elements}};
6605            
6606              !!!nack ('t386.1');
6607              !!!next-token;
6608            } else {
6609              !!!nack ('t347.1');
6610              !!!next-token;
6611            }
6612            next B;
6613          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6614            ## has a p element in scope
6615            INSCOPE: for (reverse @{$self->{open_elements}}) {
6616              if ($_->[1] & P_EL) {
6617                !!!cp ('t353');
6618                !!!back-token; # <x>
6619                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6620                          line => $token->{line}, column => $token->{column}};
6621                next B;
6622              } elsif ($_->[1] & SCOPING_EL) {
6623                !!!cp ('t354');
6624                last INSCOPE;
6625              }
6626            } # INSCOPE
6627              
6628            ## Step 1
6629            my $i = -1;
6630            my $node = $self->{open_elements}->[$i];
6631            my $li_or_dtdd = {li => {li => 1},
6632                              dt => {dt => 1, dd => 1},
6633                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6634            LI: {
6635              ## Step 2
6636              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6637                if ($i != -1) {
6638                  !!!cp ('t355');
6639                  !!!parse-error (type => 'not closed',
6640                                  text => $self->{open_elements}->[-1]->[0]
6641                                      ->manakai_local_name,
6642                                  token => $token);
6643                } else {
6644                  !!!cp ('t356');
6645                }
6646                splice @{$self->{open_elements}}, $i;
6647                last LI;
6648            } else {            } else {
6649              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
6650            }            }
6651              
6652              ## Step 3
6653              if (not ($node->[1] & FORMATTING_EL) and
6654                  #not $phrasing_category->{$node->[1]} and
6655                  ($node->[1] & SPECIAL_EL or
6656                   $node->[1] & SCOPING_EL) and
6657                  not ($node->[1] & ADDRESS_EL) and
6658                  not ($node->[1] & DIV_EL)) {
6659                !!!cp ('t358');
6660                last LI;
6661              }
6662              
6663              !!!cp ('t359');
6664              ## Step 4
6665              $i--;
6666              $node = $self->{open_elements}->[$i];
6667              redo LI;
6668            } # LI
6669              
6670            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6671            !!!nack ('t359.1');
6672            !!!next-token;
6673            next B;
6674          } elsif ($token->{tag_name} eq 'plaintext') {
6675            ## has a p element in scope
6676            INSCOPE: for (reverse @{$self->{open_elements}}) {
6677              if ($_->[1] & P_EL) {
6678                !!!cp ('t367');
6679                !!!back-token; # <plaintext>
6680                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6681                          line => $token->{line}, column => $token->{column}};
6682                next B;
6683              } elsif ($_->[1] & SCOPING_EL) {
6684                !!!cp ('t368');
6685                last INSCOPE;
6686              }
6687            } # INSCOPE
6688              
6689            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6690              
6691            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6692              
6693            !!!nack ('t368.1');
6694            !!!next-token;
6695            next B;
6696          } elsif ($token->{tag_name} eq 'a') {
6697            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6698              my $node = $active_formatting_elements->[$i];
6699              if ($node->[1] & A_EL) {
6700                !!!cp ('t371');
6701                !!!parse-error (type => 'in a:a', token => $token);
6702                
6703                !!!back-token; # <a>
6704                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6705                          line => $token->{line}, column => $token->{column}};
6706                $formatting_end_tag->($token);
6707                
6708                AFE2: for (reverse 0..$#$active_formatting_elements) {
6709                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6710                    !!!cp ('t372');
6711                    splice @$active_formatting_elements, $_, 1;
6712                    last AFE2;
6713                  }
6714                } # AFE2
6715                OE: for (reverse 0..$#{$self->{open_elements}}) {
6716                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6717                    !!!cp ('t373');
6718                    splice @{$self->{open_elements}}, $_, 1;
6719                    last OE;
6720                  }
6721                } # OE
6722                last AFE;
6723              } elsif ($node->[0] eq '#marker') {
6724                !!!cp ('t374');
6725                last AFE;
6726              }
6727            } # AFE
6728              
6729            $reconstruct_active_formatting_elements->($insert_to_current);
6730    
6731            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6732            push @$active_formatting_elements, $self->{open_elements}->[-1];
6733    
6734            !!!nack ('t374.1');
6735            !!!next-token;
6736            next B;
6737          } elsif ($token->{tag_name} eq 'nobr') {
6738            $reconstruct_active_formatting_elements->($insert_to_current);
6739    
6740            ## has a |nobr| element in scope
6741            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6742              my $node = $self->{open_elements}->[$_];
6743              if ($node->[1] & NOBR_EL) {
6744                !!!cp ('t376');
6745                !!!parse-error (type => 'in nobr:nobr', token => $token);
6746                !!!back-token; # <nobr>
6747                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6748                          line => $token->{line}, column => $token->{column}};
6749                next B;
6750              } elsif ($node->[1] & SCOPING_EL) {
6751                !!!cp ('t377');
6752                last INSCOPE;
6753              }
6754            } # INSCOPE
6755            
6756            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6757            push @$active_formatting_elements, $self->{open_elements}->[-1];
6758            
6759            !!!nack ('t377.1');
6760            !!!next-token;
6761            next B;
6762          } elsif ($token->{tag_name} eq 'button') {
6763            ## has a button element in scope
6764            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6765              my $node = $self->{open_elements}->[$_];
6766              if ($node->[1] & BUTTON_EL) {
6767                !!!cp ('t378');
6768                !!!parse-error (type => 'in button:button', token => $token);
6769                !!!back-token; # <button>
6770                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6771                          line => $token->{line}, column => $token->{column}};
6772                next B;
6773              } elsif ($node->[1] & SCOPING_EL) {
6774                !!!cp ('t379');
6775                last INSCOPE;
6776              }
6777            } # INSCOPE
6778              
6779            $reconstruct_active_formatting_elements->($insert_to_current);
6780              
6781            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6782    
6783            ## TODO: associate with $self->{form_element} if defined
6784    
6785            push @$active_formatting_elements, ['#marker', ''];
6786    
6787            !!!nack ('t379.1');
6788            !!!next-token;
6789            next B;
6790          } elsif ({
6791                    xmp => 1,
6792                    iframe => 1,
6793                    noembed => 1,
6794                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6795                    noscript => 0, ## TODO: 1 if scripting is enabled
6796                   }->{$token->{tag_name}}) {
6797            if ($token->{tag_name} eq 'xmp') {
6798              !!!cp ('t381');
6799              $reconstruct_active_formatting_elements->($insert_to_current);
6800            } else {
6801              !!!cp ('t399');
6802            }
6803            ## NOTE: There is an "as if in body" code clone.
6804            $parse_rcdata->(CDATA_CONTENT_MODEL);
6805            next B;
6806          } elsif ($token->{tag_name} eq 'isindex') {
6807            !!!parse-error (type => 'isindex', token => $token);
6808            
6809            if (defined $self->{form_element}) {
6810              !!!cp ('t389');
6811            ## Ignore the token            ## Ignore the token
6812              !!!nack ('t389'); ## NOTE: Not acknowledged.
6813            !!!next-token;            !!!next-token;
6814            redo B;            next B;
6815            } else {
6816              !!!ack ('t391.1');
6817    
6818            ## ISSUE: An issue in spec there            my $at = $token->{attributes};
6819              my $form_attrs;
6820              $form_attrs->{action} = $at->{action} if $at->{action};
6821              my $prompt_attr = $at->{prompt};
6822              $at->{name} = {name => 'name', value => 'isindex'};
6823              delete $at->{action};
6824              delete $at->{prompt};
6825              my @tokens = (
6826                            {type => START_TAG_TOKEN, tag_name => 'form',
6827                             attributes => $form_attrs,
6828                             line => $token->{line}, column => $token->{column}},
6829                            {type => START_TAG_TOKEN, tag_name => 'hr',
6830                             line => $token->{line}, column => $token->{column}},
6831                            {type => START_TAG_TOKEN, tag_name => 'p',
6832                             line => $token->{line}, column => $token->{column}},
6833                            {type => START_TAG_TOKEN, tag_name => 'label',
6834                             line => $token->{line}, column => $token->{column}},
6835                           );
6836              if ($prompt_attr) {
6837                !!!cp ('t390');
6838                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6839                               #line => $token->{line}, column => $token->{column},
6840                              };
6841              } else {
6842                !!!cp ('t391');
6843                push @tokens, {type => CHARACTER_TOKEN,
6844                               data => 'This is a searchable index. Insert your search keywords here: ',
6845                               #line => $token->{line}, column => $token->{column},
6846                              }; # SHOULD
6847                ## TODO: make this configurable
6848              }
6849              push @tokens,
6850                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6851                             line => $token->{line}, column => $token->{column}},
6852                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6853                            {type => END_TAG_TOKEN, tag_name => 'label',
6854                             line => $token->{line}, column => $token->{column}},
6855                            {type => END_TAG_TOKEN, tag_name => 'p',
6856                             line => $token->{line}, column => $token->{column}},
6857                            {type => START_TAG_TOKEN, tag_name => 'hr',
6858                             line => $token->{line}, column => $token->{column}},
6859                            {type => END_TAG_TOKEN, tag_name => 'form',
6860                             line => $token->{line}, column => $token->{column}};
6861              !!!back-token (@tokens);
6862              !!!next-token;
6863              next B;
6864            }
6865          } elsif ($token->{tag_name} eq 'textarea') {
6866            my $tag_name = $token->{tag_name};
6867            my $el;
6868            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6869            
6870            ## TODO: $self->{form_element} if defined
6871            $self->{content_model} = RCDATA_CONTENT_MODEL;
6872            delete $self->{escape}; # MUST
6873            
6874            $insert->($el);
6875            
6876            my $text = '';
6877            !!!nack ('t392.1');
6878            !!!next-token;
6879            if ($token->{type} == CHARACTER_TOKEN) {
6880              $token->{data} =~ s/^\x0A//;
6881              unless (length $token->{data}) {
6882                !!!cp ('t392');
6883                !!!next-token;
6884              } else {
6885                !!!cp ('t393');
6886              }
6887          } else {          } else {
6888            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t394');
6889            }
6890            while ($token->{type} == CHARACTER_TOKEN) {
6891              !!!cp ('t395');
6892              $text .= $token->{data};
6893              !!!next-token;
6894            }
6895            if (length $text) {
6896              !!!cp ('t396');
6897              $el->manakai_append_text ($text);
6898            }
6899            
6900            $self->{content_model} = PCDATA_CONTENT_MODEL;
6901            
6902            if ($token->{type} == END_TAG_TOKEN and
6903                $token->{tag_name} eq $tag_name) {
6904              !!!cp ('t397');
6905              ## Ignore the token
6906            } else {
6907              !!!cp ('t398');
6908              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6909          }          }
       }  
     } elsif ($phase eq 'trailing end') {  
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
6910          !!!next-token;          !!!next-token;
6911          redo B;          next B;
6912        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'rt' or
6913          my $comment = $self->{document}->create_comment ($token->{data});                 $token->{tag_name} eq 'rp') {
6914          $self->{document}->append_child ($comment);          ## has a |ruby| element in scope
6915            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6916              my $node = $self->{open_elements}->[$_];
6917              if ($node->[1] & RUBY_EL) {
6918                !!!cp ('t398.1');
6919                ## generate implied end tags
6920                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6921                  !!!cp ('t398.2');
6922                  pop @{$self->{open_elements}};
6923                }
6924                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6925                  !!!cp ('t398.3');
6926                  !!!parse-error (type => 'not closed',
6927                                  text => $self->{open_elements}->[-1]->[0]
6928                                      ->manakai_local_name,
6929                                  token => $token);
6930                  pop @{$self->{open_elements}}
6931                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6932                }
6933                last INSCOPE;
6934              } elsif ($node->[1] & SCOPING_EL) {
6935                !!!cp ('t398.4');
6936                last INSCOPE;
6937              }
6938            } # INSCOPE
6939    
6940            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6941    
6942            !!!nack ('t398.5');
6943          !!!next-token;          !!!next-token;
6944          redo B;          redo B;
6945        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'math' or
6946          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                 $token->{tag_name} eq 'svg') {
6947            my $data = $1;          $reconstruct_active_formatting_elements->($insert_to_current);
6948            ## As if in the main phase.  
6949            ## NOTE: The insertion mode in the main phase          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6950            ## just before the phase has been changed to the trailing  
6951            ## end phase is either "after body" or "after frameset".          ## "adjust foreign attributes" - done in insert-element-f
6952            $reconstruct_active_formatting_elements->($insert_to_current)          
6953              if $phase eq 'main';          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6954            
6955            if ($self->{self_closing}) {
6956              pop @{$self->{open_elements}};
6957              !!!ack ('t398.1');
6958            } else {
6959              !!!cp ('t398.2');
6960              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6961              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6962              ## mode, "in body" (not "in foreign content") secondary insertion
6963              ## mode, maybe.
6964            }
6965    
6966            !!!next-token;
6967            next B;
6968          } elsif ({
6969                    caption => 1, col => 1, colgroup => 1, frame => 1,
6970                    frameset => 1, head => 1, option => 1, optgroup => 1,
6971                    tbody => 1, td => 1, tfoot => 1, th => 1,
6972                    thead => 1, tr => 1,
6973                   }->{$token->{tag_name}}) {
6974            !!!cp ('t401');
6975            !!!parse-error (type => 'in body',
6976                            text => $token->{tag_name}, token => $token);
6977            ## Ignore the token
6978            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6979            !!!next-token;
6980            next B;
6981            
6982            ## ISSUE: An issue on HTML5 new elements in the spec.
6983          } else {
6984            if ($token->{tag_name} eq 'image') {
6985              !!!cp ('t384');
6986              !!!parse-error (type => 'image', token => $token);
6987              $token->{tag_name} = 'img';
6988            } else {
6989              !!!cp ('t385');
6990            }
6991    
6992            ## NOTE: There is an "as if <br>" code clone.
6993            $reconstruct_active_formatting_elements->($insert_to_current);
6994            
6995            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6996    
6997            if ({
6998                 applet => 1, marquee => 1, object => 1,
6999                }->{$token->{tag_name}}) {
7000              !!!cp ('t380');
7001              push @$active_formatting_elements, ['#marker', ''];
7002              !!!nack ('t380.1');
7003            } elsif ({
7004                      b => 1, big => 1, em => 1, font => 1, i => 1,
7005                      s => 1, small => 1, strile => 1,
7006                      strong => 1, tt => 1, u => 1,
7007                     }->{$token->{tag_name}}) {
7008              !!!cp ('t375');
7009              push @$active_formatting_elements, $self->{open_elements}->[-1];
7010              !!!nack ('t375.1');
7011            } elsif ($token->{tag_name} eq 'input') {
7012              !!!cp ('t388');
7013              ## TODO: associate with $self->{form_element} if defined
7014              pop @{$self->{open_elements}};
7015              !!!ack ('t388.2');
7016            } elsif ({
7017                      area => 1, basefont => 1, bgsound => 1, br => 1,
7018                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7019                      #image => 1,
7020                     }->{$token->{tag_name}}) {
7021              !!!cp ('t388.1');
7022              pop @{$self->{open_elements}};
7023              !!!ack ('t388.3');
7024            } elsif ($token->{tag_name} eq 'select') {
7025              ## TODO: associate with $self->{form_element} if defined
7026            
7027              if ($self->{insertion_mode} & TABLE_IMS or
7028                  $self->{insertion_mode} & BODY_TABLE_IMS or
7029                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7030                !!!cp ('t400.1');
7031                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7032              } else {
7033                !!!cp ('t400.2');
7034                $self->{insertion_mode} = IN_SELECT_IM;
7035              }
7036              !!!nack ('t400.3');
7037            } else {
7038              !!!nack ('t402');
7039            }
7040            
7041            !!!next-token;
7042            next B;
7043          }
7044        } elsif ($token->{type} == END_TAG_TOKEN) {
7045          if ($token->{tag_name} eq 'body') {
7046            ## has a |body| element in scope
7047            my $i;
7048            INSCOPE: {
7049              for (reverse @{$self->{open_elements}}) {
7050                if ($_->[1] & BODY_EL) {
7051                  !!!cp ('t405');
7052                  $i = $_;
7053                  last INSCOPE;
7054                } elsif ($_->[1] & SCOPING_EL) {
7055                  !!!cp ('t405.1');
7056                  last;
7057                }
7058              }
7059    
7060              !!!parse-error (type => 'start tag not allowed',
7061                              text => $token->{tag_name}, token => $token);
7062              ## NOTE: Ignore the token.
7063              !!!next-token;
7064              next B;
7065            } # INSCOPE
7066    
7067            for (@{$self->{open_elements}}) {
7068              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7069                !!!cp ('t403');
7070                !!!parse-error (type => 'not closed',
7071                                text => $_->[0]->manakai_local_name,
7072                                token => $token);
7073                last;
7074              } else {
7075                !!!cp ('t404');
7076              }
7077            }
7078    
7079            $self->{insertion_mode} = AFTER_BODY_IM;
7080            !!!next-token;
7081            next B;
7082          } elsif ($token->{tag_name} eq 'html') {
7083            ## TODO: Update this code.  It seems that the code below is not
7084            ## up-to-date, though it has same effect as speced.
7085            if (@{$self->{open_elements}} > 1 and
7086                $self->{open_elements}->[1]->[1] & BODY_EL) {
7087              ## ISSUE: There is an issue in the spec.
7088              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7089                !!!cp ('t406');
7090                !!!parse-error (type => 'not closed',
7091                                text => $self->{open_elements}->[1]->[0]
7092                                    ->manakai_local_name,
7093                                token => $token);
7094              } else {
7095                !!!cp ('t407');
7096              }
7097              $self->{insertion_mode} = AFTER_BODY_IM;
7098              ## reprocess
7099              next B;
7100            } else {
7101              !!!cp ('t408');
7102              !!!parse-error (type => 'unmatched end tag',
7103                              text => $token->{tag_name}, token => $token);
7104              ## Ignore the token
7105              !!!next-token;
7106              next B;
7107            }
7108          } elsif ({
7109                    address => 1, blockquote => 1, center => 1, dir => 1,
7110                    div => 1, dl => 1, fieldset => 1, listing => 1,
7111                    menu => 1, ol => 1, pre => 1, ul => 1,
7112                    dd => 1, dt => 1, li => 1,
7113                    applet => 1, button => 1, marquee => 1, object => 1,
7114                   }->{$token->{tag_name}}) {
7115            ## has an element in scope
7116            my $i;
7117            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7118              my $node = $self->{open_elements}->[$_];
7119              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7120                !!!cp ('t410');
7121                $i = $_;
7122                last INSCOPE;
7123              } elsif ($node->[1] & SCOPING_EL) {
7124                !!!cp ('t411');
7125                last INSCOPE;
7126              }
7127            } # INSCOPE
7128    
7129            unless (defined $i) { # has an element in scope
7130              !!!cp ('t413');
7131              !!!parse-error (type => 'unmatched end tag',
7132                              text => $token->{tag_name}, token => $token);
7133            } else {
7134              ## Step 1. generate implied end tags
7135              while ({
7136                      ## END_TAG_OPTIONAL_EL
7137                      dd => ($token->{tag_name} ne 'dd'),
7138                      dt => ($token->{tag_name} ne 'dt'),
7139                      li => ($token->{tag_name} ne 'li'),
7140                      p => 1,
7141                      rt => 1,
7142                      rp => 1,
7143                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7144                !!!cp ('t409');
7145                pop @{$self->{open_elements}};
7146              }
7147    
7148              ## Step 2.
7149              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7150                      ne $token->{tag_name}) {
7151                !!!cp ('t412');
7152                !!!parse-error (type => 'not closed',
7153                                text => $self->{open_elements}->[-1]->[0]
7154                                    ->manakai_local_name,
7155                                token => $token);
7156              } else {
7157                !!!cp ('t414');
7158              }
7159    
7160              ## Step 3.
7161              splice @{$self->{open_elements}}, $i;
7162    
7163              ## Step 4.
7164              $clear_up_to_marker->()
7165                  if {
7166                    applet => 1, button => 1, marquee => 1, object => 1,
7167                  }->{$token->{tag_name}};
7168            }
7169            !!!next-token;
7170            next B;
7171          } elsif ($token->{tag_name} eq 'form') {
7172            undef $self->{form_element};
7173    
7174            ## has an element in scope
7175            my $i;
7176            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7177              my $node = $self->{open_elements}->[$_];
7178              if ($node->[1] & FORM_EL) {
7179                !!!cp ('t418');
7180                $i = $_;
7181                last INSCOPE;
7182              } elsif ($node->[1] & SCOPING_EL) {
7183                !!!cp ('t419');
7184                last INSCOPE;
7185              }
7186            } # INSCOPE
7187    
7188            unless (defined $i) { # has an element in scope
7189              !!!cp ('t421');
7190              !!!parse-error (type => 'unmatched end tag',
7191                              text => $token->{tag_name}, token => $token);
7192            } else {
7193              ## Step 1. generate implied end tags
7194              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7195                !!!cp ('t417');
7196                pop @{$self->{open_elements}};
7197              }
7198                        
7199            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
7200              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7201                      ne $token->{tag_name}) {
7202                !!!cp ('t417.1');
7203                !!!parse-error (type => 'not closed',
7204                                text => $self->{open_elements}->[-1]->[0]
7205                                    ->manakai_local_name,
7206                                token => $token);
7207              } else {
7208                !!!cp ('t420');
7209              }  
7210                        
7211            unless (length $token->{data}) {            ## Step 3.
7212              !!!next-token;            splice @{$self->{open_elements}}, $i;
7213              redo B;          }
7214    
7215            !!!next-token;
7216            next B;
7217          } elsif ({
7218                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7219                   }->{$token->{tag_name}}) {
7220            ## has an element in scope
7221            my $i;
7222            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7223              my $node = $self->{open_elements}->[$_];
7224              if ($node->[1] & HEADING_EL) {
7225                !!!cp ('t423');
7226                $i = $_;
7227                last INSCOPE;
7228              } elsif ($node->[1] & SCOPING_EL) {
7229                !!!cp ('t424');
7230                last INSCOPE;
7231            }            }
7232            } # INSCOPE
7233    
7234            unless (defined $i) { # has an element in scope
7235              !!!cp ('t425.1');
7236              !!!parse-error (type => 'unmatched end tag',
7237                              text => $token->{tag_name}, token => $token);
7238            } else {
7239              ## Step 1. generate implied end tags
7240              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7241                !!!cp ('t422');
7242                pop @{$self->{open_elements}};
7243              }
7244              
7245              ## Step 2.
7246              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7247                      ne $token->{tag_name}) {
7248                !!!cp ('t425');
7249                !!!parse-error (type => 'unmatched end tag',
7250                                text => $token->{tag_name}, token => $token);
7251              } else {
7252                !!!cp ('t426');
7253              }
7254    
7255              ## Step 3.
7256              splice @{$self->{open_elements}}, $i;
7257          }          }
7258            
7259            !!!next-token;
7260            next B;
7261          } elsif ($token->{tag_name} eq 'p') {
7262            ## has an element in scope
7263            my $i;
7264            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7265              my $node = $self->{open_elements}->[$_];
7266              if ($node->[1] & P_EL) {
7267                !!!cp ('t410.1');
7268                $i = $_;
7269                last INSCOPE;
7270              } elsif ($node->[1] & SCOPING_EL) {
7271                !!!cp ('t411.1');
7272                last INSCOPE;
7273              }
7274            } # INSCOPE
7275    
7276          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7277          $phase = 'main';            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7278          ## reprocess                    ne $token->{tag_name}) {
7279          redo B;              !!!cp ('t412.1');
7280        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
7281                 $token->{type} eq 'end tag') {                              text => $self->{open_elements}->[-1]->[0]
7282          !!!parse-error (type => 'after html:'.$token->{tag_name});                                  ->manakai_local_name,
7283          $phase = 'main';                              token => $token);
7284          ## reprocess            } else {
7285          redo B;              !!!cp ('t414.1');
7286        } elsif ($token->{type} eq 'end-of-file') {            }
7287          ## Stop parsing  
7288          last B;            splice @{$self->{open_elements}}, $i;
7289            } else {
7290              !!!cp ('t413.1');
7291              !!!parse-error (type => 'unmatched end tag',
7292                              text => $token->{tag_name}, token => $token);
7293    
7294              !!!cp ('t415.1');
7295              ## As if <p>, then reprocess the current token
7296              my $el;
7297              !!!create-element ($el, $HTML_NS, 'p',, $token);
7298              $insert->($el);
7299              ## NOTE: Not inserted into |$self->{open_elements}|.
7300            }
7301    
7302            !!!next-token;
7303            next B;
7304          } elsif ({
7305                    a => 1,
7306                    b => 1, big => 1, em => 1, font => 1, i => 1,
7307                    nobr => 1, s => 1, small => 1, strile => 1,
7308                    strong => 1, tt => 1, u => 1,
7309                   }->{$token->{tag_name}}) {
7310            !!!cp ('t427');
7311            $formatting_end_tag->($token);
7312            next B;
7313          } elsif ($token->{tag_name} eq 'br') {
7314            !!!cp ('t428');
7315            !!!parse-error (type => 'unmatched end tag',
7316                            text => 'br', token => $token);
7317    
7318            ## As if <br>
7319            $reconstruct_active_formatting_elements->($insert_to_current);
7320            
7321            my $el;
7322            !!!create-element ($el, $HTML_NS, 'br',, $token);
7323            $insert->($el);
7324            
7325            ## Ignore the token.
7326            !!!next-token;
7327            next B;
7328          } elsif ({
7329                    caption => 1, col => 1, colgroup => 1, frame => 1,
7330                    frameset => 1, head => 1, option => 1, optgroup => 1,
7331                    tbody => 1, td => 1, tfoot => 1, th => 1,
7332                    thead => 1, tr => 1,
7333                    area => 1, basefont => 1, bgsound => 1,
7334                    embed => 1, hr => 1, iframe => 1, image => 1,
7335                    img => 1, input => 1, isindex => 1, noembed => 1,
7336                    noframes => 1, param => 1, select => 1, spacer => 1,
7337                    table => 1, textarea => 1, wbr => 1,
7338                    noscript => 0, ## TODO: if scripting is enabled
7339                   }->{$token->{tag_name}}) {
7340            !!!cp ('t429');
7341            !!!parse-error (type => 'unmatched end tag',
7342                            text => $token->{tag_name}, token => $token);
7343            ## Ignore the token
7344            !!!next-token;
7345            next B;
7346            
7347            ## ISSUE: Issue on HTML5 new elements in spec
7348            
7349        } else {        } else {
7350          die "$0: $token->{type}: Unknown token";          ## Step 1
7351            my $node_i = -1;
7352            my $node = $self->{open_elements}->[$node_i];
7353    
7354            ## Step 2
7355            S2: {
7356              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7357                ## Step 1
7358                ## generate implied end tags
7359                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7360                  !!!cp ('t430');
7361                  ## NOTE: |<ruby><rt></ruby>|.
7362                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7363                  ## which seems wrong.
7364                  pop @{$self->{open_elements}};
7365                  $node_i++;
7366                }
7367            
7368                ## Step 2
7369                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7370                        ne $token->{tag_name}) {
7371                  !!!cp ('t431');
7372                  ## NOTE: <x><y></x>
7373                  !!!parse-error (type => 'not closed',
7374                                  text => $self->{open_elements}->[-1]->[0]
7375                                      ->manakai_local_name,
7376                                  token => $token);
7377                } else {
7378                  !!!cp ('t432');
7379                }
7380                
7381                ## Step 3
7382                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7383    
7384                !!!next-token;
7385                last S2;
7386              } else {
7387                ## Step 3
7388                if (not ($node->[1] & FORMATTING_EL) and
7389                    #not $phrasing_category->{$node->[1]} and
7390                    ($node->[1] & SPECIAL_EL or
7391                     $node->[1] & SCOPING_EL)) {
7392                  !!!cp ('t433');
7393                  !!!parse-error (type => 'unmatched end tag',
7394                                  text => $token->{tag_name}, token => $token);
7395                  ## Ignore the token
7396                  !!!next-token;
7397                  last S2;
7398                }
7399    
7400                !!!cp ('t434');
7401              }
7402              
7403              ## Step 4
7404              $node_i--;
7405              $node = $self->{open_elements}->[$node_i];
7406              
7407              ## Step 5;
7408              redo S2;
7409            } # S2
7410            next B;
7411        }        }
7412      }      }
7413        next B;
7414      } continue { # B
7415        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7416          ## NOTE: The code below is executed in cases where it does not have
7417          ## to be, but it it is harmless even in those cases.
7418          ## has an element in scope
7419          INSCOPE: {
7420            for (reverse 0..$#{$self->{open_elements}}) {
7421              my $node = $self->{open_elements}->[$_];
7422              if ($node->[1] & FOREIGN_EL) {
7423                last INSCOPE;
7424              } elsif ($node->[1] & SCOPING_EL) {
7425                last;
7426              }
7427            }
7428            
7429            ## NOTE: No foreign element in scope.
7430            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7431          } # INSCOPE
7432        }
7433    } # B    } # B
7434    
7435    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4771  sub set_inner_html ($$$) { Line 7443  sub set_inner_html ($$$) {
7443    my $s = \$_[0];    my $s = \$_[0];
7444    my $onerror = $_[1];    my $onerror = $_[1];
7445    
7446      ## ISSUE: Should {confident} be true?
7447    
7448    my $nt = $node->node_type;    my $nt = $node->node_type;
7449    if ($nt == 9) {    if ($nt == 9) {
7450      # MUST      # MUST
# Line 4793  sub set_inner_html ($$$) { Line 7467  sub set_inner_html ($$$) {
7467      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7468    
7469      ## Step 1 # MUST      ## Step 1 # MUST
7470      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
7471      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
7472        $doc->manakai_is_html (1);
7473      my $p = $class->new;      my $p = $class->new;
7474      $p->{document} = $doc;      $p->{document} = $doc;
7475    
7476      ## Step 9 # MUST      ## Step 8 # MUST
7477      my $i = 0;      my $i = 0;
7478      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7479      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7480      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7481        my $self = shift;        my $self = shift;
7482        $self->{next_input_character} = -1 and return if $i >= length $$s;  
7483        $self->{next_input_character} = ord substr $$s, $i++, 1;        pop @{$self->{prev_char}};
7484        $column++;        unshift @{$self->{prev_char}}, $self->{next_char};
7485          
7486        if ($self->{next_input_character} == 0x000D) { # CR        $self->{next_char} = -1 and return if $i >= length $$s;
7487          if ($i >= length $$s) {        $self->{next_char} = ord substr $$s, $i++, 1;
7488            #  
7489          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7490          $p->{column}++;
7491    
7492          if ($self->{next_char} == 0x000A) { # LF
7493            $p->{line}++;
7494            $p->{column} = 0;
7495            !!!cp ('i1');
7496          } elsif ($self->{next_char} == 0x000D) { # CR
7497            $i++ if substr ($$s, $i, 1) eq "\x0A";
7498            $self->{next_char} = 0x000A; # LF # MUST
7499            $p->{line}++;
7500            $p->{column} = 0;
7501            !!!cp ('i2');
7502          } elsif ($self->{next_char} > 0x10FFFF) {
7503            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7504            !!!cp ('i3');
7505          } elsif ($self->{next_char} == 0x0000) { # NULL
7506            !!!cp ('i4');
7507            !!!parse-error (type => 'NULL');
7508            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7509          } elsif ($self->{next_char} <= 0x0008 or
7510                   (0x000E <= $self->{next_char} and
7511                    $self->{next_char} <= 0x001F) or
7512                   (0x007F <= $self->{next_char} and
7513                    $self->{next_char} <= 0x009F) or
7514                   (0xD800 <= $self->{next_char} and
7515                    $self->{next_char} <= 0xDFFF) or
7516                   (0xFDD0 <= $self->{next_char} and
7517                    $self->{next_char} <= 0xFDDF) or
7518                   {
7519                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7520                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7521                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7522                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7523                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7524                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7525                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7526                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7527                    0x10FFFE => 1, 0x10FFFF => 1,
7528                   }->{$self->{next_char}}) {
7529            !!!cp ('i4.1');
7530            if ($self->{next_char} < 0x10000) {
7531              !!!parse-error (type => 'control char',
7532                              text => (sprintf 'U+%04X', $self->{next_char}));
7533          } else {          } else {
7534            my $next_char = ord substr $$s, $i++, 1;            !!!parse-error (type => 'control char',
7535            if ($next_char == 0x000A) { # LF                            text => (sprintf 'U-%08X', $self->{next_char}));
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
7536          }          }
         $self->{next_input_character} = 0x000A; # LF # MUST  
         $line++;  
         $column = -1;  
       } elsif ($self->{next_input_character} > 0x10FFFF) {  
         $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
       } elsif ($self->{next_input_character} == 0x0000) { # NULL  
         $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
7537        }        }
7538      };      };
7539        $p->{prev_char} = [-1, -1, -1];
7540        $p->{next_char} = -1;
7541            
7542      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7543        my (%opt) = @_;        my (%opt) = @_;
7544        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7545          my $column = $opt{column};
7546          if (defined $opt{token} and defined $opt{token}->{line}) {
7547            $line = $opt{token}->{line};
7548            $column = $opt{token}->{column};
7549          }
7550          warn "Parse error ($opt{type}) at line $line column $column\n";
7551      };      };
7552      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7553        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7554      };      };
7555            
7556      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7557      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7558    
7559      ## Step 2      ## Step 2
7560      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7561      $p->{content_model_flag} = {      $p->{content_model} = {
7562        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7563        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7564        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7565        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7566        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7567        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7568        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7569        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7570        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7571        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7572      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7573         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7574            unless defined $p->{content_model};
7575            ## ISSUE: What is "the name of the element"? local name?
7576    
7577      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7578          ## TODO: Foreign element OK?
7579    
7580      ## Step 4      ## Step 3
7581      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7582        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7583    
7584      ## Step 5 # MUST      ## Step 4 # MUST
7585      $doc->append_child ($root);      $doc->append_child ($root);
7586    
7587      ## Step 6 # MUST      ## Step 5 # MUST
7588      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7589    
7590      undef $p->{head_element};      undef $p->{head_element};
7591    
7592      ## Step 7 # MUST      ## Step 6 # MUST
7593      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7594    
7595      ## Step 8 # MUST      ## Step 7 # MUST
7596      my $anode = $node;      my $anode = $node;
7597      AN: while (defined $anode) {      AN: while (defined $anode) {
7598        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7599          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7600          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7601            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7602                !!!cp ('i5');
7603              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7604              last AN;              last AN;
7605            }            }
# Line 4888  sub set_inner_html ($$$) { Line 7608  sub set_inner_html ($$$) {
7608        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7609      } # AN      } # AN
7610            
7611      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7612      {      {
7613        my $self = $p;        my $self = $p;
7614        !!!next-token;        !!!next-token;
7615      }      }
7616      $p->_tree_construction_main;      $p->_tree_construction_main;
7617    
7618      ## Step 11 # MUST      ## Step 10 # MUST
7619      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7620      for (@cn) {      for (@cn) {
7621        $node->remove_child ($_);        $node->remove_child ($_);
7622      }      }
7623      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7624    
7625      ## Step 12 # MUST      ## Step 11 # MUST
7626      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7627      for (@cn) {      for (@cn) {
7628          $this_doc->adopt_node ($_);
7629        $node->append_child ($_);        $node->append_child ($_);
7630      }      }
7631      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
7632    
7633      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7634    
7635        delete $p->{parse_error}; # delete loop
7636    } else {    } else {
7637      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";
7638    }    }
# Line 4918  sub set_inner_html ($$$) { Line 7640  sub set_inner_html ($$$) {
7640    
7641  } # tree construction stage  } # tree construction stage
7642    
7643  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7644    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  
7645    
7646  1;  1;
7647  # $Date$  # $Date$

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24