/[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.179 by wakaba, Sun Sep 14 13:09:01 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## This is an early version of an HTML parser.  ## ISSUE:
7    ## var doc = implementation.createDocument (null, null, null);
8  my $permitted_slash_tag_name = {  ## doc.write ('');
9    base => 1,  ## alert (doc.compatMode);
10    link => 1,  
11    meta => 1,  require IO::Handle;
12    hr => 1,  
13    br => 1,  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    img=> 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15    embed => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    param => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    area => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    col => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    input => 1,  
20    sub A_EL () { 0b1 }
21    sub ADDRESS_EL () { 0b10 }
22    sub BODY_EL () { 0b100 }
23    sub BUTTON_EL () { 0b1000 }
24    sub CAPTION_EL () { 0b10000 }
25    sub DD_EL () { 0b100000 }
26    sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214  };  };
215    
216  my $entity_char = {  my $el_category_f = {
217    AElig => "\x{00C6}",    $MML_NS => {
218    Aacute => "\x{00C1}",      'annotation-xml' => MML_AXML_EL,
219    Acirc => "\x{00C2}",      mi => FOREIGN_FLOW_CONTENT_EL,
220    Agrave => "\x{00C0}",      mo => FOREIGN_FLOW_CONTENT_EL,
221    Alpha => "\x{0391}",      mn => FOREIGN_FLOW_CONTENT_EL,
222    Aring => "\x{00C5}",      ms => FOREIGN_FLOW_CONTENT_EL,
223    Atilde => "\x{00C3}",      mtext => FOREIGN_FLOW_CONTENT_EL,
224    Auml => "\x{00C4}",    },
225    Beta => "\x{0392}",    $SVG_NS => {
226    Ccedil => "\x{00C7}",      foreignObject => FOREIGN_FLOW_CONTENT_EL,
227    Chi => "\x{03A7}",      desc => FOREIGN_FLOW_CONTENT_EL,
228    Dagger => "\x{2021}",      title => FOREIGN_FLOW_CONTENT_EL,
229    Delta => "\x{0394}",    },
230    ETH => "\x{00D0}",    ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
   Eacute => "\x{00C9}",  
   Ecirc => "\x{00CA}",  
   Egrave => "\x{00C8}",  
   Epsilon => "\x{0395}",  
   Eta => "\x{0397}",  
   Euml => "\x{00CB}",  
   Gamma => "\x{0393}",  
   Iacute => "\x{00CD}",  
   Icirc => "\x{00CE}",  
   Igrave => "\x{00CC}",  
   Iota => "\x{0399}",  
   Iuml => "\x{00CF}",  
   Kappa => "\x{039A}",  
   Lambda => "\x{039B}",  
   Mu => "\x{039C}",  
   Ntilde => "\x{00D1}",  
   Nu => "\x{039D}",  
   OElig => "\x{0152}",  
   Oacute => "\x{00D3}",  
   Ocirc => "\x{00D4}",  
   Ograve => "\x{00D2}",  
   Omega => "\x{03A9}",  
   Omicron => "\x{039F}",  
   Oslash => "\x{00D8}",  
   Otilde => "\x{00D5}",  
   Ouml => "\x{00D6}",  
   Phi => "\x{03A6}",  
   Pi => "\x{03A0}",  
   Prime => "\x{2033}",  
   Psi => "\x{03A8}",  
   Rho => "\x{03A1}",  
   Scaron => "\x{0160}",  
   Sigma => "\x{03A3}",  
   THORN => "\x{00DE}",  
   Tau => "\x{03A4}",  
   Theta => "\x{0398}",  
   Uacute => "\x{00DA}",  
   Ucirc => "\x{00DB}",  
   Ugrave => "\x{00D9}",  
   Upsilon => "\x{03A5}",  
   Uuml => "\x{00DC}",  
   Xi => "\x{039E}",  
   Yacute => "\x{00DD}",  
   Yuml => "\x{0178}",  
   Zeta => "\x{0396}",  
   aacute => "\x{00E1}",  
   acirc => "\x{00E2}",  
   acute => "\x{00B4}",  
   aelig => "\x{00E6}",  
   agrave => "\x{00E0}",  
   alefsym => "\x{2135}",  
   alpha => "\x{03B1}",  
   amp => "\x{0026}",  
   AMP => "\x{0026}",  
   and => "\x{2227}",  
   ang => "\x{2220}",  
   apos => "\x{0027}",  
   aring => "\x{00E5}",  
   asymp => "\x{2248}",  
   atilde => "\x{00E3}",  
   auml => "\x{00E4}",  
   bdquo => "\x{201E}",  
   beta => "\x{03B2}",  
   brvbar => "\x{00A6}",  
   bull => "\x{2022}",  
   cap => "\x{2229}",  
   ccedil => "\x{00E7}",  
   cedil => "\x{00B8}",  
   cent => "\x{00A2}",  
   chi => "\x{03C7}",  
   circ => "\x{02C6}",  
   clubs => "\x{2663}",  
   cong => "\x{2245}",  
   copy => "\x{00A9}",  
   COPY => "\x{00A9}",  
   crarr => "\x{21B5}",  
   cup => "\x{222A}",  
   curren => "\x{00A4}",  
   dArr => "\x{21D3}",  
   dagger => "\x{2020}",  
   darr => "\x{2193}",  
   deg => "\x{00B0}",  
   delta => "\x{03B4}",  
   diams => "\x{2666}",  
   divide => "\x{00F7}",  
   eacute => "\x{00E9}",  
   ecirc => "\x{00EA}",  
   egrave => "\x{00E8}",  
   empty => "\x{2205}",  
   emsp => "\x{2003}",  
   ensp => "\x{2002}",  
   epsilon => "\x{03B5}",  
   equiv => "\x{2261}",  
   eta => "\x{03B7}",  
   eth => "\x{00F0}",  
   euml => "\x{00EB}",  
   euro => "\x{20AC}",  
   exist => "\x{2203}",  
   fnof => "\x{0192}",  
   forall => "\x{2200}",  
   frac12 => "\x{00BD}",  
   frac14 => "\x{00BC}",  
   frac34 => "\x{00BE}",  
   frasl => "\x{2044}",  
   gamma => "\x{03B3}",  
   ge => "\x{2265}",  
   gt => "\x{003E}",  
   GT => "\x{003E}",  
   hArr => "\x{21D4}",  
   harr => "\x{2194}",  
   hearts => "\x{2665}",  
   hellip => "\x{2026}",  
   iacute => "\x{00ED}",  
   icirc => "\x{00EE}",  
   iexcl => "\x{00A1}",  
   igrave => "\x{00EC}",  
   image => "\x{2111}",  
   infin => "\x{221E}",  
   int => "\x{222B}",  
   iota => "\x{03B9}",  
   iquest => "\x{00BF}",  
   isin => "\x{2208}",  
   iuml => "\x{00EF}",  
   kappa => "\x{03BA}",  
   lArr => "\x{21D0}",  
   lambda => "\x{03BB}",  
   lang => "\x{2329}",  
   laquo => "\x{00AB}",  
   larr => "\x{2190}",  
   lceil => "\x{2308}",  
   ldquo => "\x{201C}",  
   le => "\x{2264}",  
   lfloor => "\x{230A}",  
   lowast => "\x{2217}",  
   loz => "\x{25CA}",  
   lrm => "\x{200E}",  
   lsaquo => "\x{2039}",  
   lsquo => "\x{2018}",  
   lt => "\x{003C}",  
   LT => "\x{003C}",  
   macr => "\x{00AF}",  
   mdash => "\x{2014}",  
   micro => "\x{00B5}",  
   middot => "\x{00B7}",  
   minus => "\x{2212}",  
   mu => "\x{03BC}",  
   nabla => "\x{2207}",  
   nbsp => "\x{00A0}",  
   ndash => "\x{2013}",  
   ne => "\x{2260}",  
   ni => "\x{220B}",  
   not => "\x{00AC}",  
   notin => "\x{2209}",  
   nsub => "\x{2284}",  
   ntilde => "\x{00F1}",  
   nu => "\x{03BD}",  
   oacute => "\x{00F3}",  
   ocirc => "\x{00F4}",  
   oelig => "\x{0153}",  
   ograve => "\x{00F2}",  
   oline => "\x{203E}",  
   omega => "\x{03C9}",  
   omicron => "\x{03BF}",  
   oplus => "\x{2295}",  
   or => "\x{2228}",  
   ordf => "\x{00AA}",  
   ordm => "\x{00BA}",  
   oslash => "\x{00F8}",  
   otilde => "\x{00F5}",  
   otimes => "\x{2297}",  
   ouml => "\x{00F6}",  
   para => "\x{00B6}",  
   part => "\x{2202}",  
   permil => "\x{2030}",  
   perp => "\x{22A5}",  
   phi => "\x{03C6}",  
   pi => "\x{03C0}",  
   piv => "\x{03D6}",  
   plusmn => "\x{00B1}",  
   pound => "\x{00A3}",  
   prime => "\x{2032}",  
   prod => "\x{220F}",  
   prop => "\x{221D}",  
   psi => "\x{03C8}",  
   quot => "\x{0022}",  
   QUOT => "\x{0022}",  
   rArr => "\x{21D2}",  
   radic => "\x{221A}",  
   rang => "\x{232A}",  
   raquo => "\x{00BB}",  
   rarr => "\x{2192}",  
   rceil => "\x{2309}",  
   rdquo => "\x{201D}",  
   real => "\x{211C}",  
   reg => "\x{00AE}",  
   REG => "\x{00AE}",  
   rfloor => "\x{230B}",  
   rho => "\x{03C1}",  
   rlm => "\x{200F}",  
   rsaquo => "\x{203A}",  
   rsquo => "\x{2019}",  
   sbquo => "\x{201A}",  
   scaron => "\x{0161}",  
   sdot => "\x{22C5}",  
   sect => "\x{00A7}",  
   shy => "\x{00AD}",  
   sigma => "\x{03C3}",  
   sigmaf => "\x{03C2}",  
   sim => "\x{223C}",  
   spades => "\x{2660}",  
   sub => "\x{2282}",  
   sube => "\x{2286}",  
   sum => "\x{2211}",  
   sup => "\x{2283}",  
   sup1 => "\x{00B9}",  
   sup2 => "\x{00B2}",  
   sup3 => "\x{00B3}",  
   supe => "\x{2287}",  
   szlig => "\x{00DF}",  
   tau => "\x{03C4}",  
   there4 => "\x{2234}",  
   theta => "\x{03B8}",  
   thetasym => "\x{03D1}",  
   thinsp => "\x{2009}",  
   thorn => "\x{00FE}",  
   tilde => "\x{02DC}",  
   times => "\x{00D7}",  
   trade => "\x{2122}",  
   uArr => "\x{21D1}",  
   uacute => "\x{00FA}",  
   uarr => "\x{2191}",  
   ucirc => "\x{00FB}",  
   ugrave => "\x{00F9}",  
   uml => "\x{00A8}",  
   upsih => "\x{03D2}",  
   upsilon => "\x{03C5}",  
   uuml => "\x{00FC}",  
   weierp => "\x{2118}",  
   xi => "\x{03BE}",  
   yacute => "\x{00FD}",  
   yen => "\x{00A5}",  
   yuml => "\x{00FF}",  
   zeta => "\x{03B6}",  
   zwj => "\x{200D}",  
   zwnj => "\x{200C}",  
231  };  };
232    
233  my $special_category = {  my $svg_attr_name = {
234    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    attributename => 'attributeName',
235    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    attributetype => 'attributeType',
236    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    basefrequency => 'baseFrequency',
237    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    baseprofile => 'baseProfile',
238    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,    calcmode => 'calcMode',
239    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,    clippathunits => 'clipPathUnits',
240    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,    contentscripttype => 'contentScriptType',
241    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    contentstyletype => 'contentStyleType',
242    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    diffuseconstant => 'diffuseConstant',
243    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    edgemode => 'edgeMode',
244  };    externalresourcesrequired => 'externalResourcesRequired',
245  my $scoping_category = {    filterres => 'filterRes',
246    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    filterunits => 'filterUnits',
247    table => 1, td => 1, th => 1,    glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296  };  };
297  my $formatting_category = {  
298    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  my $foreign_attr_xname = {
299    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,    'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311  };  };
 # $phrasing_category: all other elements  
312    
313  sub parse_string ($$$;$) {  ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    my $self = shift->new;  
315    my $s = \$_[0];  my $c1_entity_char = {
316      0x80 => 0x20AC,
317      0x81 => 0xFFFD,
318      0x82 => 0x201A,
319      0x83 => 0x0192,
320      0x84 => 0x201E,
321      0x85 => 0x2026,
322      0x86 => 0x2020,
323      0x87 => 0x2021,
324      0x88 => 0x02C6,
325      0x89 => 0x2030,
326      0x8A => 0x0160,
327      0x8B => 0x2039,
328      0x8C => 0x0152,
329      0x8D => 0xFFFD,
330      0x8E => 0x017D,
331      0x8F => 0xFFFD,
332      0x90 => 0xFFFD,
333      0x91 => 0x2018,
334      0x92 => 0x2019,
335      0x93 => 0x201C,
336      0x94 => 0x201D,
337      0x95 => 0x2022,
338      0x96 => 0x2013,
339      0x97 => 0x2014,
340      0x98 => 0x02DC,
341      0x99 => 0x2122,
342      0x9A => 0x0161,
343      0x9B => 0x203A,
344      0x9C => 0x0153,
345      0x9D => 0xFFFD,
346      0x9E => 0x017E,
347      0x9F => 0x0178,
348    }; # $c1_entity_char
349    
350    sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$$) {
358      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359      my $self = ref $_[0] ? shift : shift->new;
360      my $charset_name = shift;
361      my $byte_stream = $_[0];
362    
363      my $onerror = $_[2] || sub {
364        my (%opt) = @_;
365        warn "Parse error ($opt{type})\n";
366      };
367      $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      };
372    
373      ## HTML5 encoding sniffing algorithm
374      require Message::Charset::Info;
375      my $charset;
376      my $buffer;
377      my ($char_stream, $e_status);
378    
379      SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387        ## Step 1
388        if (defined $charset_name) {
389          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393          ## ISSUE: Unsupported encoding is not ignored according to the spec.
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1);
397          if ($char_stream) {
398            $self->{confident} = 1;
399            last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402          }
403        }
404    
405        ## Step 2
406        my $byte_buffer = '';
407        for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413        ## Step 3
414        if ($byte_buffer =~ /^\xFE\xFF/) {
415          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416          ($char_stream, $e_status) = $charset->get_decode_handle
417              ($byte_stream, allow_error_reporting => 1,
418               allow_fallback => 1, byte_buffer => \$byte_buffer);
419          $self->{confident} = 1;
420          last SNIFFING;
421        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435        }
436    
437        ## Step 4
438        ## TODO: <meta charset>
439    
440        ## Step 5
441        ## TODO: from history
442    
443        ## Step 6
444        require Whatpm::Charset::UniversalCharDet;
445        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446            ($byte_buffer);
447        if (defined $charset_name) {
448          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450          ## ISSUE: Unsupported encoding is not ignored according to the spec.
451          require Whatpm::Charset::DecodeHandle;
452          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453              ($byte_stream);
454          ($char_stream, $e_status) = $charset->get_decode_handle
455              ($buffer, allow_error_reporting => 1,
456               allow_fallback => 1, byte_buffer => \$byte_buffer);
457          if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            level => $self->{level}->{info},
462                            layer => 'encode',
463                            line => 1, column => 1);
464            $self->{confident} = 0;
465            last SNIFFING;
466          }
467        }
468    
469        ## Step 7: default
470        ## TODO: Make this configurable.
471        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473            ## detectable in the step 6.
474        require Whatpm::Charset::DecodeHandle;
475        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476            ($byte_stream);
477        ($char_stream, $e_status)
478            = $charset->get_decode_handle ($buffer,
479                                           allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        line => 1, column => 1,
487                        layer => 'encode');
488        $self->{confident} = 0;
489      } # SNIFFING
490    
491      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493        !!!parse-error (type => 'chardecode:fallback',
494                        #text => $self->{input_encoding},
495                        level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498      } elsif (not ($e_status &
499                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
500        $self->{input_encoding} = $charset->get_iana_name;
501        !!!parse-error (type => 'chardecode:no error',
502                        text => $self->{input_encoding},
503                        level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508      }
509    
510      $self->{change_encoding} = sub {
511        my $self = shift;
512        $charset_name = shift;
513        my $token = shift;
514    
515        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516        ($char_stream, $e_status) = $charset->get_decode_handle
517            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519        
520        if ($char_stream) { # if supported
521          ## "Change the encoding" algorithm:
522    
523          ## Step 1    
524          if ($charset->{category} &
525              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530          }
531          $charset_name = $charset->get_iana_name;
532          
533          ## Step 2
534          if (defined $self->{input_encoding} and
535              $self->{input_encoding} eq $charset_name) {
536            !!!parse-error (type => 'charset label:matching',
537                            text => $charset_name,
538                            level => $self->{level}->{info});
539            $self->{confident} = 1;
540            return;
541          }
542    
543          !!!parse-error (type => 'charset label detected',
544                          text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          token => $token);
548          
549          ## Step 3
550          # if (can) {
551            ## change the encoding on the fly.
552            #$self->{confident} = 1;
553            #return;
554          # }
555          
556          ## Step 4
557          throw Whatpm::HTML::RestartParser ();
558        }
559      }; # $self->{change_encoding}
560    
561      my $char_onerror = sub {
562        my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564                        line => $self->{line}, column => $self->{column} + 1,
565                        %opt, type => $type);
566        if ($opt{octets}) {
567          ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        }
569      };
570    
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574      my @args = @_; shift @args; # $s
575      my $return;
576      try {
577        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578      } catch Whatpm::HTML::RestartParser with {
579        ## NOTE: Invoked after {change_encoding}.
580    
581        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583          !!!parse-error (type => 'chardecode:fallback',
584                          level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588        } elsif (not ($e_status &
589                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
590          $self->{input_encoding} = $charset->get_iana_name;
591          !!!parse-error (type => 'chardecode:no error',
592                          text => $self->{input_encoding},
593                          level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598        }
599        $self->{confident} = 1;
600    
601        $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605      };
606      return $return;
607    } # parse_byte_stream
608    
609    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
611    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
612    ## because the core part of our HTML parser expects a string of character,
613    ## not a string of bytes or code units or anything which might contain a BOM.
614    ## Therefore, any parser interface that accepts a string of bytes,
615    ## such as |parse_byte_string| in this module, must ensure that it does
616    ## strip the BOM and never strip any ZWNBSP.
617    
618    sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      my $s = ref $_[0] ? $_[0] : \($_[0]);
622      require Whatpm::Charset::DecodeHandle;
623      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631    sub parse_char_stream ($$$;$) {
632      my $self = ref $_[0] ? shift : shift->new;
633      my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635      @{$self->{document}->child_nodes} = ();
636    
637    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
638    
639      $self->{confident} = 1 unless exists $self->{confident};
640      $self->{document}->input_encoding ($self->{input_encoding})
641          if defined $self->{input_encoding};
642    ## TODO: |{input_encoding}| is needless?
643    
644    my $i = 0;    my $i = 0;
645    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
646    my $column = 0;    $self->{column_prev} = -1;
647    $self->{set_next_input_character} = sub {    $self->{column} = 0;
648      $self->{set_next_char} = sub {
649      my $self = shift;      my $self = shift;
650      $self->{next_input_character} = -1 and return if $i >= length $$s;  
651      $self->{next_input_character} = ord substr $$s, $i++, 1;      my $char = '';
652      $column++;      if (defined $self->{next_next_char}) {
653          $char = $self->{next_next_char};
654          delete $self->{next_next_char};
655          $self->{next_char} = ord $char;
656        } else {
657          $self->{char_buffer} = '';
658          $self->{char_buffer_pos} = 0;
659    
660          my $count = $input->manakai_read_until
661             ($self->{char_buffer},
662              qr/(?!\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
663              $self->{char_buffer_pos});
664          if ($count) {
665            $self->{line_prev} = $self->{line};
666            $self->{column_prev} = $self->{column};
667            $self->{column}++;
668            $self->{next_char}
669                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
670            return;
671          }
672    
673          if ($input->read ($char, 1)) {
674            $self->{next_char} = ord $char;
675          } else {
676            $self->{next_char} = -1;
677            return;
678          }
679        }
680    
681        ($self->{line_prev}, $self->{column_prev})
682            = ($self->{line}, $self->{column});
683        $self->{column}++;
684            
685      if ($self->{next_input_character} == 0x000D) { # CR      if ($self->{next_char} == 0x000A) { # LF
686        if ($i >= length $$s) {        !!!cp ('j1');
687          #        $self->{line}++;
688          $self->{column} = 0;
689        } elsif ($self->{next_char} == 0x000D) { # CR
690          !!!cp ('j2');
691    ## TODO: support for abort/streaming
692          my $next = '';
693          if ($input->read ($next, 1) and $next ne "\x0A") {
694            $self->{next_next_char} = $next;
695          }
696          $self->{next_char} = 0x000A; # LF # MUST
697          $self->{line}++;
698          $self->{column} = 0;
699        } elsif ($self->{next_char} > 0x10FFFF) {
700          !!!cp ('j3');
701          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
702        } elsif ($self->{next_char} == 0x0000) { # NULL
703          !!!cp ('j4');
704          !!!parse-error (type => 'NULL');
705          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
706        } elsif ($self->{next_char} <= 0x0008 or
707                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
708                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
709                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
710                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
711    ## ISSUE: U+FDE0-U+FDEF are not excluded
712                 {
713                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
714                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
715                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
716                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
717                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
718                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
719                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
720                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
721                  0x10FFFE => 1, 0x10FFFF => 1,
722                 }->{$self->{next_char}}) {
723          !!!cp ('j5');
724          if ($self->{next_char} < 0x10000) {
725            !!!parse-error (type => 'control char',
726                            text => (sprintf 'U+%04X', $self->{next_char}));
727        } else {        } else {
728          my $next_char = ord substr $$s, $i++, 1;          !!!parse-error (type => 'control char',
729          if ($next_char == 0x000A) { # LF                          text => (sprintf 'U-%08X', $self->{next_char}));
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
730        }        }
       $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  
731      }      }
732    };    };
733      $self->{prev_char} = [-1, -1, -1];
734      $self->{next_char} = -1;
735    
736      $self->{read_until} = sub {
737        #my ($scalar, $specials_range, $offset) = @_;
738        my $specials_range = $_[1];
739        return 0 if defined $self->{next_next_char};
740        my $count = $input->manakai_read_until
741           ($_[0],
742            qr/(?![$specials_range\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
743            $_[2]);
744        if ($count) {
745          $self->{column} += $count;
746          $self->{column_prev} += $count;
747          $self->{prev_char} = [-1, -1, -1];
748          $self->{next_char} = -1;
749        }
750        return $count;
751      }; # $self->{read_until}
752    $self->{read_until}=sub{0};
753    
754    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
755      my (%opt) = @_;      my (%opt) = @_;
756      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
757        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
758        warn "Parse error ($opt{type}) at line $line column $column\n";
759    };    };
760    $self->{parse_error} = sub {    $self->{parse_error} = sub {
761      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
762    };    };
763    
764    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 352  sub parse_string ($$$;$) { Line 766  sub parse_string ($$$;$) {
766    $self->_construct_tree;    $self->_construct_tree;
767    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
768    
769      delete $self->{parse_error}; # remove loop
770    
771    return $self->{document};    return $self->{document};
772  } # parse_string  } # parse_char_stream
773    
774  sub new ($) {  sub new ($) {
775    my $class = shift;    my $class = shift;
776    my $self = bless {}, $class;    my $self = bless {
777    $self->{set_next_input_character} = sub {      level => {must => 'm',
778      $self->{next_input_character} = -1;                should => 's',
779                  warn => 'w',
780                  info => 'i',
781                  uncertain => 'u'},
782      }, $class;
783      $self->{set_next_char} = sub {
784        $self->{next_char} = -1;
785    };    };
786    $self->{parse_error} = sub {    $self->{parse_error} = sub {
787      #      #
788    };    };
789      $self->{change_encoding} = sub {
790        # if ($_[0] is a supported encoding) {
791        #   run "change the encoding" algorithm;
792        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
793        # }
794      };
795      $self->{application_cache_selection} = sub {
796        #
797      };
798    return $self;    return $self;
799  } # new  } # new
800    
801    sub CM_ENTITY () { 0b001 } # & markup in data
802    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
803    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
804    
805    sub PLAINTEXT_CONTENT_MODEL () { 0 }
806    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
807    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
808    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
809    
810    sub DATA_STATE () { 0 }
811    #sub ENTITY_DATA_STATE () { 1 }
812    sub TAG_OPEN_STATE () { 2 }
813    sub CLOSE_TAG_OPEN_STATE () { 3 }
814    sub TAG_NAME_STATE () { 4 }
815    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
816    sub ATTRIBUTE_NAME_STATE () { 6 }
817    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
818    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
819    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
820    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
821    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
822    #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
823    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
824    sub COMMENT_START_STATE () { 14 }
825    sub COMMENT_START_DASH_STATE () { 15 }
826    sub COMMENT_STATE () { 16 }
827    sub COMMENT_END_STATE () { 17 }
828    sub COMMENT_END_DASH_STATE () { 18 }
829    sub BOGUS_COMMENT_STATE () { 19 }
830    sub DOCTYPE_STATE () { 20 }
831    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
832    sub DOCTYPE_NAME_STATE () { 22 }
833    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
834    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
835    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
836    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
837    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
838    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
839    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
840    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
841    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
842    sub BOGUS_DOCTYPE_STATE () { 32 }
843    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
844    sub SELF_CLOSING_START_TAG_STATE () { 34 }
845    sub CDATA_SECTION_STATE () { 35 }
846    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
847    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
848    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
849    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
850    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
851    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
852    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
853    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
854    ## NOTE: "Entity data state", "entity in attribute value state", and
855    ## "consume a character reference" algorithm are jointly implemented
856    ## using the following six states:
857    sub ENTITY_STATE () { 44 }
858    sub ENTITY_HASH_STATE () { 45 }
859    sub NCR_NUM_STATE () { 46 }
860    sub HEXREF_X_STATE () { 47 }
861    sub HEXREF_HEX_STATE () { 48 }
862    sub ENTITY_NAME_STATE () { 49 }
863    
864    sub DOCTYPE_TOKEN () { 1 }
865    sub COMMENT_TOKEN () { 2 }
866    sub START_TAG_TOKEN () { 3 }
867    sub END_TAG_TOKEN () { 4 }
868    sub END_OF_FILE_TOKEN () { 5 }
869    sub CHARACTER_TOKEN () { 6 }
870    
871    sub AFTER_HTML_IMS () { 0b100 }
872    sub HEAD_IMS ()       { 0b1000 }
873    sub BODY_IMS ()       { 0b10000 }
874    sub BODY_TABLE_IMS () { 0b100000 }
875    sub TABLE_IMS ()      { 0b1000000 }
876    sub ROW_IMS ()        { 0b10000000 }
877    sub BODY_AFTER_IMS () { 0b100000000 }
878    sub FRAME_IMS ()      { 0b1000000000 }
879    sub SELECT_IMS ()     { 0b10000000000 }
880    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
881        ## NOTE: "in foreign content" insertion mode is special; it is combined
882        ## with the secondary insertion mode.  In this parser, they are stored
883        ## together in the bit-or'ed form.
884    
885    ## NOTE: "initial" and "before html" insertion modes have no constants.
886    
887    ## NOTE: "after after body" insertion mode.
888    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
889    
890    ## NOTE: "after after frameset" insertion mode.
891    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
892    
893    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
894    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
895    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
896    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
897    sub IN_BODY_IM () { BODY_IMS }
898    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
899    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
900    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
901    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
902    sub IN_TABLE_IM () { TABLE_IMS }
903    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
904    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
905    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
906    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
907    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
908    sub IN_COLUMN_GROUP_IM () { 0b10 }
909    
910  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
911    
912  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
913    my $self = shift;    my $self = shift;
914    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
915    $self->{content_model_flag} = 'PCDATA'; # be    #$self->{state_keyword}; # initialized when used
916    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    #$self->{entity__value}; # initialized when used
917      #$self->{entity__match}; # initialized when used
918      $self->{content_model} = PCDATA_CONTENT_MODEL; # be
919      undef $self->{current_token};
920    undef $self->{current_attribute};    undef $self->{current_attribute};
921    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
922    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
923    $self->{char} = [];    delete $self->{self_closing};
924    # $self->{next_input_character}    $self->{char_buffer} = '';
925      $self->{char_buffer_pos} = 0;
926      # $self->{next_char}
927    !!!next-input-character;    !!!next-input-character;
928    $self->{token} = [];    $self->{token} = [];
929      # $self->{escape}
930  } # _initialize_tokenizer  } # _initialize_tokenizer
931    
932  ## A token has:  ## A token has:
933  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
934  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
935  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
936      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
937  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
938  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
939  ##   ->{data} (comment, character)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
940    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
941  ## Macros  ##        ->{name}
942  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
943  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
944  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
945    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
946    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
947    ##     while the token is pushed back to the stack.
948    
949  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
950    
# Line 405  sub _initialize_tokenizer ($) { Line 954  sub _initialize_tokenizer ($) {
954  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
955  ## and removed from the list.  ## and removed from the list.
956    
957    ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
958    ## (This requirement was dropped from HTML5 spec, unfortunately.)
959    
960  sub _get_next_token ($) {  sub _get_next_token ($) {
961    my $self = shift;    my $self = shift;
962    
963      if ($self->{self_closing}) {
964        !!!parse-error (type => 'nestc', token => $self->{current_token});
965        ## NOTE: The |self_closing| flag is only set by start tag token.
966        ## In addition, when a start tag token is emitted, it is always set to
967        ## |current_token|.
968        delete $self->{self_closing};
969      }
970    
971    if (@{$self->{token}}) {    if (@{$self->{token}}) {
972        $self->{self_closing} = $self->{token}->[0]->{self_closing};
973      return shift @{$self->{token}};      return shift @{$self->{token}};
974    }    }
975    
976    A: {    A: {
977      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
978        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
979          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
980              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
981            $self->{state} = 'entity data';            !!!cp (1);
982              ## NOTE: In the spec, the tokenizer is switched to the
983              ## "entity data state".  In this implementation, the tokenizer
984              ## is switched to the |ENTITY_STATE|, which is an implementation
985              ## of the "consume a character reference" algorithm.
986              $self->{entity_additional} = -1;
987              $self->{prev_state} = DATA_STATE;
988              $self->{state} = ENTITY_STATE;
989            !!!next-input-character;            !!!next-input-character;
990            redo A;            redo A;
991          } else {          } else {
992              !!!cp (2);
993            #            #
994          }          }
995        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x002D) { # -
996          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
997            $self->{state} = 'tag open';            unless ($self->{escape}) {
998                if ($self->{prev_char}->[0] == 0x002D and # -
999                    $self->{prev_char}->[1] == 0x0021 and # !
1000                    $self->{prev_char}->[2] == 0x003C) { # <
1001                  !!!cp (3);
1002                  $self->{escape} = 1;
1003                } else {
1004                  !!!cp (4);
1005                }
1006              } else {
1007                !!!cp (5);
1008              }
1009            }
1010            
1011            #
1012          } elsif ($self->{next_char} == 0x003C) { # <
1013            if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1014                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
1015                 not $self->{escape})) {
1016              !!!cp (6);
1017              $self->{state} = TAG_OPEN_STATE;
1018            !!!next-input-character;            !!!next-input-character;
1019            redo A;            redo A;
1020          } else {          } else {
1021              !!!cp (7);
1022            #            #
1023          }          }
1024        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
1025          !!!emit ({type => 'end-of-file'});          if ($self->{escape} and
1026                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1027              if ($self->{prev_char}->[0] == 0x002D and # -
1028                  $self->{prev_char}->[1] == 0x002D) { # -
1029                !!!cp (8);
1030                delete $self->{escape};
1031              } else {
1032                !!!cp (9);
1033              }
1034            } else {
1035              !!!cp (10);
1036            }
1037            
1038            #
1039          } elsif ($self->{next_char} == -1) {
1040            !!!cp (11);
1041            !!!emit ({type => END_OF_FILE_TOKEN,
1042                      line => $self->{line}, column => $self->{column}});
1043          last A; ## TODO: ok?          last A; ## TODO: ok?
1044          } else {
1045            !!!cp (12);
1046        }        }
1047        # Anything else        # Anything else
1048        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
1049                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
1050                       line => $self->{line}, column => $self->{column},
1051                      };
1052          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1053    
1054        ## Stay in the data state        ## Stay in the data state
1055        !!!next-input-character;        !!!next-input-character;
1056    
1057        !!!emit ($token);        !!!emit ($token);
1058    
1059        redo A;        redo A;
1060      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1061        ## (cannot happen in CDATA state)        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1062                  if ($self->{next_char} == 0x002F) { # /
1063        my $token = $self->_tokenize_attempt_to_consume_an_entity;            !!!cp (15);
   
       $self->{state} = 'data';  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!emit ({type => 'character', data => '&'});  
       } else {  
         !!!emit ($token);  
       }  
   
       redo A;  
     } elsif ($self->{state} eq 'tag open') {  
       if ($self->{content_model_flag} eq 'RCDATA' or  
           $self->{content_model_flag} eq 'CDATA') {  
         if ($self->{next_input_character} == 0x002F) { # /  
1064            !!!next-input-character;            !!!next-input-character;
1065            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1066            redo A;            redo A;
1067          } else {          } else {
1068              !!!cp (16);
1069            ## reconsume            ## reconsume
1070            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1071    
1072            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1073                        line => $self->{line_prev},
1074                        column => $self->{column_prev},
1075                       });
1076    
1077            redo A;            redo A;
1078          }          }
1079        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1080          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1081            $self->{state} = 'markup declaration open';            !!!cp (17);
1082              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1083            !!!next-input-character;            !!!next-input-character;
1084            redo A;            redo A;
1085          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1086            $self->{state} = 'close tag open';            !!!cp (18);
1087              $self->{state} = CLOSE_TAG_OPEN_STATE;
1088            !!!next-input-character;            !!!next-input-character;
1089            redo A;            redo A;
1090          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1091                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1092              !!!cp (19);
1093            $self->{current_token}            $self->{current_token}
1094              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1095                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1096            $self->{state} = 'tag name';                 line => $self->{line_prev},
1097                   column => $self->{column_prev}};
1098              $self->{state} = TAG_NAME_STATE;
1099            !!!next-input-character;            !!!next-input-character;
1100            redo A;            redo A;
1101          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1102                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1103            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1104                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1105            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1106                                        line => $self->{line_prev},
1107                                        column => $self->{column_prev}};
1108              $self->{state} = TAG_NAME_STATE;
1109            !!!next-input-character;            !!!next-input-character;
1110            redo A;            redo A;
1111          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1112            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1113            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1114                              line => $self->{line_prev},
1115                              column => $self->{column_prev});
1116              $self->{state} = DATA_STATE;
1117            !!!next-input-character;            !!!next-input-character;
1118    
1119            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1120                        line => $self->{line_prev},
1121                        column => $self->{column_prev},
1122                       });
1123    
1124            redo A;            redo A;
1125          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1126            !!!parse-error (type => 'pio');            !!!cp (22);
1127            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1128            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1129                              column => $self->{column_prev});
1130              $self->{state} = BOGUS_COMMENT_STATE;
1131              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1132                                        line => $self->{line_prev},
1133                                        column => $self->{column_prev},
1134                                       };
1135              ## $self->{next_char} is intentionally left as is
1136            redo A;            redo A;
1137          } else {          } else {
1138            !!!parse-error (type => 'bare stago');            !!!cp (23);
1139            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1140                              line => $self->{line_prev},
1141                              column => $self->{column_prev});
1142              $self->{state} = DATA_STATE;
1143            ## reconsume            ## reconsume
1144    
1145            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1146                        line => $self->{line_prev},
1147                        column => $self->{column_prev},
1148                       });
1149    
1150            redo A;            redo A;
1151          }          }
1152        } else {        } else {
1153          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1154        }        }
1155      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1156        if ($self->{content_model_flag} eq 'RCDATA' or        ## NOTE: The "close tag open state" in the spec is implemented as
1157            $self->{content_model_flag} eq 'CDATA') {        ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1158          my @next_char;  
1159          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1160            push @next_char, $self->{next_input_character};        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1161            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);          if (defined $self->{last_emitted_start_tag_name}) {
1162            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1163            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {            $self->{state_keyword} = '';
1164              !!!next-input-character;            ## Reconsume.
             next TAGNAME;  
           } else {  
             !!!parse-error (type => 'unmatched end tag');  
             $self->{next_input_character} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = 'data';  
   
             !!!emit ({type => 'character', data => '</'});  
   
             redo A;  
           }  
         }  
         push @next_char, $self->{next_input_character};  
       
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == 0x003C or # <  
                 $self->{next_input_character} == -1) {  
           !!!parse-error (type => 'unmatched end tag');  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
           $self->{state} = 'data';  
   
           !!!emit ({type => 'character', data => '</'});  
   
1165            redo A;            redo A;
1166          } else {          } else {
1167            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
1168            !!!back-next-input-character (@next_char);            ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1169            # and consume...            !!!cp (28);
1170              $self->{state} = DATA_STATE;
1171              ## Reconsume.
1172              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1173                        line => $l, column => $c,
1174                       });
1175              redo A;
1176          }          }
1177        }        }
1178          
1179        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1180            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1181          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1182                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1183          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1184          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1185          redo A;                 line => $l, column => $c};
1186        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1187                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1188          $self->{current_token} = {type => 'end tag',          redo A;
1189                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1190          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1191          !!!next-input-character;          !!!cp (30);
1192          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1193        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1194          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1195          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1196            !!!next-input-character;
1197            redo A;
1198          } elsif ($self->{next_char} == 0x003E) { # >
1199            !!!cp (31);
1200            !!!parse-error (type => 'empty end tag',
1201                            line => $self->{line_prev}, ## "<" in "</>"
1202                            column => $self->{column_prev} - 1);
1203            $self->{state} = DATA_STATE;
1204          !!!next-input-character;          !!!next-input-character;
1205          redo A;          redo A;
1206        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1207            !!!cp (32);
1208          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1209          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1210          # reconsume          # reconsume
1211    
1212          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1213                      line => $l, column => $c,
1214                     });
1215    
1216          redo A;          redo A;
1217        } else {        } else {
1218            !!!cp (33);
1219          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1220          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1221          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1222          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1223                                      column => $self->{column_prev} - 1,
1224                                     };
1225            ## NOTE: $self->{next_char} is intentionally left as is.
1226            ## Although the "anything else" case of the spec not explicitly
1227            ## states that the next input character is to be reconsumed,
1228            ## it will be included to the |data| of the comment token
1229            ## generated from the bogus end tag, as defined in the
1230            ## "bogus comment state" entry.
1231            redo A;
1232          }
1233        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1234          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1235          if (length $ch) {
1236            my $CH = $ch;
1237            $ch =~ tr/a-z/A-Z/;
1238            my $nch = chr $self->{next_char};
1239            if ($nch eq $ch or $nch eq $CH) {
1240              !!!cp (24);
1241              ## Stay in the state.
1242              $self->{state_keyword} .= $nch;
1243              !!!next-input-character;
1244              redo A;
1245            } else {
1246              !!!cp (25);
1247              $self->{state} = DATA_STATE;
1248              ## Reconsume.
1249              !!!emit ({type => CHARACTER_TOKEN,
1250                        data => '</' . $self->{state_keyword},
1251                        line => $self->{line_prev},
1252                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1253                       });
1254              redo A;
1255            }
1256          } else { # after "<{tag-name}"
1257            unless ({
1258                     0x0009 => 1, # HT
1259                     0x000A => 1, # LF
1260                     0x000B => 1, # VT
1261                     0x000C => 1, # FF
1262                     0x0020 => 1, # SP
1263                     0x003E => 1, # >
1264                     0x002F => 1, # /
1265                     -1 => 1, # EOF
1266                    }->{$self->{next_char}}) {
1267              !!!cp (26);
1268              ## Reconsume.
1269              $self->{state} = DATA_STATE;
1270              !!!emit ({type => CHARACTER_TOKEN,
1271                        data => '</' . $self->{state_keyword},
1272                        line => $self->{line_prev},
1273                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1274                       });
1275              redo A;
1276            } else {
1277              !!!cp (27);
1278              $self->{current_token}
1279                  = {type => END_TAG_TOKEN,
1280                     tag_name => $self->{last_emitted_start_tag_name},
1281                     line => $self->{line_prev},
1282                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1283              $self->{state} = TAG_NAME_STATE;
1284              ## Reconsume.
1285              redo A;
1286            }
1287        }        }
1288      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
1289        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1290            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1291            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1292            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1293            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1294          $self->{state} = 'before attribute name';          !!!cp (34);
1295            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1296          !!!next-input-character;          !!!next-input-character;
1297          redo A;          redo A;
1298        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1299          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1300              !!!cp (35);
1301            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1302          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1303            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1304            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1305              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1306            }            #  !!! cp (36);
1307              #  !!! parse-error (type => 'end tag attribute');
1308              #} else {
1309                !!!cp (37);
1310              #}
1311          } else {          } else {
1312            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1313          }          }
1314          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1315          !!!next-input-character;          !!!next-input-character;
1316    
1317          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1318    
1319          redo A;          redo A;
1320        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1321                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1322          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1323            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1324            # start tag or end tag            # start tag or end tag
1325          ## Stay in this state          ## Stay in this state
1326          !!!next-input-character;          !!!next-input-character;
1327          redo A;          redo A;
1328        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1329          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1330          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1331              !!!cp (39);
1332            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1333          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1334            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1335            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1336              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1337            }            #  !!! cp (40);
1338              #  !!! parse-error (type => 'end tag attribute');
1339              #} else {
1340                !!!cp (41);
1341              #}
1342          } else {          } else {
1343            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1344          }          }
1345          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1346          # reconsume          # reconsume
1347    
1348          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1349    
1350          redo A;          redo A;
1351        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1352            !!!cp (42);
1353            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1354          !!!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  
1355          redo A;          redo A;
1356        } else {        } else {
1357          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1358            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1359            # start tag or end tag            # start tag or end tag
1360          ## Stay in the state          ## Stay in the state
1361          !!!next-input-character;          !!!next-input-character;
1362          redo A;          redo A;
1363        }        }
1364      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1365        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1366            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1367            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1368            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1369            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1370            !!!cp (45);
1371          ## Stay in the state          ## Stay in the state
1372          !!!next-input-character;          !!!next-input-character;
1373          redo A;          redo A;
1374        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1375          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1376              !!!cp (46);
1377            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1378          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1379            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1380            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1381                !!!cp (47);
1382              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1383              } else {
1384                !!!cp (48);
1385            }            }
1386          } else {          } else {
1387            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1388          }          }
1389          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1390          !!!next-input-character;          !!!next-input-character;
1391    
1392          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1393    
1394          redo A;          redo A;
1395        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1396                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1397          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1398                                value => ''};          $self->{current_attribute}
1399          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1400                   value => '',
1401                   line => $self->{line}, column => $self->{column}};
1402            $self->{state} = ATTRIBUTE_NAME_STATE;
1403            !!!next-input-character;
1404            redo A;
1405          } elsif ($self->{next_char} == 0x002F) { # /
1406            !!!cp (50);
1407            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1408          !!!next-input-character;          !!!next-input-character;
1409          redo A;          redo A;
1410        } 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) {  
1411          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1412          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1413              !!!cp (52);
1414            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1415          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1416            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1417            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1418                !!!cp (53);
1419              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1420              } else {
1421                !!!cp (54);
1422            }            }
1423          } else {          } else {
1424            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1425          }          }
1426          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1427          # reconsume          # reconsume
1428    
1429          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1430    
1431          redo A;          redo A;
1432        } else {        } else {
1433          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1434                                value => ''};               0x0022 => 1, # "
1435          $self->{state} = 'attribute name';               0x0027 => 1, # '
1436                 0x003D => 1, # =
1437                }->{$self->{next_char}}) {
1438              !!!cp (55);
1439              !!!parse-error (type => 'bad attribute name');
1440            } else {
1441              !!!cp (56);
1442            }
1443            $self->{current_attribute}
1444                = {name => chr ($self->{next_char}),
1445                   value => '',
1446                   line => $self->{line}, column => $self->{column}};
1447            $self->{state} = ATTRIBUTE_NAME_STATE;
1448          !!!next-input-character;          !!!next-input-character;
1449          redo A;          redo A;
1450        }        }
1451      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1452        my $before_leave = sub {        my $before_leave = sub {
1453          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1454              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1455            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1456              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1457            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1458          } else {          } else {
1459              !!!cp (58);
1460            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1461              = $self->{current_attribute};              = $self->{current_attribute};
1462          }          }
1463        }; # $before_leave        }; # $before_leave
1464    
1465        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1466            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1467            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1468            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1469            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1470            !!!cp (59);
1471          $before_leave->();          $before_leave->();
1472          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1473          !!!next-input-character;          !!!next-input-character;
1474          redo A;          redo A;
1475        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1476            !!!cp (60);
1477          $before_leave->();          $before_leave->();
1478          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1479          !!!next-input-character;          !!!next-input-character;
1480          redo A;          redo A;
1481        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1482          $before_leave->();          $before_leave->();
1483          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1484              !!!cp (61);
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            !!!cp (62);
1488              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1489            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1490              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1491            }            }
1492          } else {          } else {
1493            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1494          }          }
1495          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1496          !!!next-input-character;          !!!next-input-character;
1497    
1498          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1499    
1500          redo A;          redo A;
1501        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1502                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1503          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1504            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1505          ## Stay in the state          ## Stay in the state
1506          !!!next-input-character;          !!!next-input-character;
1507          redo A;          redo A;
1508        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1509            !!!cp (64);
1510          $before_leave->();          $before_leave->();
1511            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1512          !!!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  
1513          redo A;          redo A;
1514        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1515          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1516          $before_leave->();          $before_leave->();
1517          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1518              !!!cp (66);
1519            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1520          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1521            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1522            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1523                !!!cp (67);
1524              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1525              } else {
1526                ## NOTE: This state should never be reached.
1527                !!!cp (68);
1528            }            }
1529          } else {          } else {
1530            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1531          }          }
1532          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1533          # reconsume          # reconsume
1534    
1535          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1536    
1537          redo A;          redo A;
1538        } else {        } else {
1539          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1540                $self->{next_char} == 0x0027) { # '
1541              !!!cp (69);
1542              !!!parse-error (type => 'bad attribute name');
1543            } else {
1544              !!!cp (70);
1545            }
1546            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1547          ## Stay in the state          ## Stay in the state
1548          !!!next-input-character;          !!!next-input-character;
1549          redo A;          redo A;
1550        }        }
1551      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1552        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1553            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1554            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1555            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1556            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1557            !!!cp (71);
1558          ## Stay in the state          ## Stay in the state
1559          !!!next-input-character;          !!!next-input-character;
1560          redo A;          redo A;
1561        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1562          $self->{state} = 'before attribute value';          !!!cp (72);
1563            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1564          !!!next-input-character;          !!!next-input-character;
1565          redo A;          redo A;
1566        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1567          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1568              !!!cp (73);
1569            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1570          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1571            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1572            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1573                !!!cp (74);
1574              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1575              } else {
1576                ## NOTE: This state should never be reached.
1577                !!!cp (75);
1578            }            }
1579          } else {          } else {
1580            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1581          }          }
1582          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1583          !!!next-input-character;          !!!next-input-character;
1584    
1585          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1586    
1587          redo A;          redo A;
1588        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1589                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1590          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1591                                value => ''};          $self->{current_attribute}
1592          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1593          !!!next-input-character;                 value => '',
1594          redo A;                 line => $self->{line}, column => $self->{column}};
1595        } elsif ($self->{next_input_character} == 0x002F) { # /          $self->{state} = ATTRIBUTE_NAME_STATE;
1596            !!!next-input-character;
1597            redo A;
1598          } elsif ($self->{next_char} == 0x002F) { # /
1599            !!!cp (77);
1600            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1601          !!!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  
1602          redo A;          redo A;
1603        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1604          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1605          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1606              !!!cp (79);
1607            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1608          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1609            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1610            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1611                !!!cp (80);
1612              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1613              } else {
1614                ## NOTE: This state should never be reached.
1615                !!!cp (81);
1616            }            }
1617          } else {          } else {
1618            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1619          }          }
1620          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1621          # reconsume          # reconsume
1622    
1623          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1624    
1625          redo A;          redo A;
1626        } else {        } else {
1627          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{next_char} == 0x0022 or # "
1628                                value => ''};              $self->{next_char} == 0x0027) { # '
1629          $self->{state} = 'attribute name';            !!!cp (78);
1630              !!!parse-error (type => 'bad attribute name');
1631            } else {
1632              !!!cp (82);
1633            }
1634            $self->{current_attribute}
1635                = {name => chr ($self->{next_char}),
1636                   value => '',
1637                   line => $self->{line}, column => $self->{column}};
1638            $self->{state} = ATTRIBUTE_NAME_STATE;
1639          !!!next-input-character;          !!!next-input-character;
1640          redo A;                  redo A;        
1641        }        }
1642      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1643        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1644            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1645            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1646            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1647            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1648            !!!cp (83);
1649          ## Stay in the state          ## Stay in the state
1650          !!!next-input-character;          !!!next-input-character;
1651          redo A;          redo A;
1652        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1653          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1654            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1655          !!!next-input-character;          !!!next-input-character;
1656          redo A;          redo A;
1657        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1658          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1659            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1660          ## reconsume          ## reconsume
1661          redo A;          redo A;
1662        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1663          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1664            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1665          !!!next-input-character;          !!!next-input-character;
1666          redo A;          redo A;
1667        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1668          if ($self->{current_token}->{type} eq 'start tag') {          !!!parse-error (type => 'empty unquoted attribute value');
1669            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1670              !!!cp (87);
1671            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1672          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1673            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1674            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1675                !!!cp (88);
1676              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1677              } else {
1678                ## NOTE: This state should never be reached.
1679                !!!cp (89);
1680            }            }
1681          } else {          } else {
1682            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1683          }          }
1684          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1685          !!!next-input-character;          !!!next-input-character;
1686    
1687          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1688    
1689          redo A;          redo A;
1690        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1691          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1692          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1693              !!!cp (90);
1694            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1695          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1696            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1697            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1698                !!!cp (91);
1699              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1700              } else {
1701                ## NOTE: This state should never be reached.
1702                !!!cp (92);
1703            }            }
1704          } else {          } else {
1705            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1706          }          }
1707          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1708          ## reconsume          ## reconsume
1709    
1710          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1711    
1712          redo A;          redo A;
1713        } else {        } else {
1714          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1715          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1716              !!!parse-error (type => 'bad attribute value');
1717            } else {
1718              !!!cp (94);
1719            }
1720            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1721            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1722          !!!next-input-character;          !!!next-input-character;
1723          redo A;          redo A;
1724        }        }
1725      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1726        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1727          $self->{state} = 'before attribute name';          !!!cp (95);
1728            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1729          !!!next-input-character;          !!!next-input-character;
1730          redo A;          redo A;
1731        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1732          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1733          $self->{state} = 'entity in attribute value';          ## NOTE: In the spec, the tokenizer is switched to the
1734            ## "entity in attribute value state".  In this implementation, the
1735            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1736            ## implementation of the "consume a character reference" algorithm.
1737            $self->{prev_state} = $self->{state};
1738            $self->{entity_additional} = 0x0022; # "
1739            $self->{state} = ENTITY_STATE;
1740          !!!next-input-character;          !!!next-input-character;
1741          redo A;          redo A;
1742        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1743          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1744          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1745              !!!cp (97);
1746            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1747          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1748            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1749            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1750                !!!cp (98);
1751              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1752              } else {
1753                ## NOTE: This state should never be reached.
1754                !!!cp (99);
1755            }            }
1756          } else {          } else {
1757            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1758          }          }
1759          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1760          ## reconsume          ## reconsume
1761    
1762          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1763    
1764          redo A;          redo A;
1765        } else {        } else {
1766          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1767            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1768            $self->{read_until}->($self->{current_attribute}->{value},
1769                                  q["&],
1770                                  length $self->{current_attribute}->{value});
1771    
1772          ## Stay in the state          ## Stay in the state
1773          !!!next-input-character;          !!!next-input-character;
1774          redo A;          redo A;
1775        }        }
1776      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1777        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1778          $self->{state} = 'before attribute name';          !!!cp (101);
1779          !!!next-input-character;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1780          redo A;          !!!next-input-character;
1781        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1782          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1783          $self->{state} = 'entity in attribute value';          !!!cp (102);
1784            ## NOTE: In the spec, the tokenizer is switched to the
1785            ## "entity in attribute value state".  In this implementation, the
1786            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1787            ## implementation of the "consume a character reference" algorithm.
1788            $self->{entity_additional} = 0x0027; # '
1789            $self->{prev_state} = $self->{state};
1790            $self->{state} = ENTITY_STATE;
1791          !!!next-input-character;          !!!next-input-character;
1792          redo A;          redo A;
1793        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1794          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1795          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1796              !!!cp (103);
1797            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1798          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1799            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1800            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1801                !!!cp (104);
1802              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1803              } else {
1804                ## NOTE: This state should never be reached.
1805                !!!cp (105);
1806            }            }
1807          } else {          } else {
1808            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1809          }          }
1810          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1811          ## reconsume          ## reconsume
1812    
1813          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1814    
1815          redo A;          redo A;
1816        } else {        } else {
1817          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1818            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1819            $self->{read_until}->($self->{current_attribute}->{value},
1820                                  q['&],
1821                                  length $self->{current_attribute}->{value});
1822    
1823          ## Stay in the state          ## Stay in the state
1824          !!!next-input-character;          !!!next-input-character;
1825          redo A;          redo A;
1826        }        }
1827      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1828        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1829            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1830            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1831            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1832            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1833          $self->{state} = 'before attribute name';          !!!cp (107);
1834          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1835          redo A;          !!!next-input-character;
1836        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1837          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1838          $self->{state} = 'entity in attribute value';          !!!cp (108);
1839          !!!next-input-character;          ## NOTE: In the spec, the tokenizer is switched to the
1840          redo A;          ## "entity in attribute value state".  In this implementation, the
1841        } elsif ($self->{next_input_character} == 0x003E) { # >          ## tokenizer is switched to the |ENTITY_STATE|, which is an
1842          if ($self->{current_token}->{type} eq 'start tag') {          ## implementation of the "consume a character reference" algorithm.
1843            $self->{entity_additional} = -1;
1844            $self->{prev_state} = $self->{state};
1845            $self->{state} = ENTITY_STATE;
1846            !!!next-input-character;
1847            redo A;
1848          } elsif ($self->{next_char} == 0x003E) { # >
1849            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1850              !!!cp (109);
1851            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1852          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1853            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1854            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1855                !!!cp (110);
1856              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1857              } else {
1858                ## NOTE: This state should never be reached.
1859                !!!cp (111);
1860            }            }
1861          } else {          } else {
1862            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1863          }          }
1864          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1865          !!!next-input-character;          !!!next-input-character;
1866    
1867          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1868    
1869          redo A;          redo A;
1870        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1871          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1872          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1873              !!!cp (112);
1874            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1875          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1876            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1877            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1878                !!!cp (113);
1879              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1880              } else {
1881                ## NOTE: This state should never be reached.
1882                !!!cp (114);
1883            }            }
1884          } else {          } else {
1885            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1886          }          }
1887          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1888          ## reconsume          ## reconsume
1889    
1890          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1891    
1892          redo A;          redo A;
1893        } else {        } else {
1894          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1895                 0x0022 => 1, # "
1896                 0x0027 => 1, # '
1897                 0x003D => 1, # =
1898                }->{$self->{next_char}}) {
1899              !!!cp (115);
1900              !!!parse-error (type => 'bad attribute value');
1901            } else {
1902              !!!cp (116);
1903            }
1904            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1905            $self->{read_until}->($self->{current_attribute}->{value},
1906                                  q["'=& >],
1907                                  length $self->{current_attribute}->{value});
1908    
1909          ## Stay in the state          ## Stay in the state
1910          !!!next-input-character;          !!!next-input-character;
1911          redo A;          redo A;
1912        }        }
1913      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1914        my $token = $self->_tokenize_attempt_to_consume_an_entity;        if ($self->{next_char} == 0x0009 or # HT
1915              $self->{next_char} == 0x000A or # LF
1916              $self->{next_char} == 0x000B or # VT
1917              $self->{next_char} == 0x000C or # FF
1918              $self->{next_char} == 0x0020) { # SP
1919            !!!cp (118);
1920            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1921            !!!next-input-character;
1922            redo A;
1923          } elsif ($self->{next_char} == 0x003E) { # >
1924            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1925              !!!cp (119);
1926              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1927            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1928              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1929              if ($self->{current_token}->{attributes}) {
1930                !!!cp (120);
1931                !!!parse-error (type => 'end tag attribute');
1932              } else {
1933                ## NOTE: This state should never be reached.
1934                !!!cp (121);
1935              }
1936            } else {
1937              die "$0: $self->{current_token}->{type}: Unknown token type";
1938            }
1939            $self->{state} = DATA_STATE;
1940            !!!next-input-character;
1941    
1942        unless (defined $token) {          !!!emit ($self->{current_token}); # start tag or end tag
1943          $self->{current_attribute}->{value} .= '&';  
1944            redo A;
1945          } elsif ($self->{next_char} == 0x002F) { # /
1946            !!!cp (122);
1947            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1948            !!!next-input-character;
1949            redo A;
1950          } elsif ($self->{next_char} == -1) {
1951            !!!parse-error (type => 'unclosed tag');
1952            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1953              !!!cp (122.3);
1954              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1955            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1956              if ($self->{current_token}->{attributes}) {
1957                !!!cp (122.1);
1958                !!!parse-error (type => 'end tag attribute');
1959              } else {
1960                ## NOTE: This state should never be reached.
1961                !!!cp (122.2);
1962              }
1963            } else {
1964              die "$0: $self->{current_token}->{type}: Unknown token type";
1965            }
1966            $self->{state} = DATA_STATE;
1967            ## Reconsume.
1968            !!!emit ($self->{current_token}); # start tag or end tag
1969            redo A;
1970        } else {        } else {
1971          $self->{current_attribute}->{value} .= $token->{data};          !!!cp ('124.1');
1972          ## ISSUE: spec says "append the returned character token to the current attribute's value"          !!!parse-error (type => 'no space between attributes');
1973            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1974            ## reconsume
1975            redo A;
1976        }        }
1977        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1978          if ($self->{next_char} == 0x003E) { # >
1979            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1980              !!!cp ('124.2');
1981              !!!parse-error (type => 'nestc', token => $self->{current_token});
1982              ## TODO: Different type than slash in start tag
1983              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1984              if ($self->{current_token}->{attributes}) {
1985                !!!cp ('124.4');
1986                !!!parse-error (type => 'end tag attribute');
1987              } else {
1988                !!!cp ('124.5');
1989              }
1990              ## TODO: Test |<title></title/>|
1991            } else {
1992              !!!cp ('124.3');
1993              $self->{self_closing} = 1;
1994            }
1995    
1996        $self->{state} = $self->{last_attribute_value_state};          $self->{state} = DATA_STATE;
1997        # next-input-character is already done          !!!next-input-character;
       redo A;  
     } elsif ($self->{state} eq 'bogus comment') {  
       ## (only happen if PCDATA state)  
         
       my $token = {type => 'comment', data => ''};  
   
       BC: {  
         if ($self->{next_input_character} == 0x003E) { # >  
           $self->{state} = 'data';  
           !!!next-input-character;  
   
           !!!emit ($token);  
   
           redo A;  
         } elsif ($self->{next_input_character} == -1) {  
           $self->{state} = 'data';  
           ## reconsume  
1998    
1999            !!!emit ($token);          !!!emit ($self->{current_token}); # start tag or end tag
2000    
2001            redo A;          redo A;
2002          } elsif ($self->{next_char} == -1) {
2003            !!!parse-error (type => 'unclosed tag');
2004            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
2005              !!!cp (124.7);
2006              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
2007            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
2008              if ($self->{current_token}->{attributes}) {
2009                !!!cp (124.5);
2010                !!!parse-error (type => 'end tag attribute');
2011              } else {
2012                ## NOTE: This state should never be reached.
2013                !!!cp (124.6);
2014              }
2015          } else {          } else {
2016            $token->{data} .= chr ($self->{next_input_character});            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!next-input-character;  
           redo BC;  
2017          }          }
2018        } # BC          $self->{state} = DATA_STATE;
2019      } elsif ($self->{state} eq 'markup declaration open') {          ## Reconsume.
2020            !!!emit ($self->{current_token}); # start tag or end tag
2021            redo A;
2022          } else {
2023            !!!cp ('124.4');
2024            !!!parse-error (type => 'nestc');
2025            ## TODO: This error type is wrong.
2026            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2027            ## Reconsume.
2028            redo A;
2029          }
2030        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2031        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2032    
2033        my @next_char;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2034        push @next_char, $self->{next_input_character};        ## consumes characters one-by-one basis.
2035                
2036        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x003E) { # >
2037            !!!cp (124);
2038            $self->{state} = DATA_STATE;
2039          !!!next-input-character;          !!!next-input-character;
2040          push @next_char, $self->{next_input_character};  
2041          if ($self->{next_input_character} == 0x002D) { # -          !!!emit ($self->{current_token}); # comment
2042            $self->{current_token} = {type => 'comment', data => ''};          redo A;
2043            $self->{state} = 'comment';        } elsif ($self->{next_char} == -1) {
2044            !!!next-input-character;          !!!cp (125);
2045            redo A;          $self->{state} = DATA_STATE;
2046          }          ## reconsume
2047        } elsif ($self->{next_input_character} == 0x0044 or # D  
2048                 $self->{next_input_character} == 0x0064) { # d          !!!emit ($self->{current_token}); # comment
2049            redo A;
2050          } else {
2051            !!!cp (126);
2052            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2053            $self->{read_until}->($self->{current_token}->{data},
2054                                  q[>],
2055                                  length $self->{current_token}->{data});
2056    
2057            ## Stay in the state.
2058          !!!next-input-character;          !!!next-input-character;
2059          push @next_char, $self->{next_input_character};          redo A;
2060          if ($self->{next_input_character} == 0x004F or # O        }
2061              $self->{next_input_character} == 0x006F) { # o      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2062            !!!next-input-character;        ## (only happen if PCDATA state)
2063            push @next_char, $self->{next_input_character};        
2064            if ($self->{next_input_character} == 0x0043 or # C        if ($self->{next_char} == 0x002D) { # -
2065                $self->{next_input_character} == 0x0063) { # c          !!!cp (133);
2066              !!!next-input-character;          $self->{state} = MD_HYPHEN_STATE;
2067              push @next_char, $self->{next_input_character};          !!!next-input-character;
2068              if ($self->{next_input_character} == 0x0054 or # T          redo A;
2069                  $self->{next_input_character} == 0x0074) { # t        } elsif ($self->{next_char} == 0x0044 or # D
2070                !!!next-input-character;                 $self->{next_char} == 0x0064) { # d
2071                push @next_char, $self->{next_input_character};          ## ASCII case-insensitive.
2072                if ($self->{next_input_character} == 0x0059 or # Y          !!!cp (130);
2073                    $self->{next_input_character} == 0x0079) { # y          $self->{state} = MD_DOCTYPE_STATE;
2074                  !!!next-input-character;          $self->{state_keyword} = chr $self->{next_char};
2075                  push @next_char, $self->{next_input_character};          !!!next-input-character;
2076                  if ($self->{next_input_character} == 0x0050 or # P          redo A;
2077                      $self->{next_input_character} == 0x0070) { # p        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2078                    !!!next-input-character;                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2079                    push @next_char, $self->{next_input_character};                 $self->{next_char} == 0x005B) { # [
2080                    if ($self->{next_input_character} == 0x0045 or # E          !!!cp (135.4);                
2081                        $self->{next_input_character} == 0x0065) { # e          $self->{state} = MD_CDATA_STATE;
2082                      ## ISSUE: What a stupid code this is!          $self->{state_keyword} = '[';
2083                      $self->{state} = 'DOCTYPE';          !!!next-input-character;
2084                      !!!next-input-character;          redo A;
2085                      redo A;        } else {
2086                    }          !!!cp (136);
                 }  
               }  
             }  
           }  
         }  
2087        }        }
2088    
2089        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment',
2090        $self->{next_input_character} = shift @next_char;                        line => $self->{line_prev},
2091        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2092        $self->{state} = 'bogus comment';        ## Reconsume.
2093          $self->{state} = BOGUS_COMMENT_STATE;
2094          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2095                                    line => $self->{line_prev},
2096                                    column => $self->{column_prev} - 1,
2097                                   };
2098        redo A;        redo A;
2099              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2100        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2101        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?          !!!cp (127);
2102      } elsif ($self->{state} eq 'comment') {          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2103        if ($self->{next_input_character} == 0x002D) { # -                                    line => $self->{line_prev},
2104          $self->{state} = 'comment dash';                                    column => $self->{column_prev} - 2,
2105                                     };
2106            $self->{state} = COMMENT_START_STATE;
2107            !!!next-input-character;
2108            redo A;
2109          } else {
2110            !!!cp (128);
2111            !!!parse-error (type => 'bogus comment',
2112                            line => $self->{line_prev},
2113                            column => $self->{column_prev} - 2);
2114            $self->{state} = BOGUS_COMMENT_STATE;
2115            ## Reconsume.
2116            $self->{current_token} = {type => COMMENT_TOKEN,
2117                                      data => '-',
2118                                      line => $self->{line_prev},
2119                                      column => $self->{column_prev} - 2,
2120                                     };
2121            redo A;
2122          }
2123        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2124          ## ASCII case-insensitive.
2125          if ($self->{next_char} == [
2126                undef,
2127                0x004F, # O
2128                0x0043, # C
2129                0x0054, # T
2130                0x0059, # Y
2131                0x0050, # P
2132              ]->[length $self->{state_keyword}] or
2133              $self->{next_char} == [
2134                undef,
2135                0x006F, # o
2136                0x0063, # c
2137                0x0074, # t
2138                0x0079, # y
2139                0x0070, # p
2140              ]->[length $self->{state_keyword}]) {
2141            !!!cp (131);
2142            ## Stay in the state.
2143            $self->{state_keyword} .= chr $self->{next_char};
2144            !!!next-input-character;
2145            redo A;
2146          } elsif ((length $self->{state_keyword}) == 6 and
2147                   ($self->{next_char} == 0x0045 or # E
2148                    $self->{next_char} == 0x0065)) { # e
2149            !!!cp (129);
2150            $self->{state} = DOCTYPE_STATE;
2151            $self->{current_token} = {type => DOCTYPE_TOKEN,
2152                                      quirks => 1,
2153                                      line => $self->{line_prev},
2154                                      column => $self->{column_prev} - 7,
2155                                     };
2156          !!!next-input-character;          !!!next-input-character;
2157          redo A;          redo A;
2158        } elsif ($self->{next_input_character} == -1) {        } else {
2159            !!!cp (132);        
2160            !!!parse-error (type => 'bogus comment',
2161                            line => $self->{line_prev},
2162                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2163            $self->{state} = BOGUS_COMMENT_STATE;
2164            ## Reconsume.
2165            $self->{current_token} = {type => COMMENT_TOKEN,
2166                                      data => $self->{state_keyword},
2167                                      line => $self->{line_prev},
2168                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2169                                     };
2170            redo A;
2171          }
2172        } elsif ($self->{state} == MD_CDATA_STATE) {
2173          if ($self->{next_char} == {
2174                '[' => 0x0043, # C
2175                '[C' => 0x0044, # D
2176                '[CD' => 0x0041, # A
2177                '[CDA' => 0x0054, # T
2178                '[CDAT' => 0x0041, # A
2179              }->{$self->{state_keyword}}) {
2180            !!!cp (135.1);
2181            ## Stay in the state.
2182            $self->{state_keyword} .= chr $self->{next_char};
2183            !!!next-input-character;
2184            redo A;
2185          } elsif ($self->{state_keyword} eq '[CDATA' and
2186                   $self->{next_char} == 0x005B) { # [
2187            !!!cp (135.2);
2188            $self->{current_token} = {type => CHARACTER_TOKEN,
2189                                      data => '',
2190                                      line => $self->{line_prev},
2191                                      column => $self->{column_prev} - 7};
2192            $self->{state} = CDATA_SECTION_STATE;
2193            !!!next-input-character;
2194            redo A;
2195          } else {
2196            !!!cp (135.3);
2197            !!!parse-error (type => 'bogus comment',
2198                            line => $self->{line_prev},
2199                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2200            $self->{state} = BOGUS_COMMENT_STATE;
2201            ## Reconsume.
2202            $self->{current_token} = {type => COMMENT_TOKEN,
2203                                      data => $self->{state_keyword},
2204                                      line => $self->{line_prev},
2205                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2206                                     };
2207            redo A;
2208          }
2209        } elsif ($self->{state} == COMMENT_START_STATE) {
2210          if ($self->{next_char} == 0x002D) { # -
2211            !!!cp (137);
2212            $self->{state} = COMMENT_START_DASH_STATE;
2213            !!!next-input-character;
2214            redo A;
2215          } elsif ($self->{next_char} == 0x003E) { # >
2216            !!!cp (138);
2217            !!!parse-error (type => 'bogus comment');
2218            $self->{state} = DATA_STATE;
2219            !!!next-input-character;
2220    
2221            !!!emit ($self->{current_token}); # comment
2222    
2223            redo A;
2224          } elsif ($self->{next_char} == -1) {
2225            !!!cp (139);
2226          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2227          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2228          ## reconsume          ## reconsume
2229    
2230          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2231    
2232          redo A;          redo A;
2233        } else {        } else {
2234          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (140);
2235            $self->{current_token}->{data} # comment
2236                .= chr ($self->{next_char});
2237            $self->{state} = COMMENT_STATE;
2238            !!!next-input-character;
2239            redo A;
2240          }
2241        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2242          if ($self->{next_char} == 0x002D) { # -
2243            !!!cp (141);
2244            $self->{state} = COMMENT_END_STATE;
2245            !!!next-input-character;
2246            redo A;
2247          } elsif ($self->{next_char} == 0x003E) { # >
2248            !!!cp (142);
2249            !!!parse-error (type => 'bogus comment');
2250            $self->{state} = DATA_STATE;
2251            !!!next-input-character;
2252    
2253            !!!emit ($self->{current_token}); # comment
2254    
2255            redo A;
2256          } elsif ($self->{next_char} == -1) {
2257            !!!cp (143);
2258            !!!parse-error (type => 'unclosed comment');
2259            $self->{state} = DATA_STATE;
2260            ## reconsume
2261    
2262            !!!emit ($self->{current_token}); # comment
2263    
2264            redo A;
2265          } else {
2266            !!!cp (144);
2267            $self->{current_token}->{data} # comment
2268                .= '-' . chr ($self->{next_char});
2269            $self->{state} = COMMENT_STATE;
2270            !!!next-input-character;
2271            redo A;
2272          }
2273        } elsif ($self->{state} == COMMENT_STATE) {
2274          if ($self->{next_char} == 0x002D) { # -
2275            !!!cp (145);
2276            $self->{state} = COMMENT_END_DASH_STATE;
2277            !!!next-input-character;
2278            redo A;
2279          } elsif ($self->{next_char} == -1) {
2280            !!!cp (146);
2281            !!!parse-error (type => 'unclosed comment');
2282            $self->{state} = DATA_STATE;
2283            ## reconsume
2284    
2285            !!!emit ($self->{current_token}); # comment
2286    
2287            redo A;
2288          } else {
2289            !!!cp (147);
2290            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2291            $self->{read_until}->($self->{current_token}->{data},
2292                                  q[-],
2293                                  length $self->{current_token}->{data});
2294    
2295          ## Stay in the state          ## Stay in the state
2296          !!!next-input-character;          !!!next-input-character;
2297          redo A;          redo A;
2298        }        }
2299      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2300        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2301          $self->{state} = 'comment end';          !!!cp (148);
2302            $self->{state} = COMMENT_END_STATE;
2303          !!!next-input-character;          !!!next-input-character;
2304          redo A;          redo A;
2305        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2306            !!!cp (149);
2307          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2308          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2309          ## reconsume          ## reconsume
2310    
2311          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2312    
2313          redo A;          redo A;
2314        } else {        } else {
2315          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2316          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2317            $self->{state} = COMMENT_STATE;
2318          !!!next-input-character;          !!!next-input-character;
2319          redo A;          redo A;
2320        }        }
2321      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2322        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2323          $self->{state} = 'data';          !!!cp (151);
2324            $self->{state} = DATA_STATE;
2325          !!!next-input-character;          !!!next-input-character;
2326    
2327          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2328    
2329          redo A;          redo A;
2330        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2331          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2332            !!!parse-error (type => 'dash in comment',
2333                            line => $self->{line_prev},
2334                            column => $self->{column_prev});
2335          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2336          ## Stay in the state          ## Stay in the state
2337          !!!next-input-character;          !!!next-input-character;
2338          redo A;          redo A;
2339        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2340            !!!cp (153);
2341          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2342          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2343          ## reconsume          ## reconsume
2344    
2345          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2346    
2347          redo A;          redo A;
2348        } else {        } else {
2349          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2350          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2351          $self->{state} = 'comment';                          line => $self->{line_prev},
2352                            column => $self->{column_prev});
2353            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2354            $self->{state} = COMMENT_STATE;
2355          !!!next-input-character;          !!!next-input-character;
2356          redo A;          redo A;
2357        }        }
2358      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2359        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2360            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2361            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2362            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2363            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2364          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2365            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2366          !!!next-input-character;          !!!next-input-character;
2367          redo A;          redo A;
2368        } else {        } else {
2369            !!!cp (156);
2370          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2371          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2372          ## reconsume          ## reconsume
2373          redo A;          redo A;
2374        }        }
2375      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2376        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2377            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2378            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2379            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2380            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2381            !!!cp (157);
2382          ## Stay in the state          ## Stay in the state
2383          !!!next-input-character;          !!!next-input-character;
2384          redo A;          redo A;
2385        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
2386                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (158);
2387          $self->{current_token} = {type => 'DOCTYPE',          !!!parse-error (type => 'no DOCTYPE name');
2388                            name => chr ($self->{next_input_character} - 0x0020),          $self->{state} = DATA_STATE;
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
2389          !!!next-input-character;          !!!next-input-character;
2390    
2391            !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2392    
2393          redo A;          redo A;
2394        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == -1) {
2395            !!!cp (159);
2396          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2397          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2398            ## reconsume
2399    
2400            !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2401    
2402            redo A;
2403          } else {
2404            !!!cp (160);
2405            $self->{current_token}->{name} = chr $self->{next_char};
2406            delete $self->{current_token}->{quirks};
2407    ## ISSUE: "Set the token's name name to the" in the spec
2408            $self->{state} = DOCTYPE_NAME_STATE;
2409            !!!next-input-character;
2410            redo A;
2411          }
2412        } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2413    ## ISSUE: Redundant "First," in the spec.
2414          if ($self->{next_char} == 0x0009 or # HT
2415              $self->{next_char} == 0x000A or # LF
2416              $self->{next_char} == 0x000B or # VT
2417              $self->{next_char} == 0x000C or # FF
2418              $self->{next_char} == 0x0020) { # SP
2419            !!!cp (161);
2420            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2421            !!!next-input-character;
2422            redo A;
2423          } elsif ($self->{next_char} == 0x003E) { # >
2424            !!!cp (162);
2425            $self->{state} = DATA_STATE;
2426          !!!next-input-character;          !!!next-input-character;
2427    
2428          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE
2429    
2430          redo A;          redo A;
2431        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2432          !!!parse-error (type => 'no DOCTYPE name');          !!!cp (163);
2433          $self->{state} = 'data';          !!!parse-error (type => 'unclosed DOCTYPE');
2434            $self->{state} = DATA_STATE;
2435          ## reconsume          ## reconsume
2436    
2437          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          $self->{current_token}->{quirks} = 1;
2438            !!!emit ($self->{current_token}); # DOCTYPE
2439    
2440          redo A;          redo A;
2441        } else {        } else {
2442          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (164);
2443                            name => chr ($self->{next_input_character}),          $self->{current_token}->{name}
2444                            error => 1};            .= chr ($self->{next_char}); # DOCTYPE
2445          $self->{state} = 'DOCTYPE name';          ## Stay in the state
2446          !!!next-input-character;          !!!next-input-character;
2447          redo A;          redo A;
2448        }        }
2449      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2450        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2451            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2452            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2453            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2454            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2455          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (165);
2456          $self->{state} = 'after DOCTYPE name';          ## Stay in the state
2457          !!!next-input-character;          !!!next-input-character;
2458          redo A;          redo A;
2459        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2460          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (166);
2461          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2462          !!!next-input-character;          !!!next-input-character;
2463    
2464          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2465    
2466          redo A;          redo A;
2467        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
2468                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (167);
2469          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed DOCTYPE');
2470          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{state} = DATA_STATE;
2471            ## reconsume
2472    
2473            $self->{current_token}->{quirks} = 1;
2474            !!!emit ($self->{current_token}); # DOCTYPE
2475    
2476            redo A;
2477          } elsif ($self->{next_char} == 0x0050 or # P
2478                   $self->{next_char} == 0x0070) { # p
2479            $self->{state} = PUBLIC_STATE;
2480            $self->{state_keyword} = chr $self->{next_char};
2481            !!!next-input-character;
2482            redo A;
2483          } elsif ($self->{next_char} == 0x0053 or # S
2484                   $self->{next_char} == 0x0073) { # s
2485            $self->{state} = SYSTEM_STATE;
2486            $self->{state_keyword} = chr $self->{next_char};
2487            !!!next-input-character;
2488            redo A;
2489          } else {
2490            !!!cp (180);
2491            !!!parse-error (type => 'string after DOCTYPE name');
2492            $self->{current_token}->{quirks} = 1;
2493    
2494            $self->{state} = BOGUS_DOCTYPE_STATE;
2495            !!!next-input-character;
2496            redo A;
2497          }
2498        } elsif ($self->{state} == PUBLIC_STATE) {
2499          ## ASCII case-insensitive
2500          if ($self->{next_char} == [
2501                undef,
2502                0x0055, # U
2503                0x0042, # B
2504                0x004C, # L
2505                0x0049, # I
2506              ]->[length $self->{state_keyword}] or
2507              $self->{next_char} == [
2508                undef,
2509                0x0075, # u
2510                0x0062, # b
2511                0x006C, # l
2512                0x0069, # i
2513              ]->[length $self->{state_keyword}]) {
2514            !!!cp (175);
2515            ## Stay in the state.
2516            $self->{state_keyword} .= chr $self->{next_char};
2517            !!!next-input-character;
2518            redo A;
2519          } elsif ((length $self->{state_keyword}) == 5 and
2520                   ($self->{next_char} == 0x0043 or # C
2521                    $self->{next_char} == 0x0063)) { # c
2522            !!!cp (168);
2523            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2524            !!!next-input-character;
2525            redo A;
2526          } else {
2527            !!!cp (169);
2528            !!!parse-error (type => 'string after DOCTYPE name',
2529                            line => $self->{line_prev},
2530                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2531            $self->{current_token}->{quirks} = 1;
2532    
2533            $self->{state} = BOGUS_DOCTYPE_STATE;
2534            ## Reconsume.
2535            redo A;
2536          }
2537        } elsif ($self->{state} == SYSTEM_STATE) {
2538          ## ASCII case-insensitive
2539          if ($self->{next_char} == [
2540                undef,
2541                0x0059, # Y
2542                0x0053, # S
2543                0x0054, # T
2544                0x0045, # E
2545              ]->[length $self->{state_keyword}] or
2546              $self->{next_char} == [
2547                undef,
2548                0x0079, # y
2549                0x0073, # s
2550                0x0074, # t
2551                0x0065, # e
2552              ]->[length $self->{state_keyword}]) {
2553            !!!cp (170);
2554            ## Stay in the state.
2555            $self->{state_keyword} .= chr $self->{next_char};
2556            !!!next-input-character;
2557            redo A;
2558          } elsif ((length $self->{state_keyword}) == 5 and
2559                   ($self->{next_char} == 0x004D or # M
2560                    $self->{next_char} == 0x006D)) { # m
2561            !!!cp (171);
2562            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2563            !!!next-input-character;
2564            redo A;
2565          } else {
2566            !!!cp (172);
2567            !!!parse-error (type => 'string after DOCTYPE name',
2568                            line => $self->{line_prev},
2569                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2570            $self->{current_token}->{quirks} = 1;
2571    
2572            $self->{state} = BOGUS_DOCTYPE_STATE;
2573            ## Reconsume.
2574            redo A;
2575          }
2576        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2577          if ({
2578                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2579                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2580              }->{$self->{next_char}}) {
2581            !!!cp (181);
2582          ## Stay in the state          ## Stay in the state
2583          !!!next-input-character;          !!!next-input-character;
2584          redo A;          redo A;
2585        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} eq 0x0022) { # "
2586            !!!cp (182);
2587            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2588            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2589            !!!next-input-character;
2590            redo A;
2591          } elsif ($self->{next_char} eq 0x0027) { # '
2592            !!!cp (183);
2593            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2594            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2595            !!!next-input-character;
2596            redo A;
2597          } elsif ($self->{next_char} eq 0x003E) { # >
2598            !!!cp (184);
2599            !!!parse-error (type => 'no PUBLIC literal');
2600    
2601            $self->{state} = DATA_STATE;
2602            !!!next-input-character;
2603    
2604            $self->{current_token}->{quirks} = 1;
2605            !!!emit ($self->{current_token}); # DOCTYPE
2606    
2607            redo A;
2608          } elsif ($self->{next_char} == -1) {
2609            !!!cp (185);
2610          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2611          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
2612          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2613          ## reconsume          ## reconsume
2614    
2615          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
2616          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
2617    
2618          redo A;          redo A;
2619        } else {        } else {
2620          $self->{current_token}->{name}          !!!cp (186);
2621            .= chr ($self->{next_input_character}); # DOCTYPE          !!!parse-error (type => 'string after PUBLIC');
2622          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{current_token}->{quirks} = 1;
2623    
2624            $self->{state} = BOGUS_DOCTYPE_STATE;
2625            !!!next-input-character;
2626            redo A;
2627          }
2628        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2629          if ($self->{next_char} == 0x0022) { # "
2630            !!!cp (187);
2631            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2632            !!!next-input-character;
2633            redo A;
2634          } elsif ($self->{next_char} == 0x003E) { # >
2635            !!!cp (188);
2636            !!!parse-error (type => 'unclosed PUBLIC literal');
2637    
2638            $self->{state} = DATA_STATE;
2639            !!!next-input-character;
2640    
2641            $self->{current_token}->{quirks} = 1;
2642            !!!emit ($self->{current_token}); # DOCTYPE
2643    
2644            redo A;
2645          } elsif ($self->{next_char} == -1) {
2646            !!!cp (189);
2647            !!!parse-error (type => 'unclosed PUBLIC literal');
2648    
2649            $self->{state} = DATA_STATE;
2650            ## reconsume
2651    
2652            $self->{current_token}->{quirks} = 1;
2653            !!!emit ($self->{current_token}); # DOCTYPE
2654    
2655            redo A;
2656          } else {
2657            !!!cp (190);
2658            $self->{current_token}->{public_identifier} # DOCTYPE
2659                .= chr $self->{next_char};
2660            $self->{read_until}->($self->{current_token}->{public_identifier},
2661                                  q[">],
2662                                  length $self->{current_token}->{public_identifier});
2663    
2664          ## Stay in the state          ## Stay in the state
2665          !!!next-input-character;          !!!next-input-character;
2666          redo A;          redo A;
2667        }        }
2668      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2669        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0027) { # '
2670            $self->{next_input_character} == 0x000A or # LF          !!!cp (191);
2671            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2672            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
2673            $self->{next_input_character} == 0x0020) { # SP          redo A;
2674          } elsif ($self->{next_char} == 0x003E) { # >
2675            !!!cp (192);
2676            !!!parse-error (type => 'unclosed PUBLIC literal');
2677    
2678            $self->{state} = DATA_STATE;
2679            !!!next-input-character;
2680    
2681            $self->{current_token}->{quirks} = 1;
2682            !!!emit ($self->{current_token}); # DOCTYPE
2683    
2684            redo A;
2685          } elsif ($self->{next_char} == -1) {
2686            !!!cp (193);
2687            !!!parse-error (type => 'unclosed PUBLIC literal');
2688    
2689            $self->{state} = DATA_STATE;
2690            ## reconsume
2691    
2692            $self->{current_token}->{quirks} = 1;
2693            !!!emit ($self->{current_token}); # DOCTYPE
2694    
2695            redo A;
2696          } else {
2697            !!!cp (194);
2698            $self->{current_token}->{public_identifier} # DOCTYPE
2699                .= chr $self->{next_char};
2700            $self->{read_until}->($self->{current_token}->{public_identifier},
2701                                  q['>],
2702                                  length $self->{current_token}->{public_identifier});
2703    
2704          ## Stay in the state          ## Stay in the state
2705          !!!next-input-character;          !!!next-input-character;
2706          redo A;          redo A;
2707        } elsif ($self->{next_input_character} == 0x003E) { # >        }
2708          $self->{state} = 'data';      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2709          if ({
2710                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2711                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2712              }->{$self->{next_char}}) {
2713            !!!cp (195);
2714            ## Stay in the state
2715            !!!next-input-character;
2716            redo A;
2717          } elsif ($self->{next_char} == 0x0022) { # "
2718            !!!cp (196);
2719            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2720            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2721            !!!next-input-character;
2722            redo A;
2723          } elsif ($self->{next_char} == 0x0027) { # '
2724            !!!cp (197);
2725            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2726            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2727            !!!next-input-character;
2728            redo A;
2729          } elsif ($self->{next_char} == 0x003E) { # >
2730            !!!cp (198);
2731            $self->{state} = DATA_STATE;
2732          !!!next-input-character;          !!!next-input-character;
2733    
2734          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2735    
2736          redo A;          redo A;
2737        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2738            !!!cp (199);
2739          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2740          $self->{state} = 'data';  
2741            $self->{state} = DATA_STATE;
2742          ## reconsume          ## reconsume
2743    
2744            $self->{current_token}->{quirks} = 1;
2745          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2746    
2747          redo A;          redo A;
2748        } else {        } else {
2749          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (200);
2750          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after PUBLIC literal');
2751          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2752    
2753            $self->{state} = BOGUS_DOCTYPE_STATE;
2754          !!!next-input-character;          !!!next-input-character;
2755          redo A;          redo A;
2756        }        }
2757      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2758        if ($self->{next_input_character} == 0x003E) { # >        if ({
2759          $self->{state} = 'data';              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2760                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2761              }->{$self->{next_char}}) {
2762            !!!cp (201);
2763            ## Stay in the state
2764            !!!next-input-character;
2765            redo A;
2766          } elsif ($self->{next_char} == 0x0022) { # "
2767            !!!cp (202);
2768            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2769            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2770            !!!next-input-character;
2771            redo A;
2772          } elsif ($self->{next_char} == 0x0027) { # '
2773            !!!cp (203);
2774            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2775            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2776            !!!next-input-character;
2777            redo A;
2778          } elsif ($self->{next_char} == 0x003E) { # >
2779            !!!cp (204);
2780            !!!parse-error (type => 'no SYSTEM literal');
2781            $self->{state} = DATA_STATE;
2782          !!!next-input-character;          !!!next-input-character;
2783    
2784            $self->{current_token}->{quirks} = 1;
2785          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2786    
2787          redo A;          redo A;
2788        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2789            !!!cp (205);
2790          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2791          $self->{state} = 'data';  
2792            $self->{state} = DATA_STATE;
2793            ## reconsume
2794    
2795            $self->{current_token}->{quirks} = 1;
2796            !!!emit ($self->{current_token}); # DOCTYPE
2797    
2798            redo A;
2799          } else {
2800            !!!cp (206);
2801            !!!parse-error (type => 'string after SYSTEM');
2802            $self->{current_token}->{quirks} = 1;
2803    
2804            $self->{state} = BOGUS_DOCTYPE_STATE;
2805            !!!next-input-character;
2806            redo A;
2807          }
2808        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2809          if ($self->{next_char} == 0x0022) { # "
2810            !!!cp (207);
2811            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2812            !!!next-input-character;
2813            redo A;
2814          } elsif ($self->{next_char} == 0x003E) { # >
2815            !!!cp (208);
2816            !!!parse-error (type => 'unclosed SYSTEM literal');
2817    
2818            $self->{state} = DATA_STATE;
2819            !!!next-input-character;
2820    
2821            $self->{current_token}->{quirks} = 1;
2822            !!!emit ($self->{current_token}); # DOCTYPE
2823    
2824            redo A;
2825          } elsif ($self->{next_char} == -1) {
2826            !!!cp (209);
2827            !!!parse-error (type => 'unclosed SYSTEM literal');
2828    
2829            $self->{state} = DATA_STATE;
2830          ## reconsume          ## reconsume
2831    
2832            $self->{current_token}->{quirks} = 1;
2833          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2834    
2835          redo A;          redo A;
2836        } else {        } else {
2837            !!!cp (210);
2838            $self->{current_token}->{system_identifier} # DOCTYPE
2839                .= chr $self->{next_char};
2840            $self->{read_until}->($self->{current_token}->{system_identifier},
2841                                  q[">],
2842                                  length $self->{current_token}->{system_identifier});
2843    
2844          ## Stay in the state          ## Stay in the state
2845          !!!next-input-character;          !!!next-input-character;
2846          redo A;          redo A;
2847        }        }
2848      } else {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2849        die "$0: $self->{state}: Unknown state";        if ($self->{next_char} == 0x0027) { # '
2850      }          !!!cp (211);
2851    } # A            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2852            !!!next-input-character;
2853            redo A;
2854          } elsif ($self->{next_char} == 0x003E) { # >
2855            !!!cp (212);
2856            !!!parse-error (type => 'unclosed SYSTEM literal');
2857    
2858    die "$0: _get_next_token: unexpected case";          $self->{state} = DATA_STATE;
2859  } # _get_next_token          !!!next-input-character;
2860    
2861  sub _tokenize_attempt_to_consume_an_entity ($) {          $self->{current_token}->{quirks} = 1;
2862    my $self = shift;          !!!emit ($self->{current_token}); # DOCTYPE
     
   if ($self->{next_input_character} == 0x0023) { # #  
     !!!next-input-character;  
     my $num;  
     if ($self->{next_input_character} == 0x0078 or # x  
         $self->{next_input_character} == 0x0058) { # X  
       X: {  
         my $x_char = $self->{next_input_character};  
         !!!next-input-character;  
         if (0x0030 <= $self->{next_input_character} and  
             $self->{next_input_character} <= 0x0039) { # 0..9  
           $num ||= 0;  
           $num *= 0x10;  
           $num += $self->{next_input_character} - 0x0030;  
           redo X;  
         } elsif (0x0061 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0066) { # a..f  
           ## ISSUE: the spec says U+0078, which is apparently incorrect  
           $num ||= 0;  
           $num *= 0x10;  
           $num += $self->{next_input_character} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0046) { # A..F  
           ## ISSUE: the spec says U+0058, which is apparently incorrect  
           $num ||= 0;  
           $num *= 0x10;  
           $num += $self->{next_input_character} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $num) { # no hexadecimal digit  
           !!!parse-error (type => 'bare hcro');  
           $self->{next_input_character} = 0x0023; # #  
           !!!back-next-input-character ($x_char);  
           return undef;  
         } elsif ($self->{next_input_character} == 0x003B) { # ;  
           !!!next-input-character;  
         } else {  
           !!!parse-error (type => 'no refc');  
         }  
2863    
2864          ## TODO: check the definition for |a valid Unicode character|.          redo A;
2865          if ($num > 1114111 or $num == 0) {        } elsif ($self->{next_char} == -1) {
2866            $num = 0xFFFD; # REPLACEMENT CHARACTER          !!!cp (213);
2867            ## ISSUE: Why this is not an error?          !!!parse-error (type => 'unclosed SYSTEM literal');
         }  
2868    
2869          return {type => 'character', data => chr $num};          $self->{state} = DATA_STATE;
2870        } # X          ## reconsume
2871      } elsif (0x0030 <= $self->{next_input_character} and  
2872               $self->{next_input_character} <= 0x0039) { # 0..9          $self->{current_token}->{quirks} = 1;
2873        my $code = $self->{next_input_character} - 0x0030;          !!!emit ($self->{current_token}); # DOCTYPE
2874        !!!next-input-character;  
2875            redo A;
2876          } else {
2877            !!!cp (214);
2878            $self->{current_token}->{system_identifier} # DOCTYPE
2879                .= chr $self->{next_char};
2880            $self->{read_until}->($self->{current_token}->{system_identifier},
2881                                  q['>],
2882                                  length $self->{current_token}->{system_identifier});
2883    
2884            ## Stay in the state
2885            !!!next-input-character;
2886            redo A;
2887          }
2888        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2889          if ({
2890                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2891                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2892              }->{$self->{next_char}}) {
2893            !!!cp (215);
2894            ## Stay in the state
2895            !!!next-input-character;
2896            redo A;
2897          } elsif ($self->{next_char} == 0x003E) { # >
2898            !!!cp (216);
2899            $self->{state} = DATA_STATE;
2900            !!!next-input-character;
2901    
2902            !!!emit ($self->{current_token}); # DOCTYPE
2903    
2904            redo A;
2905          } elsif ($self->{next_char} == -1) {
2906            !!!cp (217);
2907            !!!parse-error (type => 'unclosed DOCTYPE');
2908            $self->{state} = DATA_STATE;
2909            ## reconsume
2910    
2911            $self->{current_token}->{quirks} = 1;
2912            !!!emit ($self->{current_token}); # DOCTYPE
2913    
2914            redo A;
2915          } else {
2916            !!!cp (218);
2917            !!!parse-error (type => 'string after SYSTEM literal');
2918            #$self->{current_token}->{quirks} = 1;
2919    
2920            $self->{state} = BOGUS_DOCTYPE_STATE;
2921            !!!next-input-character;
2922            redo A;
2923          }
2924        } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2925          if ($self->{next_char} == 0x003E) { # >
2926            !!!cp (219);
2927            $self->{state} = DATA_STATE;
2928            !!!next-input-character;
2929    
2930            !!!emit ($self->{current_token}); # DOCTYPE
2931    
2932            redo A;
2933          } elsif ($self->{next_char} == -1) {
2934            !!!cp (220);
2935            !!!parse-error (type => 'unclosed DOCTYPE');
2936            $self->{state} = DATA_STATE;
2937            ## reconsume
2938    
2939            !!!emit ($self->{current_token}); # DOCTYPE
2940    
2941            redo A;
2942          } else {
2943            !!!cp (221);
2944            my $s = '';
2945            $self->{read_until}->($s, q[>], 0);
2946    
2947            ## Stay in the state
2948            !!!next-input-character;
2949            redo A;
2950          }
2951        } elsif ($self->{state} == CDATA_SECTION_STATE) {
2952          ## NOTE: "CDATA section state" in the state is jointly implemented
2953          ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2954          ## and |CDATA_SECTION_MSE2_STATE|.
2955                
2956        while (0x0030 <= $self->{next_input_character} and        if ($self->{next_char} == 0x005D) { # ]
2957                  $self->{next_input_character} <= 0x0039) { # 0..9          !!!cp (221.1);
2958          $code *= 10;          $self->{state} = CDATA_SECTION_MSE1_STATE;
         $code += $self->{next_input_character} - 0x0030;  
           
2959          !!!next-input-character;          !!!next-input-character;
2960            redo A;
2961          } elsif ($self->{next_char} == -1) {
2962            $self->{state} = DATA_STATE;
2963            !!!next-input-character;
2964            if (length $self->{current_token}->{data}) { # character
2965              !!!cp (221.2);
2966              !!!emit ($self->{current_token}); # character
2967            } else {
2968              !!!cp (221.3);
2969              ## No token to emit. $self->{current_token} is discarded.
2970            }        
2971            redo A;
2972          } else {
2973            !!!cp (221.4);
2974            $self->{current_token}->{data} .= chr $self->{next_char};
2975            $self->{read_until}->($self->{current_token}->{data},
2976                                  q<]>,
2977                                  length $self->{current_token}->{data});
2978    
2979            ## Stay in the state.
2980            !!!next-input-character;
2981            redo A;
2982        }        }
2983    
2984        if ($self->{next_input_character} == 0x003B) { # ;        ## ISSUE: "text tokens" in spec.
2985        } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2986          if ($self->{next_char} == 0x005D) { # ]
2987            !!!cp (221.5);
2988            $self->{state} = CDATA_SECTION_MSE2_STATE;
2989          !!!next-input-character;          !!!next-input-character;
2990            redo A;
2991        } else {        } else {
2992          !!!parse-error (type => 'no refc');          !!!cp (221.6);
2993            $self->{current_token}->{data} .= ']';
2994            $self->{state} = CDATA_SECTION_STATE;
2995            ## Reconsume.
2996            redo A;
2997          }
2998        } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2999          if ($self->{next_char} == 0x003E) { # >
3000            $self->{state} = DATA_STATE;
3001            !!!next-input-character;
3002            if (length $self->{current_token}->{data}) { # character
3003              !!!cp (221.7);
3004              !!!emit ($self->{current_token}); # character
3005            } else {
3006              !!!cp (221.8);
3007              ## No token to emit. $self->{current_token} is discarded.
3008            }
3009            redo A;
3010          } elsif ($self->{next_char} == 0x005D) { # ]
3011            !!!cp (221.9); # character
3012            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
3013            ## Stay in the state.
3014            !!!next-input-character;
3015            redo A;
3016          } else {
3017            !!!cp (221.11);
3018            $self->{current_token}->{data} .= ']]'; # character
3019            $self->{state} = CDATA_SECTION_STATE;
3020            ## Reconsume.
3021            redo A;
3022          }
3023        } elsif ($self->{state} == ENTITY_STATE) {
3024          if ({
3025            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3026            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3027            $self->{entity_additional} => 1,
3028          }->{$self->{next_char}}) {
3029            !!!cp (1001);
3030            ## Don't consume
3031            ## No error
3032            ## Return nothing.
3033            #
3034          } elsif ($self->{next_char} == 0x0023) { # #
3035            !!!cp (999);
3036            $self->{state} = ENTITY_HASH_STATE;
3037            $self->{state_keyword} = '#';
3038            !!!next-input-character;
3039            redo A;
3040          } elsif ((0x0041 <= $self->{next_char} and
3041                    $self->{next_char} <= 0x005A) or # A..Z
3042                   (0x0061 <= $self->{next_char} and
3043                    $self->{next_char} <= 0x007A)) { # a..z
3044            !!!cp (998);
3045            require Whatpm::_NamedEntityList;
3046            $self->{state} = ENTITY_NAME_STATE;
3047            $self->{state_keyword} = chr $self->{next_char};
3048            $self->{entity__value} = $self->{state_keyword};
3049            $self->{entity__match} = 0;
3050            !!!next-input-character;
3051            redo A;
3052          } else {
3053            !!!cp (1027);
3054            !!!parse-error (type => 'bare ero');
3055            ## Return nothing.
3056            #
3057        }        }
3058    
3059        ## TODO: check the definition for |a valid Unicode character|.        ## NOTE: No character is consumed by the "consume a character
3060        if ($code > 1114111 or $code == 0) {        ## reference" algorithm.  In other word, there is an "&" character
3061          $code = 0xFFFD; # REPLACEMENT CHARACTER        ## that does not introduce a character reference, which would be
3062          ## ISSUE: Why this is not an error?        ## appended to the parent element or the attribute value in later
3063          ## process of the tokenizer.
3064    
3065          if ($self->{prev_state} == DATA_STATE) {
3066            !!!cp (997);
3067            $self->{state} = $self->{prev_state};
3068            ## Reconsume.
3069            !!!emit ({type => CHARACTER_TOKEN, data => '&',
3070                      line => $self->{line_prev},
3071                      column => $self->{column_prev},
3072                     });
3073            redo A;
3074          } else {
3075            !!!cp (996);
3076            $self->{current_attribute}->{value} .= '&';
3077            $self->{state} = $self->{prev_state};
3078            ## Reconsume.
3079            redo A;
3080        }        }
3081              } elsif ($self->{state} == ENTITY_HASH_STATE) {
3082        return {type => 'character', data => chr $code};        if ($self->{next_char} == 0x0078 or # x
3083      } else {            $self->{next_char} == 0x0058) { # X
3084        !!!parse-error (type => 'bare nero');          !!!cp (995);
3085        !!!back-next-input-character ($self->{next_input_character});          $self->{state} = HEXREF_X_STATE;
3086        $self->{next_input_character} = 0x0023; # #          $self->{state_keyword} .= chr $self->{next_char};
3087        return undef;          !!!next-input-character;
3088      }          redo A;
3089    } elsif ((0x0041 <= $self->{next_input_character} and        } elsif (0x0030 <= $self->{next_char} and
3090              $self->{next_input_character} <= 0x005A) or                 $self->{next_char} <= 0x0039) { # 0..9
3091             (0x0061 <= $self->{next_input_character} and          !!!cp (994);
3092              $self->{next_input_character} <= 0x007A)) {          $self->{state} = NCR_NUM_STATE;
3093      my $entity_name = chr $self->{next_input_character};          $self->{state_keyword} = $self->{next_char} - 0x0030;
3094      !!!next-input-character;          !!!next-input-character;
3095            redo A;
     my $value = $entity_name;  
     my $match;  
   
     while (length $entity_name < 10 and  
            ## NOTE: Some number greater than the maximum length of entity name  
            ((0x0041 <= $self->{next_input_character} and  
              $self->{next_input_character} <= 0x005A) or  
             (0x0061 <= $self->{next_input_character} and  
              $self->{next_input_character} <= 0x007A) or  
             (0x0030 <= $self->{next_input_character} and  
              $self->{next_input_character} <= 0x0039))) {  
       $entity_name .= chr $self->{next_input_character};  
       if (defined $entity_char->{$entity_name}) {  
         $value = $entity_char->{$entity_name};  
         $match = 1;  
3096        } else {        } else {
3097          $value .= chr $self->{next_input_character};          !!!parse-error (type => 'bare nero',
3098                            line => $self->{line_prev},
3099                            column => $self->{column_prev} - 1);
3100    
3101            ## NOTE: According to the spec algorithm, nothing is returned,
3102            ## and then "&#" is appended to the parent element or the attribute
3103            ## value in the later processing.
3104    
3105            if ($self->{prev_state} == DATA_STATE) {
3106              !!!cp (1019);
3107              $self->{state} = $self->{prev_state};
3108              ## Reconsume.
3109              !!!emit ({type => CHARACTER_TOKEN,
3110                        data => '&#',
3111                        line => $self->{line_prev},
3112                        column => $self->{column_prev} - 1,
3113                       });
3114              redo A;
3115            } else {
3116              !!!cp (993);
3117              $self->{current_attribute}->{value} .= '&#';
3118              $self->{state} = $self->{prev_state};
3119              ## Reconsume.
3120              redo A;
3121            }
3122        }        }
3123        !!!next-input-character;      } elsif ($self->{state} == NCR_NUM_STATE) {
3124      }        if (0x0030 <= $self->{next_char} and
3125                  $self->{next_char} <= 0x0039) { # 0..9
3126      if ($match) {          !!!cp (1012);
3127        if ($self->{next_input_character} == 0x003B) { # ;          $self->{state_keyword} *= 10;
3128            $self->{state_keyword} += $self->{next_char} - 0x0030;
3129            
3130            ## Stay in the state.
3131            !!!next-input-character;
3132            redo A;
3133          } elsif ($self->{next_char} == 0x003B) { # ;
3134            !!!cp (1013);
3135          !!!next-input-character;          !!!next-input-character;
3136            #
3137        } else {        } else {
3138          !!!parse-error (type => 'refc');          !!!cp (1014);
3139            !!!parse-error (type => 'no refc');
3140            ## Reconsume.
3141            #
3142          }
3143    
3144          my $code = $self->{state_keyword};
3145          my $l = $self->{line_prev};
3146          my $c = $self->{column_prev};
3147          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3148            !!!cp (1015);
3149            !!!parse-error (type => 'invalid character reference',
3150                            text => (sprintf 'U+%04X', $code),
3151                            line => $l, column => $c);
3152            $code = 0xFFFD;
3153          } elsif ($code > 0x10FFFF) {
3154            !!!cp (1016);
3155            !!!parse-error (type => 'invalid character reference',
3156                            text => (sprintf 'U-%08X', $code),
3157                            line => $l, column => $c);
3158            $code = 0xFFFD;
3159          } elsif ($code == 0x000D) {
3160            !!!cp (1017);
3161            !!!parse-error (type => 'CR character reference',
3162                            line => $l, column => $c);
3163            $code = 0x000A;
3164          } elsif (0x80 <= $code and $code <= 0x9F) {
3165            !!!cp (1018);
3166            !!!parse-error (type => 'C1 character reference',
3167                            text => (sprintf 'U+%04X', $code),
3168                            line => $l, column => $c);
3169            $code = $c1_entity_char->{$code};
3170          }
3171    
3172          if ($self->{prev_state} == DATA_STATE) {
3173            !!!cp (992);
3174            $self->{state} = $self->{prev_state};
3175            ## Reconsume.
3176            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3177                      line => $l, column => $c,
3178                     });
3179            redo A;
3180          } else {
3181            !!!cp (991);
3182            $self->{current_attribute}->{value} .= chr $code;
3183            $self->{current_attribute}->{has_reference} = 1;
3184            $self->{state} = $self->{prev_state};
3185            ## Reconsume.
3186            redo A;
3187          }
3188        } elsif ($self->{state} == HEXREF_X_STATE) {
3189          if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3190              (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3191              (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3192            # 0..9, A..F, a..f
3193            !!!cp (990);
3194            $self->{state} = HEXREF_HEX_STATE;
3195            $self->{state_keyword} = 0;
3196            ## Reconsume.
3197            redo A;
3198          } else {
3199            !!!parse-error (type => 'bare hcro',
3200                            line => $self->{line_prev},
3201                            column => $self->{column_prev} - 2);
3202    
3203            ## NOTE: According to the spec algorithm, nothing is returned,
3204            ## and then "&#" followed by "X" or "x" is appended to the parent
3205            ## element or the attribute value in the later processing.
3206    
3207            if ($self->{prev_state} == DATA_STATE) {
3208              !!!cp (1005);
3209              $self->{state} = $self->{prev_state};
3210              ## Reconsume.
3211              !!!emit ({type => CHARACTER_TOKEN,
3212                        data => '&' . $self->{state_keyword},
3213                        line => $self->{line_prev},
3214                        column => $self->{column_prev} - length $self->{state_keyword},
3215                       });
3216              redo A;
3217            } else {
3218              !!!cp (989);
3219              $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3220              $self->{state} = $self->{prev_state};
3221              ## Reconsume.
3222              redo A;
3223            }
3224          }
3225        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3226          if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3227            # 0..9
3228            !!!cp (1002);
3229            $self->{state_keyword} *= 0x10;
3230            $self->{state_keyword} += $self->{next_char} - 0x0030;
3231            ## Stay in the state.
3232            !!!next-input-character;
3233            redo A;
3234          } elsif (0x0061 <= $self->{next_char} and
3235                   $self->{next_char} <= 0x0066) { # a..f
3236            !!!cp (1003);
3237            $self->{state_keyword} *= 0x10;
3238            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3239            ## Stay in the state.
3240            !!!next-input-character;
3241            redo A;
3242          } elsif (0x0041 <= $self->{next_char} and
3243                   $self->{next_char} <= 0x0046) { # A..F
3244            !!!cp (1004);
3245            $self->{state_keyword} *= 0x10;
3246            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3247            ## Stay in the state.
3248            !!!next-input-character;
3249            redo A;
3250          } elsif ($self->{next_char} == 0x003B) { # ;
3251            !!!cp (1006);
3252            !!!next-input-character;
3253            #
3254          } else {
3255            !!!cp (1007);
3256            !!!parse-error (type => 'no refc',
3257                            line => $self->{line},
3258                            column => $self->{column});
3259            ## Reconsume.
3260            #
3261        }        }
3262    
3263        return {type => 'character', data => $value};        my $code = $self->{state_keyword};
3264          my $l = $self->{line_prev};
3265          my $c = $self->{column_prev};
3266          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3267            !!!cp (1008);
3268            !!!parse-error (type => 'invalid character reference',
3269                            text => (sprintf 'U+%04X', $code),
3270                            line => $l, column => $c);
3271            $code = 0xFFFD;
3272          } elsif ($code > 0x10FFFF) {
3273            !!!cp (1009);
3274            !!!parse-error (type => 'invalid character reference',
3275                            text => (sprintf 'U-%08X', $code),
3276                            line => $l, column => $c);
3277            $code = 0xFFFD;
3278          } elsif ($code == 0x000D) {
3279            !!!cp (1010);
3280            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3281            $code = 0x000A;
3282          } elsif (0x80 <= $code and $code <= 0x9F) {
3283            !!!cp (1011);
3284            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3285            $code = $c1_entity_char->{$code};
3286          }
3287    
3288          if ($self->{prev_state} == DATA_STATE) {
3289            !!!cp (988);
3290            $self->{state} = $self->{prev_state};
3291            ## Reconsume.
3292            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3293                      line => $l, column => $c,
3294                     });
3295            redo A;
3296          } else {
3297            !!!cp (987);
3298            $self->{current_attribute}->{value} .= chr $code;
3299            $self->{current_attribute}->{has_reference} = 1;
3300            $self->{state} = $self->{prev_state};
3301            ## Reconsume.
3302            redo A;
3303          }
3304        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3305          if (length $self->{state_keyword} < 30 and
3306              ## NOTE: Some number greater than the maximum length of entity name
3307              ((0x0041 <= $self->{next_char} and # a
3308                $self->{next_char} <= 0x005A) or # x
3309               (0x0061 <= $self->{next_char} and # a
3310                $self->{next_char} <= 0x007A) or # z
3311               (0x0030 <= $self->{next_char} and # 0
3312                $self->{next_char} <= 0x0039) or # 9
3313               $self->{next_char} == 0x003B)) { # ;
3314            our $EntityChar;
3315            $self->{state_keyword} .= chr $self->{next_char};
3316            if (defined $EntityChar->{$self->{state_keyword}}) {
3317              if ($self->{next_char} == 0x003B) { # ;
3318                !!!cp (1020);
3319                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3320                $self->{entity__match} = 1;
3321                !!!next-input-character;
3322                #
3323              } else {
3324                !!!cp (1021);
3325                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3326                $self->{entity__match} = -1;
3327                ## Stay in the state.
3328                !!!next-input-character;
3329                redo A;
3330              }
3331            } else {
3332              !!!cp (1022);
3333              $self->{entity__value} .= chr $self->{next_char};
3334              $self->{entity__match} *= 2;
3335              ## Stay in the state.
3336              !!!next-input-character;
3337              redo A;
3338            }
3339          }
3340    
3341          my $data;
3342          my $has_ref;
3343          if ($self->{entity__match} > 0) {
3344            !!!cp (1023);
3345            $data = $self->{entity__value};
3346            $has_ref = 1;
3347            #
3348          } elsif ($self->{entity__match} < 0) {
3349            !!!parse-error (type => 'no refc');
3350            if ($self->{prev_state} != DATA_STATE and # in attribute
3351                $self->{entity__match} < -1) {
3352              !!!cp (1024);
3353              $data = '&' . $self->{state_keyword};
3354              #
3355            } else {
3356              !!!cp (1025);
3357              $data = $self->{entity__value};
3358              $has_ref = 1;
3359              #
3360            }
3361          } else {
3362            !!!cp (1026);
3363            !!!parse-error (type => 'bare ero',
3364                            line => $self->{line_prev},
3365                            column => $self->{column_prev} - length $self->{state_keyword});
3366            $data = '&' . $self->{state_keyword};
3367            #
3368          }
3369      
3370          ## NOTE: In these cases, when a character reference is found,
3371          ## it is consumed and a character token is returned, or, otherwise,
3372          ## nothing is consumed and returned, according to the spec algorithm.
3373          ## In this implementation, anything that has been examined by the
3374          ## tokenizer is appended to the parent element or the attribute value
3375          ## as string, either literal string when no character reference or
3376          ## entity-replaced string otherwise, in this stage, since any characters
3377          ## that would not be consumed are appended in the data state or in an
3378          ## appropriate attribute value state anyway.
3379    
3380          if ($self->{prev_state} == DATA_STATE) {
3381            !!!cp (986);
3382            $self->{state} = $self->{prev_state};
3383            ## Reconsume.
3384            !!!emit ({type => CHARACTER_TOKEN,
3385                      data => $data,
3386                      line => $self->{line_prev},
3387                      column => $self->{column_prev} + 1 - length $self->{state_keyword},
3388                     });
3389            redo A;
3390          } else {
3391            !!!cp (985);
3392            $self->{current_attribute}->{value} .= $data;
3393            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3394            $self->{state} = $self->{prev_state};
3395            ## Reconsume.
3396            redo A;
3397          }
3398      } else {      } else {
3399        !!!parse-error (type => 'bare ero');        die "$0: $self->{state}: Unknown state";
       ## NOTE: No characters are consumed in the spec.  
       !!!back-token ({type => 'character', data => $value});  
       return undef;  
3400      }      }
3401    } else {    } # A  
3402      ## no characters are consumed  
3403      !!!parse-error (type => 'bare ero');    die "$0: _get_next_token: unexpected case";
3404      return undef;  } # _get_next_token
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3405    
3406  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3407    my $self = shift;    my $self = shift;
# Line 1586  sub _initialize_tree_constructor ($) { Line 3409  sub _initialize_tree_constructor ($) {
3409    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
3410    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3411    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3412    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
3413      $self->{document}->set_user_data (manakai_source_line => 1);
3414      $self->{document}->set_user_data (manakai_source_column => 1);
3415  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3416    
3417  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1613  sub _construct_tree ($) { Line 3438  sub _construct_tree ($) {
3438        
3439    !!!next-token;    !!!next-token;
3440    
   $self->{insertion_mode} = 'before head';  
3441    undef $self->{form_element};    undef $self->{form_element};
3442    undef $self->{head_element};    undef $self->{head_element};
3443    $self->{open_elements} = [];    $self->{open_elements} = [];
3444    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3445    
3446      ## NOTE: The "initial" insertion mode.
3447    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3448    
3449      ## NOTE: The "before html" insertion mode.
3450    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3451      $self->{insertion_mode} = BEFORE_HEAD_IM;
3452    
3453      ## NOTE: The "before head" insertion mode and so on.
3454    $self->_tree_construction_main;    $self->_tree_construction_main;
3455  } # _construct_tree  } # _construct_tree
3456    
3457  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3458    my $self = shift;    my $self = shift;
3459    B: {  
3460        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
3461          if ($token->{error}) {  
3462            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
3463            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
3464          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3465          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
3466            ($token->{name});        ## language.
3467          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
3468          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
3469          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3470          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
3471          return;            defined $token->{system_identifier}) {
3472        } elsif ({          !!!cp ('t1');
3473                  comment => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3474                  'start tag' => 1,        } elsif ($doctype_name ne 'HTML') {
3475                  'end tag' => 1,          !!!cp ('t2');
3476                  'end-of-file' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3477                 }->{$token->{type}}) {        } elsif (defined $token->{public_identifier}) {
3478          ## ISSUE: Spec currently left this case undefined.          if ($token->{public_identifier} eq 'XSLT-compat') {
3479          !!!parse-error (type => 'missing DOCTYPE');            !!!cp ('t1.2');
3480          #$phase = 'root element';            !!!parse-error (type => 'XSLT-compat', token => $token,
3481          ## reprocess                            level => $self->{level}->{should});
3482          #redo B;          } else {
3483          return;            !!!parse-error (type => 'not HTML5', token => $token);
3484        } elsif ($token->{type} eq 'character') {          }
3485          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } else {
3486            $self->{document}->manakai_append_text ($1);          !!!cp ('t3');
3487            ## ISSUE: DOM3 Core does not allow Document > Text          #
3488            unless (length $token->{data}) {        }
3489              ## Stay in the phase        
3490              !!!next-token;        my $doctype = $self->{document}->create_document_type_definition
3491              redo B;          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3492          ## NOTE: Default value for both |public_id| and |system_id| attributes
3493          ## are empty strings, so that we don't set any value in missing cases.
3494          $doctype->public_id ($token->{public_identifier})
3495              if defined $token->{public_identifier};
3496          $doctype->system_id ($token->{system_identifier})
3497              if defined $token->{system_identifier};
3498          ## NOTE: Other DocumentType attributes are null or empty lists.
3499          ## ISSUE: internalSubset = null??
3500          $self->{document}->append_child ($doctype);
3501          
3502          if ($token->{quirks} or $doctype_name ne 'HTML') {
3503            !!!cp ('t4');
3504            $self->{document}->manakai_compat_mode ('quirks');
3505          } elsif (defined $token->{public_identifier}) {
3506            my $pubid = $token->{public_identifier};
3507            $pubid =~ tr/a-z/A-z/;
3508            my $prefix = [
3509              "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3510              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3511              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3512              "-//IETF//DTD HTML 2.0 LEVEL 1//",
3513              "-//IETF//DTD HTML 2.0 LEVEL 2//",
3514              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3515              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3516              "-//IETF//DTD HTML 2.0 STRICT//",
3517              "-//IETF//DTD HTML 2.0//",
3518              "-//IETF//DTD HTML 2.1E//",
3519              "-//IETF//DTD HTML 3.0//",
3520              "-//IETF//DTD HTML 3.2 FINAL//",
3521              "-//IETF//DTD HTML 3.2//",
3522              "-//IETF//DTD HTML 3//",
3523              "-//IETF//DTD HTML LEVEL 0//",
3524              "-//IETF//DTD HTML LEVEL 1//",
3525              "-//IETF//DTD HTML LEVEL 2//",
3526              "-//IETF//DTD HTML LEVEL 3//",
3527              "-//IETF//DTD HTML STRICT LEVEL 0//",
3528              "-//IETF//DTD HTML STRICT LEVEL 1//",
3529              "-//IETF//DTD HTML STRICT LEVEL 2//",
3530              "-//IETF//DTD HTML STRICT LEVEL 3//",
3531              "-//IETF//DTD HTML STRICT//",
3532              "-//IETF//DTD HTML//",
3533              "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3534              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3535              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3536              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3537              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3538              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3539              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3540              "-//NETSCAPE COMM. CORP.//DTD HTML//",
3541              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3542              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3543              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3544              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3545              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3546              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3547              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3548              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3549              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3550              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3551              "-//W3C//DTD HTML 3 1995-03-24//",
3552              "-//W3C//DTD HTML 3.2 DRAFT//",
3553              "-//W3C//DTD HTML 3.2 FINAL//",
3554              "-//W3C//DTD HTML 3.2//",
3555              "-//W3C//DTD HTML 3.2S DRAFT//",
3556              "-//W3C//DTD HTML 4.0 FRAMESET//",
3557              "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3558              "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3559              "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3560              "-//W3C//DTD W3 HTML//",
3561              "-//W3O//DTD W3 HTML 3.0//",
3562              "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3563              "-//WEBTECHS//DTD MOZILLA HTML//",
3564            ]; # $prefix
3565            my $match;
3566            for (@$prefix) {
3567              if (substr ($prefix, 0, length $_) eq $_) {
3568                $match = 1;
3569                last;
3570              }
3571            }
3572            if ($match or
3573                $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3574                $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3575                $pubid eq "HTML") {
3576              !!!cp ('t5');
3577              $self->{document}->manakai_compat_mode ('quirks');
3578            } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3579                     $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3580              if (defined $token->{system_identifier}) {
3581                !!!cp ('t6');
3582                $self->{document}->manakai_compat_mode ('quirks');
3583              } else {
3584                !!!cp ('t7');
3585                $self->{document}->manakai_compat_mode ('limited quirks');
3586            }            }
3587            } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3588                     $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3589              !!!cp ('t8');
3590              $self->{document}->manakai_compat_mode ('limited quirks');
3591            } else {
3592              !!!cp ('t9');
3593            }
3594          } else {
3595            !!!cp ('t10');
3596          }
3597          if (defined $token->{system_identifier}) {
3598            my $sysid = $token->{system_identifier};
3599            $sysid =~ tr/A-Z/a-z/;
3600            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3601              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3602              ## marked as quirks.
3603              $self->{document}->manakai_compat_mode ('quirks');
3604              !!!cp ('t11');
3605            } else {
3606              !!!cp ('t12');
3607            }
3608          } else {
3609            !!!cp ('t13');
3610          }
3611          
3612          ## Go to the "before html" insertion mode.
3613          !!!next-token;
3614          return;
3615        } elsif ({
3616                  START_TAG_TOKEN, 1,
3617                  END_TAG_TOKEN, 1,
3618                  END_OF_FILE_TOKEN, 1,
3619                 }->{$token->{type}}) {
3620          !!!cp ('t14');
3621          !!!parse-error (type => 'no DOCTYPE', token => $token);
3622          $self->{document}->manakai_compat_mode ('quirks');
3623          ## Go to the "before html" insertion mode.
3624          ## reprocess
3625          !!!ack-later;
3626          return;
3627        } elsif ($token->{type} == CHARACTER_TOKEN) {
3628          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3629            ## Ignore the token
3630    
3631            unless (length $token->{data}) {
3632              !!!cp ('t15');
3633              ## Stay in the insertion mode.
3634              !!!next-token;
3635              redo INITIAL;
3636            } else {
3637              !!!cp ('t16');
3638          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
3639        } else {        } else {
3640          die "$0: $token->{type}: Unknown token";          !!!cp ('t17');
3641        }        }
3642      } # B  
3643          !!!parse-error (type => 'no DOCTYPE', token => $token);
3644          $self->{document}->manakai_compat_mode ('quirks');
3645          ## Go to the "before html" insertion mode.
3646          ## reprocess
3647          return;
3648        } elsif ($token->{type} == COMMENT_TOKEN) {
3649          !!!cp ('t18');
3650          my $comment = $self->{document}->create_comment ($token->{data});
3651          $self->{document}->append_child ($comment);
3652          
3653          ## Stay in the insertion mode.
3654          !!!next-token;
3655          redo INITIAL;
3656        } else {
3657          die "$0: $token->{type}: Unknown token type";
3658        }
3659      } # INITIAL
3660    
3661      die "$0: _tree_construction_initial: This should be never reached";
3662  } # _tree_construction_initial  } # _tree_construction_initial
3663    
3664  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3665    my $self = shift;    my $self = shift;
3666    
3667      ## NOTE: "before html" insertion mode.
3668        
3669    B: {    B: {
3670        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3671          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3672            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3673          ## Ignore the token          ## Ignore the token
3674          ## Stay in the phase          ## Stay in the insertion mode.
3675          !!!next-token;          !!!next-token;
3676          redo B;          redo B;
3677        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3678            !!!cp ('t20');
3679          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3680          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3681          ## Stay in the phase          ## Stay in the insertion mode.
3682          !!!next-token;          !!!next-token;
3683          redo B;          redo B;
3684        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3685          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3686            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
3687            ## ISSUE: DOM3 Core does not allow Document > Text  
3688            unless (length $token->{data}) {            unless (length $token->{data}) {
3689              ## Stay in the phase              !!!cp ('t21');
3690                ## Stay in the insertion mode.
3691              !!!next-token;              !!!next-token;
3692              redo B;              redo B;
3693              } else {
3694                !!!cp ('t22');
3695            }            }
3696            } else {
3697              !!!cp ('t23');
3698          }          }
3699    
3700            $self->{application_cache_selection}->(undef);
3701    
3702          #          #
3703          } elsif ($token->{type} == START_TAG_TOKEN) {
3704            if ($token->{tag_name} eq 'html') {
3705              my $root_element;
3706              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3707              $self->{document}->append_child ($root_element);
3708              push @{$self->{open_elements}},
3709                  [$root_element, $el_category->{html}];
3710    
3711              if ($token->{attributes}->{manifest}) {
3712                !!!cp ('t24');
3713                $self->{application_cache_selection}
3714                    ->($token->{attributes}->{manifest}->{value});
3715                ## ISSUE: Spec is unclear on relative references.
3716                ## According to Hixie (#whatwg 2008-03-19), it should be
3717                ## resolved against the base URI of the document in HTML
3718                ## or xml:base of the element in XHTML.
3719              } else {
3720                !!!cp ('t25');
3721                $self->{application_cache_selection}->(undef);
3722              }
3723    
3724              !!!nack ('t25c');
3725    
3726              !!!next-token;
3727              return; ## Go to the "before head" insertion mode.
3728            } else {
3729              !!!cp ('t25.1');
3730              #
3731            }
3732        } elsif ({        } elsif ({
3733                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3734                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3735                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3736          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3737          #          #
3738        } else {        } else {
3739          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3740        }        }
3741        my $root_element; !!!create-element ($root_element, 'html');  
3742        $self->{document}->append_child ($root_element);      my $root_element;
3743        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3744        #$phase = 'main';      $self->{document}->append_child ($root_element);
3745        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3746        #redo B;  
3747        return;      $self->{application_cache_selection}->(undef);
3748    
3749        ## NOTE: Reprocess the token.
3750        !!!ack-later;
3751        return; ## Go to the "before head" insertion mode.
3752    
3753        ## ISSUE: There is an issue in the spec
3754    } # B    } # B
3755    
3756      die "$0: _tree_construction_root_element: This should never be reached";
3757  } # _tree_construction_root_element  } # _tree_construction_root_element
3758    
3759  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1732  sub _reset_insertion_mode ($) { Line 3768  sub _reset_insertion_mode ($) {
3768            
3769      ## Step 3      ## Step 3
3770      S3: {      S3: {
3771        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3772        if (defined $self->{inner_html_node}) {          $last = 1;
3773          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
3774              $self->{inner_html_node}->[1] eq 'th') {            !!!cp ('t28');
3775              $node = $self->{inner_html_node};
3776            } else {
3777              die "_reset_insertion_mode: t27";
3778            }
3779          }
3780          
3781          ## Step 4..14
3782          my $new_mode;
3783          if ($node->[1] & FOREIGN_EL) {
3784            !!!cp ('t28.1');
3785            ## NOTE: Strictly spaking, the line below only applies to MathML and
3786            ## SVG elements.  Currently the HTML syntax supports only MathML and
3787            ## SVG elements as foreigners.
3788            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3789          } elsif ($node->[1] & TABLE_CELL_EL) {
3790            if ($last) {
3791              !!!cp ('t28.2');
3792            #            #
3793          } else {          } else {
3794            $node = $self->{inner_html_node};            !!!cp ('t28.3');
3795              $new_mode = IN_CELL_IM;
3796          }          }
3797          } else {
3798            !!!cp ('t28.4');
3799            $new_mode = {
3800                          select => IN_SELECT_IM,
3801                          ## NOTE: |option| and |optgroup| do not set
3802                          ## insertion mode to "in select" by themselves.
3803                          tr => IN_ROW_IM,
3804                          tbody => IN_TABLE_BODY_IM,
3805                          thead => IN_TABLE_BODY_IM,
3806                          tfoot => IN_TABLE_BODY_IM,
3807                          caption => IN_CAPTION_IM,
3808                          colgroup => IN_COLUMN_GROUP_IM,
3809                          table => IN_TABLE_IM,
3810                          head => IN_BODY_IM, # not in head!
3811                          body => IN_BODY_IM,
3812                          frameset => IN_FRAMESET_IM,
3813                         }->{$node->[0]->manakai_local_name};
3814        }        }
       
       ## 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]};  
3815        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3816                
3817        ## Step 14        ## Step 15
3818        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3819          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3820            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3821              $self->{insertion_mode} = BEFORE_HEAD_IM;
3822          } else {          } else {
3823            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3824              !!!cp ('t30');
3825              $self->{insertion_mode} = AFTER_HEAD_IM;
3826          }          }
3827          return;          return;
3828          } else {
3829            !!!cp ('t31');
3830        }        }
3831                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3832        ## Step 16        ## Step 16
3833          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3834          
3835          ## Step 17
3836        $i--;        $i--;
3837        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3838                
3839        ## Step 17        ## Step 18
3840        redo S3;        redo S3;
3841      } # S3      } # S3
3842    
3843      die "$0: _reset_insertion_mode: This line should never be reached";
3844  } # _reset_insertion_mode  } # _reset_insertion_mode
3845    
3846  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3847    my $self = shift;    my $self = shift;
3848    
   my $phase = 'main';  
   
3849    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3850    
3851    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1803  sub _tree_construction_main ($) { Line 3862  sub _tree_construction_main ($) {
3862      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3863      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3864        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3865            !!!cp ('t32');
3866          return;          return;
3867        }        }
3868      }      }
# Line 1817  sub _tree_construction_main ($) { Line 3877  sub _tree_construction_main ($) {
3877    
3878        ## Step 6        ## Step 6
3879        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3880            !!!cp ('t33_1');
3881          #          #
3882        } else {        } else {
3883          my $in_open_elements;          my $in_open_elements;
3884          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3885            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3886                !!!cp ('t33');
3887              $in_open_elements = 1;              $in_open_elements = 1;
3888              last OE;              last OE;
3889            }            }
3890          }          }
3891          if ($in_open_elements) {          if ($in_open_elements) {
3892              !!!cp ('t34');
3893            #            #
3894          } else {          } else {
3895              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3896              !!!cp ('t35');
3897            redo S4;            redo S4;
3898          }          }
3899        }        }
# Line 1851  sub _tree_construction_main ($) { Line 3916  sub _tree_construction_main ($) {
3916    
3917        ## Step 11        ## Step 11
3918        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3919            !!!cp ('t36');
3920          ## Step 7'          ## Step 7'
3921          $i++;          $i++;
3922          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3923                    
3924          redo S7;          redo S7;
3925        }        }
3926    
3927          !!!cp ('t37');
3928      } # S7      } # S7
3929    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3930    
3931    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3932      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3933        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3934            !!!cp ('t38');
3935          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3936          return;          return;
3937        }        }
3938      }      }
3939    
3940        !!!cp ('t39');
3941    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3942    
3943    my $style_start_tag = sub {    my $insert;
3944      my $style_el; !!!create-element ($style_el, 'style');  
3945      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
3946      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
3947       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
3948        ->append_child ($style_el);      ## Step 1
3949      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
3950                      my $el;
3951        !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3952    
3953        ## Step 2
3954        $insert->($el);
3955    
3956        ## Step 3
3957        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3958        delete $self->{escape}; # MUST
3959    
3960        ## Step 4
3961      my $text = '';      my $text = '';
3962        !!!nack ('t40.1');
3963      !!!next-token;      !!!next-token;
3964      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3965          !!!cp ('t40');
3966        $text .= $token->{data};        $text .= $token->{data};
3967        !!!next-token;        !!!next-token;
3968      } # stop if non-character token or tokenizer stops tokenising      }
3969    
3970        ## Step 5
3971      if (length $text) {      if (length $text) {
3972        $style_el->manakai_append_text ($text);        !!!cp ('t41');
3973          my $text = $self->{document}->create_text_node ($text);
3974          $el->append_child ($text);
3975      }      }
3976        
3977      $self->{content_model_flag} = 'PCDATA';      ## Step 6
3978                      $self->{content_model} = PCDATA_CONTENT_MODEL;
3979      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
3980        ## Step 7
3981        if ($token->{type} == END_TAG_TOKEN and
3982            $token->{tag_name} eq $start_tag_name) {
3983          !!!cp ('t42');
3984        ## Ignore the token        ## Ignore the token
3985      } else {      } else {
3986        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
3987        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3988            !!!cp ('t43');
3989            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3990          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3991            !!!cp ('t44');
3992            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3993          } else {
3994            die "$0: $content_model_flag in parse_rcdata";
3995          }
3996      }      }
3997      !!!next-token;      !!!next-token;
3998    }; # $style_start_tag    }; # $parse_rcdata
3999    
4000    my $script_start_tag = sub {    my $script_start_tag = sub () {
4001      my $script_el;      my $script_el;
4002      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
4003      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
4004    
4005      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
4006        delete $self->{escape}; # MUST
4007            
4008      my $text = '';      my $text = '';
4009        !!!nack ('t45.1');
4010      !!!next-token;      !!!next-token;
4011      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
4012          !!!cp ('t45');
4013        $text .= $token->{data};        $text .= $token->{data};
4014        !!!next-token;        !!!next-token;
4015      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
4016      if (length $text) {      if (length $text) {
4017          !!!cp ('t46');
4018        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
4019      }      }
4020                                
4021      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
4022    
4023      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
4024          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
4025          !!!cp ('t47');
4026        ## Ignore the token        ## Ignore the token
4027      } else {      } else {
4028        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
4029          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4030        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4031        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4032      }      }
4033            
4034      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
4035          !!!cp ('t49');
4036        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4037      } else {      } else {
4038          !!!cp ('t50');
4039        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
4040        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
4041          
4042        (($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);  
4043                
4044        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
4045                
# Line 1943  sub _tree_construction_main ($) { Line 4049  sub _tree_construction_main ($) {
4049      !!!next-token;      !!!next-token;
4050    }; # $script_start_tag    }; # $script_start_tag
4051    
4052      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
4053      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
4054      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4055    
4056    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4057      my $tag_name = shift;      my $end_tag_token = shift;
4058        my $tag_name = $end_tag_token->{tag_name};
4059    
4060        ## NOTE: The adoption agency algorithm (AAA).
4061    
4062      FET: {      FET: {
4063        ## Step 1        ## Step 1
4064        my $formatting_element;        my $formatting_element;
4065        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4066        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4067          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4068              !!!cp ('t52');
4069              last AFE;
4070            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4071                         eq $tag_name) {
4072              !!!cp ('t51');
4073            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4074            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4075            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
4076          }          }
4077        } # AFE        } # AFE
4078        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4079          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
4080            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4081          ## Ignore the token          ## Ignore the token
4082          !!!next-token;          !!!next-token;
4083          return;          return;
# Line 1972  sub _tree_construction_main ($) { Line 4089  sub _tree_construction_main ($) {
4089          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4090          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
4091            if ($in_scope) {            if ($in_scope) {
4092                !!!cp ('t54');
4093              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
4094              last INSCOPE;              last INSCOPE;
4095            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4096              !!!parse-error;              !!!cp ('t55');
4097                !!!parse-error (type => 'unmatched end tag',
4098                                text => $token->{tag_name},
4099                                token => $end_tag_token);
4100              ## Ignore the token              ## Ignore the token
4101              !!!next-token;              !!!next-token;
4102              return;              return;
4103            }            }
4104          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
4105                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4106            $in_scope = 0;            $in_scope = 0;
4107          }          }
4108        } # INSCOPE        } # INSCOPE
4109        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4110          !!!parse-error;          !!!cp ('t57');
4111            !!!parse-error (type => 'unmatched end tag',
4112                            text => $token->{tag_name},
4113                            token => $end_tag_token);
4114          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4115          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4116          return;          return;
4117        }        }
4118        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4119          !!!parse-error;          !!!cp ('t58');
4120            !!!parse-error (type => 'not closed',
4121                            text => $self->{open_elements}->[-1]->[0]
4122                                ->manakai_local_name,
4123                            token => $end_tag_token);
4124        }        }
4125                
4126        ## Step 2        ## Step 2
# Line 2002  sub _tree_construction_main ($) { Line 4128  sub _tree_construction_main ($) {
4128        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4129        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4130          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4131          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4132              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4133              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4134               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4135              !!!cp ('t59');
4136            $furthest_block = $node;            $furthest_block = $node;
4137            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
4138          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
4139              !!!cp ('t60');
4140            last OE;            last OE;
4141          }          }
4142        } # OE        } # OE
4143                
4144        ## Step 3        ## Step 3
4145        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
4146            !!!cp ('t61');
4147          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
4148          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
4149          !!!next-token;          !!!next-token;
# Line 2027  sub _tree_construction_main ($) { Line 4156  sub _tree_construction_main ($) {
4156        ## Step 5        ## Step 5
4157        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
4158        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
4159            !!!cp ('t62');
4160          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
4161        }        }
4162                
# Line 2049  sub _tree_construction_main ($) { Line 4179  sub _tree_construction_main ($) {
4179          S7S2: {          S7S2: {
4180            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
4181              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4182                  !!!cp ('t63');
4183                $node_i_in_active = $_;                $node_i_in_active = $_;
4184                last S7S2;                last S7S2;
4185              }              }
# Line 2062  sub _tree_construction_main ($) { Line 4193  sub _tree_construction_main ($) {
4193                    
4194          ## Step 4          ## Step 4
4195          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
4196              !!!cp ('t64');
4197            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
4198          }          }
4199                    
4200          ## Step 5          ## Step 5
4201          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
4202              !!!cp ('t65');
4203            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
4204            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
4205            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2084  sub _tree_construction_main ($) { Line 4217  sub _tree_construction_main ($) {
4217        } # S7          } # S7  
4218                
4219        ## Step 8        ## Step 8
4220        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4221            my $foster_parent_element;
4222            my $next_sibling;
4223            OE: for (reverse 0..$#{$self->{open_elements}}) {
4224              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4225                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4226                                 if (defined $parent and $parent->node_type == 1) {
4227                                   !!!cp ('t65.1');
4228                                   $foster_parent_element = $parent;
4229                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4230                                 } else {
4231                                   !!!cp ('t65.2');
4232                                   $foster_parent_element
4233                                     = $self->{open_elements}->[$_ - 1]->[0];
4234                                 }
4235                                 last OE;
4236                               }
4237                             } # OE
4238                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4239                               unless defined $foster_parent_element;
4240            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4241            $open_tables->[-1]->[1] = 1; # tainted
4242          } else {
4243            !!!cp ('t65.3');
4244            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4245          }
4246                
4247        ## Step 9        ## Step 9
4248        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2101  sub _tree_construction_main ($) { Line 4259  sub _tree_construction_main ($) {
4259        my $i;        my $i;
4260        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4261          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4262              !!!cp ('t66');
4263            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
4264            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
4265          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4266              !!!cp ('t67');
4267            $i = $_;            $i = $_;
4268          }          }
4269        } # AFE        } # AFE
# Line 2113  sub _tree_construction_main ($) { Line 4273  sub _tree_construction_main ($) {
4273        undef $i;        undef $i;
4274        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4275          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4276              !!!cp ('t68');
4277            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4278            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4279          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4280              !!!cp ('t69');
4281            $i = $_;            $i = $_;
4282          }          }
4283        } # OE        } # OE
# Line 2126  sub _tree_construction_main ($) { Line 4288  sub _tree_construction_main ($) {
4288      } # FET      } # FET
4289    }; # $formatting_end_tag    }; # $formatting_end_tag
4290    
4291    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4292      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4293    }; # $insert_to_current    }; # $insert_to_current
4294    
4295    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4296                         my $child = shift;      my $child = shift;
4297                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4298                              table => 1, tbody => 1, tfoot => 1,        # MUST
4299                              thead => 1, tr => 1,        my $foster_parent_element;
4300                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4301                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4302                           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') {  
4303                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4304                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4305                                   !!!cp ('t70');
4306                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4307                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4308                               } else {                               } else {
4309                                   !!!cp ('t71');
4310                                 $foster_parent_element                                 $foster_parent_element
4311                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4312                               }                               }
# Line 2156  sub _tree_construction_main ($) { Line 4317  sub _tree_construction_main ($) {
4317                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4318                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4319                             ($child, $next_sibling);                             ($child, $next_sibling);
4320                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4321                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4322                         }        !!!cp ('t72');
4323          $self->{open_elements}->[-1]->[0]->append_child ($child);
4324        }
4325    }; # $insert_to_foster    }; # $insert_to_foster
4326    
4327    my $in_body = sub {    B: while (1) {
4328      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
4329      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
4330        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4331          $script_start_tag->();        ## Ignore the token
4332          return;        ## Stay in the phase
4333        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
4334          $style_start_tag->();        next B;
4335          return;      } elsif ($token->{type} == START_TAG_TOKEN and
4336        } elsif ({               $token->{tag_name} eq 'html') {
4337                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4338                 }->{$token->{tag_name}}) {          !!!cp ('t79');
4339          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html', text => 'html', token => $token);
4340          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
4341          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4342          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
4343          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html', text => 'html', token => $token);
4344            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4345          } else {        } else {
4346            $insert->($el);          !!!cp ('t81');
4347          }        }
4348            
4349          !!!next-token;        !!!cp ('t82');
4350          return;        !!!parse-error (type => 'not first start tag', token => $token);
4351        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
4352          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
4353          ## NOTE: There is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4354          my $title_el;            !!!cp ('t84');
4355          !!!create-element ($title_el, 'title', $token->{attributes});            $top_el->set_attribute_ns
4356          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              (undef, [undef, $attr_name],
4357            ->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;  
4358          }          }
4359                    }
4360          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!nack ('t84.1');
4361                    !!!next-token;
4362          next B;
4363        } elsif ($token->{type} == COMMENT_TOKEN) {
4364          my $comment = $self->{document}->create_comment ($token->{data});
4365          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4366            !!!cp ('t85');
4367            $self->{document}->append_child ($comment);
4368          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4369            !!!cp ('t86');
4370            $self->{open_elements}->[0]->[0]->append_child ($comment);
4371          } else {
4372            !!!cp ('t87');
4373            $self->{open_elements}->[-1]->[0]->append_child ($comment);
4374          }
4375          !!!next-token;
4376          next B;
4377        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4378          if ($token->{type} == CHARACTER_TOKEN) {
4379            !!!cp ('t87.1');
4380            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4381          !!!next-token;          !!!next-token;
4382          return;          next B;
4383        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4384          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4385            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4386            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4387              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4388                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4389              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4390              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4391              $formatting_end_tag->($token->{tag_name});            #
4392                        } elsif ({
4393              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4394                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4395                  splice @$active_formatting_elements, $_, 1;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4396                  last AFE2;                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4397                }                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4398              } # AFE2                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4399              OE: for (reverse 0..$#{$self->{open_elements}}) {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4400                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4401                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4402                  last OE;            !!!cp ('t87.2');
4403                }            !!!parse-error (type => 'not closed',
4404              } # OE                            text => $self->{open_elements}->[-1]->[0]
4405              last AFE;                                ->manakai_local_name,
4406            } elsif ($node->[0] eq '#marker') {                            token => $token);
4407              last AFE;  
4408              pop @{$self->{open_elements}}
4409                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4410    
4411              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4412              ## Reprocess.
4413              next B;
4414            } else {
4415              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4416              my $tag_name = $token->{tag_name};
4417              if ($nsuri eq $SVG_NS) {
4418                $tag_name = {
4419                   altglyph => 'altGlyph',
4420                   altglyphdef => 'altGlyphDef',
4421                   altglyphitem => 'altGlyphItem',
4422                   animatecolor => 'animateColor',
4423                   animatemotion => 'animateMotion',
4424                   animatetransform => 'animateTransform',
4425                   clippath => 'clipPath',
4426                   feblend => 'feBlend',
4427                   fecolormatrix => 'feColorMatrix',
4428                   fecomponenttransfer => 'feComponentTransfer',
4429                   fecomposite => 'feComposite',
4430                   feconvolvematrix => 'feConvolveMatrix',
4431                   fediffuselighting => 'feDiffuseLighting',
4432                   fedisplacementmap => 'feDisplacementMap',
4433                   fedistantlight => 'feDistantLight',
4434                   feflood => 'feFlood',
4435                   fefunca => 'feFuncA',
4436                   fefuncb => 'feFuncB',
4437                   fefuncg => 'feFuncG',
4438                   fefuncr => 'feFuncR',
4439                   fegaussianblur => 'feGaussianBlur',
4440                   feimage => 'feImage',
4441                   femerge => 'feMerge',
4442                   femergenode => 'feMergeNode',
4443                   femorphology => 'feMorphology',
4444                   feoffset => 'feOffset',
4445                   fepointlight => 'fePointLight',
4446                   fespecularlighting => 'feSpecularLighting',
4447                   fespotlight => 'feSpotLight',
4448                   fetile => 'feTile',
4449                   feturbulence => 'feTurbulence',
4450                   foreignobject => 'foreignObject',
4451                   glyphref => 'glyphRef',
4452                   lineargradient => 'linearGradient',
4453                   radialgradient => 'radialGradient',
4454                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4455                   textpath => 'textPath',  
4456                }->{$tag_name} || $tag_name;
4457            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4458    
4459          !!!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];  
4460    
4461          !!!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', ''];  
4462    
4463          !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4464          return;  
4465        } elsif ($token->{tag_name} eq 'marquee' or            if ($self->{self_closing}) {
4466                 $token->{tag_name} eq 'object') {              pop @{$self->{open_elements}};
4467          $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});  
4468            } else {            } else {
4469              !!!parse-error (type => 'in RCDATA:#'.$token->{type});              !!!cp ('t87.4');
4470            }            }
4471            ## ISSUE: And ignore?  
4472              !!!next-token;
4473              next B;
4474          }          }
4475          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4476          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4477        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
4478          $reconstruct_active_formatting_elements->($insert_to_current);          #
4479                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4480          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t87.6');
4481                    !!!parse-error (type => 'not closed',
4482          $self->{insertion_mode} = 'in select';                          text => $self->{open_elements}->[-1]->[0]
4483          !!!next-token;                              ->manakai_local_name,
4484          return;                          token => $token);
4485        } elsif ({  
4486                  caption => 1, col => 1, colgroup => 1, frame => 1,          pop @{$self->{open_elements}}
4487                  frameset => 1, head => 1, option => 1, optgroup => 1,              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4488                  tbody => 1, td => 1, tfoot => 1, th => 1,  
4489                  thead => 1, tr => 1,          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4490                 }->{$token->{tag_name}}) {          ## Reprocess.
4491          !!!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.  
4492        } else {        } else {
4493          $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;  
4494        }        }
4495      } elsif ($token->{type} eq 'end tag') {      }
4496        if ($token->{tag_name} eq 'body') {  
4497          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      if ($self->{insertion_mode} & HEAD_IMS) {
4498            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
4499            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4500              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4501            }              !!!cp ('t88.2');
4502            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4503            !!!next-token;              #
4504            return;            } else {
4505          } else {              !!!cp ('t88.1');
4506            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              ## Ignore the token.
4507            ## Ignore the token              #
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 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;  
4508            }            }
4509          } # INSCOPE            unless (length $token->{data}) {
4510                        !!!cp ('t88');
4511          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
4512            !!!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;  
4513            }            }
4514          } # INSCOPE  ## TODO: set $token->{column} appropriately
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4515          }          }
           
         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];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
4516    
4517              !!!next-token;          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4518              last S2;            !!!cp ('t89');
4519            } else {            ## As if <head>
4520              ## Step 3            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4521              if (not $formatting_category->{$node->[1]} and            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4522                  #not $phrasing_category->{$node->[1]} and            push @{$self->{open_elements}},
4523                  ($special_category->{$node->[1]} or                [$self->{head_element}, $el_category->{head}];
4524                   $scoping_category->{$node->[1]})) {  
4525                !!!parse-error (type => 'not closed:'.$node->[1]);            ## Reprocess in the "in head" insertion mode...
4526                ## Ignore the token            pop @{$self->{open_elements}};
4527                !!!next-token;  
4528                last S2;            ## Reprocess in the "after head" insertion mode...
4529              }          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4530            }            !!!cp ('t90');
4531                        ## As if </noscript>
4532            ## Step 4            pop @{$self->{open_elements}};
4533            $node_i--;            !!!parse-error (type => 'in noscript:#text', token => $token);
           $node = $self->{open_elements}->[$node_i];  
4534                        
4535            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4536            redo S2;            ## As if </head>
4537          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
4538    
4539    B: {            ## Reprocess in the "after head" insertion mode...
4540      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4541        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
4542          !!!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]);  
         }  
4543    
4544          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4545          last B;          } else {
4546              !!!cp ('t92');
4547            }
4548    
4549          ## ISSUE: There is an issue in the spec.          ## "after head" insertion mode
4550        } else {          ## As if <body>
4551          if ($self->{insertion_mode} eq 'before head') {          !!!insert-element ('body',, $token);
4552            if ($token->{type} eq 'character') {          $self->{insertion_mode} = IN_BODY_IM;
4553              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## reprocess
4554                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          next B;
4555                unless (length $token->{data}) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4556                  !!!next-token;          if ($token->{tag_name} eq 'head') {
4557                  redo B;            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4558                }              !!!cp ('t93');
4559              }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4560              ## As if <head>              $self->{open_elements}->[-1]->[0]->append_child
4561              !!!create-element ($self->{head_element}, 'head');                  ($self->{head_element});
4562              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              push @{$self->{open_elements}},
4563              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  [$self->{head_element}, $el_category->{head}];
4564              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = IN_HEAD_IM;
4565              ## 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);  
4566              !!!next-token;              !!!next-token;
4567              redo B;              next B;
4568            } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4569              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              !!!cp ('t93.2');
4570              !!!create-element ($self->{head_element}, 'head', $attr);              !!!parse-error (type => 'after head', text => 'head',
4571              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token);
4572              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4573              $self->{insertion_mode} = 'in head';              !!!nack ('t93.3');
4574              if ($token->{tag_name} eq 'head') {              !!!next-token;
4575                !!!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;  
             }  
4576            } else {            } else {
4577              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4578            }              !!!parse-error (type => 'in head:head',
4579          } elsif ($self->{insertion_mode} eq 'in head') {                              token => $token); # or in head noscript
4580            if ($token->{type} eq 'character') {              ## Ignore the token
4581              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);  
4582              !!!next-token;              !!!next-token;
4583              redo B;              next B;
4584            } elsif ($token->{type} eq 'start tag') {            }
4585              if ($token->{tag_name} eq 'title') {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4586                ## NOTE: There is an "as if in head" code clone            !!!cp ('t96');
4587                my $title_el;            ## As if <head>
4588                !!!create-element ($title_el, 'title', $token->{attributes});            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4589                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4590                  ->append_child ($title_el);            push @{$self->{open_elements}},
4591                $self->{content_model_flag} = 'RCDATA';                [$self->{head_element}, $el_category->{head}];
4592    
4593                my $text = '';            $self->{insertion_mode} = IN_HEAD_IM;
4594                !!!next-token;            ## Reprocess in the "in head" insertion mode...
4595                while ($token->{type} eq 'character') {          } else {
4596                  $text .= $token->{data};            !!!cp ('t97');
4597                  !!!next-token;          }
4598                }  
4599                if (length $text) {              if ($token->{tag_name} eq 'base') {
4600                  $title_el->manakai_append_text ($text);                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4601                }                  !!!cp ('t98');
4602                                  ## As if </noscript>
4603                $self->{content_model_flag} = 'PCDATA';                  pop @{$self->{open_elements}};
4604                    !!!parse-error (type => 'in noscript', text => 'base',
4605                                    token => $token);
4606                                
4607                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
4608                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
4609                } else {                } else {
4610                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
4611                }                }
               !!!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);  
4612    
4613                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
4614                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4615              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
4616                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head',
4617                ## Ignore the token                                  text => $token->{tag_name}, token => $token);
4618                !!!next-token;                  push @{$self->{open_elements}},
4619                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}};  
4620                } else {                } else {
4621                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
4622                }                }
4623                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4624                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4625                  pop @{$self->{open_elements}} # <head>
4626                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4627                  !!!nack ('t101.1');
4628                !!!next-token;                !!!next-token;
4629                redo B;                next B;
4630              } elsif ($token->{tag_name} eq 'html') {              } elsif ($token->{tag_name} eq 'link') {
4631                #                ## NOTE: There is a "as if in head" code clone.
4632              } else {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4633                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t102');
4634                ## Ignore the token                  !!!parse-error (type => 'after head',
4635                !!!next-token;                                  text => $token->{tag_name}, token => $token);
4636                redo B;                  push @{$self->{open_elements}},
4637              }                      [$self->{head_element}, $el_category->{head}];
4638            } else {                } else {
4639              #                  !!!cp ('t103');
           }  
   
           if ($self->{open_elements}->[-1]->[1] eq 'head') {  
             ## As if </head>  
             pop @{$self->{open_elements}};  
           }  
           $self->{insertion_mode} = 'after head';  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
4640                }                }
4641              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4642                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4643              #                pop @{$self->{open_elements}} # <head>
4644            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4645              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';  
4646                !!!next-token;                !!!next-token;
4647                redo B;                next B;
4648              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
4649                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
4650                        script => 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4651                       }->{$token->{tag_name}}) {                  !!!cp ('t104');
4652                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4653                $self->{insertion_mode} = 'in head';                                  text => $token->{tag_name}, token => $token);
4654                ## reprocess                  push @{$self->{open_elements}},
4655                redo B;                      [$self->{head_element}, $el_category->{head}];
4656              } else {                } else {
4657                #                  !!!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;  
4658                }                }
4659              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4660                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
             !!!parse-error (type => 'in table:#character');  
4661    
4662              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
4663              ## ISSUE: Spec says that "whenever a node would be inserted                  if ($token->{attributes}->{charset}) {
4664              ## into the current node" while characters might not be                    !!!cp ('t106');
4665              ## result in a new Text node.                    ## NOTE: Whether the encoding is supported or not is handled
4666              $reconstruct_active_formatting_elements->($insert_to_foster);                    ## in the {change_encoding} callback.
4667                                  $self->{change_encoding}
4668              if ({                        ->($self, $token->{attributes}->{charset}->{value},
4669                   table => 1, tbody => 1, tfoot => 1,                           $token);
4670                   thead => 1, tr => 1,                    
4671                  }->{$self->{open_elements}->[-1]->[1]}) {                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4672                # MUST                        ->set_user_data (manakai_has_reference =>
4673                my $foster_parent_element;                                             $token->{attributes}->{charset}
4674                my $next_sibling;                                                 ->{has_reference});
4675                my $prev_sibling;                  } elsif ($token->{attributes}->{content}) {
4676                OE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($token->{attributes}->{content}->{value}
4677                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4678                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            [\x09-\x0D\x20]*=
4679                    if (defined $parent and $parent->node_type == 1) {                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4680                      $foster_parent_element = $parent;                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4681                      $next_sibling = $self->{open_elements}->[$_]->[0];                      !!!cp ('t107');
4682                      $prev_sibling = $next_sibling->previous_sibling;                      ## NOTE: Whether the encoding is supported or not is handled
4683                        ## in the {change_encoding} callback.
4684                        $self->{change_encoding}
4685                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4686                               $token);
4687                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4688                            ->set_user_data (manakai_has_reference =>
4689                                                 $token->{attributes}->{content}
4690                                                       ->{has_reference});
4691                    } else {                    } else {
4692                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
4693                    }                    }
                   last OE;  
4694                  }                  }
               } # 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});  
4695                } else {                } else {
4696                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
4697                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
4698                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4699                }                        ->set_user_data (manakai_has_reference =>
4700              } else {                                             $token->{attributes}->{charset}
4701                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
4702              }                  }
4703                                if ($token->{attributes}->{content}) {
4704              !!!next-token;                    !!!cp ('t110');
4705              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4706            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
4707              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
4708              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
4709              !!!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}};  
4710                }                }
4711    
4712                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
4713                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4714                  !!!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}};  
4715                !!!next-token;                !!!next-token;
4716                redo B;                next B;
4717              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
4718                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4719                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
4720                       }->{$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]);  
4721                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4722                    !!!parse-error (type => 'in noscript', text => 'title',
4723                                    token => $token);
4724                  
4725                    $self->{insertion_mode} = IN_HEAD_IM;
4726                    ## Reprocess in the "in head" insertion mode...
4727                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4728                    !!!cp ('t112');
4729                    !!!parse-error (type => 'after head',
4730                                    text => $token->{tag_name}, token => $token);
4731                    push @{$self->{open_elements}},
4732                        [$self->{head_element}, $el_category->{head}];
4733                  } else {
4734                    !!!cp ('t113');
4735                }                }
4736    
4737                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
4738                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
4739                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
4740                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4741                redo B;                pop @{$self->{open_elements}} # <head>
4742              } elsif ($token->{tag_name} eq 'table') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4743                ## NOTE: There are code clones for this "table in table"                next B;
4744                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } elsif ($token->{tag_name} eq 'style' or
4745                         $token->{tag_name} eq 'noframes') {
4746                ## As if </table>                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4747                ## have a table element in table scope                ## insertion mode IN_HEAD_IM)
4748                my $i;                ## NOTE: There is a "as if in head" code clone.
4749                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4750                  my $node = $self->{open_elements}->[$_];                  !!!cp ('t114');
4751                  if ($node->[1] eq 'table') {                  !!!parse-error (type => 'after head',
4752                    $i = $_;                                  text => $token->{tag_name}, token => $token);
4753                    last INSCOPE;                  push @{$self->{open_elements}},
4754                  } elsif ({                      [$self->{head_element}, $el_category->{head}];
4755                            table => 1, html => 1,                } else {
4756                           }->{$node->[1]}) {                  !!!cp ('t115');
4757                    last INSCOPE;                }
4758                  }                $parse_rcdata->(CDATA_CONTENT_MODEL);
4759                } # INSCOPE                pop @{$self->{open_elements}} # <head>
4760                unless (defined $i) {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4761                  !!!parse-error (type => 'unmatched end tag:table');                next B;
4762                  ## Ignore tokens </table><table>              } elsif ($token->{tag_name} eq 'noscript') {
4763                  if ($self->{insertion_mode} == IN_HEAD_IM) {
4764                    !!!cp ('t116');
4765                    ## NOTE: and scripting is disalbed
4766                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4767                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4768                    !!!nack ('t116.1');
4769                    !!!next-token;
4770                    next B;
4771                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4772                    !!!cp ('t117');
4773                    !!!parse-error (type => 'in noscript', text => 'noscript',
4774                                    token => $token);
4775                    ## Ignore the token
4776                    !!!nack ('t117.1');
4777                  !!!next-token;                  !!!next-token;
4778                  redo B;                  next B;
4779                  } else {
4780                    !!!cp ('t118');
4781                    #
4782                }                }
4783                } elsif ($token->{tag_name} eq 'script') {
4784                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4785                    !!!cp ('t119');
4786                    ## As if </noscript>
4787                    pop @{$self->{open_elements}};
4788                    !!!parse-error (type => 'in noscript', text => 'script',
4789                                    token => $token);
4790                                
4791                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
4792                if ({                  ## Reprocess in the "in head" insertion mode...
4793                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4794                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
4795                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head',
4796                  !!!back-token; # <table>                                  text => $token->{tag_name}, token => $token);
4797                  $token = {type => 'end tag', tag_name => 'table'};                  push @{$self->{open_elements}},
4798                  !!!back-token;                      [$self->{head_element}, $el_category->{head}];
4799                  $token = {type => 'end tag',                } else {
4800                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!cp ('t121');
                 redo B;  
4801                }                }
4802    
4803                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
4804                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
4805                  pop @{$self->{open_elements}} # <head>
4806                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4807                  next B;
4808                } elsif ($token->{tag_name} eq 'body' or
4809                         $token->{tag_name} eq 'frameset') {
4810                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4811                    !!!cp ('t122');
4812                    ## As if </noscript>
4813                    pop @{$self->{open_elements}};
4814                    !!!parse-error (type => 'in noscript',
4815                                    text => $token->{tag_name}, token => $token);
4816                    
4817                    ## Reprocess in the "in head" insertion mode...
4818                    ## As if </head>
4819                    pop @{$self->{open_elements}};
4820                    
4821                    ## Reprocess in the "after head" insertion mode...
4822                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4823                    !!!cp ('t124');
4824                    pop @{$self->{open_elements}};
4825                    
4826                    ## Reprocess in the "after head" insertion mode...
4827                  } else {
4828                    !!!cp ('t125');
4829                }                }
4830    
4831                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
4832                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4833                  if ($token->{tag_name} eq 'body') {
4834                    !!!cp ('t126');
4835                    $self->{insertion_mode} = IN_BODY_IM;
4836                  } elsif ($token->{tag_name} eq 'frameset') {
4837                    !!!cp ('t127');
4838                    $self->{insertion_mode} = IN_FRAMESET_IM;
4839                  } else {
4840                    die "$0: tag name: $self->{tag_name}";
4841                  }
4842                  !!!nack ('t127.1');
4843                  !!!next-token;
4844                  next B;
4845                } else {
4846                  !!!cp ('t128');
4847                  #
4848                }
4849    
4850                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4851                  !!!cp ('t129');
4852                  ## As if </noscript>
4853                  pop @{$self->{open_elements}};
4854                  !!!parse-error (type => 'in noscript:/',
4855                                  text => $token->{tag_name}, token => $token);
4856                  
4857                  ## Reprocess in the "in head" insertion mode...
4858                  ## As if </head>
4859                  pop @{$self->{open_elements}};
4860    
4861                ## reprocess                ## Reprocess in the "after head" insertion mode...
4862                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4863                  !!!cp ('t130');
4864                  ## As if </head>
4865                  pop @{$self->{open_elements}};
4866    
4867                  ## Reprocess in the "after head" insertion mode...
4868              } else {              } else {
4869                #                !!!cp ('t131');
4870              }              }
4871            } elsif ($token->{type} eq 'end tag') {  
4872              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
4873                ## have a table element in table scope              ## As if <body>
4874                my $i;              !!!insert-element ('body',, $token);
4875                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
4876                  my $node = $self->{open_elements}->[$_];              ## reprocess
4877                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
4878                    $i = $_;              next B;
4879                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
4880                  } elsif ({              if ($token->{tag_name} eq 'head') {
4881                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4882                           }->{$node->[1]}) {                  !!!cp ('t132');
4883                    last INSCOPE;                  ## As if <head>
4884                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4885                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4886                unless (defined $i) {                  push @{$self->{open_elements}},
4887                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
4888    
4889                    ## Reprocess in the "in head" insertion mode...
4890                    pop @{$self->{open_elements}};
4891                    $self->{insertion_mode} = AFTER_HEAD_IM;
4892                    !!!next-token;
4893                    next B;
4894                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4895                    !!!cp ('t133');
4896                    ## As if </noscript>
4897                    pop @{$self->{open_elements}};
4898                    !!!parse-error (type => 'in noscript:/',
4899                                    text => 'head', token => $token);
4900                    
4901                    ## Reprocess in the "in head" insertion mode...
4902                    pop @{$self->{open_elements}};
4903                    $self->{insertion_mode} = AFTER_HEAD_IM;
4904                    !!!next-token;
4905                    next B;
4906                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4907                    !!!cp ('t134');
4908                    pop @{$self->{open_elements}};
4909                    $self->{insertion_mode} = AFTER_HEAD_IM;
4910                    !!!next-token;
4911                    next B;
4912                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4913                    !!!cp ('t134.1');
4914                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4915                                    token => $token);
4916                  ## Ignore the token                  ## Ignore the token
4917                  !!!next-token;                  !!!next-token;
4918                  redo B;                  next B;
4919                  } else {
4920                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4921                }                }
4922                              } elsif ($token->{tag_name} eq 'noscript') {
4923                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4924                if ({                  !!!cp ('t136');
4925                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}};
4926                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
4927                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!next-token;
4928                  !!!back-token;                  next B;
4929                  $token = {type => 'end tag',                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4930                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4931                  redo B;                  !!!cp ('t137');
4932                    !!!parse-error (type => 'unmatched end tag',
4933                                    text => 'noscript', token => $token);
4934                    ## Ignore the token ## ISSUE: An issue in the spec.
4935                    !!!next-token;
4936                    next B;
4937                  } else {
4938                    !!!cp ('t138');
4939                    #
4940                }                }
4941                } elsif ({
4942                if ($self->{open_elements}->[-1]->[1] ne 'table') {                        body => 1, html => 1,
4943                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                       }->{$token->{tag_name}}) {
4944                  if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4945                      $self->{insertion_mode} == IN_HEAD_IM or
4946                      $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4947                    !!!cp ('t140');
4948                    !!!parse-error (type => 'unmatched end tag',
4949                                    text => $token->{tag_name}, token => $token);
4950                    ## Ignore the token
4951                    !!!next-token;
4952                    next B;
4953                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4954                    !!!cp ('t140.1');
4955                    !!!parse-error (type => 'unmatched end tag',
4956                                    text => $token->{tag_name}, token => $token);
4957                    ## Ignore the token
4958                    !!!next-token;
4959                    next B;
4960                  } else {
4961                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4962                }                }
4963                } elsif ($token->{tag_name} eq 'p') {
4964                  !!!cp ('t142');
4965                  !!!parse-error (type => 'unmatched end tag',
4966                                  text => $token->{tag_name}, token => $token);
4967                  ## Ignore the token
4968                  !!!next-token;
4969                  next B;
4970                } elsif ($token->{tag_name} eq 'br') {
4971                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4972                    !!!cp ('t142.2');
4973                    ## (before head) as if <head>, (in head) as if </head>
4974                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4975                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4976                    $self->{insertion_mode} = AFTER_HEAD_IM;
4977      
4978                    ## Reprocess in the "after head" insertion mode...
4979                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4980                    !!!cp ('t143.2');
4981                    ## As if </head>
4982                    pop @{$self->{open_elements}};
4983                    $self->{insertion_mode} = AFTER_HEAD_IM;
4984      
4985                    ## Reprocess in the "after head" insertion mode...
4986                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4987                    !!!cp ('t143.3');
4988                    ## ISSUE: Two parse errors for <head><noscript></br>
4989                    !!!parse-error (type => 'unmatched end tag',
4990                                    text => 'br', token => $token);
4991                    ## As if </noscript>
4992                    pop @{$self->{open_elements}};
4993                    $self->{insertion_mode} = IN_HEAD_IM;
4994    
4995                splice @{$self->{open_elements}}, $i;                  ## Reprocess in the "in head" insertion mode...
4996                    ## As if </head>
4997                    pop @{$self->{open_elements}};
4998                    $self->{insertion_mode} = AFTER_HEAD_IM;
4999    
5000                $self->_reset_insertion_mode;                  ## Reprocess in the "after head" insertion mode...
5001                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5002                    !!!cp ('t143.4');
5003                    #
5004                  } else {
5005                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
5006                  }
5007    
5008                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5009                  !!!parse-error (type => 'unmatched end tag',
5010                                  text => 'br', token => $token);
5011                  ## Ignore the token
5012                !!!next-token;                !!!next-token;
5013                redo B;                next B;
5014              } elsif ({              } else {
5015                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t145');
5016                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                !!!parse-error (type => 'unmatched end tag',
5017                        thead => 1, tr => 1,                                text => $token->{tag_name}, token => $token);
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5018                ## Ignore the token                ## Ignore the token
5019                !!!next-token;                !!!next-token;
5020                redo B;                next B;
5021                }
5022    
5023                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5024                  !!!cp ('t146');
5025                  ## As if </noscript>
5026                  pop @{$self->{open_elements}};
5027                  !!!parse-error (type => 'in noscript:/',
5028                                  text => $token->{tag_name}, token => $token);
5029                  
5030                  ## Reprocess in the "in head" insertion mode...
5031                  ## As if </head>
5032                  pop @{$self->{open_elements}};
5033    
5034                  ## Reprocess in the "after head" insertion mode...
5035                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5036                  !!!cp ('t147');
5037                  ## As if </head>
5038                  pop @{$self->{open_elements}};
5039    
5040                  ## Reprocess in the "after head" insertion mode...
5041                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5042    ## ISSUE: This case cannot be reached?
5043                  !!!cp ('t148');
5044                  !!!parse-error (type => 'unmatched end tag',
5045                                  text => $token->{tag_name}, token => $token);
5046                  ## Ignore the token ## ISSUE: An issue in the spec.
5047                  !!!next-token;
5048                  next B;
5049              } else {              } else {
5050                #                !!!cp ('t149');
5051              }              }
           } else {  
             #  
           }  
5052    
5053            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
5054            $in_body->($insert_to_foster);              ## As if <body>
5055            redo B;              !!!insert-element ('body',, $token);
5056          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
5057            if ($token->{type} eq 'character') {              ## reprocess
5058              ## NOTE: This is a code clone of "character in body".              next B;
5059          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5060            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5061              !!!cp ('t149.1');
5062    
5063              ## NOTE: As if <head>
5064              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5065              $self->{open_elements}->[-1]->[0]->append_child
5066                  ($self->{head_element});
5067              #push @{$self->{open_elements}},
5068              #    [$self->{head_element}, $el_category->{head}];
5069              #$self->{insertion_mode} = IN_HEAD_IM;
5070              ## NOTE: Reprocess.
5071    
5072              ## NOTE: As if </head>
5073              #pop @{$self->{open_elements}};
5074              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5075              ## NOTE: Reprocess.
5076              
5077              #
5078            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5079              !!!cp ('t149.2');
5080    
5081              ## NOTE: As if </head>
5082              pop @{$self->{open_elements}};
5083              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5084              ## NOTE: Reprocess.
5085    
5086              #
5087            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5088              !!!cp ('t149.3');
5089    
5090              !!!parse-error (type => 'in noscript:#eof', token => $token);
5091    
5092              ## As if </noscript>
5093              pop @{$self->{open_elements}};
5094              #$self->{insertion_mode} = IN_HEAD_IM;
5095              ## NOTE: Reprocess.
5096    
5097              ## NOTE: As if </head>
5098              pop @{$self->{open_elements}};
5099              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5100              ## NOTE: Reprocess.
5101    
5102              #
5103            } else {
5104              !!!cp ('t149.4');
5105              #
5106            }
5107    
5108            ## NOTE: As if <body>
5109            !!!insert-element ('body',, $token);
5110            $self->{insertion_mode} = IN_BODY_IM;
5111            ## NOTE: Reprocess.
5112            next B;
5113          } else {
5114            die "$0: $token->{type}: Unknown token type";
5115          }
5116    
5117              ## ISSUE: An issue in the spec.
5118        } elsif ($self->{insertion_mode} & BODY_IMS) {
5119              if ($token->{type} == CHARACTER_TOKEN) {
5120                !!!cp ('t150');
5121                ## NOTE: There is a code clone of "character in body".
5122              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
5123                            
5124              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5125    
5126              !!!next-token;              !!!next-token;
5127              redo B;              next B;
5128            } 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') {  
5129              if ({              if ({
5130                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
5131                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
5132                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5133                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
5134                    ## have an element in table scope
5135                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
5136                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
5137                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
5138                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
5139                  my $node = $self->{open_elements}->[$_];  
5140                  if ($node->[1] eq 'caption') {                      ## Close the cell
5141                    $i = $_;                      !!!back-token; # <x>
5142                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
5143                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
5144                            table => 1, html => 1,                                line => $token->{line},
5145                           }->{$node->[1]}) {                                column => $token->{column}};
5146                    last INSCOPE;                      next B;
5147                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5148                        !!!cp ('t152');
5149                        ## ISSUE: This case can never be reached, maybe.
5150                        last;
5151                      }
5152                  }                  }
5153                } # INSCOPE  
5154                unless (defined $i) {                  !!!cp ('t153');
5155                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
5156                        text => $token->{tag_name}, token => $token);
5157                  ## Ignore the token                  ## Ignore the token
5158                    !!!nack ('t153.1');
5159                  !!!next-token;                  !!!next-token;
5160                  redo B;                  next B;
5161                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5162                                  !!!parse-error (type => 'not closed', text => 'caption',
5163                ## generate implied end tags                                  token => $token);
5164                if ({                  
5165                     dd => 1, dt => 1, li => 1, p => 1,                  ## NOTE: As if </caption>.
5166                     td => 1, th => 1, tr => 1,                  ## have a table element in table scope
5167                    }->{$self->{open_elements}->[-1]->[1]}) {                  my $i;
5168                  !!!back-token; # <?>                  INSCOPE: {
5169                  $token = {type => 'end tag', tag_name => 'caption'};                    for (reverse 0..$#{$self->{open_elements}}) {
5170                  !!!back-token;                      my $node = $self->{open_elements}->[$_];
5171                  $token = {type => 'end tag',                      if ($node->[1] & CAPTION_EL) {
5172                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        !!!cp ('t155');
5173                  redo B;                        $i = $_;
5174                }                        last INSCOPE;
5175                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5176                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        !!!cp ('t156');
5177                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        last;
5178                }                      }
5179                      }
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
5180    
5181                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
5182                      !!!parse-error (type => 'start tag not allowed',
5183                                      text => $token->{tag_name}, token => $token);
5184                      ## Ignore the token
5185                      !!!nack ('t157.1');
5186                      !!!next-token;
5187                      next B;
5188                    } # INSCOPE
5189                    
5190                    ## generate implied end tags
5191                    while ($self->{open_elements}->[-1]->[1]
5192                               & END_TAG_OPTIONAL_EL) {
5193                      !!!cp ('t158');
5194                      pop @{$self->{open_elements}};
5195                    }
5196    
5197                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5198                redo B;                    !!!cp ('t159');
5199                      !!!parse-error (type => 'not closed',
5200                                      text => $self->{open_elements}->[-1]->[0]
5201                                          ->manakai_local_name,
5202                                      token => $token);
5203                    } else {
5204                      !!!cp ('t160');
5205                    }
5206                    
5207                    splice @{$self->{open_elements}}, $i;
5208                    
5209                    $clear_up_to_marker->();
5210                    
5211                    $self->{insertion_mode} = IN_TABLE_IM;
5212                    
5213                    ## reprocess
5214                    !!!ack-later;
5215                    next B;
5216                  } else {
5217                    !!!cp ('t161');
5218                    #
5219                  }
5220              } else {              } else {
5221                  !!!cp ('t162');
5222                #                #
5223              }              }
5224            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5225              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
5226                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
5227                my $i;                  ## have an element in table scope
5228                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
5229                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5230                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5231                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5232                    last INSCOPE;                      !!!cp ('t163');
5233                  } elsif ({                      $i = $_;
5234                            table => 1, html => 1,                      last INSCOPE;
5235                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5236                    last INSCOPE;                      !!!cp ('t164');
5237                        last INSCOPE;
5238                      }
5239                    } # INSCOPE
5240                      unless (defined $i) {
5241                        !!!cp ('t165');
5242                        !!!parse-error (type => 'unmatched end tag',
5243                                        text => $token->{tag_name},
5244                                        token => $token);
5245                        ## Ignore the token
5246                        !!!next-token;
5247                        next B;
5248                      }
5249                    
5250                    ## generate implied end tags
5251                    while ($self->{open_elements}->[-1]->[1]
5252                               & END_TAG_OPTIONAL_EL) {
5253                      !!!cp ('t166');
5254                      pop @{$self->{open_elements}};
5255                  }                  }
5256                } # INSCOPE  
5257                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5258                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
5259                      !!!cp ('t167');
5260                      !!!parse-error (type => 'not closed',
5261                                      text => $self->{open_elements}->[-1]->[0]
5262                                          ->manakai_local_name,
5263                                      token => $token);
5264                    } else {
5265                      !!!cp ('t168');
5266                    }
5267                    
5268                    splice @{$self->{open_elements}}, $i;
5269                    
5270                    $clear_up_to_marker->();
5271                    
5272                    $self->{insertion_mode} = IN_ROW_IM;
5273                    
5274                    !!!next-token;
5275                    next B;
5276                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5277                    !!!cp ('t169');
5278                    !!!parse-error (type => 'unmatched end tag',
5279                                    text => $token->{tag_name}, token => $token);
5280                  ## Ignore the token                  ## Ignore the token
5281                  !!!next-token;                  !!!next-token;
5282                  redo B;                  next B;
5283                }                } else {
5284                                  !!!cp ('t170');
5285                ## 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;  
5286                }                }
5287                } elsif ($token->{tag_name} eq 'caption') {
5288                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
5289                    ## have a table element in table scope
5290                    my $i;
5291                    INSCOPE: {
5292                      for (reverse 0..$#{$self->{open_elements}}) {
5293                        my $node = $self->{open_elements}->[$_];
5294                        if ($node->[1] & CAPTION_EL) {
5295                          !!!cp ('t171');
5296                          $i = $_;
5297                          last INSCOPE;
5298                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5299                          !!!cp ('t172');
5300                          last;
5301                        }
5302                      }
5303    
5304                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
5305                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
5306                                      text => $token->{tag_name}, token => $token);
5307                      ## Ignore the token
5308                      !!!next-token;
5309                      next B;
5310                    } # INSCOPE
5311                    
5312                    ## generate implied end tags
5313                    while ($self->{open_elements}->[-1]->[1]
5314                               & END_TAG_OPTIONAL_EL) {
5315                      !!!cp ('t174');
5316                      pop @{$self->{open_elements}};
5317                    }
5318                    
5319                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5320                      !!!cp ('t175');
5321                      !!!parse-error (type => 'not closed',
5322                                      text => $self->{open_elements}->[-1]->[0]
5323                                          ->manakai_local_name,
5324                                      token => $token);
5325                    } else {
5326                      !!!cp ('t176');
5327                    }
5328                    
5329                    splice @{$self->{open_elements}}, $i;
5330                    
5331                    $clear_up_to_marker->();
5332                    
5333                    $self->{insertion_mode} = IN_TABLE_IM;
5334                    
5335                    !!!next-token;
5336                    next B;
5337                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5338                    !!!cp ('t177');
5339                    !!!parse-error (type => 'unmatched end tag',
5340                                    text => $token->{tag_name}, token => $token);
5341                    ## Ignore the token
5342                    !!!next-token;
5343                    next B;
5344                  } else {
5345                    !!!cp ('t178');
5346                    #
5347                }                }
5348                } elsif ({
5349                          table => 1, tbody => 1, tfoot => 1,
5350                          thead => 1, tr => 1,
5351                         }->{$token->{tag_name}} and
5352                         $self->{insertion_mode} == IN_CELL_IM) {
5353                  ## have an element in table scope
5354                  my $i;
5355                  my $tn;
5356                  INSCOPE: {
5357                    for (reverse 0..$#{$self->{open_elements}}) {
5358                      my $node = $self->{open_elements}->[$_];
5359                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5360                        !!!cp ('t179');
5361                        $i = $_;
5362    
5363                        ## Close the cell
5364                        !!!back-token; # </x>
5365                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
5366                                  line => $token->{line},
5367                                  column => $token->{column}};
5368                        next B;
5369                      } elsif ($node->[1] & TABLE_CELL_EL) {
5370                        !!!cp ('t180');
5371                        $tn = $node->[0]->manakai_local_name;
5372                        ## NOTE: There is exactly one |td| or |th| element
5373                        ## in scope in the stack of open elements by definition.
5374                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5375                        ## ISSUE: Can this be reached?
5376                        !!!cp ('t181');
5377                        last;
5378                      }
5379                    }
5380    
5381                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
5382                    !!!parse-error (type => 'unmatched end tag',
5383                $clear_up_to_marker->();                      text => $token->{tag_name}, token => $token);
5384                    ## Ignore the token
5385                $self->{insertion_mode} = 'in table';                  !!!next-token;
5386                    next B;
5387                !!!next-token;                } # INSCOPE
5388                redo B;              } elsif ($token->{tag_name} eq 'table' and
5389              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5390                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5391                                  token => $token);
5392    
5393                ## As if </caption>                ## As if </caption>
5394                ## have a table element in table scope                ## have a table element in table scope
5395                my $i;                my $i;
5396                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5397                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5398                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5399                      !!!cp ('t184');
5400                    $i = $_;                    $i = $_;
5401                    last INSCOPE;                    last INSCOPE;
5402                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5403                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5404                    last INSCOPE;                    last INSCOPE;
5405                  }                  }
5406                } # INSCOPE                } # INSCOPE
5407                unless (defined $i) {                unless (defined $i) {
5408                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5409                    !!!parse-error (type => 'unmatched end tag',
5410                                    text => 'caption', token => $token);
5411                  ## Ignore the token                  ## Ignore the token
5412                  !!!next-token;                  !!!next-token;
5413                  redo B;                  next B;
5414                }                }
5415                                
5416                ## generate implied end tags                ## generate implied end tags
5417                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5418                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5419                     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;  
5420                }                }
5421    
5422                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5423                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5424                    !!!parse-error (type => 'not closed',
5425                                    text => $self->{open_elements}->[-1]->[0]
5426                                        ->manakai_local_name,
5427                                    token => $token);
5428                  } else {
5429                    !!!cp ('t189');
5430                }                }
5431    
5432                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5433    
5434                $clear_up_to_marker->();                $clear_up_to_marker->();
5435    
5436                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5437    
5438                ## reprocess                ## reprocess
5439                redo B;                next B;
5440              } elsif ({              } elsif ({
5441                        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,  
5442                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5443                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5444                ## Ignore the token                  !!!cp ('t190');
5445                redo B;                  !!!parse-error (type => 'unmatched end tag',
5446              } 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');  
5447                  ## Ignore the token                  ## Ignore the token
5448                  !!!next-token;                  !!!next-token;
5449                  redo B;                  next B;
5450                } else {                } else {
5451                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
5452                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
5453                }                }
5454              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
5455                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
5456                          thead => 1, tr => 1,
5457                         }->{$token->{tag_name}} and
5458                         $self->{insertion_mode} == IN_CAPTION_IM) {
5459                  !!!cp ('t192');
5460                  !!!parse-error (type => 'unmatched end tag',
5461                                  text => $token->{tag_name}, token => $token);
5462                ## Ignore the token                ## Ignore the token
5463                !!!next-token;                !!!next-token;
5464                redo B;                next B;
5465              } else {              } else {
5466                #                !!!cp ('t193');
5467                  #
5468              }              }
5469            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5470              #          for my $entry (@{$self->{open_elements}}) {
5471              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5472                !!!cp ('t75');
5473                !!!parse-error (type => 'in body:#eof', token => $token);
5474                last;
5475            }            }
5476            }
5477    
5478            ## As if </colgroup>          ## Stop parsing.
5479            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
5480              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
5481              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
5482          }
5483    
5484          $insert = $insert_to_current;
5485          #
5486        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5487          if ($token->{type} == CHARACTER_TOKEN) {
5488            if (not $open_tables->[-1]->[1] and # tainted
5489                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5490              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5491                  
5492              unless (length $token->{data}) {
5493                !!!cp ('t194');
5494              !!!next-token;              !!!next-token;
5495              redo B;              next B;
5496            } else {            } else {
5497              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5498            }            }
5499          } 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;  
               }  
             }  
5500    
5501              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5502    
5503              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5504              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5505              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5506              ## result in a new Text node.              ## result in a new Text node.
5507              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5508                
5509              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]}) {  
5510                # MUST                # MUST
5511                my $foster_parent_element;                my $foster_parent_element;
5512                my $next_sibling;                my $next_sibling;
5513                my $prev_sibling;                my $prev_sibling;
5514                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5515                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5516                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5517                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5518                        !!!cp ('t196');
5519                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5520                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5521                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5522                    } else {                    } else {
5523                        !!!cp ('t197');
5524                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5525                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5526                    }                    }
# Line 3674  sub _tree_construction_main ($) { Line 5532  sub _tree_construction_main ($) {
5532                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5533                if (defined $prev_sibling and                if (defined $prev_sibling and
5534                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5535                    !!!cp ('t198');
5536                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5537                } else {                } else {
5538                    !!!cp ('t199');
5539                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5540                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5541                     $next_sibling);                     $next_sibling);
5542                }                }
5543              } else {            $open_tables->[-1]->[1] = 1; # tainted
5544                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5545              !!!cp ('t200');
5546              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5547            }
5548                
5549            !!!next-token;
5550            next B;
5551          } elsif ($token->{type} == START_TAG_TOKEN) {
5552            if ({
5553                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5554                 th => 1, td => 1,
5555                }->{$token->{tag_name}}) {
5556              if ($self->{insertion_mode} == IN_TABLE_IM) {
5557                ## Clear back to table context
5558                while (not ($self->{open_elements}->[-1]->[1]
5559                                & TABLE_SCOPING_EL)) {
5560                  !!!cp ('t201');
5561                  pop @{$self->{open_elements}};
5562              }              }
5563                            
5564              !!!next-token;              !!!insert-element ('tbody',, $token);
5565              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5566            } elsif ($token->{type} eq 'comment') {              ## reprocess in the "in table body" insertion mode...
5567              ## Copied from 'in table'            }
5568              my $comment = $self->{document}->create_comment ($token->{data});            
5569              $self->{open_elements}->[-1]->[0]->append_child ($comment);            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5570              !!!next-token;              unless ($token->{tag_name} eq 'tr') {
5571              redo B;                !!!cp ('t202');
5572            } elsif ($token->{type} eq 'start tag') {                !!!parse-error (type => 'missing start tag:tr', token => $token);
5573              if ({              }
5574                   tr => 1,                  
5575                   th => 1, td => 1,              ## Clear back to table body context
5576                  }->{$token->{tag_name}}) {              while (not ($self->{open_elements}->[-1]->[1]
5577                unless ($token->{tag_name} eq 'tr') {                              & TABLE_ROWS_SCOPING_EL)) {
5578                  !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t203');
5579                  ## ISSUE: Can this case be reached?
5580                  pop @{$self->{open_elements}};
5581                }
5582                    
5583                    $self->{insertion_mode} = IN_ROW_IM;
5584                    if ($token->{tag_name} eq 'tr') {
5585                      !!!cp ('t204');
5586                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5587                      !!!nack ('t204');
5588                      !!!next-token;
5589                      next B;
5590                    } else {
5591                      !!!cp ('t205');
5592                      !!!insert-element ('tr',, $token);
5593                      ## reprocess in the "in row" insertion mode
5594                    }
5595                  } else {
5596                    !!!cp ('t206');
5597                }                }
5598    
5599                ## Clear back to table body context                ## Clear back to table row context
5600                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5601                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5602                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5603                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5604                }                }
5605                                
5606                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5607                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5608                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5609                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5610                } else {                
5611                  !!!insert-element ('tr');                !!!nack ('t207.1');
5612                  ## reprocess                !!!next-token;
5613                }                next B;
               redo B;  
5614              } elsif ({              } elsif ({
5615                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5616                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5617                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5618                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5619                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5620                my $i;                  ## As if </tr>
5621                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5622                  my $node = $self->{open_elements}->[$_];                  my $i;
5623                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5624                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5625                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5626                    $i = $_;                      !!!cp ('t208');
5627                    last INSCOPE;                      $i = $_;
5628                  } elsif ({                      last INSCOPE;
5629                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5630                           }->{$node->[1]}) {                      !!!cp ('t209');
5631                    last INSCOPE;                      last INSCOPE;
5632                      }
5633                    } # INSCOPE
5634                    unless (defined $i) {
5635                      !!!cp ('t210');
5636    ## TODO: This type is wrong.
5637                      !!!parse-error (type => 'unmacthed end tag',
5638                                      text => $token->{tag_name}, token => $token);
5639                      ## Ignore the token
5640                      !!!nack ('t210.1');
5641                      !!!next-token;
5642                      next B;
5643                    }
5644                    
5645                    ## Clear back to table row context
5646                    while (not ($self->{open_elements}->[-1]->[1]
5647                                    & TABLE_ROW_SCOPING_EL)) {
5648                      !!!cp ('t211');
5649                      ## ISSUE: Can this case be reached?
5650                      pop @{$self->{open_elements}};
5651                    }
5652                    
5653                    pop @{$self->{open_elements}}; # tr
5654                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5655                    if ($token->{tag_name} eq 'tr') {
5656                      !!!cp ('t212');
5657                      ## reprocess
5658                      !!!ack-later;
5659                      next B;
5660                    } else {
5661                      !!!cp ('t213');
5662                      ## reprocess in the "in table body" insertion mode...
5663                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5664                }                }
5665    
5666                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5667                while (not {                  ## have an element in table scope
5668                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5669                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5670                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5671                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5672                        !!!cp ('t214');
5673                        $i = $_;
5674                        last INSCOPE;
5675                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5676                        !!!cp ('t215');
5677                        last INSCOPE;
5678                      }
5679                    } # INSCOPE
5680                    unless (defined $i) {
5681                      !!!cp ('t216');
5682    ## TODO: This erorr type is wrong.
5683                      !!!parse-error (type => 'unmatched end tag',
5684                                      text => $token->{tag_name}, token => $token);
5685                      ## Ignore the token
5686                      !!!nack ('t216.1');
5687                      !!!next-token;
5688                      next B;
5689                    }
5690    
5691                    ## Clear back to table body context
5692                    while (not ($self->{open_elements}->[-1]->[1]
5693                                    & TABLE_ROWS_SCOPING_EL)) {
5694                      !!!cp ('t217');
5695                      ## ISSUE: Can this state be reached?
5696                      pop @{$self->{open_elements}};
5697                    }
5698                    
5699                    ## As if <{current node}>
5700                    ## have an element in table scope
5701                    ## true by definition
5702                    
5703                    ## Clear back to table body context
5704                    ## nop by definition
5705                    
5706                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5707                    $self->{insertion_mode} = IN_TABLE_IM;
5708                    ## reprocess in "in table" insertion mode...
5709                  } else {
5710                    !!!cp ('t218');
5711                }                }
5712    
5713                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5714                ## have an element in table scope                  ## Clear back to table context
5715                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5716                                    & TABLE_SCOPING_EL)) {
5717                ## Clear back to table body context                    !!!cp ('t219');
5718                ## nop by definition                    ## ISSUE: Can this state be reached?
5719                      pop @{$self->{open_elements}};
5720                pop @{$self->{open_elements}};                  }
5721                $self->{insertion_mode} = 'in table';                  
5722                ## reprocess                  !!!insert-element ('colgroup',, $token);
5723                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5724                    ## reprocess
5725                    !!!ack-later;
5726                    next B;
5727                  } elsif ({
5728                            caption => 1,
5729                            colgroup => 1,
5730                            tbody => 1, tfoot => 1, thead => 1,
5731                           }->{$token->{tag_name}}) {
5732                    ## Clear back to table context
5733                    while (not ($self->{open_elements}->[-1]->[1]
5734                                    & TABLE_SCOPING_EL)) {
5735                      !!!cp ('t220');
5736                      ## ISSUE: Can this state be reached?
5737                      pop @{$self->{open_elements}};
5738                    }
5739                    
5740                    push @$active_formatting_elements, ['#marker', '']
5741                        if $token->{tag_name} eq 'caption';
5742                    
5743                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5744                    $self->{insertion_mode} = {
5745                                               caption => IN_CAPTION_IM,
5746                                               colgroup => IN_COLUMN_GROUP_IM,
5747                                               tbody => IN_TABLE_BODY_IM,
5748                                               tfoot => IN_TABLE_BODY_IM,
5749                                               thead => IN_TABLE_BODY_IM,
5750                                              }->{$token->{tag_name}};
5751                    !!!next-token;
5752                    !!!nack ('t220.1');
5753                    next B;
5754                  } else {
5755                    die "$0: in table: <>: $token->{tag_name}";
5756                  }
5757              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5758                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5759                !!!parse-error (type => 'not closed:table');                                text => $self->{open_elements}->[-1]->[0]
5760                                      ->manakai_local_name,
5761                                  token => $token);
5762    
5763                ## As if </table>                ## As if </table>
5764                ## have a table element in table scope                ## have a table element in table scope
5765                my $i;                my $i;
5766                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5767                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5768                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5769                      !!!cp ('t221');
5770                    $i = $_;                    $i = $_;
5771                    last INSCOPE;                    last INSCOPE;
5772                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5773                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5774                    last INSCOPE;                    last INSCOPE;
5775                  }                  }
5776                } # INSCOPE                } # INSCOPE
5777                unless (defined $i) {                unless (defined $i) {
5778                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5779    ## TODO: The following is wrong, maybe.
5780                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5781                                    token => $token);
5782                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5783                    !!!nack ('t223.1');
5784                  !!!next-token;                  !!!next-token;
5785                  redo B;                  next B;
5786                }                }
5787                                
5788    ## TODO: Followings are removed from the latest spec.
5789                ## generate implied end tags                ## generate implied end tags
5790                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5791                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5792                     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;  
5793                }                }
5794    
5795                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5796                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5797                    ## NOTE: |<table><tr><table>|
5798                    !!!parse-error (type => 'not closed',
5799                                    text => $self->{open_elements}->[-1]->[0]
5800                                        ->manakai_local_name,
5801                                    token => $token);
5802                  } else {
5803                    !!!cp ('t226');
5804                }                }
5805    
5806                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5807                  pop @{$open_tables};
5808    
5809                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5810    
5811                ## reprocess            ## reprocess
5812                redo B;            !!!ack-later;
5813              } else {            next B;
5814                #          } elsif ($token->{tag_name} eq 'style') {
5815              }            if (not $open_tables->[-1]->[1]) { # tainted
5816            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5817              if ({              ## NOTE: This is a "as if in head" code clone.
5818                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5819                  }->{$token->{tag_name}}) {              next B;
5820                ## have an element in table scope            } else {
5821                my $i;              !!!cp ('t227.7');
5822                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5823                  my $node = $self->{open_elements}->[$_];            }
5824                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5825                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5826                    last INSCOPE;              !!!cp ('t227.6');
5827                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5828                            table => 1, html => 1,              $script_start_tag->();
5829                           }->{$node->[1]}) {              next B;
5830                    last INSCOPE;            } else {
5831                  }              !!!cp ('t227.5');
5832                } # INSCOPE              #
5833                unless (defined $i) {            }
5834                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5835                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5836                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5837                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5838                }                if ($type eq 'hidden') {
5839                    !!!cp ('t227.3');
5840                    !!!parse-error (type => 'in table',
5841                                    text => $token->{tag_name}, token => $token);
5842    
5843                ## 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}};  
               }  
5844    
5845                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;  
               }  
5846    
               ## 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]);  
5847                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
5848    
5849                pop @{$self->{open_elements}};                  !!!next-token;
5850                $self->{insertion_mode} = 'in table';                  !!!ack ('t227.2.1');
5851                ## reprocess                  next B;
5852                redo B;                } else {
5853              } elsif ({                  !!!cp ('t227.2');
5854                        body => 1, caption => 1, col => 1, colgroup => 1,                  #
5855                        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;  
5856              } else {              } else {
5857                  !!!cp ('t227.1');
5858                #                #
5859              }              }
5860            } else {            } else {
5861                !!!cp ('t227.4');
5862              #              #
5863            }            }
5864                      } else {
5865            ## As if in table            !!!cp ('t227');
5866            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5867            $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;  
               }  
             }  
5868    
5869              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table', text => $token->{tag_name},
5870                            token => $token);
5871    
5872              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
5873              ## ISSUE: Spec says that "whenever a node would be inserted          #
5874              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
5875              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
5876              $reconstruct_active_formatting_elements->($insert_to_foster);                  $self->{insertion_mode} == IN_ROW_IM) {
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
5877                ## have an element in table scope                ## have an element in table scope
5878                my $i;                my $i;
5879                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5880                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5881                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5882                      !!!cp ('t228');
5883                    $i = $_;                    $i = $_;
5884                    last INSCOPE;                    last INSCOPE;
5885                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5886                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5887                    last INSCOPE;                    last INSCOPE;
5888                  }                  }
5889                } # INSCOPE                } # INSCOPE
5890                unless (defined $i) {                unless (defined $i) {
5891                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5892                    !!!parse-error (type => 'unmatched end tag',
5893                                    text => $token->{tag_name}, token => $token);
5894                  ## Ignore the token                  ## Ignore the token
5895                    !!!nack ('t230.1');
5896                  !!!next-token;                  !!!next-token;
5897                  redo B;                  next B;
5898                  } else {
5899                    !!!cp ('t232');
5900                }                }
5901    
5902                ## Clear back to table row context                ## Clear back to table row context
5903                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5904                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5905                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5906                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5907                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5908                }                }
5909    
5910                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5911                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5912                ## reprocess                !!!next-token;
5913                redo B;                !!!nack ('t231.1');
5914                  next B;
5915              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5916                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5917                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5918                    ## have an element in table scope
5919                ## As if </table>                  my $i;
5920                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5921                my $i;                    my $node = $self->{open_elements}->[$_];
5922                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5923                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5924                  if ($node->[1] eq 'table') {                      $i = $_;
5925                    $i = $_;                      last INSCOPE;
5926                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5927                  } elsif ({                      !!!cp ('t234');
5928                            table => 1, html => 1,                      last INSCOPE;
5929                           }->{$node->[1]}) {                    }
5930                    last INSCOPE;                  } # INSCOPE
5931                    unless (defined $i) {
5932                      !!!cp ('t235');
5933    ## TODO: The following is wrong.
5934                      !!!parse-error (type => 'unmatched end tag',
5935                                      text => $token->{type}, token => $token);
5936                      ## Ignore the token
5937                      !!!nack ('t236.1');
5938                      !!!next-token;
5939                      next B;
5940                  }                  }
5941                } # INSCOPE                  
5942                unless (defined $i) {                  ## Clear back to table row context
5943                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5944                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5945                  !!!next-token;                    !!!cp ('t236');
5946                  redo B;  ## ISSUE: Can this state be reached?
5947                }                    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;  
5948                  }                  }
5949                } # INSCOPE                  
5950                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5951                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5952                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5953                  !!!next-token;                }
5954                  redo B;  
5955                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5956                    ## have an element in table scope
5957                ## Clear back to table row context                  my $i;
5958                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5959                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5960                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5961                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5962                        $i = $_;
5963                        last INSCOPE;
5964                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5965                        !!!cp ('t238');
5966                        last INSCOPE;
5967                      }
5968                    } # INSCOPE
5969                    unless (defined $i) {
5970                      !!!cp ('t239');
5971                      !!!parse-error (type => 'unmatched end tag',
5972                                      text => $token->{tag_name}, token => $token);
5973                      ## Ignore the token
5974                      !!!nack ('t239.1');
5975                      !!!next-token;
5976                      next B;
5977                    }
5978                    
5979                    ## Clear back to table body context
5980                    while (not ($self->{open_elements}->[-1]->[1]
5981                                    & TABLE_ROWS_SCOPING_EL)) {
5982                      !!!cp ('t240');
5983                      pop @{$self->{open_elements}};
5984                    }
5985                    
5986                    ## As if <{current node}>
5987                    ## have an element in table scope
5988                    ## true by definition
5989                    
5990                    ## Clear back to table body context
5991                    ## nop by definition
5992                    
5993                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5994                    $self->{insertion_mode} = IN_TABLE_IM;
5995                    ## reprocess in the "in table" insertion mode...
5996                }                }
5997    
5998                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
5999                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
6000                !!!next-token;                ## the code for <table> in the "in table" insertion mode
6001                redo B;                ## is synced with it.
6002              } elsif ($token->{tag_name} eq 'table') {  
6003                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
6004                my $i;                my $i;
6005                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6006                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6007                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
6008                      !!!cp ('t241');
6009                    $i = $_;                    $i = $_;
6010                    last INSCOPE;                    last INSCOPE;
6011                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6012                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
6013                    last INSCOPE;                    last INSCOPE;
6014                  }                  }
6015                } # INSCOPE                } # INSCOPE
6016                unless (defined $i) {                unless (defined $i) {
6017                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
6018                    !!!parse-error (type => 'unmatched end tag',
6019                                    text => $token->{tag_name}, token => $token);
6020                  ## Ignore the token                  ## Ignore the token
6021                    !!!nack ('t243.1');
6022                  !!!next-token;                  !!!next-token;
6023                  redo B;                  next B;
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
6024                }                }
6025                    
6026                pop @{$self->{open_elements}}; # tr                splice @{$self->{open_elements}}, $i;
6027                $self->{insertion_mode} = 'in table body';                pop @{$open_tables};
6028                ## reprocess                
6029                redo B;                $self->_reset_insertion_mode;
6030                  
6031                  !!!next-token;
6032                  next B;
6033              } elsif ({              } elsif ({
6034                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6035                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
6036                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
6037                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
6038                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
6039                  my $node = $self->{open_elements}->[$_];                  my $i;
6040                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6041                    $i = $_;                    my $node = $self->{open_elements}->[$_];
6042                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6043                  } elsif ({                      !!!cp ('t247');
6044                            table => 1, html => 1,                      $i = $_;
6045                           }->{$node->[1]}) {                      last INSCOPE;
6046                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6047                        !!!cp ('t248');
6048                        last INSCOPE;
6049                      }
6050                    } # INSCOPE
6051                      unless (defined $i) {
6052                        !!!cp ('t249');
6053                        !!!parse-error (type => 'unmatched end tag',
6054                                        text => $token->{tag_name}, token => $token);
6055                        ## Ignore the token
6056                        !!!nack ('t249.1');
6057                        !!!next-token;
6058                        next B;
6059                      }
6060                    
6061                    ## As if </tr>
6062                    ## have an element in table scope
6063                    my $i;
6064                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6065                      my $node = $self->{open_elements}->[$_];
6066                      if ($node->[1] & TABLE_ROW_EL) {
6067                        !!!cp ('t250');
6068                        $i = $_;
6069                        last INSCOPE;
6070                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
6071                        !!!cp ('t251');
6072                        last INSCOPE;
6073                      }
6074                    } # INSCOPE
6075                      unless (defined $i) {
6076                        !!!cp ('t252');
6077                        !!!parse-error (type => 'unmatched end tag',
6078                                        text => 'tr', token => $token);
6079                        ## Ignore the token
6080                        !!!nack ('t252.1');
6081                        !!!next-token;
6082                        next B;
6083                      }
6084                    
6085                    ## Clear back to table row context
6086                    while (not ($self->{open_elements}->[-1]->[1]
6087                                    & TABLE_ROW_SCOPING_EL)) {
6088                      !!!cp ('t253');
6089    ## ISSUE: Can this case be reached?
6090                      pop @{$self->{open_elements}};
6091                  }                  }
6092                } # INSCOPE                  
6093                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
6094                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
6095                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
6096                }                }
6097    
               ## As if </tr>  
6098                ## have an element in table scope                ## have an element in table scope
6099                my $i;                my $i;
6100                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6101                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6102                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6103                      !!!cp ('t254');
6104                    $i = $_;                    $i = $_;
6105                    last INSCOPE;                    last INSCOPE;
6106                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6107                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
6108                    last INSCOPE;                    last INSCOPE;
6109                  }                  }
6110                } # INSCOPE                } # INSCOPE
6111                unless (defined $i) {                unless (defined $i) {
6112                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
6113                    !!!parse-error (type => 'unmatched end tag',
6114                                    text => $token->{tag_name}, token => $token);
6115                  ## Ignore the token                  ## Ignore the token
6116                    !!!nack ('t256.1');
6117                  !!!next-token;                  !!!next-token;
6118                  redo B;                  next B;
6119                }                }
6120    
6121                ## Clear back to table row context                ## Clear back to table body context
6122                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6123                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
6124                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
6125                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6126                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6127                }                }
6128    
6129                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
6130                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
6131                ## reprocess                !!!nack ('t257.1');
6132                redo B;                !!!next-token;
6133                  next B;
6134              } elsif ({              } elsif ({
6135                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6136                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6137                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6138                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6139                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6140                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
6141                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
6142                !!!next-token;                            text => $token->{tag_name}, token => $token);
6143                redo B;            ## Ignore the token
6144              } else {            !!!nack ('t258.1');
6145                #             !!!next-token;
6146              }            next B;
6147            } else {          } else {
6148              #            !!!cp ('t259');
6149            }            !!!parse-error (type => 'in table:/',
6150                              text => $token->{tag_name}, token => $token);
6151    
6152            ## As if in table            $insert = $insert_to_foster;
6153            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
6154            $in_body->($insert_to_foster);          }
6155            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6156          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6157            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
6158              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
6159              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
6160                          #
6161              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6162              !!!cp ('t259.2');
6163              #
6164            }
6165    
6166              !!!next-token;          ## Stop parsing
6167              redo B;          last B;
6168            } elsif ($token->{type} eq 'comment') {        } else {
6169              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
6170              my $comment = $self->{document}->create_comment ($token->{data});        }
6171              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6172              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
6173              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6174            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6175              if ({                unless (length $token->{data}) {
6176                   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  
6177                  !!!next-token;                  !!!next-token;
6178                  redo B;                  next B;
6179                }                }
6180                }
6181                ## Close the cell              
6182                !!!back-token; # <?>              !!!cp ('t261');
6183                $token = {type => 'end tag', tag_name => $tn};              #
6184                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
6185              } else {              if ($token->{tag_name} eq 'col') {
6186                  !!!cp ('t262');
6187                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6188                  pop @{$self->{open_elements}};
6189                  !!!ack ('t262.1');
6190                  !!!next-token;
6191                  next B;
6192                } else {
6193                  !!!cp ('t263');
6194                #                #
6195              }              }
6196            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
6197              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
6198                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6199                my $i;                  !!!cp ('t264');
6200                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag',
6201                  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});  
6202                  ## Ignore the token                  ## Ignore the token
6203                  !!!next-token;                  !!!next-token;
6204                  redo B;                  next B;
6205                }                } else {
6206                                  !!!cp ('t265');
6207                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
6208                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
6209                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
6210                     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]);  
6211                }                }
6212                } elsif ($token->{tag_name} eq 'col') {
6213                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
6214                  !!!parse-error (type => 'unmatched end tag',
6215                $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});  
6216                ## Ignore the token                ## Ignore the token
6217                !!!next-token;                !!!next-token;
6218                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;  
6219              } else {              } else {
6220                #                !!!cp ('t267');
6221                  #
6222              }              }
6223          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6224            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6225                @{$self->{open_elements}} == 1) { # redundant, maybe
6226              !!!cp ('t270.2');
6227              ## Stop parsing.
6228              last B;
6229            } else {
6230              ## NOTE: As if </colgroup>.
6231              !!!cp ('t270.1');
6232              pop @{$self->{open_elements}}; # colgroup
6233              $self->{insertion_mode} = IN_TABLE_IM;
6234              ## Reprocess.
6235              next B;
6236            }
6237          } else {
6238            die "$0: $token->{type}: Unknown token type";
6239          }
6240    
6241              ## As if </colgroup>
6242              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6243                !!!cp ('t269');
6244    ## TODO: Wrong error type?
6245                !!!parse-error (type => 'unmatched end tag',
6246                                text => 'colgroup', token => $token);
6247                ## Ignore the token
6248                !!!nack ('t269.1');
6249                !!!next-token;
6250                next B;
6251            } else {            } else {
6252              #              !!!cp ('t270');
6253                pop @{$self->{open_elements}}; # colgroup
6254                $self->{insertion_mode} = IN_TABLE_IM;
6255                !!!ack-later;
6256                ## reprocess
6257                next B;
6258              }
6259        } elsif ($self->{insertion_mode} & SELECT_IMS) {
6260          if ($token->{type} == CHARACTER_TOKEN) {
6261            !!!cp ('t271');
6262            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6263            !!!next-token;
6264            next B;
6265          } elsif ($token->{type} == START_TAG_TOKEN) {
6266            if ($token->{tag_name} eq 'option') {
6267              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6268                !!!cp ('t272');
6269                ## As if </option>
6270                pop @{$self->{open_elements}};
6271              } else {
6272                !!!cp ('t273');
6273            }            }
             
           $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}};  
               }  
6274    
6275                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6276                !!!next-token;            !!!nack ('t273.1');
6277                redo B;            !!!next-token;
6278              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6279                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6280                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6281                  pop @{$self->{open_elements}};              !!!cp ('t274');
6282                }              ## As if </option>
6283                pop @{$self->{open_elements}};
6284              } else {
6285                !!!cp ('t275');
6286              }
6287    
6288                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6289                  ## As if </optgroup>              !!!cp ('t276');
6290                  pop @{$self->{open_elements}};              ## As if </optgroup>
6291                }              pop @{$self->{open_elements}};
6292              } else {
6293                !!!cp ('t277');
6294              }
6295    
6296                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6297                !!!next-token;            !!!nack ('t277.1');
6298                redo B;            !!!next-token;
6299              } elsif ($token->{tag_name} eq 'select') {            next B;
6300                !!!parse-error (type => 'not closed:select');          } elsif ({
6301                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6302                ## have an element in table scope                   }->{$token->{tag_name}} or
6303                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6304                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6305                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6306                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6307                    $i = $_;                     tr => 1, td => 1, th => 1,
6308                    last INSCOPE;                    }->{$token->{tag_name}})) {
6309                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6310                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6311                           }->{$node->[1]}) {                            token => $token);
6312                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6313                  }            ## as if there were </select> (otherwise).
6314                } # INSCOPE            ## have an element in table scope
6315                unless (defined $i) {            my $i;
6316                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6317                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6318                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6319                  redo B;                !!!cp ('t278');
6320                }                $i = $_;
6321                  last INSCOPE;
6322                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6323                  !!!cp ('t279');
6324                  last INSCOPE;
6325                }
6326              } # INSCOPE
6327              unless (defined $i) {
6328                !!!cp ('t280');
6329                !!!parse-error (type => 'unmatched end tag',
6330                                text => 'select', token => $token);
6331                ## Ignore the token
6332                !!!nack ('t280.1');
6333                !!!next-token;
6334                next B;
6335              }
6336                                
6337                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6338              splice @{$self->{open_elements}}, $i;
6339    
6340                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6341    
6342                !!!next-token;            if ($token->{tag_name} eq 'select') {
6343                redo B;              !!!nack ('t281.2');
6344              } else {              !!!next-token;
6345                #              next B;
6346              } else {
6347                !!!cp ('t281.1');
6348                !!!ack-later;
6349                ## Reprocess the token.
6350                next B;
6351              }
6352            } else {
6353              !!!cp ('t282');
6354              !!!parse-error (type => 'in select',
6355                              text => $token->{tag_name}, token => $token);
6356              ## Ignore the token
6357              !!!nack ('t282.1');
6358              !!!next-token;
6359              next B;
6360            }
6361          } elsif ($token->{type} == END_TAG_TOKEN) {
6362            if ($token->{tag_name} eq 'optgroup') {
6363              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6364                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6365                !!!cp ('t283');
6366                ## As if </option>
6367                splice @{$self->{open_elements}}, -2;
6368              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6369                !!!cp ('t284');
6370                pop @{$self->{open_elements}};
6371              } else {
6372                !!!cp ('t285');
6373                !!!parse-error (type => 'unmatched end tag',
6374                                text => $token->{tag_name}, token => $token);
6375                ## Ignore the token
6376              }
6377              !!!nack ('t285.1');
6378              !!!next-token;
6379              next B;
6380            } elsif ($token->{tag_name} eq 'option') {
6381              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6382                !!!cp ('t286');
6383                pop @{$self->{open_elements}};
6384              } else {
6385                !!!cp ('t287');
6386                !!!parse-error (type => 'unmatched end tag',
6387                                text => $token->{tag_name}, token => $token);
6388                ## Ignore the token
6389              }
6390              !!!nack ('t287.1');
6391              !!!next-token;
6392              next B;
6393            } elsif ($token->{tag_name} eq 'select') {
6394              ## have an element in table scope
6395              my $i;
6396              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6397                my $node = $self->{open_elements}->[$_];
6398                if ($node->[1] & SELECT_EL) {
6399                  !!!cp ('t288');
6400                  $i = $_;
6401                  last INSCOPE;
6402                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6403                  !!!cp ('t289');
6404                  last INSCOPE;
6405              }              }
6406            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6407              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6408                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6409                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6410                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6411                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6412                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6413                  pop @{$self->{open_elements}};              !!!next-token;
6414                } else {              next B;
6415                  !!!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;  
               }  
6416                                
6417                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6418              splice @{$self->{open_elements}}, $i;
6419    
6420                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6421    
6422                !!!next-token;            !!!nack ('t291.1');
6423                redo B;            !!!next-token;
6424              } elsif ({            next B;
6425                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6426                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6427                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6428                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6429                                   }->{$token->{tag_name}}) {
6430                ## have an element in table scope  ## TODO: The following is wrong?
6431                my $i;            !!!parse-error (type => 'unmatched end tag',
6432                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;  
               }  
6433                                
6434                ## As if </select>            ## have an element in table scope
6435                ## have an element in table scope            my $i;
6436                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6437                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6438                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6439                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6440                    $i = $_;                $i = $_;
6441                    last INSCOPE;                last INSCOPE;
6442                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6443                            table => 1, html => 1,                !!!cp ('t293');
6444                           }->{$node->[1]}) {                last INSCOPE;
6445                    last INSCOPE;              }
6446                  }            } # INSCOPE
6447                } # INSCOPE            unless (defined $i) {
6448                unless (defined $i) {              !!!cp ('t294');
6449                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6450                  ## Ignore the </select> token              !!!nack ('t294.1');
6451                  !!!next-token; ## TODO: ok?              !!!next-token;
6452                  redo B;              next B;
6453                }            }
6454                                
6455                splice @{$self->{open_elements}}, $i;            ## As if </select>
6456              ## have an element in table scope
6457                $self->_reset_insertion_mode;            undef $i;
6458              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6459                ## reprocess              my $node = $self->{open_elements}->[$_];
6460                redo B;              if ($node->[1] & SELECT_EL) {
6461              } else {                !!!cp ('t295');
6462                #                $i = $_;
6463                  last INSCOPE;
6464                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6465    ## ISSUE: Can this state be reached?
6466                  !!!cp ('t296');
6467                  last INSCOPE;
6468              }              }
6469            } else {            } # INSCOPE
6470              #            unless (defined $i) {
6471                !!!cp ('t297');
6472    ## TODO: The following error type is correct?
6473                !!!parse-error (type => 'unmatched end tag',
6474                                text => 'select', token => $token);
6475                ## Ignore the </select> token
6476                !!!nack ('t297.1');
6477                !!!next-token; ## TODO: ok?
6478                next B;
6479            }            }
6480                  
6481              !!!cp ('t298');
6482              splice @{$self->{open_elements}}, $i;
6483    
6484              $self->_reset_insertion_mode;
6485    
6486            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
6487              ## reprocess
6488              next B;
6489            } else {
6490              !!!cp ('t299');
6491              !!!parse-error (type => 'in select:/',
6492                              text => $token->{tag_name}, token => $token);
6493            ## Ignore the token            ## Ignore the token
6494              !!!nack ('t299.3');
6495            !!!next-token;            !!!next-token;
6496            redo B;            next B;
6497          } elsif ($self->{insertion_mode} eq 'after body') {          }
6498            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6499              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6500                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
6501                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
6502                            !!!parse-error (type => 'in body:#eof', token => $token);
6503                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6504              !!!cp ('t299.2');
6505            }
6506    
6507                unless (length $token->{data}) {          ## Stop parsing.
6508                  !!!next-token;          last B;
6509                  redo B;        } else {
6510                }          die "$0: $token->{type}: Unknown token type";
6511              }        }
6512                    } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6513              #        if ($token->{type} == CHARACTER_TOKEN) {
6514              !!!parse-error (type => 'after body:#'.$token->{type});          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6515            } elsif ($token->{type} eq 'comment') {            my $data = $1;
6516              my $comment = $self->{document}->create_comment ($token->{data});            ## As if in body
6517              $self->{open_elements}->[0]->[0]->append_child ($comment);            $reconstruct_active_formatting_elements->($insert_to_current);
6518                  
6519              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6520              
6521              unless (length $token->{data}) {
6522                !!!cp ('t300');
6523              !!!next-token;              !!!next-token;
6524              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});  
6525            }            }
6526            }
6527            
6528            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6529              !!!cp ('t301');
6530              !!!parse-error (type => 'after html:#text', token => $token);
6531    
6532            $self->{insertion_mode} = 'in body';            ## Reprocess in the "after body" insertion mode.
6533            ## reprocess          } else {
6534            redo B;            !!!cp ('t302');
6535          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6536            if ($token->{type} eq 'character') {          
6537              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## "after body" insertion mode
6538                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          !!!parse-error (type => 'after body:#text', token => $token);
6539    
6540                unless (length $token->{data}) {          $self->{insertion_mode} = IN_BODY_IM;
6541                  !!!next-token;          ## reprocess
6542                  redo B;          next B;
6543                }        } elsif ($token->{type} == START_TAG_TOKEN) {
6544              }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6545              !!!cp ('t303');
6546              !!!parse-error (type => 'after html',
6547                              text => $token->{tag_name}, token => $token);
6548              
6549              ## Reprocess in the "after body" insertion mode.
6550            } else {
6551              !!!cp ('t304');
6552            }
6553    
6554              #          ## "after body" insertion mode
6555            } elsif ($token->{type} eq 'comment') {          !!!parse-error (type => 'after body',
6556              my $comment = $self->{document}->create_comment ($token->{data});                          text => $token->{tag_name}, token => $token);
6557              $self->{open_elements}->[-1]->[0]->append_child ($comment);  
6558            $self->{insertion_mode} = IN_BODY_IM;
6559            !!!ack-later;
6560            ## reprocess
6561            next B;
6562          } elsif ($token->{type} == END_TAG_TOKEN) {
6563            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6564              !!!cp ('t305');
6565              !!!parse-error (type => 'after html:/',
6566                              text => $token->{tag_name}, token => $token);
6567              
6568              $self->{insertion_mode} = AFTER_BODY_IM;
6569              ## Reprocess in the "after body" insertion mode.
6570            } else {
6571              !!!cp ('t306');
6572            }
6573    
6574            ## "after body" insertion mode
6575            if ($token->{tag_name} eq 'html') {
6576              if (defined $self->{inner_html_node}) {
6577                !!!cp ('t307');
6578                !!!parse-error (type => 'unmatched end tag',
6579                                text => 'html', token => $token);
6580                ## Ignore the token
6581              !!!next-token;              !!!next-token;
6582              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 {  
               #  
             }  
6583            } else {            } else {
6584              #              !!!cp ('t308');
6585                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6586                !!!next-token;
6587                next B;
6588              }
6589            } else {
6590              !!!cp ('t309');
6591              !!!parse-error (type => 'after body:/',
6592                              text => $token->{tag_name}, token => $token);
6593    
6594              $self->{insertion_mode} = IN_BODY_IM;
6595              ## reprocess
6596              next B;
6597            }
6598          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6599            !!!cp ('t309.2');
6600            ## Stop parsing
6601            last B;
6602          } else {
6603            die "$0: $token->{type}: Unknown token type";
6604          }
6605        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6606          if ($token->{type} == CHARACTER_TOKEN) {
6607            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6608              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6609              
6610              unless (length $token->{data}) {
6611                !!!cp ('t310');
6612                !!!next-token;
6613                next B;
6614              }
6615            }
6616            
6617            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6618              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6619                !!!cp ('t311');
6620                !!!parse-error (type => 'in frameset:#text', token => $token);
6621              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6622                !!!cp ('t312');
6623                !!!parse-error (type => 'after frameset:#text', token => $token);
6624              } else { # "after after frameset"
6625                !!!cp ('t313');
6626                !!!parse-error (type => 'after html:#text', token => $token);
6627            }            }
6628                        
6629            if (defined $token->{tag_name}) {            ## Ignore the token.
6630              !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if (length $token->{data}) {
6631                !!!cp ('t314');
6632                ## reprocess the rest of characters
6633            } else {            } else {
6634              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t315');
6635                !!!next-token;
6636              }
6637              next B;
6638            }
6639            
6640            die qq[$0: Character "$token->{data}"];
6641          } elsif ($token->{type} == START_TAG_TOKEN) {
6642            if ($token->{tag_name} eq 'frameset' and
6643                $self->{insertion_mode} == IN_FRAMESET_IM) {
6644              !!!cp ('t318');
6645              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6646              !!!nack ('t318.1');
6647              !!!next-token;
6648              next B;
6649            } elsif ($token->{tag_name} eq 'frame' and
6650                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6651              !!!cp ('t319');
6652              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6653              pop @{$self->{open_elements}};
6654              !!!ack ('t319.1');
6655              !!!next-token;
6656              next B;
6657            } elsif ($token->{tag_name} eq 'noframes') {
6658              !!!cp ('t320');
6659              ## NOTE: As if in head.
6660              $parse_rcdata->(CDATA_CONTENT_MODEL);
6661              next B;
6662    
6663              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6664              ## has no parse error.
6665            } else {
6666              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6667                !!!cp ('t321');
6668                !!!parse-error (type => 'in frameset',
6669                                text => $token->{tag_name}, token => $token);
6670              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6671                !!!cp ('t322');
6672                !!!parse-error (type => 'after frameset',
6673                                text => $token->{tag_name}, token => $token);
6674              } else { # "after after frameset"
6675                !!!cp ('t322.2');
6676                !!!parse-error (type => 'after after frameset',
6677                                text => $token->{tag_name}, token => $token);
6678            }            }
6679            ## Ignore the token            ## Ignore the token
6680              !!!nack ('t322.1');
6681            !!!next-token;            !!!next-token;
6682            redo B;            next B;
6683          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6684            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6685              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{tag_name} eq 'frameset' and
6686                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{insertion_mode} == IN_FRAMESET_IM) {
6687              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6688                  @{$self->{open_elements}} == 1) {
6689                !!!cp ('t325');
6690                !!!parse-error (type => 'unmatched end tag',
6691                                text => $token->{tag_name}, token => $token);
6692                ## Ignore the token
6693                !!!next-token;
6694              } else {
6695                !!!cp ('t326');
6696                pop @{$self->{open_elements}};
6697                !!!next-token;
6698              }
6699    
6700                unless (length $token->{data}) {            if (not defined $self->{inner_html_node} and
6701                  !!!next-token;                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6702                  redo B;              !!!cp ('t327');
6703                }              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6704              } else {
6705                !!!cp ('t328');
6706              }
6707              next B;
6708            } elsif ($token->{tag_name} eq 'html' and
6709                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6710              !!!cp ('t329');
6711              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6712              !!!next-token;
6713              next B;
6714            } else {
6715              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6716                !!!cp ('t330');
6717                !!!parse-error (type => 'in frameset:/',
6718                                text => $token->{tag_name}, token => $token);
6719              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6720                !!!cp ('t330.1');
6721                !!!parse-error (type => 'after frameset:/',
6722                                text => $token->{tag_name}, token => $token);
6723              } else { # "after after html"
6724                !!!cp ('t331');
6725                !!!parse-error (type => 'after after frameset:/',
6726                                text => $token->{tag_name}, token => $token);
6727              }
6728              ## Ignore the token
6729              !!!next-token;
6730              next B;
6731            }
6732          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6733            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6734                    @{$self->{open_elements}} == 1) { # redundant, maybe
6735              !!!cp ('t331.1');
6736              !!!parse-error (type => 'in body:#eof', token => $token);
6737            } else {
6738              !!!cp ('t331.2');
6739            }
6740            
6741            ## Stop parsing
6742            last B;
6743          } else {
6744            die "$0: $token->{type}: Unknown token type";
6745          }
6746    
6747          ## ISSUE: An issue in spec here
6748        } else {
6749          die "$0: $self->{insertion_mode}: Unknown insertion mode";
6750        }
6751    
6752        ## "in body" insertion mode
6753        if ($token->{type} == START_TAG_TOKEN) {
6754          if ($token->{tag_name} eq 'script') {
6755            !!!cp ('t332');
6756            ## NOTE: This is an "as if in head" code clone
6757            $script_start_tag->();
6758            next B;
6759          } elsif ($token->{tag_name} eq 'style') {
6760            !!!cp ('t333');
6761            ## NOTE: This is an "as if in head" code clone
6762            $parse_rcdata->(CDATA_CONTENT_MODEL);
6763            next B;
6764          } elsif ({
6765                    base => 1, link => 1,
6766                   }->{$token->{tag_name}}) {
6767            !!!cp ('t334');
6768            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6769            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6770            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6771            !!!ack ('t334.1');
6772            !!!next-token;
6773            next B;
6774          } elsif ($token->{tag_name} eq 'meta') {
6775            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6776            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6777            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6778    
6779            unless ($self->{confident}) {
6780              if ($token->{attributes}->{charset}) {
6781                !!!cp ('t335');
6782                ## NOTE: Whether the encoding is supported or not is handled
6783                ## in the {change_encoding} callback.
6784                $self->{change_encoding}
6785                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6786                
6787                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6788                    ->set_user_data (manakai_has_reference =>
6789                                         $token->{attributes}->{charset}
6790                                             ->{has_reference});
6791              } elsif ($token->{attributes}->{content}) {
6792                if ($token->{attributes}->{content}->{value}
6793                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6794                        [\x09-\x0D\x20]*=
6795                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6796                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6797                  !!!cp ('t336');
6798                  ## NOTE: Whether the encoding is supported or not is handled
6799                  ## in the {change_encoding} callback.
6800                  $self->{change_encoding}
6801                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6802                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6803                      ->set_user_data (manakai_has_reference =>
6804                                           $token->{attributes}->{content}
6805                                                 ->{has_reference});
6806              }              }
6807              }
6808            } else {
6809              if ($token->{attributes}->{charset}) {
6810                !!!cp ('t337');
6811                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6812                    ->set_user_data (manakai_has_reference =>
6813                                         $token->{attributes}->{charset}
6814                                             ->{has_reference});
6815              }
6816              if ($token->{attributes}->{content}) {
6817                !!!cp ('t338');
6818                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6819                    ->set_user_data (manakai_has_reference =>
6820                                         $token->{attributes}->{content}
6821                                             ->{has_reference});
6822              }
6823            }
6824    
6825              #          !!!ack ('t338.1');
6826            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6827              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6828              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6829              !!!next-token;          !!!cp ('t341');
6830              redo B;          ## NOTE: This is an "as if in head" code clone
6831            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6832              if ($token->{tag_name} eq 'noframes') {          next B;
6833                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6834                redo B;          !!!parse-error (type => 'in body', text => 'body', token => $token);
6835              } else {                
6836                #          if (@{$self->{open_elements}} == 1 or
6837                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6838              !!!cp ('t342');
6839              ## Ignore the token
6840            } else {
6841              my $body_el = $self->{open_elements}->[1]->[0];
6842              for my $attr_name (keys %{$token->{attributes}}) {
6843                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6844                  !!!cp ('t343');
6845                  $body_el->set_attribute_ns
6846                    (undef, [undef, $attr_name],
6847                     $token->{attributes}->{$attr_name}->{value});
6848              }              }
6849            } elsif ($token->{type} eq 'end tag') {            }
6850              if ($token->{tag_name} eq 'html') {          }
6851                $phase = 'trailing end';          !!!nack ('t343.1');
6852            !!!next-token;
6853            next B;
6854          } elsif ({
6855                    address => 1, blockquote => 1, center => 1, dir => 1,
6856                    div => 1, dl => 1, fieldset => 1,
6857                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6858                    menu => 1, ol => 1, p => 1, ul => 1,
6859                    pre => 1, listing => 1,
6860                    form => 1,
6861                    table => 1,
6862                    hr => 1,
6863                   }->{$token->{tag_name}}) {
6864            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6865              !!!cp ('t350');
6866              !!!parse-error (type => 'in form:form', token => $token);
6867              ## Ignore the token
6868              !!!nack ('t350.1');
6869              !!!next-token;
6870              next B;
6871            }
6872    
6873            ## has a p element in scope
6874            INSCOPE: for (reverse @{$self->{open_elements}}) {
6875              if ($_->[1] & P_EL) {
6876                !!!cp ('t344');
6877                !!!back-token; # <form>
6878                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6879                          line => $token->{line}, column => $token->{column}};
6880                next B;
6881              } elsif ($_->[1] & SCOPING_EL) {
6882                !!!cp ('t345');
6883                last INSCOPE;
6884              }
6885            } # INSCOPE
6886              
6887            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6888            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6889              !!!nack ('t346.1');
6890              !!!next-token;
6891              if ($token->{type} == CHARACTER_TOKEN) {
6892                $token->{data} =~ s/^\x0A//;
6893                unless (length $token->{data}) {
6894                  !!!cp ('t346');
6895                !!!next-token;                !!!next-token;
               redo B;  
6896              } else {              } else {
6897                #                !!!cp ('t349');
6898              }              }
6899            } else {            } else {
6900              #              !!!cp ('t348');
6901            }            }
6902            } elsif ($token->{tag_name} eq 'form') {
6903              !!!cp ('t347.1');
6904              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6905    
6906              !!!nack ('t347.2');
6907              !!!next-token;
6908            } elsif ($token->{tag_name} eq 'table') {
6909              !!!cp ('t382');
6910              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6911              
6912              $self->{insertion_mode} = IN_TABLE_IM;
6913    
6914              !!!nack ('t382.1');
6915              !!!next-token;
6916            } elsif ($token->{tag_name} eq 'hr') {
6917              !!!cp ('t386');
6918              pop @{$self->{open_elements}};
6919            
6920              !!!nack ('t386.1');
6921              !!!next-token;
6922            } else {
6923              !!!nack ('t347.1');
6924              !!!next-token;
6925            }
6926            next B;
6927          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6928            ## has a p element in scope
6929            INSCOPE: for (reverse @{$self->{open_elements}}) {
6930              if ($_->[1] & P_EL) {
6931                !!!cp ('t353');
6932                !!!back-token; # <x>
6933                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6934                          line => $token->{line}, column => $token->{column}};
6935                next B;
6936              } elsif ($_->[1] & SCOPING_EL) {
6937                !!!cp ('t354');
6938                last INSCOPE;
6939              }
6940            } # INSCOPE
6941                        
6942            if (defined $token->{tag_name}) {          ## Step 1
6943              !!!parse-error (type => 'after frameset:'.$token->{tag_name});          my $i = -1;
6944            my $node = $self->{open_elements}->[$i];
6945            my $li_or_dtdd = {li => {li => 1},
6946                              dt => {dt => 1, dd => 1},
6947                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6948            LI: {
6949              ## Step 2
6950              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6951                if ($i != -1) {
6952                  !!!cp ('t355');
6953                  !!!parse-error (type => 'not closed',
6954                                  text => $self->{open_elements}->[-1]->[0]
6955                                      ->manakai_local_name,
6956                                  token => $token);
6957                } else {
6958                  !!!cp ('t356');
6959                }
6960                splice @{$self->{open_elements}}, $i;
6961                last LI;
6962            } else {            } else {
6963              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
6964              }
6965              
6966              ## Step 3
6967              if (not ($node->[1] & FORMATTING_EL) and
6968                  #not $phrasing_category->{$node->[1]} and
6969                  ($node->[1] & SPECIAL_EL or
6970                   $node->[1] & SCOPING_EL) and
6971                  not ($node->[1] & ADDRESS_EL) and
6972                  not ($node->[1] & DIV_EL)) {
6973                !!!cp ('t358');
6974                last LI;
6975              }
6976              
6977              !!!cp ('t359');
6978              ## Step 4
6979              $i--;
6980              $node = $self->{open_elements}->[$i];
6981              redo LI;
6982            } # LI
6983              
6984            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6985            !!!nack ('t359.1');
6986            !!!next-token;
6987            next B;
6988          } elsif ($token->{tag_name} eq 'plaintext') {
6989            ## has a p element in scope
6990            INSCOPE: for (reverse @{$self->{open_elements}}) {
6991              if ($_->[1] & P_EL) {
6992                !!!cp ('t367');
6993                !!!back-token; # <plaintext>
6994                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6995                          line => $token->{line}, column => $token->{column}};
6996                next B;
6997              } elsif ($_->[1] & SCOPING_EL) {
6998                !!!cp ('t368');
6999                last INSCOPE;
7000              }
7001            } # INSCOPE
7002              
7003            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7004              
7005            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
7006              
7007            !!!nack ('t368.1');
7008            !!!next-token;
7009            next B;
7010          } elsif ($token->{tag_name} eq 'a') {
7011            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
7012              my $node = $active_formatting_elements->[$i];
7013              if ($node->[1] & A_EL) {
7014                !!!cp ('t371');
7015                !!!parse-error (type => 'in a:a', token => $token);
7016                
7017                !!!back-token; # <a>
7018                $token = {type => END_TAG_TOKEN, tag_name => 'a',
7019                          line => $token->{line}, column => $token->{column}};
7020                $formatting_end_tag->($token);
7021                
7022                AFE2: for (reverse 0..$#$active_formatting_elements) {
7023                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
7024                    !!!cp ('t372');
7025                    splice @$active_formatting_elements, $_, 1;
7026                    last AFE2;
7027                  }
7028                } # AFE2
7029                OE: for (reverse 0..$#{$self->{open_elements}}) {
7030                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
7031                    !!!cp ('t373');
7032                    splice @{$self->{open_elements}}, $_, 1;
7033                    last OE;
7034                  }
7035                } # OE
7036                last AFE;
7037              } elsif ($node->[0] eq '#marker') {
7038                !!!cp ('t374');
7039                last AFE;
7040              }
7041            } # AFE
7042              
7043            $reconstruct_active_formatting_elements->($insert_to_current);
7044    
7045            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7046            push @$active_formatting_elements, $self->{open_elements}->[-1];
7047    
7048            !!!nack ('t374.1');
7049            !!!next-token;
7050            next B;
7051          } elsif ($token->{tag_name} eq 'nobr') {
7052            $reconstruct_active_formatting_elements->($insert_to_current);
7053    
7054            ## has a |nobr| element in scope
7055            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7056              my $node = $self->{open_elements}->[$_];
7057              if ($node->[1] & NOBR_EL) {
7058                !!!cp ('t376');
7059                !!!parse-error (type => 'in nobr:nobr', token => $token);
7060                !!!back-token; # <nobr>
7061                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7062                          line => $token->{line}, column => $token->{column}};
7063                next B;
7064              } elsif ($node->[1] & SCOPING_EL) {
7065                !!!cp ('t377');
7066                last INSCOPE;
7067              }
7068            } # INSCOPE
7069            
7070            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7071            push @$active_formatting_elements, $self->{open_elements}->[-1];
7072            
7073            !!!nack ('t377.1');
7074            !!!next-token;
7075            next B;
7076          } elsif ($token->{tag_name} eq 'button') {
7077            ## has a button element in scope
7078            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7079              my $node = $self->{open_elements}->[$_];
7080              if ($node->[1] & BUTTON_EL) {
7081                !!!cp ('t378');
7082                !!!parse-error (type => 'in button:button', token => $token);
7083                !!!back-token; # <button>
7084                $token = {type => END_TAG_TOKEN, tag_name => 'button',
7085                          line => $token->{line}, column => $token->{column}};
7086                next B;
7087              } elsif ($node->[1] & SCOPING_EL) {
7088                !!!cp ('t379');
7089                last INSCOPE;
7090            }            }
7091            } # INSCOPE
7092              
7093            $reconstruct_active_formatting_elements->($insert_to_current);
7094              
7095            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7096    
7097            ## TODO: associate with $self->{form_element} if defined
7098    
7099            push @$active_formatting_elements, ['#marker', ''];
7100    
7101            !!!nack ('t379.1');
7102            !!!next-token;
7103            next B;
7104          } elsif ({
7105                    xmp => 1,
7106                    iframe => 1,
7107                    noembed => 1,
7108                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7109                    noscript => 0, ## TODO: 1 if scripting is enabled
7110                   }->{$token->{tag_name}}) {
7111            if ($token->{tag_name} eq 'xmp') {
7112              !!!cp ('t381');
7113              $reconstruct_active_formatting_elements->($insert_to_current);
7114            } else {
7115              !!!cp ('t399');
7116            }
7117            ## NOTE: There is an "as if in body" code clone.
7118            $parse_rcdata->(CDATA_CONTENT_MODEL);
7119            next B;
7120          } elsif ($token->{tag_name} eq 'isindex') {
7121            !!!parse-error (type => 'isindex', token => $token);
7122            
7123            if (defined $self->{form_element}) {
7124              !!!cp ('t389');
7125            ## Ignore the token            ## Ignore the token
7126              !!!nack ('t389'); ## NOTE: Not acknowledged.
7127            !!!next-token;            !!!next-token;
7128            redo B;            next B;
7129            } else {
7130              !!!ack ('t391.1');
7131    
7132            ## ISSUE: An issue in spec there            my $at = $token->{attributes};
7133              my $form_attrs;
7134              $form_attrs->{action} = $at->{action} if $at->{action};
7135              my $prompt_attr = $at->{prompt};
7136              $at->{name} = {name => 'name', value => 'isindex'};
7137              delete $at->{action};
7138              delete $at->{prompt};
7139              my @tokens = (
7140                            {type => START_TAG_TOKEN, tag_name => 'form',
7141                             attributes => $form_attrs,
7142                             line => $token->{line}, column => $token->{column}},
7143                            {type => START_TAG_TOKEN, tag_name => 'hr',
7144                             line => $token->{line}, column => $token->{column}},
7145                            {type => START_TAG_TOKEN, tag_name => 'p',
7146                             line => $token->{line}, column => $token->{column}},
7147                            {type => START_TAG_TOKEN, tag_name => 'label',
7148                             line => $token->{line}, column => $token->{column}},
7149                           );
7150              if ($prompt_attr) {
7151                !!!cp ('t390');
7152                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7153                               #line => $token->{line}, column => $token->{column},
7154                              };
7155              } else {
7156                !!!cp ('t391');
7157                push @tokens, {type => CHARACTER_TOKEN,
7158                               data => 'This is a searchable index. Insert your search keywords here: ',
7159                               #line => $token->{line}, column => $token->{column},
7160                              }; # SHOULD
7161                ## TODO: make this configurable
7162              }
7163              push @tokens,
7164                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7165                             line => $token->{line}, column => $token->{column}},
7166                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7167                            {type => END_TAG_TOKEN, tag_name => 'label',
7168                             line => $token->{line}, column => $token->{column}},
7169                            {type => END_TAG_TOKEN, tag_name => 'p',
7170                             line => $token->{line}, column => $token->{column}},
7171                            {type => START_TAG_TOKEN, tag_name => 'hr',
7172                             line => $token->{line}, column => $token->{column}},
7173                            {type => END_TAG_TOKEN, tag_name => 'form',
7174                             line => $token->{line}, column => $token->{column}};
7175              !!!back-token (@tokens);
7176              !!!next-token;
7177              next B;
7178            }
7179          } elsif ($token->{tag_name} eq 'textarea') {
7180            my $tag_name = $token->{tag_name};
7181            my $el;
7182            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7183            
7184            ## TODO: $self->{form_element} if defined
7185            $self->{content_model} = RCDATA_CONTENT_MODEL;
7186            delete $self->{escape}; # MUST
7187            
7188            $insert->($el);
7189            
7190            my $text = '';
7191            !!!nack ('t392.1');
7192            !!!next-token;
7193            if ($token->{type} == CHARACTER_TOKEN) {
7194              $token->{data} =~ s/^\x0A//;
7195              unless (length $token->{data}) {
7196                !!!cp ('t392');
7197                !!!next-token;
7198              } else {
7199                !!!cp ('t393');
7200              }
7201          } else {          } else {
7202            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t394');
7203            }
7204            while ($token->{type} == CHARACTER_TOKEN) {
7205              !!!cp ('t395');
7206              $text .= $token->{data};
7207              !!!next-token;
7208            }
7209            if (length $text) {
7210              !!!cp ('t396');
7211              $el->manakai_append_text ($text);
7212            }
7213            
7214            $self->{content_model} = PCDATA_CONTENT_MODEL;
7215            
7216            if ($token->{type} == END_TAG_TOKEN and
7217                $token->{tag_name} eq $tag_name) {
7218              !!!cp ('t397');
7219              ## Ignore the token
7220            } else {
7221              !!!cp ('t398');
7222              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7223          }          }
       }  
     } 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  
7224          !!!next-token;          !!!next-token;
7225          redo B;          next B;
7226        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'rt' or
7227          my $comment = $self->{document}->create_comment ($token->{data});                 $token->{tag_name} eq 'rp') {
7228          $self->{document}->append_child ($comment);          ## has a |ruby| element in scope
7229            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7230              my $node = $self->{open_elements}->[$_];
7231              if ($node->[1] & RUBY_EL) {
7232                !!!cp ('t398.1');
7233                ## generate implied end tags
7234                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7235                  !!!cp ('t398.2');
7236                  pop @{$self->{open_elements}};
7237                }
7238                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7239                  !!!cp ('t398.3');
7240                  !!!parse-error (type => 'not closed',
7241                                  text => $self->{open_elements}->[-1]->[0]
7242                                      ->manakai_local_name,
7243                                  token => $token);
7244                  pop @{$self->{open_elements}}
7245                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7246                }
7247                last INSCOPE;
7248              } elsif ($node->[1] & SCOPING_EL) {
7249                !!!cp ('t398.4');
7250                last INSCOPE;
7251              }
7252            } # INSCOPE
7253    
7254            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7255    
7256            !!!nack ('t398.5');
7257          !!!next-token;          !!!next-token;
7258          redo B;          redo B;
7259        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'math' or
7260          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                 $token->{tag_name} eq 'svg') {
7261            my $data = $1;          $reconstruct_active_formatting_elements->($insert_to_current);
7262            ## As if in the main phase.  
7263            ## NOTE: The insertion mode in the main phase          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7264            ## just before the phase has been changed to the trailing  
7265            ## end phase is either "after body" or "after frameset".          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7266            $reconstruct_active_formatting_elements->($insert_to_current)  
7267              if $phase eq 'main';          ## "adjust foreign attributes" - done in insert-element-f
7268            
7269            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7270            
7271            if ($self->{self_closing}) {
7272              pop @{$self->{open_elements}};
7273              !!!ack ('t398.1');
7274            } else {
7275              !!!cp ('t398.2');
7276              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7277              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7278              ## mode, "in body" (not "in foreign content") secondary insertion
7279              ## mode, maybe.
7280            }
7281    
7282            !!!next-token;
7283            next B;
7284          } elsif ({
7285                    caption => 1, col => 1, colgroup => 1, frame => 1,
7286                    frameset => 1, head => 1, option => 1, optgroup => 1,
7287                    tbody => 1, td => 1, tfoot => 1, th => 1,
7288                    thead => 1, tr => 1,
7289                   }->{$token->{tag_name}}) {
7290            !!!cp ('t401');
7291            !!!parse-error (type => 'in body',
7292                            text => $token->{tag_name}, token => $token);
7293            ## Ignore the token
7294            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7295            !!!next-token;
7296            next B;
7297            
7298            ## ISSUE: An issue on HTML5 new elements in the spec.
7299          } else {
7300            if ($token->{tag_name} eq 'image') {
7301              !!!cp ('t384');
7302              !!!parse-error (type => 'image', token => $token);
7303              $token->{tag_name} = 'img';
7304            } else {
7305              !!!cp ('t385');
7306            }
7307    
7308            ## NOTE: There is an "as if <br>" code clone.
7309            $reconstruct_active_formatting_elements->($insert_to_current);
7310            
7311            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7312    
7313            if ({
7314                 applet => 1, marquee => 1, object => 1,
7315                }->{$token->{tag_name}}) {
7316              !!!cp ('t380');
7317              push @$active_formatting_elements, ['#marker', ''];
7318              !!!nack ('t380.1');
7319            } elsif ({
7320                      b => 1, big => 1, em => 1, font => 1, i => 1,
7321                      s => 1, small => 1, strile => 1,
7322                      strong => 1, tt => 1, u => 1,
7323                     }->{$token->{tag_name}}) {
7324              !!!cp ('t375');
7325              push @$active_formatting_elements, $self->{open_elements}->[-1];
7326              !!!nack ('t375.1');
7327            } elsif ($token->{tag_name} eq 'input') {
7328              !!!cp ('t388');
7329              ## TODO: associate with $self->{form_element} if defined
7330              pop @{$self->{open_elements}};
7331              !!!ack ('t388.2');
7332            } elsif ({
7333                      area => 1, basefont => 1, bgsound => 1, br => 1,
7334                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7335                      #image => 1,
7336                     }->{$token->{tag_name}}) {
7337              !!!cp ('t388.1');
7338              pop @{$self->{open_elements}};
7339              !!!ack ('t388.3');
7340            } elsif ($token->{tag_name} eq 'select') {
7341              ## TODO: associate with $self->{form_element} if defined
7342            
7343              if ($self->{insertion_mode} & TABLE_IMS or
7344                  $self->{insertion_mode} & BODY_TABLE_IMS or
7345                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7346                !!!cp ('t400.1');
7347                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7348              } else {
7349                !!!cp ('t400.2');
7350                $self->{insertion_mode} = IN_SELECT_IM;
7351              }
7352              !!!nack ('t400.3');
7353            } else {
7354              !!!nack ('t402');
7355            }
7356            
7357            !!!next-token;
7358            next B;
7359          }
7360        } elsif ($token->{type} == END_TAG_TOKEN) {
7361          if ($token->{tag_name} eq 'body') {
7362            ## has a |body| element in scope
7363            my $i;
7364            INSCOPE: {
7365              for (reverse @{$self->{open_elements}}) {
7366                if ($_->[1] & BODY_EL) {
7367                  !!!cp ('t405');
7368                  $i = $_;
7369                  last INSCOPE;
7370                } elsif ($_->[1] & SCOPING_EL) {
7371                  !!!cp ('t405.1');
7372                  last;
7373                }
7374              }
7375    
7376              !!!parse-error (type => 'start tag not allowed',
7377                              text => $token->{tag_name}, token => $token);
7378              ## NOTE: Ignore the token.
7379              !!!next-token;
7380              next B;
7381            } # INSCOPE
7382    
7383            for (@{$self->{open_elements}}) {
7384              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7385                !!!cp ('t403');
7386                !!!parse-error (type => 'not closed',
7387                                text => $_->[0]->manakai_local_name,
7388                                token => $token);
7389                last;
7390              } else {
7391                !!!cp ('t404');
7392              }
7393            }
7394    
7395            $self->{insertion_mode} = AFTER_BODY_IM;
7396            !!!next-token;
7397            next B;
7398          } elsif ($token->{tag_name} eq 'html') {
7399            ## TODO: Update this code.  It seems that the code below is not
7400            ## up-to-date, though it has same effect as speced.
7401            if (@{$self->{open_elements}} > 1 and
7402                $self->{open_elements}->[1]->[1] & BODY_EL) {
7403              ## ISSUE: There is an issue in the spec.
7404              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7405                !!!cp ('t406');
7406                !!!parse-error (type => 'not closed',
7407                                text => $self->{open_elements}->[1]->[0]
7408                                    ->manakai_local_name,
7409                                token => $token);
7410              } else {
7411                !!!cp ('t407');
7412              }
7413              $self->{insertion_mode} = AFTER_BODY_IM;
7414              ## reprocess
7415              next B;
7416            } else {
7417              !!!cp ('t408');
7418              !!!parse-error (type => 'unmatched end tag',
7419                              text => $token->{tag_name}, token => $token);
7420              ## Ignore the token
7421              !!!next-token;
7422              next B;
7423            }
7424          } elsif ({
7425                    address => 1, blockquote => 1, center => 1, dir => 1,
7426                    div => 1, dl => 1, fieldset => 1, listing => 1,
7427                    menu => 1, ol => 1, pre => 1, ul => 1,
7428                    dd => 1, dt => 1, li => 1,
7429                    applet => 1, button => 1, marquee => 1, object => 1,
7430                   }->{$token->{tag_name}}) {
7431            ## has an element in scope
7432            my $i;
7433            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7434              my $node = $self->{open_elements}->[$_];
7435              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7436                !!!cp ('t410');
7437                $i = $_;
7438                last INSCOPE;
7439              } elsif ($node->[1] & SCOPING_EL) {
7440                !!!cp ('t411');
7441                last INSCOPE;
7442              }
7443            } # INSCOPE
7444    
7445            unless (defined $i) { # has an element in scope
7446              !!!cp ('t413');
7447              !!!parse-error (type => 'unmatched end tag',
7448                              text => $token->{tag_name}, token => $token);
7449              ## NOTE: Ignore the token.
7450            } else {
7451              ## Step 1. generate implied end tags
7452              while ({
7453                      ## END_TAG_OPTIONAL_EL
7454                      dd => ($token->{tag_name} ne 'dd'),
7455                      dt => ($token->{tag_name} ne 'dt'),
7456                      li => ($token->{tag_name} ne 'li'),
7457                      p => 1,
7458                      rt => 1,
7459                      rp => 1,
7460                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7461                !!!cp ('t409');
7462                pop @{$self->{open_elements}};
7463              }
7464    
7465              ## Step 2.
7466              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7467                      ne $token->{tag_name}) {
7468                !!!cp ('t412');
7469                !!!parse-error (type => 'not closed',
7470                                text => $self->{open_elements}->[-1]->[0]
7471                                    ->manakai_local_name,
7472                                token => $token);
7473              } else {
7474                !!!cp ('t414');
7475              }
7476    
7477              ## Step 3.
7478              splice @{$self->{open_elements}}, $i;
7479    
7480              ## Step 4.
7481              $clear_up_to_marker->()
7482                  if {
7483                    applet => 1, button => 1, marquee => 1, object => 1,
7484                  }->{$token->{tag_name}};
7485            }
7486            !!!next-token;
7487            next B;
7488          } elsif ($token->{tag_name} eq 'form') {
7489            undef $self->{form_element};
7490    
7491            ## has an element in scope
7492            my $i;
7493            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7494              my $node = $self->{open_elements}->[$_];
7495              if ($node->[1] & FORM_EL) {
7496                !!!cp ('t418');
7497                $i = $_;
7498                last INSCOPE;
7499              } elsif ($node->[1] & SCOPING_EL) {
7500                !!!cp ('t419');
7501                last INSCOPE;
7502              }
7503            } # INSCOPE
7504    
7505            unless (defined $i) { # has an element in scope
7506              !!!cp ('t421');
7507              !!!parse-error (type => 'unmatched end tag',
7508                              text => $token->{tag_name}, token => $token);
7509              ## NOTE: Ignore the token.
7510            } else {
7511              ## Step 1. generate implied end tags
7512              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7513                !!!cp ('t417');
7514                pop @{$self->{open_elements}};
7515              }
7516                        
7517            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
7518              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7519                      ne $token->{tag_name}) {
7520                !!!cp ('t417.1');
7521                !!!parse-error (type => 'not closed',
7522                                text => $self->{open_elements}->[-1]->[0]
7523                                    ->manakai_local_name,
7524                                token => $token);
7525              } else {
7526                !!!cp ('t420');
7527              }  
7528                        
7529            unless (length $token->{data}) {            ## Step 3.
7530              !!!next-token;            splice @{$self->{open_elements}}, $i;
7531              redo B;          }
7532    
7533            !!!next-token;
7534            next B;
7535          } elsif ({
7536                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7537                   }->{$token->{tag_name}}) {
7538            ## has an element in scope
7539            my $i;
7540            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7541              my $node = $self->{open_elements}->[$_];
7542              if ($node->[1] & HEADING_EL) {
7543                !!!cp ('t423');
7544                $i = $_;
7545                last INSCOPE;
7546              } elsif ($node->[1] & SCOPING_EL) {
7547                !!!cp ('t424');
7548                last INSCOPE;
7549              }
7550            } # INSCOPE
7551    
7552            unless (defined $i) { # has an element in scope
7553              !!!cp ('t425.1');
7554              !!!parse-error (type => 'unmatched end tag',
7555                              text => $token->{tag_name}, token => $token);
7556              ## NOTE: Ignore the token.
7557            } else {
7558              ## Step 1. generate implied end tags
7559              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7560                !!!cp ('t422');
7561                pop @{$self->{open_elements}};
7562              }
7563              
7564              ## Step 2.
7565              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7566                      ne $token->{tag_name}) {
7567                !!!cp ('t425');
7568                !!!parse-error (type => 'unmatched end tag',
7569                                text => $token->{tag_name}, token => $token);
7570              } else {
7571                !!!cp ('t426');
7572            }            }
7573    
7574              ## Step 3.
7575              splice @{$self->{open_elements}}, $i;
7576          }          }
7577            
7578            !!!next-token;
7579            next B;
7580          } elsif ($token->{tag_name} eq 'p') {
7581            ## has an element in scope
7582            my $i;
7583            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7584              my $node = $self->{open_elements}->[$_];
7585              if ($node->[1] & P_EL) {
7586                !!!cp ('t410.1');
7587                $i = $_;
7588                last INSCOPE;
7589              } elsif ($node->[1] & SCOPING_EL) {
7590                !!!cp ('t411.1');
7591                last INSCOPE;
7592              }
7593            } # INSCOPE
7594    
7595          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7596          $phase = 'main';            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7597          ## reprocess                    ne $token->{tag_name}) {
7598          redo B;              !!!cp ('t412.1');
7599        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
7600                 $token->{type} eq 'end tag') {                              text => $self->{open_elements}->[-1]->[0]
7601          !!!parse-error (type => 'after html:'.$token->{tag_name});                                  ->manakai_local_name,
7602          $phase = 'main';                              token => $token);
7603          ## reprocess            } else {
7604          redo B;              !!!cp ('t414.1');
7605        } elsif ($token->{type} eq 'end-of-file') {            }
7606          ## Stop parsing  
7607          last B;            splice @{$self->{open_elements}}, $i;
7608            } else {
7609              !!!cp ('t413.1');
7610              !!!parse-error (type => 'unmatched end tag',
7611                              text => $token->{tag_name}, token => $token);
7612    
7613              !!!cp ('t415.1');
7614              ## As if <p>, then reprocess the current token
7615              my $el;
7616              !!!create-element ($el, $HTML_NS, 'p',, $token);
7617              $insert->($el);
7618              ## NOTE: Not inserted into |$self->{open_elements}|.
7619            }
7620    
7621            !!!next-token;
7622            next B;
7623          } elsif ({
7624                    a => 1,
7625                    b => 1, big => 1, em => 1, font => 1, i => 1,
7626                    nobr => 1, s => 1, small => 1, strile => 1,
7627                    strong => 1, tt => 1, u => 1,
7628                   }->{$token->{tag_name}}) {
7629            !!!cp ('t427');
7630            $formatting_end_tag->($token);
7631            next B;
7632          } elsif ($token->{tag_name} eq 'br') {
7633            !!!cp ('t428');
7634            !!!parse-error (type => 'unmatched end tag',
7635                            text => 'br', token => $token);
7636    
7637            ## As if <br>
7638            $reconstruct_active_formatting_elements->($insert_to_current);
7639            
7640            my $el;
7641            !!!create-element ($el, $HTML_NS, 'br',, $token);
7642            $insert->($el);
7643            
7644            ## Ignore the token.
7645            !!!next-token;
7646            next B;
7647          } elsif ({
7648                    caption => 1, col => 1, colgroup => 1, frame => 1,
7649                    frameset => 1, head => 1, option => 1, optgroup => 1,
7650                    tbody => 1, td => 1, tfoot => 1, th => 1,
7651                    thead => 1, tr => 1,
7652                    area => 1, basefont => 1, bgsound => 1,
7653                    embed => 1, hr => 1, iframe => 1, image => 1,
7654                    img => 1, input => 1, isindex => 1, noembed => 1,
7655                    noframes => 1, param => 1, select => 1, spacer => 1,
7656                    table => 1, textarea => 1, wbr => 1,
7657                    noscript => 0, ## TODO: if scripting is enabled
7658                   }->{$token->{tag_name}}) {
7659            !!!cp ('t429');
7660            !!!parse-error (type => 'unmatched end tag',
7661                            text => $token->{tag_name}, token => $token);
7662            ## Ignore the token
7663            !!!next-token;
7664            next B;
7665            
7666            ## ISSUE: Issue on HTML5 new elements in spec
7667            
7668        } else {        } else {
7669          die "$0: $token->{type}: Unknown token";          ## Step 1
7670            my $node_i = -1;
7671            my $node = $self->{open_elements}->[$node_i];
7672    
7673            ## Step 2
7674            S2: {
7675              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7676                ## Step 1
7677                ## generate implied end tags
7678                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7679                  !!!cp ('t430');
7680                  ## NOTE: |<ruby><rt></ruby>|.
7681                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7682                  ## which seems wrong.
7683                  pop @{$self->{open_elements}};
7684                  $node_i++;
7685                }
7686            
7687                ## Step 2
7688                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7689                        ne $token->{tag_name}) {
7690                  !!!cp ('t431');
7691                  ## NOTE: <x><y></x>
7692                  !!!parse-error (type => 'not closed',
7693                                  text => $self->{open_elements}->[-1]->[0]
7694                                      ->manakai_local_name,
7695                                  token => $token);
7696                } else {
7697                  !!!cp ('t432');
7698                }
7699                
7700                ## Step 3
7701                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7702    
7703                !!!next-token;
7704                last S2;
7705              } else {
7706                ## Step 3
7707                if (not ($node->[1] & FORMATTING_EL) and
7708                    #not $phrasing_category->{$node->[1]} and
7709                    ($node->[1] & SPECIAL_EL or
7710                     $node->[1] & SCOPING_EL)) {
7711                  !!!cp ('t433');
7712                  !!!parse-error (type => 'unmatched end tag',
7713                                  text => $token->{tag_name}, token => $token);
7714                  ## Ignore the token
7715                  !!!next-token;
7716                  last S2;
7717                }
7718    
7719                !!!cp ('t434');
7720              }
7721              
7722              ## Step 4
7723              $node_i--;
7724              $node = $self->{open_elements}->[$node_i];
7725              
7726              ## Step 5;
7727              redo S2;
7728            } # S2
7729            next B;
7730        }        }
7731      }      }
7732        next B;
7733      } continue { # B
7734        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7735          ## NOTE: The code below is executed in cases where it does not have
7736          ## to be, but it it is harmless even in those cases.
7737          ## has an element in scope
7738          INSCOPE: {
7739            for (reverse 0..$#{$self->{open_elements}}) {
7740              my $node = $self->{open_elements}->[$_];
7741              if ($node->[1] & FOREIGN_EL) {
7742                last INSCOPE;
7743              } elsif ($node->[1] & SCOPING_EL) {
7744                last;
7745              }
7746            }
7747            
7748            ## NOTE: No foreign element in scope.
7749            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7750          } # INSCOPE
7751        }
7752    } # B    } # B
7753    
7754    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4765  sub _tree_construction_main ($) { Line 7756  sub _tree_construction_main ($) {
7756    ## TODO: script stuffs    ## TODO: script stuffs
7757  } # _tree_construct_main  } # _tree_construct_main
7758    
7759  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7760    my $class = shift;    my $class = shift;
7761    my $node = shift;    my $node = shift;
7762    my $s = \$_[0];    #my $s = \$_[0];
7763    my $onerror = $_[1];    my $onerror = $_[1];
7764      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7765    
7766      ## ISSUE: Should {confident} be true?
7767    
7768    my $nt = $node->node_type;    my $nt = $node->node_type;
7769    if ($nt == 9) {    if ($nt == 9) {
# Line 4786  sub set_inner_html ($$$) { Line 7780  sub set_inner_html ($$$) {
7780      }      }
7781    
7782      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7783      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7784    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7785      ## TODO: If non-html element      ## TODO: If non-html element
7786    
7787      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7788    
7789    ## TODO: Support for $get_wrapper
7790    
7791      ## Step 1 # MUST      ## Step 1 # MUST
7792      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
7793      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
7794        $doc->manakai_is_html (1);
7795      my $p = $class->new;      my $p = $class->new;
7796      $p->{document} = $doc;      $p->{document} = $doc;
7797    
7798      ## Step 9 # MUST      ## Step 8 # MUST
7799      my $i = 0;      my $i = 0;
7800      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7801      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7802      $p->{set_next_input_character} = sub {      require Whatpm::Charset::DecodeHandle;
7803        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7804        $input = $get_wrapper->($input);
7805        $p->{set_next_char} = sub {
7806        my $self = shift;        my $self = shift;
7807        $self->{next_input_character} = -1 and return if $i >= length $$s;  
7808        $self->{next_input_character} = ord substr $$s, $i++, 1;        pop @{$self->{prev_char}};
7809        $column++;        unshift @{$self->{prev_char}}, $self->{next_char};
7810          
7811        if ($self->{next_input_character} == 0x000D) { # CR        my $char = '';
7812          if ($i >= length $$s) {        if (defined $self->{next_next_char}) {
7813            #          $char = $self->{next_next_char};
7814            delete $self->{next_next_char};
7815            $self->{next_char} = ord $char;
7816          } else {
7817            if ($input->read ($char, 1)) {
7818              $self->{next_char} = ord $char;
7819          } else {          } else {
7820            my $next_char = ord substr $$s, $i++, 1;            $self->{next_char} = -1;
7821            if ($next_char == 0x000A) { # LF            return;
7822              #          }
7823            } else {        }
7824              push @{$self->{char}}, $next_char;  
7825            }        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7826          $p->{column}++;
7827    
7828          if ($self->{next_char} == 0x000A) { # LF
7829            $p->{line}++;
7830            $p->{column} = 0;
7831            !!!cp ('i1');
7832          } elsif ($self->{next_char} == 0x000D) { # CR
7833    ## TODO: support for abort/streaming
7834            my $next = '';
7835            if ($input->read ($next, 1) and $next ne "\x0A") {
7836              $self->{next_next_char} = $next;
7837            }
7838            $self->{next_char} = 0x000A; # LF # MUST
7839            $p->{line}++;
7840            $p->{column} = 0;
7841            !!!cp ('i2');
7842          } elsif ($self->{next_char} > 0x10FFFF) {
7843            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7844            !!!cp ('i3');
7845          } elsif ($self->{next_char} == 0x0000) { # NULL
7846            !!!cp ('i4');
7847            !!!parse-error (type => 'NULL');
7848            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7849          } elsif ($self->{next_char} <= 0x0008 or
7850                   (0x000E <= $self->{next_char} and
7851                    $self->{next_char} <= 0x001F) or
7852                   (0x007F <= $self->{next_char} and
7853                    $self->{next_char} <= 0x009F) or
7854                   (0xD800 <= $self->{next_char} and
7855                    $self->{next_char} <= 0xDFFF) or
7856                   (0xFDD0 <= $self->{next_char} and
7857                    $self->{next_char} <= 0xFDDF) or
7858                   {
7859                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7860                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7861                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7862                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7863                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7864                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7865                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7866                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7867                    0x10FFFE => 1, 0x10FFFF => 1,
7868                   }->{$self->{next_char}}) {
7869            !!!cp ('i4.1');
7870            if ($self->{next_char} < 0x10000) {
7871              !!!parse-error (type => 'control char',
7872                              text => (sprintf 'U+%04X', $self->{next_char}));
7873            } else {
7874              !!!parse-error (type => 'control char',
7875                              text => (sprintf 'U-%08X', $self->{next_char}));
7876          }          }
         $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  
7877        }        }
7878      };      };
7879            $p->{prev_char} = [-1, -1, -1];
7880        $p->{next_char} = -1;
7881    
7882        $p->{read_until} = sub {
7883          #my ($scalar, $specials_range, $offset) = @_;
7884          my $specials_range = $_[1];
7885          return 0 if defined $p->{next_next_char};
7886          my $count = $input->manakai_read_until
7887            ($_[0],
7888             qr/(?![$specials_range\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
7889             $_[2]);
7890          if ($count) {
7891            $p->{column} += $count;
7892            $p->{column_prev} += $count;
7893            $p->{prev_char} = [-1, -1, -1];
7894            $p->{next_char} = -1;
7895          }
7896          return $count;
7897        }; # $p->{read_until}
7898    
7899      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7900        my (%opt) = @_;        my (%opt) = @_;
7901        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7902          my $column = $opt{column};
7903          if (defined $opt{token} and defined $opt{token}->{line}) {
7904            $line = $opt{token}->{line};
7905            $column = $opt{token}->{column};
7906          }
7907          warn "Parse error ($opt{type}) at line $line column $column\n";
7908      };      };
7909      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7910        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7911      };      };
7912            
7913        my $char_onerror = sub {
7914          my (undef, $type, %opt) = @_;
7915          $ponerror->(layer => 'encode',
7916                      line => $p->{line}, column => $p->{column} + 1,
7917                      %opt, type => $type);
7918        }; # $char_onerror
7919        $input->onerror ($char_onerror);
7920    
7921      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7922      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7923    
7924      ## Step 2      ## Step 2
7925      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7926      $p->{content_model_flag} = {      $p->{content_model} = {
7927        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7928        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7929        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7930        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7931        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7932        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7933        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7934        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7935        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7936        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7937      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7938         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7939            unless defined $p->{content_model};
7940            ## ISSUE: What is "the name of the element"? local name?
7941    
7942      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7943          ## TODO: Foreign element OK?
7944    
7945      ## Step 4      ## Step 3
7946      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7947        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7948    
7949      ## Step 5 # MUST      ## Step 4 # MUST
7950      $doc->append_child ($root);      $doc->append_child ($root);
7951    
7952      ## Step 6 # MUST      ## Step 5 # MUST
7953      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7954    
7955      undef $p->{head_element};      undef $p->{head_element};
7956    
7957      ## Step 7 # MUST      ## Step 6 # MUST
7958      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7959    
7960      ## Step 8 # MUST      ## Step 7 # MUST
7961      my $anode = $node;      my $anode = $node;
7962      AN: while (defined $anode) {      AN: while (defined $anode) {
7963        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7964          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7965          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7966            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7967                !!!cp ('i5');
7968              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7969              last AN;              last AN;
7970            }            }
# Line 4888  sub set_inner_html ($$$) { Line 7973  sub set_inner_html ($$$) {
7973        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7974      } # AN      } # AN
7975            
7976      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7977      {      {
7978        my $self = $p;        my $self = $p;
7979        !!!next-token;        !!!next-token;
7980      }      }
7981      $p->_tree_construction_main;      $p->_tree_construction_main;
7982    
7983      ## Step 11 # MUST      ## Step 10 # MUST
7984      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7985      for (@cn) {      for (@cn) {
7986        $node->remove_child ($_);        $node->remove_child ($_);
7987      }      }
7988      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7989    
7990      ## Step 12 # MUST      ## Step 11 # MUST
7991      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7992      for (@cn) {      for (@cn) {
7993          $this_doc->adopt_node ($_);
7994        $node->append_child ($_);        $node->append_child ($_);
7995      }      }
7996      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
7997    
7998      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7999    
8000        delete $p->{parse_error}; # delete loop
8001    } else {    } else {
8002      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";
8003    }    }
# Line 4918  sub set_inner_html ($$$) { Line 8005  sub set_inner_html ($$$) {
8005    
8006  } # tree construction stage  } # tree construction stage
8007    
8008  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
8009    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  
8010    
8011  1;  1;
8012  # $Date$  # $Date$

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24