/[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.11 by wakaba, Sat Jun 23 03:53:35 2007 UTC revision 1.133 by wakaba, Sat May 17 04:54:11 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## This is an early version of an HTML parser.  ## ISSUE:
7    ## var doc = implementation.createDocument (null, null, null);
8    ## doc.write ('');
9    ## alert (doc.compatMode);
10    
11    ## TODO: 1252 parse error (revision 1264)
12    ## TODO: 8859-11 = 874 (revision 1271)
13    
14    my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
15    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
16    my $SVG_NS = q<http://www.w3.org/2000/svg>;
17    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
18    my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
19    my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
20    
21    sub A_EL () { 0b1 }
22    sub ADDRESS_EL () { 0b10 }
23    sub BODY_EL () { 0b100 }
24    sub BUTTON_EL () { 0b1000 }
25    sub CAPTION_EL () { 0b10000 }
26    sub DD_EL () { 0b100000 }
27    sub DIV_EL () { 0b1000000 }
28    sub DT_EL () { 0b10000000 }
29    sub FORM_EL () { 0b100000000 }
30    sub FORMATTING_EL () { 0b1000000000 }
31    sub FRAMESET_EL () { 0b10000000000 }
32    sub HEADING_EL () { 0b100000000000 }
33    sub HTML_EL () { 0b1000000000000 }
34    sub LI_EL () { 0b10000000000000 }
35    sub NOBR_EL () { 0b100000000000000 }
36    sub OPTION_EL () { 0b1000000000000000 }
37    sub OPTGROUP_EL () { 0b10000000000000000 }
38    sub P_EL () { 0b100000000000000000 }
39    sub SELECT_EL () { 0b1000000000000000000 }
40    sub TABLE_EL () { 0b10000000000000000000 }
41    sub TABLE_CELL_EL () { 0b100000000000000000000 }
42    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
43    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
44    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
45    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
46    sub FOREIGN_EL () { 0b10000000000000000000000000 }
47    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
48    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
49    
50    sub TABLE_ROWS_EL () {
51      TABLE_EL |
52      TABLE_ROW_EL |
53      TABLE_ROW_GROUP_EL
54    }
55    
56    sub END_TAG_OPTIONAL_EL () {
57      DD_EL |
58      DT_EL |
59      LI_EL |
60      P_EL
61    }
62    
63    sub ALL_END_TAG_OPTIONAL_EL () {
64      END_TAG_OPTIONAL_EL |
65      BODY_EL |
66      HTML_EL |
67      TABLE_CELL_EL |
68      TABLE_ROW_EL |
69      TABLE_ROW_GROUP_EL
70    }
71    
72    sub SCOPING_EL () {
73      BUTTON_EL |
74      CAPTION_EL |
75      HTML_EL |
76      TABLE_EL |
77      TABLE_CELL_EL |
78      MISC_SCOPING_EL
79    }
80    
81    sub TABLE_SCOPING_EL () {
82      HTML_EL |
83      TABLE_EL
84    }
85    
86    sub TABLE_ROWS_SCOPING_EL () {
87      HTML_EL |
88      TABLE_ROW_GROUP_EL
89    }
90    
91    sub TABLE_ROW_SCOPING_EL () {
92      HTML_EL |
93      TABLE_ROW_EL
94    }
95    
96    sub SPECIAL_EL () {
97      ADDRESS_EL |
98      BODY_EL |
99      DIV_EL |
100      END_TAG_OPTIONAL_EL |
101      FORM_EL |
102      FRAMESET_EL |
103      HEADING_EL |
104      OPTION_EL |
105      OPTGROUP_EL |
106      SELECT_EL |
107      TABLE_ROW_EL |
108      TABLE_ROW_GROUP_EL |
109      MISC_SPECIAL_EL
110    }
111    
112    my $el_category = {
113      a => A_EL | FORMATTING_EL,
114      address => ADDRESS_EL,
115      applet => MISC_SCOPING_EL,
116      area => MISC_SPECIAL_EL,
117      b => FORMATTING_EL,
118      base => MISC_SPECIAL_EL,
119      basefont => MISC_SPECIAL_EL,
120      bgsound => MISC_SPECIAL_EL,
121      big => FORMATTING_EL,
122      blockquote => MISC_SPECIAL_EL,
123      body => BODY_EL,
124      br => MISC_SPECIAL_EL,
125      button => BUTTON_EL,
126      caption => CAPTION_EL,
127      center => MISC_SPECIAL_EL,
128      col => MISC_SPECIAL_EL,
129      colgroup => MISC_SPECIAL_EL,
130      dd => DD_EL,
131      dir => MISC_SPECIAL_EL,
132      div => DIV_EL,
133      dl => MISC_SPECIAL_EL,
134      dt => DT_EL,
135      em => FORMATTING_EL,
136      embed => MISC_SPECIAL_EL,
137      fieldset => MISC_SPECIAL_EL,
138      font => FORMATTING_EL,
139      form => FORM_EL,
140      frame => MISC_SPECIAL_EL,
141      frameset => FRAMESET_EL,
142      h1 => HEADING_EL,
143      h2 => HEADING_EL,
144      h3 => HEADING_EL,
145      h4 => HEADING_EL,
146      h5 => HEADING_EL,
147      h6 => HEADING_EL,
148      head => MISC_SPECIAL_EL,
149      hr => MISC_SPECIAL_EL,
150      html => HTML_EL,
151      i => FORMATTING_EL,
152      iframe => MISC_SPECIAL_EL,
153      img => MISC_SPECIAL_EL,
154      input => MISC_SPECIAL_EL,
155      isindex => MISC_SPECIAL_EL,
156      li => LI_EL,
157      link => MISC_SPECIAL_EL,
158      listing => MISC_SPECIAL_EL,
159      marquee => MISC_SCOPING_EL,
160      menu => MISC_SPECIAL_EL,
161      meta => MISC_SPECIAL_EL,
162      nobr => NOBR_EL | FORMATTING_EL,
163      noembed => MISC_SPECIAL_EL,
164      noframes => MISC_SPECIAL_EL,
165      noscript => MISC_SPECIAL_EL,
166      object => MISC_SCOPING_EL,
167      ol => MISC_SPECIAL_EL,
168      optgroup => OPTGROUP_EL,
169      option => OPTION_EL,
170      p => P_EL,
171      param => MISC_SPECIAL_EL,
172      plaintext => MISC_SPECIAL_EL,
173      pre => MISC_SPECIAL_EL,
174      s => FORMATTING_EL,
175      script => MISC_SPECIAL_EL,
176      select => SELECT_EL,
177      small => FORMATTING_EL,
178      spacer => MISC_SPECIAL_EL,
179      strike => FORMATTING_EL,
180      strong => FORMATTING_EL,
181      style => MISC_SPECIAL_EL,
182      table => TABLE_EL,
183      tbody => TABLE_ROW_GROUP_EL,
184      td => TABLE_CELL_EL,
185      textarea => MISC_SPECIAL_EL,
186      tfoot => TABLE_ROW_GROUP_EL,
187      th => TABLE_CELL_EL,
188      thead => TABLE_ROW_GROUP_EL,
189      title => MISC_SPECIAL_EL,
190      tr => TABLE_ROW_EL,
191      tt => FORMATTING_EL,
192      u => FORMATTING_EL,
193      ul => MISC_SPECIAL_EL,
194      wbr => MISC_SPECIAL_EL,
195    };
196    
197    my $el_category_f = {
198      $MML_NS => {
199        'annotation-xml' => MML_AXML_EL,
200        mi => FOREIGN_FLOW_CONTENT_EL,
201        mo => FOREIGN_FLOW_CONTENT_EL,
202        mn => FOREIGN_FLOW_CONTENT_EL,
203        ms => FOREIGN_FLOW_CONTENT_EL,
204        mtext => FOREIGN_FLOW_CONTENT_EL,
205      },
206      $SVG_NS => {
207        foreignObject => FOREIGN_FLOW_CONTENT_EL,
208        desc => FOREIGN_FLOW_CONTENT_EL,
209        title => FOREIGN_FLOW_CONTENT_EL,
210      },
211      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
212    };
213    
214  my $permitted_slash_tag_name = {  my $svg_attr_name = {
215    base => 1,    attributetype => 'attributeType',
216    link => 1,    basefrequency => 'baseFrequency',
217    meta => 1,    baseprofile => 'baseProfile',
218    hr => 1,    calcmode => 'calcMode',
219    br => 1,    clippathunits => 'clipPathUnits',
220    img=> 1,    contentscripttype => 'contentScriptType',
221    embed => 1,    contentstyletype => 'contentStyleType',
222    param => 1,    diffuseconstant => 'diffuseConstant',
223    area => 1,    edgemode => 'edgeMode',
224    col => 1,    externalresourcesrequired => 'externalResourcesRequired',
225    input => 1,    fecolormatrix => 'feColorMatrix',
226      fecomposite => 'feComposite',
227      fegaussianblur => 'feGaussianBlur',
228      femorphology => 'feMorphology',
229      fetile => 'feTile',
230      filterres => 'filterRes',
231      filterunits => 'filterUnits',
232      glyphref => 'glyphRef',
233      gradienttransform => 'gradientTransform',
234      gradientunits => 'gradientUnits',
235      kernelmatrix => 'kernelMatrix',
236      kernelunitlength => 'kernelUnitLength',
237      keypoints => 'keyPoints',
238      keysplines => 'keySplines',
239      keytimes => 'keyTimes',
240      lengthadjust => 'lengthAdjust',
241      limitingconeangle => 'limitingConeAngle',
242      markerheight => 'markerHeight',
243      markerunits => 'markerUnits',
244      markerwidth => 'markerWidth',
245      maskcontentunits => 'maskContentUnits',
246      maskunits => 'maskUnits',
247      numoctaves => 'numOctaves',
248      pathlength => 'pathLength',
249      patterncontentunits => 'patternContentUnits',
250      patterntransform => 'patternTransform',
251      patternunits => 'patternUnits',
252      pointsatx => 'pointsAtX',
253      pointsaty => 'pointsAtY',
254      pointsatz => 'pointsAtZ',
255      preservealpha => 'preserveAlpha',
256      preserveaspectratio => 'preserveAspectRatio',
257      primitiveunits => 'primitiveUnits',
258      refx => 'refX',
259      refy => 'refY',
260      repeatcount => 'repeatCount',
261      repeatdur => 'repeatDur',
262      requiredextensions => 'requiredExtensions',
263      specularconstant => 'specularConstant',
264      specularexponent => 'specularExponent',
265      spreadmethod => 'spreadMethod',
266      startoffset => 'startOffset',
267      stddeviation => 'stdDeviation',
268      stitchtiles => 'stitchTiles',
269      surfacescale => 'surfaceScale',
270      systemlanguage => 'systemLanguage',
271      tablevalues => 'tableValues',
272      targetx => 'targetX',
273      targety => 'targetY',
274      textlength => 'textLength',
275      viewbox => 'viewBox',
276      viewtarget => 'viewTarget',
277      xchannelselector => 'xChannelSelector',
278      ychannelselector => 'yChannelSelector',
279      zoomandpan => 'zoomAndPan',
280  };  };
281    
282  my $entity_char = {  my $foreign_attr_xname = {
283    AElig => "\x{00C6}",    'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
284    Aacute => "\x{00C1}",    'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
285    Acirc => "\x{00C2}",    'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
286    Agrave => "\x{00C0}",    'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
287    Alpha => "\x{0391}",    'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
288    Aring => "\x{00C5}",    'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
289    Atilde => "\x{00C3}",    'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
290    Auml => "\x{00C4}",    'xml:base' => [$XML_NS, ['xml', 'base']],
291    Beta => "\x{0392}",    'xml:lang' => [$XML_NS, ['xml', 'lang']],
292    Ccedil => "\x{00C7}",    'xml:space' => [$XML_NS, ['xml', 'space']],
293    Chi => "\x{03A7}",    'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
294    Dagger => "\x{2021}",    'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
295    Delta => "\x{0394}",  };
296    ETH => "\x{00D0}",  
297    Eacute => "\x{00C9}",  ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
   Ecirc => "\x{00CA}",  
   Egrave => "\x{00C8}",  
   Epsilon => "\x{0395}",  
   Eta => "\x{0397}",  
   Euml => "\x{00CB}",  
   Gamma => "\x{0393}",  
   Iacute => "\x{00CD}",  
   Icirc => "\x{00CE}",  
   Igrave => "\x{00CC}",  
   Iota => "\x{0399}",  
   Iuml => "\x{00CF}",  
   Kappa => "\x{039A}",  
   Lambda => "\x{039B}",  
   Mu => "\x{039C}",  
   Ntilde => "\x{00D1}",  
   Nu => "\x{039D}",  
   OElig => "\x{0152}",  
   Oacute => "\x{00D3}",  
   Ocirc => "\x{00D4}",  
   Ograve => "\x{00D2}",  
   Omega => "\x{03A9}",  
   Omicron => "\x{039F}",  
   Oslash => "\x{00D8}",  
   Otilde => "\x{00D5}",  
   Ouml => "\x{00D6}",  
   Phi => "\x{03A6}",  
   Pi => "\x{03A0}",  
   Prime => "\x{2033}",  
   Psi => "\x{03A8}",  
   Rho => "\x{03A1}",  
   Scaron => "\x{0160}",  
   Sigma => "\x{03A3}",  
   THORN => "\x{00DE}",  
   Tau => "\x{03A4}",  
   Theta => "\x{0398}",  
   Uacute => "\x{00DA}",  
   Ucirc => "\x{00DB}",  
   Ugrave => "\x{00D9}",  
   Upsilon => "\x{03A5}",  
   Uuml => "\x{00DC}",  
   Xi => "\x{039E}",  
   Yacute => "\x{00DD}",  
   Yuml => "\x{0178}",  
   Zeta => "\x{0396}",  
   aacute => "\x{00E1}",  
   acirc => "\x{00E2}",  
   acute => "\x{00B4}",  
   aelig => "\x{00E6}",  
   agrave => "\x{00E0}",  
   alefsym => "\x{2135}",  
   alpha => "\x{03B1}",  
   amp => "\x{0026}",  
   AMP => "\x{0026}",  
   and => "\x{2227}",  
   ang => "\x{2220}",  
   apos => "\x{0027}",  
   aring => "\x{00E5}",  
   asymp => "\x{2248}",  
   atilde => "\x{00E3}",  
   auml => "\x{00E4}",  
   bdquo => "\x{201E}",  
   beta => "\x{03B2}",  
   brvbar => "\x{00A6}",  
   bull => "\x{2022}",  
   cap => "\x{2229}",  
   ccedil => "\x{00E7}",  
   cedil => "\x{00B8}",  
   cent => "\x{00A2}",  
   chi => "\x{03C7}",  
   circ => "\x{02C6}",  
   clubs => "\x{2663}",  
   cong => "\x{2245}",  
   copy => "\x{00A9}",  
   COPY => "\x{00A9}",  
   crarr => "\x{21B5}",  
   cup => "\x{222A}",  
   curren => "\x{00A4}",  
   dArr => "\x{21D3}",  
   dagger => "\x{2020}",  
   darr => "\x{2193}",  
   deg => "\x{00B0}",  
   delta => "\x{03B4}",  
   diams => "\x{2666}",  
   divide => "\x{00F7}",  
   eacute => "\x{00E9}",  
   ecirc => "\x{00EA}",  
   egrave => "\x{00E8}",  
   empty => "\x{2205}",  
   emsp => "\x{2003}",  
   ensp => "\x{2002}",  
   epsilon => "\x{03B5}",  
   equiv => "\x{2261}",  
   eta => "\x{03B7}",  
   eth => "\x{00F0}",  
   euml => "\x{00EB}",  
   euro => "\x{20AC}",  
   exist => "\x{2203}",  
   fnof => "\x{0192}",  
   forall => "\x{2200}",  
   frac12 => "\x{00BD}",  
   frac14 => "\x{00BC}",  
   frac34 => "\x{00BE}",  
   frasl => "\x{2044}",  
   gamma => "\x{03B3}",  
   ge => "\x{2265}",  
   gt => "\x{003E}",  
   GT => "\x{003E}",  
   hArr => "\x{21D4}",  
   harr => "\x{2194}",  
   hearts => "\x{2665}",  
   hellip => "\x{2026}",  
   iacute => "\x{00ED}",  
   icirc => "\x{00EE}",  
   iexcl => "\x{00A1}",  
   igrave => "\x{00EC}",  
   image => "\x{2111}",  
   infin => "\x{221E}",  
   int => "\x{222B}",  
   iota => "\x{03B9}",  
   iquest => "\x{00BF}",  
   isin => "\x{2208}",  
   iuml => "\x{00EF}",  
   kappa => "\x{03BA}",  
   lArr => "\x{21D0}",  
   lambda => "\x{03BB}",  
   lang => "\x{2329}",  
   laquo => "\x{00AB}",  
   larr => "\x{2190}",  
   lceil => "\x{2308}",  
   ldquo => "\x{201C}",  
   le => "\x{2264}",  
   lfloor => "\x{230A}",  
   lowast => "\x{2217}",  
   loz => "\x{25CA}",  
   lrm => "\x{200E}",  
   lsaquo => "\x{2039}",  
   lsquo => "\x{2018}",  
   lt => "\x{003C}",  
   LT => "\x{003C}",  
   macr => "\x{00AF}",  
   mdash => "\x{2014}",  
   micro => "\x{00B5}",  
   middot => "\x{00B7}",  
   minus => "\x{2212}",  
   mu => "\x{03BC}",  
   nabla => "\x{2207}",  
   nbsp => "\x{00A0}",  
   ndash => "\x{2013}",  
   ne => "\x{2260}",  
   ni => "\x{220B}",  
   not => "\x{00AC}",  
   notin => "\x{2209}",  
   nsub => "\x{2284}",  
   ntilde => "\x{00F1}",  
   nu => "\x{03BD}",  
   oacute => "\x{00F3}",  
   ocirc => "\x{00F4}",  
   oelig => "\x{0153}",  
   ograve => "\x{00F2}",  
   oline => "\x{203E}",  
   omega => "\x{03C9}",  
   omicron => "\x{03BF}",  
   oplus => "\x{2295}",  
   or => "\x{2228}",  
   ordf => "\x{00AA}",  
   ordm => "\x{00BA}",  
   oslash => "\x{00F8}",  
   otilde => "\x{00F5}",  
   otimes => "\x{2297}",  
   ouml => "\x{00F6}",  
   para => "\x{00B6}",  
   part => "\x{2202}",  
   permil => "\x{2030}",  
   perp => "\x{22A5}",  
   phi => "\x{03C6}",  
   pi => "\x{03C0}",  
   piv => "\x{03D6}",  
   plusmn => "\x{00B1}",  
   pound => "\x{00A3}",  
   prime => "\x{2032}",  
   prod => "\x{220F}",  
   prop => "\x{221D}",  
   psi => "\x{03C8}",  
   quot => "\x{0022}",  
   QUOT => "\x{0022}",  
   rArr => "\x{21D2}",  
   radic => "\x{221A}",  
   rang => "\x{232A}",  
   raquo => "\x{00BB}",  
   rarr => "\x{2192}",  
   rceil => "\x{2309}",  
   rdquo => "\x{201D}",  
   real => "\x{211C}",  
   reg => "\x{00AE}",  
   REG => "\x{00AE}",  
   rfloor => "\x{230B}",  
   rho => "\x{03C1}",  
   rlm => "\x{200F}",  
   rsaquo => "\x{203A}",  
   rsquo => "\x{2019}",  
   sbquo => "\x{201A}",  
   scaron => "\x{0161}",  
   sdot => "\x{22C5}",  
   sect => "\x{00A7}",  
   shy => "\x{00AD}",  
   sigma => "\x{03C3}",  
   sigmaf => "\x{03C2}",  
   sim => "\x{223C}",  
   spades => "\x{2660}",  
   sub => "\x{2282}",  
   sube => "\x{2286}",  
   sum => "\x{2211}",  
   sup => "\x{2283}",  
   sup1 => "\x{00B9}",  
   sup2 => "\x{00B2}",  
   sup3 => "\x{00B3}",  
   supe => "\x{2287}",  
   szlig => "\x{00DF}",  
   tau => "\x{03C4}",  
   there4 => "\x{2234}",  
   theta => "\x{03B8}",  
   thetasym => "\x{03D1}",  
   thinsp => "\x{2009}",  
   thorn => "\x{00FE}",  
   tilde => "\x{02DC}",  
   times => "\x{00D7}",  
   trade => "\x{2122}",  
   uArr => "\x{21D1}",  
   uacute => "\x{00FA}",  
   uarr => "\x{2191}",  
   ucirc => "\x{00FB}",  
   ugrave => "\x{00F9}",  
   uml => "\x{00A8}",  
   upsih => "\x{03D2}",  
   upsilon => "\x{03C5}",  
   uuml => "\x{00FC}",  
   weierp => "\x{2118}",  
   xi => "\x{03BE}",  
   yacute => "\x{00FD}",  
   yen => "\x{00A5}",  
   yuml => "\x{00FF}",  
   zeta => "\x{03B6}",  
   zwj => "\x{200D}",  
   zwnj => "\x{200C}",  
 }; # $entity_char  
298    
299  my $c1_entity_char = {  my $c1_entity_char = {
300    0x80 => 0x20AC,    0x80 => 0x20AC,
# Line 315  my $c1_entity_char = { Line 331  my $c1_entity_char = {
331    0x9F => 0x0178,    0x9F => 0x0178,
332  }; # $c1_entity_char  }; # $c1_entity_char
333    
334  my $special_category = {  sub parse_byte_string ($$$$;$) {
335    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = ref $_[0] ? shift : shift->new;
336    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
337    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
338    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    my $s;
339    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
340    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,    ## HTML5 encoding sniffing algorithm
341    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,    require Message::Charset::Info;
342    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    my $charset;
343    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my ($e, $e_status);
344    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
345  };    SNIFFING: {
346  my $scoping_category = {  
347    button => 1, caption => 1, html => 1, marquee => 1, object => 1,      ## Step 1
348    table => 1, td => 1, th => 1,      if (defined $charset_name) {
349  };        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
350  my $formatting_category = {  
351    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,        ## ISSUE: Unsupported encoding is not ignored according to the spec.
352    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,        ($e, $e_status) = $charset->get_perl_encoding
353  };            (allow_error_reporting => 1,
354  # $phrasing_category: all other elements             allow_fallback => 1);
355          if ($e) {
356            $self->{confident} = 1;
357            last SNIFFING;
358          }
359        }
360    
361        ## Step 2
362        # wait
363    
364        ## Step 3
365        my $head = substr ($$bytes_s, 0, 3);
366        if ($head =~ /^\xFE\xFF/) {
367          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
368          ($e, $e_status) = $charset->get_perl_encoding
369              (allow_error_reporting => 1,
370               allow_fallback => 1);
371          $self->{confident} = 1;
372          last SNIFFING;
373        } elsif ($head =~ /^\xFF\xFE/) {
374          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
375          ($e, $e_status) = $charset->get_perl_encoding
376              (allow_error_reporting => 1,
377               allow_fallback => 1);
378          $self->{confident} = 1;
379          last SNIFFING;
380        } elsif ($head eq "\xEF\xBB\xBF") {
381          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
382          ($e, $e_status) = $charset->get_perl_encoding
383              (allow_error_reporting => 1,
384               allow_fallback => 1);
385          $self->{confident} = 1;
386          last SNIFFING;
387        }
388    
389        ## Step 4
390        ## TODO: <meta charset>
391    
392        ## Step 5
393        ## TODO: from history
394    
395        ## Step 6
396        require Whatpm::Charset::UniversalCharDet;
397        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
398            (substr ($$bytes_s, 0, 1024));
399        if (defined $charset_name) {
400          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
401    
402          ## ISSUE: Unsupported encoding is not ignored according to the spec.
403          ($e, $e_status) = $charset->get_perl_encoding
404              (allow_error_reporting => 1,
405               allow_fallback => 1);
406          if ($e) {
407            $self->{confident} = 0;
408            last SNIFFING;
409          }
410        }
411    
412        ## Step 7: default
413        ## TODO: Make this configurable.
414        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
415            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
416            ## detectable in the step 6.
417        ($e, $e_status) = $charset->get_perl_encoding (allow_error_reporting => 1,
418                                                       allow_fallback => 1);
419        $self->{confident} = 0;
420      } # SNIFFING
421    
422      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
423        
424      } elsif (not ($e_status &
425                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
426        
427      }
428      $s = \ $e->decode ($$bytes_s);
429      $self->{input_encoding} = $charset->get_iana_name;
430    
431      $self->{change_encoding} = sub {
432        my $self = shift;
433        my $charset_name = lc shift;
434        my $token = shift;
435        ## TODO: if $charset_name is supported
436        ## TODO: normalize charset name
437    
438        ## "Change the encoding" algorithm:
439    
440        ## Step 1    
441        if ($charset_name eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
442          $charset_name = 'utf-8';
443        }
444    
445        ## Step 2
446        if (defined $self->{input_encoding} and
447            $self->{input_encoding} eq $charset_name) {
448          $self->{confident} = 1;
449          return;
450        }
451    
452        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
453            ':'.$charset_name, level => 'w', token => $token);
454    
455        ## Step 3
456        # if (can) {
457          ## change the encoding on the fly.
458          #$self->{confident} = 1;
459          #return;
460        # }
461    
462        ## Step 4
463        throw Whatpm::HTML::RestartParser (charset => $charset_name);
464      }; # $self->{change_encoding}
465    
466      my @args = @_; shift @args; # $s
467      my $return;
468      try {
469        $return = $self->parse_char_string ($s, @args);  
470      } catch Whatpm::HTML::RestartParser with {
471        my $charset_name = shift->{charset};
472        $s = \ (Encode::decode ($charset_name, $$bytes_s));    
473        $self->{input_encoding} = $charset_name; ## TODO: normalize
474        $self->{confident} = 1;
475        $return = $self->parse_char_string ($s, @args);
476      };
477      return $return;
478    } # parse_byte_string
479    
480    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
481    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
482    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
483    ## because the core part of our HTML parser expects a string of character,
484    ## not a string of bytes or code units or anything which might contain a BOM.
485    ## Therefore, any parser interface that accepts a string of bytes,
486    ## such as |parse_byte_string| in this module, must ensure that it does
487    ## strip the BOM and never strip any ZWNBSP.
488    
489    *parse_char_string = \&parse_string;
490    
491  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
492    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
493    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
494    $self->{document} = $_[1];    $self->{document} = $_[1];
495      @{$self->{document}->child_nodes} = ();
496    
497    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
498    
499      $self->{confident} = 1 unless exists $self->{confident};
500      $self->{document}->input_encoding ($self->{input_encoding})
501          if defined $self->{input_encoding};
502    
503    my $i = 0;    my $i = 0;
504    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
505    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
506    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
507      my $self = shift;      my $self = shift;
508      $self->{next_input_character} = -1 and return if $i >= length $$s;  
509      $self->{next_input_character} = ord substr $$s, $i++, 1;      pop @{$self->{prev_char}};
510      $column++;      unshift @{$self->{prev_char}}, $self->{next_char};
511    
512        $self->{next_char} = -1 and return if $i >= length $$s;
513        $self->{next_char} = ord substr $$s, $i++, 1;
514    
515        ($self->{line_prev}, $self->{column_prev})
516            = ($self->{line}, $self->{column});
517        $self->{column}++;
518            
519      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
520        $line++;        !!!cp ('j1');
521        $column = 0;        $self->{line}++;
522      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
523        if ($i >= length $$s) {      } elsif ($self->{next_char} == 0x000D) { # CR
524          #        !!!cp ('j2');
525        } else {        $i++ if substr ($$s, $i, 1) eq "\x0A";
526          my $next_char = ord substr $$s, $i++, 1;        $self->{next_char} = 0x000A; # LF # MUST
527          if ($next_char == 0x000A) { # LF        $self->{line}++;
528            #        $self->{column} = 0;
529          } else {      } elsif ($self->{next_char} > 0x10FFFF) {
530            push @{$self->{char}}, $next_char;        !!!cp ('j3');
531          }        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
532        }      } elsif ($self->{next_char} == 0x0000) { # NULL
533        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j4');
       $line++;  
       $column = 0;  
     } elsif ($self->{next_input_character} > 0x10FFFF) {  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
     } elsif ($self->{next_input_character} == 0x0000) { # NULL  
534        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
535        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
536        } elsif ($self->{next_char} <= 0x0008 or
537                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
538                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
539                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
540                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
541                 {
542                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
543                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
544                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
545                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
546                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
547                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
548                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
549                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
550                  0x10FFFE => 1, 0x10FFFF => 1,
551                 }->{$self->{next_char}}) {
552          !!!cp ('j5');
553          !!!parse-error (type => 'control char', level => $self->{must_level});
554    ## TODO: error type documentation
555      }      }
556    };    };
557      $self->{prev_char} = [-1, -1, -1];
558      $self->{next_char} = -1;
559    
560    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
561      my (%opt) = @_;      my (%opt) = @_;
562      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
563        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
564        warn "Parse error ($opt{type}) at line $line column $column\n";
565    };    };
566    $self->{parse_error} = sub {    $self->{parse_error} = sub {
567      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
568    };    };
569    
570    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 391  sub parse_string ($$$;$) { Line 572  sub parse_string ($$$;$) {
572    $self->_construct_tree;    $self->_construct_tree;
573    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
574    
575      delete $self->{parse_error}; # remove loop
576    
577    return $self->{document};    return $self->{document};
578  } # parse_string  } # parse_string
579    
580  sub new ($) {  sub new ($) {
581    my $class = shift;    my $class = shift;
582    my $self = bless {}, $class;    my $self = bless {}, $class;
583    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
584      $self->{next_input_character} = -1;      $self->{next_char} = -1;
585    };    };
586    $self->{parse_error} = sub {    $self->{parse_error} = sub {
587      #      #
588    };    };
589      $self->{change_encoding} = sub {
590        # if ($_[0] is a supported encoding) {
591        #   run "change the encoding" algorithm;
592        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
593        # }
594      };
595      $self->{application_cache_selection} = sub {
596        #
597      };
598    return $self;    return $self;
599  } # new  } # new
600    
601    sub CM_ENTITY () { 0b001 } # & markup in data
602    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
603    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
604    
605    sub PLAINTEXT_CONTENT_MODEL () { 0 }
606    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
607    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
608    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
609    
610    sub DATA_STATE () { 0 }
611    sub ENTITY_DATA_STATE () { 1 }
612    sub TAG_OPEN_STATE () { 2 }
613    sub CLOSE_TAG_OPEN_STATE () { 3 }
614    sub TAG_NAME_STATE () { 4 }
615    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
616    sub ATTRIBUTE_NAME_STATE () { 6 }
617    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
618    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
619    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
620    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
621    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
622    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
623    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
624    sub COMMENT_START_STATE () { 14 }
625    sub COMMENT_START_DASH_STATE () { 15 }
626    sub COMMENT_STATE () { 16 }
627    sub COMMENT_END_STATE () { 17 }
628    sub COMMENT_END_DASH_STATE () { 18 }
629    sub BOGUS_COMMENT_STATE () { 19 }
630    sub DOCTYPE_STATE () { 20 }
631    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
632    sub DOCTYPE_NAME_STATE () { 22 }
633    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
634    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
635    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
636    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
637    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
638    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
639    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
640    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
641    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
642    sub BOGUS_DOCTYPE_STATE () { 32 }
643    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
644    sub SELF_CLOSING_START_TAG_STATE () { 34 }
645    sub CDATA_BLOCK_STATE () { 35 }
646    
647    sub DOCTYPE_TOKEN () { 1 }
648    sub COMMENT_TOKEN () { 2 }
649    sub START_TAG_TOKEN () { 3 }
650    sub END_TAG_TOKEN () { 4 }
651    sub END_OF_FILE_TOKEN () { 5 }
652    sub CHARACTER_TOKEN () { 6 }
653    
654    sub AFTER_HTML_IMS () { 0b100 }
655    sub HEAD_IMS ()       { 0b1000 }
656    sub BODY_IMS ()       { 0b10000 }
657    sub BODY_TABLE_IMS () { 0b100000 }
658    sub TABLE_IMS ()      { 0b1000000 }
659    sub ROW_IMS ()        { 0b10000000 }
660    sub BODY_AFTER_IMS () { 0b100000000 }
661    sub FRAME_IMS ()      { 0b1000000000 }
662    sub SELECT_IMS ()     { 0b10000000000 }
663    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
664        ## NOTE: "in foreign content" insertion mode is special; it is combined
665        ## with the secondary insertion mode.  In this parser, they are stored
666        ## together in the bit-or'ed form.
667    
668    ## NOTE: "initial" and "before html" insertion modes have no constants.
669    
670    ## NOTE: "after after body" insertion mode.
671    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
672    
673    ## NOTE: "after after frameset" insertion mode.
674    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
675    
676    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
677    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
678    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
679    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
680    sub IN_BODY_IM () { BODY_IMS }
681    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
682    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
683    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
684    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
685    sub IN_TABLE_IM () { TABLE_IMS }
686    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
687    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
688    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
689    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
690    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
691    sub IN_COLUMN_GROUP_IM () { 0b10 }
692    
693  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
694    
695  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
696    my $self = shift;    my $self = shift;
697    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
698    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
699    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
700    undef $self->{current_attribute};    undef $self->{current_attribute};
701    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
702    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
703      delete $self->{self_closing};
704    $self->{char} = [];    $self->{char} = [];
705    # $self->{next_input_character}    # $self->{next_char}
706    !!!next-input-character;    !!!next-input-character;
707    $self->{token} = [];    $self->{token} = [];
708      # $self->{escape}
709  } # _initialize_tokenizer  } # _initialize_tokenizer
710    
711  ## A token has:  ## A token has:
712  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
713  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
714  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
715      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
716  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
717  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
718  ##   ->{data} (comment, character)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
719    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
720  ## Macros  ##        ->{name}
721  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
722  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
723  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
724    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
725    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
726    ##     while the token is pushed back to the stack.
727    
728    ## ISSUE: "When a DOCTYPE token is created, its
729    ## <i>self-closing flag</i> must be unset (its other state is that it
730    ## be set), and its attributes list must be empty.": Wrong subject?
731    
732  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
733    
# Line 444  sub _initialize_tokenizer ($) { Line 737  sub _initialize_tokenizer ($) {
737  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
738  ## and removed from the list.  ## and removed from the list.
739    
740    ## NOTE: HTML5 "Writing HTML documents" section, applied to
741    ## documents and not to user agents and conformance checkers,
742    ## contains some requirements that are not detected by the
743    ## parsing algorithm:
744    ## - Some requirements on character encoding declarations. ## TODO
745    ## - "Elements MUST NOT contain content that their content model disallows."
746    ##   ... Some are parse error, some are not (will be reported by c.c.).
747    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
748    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
749    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
750    
751    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
752    ## be detected by the HTML5 parsing algorithm:
753    ## - Text,
754    
755  sub _get_next_token ($) {  sub _get_next_token ($) {
756    my $self = shift;    my $self = shift;
757    
758      if ($self->{self_closing}) {
759        !!!parse-error (type => 'nestc', token => $self->{current_token});
760        ## NOTE: The |self_closing| flag is only set by start tag token.
761        ## In addition, when a start tag token is emitted, it is always set to
762        ## |current_token|.
763        delete $self->{self_closing};
764      }
765    
766    if (@{$self->{token}}) {    if (@{$self->{token}}) {
767        $self->{self_closing} = $self->{token}->[0]->{self_closing};
768      return shift @{$self->{token}};      return shift @{$self->{token}};
769    }    }
770    
771    A: {    A: {
772      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
773        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
774          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
775              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
776            $self->{state} = 'entity data';            !!!cp (1);
777              $self->{state} = ENTITY_DATA_STATE;
778            !!!next-input-character;            !!!next-input-character;
779            redo A;            redo A;
780          } else {          } else {
781              !!!cp (2);
782            #            #
783          }          }
784        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x002D) { # -
785          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
786            $self->{state} = 'tag open';            unless ($self->{escape}) {
787                if ($self->{prev_char}->[0] == 0x002D and # -
788                    $self->{prev_char}->[1] == 0x0021 and # !
789                    $self->{prev_char}->[2] == 0x003C) { # <
790                  !!!cp (3);
791                  $self->{escape} = 1;
792                } else {
793                  !!!cp (4);
794                }
795              } else {
796                !!!cp (5);
797              }
798            }
799            
800            #
801          } elsif ($self->{next_char} == 0x003C) { # <
802            if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
803                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
804                 not $self->{escape})) {
805              !!!cp (6);
806              $self->{state} = TAG_OPEN_STATE;
807            !!!next-input-character;            !!!next-input-character;
808            redo A;            redo A;
809          } else {          } else {
810              !!!cp (7);
811            #            #
812          }          }
813        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
814          !!!emit ({type => 'end-of-file'});          if ($self->{escape} and
815                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
816              if ($self->{prev_char}->[0] == 0x002D and # -
817                  $self->{prev_char}->[1] == 0x002D) { # -
818                !!!cp (8);
819                delete $self->{escape};
820              } else {
821                !!!cp (9);
822              }
823            } else {
824              !!!cp (10);
825            }
826            
827            #
828          } elsif ($self->{next_char} == -1) {
829            !!!cp (11);
830            !!!emit ({type => END_OF_FILE_TOKEN,
831                      line => $self->{line}, column => $self->{column}});
832          last A; ## TODO: ok?          last A; ## TODO: ok?
833          } else {
834            !!!cp (12);
835        }        }
836        # Anything else        # Anything else
837        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
838                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
839                       line => $self->{line}, column => $self->{column},
840                      };
841        ## Stay in the data state        ## Stay in the data state
842        !!!next-input-character;        !!!next-input-character;
843    
844        !!!emit ($token);        !!!emit ($token);
845    
846        redo A;        redo A;
847      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
848        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
849    
850          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
851                
852        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
853    
854        $self->{state} = 'data';        $self->{state} = DATA_STATE;
855        # next-input-character is already done        # next-input-character is already done
856    
857        unless (defined $token) {        unless (defined $token) {
858          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
859            !!!emit ({type => CHARACTER_TOKEN, data => '&',
860                      line => $l, column => $c,
861                     });
862        } else {        } else {
863            !!!cp (14);
864          !!!emit ($token);          !!!emit ($token);
865        }        }
866    
867        redo A;        redo A;
868      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
869        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
870            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
871          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
872            !!!next-input-character;            !!!next-input-character;
873            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
874            redo A;            redo A;
875          } else {          } else {
876              !!!cp (16);
877            ## reconsume            ## reconsume
878            $self->{state} = 'data';            $self->{state} = DATA_STATE;
879    
880            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
881                        line => $self->{line_prev},
882                        column => $self->{column_prev},
883                       });
884    
885            redo A;            redo A;
886          }          }
887        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
888          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
889            $self->{state} = 'markup declaration open';            !!!cp (17);
890              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
891            !!!next-input-character;            !!!next-input-character;
892            redo A;            redo A;
893          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
894            $self->{state} = 'close tag open';            !!!cp (18);
895              $self->{state} = CLOSE_TAG_OPEN_STATE;
896            !!!next-input-character;            !!!next-input-character;
897            redo A;            redo A;
898          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
899                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
900              !!!cp (19);
901            $self->{current_token}            $self->{current_token}
902              = {type => 'start tag',              = {type => START_TAG_TOKEN,
903                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
904            $self->{state} = 'tag name';                 line => $self->{line_prev},
905                   column => $self->{column_prev}};
906              $self->{state} = TAG_NAME_STATE;
907            !!!next-input-character;            !!!next-input-character;
908            redo A;            redo A;
909          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
910                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
911            $self->{current_token} = {type => 'start tag',            !!!cp (20);
912                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
913            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
914                                        line => $self->{line_prev},
915                                        column => $self->{column_prev}};
916              $self->{state} = TAG_NAME_STATE;
917            !!!next-input-character;            !!!next-input-character;
918            redo A;            redo A;
919          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
920            !!!parse-error (type => 'empty start tag');            !!!cp (21);
921            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
922                              line => $self->{line_prev},
923                              column => $self->{column_prev});
924              $self->{state} = DATA_STATE;
925            !!!next-input-character;            !!!next-input-character;
926    
927            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
928                        line => $self->{line_prev},
929                        column => $self->{column_prev},
930                       });
931    
932            redo A;            redo A;
933          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
934            !!!parse-error (type => 'pio');            !!!cp (22);
935            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
936            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
937                              column => $self->{column_prev});
938              $self->{state} = BOGUS_COMMENT_STATE;
939              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
940                                        line => $self->{line_prev},
941                                        column => $self->{column_prev},
942                                       };
943              ## $self->{next_char} is intentionally left as is
944            redo A;            redo A;
945          } else {          } else {
946              !!!cp (23);
947            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
948            $self->{state} = 'data';            $self->{state} = DATA_STATE;
949            ## reconsume            ## reconsume
950    
951            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
952                        line => $self->{line_prev},
953                        column => $self->{column_prev},
954                       });
955    
956            redo A;            redo A;
957          }          }
958        } else {        } else {
959          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
960        }        }
961      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
962        if ($self->{content_model_flag} eq 'RCDATA' or        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
963            $self->{content_model_flag} eq 'CDATA') {        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
964          my @next_char;          if (defined $self->{last_emitted_start_tag_name}) {
965          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
966            push @next_char, $self->{next_input_character};            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
967            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);            my @next_char;
968            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
969            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              push @next_char, $self->{next_char};
970              !!!next-input-character;              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
971              next TAGNAME;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
972            } else {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
973              !!!parse-error (type => 'unmatched end tag');                !!!cp (24);
974              $self->{next_input_character} = shift @next_char; # reconsume                !!!next-input-character;
975                  next TAGNAME;
976                } else {
977                  !!!cp (25);
978                  $self->{next_char} = shift @next_char; # reconsume
979                  !!!back-next-input-character (@next_char);
980                  $self->{state} = DATA_STATE;
981    
982                  !!!emit ({type => CHARACTER_TOKEN, data => '</',
983                            line => $l, column => $c,
984                           });
985      
986                  redo A;
987                }
988              }
989              push @next_char, $self->{next_char};
990          
991              unless ($self->{next_char} == 0x0009 or # HT
992                      $self->{next_char} == 0x000A or # LF
993                      $self->{next_char} == 0x000B or # VT
994                      $self->{next_char} == 0x000C or # FF
995                      $self->{next_char} == 0x0020 or # SP
996                      $self->{next_char} == 0x003E or # >
997                      $self->{next_char} == 0x002F or # /
998                      $self->{next_char} == -1) {
999                !!!cp (26);
1000                $self->{next_char} = shift @next_char; # reconsume
1001              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1002              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1003                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1004              !!!emit ({type => 'character', data => '</'});                        line => $l, column => $c,
1005                         });
1006              redo A;              redo A;
1007              } else {
1008                !!!cp (27);
1009                $self->{next_char} = shift @next_char;
1010                !!!back-next-input-character (@next_char);
1011                # and consume...
1012            }            }
         }  
         push @next_char, $self->{next_input_character};  
       
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == 0x003C or # <  
                 $self->{next_input_character} == -1) {  
           !!!parse-error (type => 'unmatched end tag');  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
           $self->{state} = 'data';  
   
           !!!emit ({type => 'character', data => '</'});  
   
           redo A;  
1013          } else {          } else {
1014            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
1015            !!!back-next-input-character (@next_char);            !!!cp (28);
1016            # and consume...            # next-input-character is already done
1017              $self->{state} = DATA_STATE;
1018              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1019                        line => $l, column => $c,
1020                       });
1021              redo A;
1022          }          }
1023        }        }
1024                
1025        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1026            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1027          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1028                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1029          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1030          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1031          redo A;                 line => $l, column => $c};
1032        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1033                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1034          $self->{current_token} = {type => 'end tag',          redo A;
1035                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1036          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1037          !!!next-input-character;          !!!cp (30);
1038          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1039        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1040          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1041          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1042            !!!next-input-character;
1043            redo A;
1044          } elsif ($self->{next_char} == 0x003E) { # >
1045            !!!cp (31);
1046            !!!parse-error (type => 'empty end tag',
1047                            line => $self->{line_prev}, ## "<" in "</>"
1048                            column => $self->{column_prev} - 1);
1049            $self->{state} = DATA_STATE;
1050          !!!next-input-character;          !!!next-input-character;
1051          redo A;          redo A;
1052        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1053            !!!cp (32);
1054          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1055          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1056          # reconsume          # reconsume
1057    
1058          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1059                      line => $l, column => $c,
1060                     });
1061    
1062          redo A;          redo A;
1063        } else {        } else {
1064            !!!cp (33);
1065          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1066          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1067          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1068          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1069        }                                    column => $self->{column_prev} - 1,
1070      } elsif ($self->{state} eq 'tag name') {                                   };
1071        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1072            $self->{next_input_character} == 0x000A or # LF          redo A;
1073            $self->{next_input_character} == 0x000B or # VT        }
1074            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1075            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1076          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1077          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1078          redo A;            $self->{next_char} == 0x000C or # FF
1079        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1080          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1081            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1082            !!!next-input-character;
1083            redo A;
1084          } elsif ($self->{next_char} == 0x003E) { # >
1085            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1086              !!!cp (35);
1087            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1088          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1089            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1090            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1091              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1092            }            #  !!! cp (36);
1093              #  !!! parse-error (type => 'end tag attribute');
1094              #} else {
1095                !!!cp (37);
1096              #}
1097          } else {          } else {
1098            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1099          }          }
1100          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1101          !!!next-input-character;          !!!next-input-character;
1102    
1103          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1104    
1105          redo A;          redo A;
1106        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1107                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1108          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1109            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1110            # start tag or end tag            # start tag or end tag
1111          ## Stay in this state          ## Stay in this state
1112          !!!next-input-character;          !!!next-input-character;
1113          redo A;          redo A;
1114        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1115          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1116          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1117              !!!cp (39);
1118            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1119          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1120            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1121            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1122              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1123            }            #  !!! cp (40);
1124              #  !!! parse-error (type => 'end tag attribute');
1125              #} else {
1126                !!!cp (41);
1127              #}
1128          } else {          } else {
1129            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1130          }          }
1131          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1132          # reconsume          # reconsume
1133    
1134          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1135    
1136          redo A;          redo A;
1137        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1138            !!!cp (42);
1139            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1140          !!!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  
1141          redo A;          redo A;
1142        } else {        } else {
1143          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1144            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1145            # start tag or end tag            # start tag or end tag
1146          ## Stay in the state          ## Stay in the state
1147          !!!next-input-character;          !!!next-input-character;
1148          redo A;          redo A;
1149        }        }
1150      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1151        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1152            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1153            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1154            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1155            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1156            !!!cp (45);
1157          ## Stay in the state          ## Stay in the state
1158          !!!next-input-character;          !!!next-input-character;
1159          redo A;          redo A;
1160        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1161          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1162              !!!cp (46);
1163            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1164          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1165            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1166            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1167                !!!cp (47);
1168              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1169              } else {
1170                !!!cp (48);
1171            }            }
1172          } else {          } else {
1173            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1174          }          }
1175          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1176          !!!next-input-character;          !!!next-input-character;
1177    
1178          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1179    
1180          redo A;          redo A;
1181        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1182                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1183          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1184                                value => ''};          $self->{current_attribute}
1185          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1186                   value => '',
1187                   line => $self->{line}, column => $self->{column}};
1188            $self->{state} = ATTRIBUTE_NAME_STATE;
1189            !!!next-input-character;
1190            redo A;
1191          } elsif ($self->{next_char} == 0x002F) { # /
1192            !!!cp (50);
1193            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1194          !!!next-input-character;          !!!next-input-character;
1195          redo A;          redo A;
1196        } 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) {  
1197          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1198          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1199              !!!cp (52);
1200            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1201          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1202            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1203            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1204                !!!cp (53);
1205              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1206              } else {
1207                !!!cp (54);
1208            }            }
1209          } else {          } else {
1210            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1211          }          }
1212          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1213          # reconsume          # reconsume
1214    
1215          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1216    
1217          redo A;          redo A;
1218        } else {        } else {
1219          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1220                                value => ''};               0x0022 => 1, # "
1221          $self->{state} = 'attribute name';               0x0027 => 1, # '
1222                 0x003D => 1, # =
1223                }->{$self->{next_char}}) {
1224              !!!cp (55);
1225              !!!parse-error (type => 'bad attribute name');
1226            } else {
1227              !!!cp (56);
1228            }
1229            $self->{current_attribute}
1230                = {name => chr ($self->{next_char}),
1231                   value => '',
1232                   line => $self->{line}, column => $self->{column}};
1233            $self->{state} = ATTRIBUTE_NAME_STATE;
1234          !!!next-input-character;          !!!next-input-character;
1235          redo A;          redo A;
1236        }        }
1237      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1238        my $before_leave = sub {        my $before_leave = sub {
1239          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1240              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1241            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1242              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1243            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1244          } else {          } else {
1245              !!!cp (58);
1246            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1247              = $self->{current_attribute};              = $self->{current_attribute};
1248          }          }
1249        }; # $before_leave        }; # $before_leave
1250    
1251        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1252            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1253            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1254            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1255            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1256            !!!cp (59);
1257          $before_leave->();          $before_leave->();
1258          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1259          !!!next-input-character;          !!!next-input-character;
1260          redo A;          redo A;
1261        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1262            !!!cp (60);
1263          $before_leave->();          $before_leave->();
1264          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1265          !!!next-input-character;          !!!next-input-character;
1266          redo A;          redo A;
1267        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1268          $before_leave->();          $before_leave->();
1269          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1270              !!!cp (61);
1271            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1272          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1273            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
1274              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1275            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1276              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1277            }            }
1278          } else {          } else {
1279            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1280          }          }
1281          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1282          !!!next-input-character;          !!!next-input-character;
1283    
1284          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1285    
1286          redo A;          redo A;
1287        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1288                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1289          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1290            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1291          ## Stay in the state          ## Stay in the state
1292          !!!next-input-character;          !!!next-input-character;
1293          redo A;          redo A;
1294        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1295            !!!cp (64);
1296          $before_leave->();          $before_leave->();
1297            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1298          !!!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  
1299          redo A;          redo A;
1300        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1301          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1302          $before_leave->();          $before_leave->();
1303          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1304              !!!cp (66);
1305            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1306          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1307            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1308            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1309                !!!cp (67);
1310              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1311              } else {
1312                ## NOTE: This state should never be reached.
1313                !!!cp (68);
1314            }            }
1315          } else {          } else {
1316            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1317          }          }
1318          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1319          # reconsume          # reconsume
1320    
1321          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1322    
1323          redo A;          redo A;
1324        } else {        } else {
1325          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1326                $self->{next_char} == 0x0027) { # '
1327              !!!cp (69);
1328              !!!parse-error (type => 'bad attribute name');
1329            } else {
1330              !!!cp (70);
1331            }
1332            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1333          ## Stay in the state          ## Stay in the state
1334          !!!next-input-character;          !!!next-input-character;
1335          redo A;          redo A;
1336        }        }
1337      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1338        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1339            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1340            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1341            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1342            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1343            !!!cp (71);
1344          ## Stay in the state          ## Stay in the state
1345          !!!next-input-character;          !!!next-input-character;
1346          redo A;          redo A;
1347        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1348          $self->{state} = 'before attribute value';          !!!cp (72);
1349            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1350          !!!next-input-character;          !!!next-input-character;
1351          redo A;          redo A;
1352        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1353          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1354              !!!cp (73);
1355            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1356          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1357            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1358            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1359                !!!cp (74);
1360              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1361              } else {
1362                ## NOTE: This state should never be reached.
1363                !!!cp (75);
1364            }            }
1365          } else {          } else {
1366            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1367          }          }
1368          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1369          !!!next-input-character;          !!!next-input-character;
1370    
1371          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1372    
1373          redo A;          redo A;
1374        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1375                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1376          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1377                                value => ''};          $self->{current_attribute}
1378          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1379                   value => '',
1380                   line => $self->{line}, column => $self->{column}};
1381            $self->{state} = ATTRIBUTE_NAME_STATE;
1382            !!!next-input-character;
1383            redo A;
1384          } elsif ($self->{next_char} == 0x002F) { # /
1385            !!!cp (77);
1386            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1387          !!!next-input-character;          !!!next-input-character;
1388          redo A;          redo A;
1389        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003C or # <  
                $self->{next_input_character} == -1) {  
1390          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1391          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1392              !!!cp (79);
1393            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1394          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1395            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1396            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1397                !!!cp (80);
1398              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1399              } else {
1400                ## NOTE: This state should never be reached.
1401                !!!cp (81);
1402            }            }
1403          } else {          } else {
1404            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1405          }          }
1406          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1407          # reconsume          # reconsume
1408    
1409          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1410    
1411          redo A;          redo A;
1412        } else {        } else {
1413          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1414                                value => ''};          $self->{current_attribute}
1415          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char}),
1416                   value => '',
1417                   line => $self->{line}, column => $self->{column}};
1418            $self->{state} = ATTRIBUTE_NAME_STATE;
1419          !!!next-input-character;          !!!next-input-character;
1420          redo A;                  redo A;        
1421        }        }
1422      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1423        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1424            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1425            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1426            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1427            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1428            !!!cp (83);
1429          ## Stay in the state          ## Stay in the state
1430          !!!next-input-character;          !!!next-input-character;
1431          redo A;          redo A;
1432        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1433          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1434            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1435          !!!next-input-character;          !!!next-input-character;
1436          redo A;          redo A;
1437        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1438          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1439            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1440          ## reconsume          ## reconsume
1441          redo A;          redo A;
1442        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1443          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1444            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1445          !!!next-input-character;          !!!next-input-character;
1446          redo A;          redo A;
1447        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1448          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1449              !!!cp (87);
1450            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1451          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1452            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1453            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1454                !!!cp (88);
1455              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1456              } else {
1457                ## NOTE: This state should never be reached.
1458                !!!cp (89);
1459            }            }
1460          } else {          } else {
1461            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1462          }          }
1463          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1464          !!!next-input-character;          !!!next-input-character;
1465    
1466          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1467    
1468          redo A;          redo A;
1469        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1470          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1471          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1472              !!!cp (90);
1473            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1474          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1475            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1476            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1477                !!!cp (91);
1478              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1479              } else {
1480                ## NOTE: This state should never be reached.
1481                !!!cp (92);
1482            }            }
1483          } else {          } else {
1484            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1485          }          }
1486          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1487          ## reconsume          ## reconsume
1488    
1489          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1490    
1491          redo A;          redo A;
1492        } else {        } else {
1493          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1494          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1495              !!!parse-error (type => 'bad attribute value');
1496            } else {
1497              !!!cp (94);
1498            }
1499            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1500            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1501          !!!next-input-character;          !!!next-input-character;
1502          redo A;          redo A;
1503        }        }
1504      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1505        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1506          $self->{state} = 'before attribute name';          !!!cp (95);
1507            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1508          !!!next-input-character;          !!!next-input-character;
1509          redo A;          redo A;
1510        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1511          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1512          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1513            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1514          !!!next-input-character;          !!!next-input-character;
1515          redo A;          redo A;
1516        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1517          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1518          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1519              !!!cp (97);
1520            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1521          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1522            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1523            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1524                !!!cp (98);
1525              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1526              } else {
1527                ## NOTE: This state should never be reached.
1528                !!!cp (99);
1529            }            }
1530          } else {          } else {
1531            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1532          }          }
1533          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1534          ## reconsume          ## reconsume
1535    
1536          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1537    
1538          redo A;          redo A;
1539        } else {        } else {
1540          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1541            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1542          ## Stay in the state          ## Stay in the state
1543          !!!next-input-character;          !!!next-input-character;
1544          redo A;          redo A;
1545        }        }
1546      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1547        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1548          $self->{state} = 'before attribute name';          !!!cp (101);
1549            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1550          !!!next-input-character;          !!!next-input-character;
1551          redo A;          redo A;
1552        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1553          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1554          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1555            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1556          !!!next-input-character;          !!!next-input-character;
1557          redo A;          redo A;
1558        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1559          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1560          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1561              !!!cp (103);
1562            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1563          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1564            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1565            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1566                !!!cp (104);
1567              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1568              } else {
1569                ## NOTE: This state should never be reached.
1570                !!!cp (105);
1571            }            }
1572          } else {          } else {
1573            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1574          }          }
1575          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1576          ## reconsume          ## reconsume
1577    
1578          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1579    
1580          redo A;          redo A;
1581        } else {        } else {
1582          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1583            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1584          ## Stay in the state          ## Stay in the state
1585          !!!next-input-character;          !!!next-input-character;
1586          redo A;          redo A;
1587        }        }
1588      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1589        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1590            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1591            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1592            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1593            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1594          $self->{state} = 'before attribute name';          !!!cp (107);
1595          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1596          redo A;          !!!next-input-character;
1597        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1598          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1599          $self->{state} = 'entity in attribute value';          !!!cp (108);
1600          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1601          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1602        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1603          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1604          } elsif ($self->{next_char} == 0x003E) { # >
1605            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1606              !!!cp (109);
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 (110);
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 (111);
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          !!!next-input-character;          !!!next-input-character;
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        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1627          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1628          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1629              !!!cp (112);
1630            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1631          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1632            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1633            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1634                !!!cp (113);
1635              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1636              } else {
1637                ## NOTE: This state should never be reached.
1638                !!!cp (114);
1639            }            }
1640          } else {          } else {
1641            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1642          }          }
1643          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1644          ## reconsume          ## reconsume
1645    
1646          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1647    
1648          redo A;          redo A;
1649        } else {        } else {
1650          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1651                 0x0022 => 1, # "
1652                 0x0027 => 1, # '
1653                 0x003D => 1, # =
1654                }->{$self->{next_char}}) {
1655              !!!cp (115);
1656              !!!parse-error (type => 'bad attribute value');
1657            } else {
1658              !!!cp (116);
1659            }
1660            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1661          ## Stay in the state          ## Stay in the state
1662          !!!next-input-character;          !!!next-input-character;
1663          redo A;          redo A;
1664        }        }
1665      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1666        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity
1667              (1,
1668               $self->{last_attribute_value_state}
1669                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1670               $self->{last_attribute_value_state}
1671                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1672               -1);
1673    
1674        unless (defined $token) {        unless (defined $token) {
1675            !!!cp (117);
1676          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1677        } else {        } else {
1678            !!!cp (118);
1679          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1680            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1681          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1682        }        }
1683    
1684        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1685        # next-input-character is already done        # next-input-character is already done
1686        redo A;        redo A;
1687      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1688          if ($self->{next_char} == 0x0009 or # HT
1689              $self->{next_char} == 0x000A or # LF
1690              $self->{next_char} == 0x000B or # VT
1691              $self->{next_char} == 0x000C or # FF
1692              $self->{next_char} == 0x0020) { # SP
1693            !!!cp (118);
1694            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1695            !!!next-input-character;
1696            redo A;
1697          } elsif ($self->{next_char} == 0x003E) { # >
1698            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1699              !!!cp (119);
1700              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1701            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1702              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1703              if ($self->{current_token}->{attributes}) {
1704                !!!cp (120);
1705                !!!parse-error (type => 'end tag attribute');
1706              } else {
1707                ## NOTE: This state should never be reached.
1708                !!!cp (121);
1709              }
1710            } else {
1711              die "$0: $self->{current_token}->{type}: Unknown token type";
1712            }
1713            $self->{state} = DATA_STATE;
1714            !!!next-input-character;
1715    
1716            !!!emit ($self->{current_token}); # start tag or end tag
1717    
1718            redo A;
1719          } elsif ($self->{next_char} == 0x002F) { # /
1720            !!!cp (122);
1721            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1722            !!!next-input-character;
1723            redo A;
1724          } else {
1725            !!!cp ('124.1');
1726            !!!parse-error (type => 'no space between attributes');
1727            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1728            ## reconsume
1729            redo A;
1730          }
1731        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1732          if ($self->{next_char} == 0x003E) { # >
1733            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1734              !!!cp ('124.2');
1735              !!!parse-error (type => 'nestc', token => $self->{current_token});
1736              ## TODO: Different type than slash in start tag
1737              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1738              if ($self->{current_token}->{attributes}) {
1739                !!!cp ('124.4');
1740                !!!parse-error (type => 'end tag attribute');
1741              } else {
1742                !!!cp ('124.5');
1743              }
1744              ## TODO: Test |<title></title/>|
1745            } else {
1746              !!!cp ('124.3');
1747              $self->{self_closing} = 1;
1748            }
1749    
1750            $self->{state} = DATA_STATE;
1751            !!!next-input-character;
1752    
1753            !!!emit ($self->{current_token}); # start tag or end tag
1754    
1755            redo A;
1756          } else {
1757            !!!cp ('124.4');
1758            !!!parse-error (type => 'nestc');
1759            ## TODO: This error type is wrong.
1760            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1761            ## Reconsume.
1762            redo A;
1763          }
1764        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1765        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1766                
1767        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1768          #my $token = {type => COMMENT_TOKEN, data => ''};
1769    
1770        BC: {        BC: {
1771          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1772            $self->{state} = 'data';            !!!cp (124);
1773              $self->{state} = DATA_STATE;
1774            !!!next-input-character;            !!!next-input-character;
1775    
1776            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1777    
1778            redo A;            redo A;
1779          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1780            $self->{state} = 'data';            !!!cp (125);
1781              $self->{state} = DATA_STATE;
1782            ## reconsume            ## reconsume
1783    
1784            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1785    
1786            redo A;            redo A;
1787          } else {          } else {
1788            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1789              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1790            !!!next-input-character;            !!!next-input-character;
1791            redo BC;            redo BC;
1792          }          }
1793        } # BC        } # BC
1794      } elsif ($self->{state} eq 'markup declaration open') {  
1795          die "$0: _get_next_token: unexpected case [BC]";
1796        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1797        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1798    
1799          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1800    
1801        my @next_char;        my @next_char;
1802        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1803                
1804        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1805          !!!next-input-character;          !!!next-input-character;
1806          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1807          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1808            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1809            $self->{state} = 'comment';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1810                                        line => $l, column => $c,
1811                                       };
1812              $self->{state} = COMMENT_START_STATE;
1813            !!!next-input-character;            !!!next-input-character;
1814            redo A;            redo A;
1815            } else {
1816              !!!cp (128);
1817          }          }
1818        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1819                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1820          !!!next-input-character;          !!!next-input-character;
1821          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1822          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1823              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1824            !!!next-input-character;            !!!next-input-character;
1825            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1826            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1827                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1828              !!!next-input-character;              !!!next-input-character;
1829              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1830              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1831                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1832                !!!next-input-character;                !!!next-input-character;
1833                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1834                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1835                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1836                  !!!next-input-character;                  !!!next-input-character;
1837                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1838                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1839                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1840                    !!!next-input-character;                    !!!next-input-character;
1841                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1842                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1843                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1844                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1845                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1846                        $self->{state} = DOCTYPE_STATE;
1847                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1848                                                  quirks => 1,
1849                                                  line => $l, column => $c,
1850                                                 };
1851                      !!!next-input-character;                      !!!next-input-character;
1852                      redo A;                      redo A;
1853                      } else {
1854                        !!!cp (130);
1855                    }                    }
1856                    } else {
1857                      !!!cp (131);
1858                  }                  }
1859                  } else {
1860                    !!!cp (132);
1861                }                }
1862                } else {
1863                  !!!cp (133);
1864              }              }
1865              } else {
1866                !!!cp (134);
1867            }            }
1868            } else {
1869              !!!cp (135);
1870          }          }
1871          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1872                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1873                   $self->{next_char} == 0x005B) { # [
1874            !!!next-input-character;
1875            push @next_char, $self->{next_char};
1876            if ($self->{next_char} == 0x0043) { # C
1877              !!!next-input-character;
1878              push @next_char, $self->{next_char};
1879              if ($self->{next_char} == 0x0044) { # D
1880                !!!next-input-character;
1881                push @next_char, $self->{next_char};
1882                if ($self->{next_char} == 0x0041) { # A
1883                  !!!next-input-character;
1884                  push @next_char, $self->{next_char};
1885                  if ($self->{next_char} == 0x0054) { # T
1886                    !!!next-input-character;
1887                    push @next_char, $self->{next_char};
1888                    if ($self->{next_char} == 0x0041) { # A
1889                      !!!next-input-character;
1890                      push @next_char, $self->{next_char};
1891                      if ($self->{next_char} == 0x005B) { # [
1892                        !!!cp (135.1);
1893                        $self->{state} = CDATA_BLOCK_STATE;
1894                        !!!next-input-character;
1895                        redo A;
1896                      } else {
1897                        !!!cp (135.2);
1898                      }
1899                    } else {
1900                      !!!cp (135.3);
1901                    }
1902                  } else {
1903                    !!!cp (135.4);                
1904                  }
1905                } else {
1906                  !!!cp (135.5);
1907                }
1908              } else {
1909                !!!cp (135.6);
1910              }
1911            } else {
1912              !!!cp (135.7);
1913            }
1914          } else {
1915            !!!cp (136);
1916        }        }
1917    
1918        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1919        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1920        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1921        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1922          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1923                                    line => $l, column => $c,
1924                                   };
1925        redo A;        redo A;
1926                
1927        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1928        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
1929      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1930        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1931          $self->{state} = 'comment dash';          !!!cp (137);
1932            $self->{state} = COMMENT_START_DASH_STATE;
1933            !!!next-input-character;
1934            redo A;
1935          } elsif ($self->{next_char} == 0x003E) { # >
1936            !!!cp (138);
1937            !!!parse-error (type => 'bogus comment');
1938            $self->{state} = DATA_STATE;
1939            !!!next-input-character;
1940    
1941            !!!emit ($self->{current_token}); # comment
1942    
1943            redo A;
1944          } elsif ($self->{next_char} == -1) {
1945            !!!cp (139);
1946            !!!parse-error (type => 'unclosed comment');
1947            $self->{state} = DATA_STATE;
1948            ## reconsume
1949    
1950            !!!emit ($self->{current_token}); # comment
1951    
1952            redo A;
1953          } else {
1954            !!!cp (140);
1955            $self->{current_token}->{data} # comment
1956                .= chr ($self->{next_char});
1957            $self->{state} = COMMENT_STATE;
1958            !!!next-input-character;
1959            redo A;
1960          }
1961        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1962          if ($self->{next_char} == 0x002D) { # -
1963            !!!cp (141);
1964            $self->{state} = COMMENT_END_STATE;
1965            !!!next-input-character;
1966            redo A;
1967          } elsif ($self->{next_char} == 0x003E) { # >
1968            !!!cp (142);
1969            !!!parse-error (type => 'bogus comment');
1970            $self->{state} = DATA_STATE;
1971          !!!next-input-character;          !!!next-input-character;
1972    
1973            !!!emit ($self->{current_token}); # comment
1974    
1975          redo A;          redo A;
1976        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1977            !!!cp (143);
1978          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1979          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1980          ## reconsume          ## reconsume
1981    
1982          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1983    
1984          redo A;          redo A;
1985        } else {        } else {
1986          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (144);
1987            $self->{current_token}->{data} # comment
1988                .= '-' . chr ($self->{next_char});
1989            $self->{state} = COMMENT_STATE;
1990            !!!next-input-character;
1991            redo A;
1992          }
1993        } elsif ($self->{state} == COMMENT_STATE) {
1994          if ($self->{next_char} == 0x002D) { # -
1995            !!!cp (145);
1996            $self->{state} = COMMENT_END_DASH_STATE;
1997            !!!next-input-character;
1998            redo A;
1999          } elsif ($self->{next_char} == -1) {
2000            !!!cp (146);
2001            !!!parse-error (type => 'unclosed comment');
2002            $self->{state} = DATA_STATE;
2003            ## reconsume
2004    
2005            !!!emit ($self->{current_token}); # comment
2006    
2007            redo A;
2008          } else {
2009            !!!cp (147);
2010            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2011          ## Stay in the state          ## Stay in the state
2012          !!!next-input-character;          !!!next-input-character;
2013          redo A;          redo A;
2014        }        }
2015      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2016        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2017          $self->{state} = 'comment end';          !!!cp (148);
2018            $self->{state} = COMMENT_END_STATE;
2019          !!!next-input-character;          !!!next-input-character;
2020          redo A;          redo A;
2021        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2022            !!!cp (149);
2023          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2024          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2025          ## reconsume          ## reconsume
2026    
2027          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2028    
2029          redo A;          redo A;
2030        } else {        } else {
2031          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2032          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2033            $self->{state} = COMMENT_STATE;
2034          !!!next-input-character;          !!!next-input-character;
2035          redo A;          redo A;
2036        }        }
2037      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2038        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2039          $self->{state} = 'data';          !!!cp (151);
2040            $self->{state} = DATA_STATE;
2041          !!!next-input-character;          !!!next-input-character;
2042    
2043          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2044    
2045          redo A;          redo A;
2046        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2047          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2048            !!!parse-error (type => 'dash in comment',
2049                            line => $self->{line_prev},
2050                            column => $self->{column_prev});
2051          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2052          ## Stay in the state          ## Stay in the state
2053          !!!next-input-character;          !!!next-input-character;
2054          redo A;          redo A;
2055        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2056            !!!cp (153);
2057          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2058          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2059          ## reconsume          ## reconsume
2060    
2061          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2062    
2063          redo A;          redo A;
2064        } else {        } else {
2065          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2066          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2067          $self->{state} = 'comment';                          line => $self->{line_prev},
2068                            column => $self->{column_prev});
2069            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2070            $self->{state} = COMMENT_STATE;
2071          !!!next-input-character;          !!!next-input-character;
2072          redo A;          redo A;
2073        }        }
2074      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2075        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2076            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2077            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2078            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2079            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2080          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2081            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2082          !!!next-input-character;          !!!next-input-character;
2083          redo A;          redo A;
2084        } else {        } else {
2085            !!!cp (156);
2086          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2087          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2088          ## reconsume          ## reconsume
2089          redo A;          redo A;
2090        }        }
2091      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2092        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2093            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2094            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2095            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2096            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2097            !!!cp (157);
2098          ## Stay in the state          ## Stay in the state
2099          !!!next-input-character;          !!!next-input-character;
2100          redo A;          redo A;
2101        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
2102                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (158);
 ## ISSUE: "Set the token's name name to the" in the spec  
         $self->{current_token} = {type => 'DOCTYPE',  
                           name => chr ($self->{next_input_character} - 0x0020),  
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
2103          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2104          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2105          !!!next-input-character;          !!!next-input-character;
2106    
2107          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2108    
2109          redo A;          redo A;
2110        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2111            !!!cp (159);
2112          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2113          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2114          ## reconsume          ## reconsume
2115    
2116          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2117    
2118          redo A;          redo A;
2119        } else {        } else {
2120          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (160);
2121                            name => chr ($self->{next_input_character}),          $self->{current_token}->{name} = chr $self->{next_char};
2122                            error => 1};          delete $self->{current_token}->{quirks};
2123  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2124          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2125          !!!next-input-character;          !!!next-input-character;
2126          redo A;          redo A;
2127        }        }
2128      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2129        if ($self->{next_input_character} == 0x0009 or # HT  ## ISSUE: Redundant "First," in the spec.
2130            $self->{next_input_character} == 0x000A or # LF        if ($self->{next_char} == 0x0009 or # HT
2131            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000A or # LF
2132            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000B or # VT
2133            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x000C or # FF
2134          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE            $self->{next_char} == 0x0020) { # SP
2135          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2136            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2137          !!!next-input-character;          !!!next-input-character;
2138          redo A;          redo A;
2139        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2140          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (162);
2141          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2142          !!!next-input-character;          !!!next-input-character;
2143    
2144          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2145    
2146          redo A;          redo A;
2147        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
2148                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (163);
2149          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed DOCTYPE');
2150          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{state} = DATA_STATE;
2151            ## reconsume
2152    
2153            $self->{current_token}->{quirks} = 1;
2154            !!!emit ($self->{current_token}); # DOCTYPE
2155    
2156            redo A;
2157          } else {
2158            !!!cp (164);
2159            $self->{current_token}->{name}
2160              .= chr ($self->{next_char}); # DOCTYPE
2161            ## Stay in the state
2162            !!!next-input-character;
2163            redo A;
2164          }
2165        } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2166          if ($self->{next_char} == 0x0009 or # HT
2167              $self->{next_char} == 0x000A or # LF
2168              $self->{next_char} == 0x000B or # VT
2169              $self->{next_char} == 0x000C or # FF
2170              $self->{next_char} == 0x0020) { # SP
2171            !!!cp (165);
2172            ## Stay in the state
2173            !!!next-input-character;
2174            redo A;
2175          } elsif ($self->{next_char} == 0x003E) { # >
2176            !!!cp (166);
2177            $self->{state} = DATA_STATE;
2178            !!!next-input-character;
2179    
2180            !!!emit ($self->{current_token}); # DOCTYPE
2181    
2182            redo A;
2183          } elsif ($self->{next_char} == -1) {
2184            !!!cp (167);
2185            !!!parse-error (type => 'unclosed DOCTYPE');
2186            $self->{state} = DATA_STATE;
2187            ## reconsume
2188    
2189            $self->{current_token}->{quirks} = 1;
2190            !!!emit ($self->{current_token}); # DOCTYPE
2191    
2192            redo A;
2193          } elsif ($self->{next_char} == 0x0050 or # P
2194                   $self->{next_char} == 0x0070) { # p
2195            !!!next-input-character;
2196            if ($self->{next_char} == 0x0055 or # U
2197                $self->{next_char} == 0x0075) { # u
2198              !!!next-input-character;
2199              if ($self->{next_char} == 0x0042 or # B
2200                  $self->{next_char} == 0x0062) { # b
2201                !!!next-input-character;
2202                if ($self->{next_char} == 0x004C or # L
2203                    $self->{next_char} == 0x006C) { # l
2204                  !!!next-input-character;
2205                  if ($self->{next_char} == 0x0049 or # I
2206                      $self->{next_char} == 0x0069) { # i
2207                    !!!next-input-character;
2208                    if ($self->{next_char} == 0x0043 or # C
2209                        $self->{next_char} == 0x0063) { # c
2210                      !!!cp (168);
2211                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2212                      !!!next-input-character;
2213                      redo A;
2214                    } else {
2215                      !!!cp (169);
2216                    }
2217                  } else {
2218                    !!!cp (170);
2219                  }
2220                } else {
2221                  !!!cp (171);
2222                }
2223              } else {
2224                !!!cp (172);
2225              }
2226            } else {
2227              !!!cp (173);
2228            }
2229    
2230            #
2231          } elsif ($self->{next_char} == 0x0053 or # S
2232                   $self->{next_char} == 0x0073) { # s
2233            !!!next-input-character;
2234            if ($self->{next_char} == 0x0059 or # Y
2235                $self->{next_char} == 0x0079) { # y
2236              !!!next-input-character;
2237              if ($self->{next_char} == 0x0053 or # S
2238                  $self->{next_char} == 0x0073) { # s
2239                !!!next-input-character;
2240                if ($self->{next_char} == 0x0054 or # T
2241                    $self->{next_char} == 0x0074) { # t
2242                  !!!next-input-character;
2243                  if ($self->{next_char} == 0x0045 or # E
2244                      $self->{next_char} == 0x0065) { # e
2245                    !!!next-input-character;
2246                    if ($self->{next_char} == 0x004D or # M
2247                        $self->{next_char} == 0x006D) { # m
2248                      !!!cp (174);
2249                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2250                      !!!next-input-character;
2251                      redo A;
2252                    } else {
2253                      !!!cp (175);
2254                    }
2255                  } else {
2256                    !!!cp (176);
2257                  }
2258                } else {
2259                  !!!cp (177);
2260                }
2261              } else {
2262                !!!cp (178);
2263              }
2264            } else {
2265              !!!cp (179);
2266            }
2267    
2268            #
2269          } else {
2270            !!!cp (180);
2271            !!!next-input-character;
2272            #
2273          }
2274    
2275          !!!parse-error (type => 'string after DOCTYPE name');
2276          $self->{current_token}->{quirks} = 1;
2277    
2278          $self->{state} = BOGUS_DOCTYPE_STATE;
2279          # next-input-character is already done
2280          redo A;
2281        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2282          if ({
2283                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2284                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2285              }->{$self->{next_char}}) {
2286            !!!cp (181);
2287          ## Stay in the state          ## Stay in the state
2288          !!!next-input-character;          !!!next-input-character;
2289          redo A;          redo A;
2290        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} eq 0x0022) { # "
2291            !!!cp (182);
2292            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2293            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2294            !!!next-input-character;
2295            redo A;
2296          } elsif ($self->{next_char} eq 0x0027) { # '
2297            !!!cp (183);
2298            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2299            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2300            !!!next-input-character;
2301            redo A;
2302          } elsif ($self->{next_char} eq 0x003E) { # >
2303            !!!cp (184);
2304            !!!parse-error (type => 'no PUBLIC literal');
2305    
2306            $self->{state} = DATA_STATE;
2307            !!!next-input-character;
2308    
2309            $self->{current_token}->{quirks} = 1;
2310            !!!emit ($self->{current_token}); # DOCTYPE
2311    
2312            redo A;
2313          } elsif ($self->{next_char} == -1) {
2314            !!!cp (185);
2315          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2316          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
2317          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2318          ## reconsume          ## reconsume
2319    
2320          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
2321          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
2322    
2323          redo A;          redo A;
2324        } else {        } else {
2325          $self->{current_token}->{name}          !!!cp (186);
2326            .= chr ($self->{next_input_character}); # DOCTYPE          !!!parse-error (type => 'string after PUBLIC');
2327          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{current_token}->{quirks} = 1;
2328    
2329            $self->{state} = BOGUS_DOCTYPE_STATE;
2330            !!!next-input-character;
2331            redo A;
2332          }
2333        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2334          if ($self->{next_char} == 0x0022) { # "
2335            !!!cp (187);
2336            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2337            !!!next-input-character;
2338            redo A;
2339          } elsif ($self->{next_char} == 0x003E) { # >
2340            !!!cp (188);
2341            !!!parse-error (type => 'unclosed PUBLIC literal');
2342    
2343            $self->{state} = DATA_STATE;
2344            !!!next-input-character;
2345    
2346            $self->{current_token}->{quirks} = 1;
2347            !!!emit ($self->{current_token}); # DOCTYPE
2348    
2349            redo A;
2350          } elsif ($self->{next_char} == -1) {
2351            !!!cp (189);
2352            !!!parse-error (type => 'unclosed PUBLIC literal');
2353    
2354            $self->{state} = DATA_STATE;
2355            ## reconsume
2356    
2357            $self->{current_token}->{quirks} = 1;
2358            !!!emit ($self->{current_token}); # DOCTYPE
2359    
2360            redo A;
2361          } else {
2362            !!!cp (190);
2363            $self->{current_token}->{public_identifier} # DOCTYPE
2364                .= chr $self->{next_char};
2365            ## Stay in the state
2366            !!!next-input-character;
2367            redo A;
2368          }
2369        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2370          if ($self->{next_char} == 0x0027) { # '
2371            !!!cp (191);
2372            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2373            !!!next-input-character;
2374            redo A;
2375          } elsif ($self->{next_char} == 0x003E) { # >
2376            !!!cp (192);
2377            !!!parse-error (type => 'unclosed PUBLIC literal');
2378    
2379            $self->{state} = DATA_STATE;
2380            !!!next-input-character;
2381    
2382            $self->{current_token}->{quirks} = 1;
2383            !!!emit ($self->{current_token}); # DOCTYPE
2384    
2385            redo A;
2386          } elsif ($self->{next_char} == -1) {
2387            !!!cp (193);
2388            !!!parse-error (type => 'unclosed PUBLIC literal');
2389    
2390            $self->{state} = DATA_STATE;
2391            ## reconsume
2392    
2393            $self->{current_token}->{quirks} = 1;
2394            !!!emit ($self->{current_token}); # DOCTYPE
2395    
2396            redo A;
2397          } else {
2398            !!!cp (194);
2399            $self->{current_token}->{public_identifier} # DOCTYPE
2400                .= chr $self->{next_char};
2401            ## Stay in the state
2402            !!!next-input-character;
2403            redo A;
2404          }
2405        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2406          if ({
2407                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2408                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2409              }->{$self->{next_char}}) {
2410            !!!cp (195);
2411            ## Stay in the state
2412            !!!next-input-character;
2413            redo A;
2414          } elsif ($self->{next_char} == 0x0022) { # "
2415            !!!cp (196);
2416            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2417            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2418            !!!next-input-character;
2419            redo A;
2420          } elsif ($self->{next_char} == 0x0027) { # '
2421            !!!cp (197);
2422            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2423            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2424            !!!next-input-character;
2425            redo A;
2426          } elsif ($self->{next_char} == 0x003E) { # >
2427            !!!cp (198);
2428            $self->{state} = DATA_STATE;
2429            !!!next-input-character;
2430    
2431            !!!emit ($self->{current_token}); # DOCTYPE
2432    
2433            redo A;
2434          } elsif ($self->{next_char} == -1) {
2435            !!!cp (199);
2436            !!!parse-error (type => 'unclosed DOCTYPE');
2437    
2438            $self->{state} = DATA_STATE;
2439            ## reconsume
2440    
2441            $self->{current_token}->{quirks} = 1;
2442            !!!emit ($self->{current_token}); # DOCTYPE
2443    
2444            redo A;
2445          } else {
2446            !!!cp (200);
2447            !!!parse-error (type => 'string after PUBLIC literal');
2448            $self->{current_token}->{quirks} = 1;
2449    
2450            $self->{state} = BOGUS_DOCTYPE_STATE;
2451            !!!next-input-character;
2452            redo A;
2453          }
2454        } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2455          if ({
2456                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2457                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2458              }->{$self->{next_char}}) {
2459            !!!cp (201);
2460            ## Stay in the state
2461            !!!next-input-character;
2462            redo A;
2463          } elsif ($self->{next_char} == 0x0022) { # "
2464            !!!cp (202);
2465            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2466            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2467            !!!next-input-character;
2468            redo A;
2469          } elsif ($self->{next_char} == 0x0027) { # '
2470            !!!cp (203);
2471            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2472            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2473            !!!next-input-character;
2474            redo A;
2475          } elsif ($self->{next_char} == 0x003E) { # >
2476            !!!cp (204);
2477            !!!parse-error (type => 'no SYSTEM literal');
2478            $self->{state} = DATA_STATE;
2479            !!!next-input-character;
2480    
2481            $self->{current_token}->{quirks} = 1;
2482            !!!emit ($self->{current_token}); # DOCTYPE
2483    
2484            redo A;
2485          } elsif ($self->{next_char} == -1) {
2486            !!!cp (205);
2487            !!!parse-error (type => 'unclosed DOCTYPE');
2488    
2489            $self->{state} = DATA_STATE;
2490            ## reconsume
2491    
2492            $self->{current_token}->{quirks} = 1;
2493            !!!emit ($self->{current_token}); # DOCTYPE
2494    
2495            redo A;
2496          } else {
2497            !!!cp (206);
2498            !!!parse-error (type => 'string after SYSTEM');
2499            $self->{current_token}->{quirks} = 1;
2500    
2501            $self->{state} = BOGUS_DOCTYPE_STATE;
2502            !!!next-input-character;
2503            redo A;
2504          }
2505        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2506          if ($self->{next_char} == 0x0022) { # "
2507            !!!cp (207);
2508            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2509            !!!next-input-character;
2510            redo A;
2511          } elsif ($self->{next_char} == 0x003E) { # >
2512            !!!cp (208);
2513            !!!parse-error (type => 'unclosed PUBLIC literal');
2514    
2515            $self->{state} = DATA_STATE;
2516            !!!next-input-character;
2517    
2518            $self->{current_token}->{quirks} = 1;
2519            !!!emit ($self->{current_token}); # DOCTYPE
2520    
2521            redo A;
2522          } elsif ($self->{next_char} == -1) {
2523            !!!cp (209);
2524            !!!parse-error (type => 'unclosed SYSTEM literal');
2525    
2526            $self->{state} = DATA_STATE;
2527            ## reconsume
2528    
2529            $self->{current_token}->{quirks} = 1;
2530            !!!emit ($self->{current_token}); # DOCTYPE
2531    
2532            redo A;
2533          } else {
2534            !!!cp (210);
2535            $self->{current_token}->{system_identifier} # DOCTYPE
2536                .= chr $self->{next_char};
2537          ## Stay in the state          ## Stay in the state
2538          !!!next-input-character;          !!!next-input-character;
2539          redo A;          redo A;
2540        }        }
2541      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2542        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0027) { # '
2543            $self->{next_input_character} == 0x000A or # LF          !!!cp (211);
2544            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2545            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
2546            $self->{next_input_character} == 0x0020) { # SP          redo A;
2547          } elsif ($self->{next_char} == 0x003E) { # >
2548            !!!cp (212);
2549            !!!parse-error (type => 'unclosed PUBLIC literal');
2550    
2551            $self->{state} = DATA_STATE;
2552            !!!next-input-character;
2553    
2554            $self->{current_token}->{quirks} = 1;
2555            !!!emit ($self->{current_token}); # DOCTYPE
2556    
2557            redo A;
2558          } elsif ($self->{next_char} == -1) {
2559            !!!cp (213);
2560            !!!parse-error (type => 'unclosed SYSTEM literal');
2561    
2562            $self->{state} = DATA_STATE;
2563            ## reconsume
2564    
2565            $self->{current_token}->{quirks} = 1;
2566            !!!emit ($self->{current_token}); # DOCTYPE
2567    
2568            redo A;
2569          } else {
2570            !!!cp (214);
2571            $self->{current_token}->{system_identifier} # DOCTYPE
2572                .= chr $self->{next_char};
2573          ## Stay in the state          ## Stay in the state
2574          !!!next-input-character;          !!!next-input-character;
2575          redo A;          redo A;
2576        } elsif ($self->{next_input_character} == 0x003E) { # >        }
2577          $self->{state} = 'data';      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2578          if ({
2579                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2580                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2581              }->{$self->{next_char}}) {
2582            !!!cp (215);
2583            ## Stay in the state
2584            !!!next-input-character;
2585            redo A;
2586          } elsif ($self->{next_char} == 0x003E) { # >
2587            !!!cp (216);
2588            $self->{state} = DATA_STATE;
2589          !!!next-input-character;          !!!next-input-character;
2590    
2591          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2592    
2593          redo A;          redo A;
2594        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2595            !!!cp (217);
2596          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2597          $self->{state} = 'data';  
2598            $self->{state} = DATA_STATE;
2599          ## reconsume          ## reconsume
2600    
2601            $self->{current_token}->{quirks} = 1;
2602          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2603    
2604          redo A;          redo A;
2605        } else {        } else {
2606          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (218);
2607          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after SYSTEM literal');
2608          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2609    
2610            $self->{state} = BOGUS_DOCTYPE_STATE;
2611          !!!next-input-character;          !!!next-input-character;
2612          redo A;          redo A;
2613        }        }
2614      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2615        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2616          $self->{state} = 'data';          !!!cp (219);
2617            $self->{state} = DATA_STATE;
2618          !!!next-input-character;          !!!next-input-character;
2619    
2620          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2621    
2622          redo A;          redo A;
2623        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2624            !!!cp (220);
2625          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2626          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2627          ## reconsume          ## reconsume
2628    
2629          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2630    
2631          redo A;          redo A;
2632        } else {        } else {
2633            !!!cp (221);
2634          ## Stay in the state          ## Stay in the state
2635          !!!next-input-character;          !!!next-input-character;
2636          redo A;          redo A;
2637        }        }
2638        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2639          my $s = '';
2640          
2641          my ($l, $c) = ($self->{line}, $self->{column});
2642    
2643          CS: while ($self->{next_char} != -1) {
2644            if ($self->{next_char} == 0x005D) { # ]
2645              !!!next-input-character;
2646              if ($self->{next_char} == 0x005D) { # ]
2647                !!!next-input-character;
2648                MDC: {
2649                  if ($self->{next_char} == 0x003E) { # >
2650                    !!!cp (221.1);
2651                    !!!next-input-character;
2652                    last CS;
2653                  } elsif ($self->{next_char} == 0x005D) { # ]
2654                    !!!cp (221.2);
2655                    $s .= ']';
2656                    !!!next-input-character;
2657                    redo MDC;
2658                  } else {
2659                    !!!cp (221.3);
2660                    $s .= ']]';
2661                    #
2662                  }
2663                } # MDC
2664              } else {
2665                !!!cp (221.4);
2666                $s .= ']';
2667                #
2668              }
2669            } else {
2670              !!!cp (221.5);
2671              #
2672            }
2673            $s .= chr $self->{next_char};
2674            !!!next-input-character;
2675          } # CS
2676    
2677          $self->{state} = DATA_STATE;
2678          ## next-input-character done or EOF, which is reconsumed.
2679    
2680          if (length $s) {
2681            !!!cp (221.6);
2682            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2683                      line => $l, column => $c});
2684          } else {
2685            !!!cp (221.7);
2686          }
2687    
2688          redo A;
2689    
2690          ## ISSUE: "text tokens" in spec.
2691          ## TODO: Streaming support
2692      } else {      } else {
2693        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2694      }      }
# Line 1490  sub _get_next_token ($) { Line 2697  sub _get_next_token ($) {
2697    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2698  } # _get_next_token  } # _get_next_token
2699    
2700  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2701    my $self = shift;    my ($self, $in_attr, $additional) = @_;
2702      
2703    if ($self->{next_input_character} == 0x0023) { # #    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2704    
2705      if ({
2706           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2707           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2708           $additional => 1,
2709          }->{$self->{next_char}}) {
2710        !!!cp (1001);
2711        ## Don't consume
2712        ## No error
2713        return undef;
2714      } elsif ($self->{next_char} == 0x0023) { # #
2715      !!!next-input-character;      !!!next-input-character;
2716      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2717          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2718        my $num;        my $code;
2719        X: {        X: {
2720          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2721          !!!next-input-character;          !!!next-input-character;
2722          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2723              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2724            $num ||= 0;            !!!cp (1002);
2725            $num *= 0x10;            $code ||= 0;
2726            $num += $self->{next_input_character} - 0x0030;            $code *= 0x10;
2727              $code += $self->{next_char} - 0x0030;
2728            redo X;            redo X;
2729          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2730                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2731            ## ISSUE: the spec says U+0078, which is apparently incorrect            !!!cp (1003);
2732            $num ||= 0;            $code ||= 0;
2733            $num *= 0x10;            $code *= 0x10;
2734            $num += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2735            redo X;            redo X;
2736          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2737                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2738            ## ISSUE: the spec says U+0058, which is apparently incorrect            !!!cp (1004);
2739            $num ||= 0;            $code ||= 0;
2740            $num *= 0x10;            $code *= 0x10;
2741            $num += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2742            redo X;            redo X;
2743          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2744            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2745            $self->{next_input_character} = 0x0023; # #            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2746            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char, $self->{next_char});
2747              $self->{next_char} = 0x0023; # #
2748            return undef;            return undef;
2749          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2750              !!!cp (1006);
2751            !!!next-input-character;            !!!next-input-character;
2752          } else {          } else {
2753            !!!parse-error (type => 'no refc');            !!!cp (1007);
2754          }            !!!parse-error (type => 'no refc', line => $l, column => $c);
   
         ## TODO: check the definition for |a valid Unicode character|.  
         ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>  
         if ($num > 1114111 or $num == 0) {  
           $num = 0xFFFD; # REPLACEMENT CHARACTER  
           ## ISSUE: Why this is not an error?  
         } elsif (0x80 <= $num and $num <= 0x9F) {  
           !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);  
           $num = $c1_entity_char->{$num};  
2755          }          }
2756    
2757          return {type => 'character', data => chr $num};          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2758              !!!cp (1008);
2759              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2760              $code = 0xFFFD;
2761            } elsif ($code > 0x10FFFF) {
2762              !!!cp (1009);
2763              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2764              $code = 0xFFFD;
2765            } elsif ($code == 0x000D) {
2766              !!!cp (1010);
2767              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2768              $code = 0x000A;
2769            } elsif (0x80 <= $code and $code <= 0x9F) {
2770              !!!cp (1011);
2771              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2772              $code = $c1_entity_char->{$code};
2773            }
2774    
2775            return {type => CHARACTER_TOKEN, data => chr $code,
2776                    has_reference => 1,
2777                    line => $l, column => $c,
2778                   };
2779        } # X        } # X
2780      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2781               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2782        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2783        !!!next-input-character;        !!!next-input-character;
2784                
2785        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2786                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2787            !!!cp (1012);
2788          $code *= 10;          $code *= 10;
2789          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2790                    
2791          !!!next-input-character;          !!!next-input-character;
2792        }        }
2793    
2794        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2795            !!!cp (1013);
2796          !!!next-input-character;          !!!next-input-character;
2797        } else {        } else {
2798          !!!parse-error (type => 'no refc');          !!!cp (1014);
2799            !!!parse-error (type => 'no refc', line => $l, column => $c);
2800        }        }
2801    
2802        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2803        if ($code > 1114111 or $code == 0) {          !!!cp (1015);
2804          $code = 0xFFFD; # REPLACEMENT CHARACTER          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2805          ## ISSUE: Why this is not an error?          $code = 0xFFFD;
2806          } elsif ($code > 0x10FFFF) {
2807            !!!cp (1016);
2808            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2809            $code = 0xFFFD;
2810          } elsif ($code == 0x000D) {
2811            !!!cp (1017);
2812            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2813            $code = 0x000A;
2814        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2815          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!cp (1018);
2816            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2817          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2818        }        }
2819                
2820        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2821                  line => $l, column => $c,
2822                 };
2823      } else {      } else {
2824        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2825        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2826        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2827          $self->{next_char} = 0x0023; # #
2828        return undef;        return undef;
2829      }      }
2830    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2831              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2832             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2833              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2834      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2835      !!!next-input-character;      !!!next-input-character;
2836    
2837      my $value = $entity_name;      my $value = $entity_name;
2838      my $match;      my $match = 0;
2839        require Whatpm::_NamedEntityList;
2840        our $EntityChar;
2841    
2842      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2843             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2844             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_char} and # a
2845               $self->{next_input_character} <= 0x005A) or               $self->{next_char} <= 0x005A) or # x
2846              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_char} and # a
2847               $self->{next_input_character} <= 0x007A) or               $self->{next_char} <= 0x007A) or # z
2848              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_char} and # 0
2849               $self->{next_input_character} <= 0x0039))) {               $self->{next_char} <= 0x0039) or # 9
2850        $entity_name .= chr $self->{next_input_character};              $self->{next_char} == 0x003B)) { # ;
2851        if (defined $entity_char->{$entity_name}) {        $entity_name .= chr $self->{next_char};
2852          $value = $entity_char->{$entity_name};        if (defined $EntityChar->{$entity_name}) {
2853          $match = 1;          if ($self->{next_char} == 0x003B) { # ;
2854              !!!cp (1020);
2855              $value = $EntityChar->{$entity_name};
2856              $match = 1;
2857              !!!next-input-character;
2858              last;
2859            } else {
2860              !!!cp (1021);
2861              $value = $EntityChar->{$entity_name};
2862              $match = -1;
2863              !!!next-input-character;
2864            }
2865        } else {        } else {
2866          $value .= chr $self->{next_input_character};          !!!cp (1022);
2867            $value .= chr $self->{next_char};
2868            $match *= 2;
2869            !!!next-input-character;
2870        }        }
       !!!next-input-character;  
2871      }      }
2872            
2873      if ($match) {      if ($match > 0) {
2874        if ($self->{next_input_character} == 0x003B) { # ;        !!!cp (1023);
2875          !!!next-input-character;        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2876                  line => $l, column => $c,
2877                 };
2878        } elsif ($match < 0) {
2879          !!!parse-error (type => 'no refc', line => $l, column => $c);
2880          if ($in_attr and $match < -1) {
2881            !!!cp (1024);
2882            return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2883                    line => $l, column => $c,
2884                   };
2885        } else {        } else {
2886          !!!parse-error (type => 'refc');          !!!cp (1025);
2887            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2888                    line => $l, column => $c,
2889                   };
2890        }        }
   
       return {type => 'character', data => $value};  
2891      } else {      } else {
2892        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2893        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2894        !!!back-token ({type => 'character', data => $value});        ## NOTE: "No characters are consumed" in the spec.
2895        return undef;        return {type => CHARACTER_TOKEN, data => '&'.$value,
2896                  line => $l, column => $c,
2897                 };
2898      }      }
2899    } else {    } else {
2900        !!!cp (1027);
2901      ## no characters are consumed      ## no characters are consumed
2902      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2903      return undef;      return undef;
2904    }    }
2905  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1634  sub _initialize_tree_constructor ($) { Line 2910  sub _initialize_tree_constructor ($) {
2910    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
2911    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
2912    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
2913    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
2914  } # _initialize_tree_constructor  } # _initialize_tree_constructor
2915    
2916  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1661  sub _construct_tree ($) { Line 2937  sub _construct_tree ($) {
2937        
2938    !!!next-token;    !!!next-token;
2939    
   $self->{insertion_mode} = 'before head';  
2940    undef $self->{form_element};    undef $self->{form_element};
2941    undef $self->{head_element};    undef $self->{head_element};
2942    $self->{open_elements} = [];    $self->{open_elements} = [];
2943    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2944    
2945      ## NOTE: The "initial" insertion mode.
2946    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2947    
2948      ## NOTE: The "before html" insertion mode.
2949    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2950      $self->{insertion_mode} = BEFORE_HEAD_IM;
2951    
2952      ## NOTE: The "before head" insertion mode and so on.
2953    $self->_tree_construction_main;    $self->_tree_construction_main;
2954  } # _construct_tree  } # _construct_tree
2955    
2956  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2957    my $self = shift;    my $self = shift;
2958    B: {  
2959        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
2960          if ($token->{error}) {  
2961            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
2962            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
2963          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2964          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
2965            ($token->{name});        ## language.
2966          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
2967          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
2968          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/;
2969          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
2970          return;            defined $token->{public_identifier} or
2971        } elsif ({            defined $token->{system_identifier}) {
2972                  comment => 1,          !!!cp ('t1');
2973                  'start tag' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
2974                  'end tag' => 1,        } elsif ($doctype_name ne 'HTML') {
2975                  'end-of-file' => 1,          !!!cp ('t2');
2976                 }->{$token->{type}}) {          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2977          ## ISSUE: Spec currently left this case undefined.          !!!parse-error (type => 'not HTML5', token => $token);
2978          !!!parse-error (type => 'missing DOCTYPE');        } else {
2979          #$phase = 'root element';          !!!cp ('t3');
2980          ## reprocess        }
2981          #redo B;        
2982          return;        my $doctype = $self->{document}->create_document_type_definition
2983        } elsif ($token->{type} eq 'character') {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2984          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        ## NOTE: Default value for both |public_id| and |system_id| attributes
2985            $self->{document}->manakai_append_text ($1);        ## are empty strings, so that we don't set any value in missing cases.
2986            ## ISSUE: DOM3 Core does not allow Document > Text        $doctype->public_id ($token->{public_identifier})
2987            unless (length $token->{data}) {            if defined $token->{public_identifier};
2988              ## Stay in the phase        $doctype->system_id ($token->{system_identifier})
2989              !!!next-token;            if defined $token->{system_identifier};
2990              redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
2991          ## ISSUE: internalSubset = null??
2992          $self->{document}->append_child ($doctype);
2993          
2994          if ($token->{quirks} or $doctype_name ne 'HTML') {
2995            !!!cp ('t4');
2996            $self->{document}->manakai_compat_mode ('quirks');
2997          } elsif (defined $token->{public_identifier}) {
2998            my $pubid = $token->{public_identifier};
2999            $pubid =~ tr/a-z/A-z/;
3000            if ({
3001              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
3002              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
3003              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
3004              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
3005              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
3006              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
3007              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
3008              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
3009              "-//IETF//DTD HTML 2.0//EN" => 1,
3010              "-//IETF//DTD HTML 2.1E//EN" => 1,
3011              "-//IETF//DTD HTML 3.0//EN" => 1,
3012              "-//IETF//DTD HTML 3.0//EN//" => 1,
3013              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
3014              "-//IETF//DTD HTML 3.2//EN" => 1,
3015              "-//IETF//DTD HTML 3//EN" => 1,
3016              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
3017              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
3018              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
3019              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
3020              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
3021              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
3022              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
3023              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
3024              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
3025              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
3026              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
3027              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
3028              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
3029              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
3030              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
3031              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
3032              "-//IETF//DTD HTML STRICT//EN" => 1,
3033              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
3034              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
3035              "-//IETF//DTD HTML//EN" => 1,
3036              "-//IETF//DTD HTML//EN//2.0" => 1,
3037              "-//IETF//DTD HTML//EN//3.0" => 1,
3038              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
3039              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
3040              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
3041              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
3042              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
3043              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
3044              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
3045              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
3046              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
3047              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
3048              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
3049              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
3050              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
3051              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
3052              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
3053              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
3054              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
3055              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
3056              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
3057              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
3058              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
3059              "-//W3C//DTD HTML 3.2//EN" => 1,
3060              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
3061              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
3062              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
3063              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
3064              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
3065              "-//W3C//DTD W3 HTML//EN" => 1,
3066              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
3067              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
3068              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
3069              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
3070              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
3071              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
3072              "HTML" => 1,
3073            }->{$pubid}) {
3074              !!!cp ('t5');
3075              $self->{document}->manakai_compat_mode ('quirks');
3076            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
3077                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
3078              if (defined $token->{system_identifier}) {
3079                !!!cp ('t6');
3080                $self->{document}->manakai_compat_mode ('quirks');
3081              } else {
3082                !!!cp ('t7');
3083                $self->{document}->manakai_compat_mode ('limited quirks');
3084            }            }
3085            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
3086                     $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
3087              !!!cp ('t8');
3088              $self->{document}->manakai_compat_mode ('limited quirks');
3089            } else {
3090              !!!cp ('t9');
3091            }
3092          } else {
3093            !!!cp ('t10');
3094          }
3095          if (defined $token->{system_identifier}) {
3096            my $sysid = $token->{system_identifier};
3097            $sysid =~ tr/A-Z/a-z/;
3098            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3099              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
3100              $self->{document}->manakai_compat_mode ('quirks');
3101              !!!cp ('t11');
3102            } else {
3103              !!!cp ('t12');
3104          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
3105        } else {        } else {
3106          die "$0: $token->{type}: Unknown token";          !!!cp ('t13');
3107        }        }
3108      } # B        
3109          ## Go to the "before html" insertion mode.
3110          !!!next-token;
3111          return;
3112        } elsif ({
3113                  START_TAG_TOKEN, 1,
3114                  END_TAG_TOKEN, 1,
3115                  END_OF_FILE_TOKEN, 1,
3116                 }->{$token->{type}}) {
3117          !!!cp ('t14');
3118          !!!parse-error (type => 'no DOCTYPE', token => $token);
3119          $self->{document}->manakai_compat_mode ('quirks');
3120          ## Go to the "before html" insertion mode.
3121          ## reprocess
3122          !!!ack-later;
3123          return;
3124        } elsif ($token->{type} == CHARACTER_TOKEN) {
3125          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3126            ## Ignore the token
3127    
3128            unless (length $token->{data}) {
3129              !!!cp ('t15');
3130              ## Stay in the insertion mode.
3131              !!!next-token;
3132              redo INITIAL;
3133            } else {
3134              !!!cp ('t16');
3135            }
3136          } else {
3137            !!!cp ('t17');
3138          }
3139    
3140          !!!parse-error (type => 'no DOCTYPE', token => $token);
3141          $self->{document}->manakai_compat_mode ('quirks');
3142          ## Go to the "before html" insertion mode.
3143          ## reprocess
3144          return;
3145        } elsif ($token->{type} == COMMENT_TOKEN) {
3146          !!!cp ('t18');
3147          my $comment = $self->{document}->create_comment ($token->{data});
3148          $self->{document}->append_child ($comment);
3149          
3150          ## Stay in the insertion mode.
3151          !!!next-token;
3152          redo INITIAL;
3153        } else {
3154          die "$0: $token->{type}: Unknown token type";
3155        }
3156      } # INITIAL
3157    
3158      die "$0: _tree_construction_initial: This should be never reached";
3159  } # _tree_construction_initial  } # _tree_construction_initial
3160    
3161  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3162    my $self = shift;    my $self = shift;
3163    
3164      ## NOTE: "before html" insertion mode.
3165        
3166    B: {    B: {
3167        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3168          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3169            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3170          ## Ignore the token          ## Ignore the token
3171          ## Stay in the phase          ## Stay in the insertion mode.
3172          !!!next-token;          !!!next-token;
3173          redo B;          redo B;
3174        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3175            !!!cp ('t20');
3176          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3177          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3178          ## Stay in the phase          ## Stay in the insertion mode.
3179          !!!next-token;          !!!next-token;
3180          redo B;          redo B;
3181        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3182          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3183            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
3184            ## ISSUE: DOM3 Core does not allow Document > Text  
3185            unless (length $token->{data}) {            unless (length $token->{data}) {
3186              ## Stay in the phase              !!!cp ('t21');
3187                ## Stay in the insertion mode.
3188              !!!next-token;              !!!next-token;
3189              redo B;              redo B;
3190              } else {
3191                !!!cp ('t22');
3192            }            }
3193            } else {
3194              !!!cp ('t23');
3195          }          }
3196    
3197            $self->{application_cache_selection}->(undef);
3198    
3199          #          #
3200          } elsif ($token->{type} == START_TAG_TOKEN) {
3201            if ($token->{tag_name} eq 'html') {
3202              my $root_element;
3203              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3204              $self->{document}->append_child ($root_element);
3205              push @{$self->{open_elements}},
3206                  [$root_element, $el_category->{html}];
3207    
3208              if ($token->{attributes}->{manifest}) {
3209                !!!cp ('t24');
3210                $self->{application_cache_selection}
3211                    ->($token->{attributes}->{manifest}->{value});
3212                ## ISSUE: Spec is unclear on relative references.
3213                ## According to Hixie (#whatwg 2008-03-19), it should be
3214                ## resolved against the base URI of the document in HTML
3215                ## or xml:base of the element in XHTML.
3216              } else {
3217                !!!cp ('t25');
3218                $self->{application_cache_selection}->(undef);
3219              }
3220    
3221              !!!nack ('t25c');
3222    
3223              !!!next-token;
3224              return; ## Go to the "before head" insertion mode.
3225            } else {
3226              !!!cp ('t25.1');
3227              #
3228            }
3229        } elsif ({        } elsif ({
3230                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3231                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3232                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3233          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3234          #          #
3235        } else {        } else {
3236          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3237        }        }
3238        my $root_element; !!!create-element ($root_element, 'html');  
3239        $self->{document}->append_child ($root_element);      my $root_element;
3240        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3241        #$phase = 'main';      $self->{document}->append_child ($root_element);
3242        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3243        #redo B;  
3244        return;      $self->{application_cache_selection}->(undef);
3245    
3246        ## NOTE: Reprocess the token.
3247        !!!ack-later;
3248        return; ## Go to the "before head" insertion mode.
3249    
3250        ## ISSUE: There is an issue in the spec
3251    } # B    } # B
3252    
3253      die "$0: _tree_construction_root_element: This should never be reached";
3254  } # _tree_construction_root_element  } # _tree_construction_root_element
3255    
3256  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1780  sub _reset_insertion_mode ($) { Line 3265  sub _reset_insertion_mode ($) {
3265            
3266      ## Step 3      ## Step 3
3267      S3: {      S3: {
3268        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3269        if (defined $self->{inner_html_node}) {          $last = 1;
3270          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
3271              $self->{inner_html_node}->[1] eq 'th') {            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
3272            #              !!!cp ('t27');
3273          } else {              #
3274            $node = $self->{inner_html_node};            } else {
3275                !!!cp ('t28');
3276                $node = $self->{inner_html_node};
3277              }
3278          }          }
3279        }        }
3280            
3281        ## Step 4..13      ## Step 4..14
3282        my $new_mode = {      my $new_mode;
3283                        select => 'in select',      if ($node->[1] & FOREIGN_EL) {
3284                        td => 'in cell',        ## NOTE: Strictly spaking, the line below only applies to MathML and
3285                        th => 'in cell',        ## SVG elements.  Currently the HTML syntax supports only MathML and
3286                        tr => 'in row',        ## SVG elements as foreigners.
3287                        tbody => 'in table body',        $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3288                        thead => 'in table head',        ## ISSUE: What is set as the secondary insertion mode?
3289                        tfoot => 'in table foot',      } else {
3290                        caption => 'in caption',        $new_mode = {
3291                        colgroup => 'in column group',                        select => IN_SELECT_IM,
3292                        table => 'in table',                        ## NOTE: |option| and |optgroup| do not set
3293                        head => 'in body', # not in head!                        ## insertion mode to "in select" by themselves.
3294                        body => 'in body',                        td => IN_CELL_IM,
3295                        frameset => 'in frameset',                        th => IN_CELL_IM,
3296                       }->{$node->[1]};                        tr => IN_ROW_IM,
3297        $self->{insertion_mode} = $new_mode and return if defined $new_mode;                        tbody => IN_TABLE_BODY_IM,
3298                          thead => IN_TABLE_BODY_IM,
3299                          tfoot => IN_TABLE_BODY_IM,
3300                          caption => IN_CAPTION_IM,
3301                          colgroup => IN_COLUMN_GROUP_IM,
3302                          table => IN_TABLE_IM,
3303                          head => IN_BODY_IM, # not in head!
3304                          body => IN_BODY_IM,
3305                          frameset => IN_FRAMESET_IM,
3306                         }->{$node->[0]->manakai_local_name};
3307        }
3308        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3309                
3310        ## Step 14        ## Step 15
3311        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3312          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3313            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3314              $self->{insertion_mode} = BEFORE_HEAD_IM;
3315          } else {          } else {
3316            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3317              !!!cp ('t30');
3318              $self->{insertion_mode} = AFTER_HEAD_IM;
3319          }          }
3320          return;          return;
3321          } else {
3322            !!!cp ('t31');
3323        }        }
3324                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3325        ## Step 16        ## Step 16
3326          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3327          
3328          ## Step 17
3329        $i--;        $i--;
3330        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3331                
3332        ## Step 17        ## Step 18
3333        redo S3;        redo S3;
3334      } # S3      } # S3
3335    
3336      die "$0: _reset_insertion_mode: This line should never be reached";
3337  } # _reset_insertion_mode  } # _reset_insertion_mode
3338    
3339  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3340    my $self = shift;    my $self = shift;
3341    
   my $phase = 'main';  
   
3342    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3343    
3344    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1851  sub _tree_construction_main ($) { Line 3355  sub _tree_construction_main ($) {
3355      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3356      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3357        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3358            !!!cp ('t32');
3359          return;          return;
3360        }        }
3361      }      }
# Line 1865  sub _tree_construction_main ($) { Line 3370  sub _tree_construction_main ($) {
3370    
3371        ## Step 6        ## Step 6
3372        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3373            !!!cp ('t33_1');
3374          #          #
3375        } else {        } else {
3376          my $in_open_elements;          my $in_open_elements;
3377          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3378            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3379                !!!cp ('t33');
3380              $in_open_elements = 1;              $in_open_elements = 1;
3381              last OE;              last OE;
3382            }            }
3383          }          }
3384          if ($in_open_elements) {          if ($in_open_elements) {
3385              !!!cp ('t34');
3386            #            #
3387          } else {          } else {
3388              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3389              !!!cp ('t35');
3390            redo S4;            redo S4;
3391          }          }
3392        }        }
# Line 1899  sub _tree_construction_main ($) { Line 3409  sub _tree_construction_main ($) {
3409    
3410        ## Step 11        ## Step 11
3411        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3412            !!!cp ('t36');
3413          ## Step 7'          ## Step 7'
3414          $i++;          $i++;
3415          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3416                    
3417          redo S7;          redo S7;
3418        }        }
3419    
3420          !!!cp ('t37');
3421      } # S7      } # S7
3422    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3423    
3424    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3425      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3426        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3427            !!!cp ('t38');
3428          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3429          return;          return;
3430        }        }
3431      }      }
3432    
3433        !!!cp ('t39');
3434    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3435    
3436    my $style_start_tag = sub {    my $insert;
3437      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});  
3438      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
3439      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
3440       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
3441        ->append_child ($style_el);      ## Step 1
3442      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
3443                      my $el;
3444        !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3445    
3446        ## Step 2
3447        $insert->($el);
3448    
3449        ## Step 3
3450        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3451        delete $self->{escape}; # MUST
3452    
3453        ## Step 4
3454      my $text = '';      my $text = '';
3455        !!!nack ('t40.1');
3456      !!!next-token;      !!!next-token;
3457      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3458          !!!cp ('t40');
3459        $text .= $token->{data};        $text .= $token->{data};
3460        !!!next-token;        !!!next-token;
3461      } # stop if non-character token or tokenizer stops tokenising      }
3462    
3463        ## Step 5
3464      if (length $text) {      if (length $text) {
3465        $style_el->manakai_append_text ($text);        !!!cp ('t41');
3466          my $text = $self->{document}->create_text_node ($text);
3467          $el->append_child ($text);
3468      }      }
3469        
3470      $self->{content_model_flag} = 'PCDATA';      ## Step 6
3471                      $self->{content_model} = PCDATA_CONTENT_MODEL;
3472      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
3473        ## Step 7
3474        if ($token->{type} == END_TAG_TOKEN and
3475            $token->{tag_name} eq $start_tag_name) {
3476          !!!cp ('t42');
3477        ## Ignore the token        ## Ignore the token
3478      } else {      } else {
3479        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
3480        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3481            !!!cp ('t43');
3482            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3483          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3484            !!!cp ('t44');
3485            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3486          } else {
3487            die "$0: $content_model_flag in parse_rcdata";
3488          }
3489      }      }
3490      !!!next-token;      !!!next-token;
3491    }; # $style_start_tag    }; # $parse_rcdata
3492    
3493    my $script_start_tag = sub {    my $script_start_tag = sub () {
3494      my $script_el;      my $script_el;
3495      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3496      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3497    
3498      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3499        delete $self->{escape}; # MUST
3500            
3501      my $text = '';      my $text = '';
3502        !!!nack ('t45.1');
3503      !!!next-token;      !!!next-token;
3504      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3505          !!!cp ('t45');
3506        $text .= $token->{data};        $text .= $token->{data};
3507        !!!next-token;        !!!next-token;
3508      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3509      if (length $text) {      if (length $text) {
3510          !!!cp ('t46');
3511        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3512      }      }
3513                                
3514      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3515    
3516      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3517          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3518          !!!cp ('t47');
3519        ## Ignore the token        ## Ignore the token
3520      } else {      } else {
3521        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3522          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3523        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3524        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3525      }      }
3526            
3527      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3528          !!!cp ('t49');
3529        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3530      } else {      } else {
3531          !!!cp ('t50');
3532        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3533        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3534          
3535        (($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);  
3536                
3537        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
3538                
# Line 1991  sub _tree_construction_main ($) { Line 3542  sub _tree_construction_main ($) {
3542      !!!next-token;      !!!next-token;
3543    }; # $script_start_tag    }; # $script_start_tag
3544    
3545      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3546      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3547      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3548    
3549    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3550      my $tag_name = shift;      my $end_tag_token = shift;
3551        my $tag_name = $end_tag_token->{tag_name};
3552    
3553        ## NOTE: The adoption agency algorithm (AAA).
3554    
3555      FET: {      FET: {
3556        ## Step 1        ## Step 1
3557        my $formatting_element;        my $formatting_element;
3558        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3559        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3560          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3561              !!!cp ('t52');
3562              last AFE;
3563            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3564                         eq $tag_name) {
3565              !!!cp ('t51');
3566            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3567            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3568            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3569          }          }
3570        } # AFE        } # AFE
3571        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3572          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3573            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3574          ## Ignore the token          ## Ignore the token
3575          !!!next-token;          !!!next-token;
3576          return;          return;
# Line 2020  sub _tree_construction_main ($) { Line 3582  sub _tree_construction_main ($) {
3582          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3583          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3584            if ($in_scope) {            if ($in_scope) {
3585                !!!cp ('t54');
3586              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3587              last INSCOPE;              last INSCOPE;
3588            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3589              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3590                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3591                                token => $end_tag_token);
3592              ## Ignore the token              ## Ignore the token
3593              !!!next-token;              !!!next-token;
3594              return;              return;
3595            }            }
3596          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3597                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3598            $in_scope = 0;            $in_scope = 0;
3599          }          }
3600        } # INSCOPE        } # INSCOPE
3601        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3602          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3603            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3604                            token => $end_tag_token);
3605          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3606          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3607          return;          return;
3608        }        }
3609        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3610          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3611            !!!parse-error (type => 'not closed',
3612                            value => $self->{open_elements}->[-1]->[0]
3613                                ->manakai_local_name,
3614                            token => $end_tag_token);
3615        }        }
3616                
3617        ## Step 2        ## Step 2
# Line 2050  sub _tree_construction_main ($) { Line 3619  sub _tree_construction_main ($) {
3619        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3620        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3621          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3622          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3623              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3624              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3625               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3626              !!!cp ('t59');
3627            $furthest_block = $node;            $furthest_block = $node;
3628            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3629          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3630              !!!cp ('t60');
3631            last OE;            last OE;
3632          }          }
3633        } # OE        } # OE
3634                
3635        ## Step 3        ## Step 3
3636        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3637            !!!cp ('t61');
3638          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3639          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3640          !!!next-token;          !!!next-token;
# Line 2075  sub _tree_construction_main ($) { Line 3647  sub _tree_construction_main ($) {
3647        ## Step 5        ## Step 5
3648        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3649        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3650            !!!cp ('t62');
3651          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3652        }        }
3653                
# Line 2097  sub _tree_construction_main ($) { Line 3670  sub _tree_construction_main ($) {
3670          S7S2: {          S7S2: {
3671            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3672              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3673                  !!!cp ('t63');
3674                $node_i_in_active = $_;                $node_i_in_active = $_;
3675                last S7S2;                last S7S2;
3676              }              }
# Line 2110  sub _tree_construction_main ($) { Line 3684  sub _tree_construction_main ($) {
3684                    
3685          ## Step 4          ## Step 4
3686          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3687              !!!cp ('t64');
3688            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3689          }          }
3690                    
3691          ## Step 5          ## Step 5
3692          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3693              !!!cp ('t65');
3694            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3695            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3696            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2132  sub _tree_construction_main ($) { Line 3708  sub _tree_construction_main ($) {
3708        } # S7          } # S7  
3709                
3710        ## Step 8        ## Step 8
3711        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3712            my $foster_parent_element;
3713            my $next_sibling;
3714            OE: for (reverse 0..$#{$self->{open_elements}}) {
3715              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3716                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3717                                 if (defined $parent and $parent->node_type == 1) {
3718                                   !!!cp ('t65.1');
3719                                   $foster_parent_element = $parent;
3720                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3721                                 } else {
3722                                   !!!cp ('t65.2');
3723                                   $foster_parent_element
3724                                     = $self->{open_elements}->[$_ - 1]->[0];
3725                                 }
3726                                 last OE;
3727                               }
3728                             } # OE
3729                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3730                               unless defined $foster_parent_element;
3731            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3732            $open_tables->[-1]->[1] = 1; # tainted
3733          } else {
3734            !!!cp ('t65.3');
3735            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3736          }
3737                
3738        ## Step 9        ## Step 9
3739        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2149  sub _tree_construction_main ($) { Line 3750  sub _tree_construction_main ($) {
3750        my $i;        my $i;
3751        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3752          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3753              !!!cp ('t66');
3754            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3755            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3756          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3757              !!!cp ('t67');
3758            $i = $_;            $i = $_;
3759          }          }
3760        } # AFE        } # AFE
# Line 2161  sub _tree_construction_main ($) { Line 3764  sub _tree_construction_main ($) {
3764        undef $i;        undef $i;
3765        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3766          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3767              !!!cp ('t68');
3768            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3769            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3770          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3771              !!!cp ('t69');
3772            $i = $_;            $i = $_;
3773          }          }
3774        } # OE        } # OE
# Line 2174  sub _tree_construction_main ($) { Line 3779  sub _tree_construction_main ($) {
3779      } # FET      } # FET
3780    }; # $formatting_end_tag    }; # $formatting_end_tag
3781    
3782    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3783      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3784    }; # $insert_to_current    }; # $insert_to_current
3785    
3786    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3787                         my $child = shift;      my $child = shift;
3788                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3789                              table => 1, tbody => 1, tfoot => 1,        # MUST
3790                              thead => 1, tr => 1,        my $foster_parent_element;
3791                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3792                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3793                           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') {  
3794                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3795                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3796                                   !!!cp ('t70');
3797                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3798                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3799                               } else {                               } else {
3800                                   !!!cp ('t71');
3801                                 $foster_parent_element                                 $foster_parent_element
3802                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3803                               }                               }
# Line 2204  sub _tree_construction_main ($) { Line 3808  sub _tree_construction_main ($) {
3808                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3809                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3810                             ($child, $next_sibling);                             ($child, $next_sibling);
3811                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3812                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3813                         }        !!!cp ('t72');
3814          $self->{open_elements}->[-1]->[0]->append_child ($child);
3815        }
3816    }; # $insert_to_foster    }; # $insert_to_foster
3817    
3818    my $in_body = sub {    B: while (1) {
3819      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
3820      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
3821        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3822          $script_start_tag->();        ## Ignore the token
3823          return;        ## Stay in the phase
3824        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
3825          $style_start_tag->();        next B;
3826          return;      } elsif ($token->{type} == START_TAG_TOKEN and
3827        } elsif ({               $token->{tag_name} eq 'html') {
3828                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3829                 }->{$token->{tag_name}}) {          !!!cp ('t79');
3830          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html:html', token => $token);
3831          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
3832          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3833          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
3834          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html:html', token => $token);
3835            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3836          } else {        } else {
3837            $insert->($el);          !!!cp ('t81');
3838          }        }
3839            
3840          !!!next-token;        !!!cp ('t82');
3841          return;        !!!parse-error (type => 'not first start tag', token => $token);
3842        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
3843          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
3844          ## NOTE: There is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3845          my $title_el;            !!!cp ('t84');
3846          !!!create-element ($title_el, 'title', $token->{attributes});            $top_el->set_attribute_ns
3847          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              (undef, [undef, $attr_name],
3848            ->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  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
3849          }          }
3850                    }
3851          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!nack ('t84.1');
3852                    !!!next-token;
3853          next B;
3854        } elsif ($token->{type} == COMMENT_TOKEN) {
3855          my $comment = $self->{document}->create_comment ($token->{data});
3856          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3857            !!!cp ('t85');
3858            $self->{document}->append_child ($comment);
3859          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3860            !!!cp ('t86');
3861            $self->{open_elements}->[0]->[0]->append_child ($comment);
3862          } else {
3863            !!!cp ('t87');
3864            $self->{open_elements}->[-1]->[0]->append_child ($comment);
3865          }
3866          !!!next-token;
3867          next B;
3868        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3869          if ($token->{type} == CHARACTER_TOKEN) {
3870            !!!cp ('t87.1');
3871            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3872          !!!next-token;          !!!next-token;
3873          return;          next B;
3874        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3875          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3876            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3877            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3878              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
3879                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3880              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
3881              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
3882              $formatting_end_tag->($token->{tag_name});            #
3883                        } elsif ({
3884              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3885                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3886                  splice @$active_formatting_elements, $_, 1;                    embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3887                  last AFE2;                    h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3888                }                    li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3889              } # AFE2                    ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3890              OE: for (reverse 0..$#{$self->{open_elements}}) {                    sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3891                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    var => 1,
3892                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
3893                  last OE;            !!!cp ('t87.2');
3894                }            !!!parse-error (type => 'not closed',
3895              } # OE                            value => $self->{open_elements}->[-1]->[0]
3896              last AFE;                                ->manakai_local_name,
3897            } elsif ($node->[0] eq '#marker') {                            token => $token);
3898              last AFE;  
3899              pop @{$self->{open_elements}}
3900                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3901    
3902              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3903              ## Reprocess.
3904              next B;
3905            } else {
3906              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3907              my $tag_name = $token->{tag_name};
3908              if ($nsuri eq $SVG_NS) {
3909                $tag_name = {
3910                   altglyph => 'altGlyph',
3911                   altglyphdef => 'altGlyphDef',
3912                   altglyphitem => 'altGlyphItem',
3913                   animatecolor => 'animateColor',
3914                   animatemotion => 'animateMotion',
3915                   animatetransform => 'animateTransform',
3916                   clippath => 'clipPath',
3917                   feblend => 'feBlend',
3918                   fecolormatrix => 'feColorMatrix',
3919                   fecomponenttransfer => 'feComponentTransfer',
3920                   fecomposite => 'feComposite',
3921                   feconvolvematrix => 'feConvolveMatrix',
3922                   fediffuselighting => 'feDiffuseLighting',
3923                   fedisplacementmap => 'feDisplacementMap',
3924                   fedistantlight => 'feDistantLight',
3925                   feflood => 'feFlood',
3926                   fefunca => 'feFuncA',
3927                   fefuncb => 'feFuncB',
3928                   fefuncg => 'feFuncG',
3929                   fefuncr => 'feFuncR',
3930                   fegaussianblur => 'feGaussianBlur',
3931                   feimage => 'feImage',
3932                   femerge => 'feMerge',
3933                   femergenode => 'feMergeNode',
3934                   femorphology => 'feMorphology',
3935                   feoffset => 'feOffset',
3936                   fepointlight => 'fePointLight',
3937                   fespecularlighting => 'feSpecularLighting',
3938                   fespotlight => 'feSpotLight',
3939                   fetile => 'feTile',
3940                   feturbulence => 'feTurbulence',
3941                   foreignobject => 'foreignObject',
3942                   glyphref => 'glyphRef',
3943                   lineargradient => 'linearGradient',
3944                   radialgradient => 'radialGradient',
3945                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
3946                   textpath => 'textPath',  
3947                }->{$tag_name} || $tag_name;
3948            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
3949    
3950          !!!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];  
3951    
3952          !!!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', ''];  
3953    
3954          !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
3955          return;  
3956        } elsif ($token->{tag_name} eq 'marquee' or            if ($self->{self_closing}) {
3957                 $token->{tag_name} eq 'object') {              pop @{$self->{open_elements}};
3958          $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,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: $self->{form_element} if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
           
         $insert->($el);  
           
         my $text = '';  
         if ($token->{tag_name} eq 'textarea') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3959            } else {            } else {
3960              !!!parse-error (type => 'in CDATA:#'.$token->{type});              !!!cp ('t87.4');
3961            }            }
3962            ## ISSUE: And ignore?  
3963              !!!next-token;
3964              next B;
3965          }          }
3966          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
3967          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
3968        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
3969          $reconstruct_active_formatting_elements->($insert_to_current);          #
3970                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3971          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          ## NOTE: "using the rules for secondary insertion mode" then "continue"
3972                    !!!cp ('t87.6');
3973          $self->{insertion_mode} = 'in select';          #
3974          !!!next-token;          ## TODO: ...
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
3975        } else {        } else {
3976          $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;  
3977        }        }
3978      } elsif ($token->{type} eq 'end tag') {      }
3979        if ($token->{tag_name} eq 'body') {  
3980          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      if ($self->{insertion_mode} & HEAD_IMS) {
3981            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
3982            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3983              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3984            }              !!!cp ('t88.2');
3985            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3986            !!!next-token;            } else {
3987            return;              !!!cp ('t88.1');
3988          } else {              ## Ignore the token.
3989            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
3990            ## Ignore the token              next B;
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 form => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
3991            }            }
3992          } # INSCOPE            unless (length $token->{data}) {
3993                        !!!cp ('t88');
3994          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
3995            !!!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;  
3996            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3997          }          }
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
 ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
3998    
3999          ## Step 2          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4000          S2: {            !!!cp ('t89');
4001            if ($node->[1] eq $token->{tag_name}) {            ## As if <head>
4002              ## Step 1            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4003              ## generate implied end tags            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4004              if ({            push @{$self->{open_elements}},
4005                   dd => 1, dt => 1, li => 1, p => 1,                [$self->{head_element}, $el_category->{head}];
4006                   td => 1, th => 1, tr => 1,  
4007                  }->{$self->{open_elements}->[-1]->[1]}) {            ## Reprocess in the "in head" insertion mode...
4008                !!!back-token;            pop @{$self->{open_elements}};
4009                $token = {type => 'end tag',  
4010                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST            ## Reprocess in the "after head" insertion mode...
4011                return;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4012              }            !!!cp ('t90');
4013                      ## As if </noscript>
4014              ## Step 2            pop @{$self->{open_elements}};
4015              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {            !!!parse-error (type => 'in noscript:#character', token => $token);
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
   
             !!!next-token;  
             last S2;  
           } else {  
             ## Step 3  
             if (not $formatting_category->{$node->[1]} and  
                 #not $phrasing_category->{$node->[1]} and  
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'not closed:'.$node->[1]);  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
4016                        
4017            ## Step 4            ## Reprocess in the "in head" insertion mode...
4018            $node_i--;            ## As if </head>
4019            $node = $self->{open_elements}->[$node_i];            pop @{$self->{open_elements}};
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
4020    
4021    B: {            ## Reprocess in the "after head" insertion mode...
4022      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4023        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
4024          !!!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]);  
         }  
4025    
4026          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4027          last B;          } else {
4028              !!!cp ('t92');
4029            }
4030    
4031          ## ISSUE: There is an issue in the spec.          ## "after head" insertion mode
4032        } else {          ## As if <body>
4033          if ($self->{insertion_mode} eq 'before head') {          !!!insert-element ('body',, $token);
4034            if ($token->{type} eq 'character') {          $self->{insertion_mode} = IN_BODY_IM;
4035              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## reprocess
4036                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          next B;
4037                unless (length $token->{data}) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4038                  !!!next-token;          if ($token->{tag_name} eq 'head') {
4039                  redo B;            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4040                }              !!!cp ('t93');
4041              }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4042              ## As if <head>              $self->{open_elements}->[-1]->[0]->append_child
4043              !!!create-element ($self->{head_element}, 'head');                  ($self->{head_element});
4044              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              push @{$self->{open_elements}},
4045              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  [$self->{head_element}, $el_category->{head}];
4046              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = IN_HEAD_IM;
4047              ## 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);  
4048              !!!next-token;              !!!next-token;
4049              redo B;              next B;
4050            } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4051              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              !!!cp ('t94');
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
4052              #              #
4053            } elsif ($token->{type} eq 'comment') {            } else {
4054              my $comment = $self->{document}->create_comment ($token->{data});              !!!cp ('t95');
4055              $self->{open_elements}->[-1]->[0]->append_child ($comment);              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4056                ## Ignore the token
4057                !!!nack ('t95.1');
4058              !!!next-token;              !!!next-token;
4059              redo B;              next B;
4060            } elsif ($token->{type} eq 'start tag') {            }
4061              if ($token->{tag_name} eq 'title') {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4062                ## NOTE: There is an "as if in head" code clone            !!!cp ('t96');
4063                my $title_el;            ## As if <head>
4064                !!!create-element ($title_el, 'title', $token->{attributes});            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4065                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4066                  ->append_child ($title_el);            push @{$self->{open_elements}},
4067                $self->{content_model_flag} = 'RCDATA';                [$self->{head_element}, $el_category->{head}];
4068    
4069                my $text = '';            $self->{insertion_mode} = IN_HEAD_IM;
4070                !!!next-token;            ## Reprocess in the "in head" insertion mode...
4071                while ($token->{type} eq 'character') {          } else {
4072                  $text .= $token->{data};            !!!cp ('t97');
4073                  !!!next-token;          }
4074                }  
4075                if (length $text) {              if ($token->{tag_name} eq 'base') {
4076                  $title_el->manakai_append_text ($text);                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4077                }                  !!!cp ('t98');
4078                                  ## As if </noscript>
4079                $self->{content_model_flag} = 'PCDATA';                  pop @{$self->{open_elements}};
4080                    !!!parse-error (type => 'in noscript:base', token => $token);
4081                                
4082                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
4083                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
4084                } else {                } else {
4085                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
4086                }                }
               !!!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);  
4087    
4088                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
4089                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4090              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
4091                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4092                ## Ignore the token                  push @{$self->{open_elements}},
4093                !!!next-token;                      [$self->{head_element}, $el_category->{head}];
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'head') {  
               if ($self->{open_elements}->[-1]->[1] eq 'head') {  
                 pop @{$self->{open_elements}};  
4094                } else {                } else {
4095                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
4096                }                }
4097                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4098                !!!next-token;                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4099                redo B;                pop @{$self->{open_elements}} # <head>
4100              } elsif ($token->{tag_name} eq 'html') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4101                #                !!!nack ('t101.1');
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
4102                !!!next-token;                !!!next-token;
4103                redo B;                next B;
4104              }              } elsif ($token->{tag_name} eq 'link') {
4105            } else {                ## NOTE: There is a "as if in head" code clone.
4106              #                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4107            }                  !!!cp ('t102');
4108                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4109            if ($self->{open_elements}->[-1]->[1] eq 'head') {                  push @{$self->{open_elements}},
4110              ## As if </head>                      [$self->{head_element}, $el_category->{head}];
4111              pop @{$self->{open_elements}};                } else {
4112            }                  !!!cp ('t103');
           $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;  
4113                }                }
4114              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4115                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4116              #                pop @{$self->{open_elements}} # <head>
4117            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4118              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';  
4119                !!!next-token;                !!!next-token;
4120                redo B;                next B;
4121              } elsif ($token->{tag_name} eq 'frameset') {              } elsif ($token->{tag_name} eq 'meta') {
4122                !!!insert-element ('frameset', $token->{attributes});                ## NOTE: There is a "as if in head" code clone.
4123                $self->{insertion_mode} = 'in frameset';                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4124                !!!next-token;                  !!!cp ('t104');
4125                redo B;                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4126              } elsif ({                  push @{$self->{open_elements}},
4127                        base => 1, link => 1, meta => 1,                      [$self->{head_element}, $el_category->{head}];
4128                        script => 1, style => 1, title => 1,                } else {
4129                       }->{$token->{tag_name}}) {                  !!!cp ('t105');
               !!!parse-error (type => 'after head:'.$token->{tag_name});  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } 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;  
4130                }                }
4131              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4132                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
             !!!parse-error (type => 'in table:#character');  
4133    
4134              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
4135              ## ISSUE: Spec says that "whenever a node would be inserted                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
4136              ## into the current node" while characters might not be                    !!!cp ('t106');
4137              ## result in a new Text node.                    $self->{change_encoding}
4138              $reconstruct_active_formatting_elements->($insert_to_foster);                        ->($self, $token->{attributes}->{charset}->{value},
4139                                         $token);
4140              if ({                    
4141                   table => 1, tbody => 1, tfoot => 1,                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4142                   thead => 1, tr => 1,                        ->set_user_data (manakai_has_reference =>
4143                  }->{$self->{open_elements}->[-1]->[1]}) {                                             $token->{attributes}->{charset}
4144                # MUST                                                 ->{has_reference});
4145                my $foster_parent_element;                  } elsif ($token->{attributes}->{content}) {
4146                my $next_sibling;                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4147                my $prev_sibling;                    if ($token->{attributes}->{content}->{value}
4148                OE: for (reverse 0..$#{$self->{open_elements}}) {                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4149                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                            [\x09-\x0D\x20]*=
4150                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4151                    if (defined $parent and $parent->node_type == 1) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4152                      $foster_parent_element = $parent;                      !!!cp ('t107');
4153                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $self->{change_encoding}
4154                      $prev_sibling = $next_sibling->previous_sibling;                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4155                               $token);
4156                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4157                            ->set_user_data (manakai_has_reference =>
4158                                                 $token->{attributes}->{content}
4159                                                       ->{has_reference});
4160                    } else {                    } else {
4161                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
4162                    }                    }
                   last OE;  
4163                  }                  }
               } # 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});  
4164                } else {                } else {
4165                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
4166                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
4167                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4168                }                        ->set_user_data (manakai_has_reference =>
4169              } else {                                             $token->{attributes}->{charset}
4170                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
4171              }                  }
4172                                if ($token->{attributes}->{content}) {
4173              !!!next-token;                    !!!cp ('t110');
4174              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4175            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
4176              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
4177              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
4178              !!!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}};  
4179                }                }
4180    
4181                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
4182                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4183                  !!!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}};  
4184                !!!next-token;                !!!next-token;
4185                redo B;                next B;
4186              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
4187                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4188                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
4189                       }->{$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]);  
4190                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4191                    !!!parse-error (type => 'in noscript:title', token => $token);
4192                  
4193                    $self->{insertion_mode} = IN_HEAD_IM;
4194                    ## Reprocess in the "in head" insertion mode...
4195                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4196                    !!!cp ('t112');
4197                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4198                    push @{$self->{open_elements}},
4199                        [$self->{head_element}, $el_category->{head}];
4200                  } else {
4201                    !!!cp ('t113');
4202                }                }
4203    
4204                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
4205                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
4206                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
4207                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4208                redo B;                pop @{$self->{open_elements}} # <head>
4209              } elsif ($token->{tag_name} eq 'table') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4210                ## NOTE: There are code clones for this "table in table"                next B;
4211                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } elsif ($token->{tag_name} eq 'style') {
4212                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4213                ## As if </table>                ## insertion mode IN_HEAD_IM)
4214                ## have a table element in table scope                ## NOTE: There is a "as if in head" code clone.
4215                my $i;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4216                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!cp ('t114');
4217                  my $node = $self->{open_elements}->[$_];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4218                  if ($node->[1] eq 'table') {                  push @{$self->{open_elements}},
4219                    $i = $_;                      [$self->{head_element}, $el_category->{head}];
4220                    last INSCOPE;                } else {
4221                  } elsif ({                  !!!cp ('t115');
4222                            table => 1, html => 1,                }
4223                           }->{$node->[1]}) {                $parse_rcdata->(CDATA_CONTENT_MODEL);
4224                    last INSCOPE;                pop @{$self->{open_elements}} # <head>
4225                  }                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4226                } # INSCOPE                next B;
4227                unless (defined $i) {              } elsif ($token->{tag_name} eq 'noscript') {
4228                  !!!parse-error (type => 'unmatched end tag:table');                if ($self->{insertion_mode} == IN_HEAD_IM) {
4229                  ## Ignore tokens </table><table>                  !!!cp ('t116');
4230                    ## NOTE: and scripting is disalbed
4231                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4232                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4233                    !!!nack ('t116.1');
4234                  !!!next-token;                  !!!next-token;
4235                  redo B;                  next B;
4236                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4237                    !!!cp ('t117');
4238                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4239                    ## Ignore the token
4240                    !!!nack ('t117.1');
4241                    !!!next-token;
4242                    next B;
4243                  } else {
4244                    !!!cp ('t118');
4245                    #
4246                }                }
4247                } elsif ($token->{tag_name} eq 'script') {
4248                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4249                    !!!cp ('t119');
4250                    ## As if </noscript>
4251                    pop @{$self->{open_elements}};
4252                    !!!parse-error (type => 'in noscript:script', token => $token);
4253                                
4254                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
4255                if ({                  ## Reprocess in the "in head" insertion mode...
4256                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4257                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
4258                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4259                  !!!back-token; # <table>                  push @{$self->{open_elements}},
4260                  $token = {type => 'end tag', tag_name => 'table'};                      [$self->{head_element}, $el_category->{head}];
4261                  !!!back-token;                } else {
4262                  $token = {type => 'end tag',                  !!!cp ('t121');
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4263                }                }
4264    
4265                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
4266                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
4267                  pop @{$self->{open_elements}} # <head>
4268                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4269                  next B;
4270                } elsif ($token->{tag_name} eq 'body' or
4271                         $token->{tag_name} eq 'frameset') {
4272                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4273                    !!!cp ('t122');
4274                    ## As if </noscript>
4275                    pop @{$self->{open_elements}};
4276                    !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4277                    
4278                    ## Reprocess in the "in head" insertion mode...
4279                    ## As if </head>
4280                    pop @{$self->{open_elements}};
4281                    
4282                    ## Reprocess in the "after head" insertion mode...
4283                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4284                    !!!cp ('t124');
4285                    pop @{$self->{open_elements}};
4286                    
4287                    ## Reprocess in the "after head" insertion mode...
4288                  } else {
4289                    !!!cp ('t125');
4290                }                }
4291    
4292                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
4293                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4294                  if ($token->{tag_name} eq 'body') {
4295                    !!!cp ('t126');
4296                    $self->{insertion_mode} = IN_BODY_IM;
4297                  } elsif ($token->{tag_name} eq 'frameset') {
4298                    !!!cp ('t127');
4299                    $self->{insertion_mode} = IN_FRAMESET_IM;
4300                  } else {
4301                    die "$0: tag name: $self->{tag_name}";
4302                  }
4303                  !!!nack ('t127.1');
4304                  !!!next-token;
4305                  next B;
4306                } else {
4307                  !!!cp ('t128');
4308                  #
4309                }
4310    
4311                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4312                  !!!cp ('t129');
4313                  ## As if </noscript>
4314                  pop @{$self->{open_elements}};
4315                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4316                  
4317                  ## Reprocess in the "in head" insertion mode...
4318                  ## As if </head>
4319                  pop @{$self->{open_elements}};
4320    
4321                ## reprocess                ## Reprocess in the "after head" insertion mode...
4322                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4323                  !!!cp ('t130');
4324                  ## As if </head>
4325                  pop @{$self->{open_elements}};
4326    
4327                  ## Reprocess in the "after head" insertion mode...
4328              } else {              } else {
4329                #                !!!cp ('t131');
4330              }              }
4331            } elsif ($token->{type} eq 'end tag') {  
4332              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
4333                ## have a table element in table scope              ## As if <body>
4334                my $i;              !!!insert-element ('body',, $token);
4335                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
4336                  my $node = $self->{open_elements}->[$_];              ## reprocess
4337                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
4338                    $i = $_;              next B;
4339                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
4340                  } elsif ({              if ($token->{tag_name} eq 'head') {
4341                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4342                           }->{$node->[1]}) {                  !!!cp ('t132');
4343                    last INSCOPE;                  ## As if <head>
4344                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4345                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4346                unless (defined $i) {                  push @{$self->{open_elements}},
4347                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
4348    
4349                    ## Reprocess in the "in head" insertion mode...
4350                    pop @{$self->{open_elements}};
4351                    $self->{insertion_mode} = AFTER_HEAD_IM;
4352                    !!!next-token;
4353                    next B;
4354                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4355                    !!!cp ('t133');
4356                    ## As if </noscript>
4357                    pop @{$self->{open_elements}};
4358                    !!!parse-error (type => 'in noscript:/head', token => $token);
4359                    
4360                    ## Reprocess in the "in head" insertion mode...
4361                    pop @{$self->{open_elements}};
4362                    $self->{insertion_mode} = AFTER_HEAD_IM;
4363                    !!!next-token;
4364                    next B;
4365                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4366                    !!!cp ('t134');
4367                    pop @{$self->{open_elements}};
4368                    $self->{insertion_mode} = AFTER_HEAD_IM;
4369                    !!!next-token;
4370                    next B;
4371                  } else {
4372                    !!!cp ('t135');
4373                    #
4374                  }
4375                } elsif ($token->{tag_name} eq 'noscript') {
4376                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4377                    !!!cp ('t136');
4378                    pop @{$self->{open_elements}};
4379                    $self->{insertion_mode} = IN_HEAD_IM;
4380                    !!!next-token;
4381                    next B;
4382                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4383                    !!!cp ('t137');
4384                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4385                    ## Ignore the token ## ISSUE: An issue in the spec.
4386                    !!!next-token;
4387                    next B;
4388                  } else {
4389                    !!!cp ('t138');
4390                    #
4391                  }
4392                } elsif ({
4393                          body => 1, html => 1,
4394                         }->{$token->{tag_name}}) {
4395                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4396                    !!!cp ('t139');
4397                    ## As if <head>
4398                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4399                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4400                    push @{$self->{open_elements}},
4401                        [$self->{head_element}, $el_category->{head}];
4402    
4403                    $self->{insertion_mode} = IN_HEAD_IM;
4404                    ## Reprocess in the "in head" insertion mode...
4405                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4406                    !!!cp ('t140');
4407                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4408                  ## Ignore the token                  ## Ignore the token
4409                  !!!next-token;                  !!!next-token;
4410                  redo B;                  next B;
4411                  } else {
4412                    !!!cp ('t141');
4413                }                }
4414                                
4415                ## generate implied end tags                #
4416                if ({              } elsif ({
4417                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
4418                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
4419                    }->{$self->{open_elements}->[-1]->[1]}) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4420                  !!!back-token;                  !!!cp ('t142');
4421                  $token = {type => 'end tag',                  ## As if <head>
4422                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4423                  redo B;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4424                    push @{$self->{open_elements}},
4425                        [$self->{head_element}, $el_category->{head}];
4426    
4427                    $self->{insertion_mode} = IN_HEAD_IM;
4428                    ## Reprocess in the "in head" insertion mode...
4429                  } else {
4430                    !!!cp ('t143');
4431                }                }
4432    
4433                if ($self->{open_elements}->[-1]->[1] ne 'table') {                #
4434                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } else {
4435                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4436                    !!!cp ('t144');
4437                    #
4438                  } else {
4439                    !!!cp ('t145');
4440                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4441                    ## Ignore the token
4442                    !!!next-token;
4443                    next B;
4444                }                }
4445                }
4446    
4447                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4448                  !!!cp ('t146');
4449                  ## As if </noscript>
4450                  pop @{$self->{open_elements}};
4451                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4452                  
4453                  ## Reprocess in the "in head" insertion mode...
4454                  ## As if </head>
4455                  pop @{$self->{open_elements}};
4456    
4457                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
4458                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4459                  !!!cp ('t147');
4460                  ## As if </head>
4461                  pop @{$self->{open_elements}};
4462    
4463                  ## Reprocess in the "after head" insertion mode...
4464                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4465    ## ISSUE: This case cannot be reached?
4466                  !!!cp ('t148');
4467                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4468                  ## Ignore the token ## ISSUE: An issue in the spec.
4469                !!!next-token;                !!!next-token;
4470                redo B;                next B;
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
4471              } else {              } else {
4472                #                !!!cp ('t149');
4473              }              }
           } else {  
             #  
           }  
4474    
4475            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
4476            $in_body->($insert_to_foster);              ## As if <body>
4477            redo B;              !!!insert-element ('body',, $token);
4478          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
4479            if ($token->{type} eq 'character') {              ## reprocess
4480              ## NOTE: This is a code clone of "character in body".              next B;
4481          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4482            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4483              !!!cp ('t149.1');
4484    
4485              ## NOTE: As if <head>
4486              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4487              $self->{open_elements}->[-1]->[0]->append_child
4488                  ($self->{head_element});
4489              #push @{$self->{open_elements}},
4490              #    [$self->{head_element}, $el_category->{head}];
4491              #$self->{insertion_mode} = IN_HEAD_IM;
4492              ## NOTE: Reprocess.
4493    
4494              ## NOTE: As if </head>
4495              #pop @{$self->{open_elements}};
4496              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4497              ## NOTE: Reprocess.
4498              
4499              #
4500            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4501              !!!cp ('t149.2');
4502    
4503              ## NOTE: As if </head>
4504              pop @{$self->{open_elements}};
4505              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4506              ## NOTE: Reprocess.
4507    
4508              #
4509            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4510              !!!cp ('t149.3');
4511    
4512              !!!parse-error (type => 'in noscript:#eof', token => $token);
4513    
4514              ## As if </noscript>
4515              pop @{$self->{open_elements}};
4516              #$self->{insertion_mode} = IN_HEAD_IM;
4517              ## NOTE: Reprocess.
4518    
4519              ## NOTE: As if </head>
4520              pop @{$self->{open_elements}};
4521              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4522              ## NOTE: Reprocess.
4523    
4524              #
4525            } else {
4526              !!!cp ('t149.4');
4527              #
4528            }
4529    
4530            ## NOTE: As if <body>
4531            !!!insert-element ('body',, $token);
4532            $self->{insertion_mode} = IN_BODY_IM;
4533            ## NOTE: Reprocess.
4534            next B;
4535          } else {
4536            die "$0: $token->{type}: Unknown token type";
4537          }
4538    
4539              ## ISSUE: An issue in the spec.
4540        } elsif ($self->{insertion_mode} & BODY_IMS) {
4541              if ($token->{type} == CHARACTER_TOKEN) {
4542                !!!cp ('t150');
4543                ## NOTE: There is a code clone of "character in body".
4544              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4545                            
4546              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4547    
4548              !!!next-token;              !!!next-token;
4549              redo B;              next B;
4550            } 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') {  
4551              if ({              if ({
4552                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4553                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4554                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4555                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
4556                    ## have an element in table scope
4557                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
4558                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
4559                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
4560                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
4561                  my $node = $self->{open_elements}->[$_];  
4562                  if ($node->[1] eq 'caption') {                      ## Close the cell
4563                    $i = $_;                      !!!back-token; # <x>
4564                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
4565                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
4566                            table => 1, html => 1,                                line => $token->{line},
4567                           }->{$node->[1]}) {                                column => $token->{column}};
4568                    last INSCOPE;                      next B;
4569                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4570                        !!!cp ('t152');
4571                        ## ISSUE: This case can never be reached, maybe.
4572                        last;
4573                      }
4574                  }                  }
4575                } # INSCOPE  
4576                unless (defined $i) {                  !!!cp ('t153');
4577                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
4578                        value => $token->{tag_name}, token => $token);
4579                  ## Ignore the token                  ## Ignore the token
4580                    !!!nack ('t153.1');
4581                  !!!next-token;                  !!!next-token;
4582                  redo B;                  next B;
4583                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4584                                  !!!parse-error (type => 'not closed:caption', token => $token);
4585                ## generate implied end tags                  
4586                if ({                  ## NOTE: As if </caption>.
4587                     dd => 1, dt => 1, li => 1, p => 1,                  ## have a table element in table scope
4588                     td => 1, th => 1, tr => 1,                  my $i;
4589                    }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: {
4590                  !!!back-token; # <?>                    for (reverse 0..$#{$self->{open_elements}}) {
4591                  $token = {type => 'end tag', tag_name => 'caption'};                      my $node = $self->{open_elements}->[$_];
4592                  !!!back-token;                      if ($node->[1] & CAPTION_EL) {
4593                  $token = {type => 'end tag',                        !!!cp ('t155');
4594                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        $i = $_;
4595                  redo B;                        last INSCOPE;
4596                }                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4597                          !!!cp ('t156');
4598                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        last;
4599                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      }
4600                }                    }
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
4601    
4602                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
4603                      !!!parse-error (type => 'start tag not allowed',
4604                                      value => $token->{tag_name}, token => $token);
4605                      ## Ignore the token
4606                      !!!nack ('t157.1');
4607                      !!!next-token;
4608                      next B;
4609                    } # INSCOPE
4610                    
4611                    ## generate implied end tags
4612                    while ($self->{open_elements}->[-1]->[1]
4613                               & END_TAG_OPTIONAL_EL) {
4614                      !!!cp ('t158');
4615                      pop @{$self->{open_elements}};
4616                    }
4617    
4618                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4619                redo B;                    !!!cp ('t159');
4620                      !!!parse-error (type => 'not closed',
4621                                      value => $self->{open_elements}->[-1]->[0]
4622                                          ->manakai_local_name,
4623                                      token => $token);
4624                    } else {
4625                      !!!cp ('t160');
4626                    }
4627                    
4628                    splice @{$self->{open_elements}}, $i;
4629                    
4630                    $clear_up_to_marker->();
4631                    
4632                    $self->{insertion_mode} = IN_TABLE_IM;
4633                    
4634                    ## reprocess
4635                    !!!ack-later;
4636                    next B;
4637                  } else {
4638                    !!!cp ('t161');
4639                    #
4640                  }
4641              } else {              } else {
4642                  !!!cp ('t162');
4643                #                #
4644              }              }
4645            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4646              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4647                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
4648                my $i;                  ## have an element in table scope
4649                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
4650                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4651                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4652                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4653                    last INSCOPE;                      !!!cp ('t163');
4654                  } elsif ({                      $i = $_;
4655                            table => 1, html => 1,                      last INSCOPE;
4656                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4657                    last INSCOPE;                      !!!cp ('t164');
4658                        last INSCOPE;
4659                      }
4660                    } # INSCOPE
4661                      unless (defined $i) {
4662                        !!!cp ('t165');
4663                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4664                        ## Ignore the token
4665                        !!!next-token;
4666                        next B;
4667                      }
4668                    
4669                    ## generate implied end tags
4670                    while ($self->{open_elements}->[-1]->[1]
4671                               & END_TAG_OPTIONAL_EL) {
4672                      !!!cp ('t166');
4673                      pop @{$self->{open_elements}};
4674                  }                  }
4675                } # INSCOPE  
4676                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4677                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
4678                      !!!cp ('t167');
4679                      !!!parse-error (type => 'not closed',
4680                                      value => $self->{open_elements}->[-1]->[0]
4681                                          ->manakai_local_name,
4682                                      token => $token);
4683                    } else {
4684                      !!!cp ('t168');
4685                    }
4686                    
4687                    splice @{$self->{open_elements}}, $i;
4688                    
4689                    $clear_up_to_marker->();
4690                    
4691                    $self->{insertion_mode} = IN_ROW_IM;
4692                    
4693                    !!!next-token;
4694                    next B;
4695                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4696                    !!!cp ('t169');
4697                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4698                  ## Ignore the token                  ## Ignore the token
4699                  !!!next-token;                  !!!next-token;
4700                  redo B;                  next B;
4701                }                } else {
4702                                  !!!cp ('t170');
4703                ## 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;  
4704                }                }
4705                } elsif ($token->{tag_name} eq 'caption') {
4706                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
4707                    ## have a table element in table scope
4708                    my $i;
4709                    INSCOPE: {
4710                      for (reverse 0..$#{$self->{open_elements}}) {
4711                        my $node = $self->{open_elements}->[$_];
4712                        if ($node->[1] & CAPTION_EL) {
4713                          !!!cp ('t171');
4714                          $i = $_;
4715                          last INSCOPE;
4716                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
4717                          !!!cp ('t172');
4718                          last;
4719                        }
4720                      }
4721    
4722                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
4723                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
4724                                      value => $token->{tag_name}, token => $token);
4725                      ## Ignore the token
4726                      !!!next-token;
4727                      next B;
4728                    } # INSCOPE
4729                    
4730                    ## generate implied end tags
4731                    while ($self->{open_elements}->[-1]->[1]
4732                               & END_TAG_OPTIONAL_EL) {
4733                      !!!cp ('t174');
4734                      pop @{$self->{open_elements}};
4735                    }
4736                    
4737                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4738                      !!!cp ('t175');
4739                      !!!parse-error (type => 'not closed',
4740                                      value => $self->{open_elements}->[-1]->[0]
4741                                          ->manakai_local_name,
4742                                      token => $token);
4743                    } else {
4744                      !!!cp ('t176');
4745                    }
4746                    
4747                    splice @{$self->{open_elements}}, $i;
4748                    
4749                    $clear_up_to_marker->();
4750                    
4751                    $self->{insertion_mode} = IN_TABLE_IM;
4752                    
4753                    !!!next-token;
4754                    next B;
4755                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4756                    !!!cp ('t177');
4757                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4758                    ## Ignore the token
4759                    !!!next-token;
4760                    next B;
4761                  } else {
4762                    !!!cp ('t178');
4763                    #
4764                }                }
4765                } elsif ({
4766                          table => 1, tbody => 1, tfoot => 1,
4767                          thead => 1, tr => 1,
4768                         }->{$token->{tag_name}} and
4769                         $self->{insertion_mode} == IN_CELL_IM) {
4770                  ## have an element in table scope
4771                  my $i;
4772                  my $tn;
4773                  INSCOPE: {
4774                    for (reverse 0..$#{$self->{open_elements}}) {
4775                      my $node = $self->{open_elements}->[$_];
4776                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4777                        !!!cp ('t179');
4778                        $i = $_;
4779    
4780                        ## Close the cell
4781                        !!!back-token; # </x>
4782                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
4783                                  line => $token->{line},
4784                                  column => $token->{column}};
4785                        next B;
4786                      } elsif ($node->[1] & TABLE_CELL_EL) {
4787                        !!!cp ('t180');
4788                        $tn = $node->[0]->manakai_local_name;
4789                        ## NOTE: There is exactly one |td| or |th| element
4790                        ## in scope in the stack of open elements by definition.
4791                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4792                        ## ISSUE: Can this be reached?
4793                        !!!cp ('t181');
4794                        last;
4795                      }
4796                    }
4797    
4798                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
4799                    !!!parse-error (type => 'unmatched end tag',
4800                $clear_up_to_marker->();                      value => $token->{tag_name}, token => $token);
4801                    ## Ignore the token
4802                $self->{insertion_mode} = 'in table';                  !!!next-token;
4803                    next B;
4804                !!!next-token;                } # INSCOPE
4805                redo B;              } elsif ($token->{tag_name} eq 'table' and
4806              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4807                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4808    
4809                ## As if </caption>                ## As if </caption>
4810                ## have a table element in table scope                ## have a table element in table scope
4811                my $i;                my $i;
4812                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4813                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4814                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4815                      !!!cp ('t184');
4816                    $i = $_;                    $i = $_;
4817                    last INSCOPE;                    last INSCOPE;
4818                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4819                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4820                    last INSCOPE;                    last INSCOPE;
4821                  }                  }
4822                } # INSCOPE                } # INSCOPE
4823                unless (defined $i) {                unless (defined $i) {
4824                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4825                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4826                  ## Ignore the token                  ## Ignore the token
4827                  !!!next-token;                  !!!next-token;
4828                  redo B;                  next B;
4829                }                }
4830                                
4831                ## generate implied end tags                ## generate implied end tags
4832                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4833                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4834                     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;  
4835                }                }
4836    
4837                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4838                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4839                    !!!parse-error (type => 'not closed',
4840                                    value => $self->{open_elements}->[-1]->[0]
4841                                        ->manakai_local_name,
4842                                    token => $token);
4843                  } else {
4844                    !!!cp ('t189');
4845                }                }
4846    
4847                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4848    
4849                $clear_up_to_marker->();                $clear_up_to_marker->();
4850    
4851                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4852    
4853                ## reprocess                ## reprocess
4854                redo B;                next B;
4855              } elsif ({              } elsif ({
4856                        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,  
4857                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4858                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4859                ## Ignore the token                  !!!cp ('t190');
4860                redo B;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
4861                  ## Ignore the token                  ## Ignore the token
4862                  !!!next-token;                  !!!next-token;
4863                  redo B;                  next B;
4864                } else {                } else {
4865                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
4866                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
4867                }                }
4868              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
4869                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
4870                          thead => 1, tr => 1,
4871                         }->{$token->{tag_name}} and
4872                         $self->{insertion_mode} == IN_CAPTION_IM) {
4873                  !!!cp ('t192');
4874                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4875                ## Ignore the token                ## Ignore the token
4876                !!!next-token;                !!!next-token;
4877                redo B;                next B;
4878              } else {              } else {
4879                #                !!!cp ('t193');
4880                  #
4881              }              }
4882            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4883              #          for my $entry (@{$self->{open_elements}}) {
4884              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4885                !!!cp ('t75');
4886                !!!parse-error (type => 'in body:#eof', token => $token);
4887                last;
4888            }            }
4889            }
4890    
4891            ## As if </colgroup>          ## Stop parsing.
4892            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
4893              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
4894              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
4895          }
4896    
4897          $insert = $insert_to_current;
4898          #
4899        } elsif ($self->{insertion_mode} & TABLE_IMS) {
4900          if ($token->{type} == CHARACTER_TOKEN) {
4901            if (not $open_tables->[-1]->[1] and # tainted
4902                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4903              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4904                  
4905              unless (length $token->{data}) {
4906                !!!cp ('t194');
4907              !!!next-token;              !!!next-token;
4908              redo B;              next B;
4909            } else {            } else {
4910              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
4911            }            }
4912          } 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;  
               }  
             }  
4913    
4914              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4915    
4916              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4917              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
4918              ## into the current node" while characters might not be              ## into the current node" while characters might not be
4919              ## result in a new Text node.              ## result in a new Text node.
4920              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4921                
4922              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]}) {  
4923                # MUST                # MUST
4924                my $foster_parent_element;                my $foster_parent_element;
4925                my $next_sibling;                my $next_sibling;
4926                my $prev_sibling;                my $prev_sibling;
4927                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4928                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4929                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4930                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4931                        !!!cp ('t196');
4932                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4933                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4934                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4935                    } else {                    } else {
4936                        !!!cp ('t197');
4937                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4938                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4939                    }                    }
# Line 3746  sub _tree_construction_main ($) { Line 4945  sub _tree_construction_main ($) {
4945                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4946                if (defined $prev_sibling and                if (defined $prev_sibling and
4947                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4948                    !!!cp ('t198');
4949                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4950                } else {                } else {
4951                    !!!cp ('t199');
4952                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4953                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4954                     $next_sibling);                     $next_sibling);
4955                }                }
4956              } else {            $open_tables->[-1]->[1] = 1; # tainted
4957                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4958              }            !!!cp ('t200');
4959              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4960            }
4961                            
4962              !!!next-token;          !!!next-token;
4963              redo B;          next B;
4964            } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == START_TAG_TOKEN) {
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
4965              if ({              if ({
4966                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
4967                   th => 1, td => 1,                   th => 1, td => 1,
4968                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4969                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4970                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
4971                    while (not ($self->{open_elements}->[-1]->[1]
4972                                    & TABLE_SCOPING_EL)) {
4973                      !!!cp ('t201');
4974                      pop @{$self->{open_elements}};
4975                    }
4976                    
4977                    !!!insert-element ('tbody',, $token);
4978                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4979                    ## reprocess in the "in table body" insertion mode...
4980                }                }
4981    
4982                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4983                while (not {                  unless ($token->{tag_name} eq 'tr') {
4984                  tbody => 1, tfoot => 1, thead => 1, html => 1,                    !!!cp ('t202');
4985                }->{$self->{open_elements}->[-1]->[1]}) {                    !!!parse-error (type => 'missing start tag:tr', token => $token);
4986                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  }
4987                    
4988                    ## Clear back to table body context
4989                    while (not ($self->{open_elements}->[-1]->[1]
4990                                    & TABLE_ROWS_SCOPING_EL)) {
4991                      !!!cp ('t203');
4992                      ## ISSUE: Can this case be reached?
4993                      pop @{$self->{open_elements}};
4994                    }
4995                    
4996                    $self->{insertion_mode} = IN_ROW_IM;
4997                    if ($token->{tag_name} eq 'tr') {
4998                      !!!cp ('t204');
4999                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5000                      !!!nack ('t204');
5001                      !!!next-token;
5002                      next B;
5003                    } else {
5004                      !!!cp ('t205');
5005                      !!!insert-element ('tr',, $token);
5006                      ## reprocess in the "in row" insertion mode
5007                    }
5008                  } else {
5009                    !!!cp ('t206');
5010                  }
5011    
5012                  ## Clear back to table row context
5013                  while (not ($self->{open_elements}->[-1]->[1]
5014                                  & TABLE_ROW_SCOPING_EL)) {
5015                    !!!cp ('t207');
5016                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5017                }                }
5018                                
5019                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5020                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5021                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5022                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5023                } else {                
5024                  !!!insert-element ('tr');                !!!nack ('t207.1');
5025                  ## reprocess                !!!next-token;
5026                }                next B;
               redo B;  
5027              } elsif ({              } elsif ({
5028                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5029                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5030                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5031                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5032                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5033                my $i;                  ## As if </tr>
5034                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5035                  my $node = $self->{open_elements}->[$_];                  my $i;
5036                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5037                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5038                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5039                    $i = $_;                      !!!cp ('t208');
5040                    last INSCOPE;                      $i = $_;
5041                  } elsif ({                      last INSCOPE;
5042                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5043                           }->{$node->[1]}) {                      !!!cp ('t209');
5044                    last INSCOPE;                      last INSCOPE;
5045                      }
5046                    } # INSCOPE
5047                    unless (defined $i) {
5048                      !!!cp ('t210');
5049    ## TODO: This type is wrong.
5050                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5051                      ## Ignore the token
5052                      !!!nack ('t210.1');
5053                      !!!next-token;
5054                      next B;
5055                    }
5056                    
5057                    ## Clear back to table row context
5058                    while (not ($self->{open_elements}->[-1]->[1]
5059                                    & TABLE_ROW_SCOPING_EL)) {
5060                      !!!cp ('t211');
5061                      ## ISSUE: Can this case be reached?
5062                      pop @{$self->{open_elements}};
5063                    }
5064                    
5065                    pop @{$self->{open_elements}}; # tr
5066                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5067                    if ($token->{tag_name} eq 'tr') {
5068                      !!!cp ('t212');
5069                      ## reprocess
5070                      !!!ack-later;
5071                      next B;
5072                    } else {
5073                      !!!cp ('t213');
5074                      ## reprocess in the "in table body" insertion mode...
5075                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5076                }                }
5077    
5078                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5079                while (not {                  ## have an element in table scope
5080                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5081                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5082                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5083                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5084                        !!!cp ('t214');
5085                        $i = $_;
5086                        last INSCOPE;
5087                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5088                        !!!cp ('t215');
5089                        last INSCOPE;
5090                      }
5091                    } # INSCOPE
5092                    unless (defined $i) {
5093                      !!!cp ('t216');
5094    ## TODO: This erorr type ios wrong.
5095                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5096                      ## Ignore the token
5097                      !!!nack ('t216.1');
5098                      !!!next-token;
5099                      next B;
5100                    }
5101    
5102                    ## Clear back to table body context
5103                    while (not ($self->{open_elements}->[-1]->[1]
5104                                    & TABLE_ROWS_SCOPING_EL)) {
5105                      !!!cp ('t217');
5106                      ## ISSUE: Can this state be reached?
5107                      pop @{$self->{open_elements}};
5108                    }
5109                    
5110                    ## As if <{current node}>
5111                    ## have an element in table scope
5112                    ## true by definition
5113                    
5114                    ## Clear back to table body context
5115                    ## nop by definition
5116                    
5117                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5118                    $self->{insertion_mode} = IN_TABLE_IM;
5119                    ## reprocess in "in table" insertion mode...
5120                  } else {
5121                    !!!cp ('t218');
5122                }                }
5123    
5124                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5125                ## have an element in table scope                  ## Clear back to table context
5126                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5127                                    & TABLE_SCOPING_EL)) {
5128                ## Clear back to table body context                    !!!cp ('t219');
5129                ## nop by definition                    ## ISSUE: Can this state be reached?
5130                      pop @{$self->{open_elements}};
5131                pop @{$self->{open_elements}};                  }
5132                $self->{insertion_mode} = 'in table';                  
5133                ## reprocess                  !!!insert-element ('colgroup',, $token);
5134                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5135                    ## reprocess
5136                    !!!ack-later;
5137                    next B;
5138                  } elsif ({
5139                            caption => 1,
5140                            colgroup => 1,
5141                            tbody => 1, tfoot => 1, thead => 1,
5142                           }->{$token->{tag_name}}) {
5143                    ## Clear back to table context
5144                    while (not ($self->{open_elements}->[-1]->[1]
5145                                    & TABLE_SCOPING_EL)) {
5146                      !!!cp ('t220');
5147                      ## ISSUE: Can this state be reached?
5148                      pop @{$self->{open_elements}};
5149                    }
5150                    
5151                    push @$active_formatting_elements, ['#marker', '']
5152                        if $token->{tag_name} eq 'caption';
5153                    
5154                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5155                    $self->{insertion_mode} = {
5156                                               caption => IN_CAPTION_IM,
5157                                               colgroup => IN_COLUMN_GROUP_IM,
5158                                               tbody => IN_TABLE_BODY_IM,
5159                                               tfoot => IN_TABLE_BODY_IM,
5160                                               thead => IN_TABLE_BODY_IM,
5161                                              }->{$token->{tag_name}};
5162                    !!!next-token;
5163                    !!!nack ('t220.1');
5164                    next B;
5165                  } else {
5166                    die "$0: in table: <>: $token->{tag_name}";
5167                  }
5168              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5169                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5170                !!!parse-error (type => 'not closed:table');                                value => $self->{open_elements}->[-1]->[0]
5171                                      ->manakai_local_name,
5172                                  token => $token);
5173    
5174                ## As if </table>                ## As if </table>
5175                ## have a table element in table scope                ## have a table element in table scope
5176                my $i;                my $i;
5177                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5178                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5179                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5180                      !!!cp ('t221');
5181                    $i = $_;                    $i = $_;
5182                    last INSCOPE;                    last INSCOPE;
5183                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5184                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5185                    last INSCOPE;                    last INSCOPE;
5186                  }                  }
5187                } # INSCOPE                } # INSCOPE
5188                unless (defined $i) {                unless (defined $i) {
5189                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5190    ## TODO: The following is wrong, maybe.
5191                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5192                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5193                    !!!nack ('t223.1');
5194                  !!!next-token;                  !!!next-token;
5195                  redo B;                  next B;
5196                }                }
5197                                
5198    ## TODO: Followings are removed from the latest spec.
5199                ## generate implied end tags                ## generate implied end tags
5200                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5201                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5202                     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;  
5203                }                }
5204    
5205                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5206                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5207                    ## NOTE: |<table><tr><table>|
5208                    !!!parse-error (type => 'not closed',
5209                                    value => $self->{open_elements}->[-1]->[0]
5210                                        ->manakai_local_name,
5211                                    token => $token);
5212                  } else {
5213                    !!!cp ('t226');
5214                }                }
5215    
5216                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5217                  pop @{$open_tables};
5218    
5219                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5220    
5221                ## reprocess            ## reprocess
5222                redo B;            !!!ack-later;
5223              } else {            next B;
5224                #          } elsif ($token->{tag_name} eq 'style') {
5225              }            if (not $open_tables->[-1]->[1]) { # tainted
5226            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5227              if ({              ## NOTE: This is a "as if in head" code clone.
5228                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5229                  }->{$token->{tag_name}}) {              next B;
5230                ## have an element in table scope            } else {
5231                my $i;              !!!cp ('t227.7');
5232                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5233                  my $node = $self->{open_elements}->[$_];            }
5234                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5235                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5236                    last INSCOPE;              !!!cp ('t227.6');
5237                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5238                            table => 1, html => 1,              $script_start_tag->();
5239                           }->{$node->[1]}) {              next B;
5240                    last INSCOPE;            } else {
5241                  }              !!!cp ('t227.5');
5242                } # INSCOPE              #
5243                unless (defined $i) {            }
5244                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5245                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5246                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5247                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5248                }                if ($type eq 'hidden') {
5249                    !!!cp ('t227.3');
5250                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5251    
5252                ## 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}};  
               }  
5253    
5254                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;  
               }  
5255    
               ## 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]);  
5256                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
5257    
5258                ## Clear back to table body context                  !!!next-token;
5259                ## nop by definition                  !!!ack ('t227.2.1');
5260                    next B;
5261                pop @{$self->{open_elements}};                } else {
5262                $self->{insertion_mode} = 'in table';                  !!!cp ('t227.2');
5263                ## reprocess                  #
5264                redo B;                }
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
5265              } else {              } else {
5266                  !!!cp ('t227.1');
5267                #                #
5268              }              }
5269            } else {            } else {
5270                !!!cp ('t227.4');
5271              #              #
5272            }            }
5273                      } else {
5274            ## As if in table            !!!cp ('t227');
5275            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5276            $in_body->($insert_to_foster);          }
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
5277    
5278              ## As if in body, but insert into foster parent element          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
5279    
5280                push @$active_formatting_elements, ['#marker', ''];          $insert = $insert_to_foster;
5281                          #
5282                !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
5283                redo B;              if ($token->{tag_name} eq 'tr' and
5284              } elsif ({                  $self->{insertion_mode} == IN_ROW_IM) {
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
5285                ## have an element in table scope                ## have an element in table scope
5286                my $i;                my $i;
5287                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5288                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5289                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5290                      !!!cp ('t228');
5291                    $i = $_;                    $i = $_;
5292                    last INSCOPE;                    last INSCOPE;
5293                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5294                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5295                    last INSCOPE;                    last INSCOPE;
5296                  }                  }
5297                } # INSCOPE                } # INSCOPE
5298                unless (defined $i) {                unless (defined $i) {
5299                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5300                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5301                  ## Ignore the token                  ## Ignore the token
5302                    !!!nack ('t230.1');
5303                  !!!next-token;                  !!!next-token;
5304                  redo B;                  next B;
5305                  } else {
5306                    !!!cp ('t232');
5307                }                }
5308    
5309                ## Clear back to table row context                ## Clear back to table row context
5310                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5311                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5312                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5313                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5314                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5315                }                }
5316    
5317                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5318                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5319                ## reprocess                !!!next-token;
5320                redo B;                !!!nack ('t231.1');
5321                  next B;
5322              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5323                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5324                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5325                    ## have an element in table scope
5326                ## As if </table>                  my $i;
5327                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5328                my $i;                    my $node = $self->{open_elements}->[$_];
5329                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5330                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5331                  if ($node->[1] eq 'table') {                      $i = $_;
5332                    $i = $_;                      last INSCOPE;
5333                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5334                  } elsif ({                      !!!cp ('t234');
5335                            table => 1, html => 1,                      last INSCOPE;
5336                           }->{$node->[1]}) {                    }
5337                    last INSCOPE;                  } # INSCOPE
5338                    unless (defined $i) {
5339                      !!!cp ('t235');
5340    ## TODO: The following is wrong.
5341                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5342                      ## Ignore the token
5343                      !!!nack ('t236.1');
5344                      !!!next-token;
5345                      next B;
5346                  }                  }
5347                } # INSCOPE                  
5348                unless (defined $i) {                  ## Clear back to table row context
5349                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5350                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5351                  !!!next-token;                    !!!cp ('t236');
5352                  redo B;  ## ISSUE: Can this state be reached?
5353                }                    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;  
5354                  }                  }
5355                } # INSCOPE                  
5356                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5357                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5358                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5359                  !!!next-token;                }
5360                  redo B;  
5361                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5362                    ## have an element in table scope
5363                ## Clear back to table row context                  my $i;
5364                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5365                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5366                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5367                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5368                        $i = $_;
5369                        last INSCOPE;
5370                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5371                        !!!cp ('t238');
5372                        last INSCOPE;
5373                      }
5374                    } # INSCOPE
5375                    unless (defined $i) {
5376                      !!!cp ('t239');
5377                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5378                      ## Ignore the token
5379                      !!!nack ('t239.1');
5380                      !!!next-token;
5381                      next B;
5382                    }
5383                    
5384                    ## Clear back to table body context
5385                    while (not ($self->{open_elements}->[-1]->[1]
5386                                    & TABLE_ROWS_SCOPING_EL)) {
5387                      !!!cp ('t240');
5388                      pop @{$self->{open_elements}};
5389                    }
5390                    
5391                    ## As if <{current node}>
5392                    ## have an element in table scope
5393                    ## true by definition
5394                    
5395                    ## Clear back to table body context
5396                    ## nop by definition
5397                    
5398                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5399                    $self->{insertion_mode} = IN_TABLE_IM;
5400                    ## reprocess in the "in table" insertion mode...
5401                }                }
5402    
5403                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
5404                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
5405                !!!next-token;                ## the code for <table> in the "in table" insertion mode
5406                redo B;                ## is synced with it.
5407              } elsif ($token->{tag_name} eq 'table') {  
5408                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
5409                my $i;                my $i;
5410                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5411                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5412                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
5413                      !!!cp ('t241');
5414                    $i = $_;                    $i = $_;
5415                    last INSCOPE;                    last INSCOPE;
5416                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5417                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5418                    last INSCOPE;                    last INSCOPE;
5419                  }                  }
5420                } # INSCOPE                } # INSCOPE
5421                unless (defined $i) {                unless (defined $i) {
5422                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
5423                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5424                  ## Ignore the token                  ## Ignore the token
5425                    !!!nack ('t243.1');
5426                  !!!next-token;                  !!!next-token;
5427                  redo B;                  next B;
5428                }                }
5429                    
5430                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
5431                while (not {                pop @{$open_tables};
5432                  tr => 1, html => 1,                
5433                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
5434                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
5435                  pop @{$self->{open_elements}};                !!!next-token;
5436                }                next B;
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
5437              } elsif ({              } elsif ({
5438                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5439                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
5440                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
5441                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
5442                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5443                  my $node = $self->{open_elements}->[$_];                  my $i;
5444                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5445                    $i = $_;                    my $node = $self->{open_elements}->[$_];
5446                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5447                  } elsif ({                      !!!cp ('t247');
5448                            table => 1, html => 1,                      $i = $_;
5449                           }->{$node->[1]}) {                      last INSCOPE;
5450                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5451                        !!!cp ('t248');
5452                        last INSCOPE;
5453                      }
5454                    } # INSCOPE
5455                      unless (defined $i) {
5456                        !!!cp ('t249');
5457                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5458                        ## Ignore the token
5459                        !!!nack ('t249.1');
5460                        !!!next-token;
5461                        next B;
5462                      }
5463                    
5464                    ## As if </tr>
5465                    ## have an element in table scope
5466                    my $i;
5467                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5468                      my $node = $self->{open_elements}->[$_];
5469                      if ($node->[1] & TABLE_ROW_EL) {
5470                        !!!cp ('t250');
5471                        $i = $_;
5472                        last INSCOPE;
5473                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5474                        !!!cp ('t251');
5475                        last INSCOPE;
5476                      }
5477                    } # INSCOPE
5478                      unless (defined $i) {
5479                        !!!cp ('t252');
5480                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5481                        ## Ignore the token
5482                        !!!nack ('t252.1');
5483                        !!!next-token;
5484                        next B;
5485                      }
5486                    
5487                    ## Clear back to table row context
5488                    while (not ($self->{open_elements}->[-1]->[1]
5489                                    & TABLE_ROW_SCOPING_EL)) {
5490                      !!!cp ('t253');
5491    ## ISSUE: Can this case be reached?
5492                      pop @{$self->{open_elements}};
5493                  }                  }
5494                } # INSCOPE                  
5495                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5496                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5497                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
5498                }                }
5499    
               ## As if </tr>  
5500                ## have an element in table scope                ## have an element in table scope
5501                my $i;                my $i;
5502                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5503                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5504                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5505                      !!!cp ('t254');
5506                    $i = $_;                    $i = $_;
5507                    last INSCOPE;                    last INSCOPE;
5508                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5509                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5510                    last INSCOPE;                    last INSCOPE;
5511                  }                  }
5512                } # INSCOPE                } # INSCOPE
5513                unless (defined $i) {                unless (defined $i) {
5514                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
5515                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5516                  ## Ignore the token                  ## Ignore the token
5517                    !!!nack ('t256.1');
5518                  !!!next-token;                  !!!next-token;
5519                  redo B;                  next B;
5520                }                }
5521    
5522                ## Clear back to table row context                ## Clear back to table body context
5523                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5524                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5525                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5526                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5527                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5528                }                }
5529    
5530                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
5531                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
5532                ## reprocess                !!!nack ('t257.1');
5533                redo B;                !!!next-token;
5534                  next B;
5535              } elsif ({              } elsif ({
5536                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5537                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5538                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5539                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5540                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5541                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5542                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5543                !!!next-token;            ## Ignore the token
5544                redo B;            !!!nack ('t258.1');
5545              } else {             !!!next-token;
5546                #            next B;
5547              }          } else {
5548            } else {            !!!cp ('t259');
5549              #            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
           }  
5550    
5551            ## As if in table            $insert = $insert_to_foster;
5552            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5553            $in_body->($insert_to_foster);          }
5554            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5555          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5556            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
5557              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
5558              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
5559                          #
5560              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5561              !!!cp ('t259.2');
5562              #
5563            }
5564    
5565              !!!next-token;          ## Stop parsing
5566              redo B;          last B;
5567            } elsif ($token->{type} eq 'comment') {        } else {
5568              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
5569              my $comment = $self->{document}->create_comment ($token->{data});        }
5570              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5571              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
5572              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5573            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5574              if ({                unless (length $token->{data}) {
5575                   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  
5576                  !!!next-token;                  !!!next-token;
5577                  redo B;                  next B;
5578                }                }
5579                }
5580                ## Close the cell              
5581                !!!back-token; # <?>              !!!cp ('t261');
5582                $token = {type => 'end tag', tag_name => $tn};              #
5583                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
5584              } else {              if ($token->{tag_name} eq 'col') {
5585                  !!!cp ('t262');
5586                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5587                  pop @{$self->{open_elements}};
5588                  !!!ack ('t262.1');
5589                  !!!next-token;
5590                  next B;
5591                } else {
5592                  !!!cp ('t263');
5593                #                #
5594              }              }
5595            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5596              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
5597                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5598                my $i;                  !!!cp ('t264');
5599                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5600                  ## Ignore the token                  ## Ignore the token
5601                  !!!next-token;                  !!!next-token;
5602                  redo B;                  next B;
5603                }                } else {
5604                                  !!!cp ('t265');
5605                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
5606                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
5607                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
5608                     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]);  
5609                }                }
5610                } elsif ($token->{tag_name} eq 'col') {
5611                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
5612                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5613                ## Ignore the token                ## Ignore the token
5614                !!!next-token;                !!!next-token;
5615                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;  
5616              } else {              } else {
5617                #                !!!cp ('t267');
5618                  #
5619              }              }
5620          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5621            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5622                @{$self->{open_elements}} == 1) { # redundant, maybe
5623              !!!cp ('t270.2');
5624              ## Stop parsing.
5625              last B;
5626            } else {
5627              ## NOTE: As if </colgroup>.
5628              !!!cp ('t270.1');
5629              pop @{$self->{open_elements}}; # colgroup
5630              $self->{insertion_mode} = IN_TABLE_IM;
5631              ## Reprocess.
5632              next B;
5633            }
5634          } else {
5635            die "$0: $token->{type}: Unknown token type";
5636          }
5637    
5638              ## As if </colgroup>
5639              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5640                !!!cp ('t269');
5641    ## TODO: Wrong error type?
5642                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5643                ## Ignore the token
5644                !!!nack ('t269.1');
5645                !!!next-token;
5646                next B;
5647            } else {            } else {
5648              #              !!!cp ('t270');
5649                pop @{$self->{open_elements}}; # colgroup
5650                $self->{insertion_mode} = IN_TABLE_IM;
5651                !!!ack-later;
5652                ## reprocess
5653                next B;
5654              }
5655        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5656          if ($token->{type} == CHARACTER_TOKEN) {
5657            !!!cp ('t271');
5658            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5659            !!!next-token;
5660            next B;
5661          } elsif ($token->{type} == START_TAG_TOKEN) {
5662            if ($token->{tag_name} eq 'option') {
5663              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5664                !!!cp ('t272');
5665                ## As if </option>
5666                pop @{$self->{open_elements}};
5667              } else {
5668                !!!cp ('t273');
5669            }            }
             
           $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}};  
               }  
5670    
5671                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5672                !!!next-token;            !!!nack ('t273.1');
5673                redo B;            !!!next-token;
5674              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5675                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5676                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5677                  pop @{$self->{open_elements}};              !!!cp ('t274');
5678                }              ## As if </option>
5679                pop @{$self->{open_elements}};
5680              } else {
5681                !!!cp ('t275');
5682              }
5683    
5684                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5685                  ## As if </optgroup>              !!!cp ('t276');
5686                  pop @{$self->{open_elements}};              ## As if </optgroup>
5687                }              pop @{$self->{open_elements}};
5688              } else {
5689                !!!cp ('t277');
5690              }
5691    
5692                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5693                !!!next-token;            !!!nack ('t277.1');
5694                redo B;            !!!next-token;
5695              } elsif ($token->{tag_name} eq 'select') {            next B;
5696                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5697                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5698                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5699                my $i;                    {
5700                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5701                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5702                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5703                    $i = $_;                    }->{$token->{tag_name}})) {
5704                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5705                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5706                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5707                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5708                    last INSCOPE;            ## have an element in table scope
5709                  }            my $i;
5710                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5711                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5712                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5713                  ## Ignore the token                !!!cp ('t278');
5714                  !!!next-token;                $i = $_;
5715                  redo B;                last INSCOPE;
5716                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5717                  !!!cp ('t279');
5718                  last INSCOPE;
5719                }
5720              } # INSCOPE
5721              unless (defined $i) {
5722                !!!cp ('t280');
5723                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5724                ## Ignore the token
5725                !!!nack ('t280.1');
5726                !!!next-token;
5727                next B;
5728              }
5729                                
5730                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5731              splice @{$self->{open_elements}}, $i;
5732    
5733                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5734    
5735                !!!next-token;            if ($token->{tag_name} eq 'select') {
5736                redo B;              !!!nack ('t281.2');
5737              } else {              !!!next-token;
5738                #              next B;
5739              } else {
5740                !!!cp ('t281.1');
5741                !!!ack-later;
5742                ## Reprocess the token.
5743                next B;
5744              }
5745            } else {
5746              !!!cp ('t282');
5747              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5748              ## Ignore the token
5749              !!!nack ('t282.1');
5750              !!!next-token;
5751              next B;
5752            }
5753          } elsif ($token->{type} == END_TAG_TOKEN) {
5754            if ($token->{tag_name} eq 'optgroup') {
5755              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5756                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5757                !!!cp ('t283');
5758                ## As if </option>
5759                splice @{$self->{open_elements}}, -2;
5760              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5761                !!!cp ('t284');
5762                pop @{$self->{open_elements}};
5763              } else {
5764                !!!cp ('t285');
5765                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5766                ## Ignore the token
5767              }
5768              !!!nack ('t285.1');
5769              !!!next-token;
5770              next B;
5771            } elsif ($token->{tag_name} eq 'option') {
5772              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5773                !!!cp ('t286');
5774                pop @{$self->{open_elements}};
5775              } else {
5776                !!!cp ('t287');
5777                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5778                ## Ignore the token
5779              }
5780              !!!nack ('t287.1');
5781              !!!next-token;
5782              next B;
5783            } elsif ($token->{tag_name} eq 'select') {
5784              ## have an element in table scope
5785              my $i;
5786              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5787                my $node = $self->{open_elements}->[$_];
5788                if ($node->[1] & SELECT_EL) {
5789                  !!!cp ('t288');
5790                  $i = $_;
5791                  last INSCOPE;
5792                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5793                  !!!cp ('t289');
5794                  last INSCOPE;
5795              }              }
5796            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
5797              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
5798                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
5799                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5800                  ## As if </option>              ## Ignore the token
5801                  splice @{$self->{open_elements}}, -2;              !!!nack ('t290.1');
5802                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!next-token;
5803                  pop @{$self->{open_elements}};              next B;
5804                } else {            }
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
5805                                
5806                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5807              splice @{$self->{open_elements}}, $i;
5808    
5809                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5810    
5811                !!!next-token;            !!!nack ('t291.1');
5812                redo B;            !!!next-token;
5813              } elsif ({            next B;
5814                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5815                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5816                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5817                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5818                                   }->{$token->{tag_name}}) {
5819                ## have an element in table scope  ## TODO: The following is wrong?
5820                my $i;            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
5821                                
5822                ## As if </select>            ## have an element in table scope
5823                ## have an element in table scope            my $i;
5824                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5825                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
5826                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5827                  if ($node->[1] eq 'select') {                !!!cp ('t292');
5828                    $i = $_;                $i = $_;
5829                    last INSCOPE;                last INSCOPE;
5830                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5831                            table => 1, html => 1,                !!!cp ('t293');
5832                           }->{$node->[1]}) {                last INSCOPE;
5833                    last INSCOPE;              }
5834                  }            } # INSCOPE
5835                } # INSCOPE            unless (defined $i) {
5836                unless (defined $i) {              !!!cp ('t294');
5837                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
5838                  ## Ignore the </select> token              !!!nack ('t294.1');
5839                  !!!next-token; ## TODO: ok?              !!!next-token;
5840                  redo B;              next B;
5841                }            }
5842                                
5843                splice @{$self->{open_elements}}, $i;            ## As if </select>
5844              ## have an element in table scope
5845                $self->_reset_insertion_mode;            undef $i;
5846              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5847                ## reprocess              my $node = $self->{open_elements}->[$_];
5848                redo B;              if ($node->[1] & SELECT_EL) {
5849              } else {                !!!cp ('t295');
5850                #                $i = $_;
5851                  last INSCOPE;
5852                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5853    ## ISSUE: Can this state be reached?
5854                  !!!cp ('t296');
5855                  last INSCOPE;
5856              }              }
5857            } else {            } # INSCOPE
5858              #            unless (defined $i) {
5859                !!!cp ('t297');
5860    ## TODO: The following error type is correct?
5861                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5862                ## Ignore the </select> token
5863                !!!nack ('t297.1');
5864                !!!next-token; ## TODO: ok?
5865                next B;
5866            }            }
5867                  
5868              !!!cp ('t298');
5869              splice @{$self->{open_elements}}, $i;
5870    
5871              $self->_reset_insertion_mode;
5872    
5873            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
5874              ## reprocess
5875              next B;
5876            } else {
5877              !!!cp ('t299');
5878              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5879            ## Ignore the token            ## Ignore the token
5880              !!!nack ('t299.3');
5881            !!!next-token;            !!!next-token;
5882            redo B;            next B;
5883          } elsif ($self->{insertion_mode} eq 'after body') {          }
5884            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5885              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5886                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
5887                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
5888              !!!parse-error (type => 'in body:#eof', token => $token);
5889            } else {
5890              !!!cp ('t299.2');
5891            }
5892    
5893            ## Stop parsing.
5894            last B;
5895          } else {
5896            die "$0: $token->{type}: Unknown token type";
5897          }
5898        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5899          if ($token->{type} == CHARACTER_TOKEN) {
5900            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5901              my $data = $1;
5902              ## As if in body
5903              $reconstruct_active_formatting_elements->($insert_to_current);
5904                                
5905                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5906              
5907              unless (length $token->{data}) {
5908                !!!cp ('t300');
5909                !!!next-token;
5910                next B;
5911              }
5912            }
5913            
5914            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5915              !!!cp ('t301');
5916              !!!parse-error (type => 'after html:#character', token => $token);
5917    
5918                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
5919                  !!!next-token;          } else {
5920                  redo B;            !!!cp ('t302');
5921                }          }
5922              }          
5923                        ## "after body" insertion mode
5924              #          !!!parse-error (type => 'after body:#character', token => $token);
5925              !!!parse-error (type => 'after body:#'.$token->{type});  
5926            } elsif ($token->{type} eq 'comment') {          $self->{insertion_mode} = IN_BODY_IM;
5927              my $comment = $self->{document}->create_comment ($token->{data});          ## reprocess
5928              $self->{open_elements}->[0]->[0]->append_child ($comment);          next B;
5929          } elsif ($token->{type} == START_TAG_TOKEN) {
5930            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5931              !!!cp ('t303');
5932              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5933              
5934              ## Reprocess in the "after body" insertion mode.
5935            } else {
5936              !!!cp ('t304');
5937            }
5938    
5939            ## "after body" insertion mode
5940            !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5941    
5942            $self->{insertion_mode} = IN_BODY_IM;
5943            !!!ack-later;
5944            ## reprocess
5945            next B;
5946          } elsif ($token->{type} == END_TAG_TOKEN) {
5947            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5948              !!!cp ('t305');
5949              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5950              
5951              $self->{insertion_mode} = AFTER_BODY_IM;
5952              ## Reprocess in the "after body" insertion mode.
5953            } else {
5954              !!!cp ('t306');
5955            }
5956    
5957            ## "after body" insertion mode
5958            if ($token->{tag_name} eq 'html') {
5959              if (defined $self->{inner_html_node}) {
5960                !!!cp ('t307');
5961                !!!parse-error (type => 'unmatched end tag:html', token => $token);
5962                ## Ignore the token
5963              !!!next-token;              !!!next-token;
5964              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});  
             }  
5965            } else {            } else {
5966              !!!parse-error (type => 'after body:#'.$token->{type});              !!!cp ('t308');
5967                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5968                !!!next-token;
5969                next B;
5970            }            }
5971            } else {
5972              !!!cp ('t309');
5973              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5974    
5975            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
5976            ## reprocess            ## reprocess
5977            redo B;            next B;
5978          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
5979            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5980              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          !!!cp ('t309.2');
5981                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          ## Stop parsing
5982            last B;
5983          } else {
5984            die "$0: $token->{type}: Unknown token type";
5985          }
5986        } elsif ($self->{insertion_mode} & FRAME_IMS) {
5987          if ($token->{type} == CHARACTER_TOKEN) {
5988            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5989              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5990              
5991              unless (length $token->{data}) {
5992                !!!cp ('t310');
5993                !!!next-token;
5994                next B;
5995              }
5996            }
5997            
5998            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5999              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6000                !!!cp ('t311');
6001                !!!parse-error (type => 'in frameset:#character', token => $token);
6002              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6003                !!!cp ('t312');
6004                !!!parse-error (type => 'after frameset:#character', token => $token);
6005              } else { # "after html frameset"
6006                !!!cp ('t313');
6007                !!!parse-error (type => 'after html:#character', token => $token);
6008    
6009                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6010                ## Reprocess in the "after frameset" insertion mode.
6011                !!!parse-error (type => 'after frameset:#character', token => $token);
6012              }
6013              
6014              ## Ignore the token.
6015              if (length $token->{data}) {
6016                !!!cp ('t314');
6017                ## reprocess the rest of characters
6018              } else {
6019                !!!cp ('t315');
6020                !!!next-token;
6021              }
6022              next B;
6023            }
6024            
6025            die qq[$0: Character "$token->{data}"];
6026          } elsif ($token->{type} == START_TAG_TOKEN) {
6027            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6028              !!!cp ('t316');
6029              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6030    
6031              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6032              ## Process in the "after frameset" insertion mode.
6033            } else {
6034              !!!cp ('t317');
6035            }
6036    
6037            if ($token->{tag_name} eq 'frameset' and
6038                $self->{insertion_mode} == IN_FRAMESET_IM) {
6039              !!!cp ('t318');
6040              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6041              !!!nack ('t318.1');
6042              !!!next-token;
6043              next B;
6044            } elsif ($token->{tag_name} eq 'frame' and
6045                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6046              !!!cp ('t319');
6047              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6048              pop @{$self->{open_elements}};
6049              !!!ack ('t319.1');
6050              !!!next-token;
6051              next B;
6052            } elsif ($token->{tag_name} eq 'noframes') {
6053              !!!cp ('t320');
6054              ## NOTE: As if in body.
6055              $parse_rcdata->(CDATA_CONTENT_MODEL);
6056              next B;
6057            } else {
6058              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6059                !!!cp ('t321');
6060                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6061              } else {
6062                !!!cp ('t322');
6063                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6064              }
6065              ## Ignore the token
6066              !!!nack ('t322.1');
6067              !!!next-token;
6068              next B;
6069            }
6070          } elsif ($token->{type} == END_TAG_TOKEN) {
6071            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6072              !!!cp ('t323');
6073              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6074    
6075                unless (length $token->{data}) {            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6076                  !!!next-token;            ## Process in the "after frameset" insertion mode.
6077                  redo B;          } else {
6078                }            !!!cp ('t324');
6079              }          }
6080    
6081              #          if ($token->{tag_name} eq 'frameset' and
6082            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6083              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6084              $self->{open_elements}->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
6085                !!!cp ('t325');
6086                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6087                ## Ignore the token
6088              !!!next-token;              !!!next-token;
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
6089            } else {            } else {
6090              #              !!!cp ('t326');
6091                pop @{$self->{open_elements}};
6092                !!!next-token;
6093            }            }
6094              
6095            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
6096              !!!parse-error (type => 'in frameset:'.$token->{tag_name});                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6097                !!!cp ('t327');
6098                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6099              } else {
6100                !!!cp ('t328');
6101              }
6102              next B;
6103            } elsif ($token->{tag_name} eq 'html' and
6104                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6105              !!!cp ('t329');
6106              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6107              !!!next-token;
6108              next B;
6109            } else {
6110              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6111                !!!cp ('t330');
6112                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6113            } else {            } else {
6114              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t331');
6115                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6116            }            }
6117            ## Ignore the token            ## Ignore the token
6118            !!!next-token;            !!!next-token;
6119            redo B;            next B;
6120          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6121            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6122              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6123                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                  @{$self->{open_elements}} == 1) { # redundant, maybe
6124              !!!cp ('t331.1');
6125              !!!parse-error (type => 'in body:#eof', token => $token);
6126            } else {
6127              !!!cp ('t331.2');
6128            }
6129            
6130            ## Stop parsing
6131            last B;
6132          } else {
6133            die "$0: $token->{type}: Unknown token type";
6134          }
6135    
6136                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
6137                  !!!next-token;      } else {
6138                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
6139                }      }
6140    
6141        ## "in body" insertion mode
6142        if ($token->{type} == START_TAG_TOKEN) {
6143          if ($token->{tag_name} eq 'script') {
6144            !!!cp ('t332');
6145            ## NOTE: This is an "as if in head" code clone
6146            $script_start_tag->();
6147            next B;
6148          } elsif ($token->{tag_name} eq 'style') {
6149            !!!cp ('t333');
6150            ## NOTE: This is an "as if in head" code clone
6151            $parse_rcdata->(CDATA_CONTENT_MODEL);
6152            next B;
6153          } elsif ({
6154                    base => 1, link => 1,
6155                   }->{$token->{tag_name}}) {
6156            !!!cp ('t334');
6157            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6158            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6159            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6160            !!!ack ('t334.1');
6161            !!!next-token;
6162            next B;
6163          } elsif ($token->{tag_name} eq 'meta') {
6164            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6165            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6166            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6167    
6168            unless ($self->{confident}) {
6169              if ($token->{attributes}->{charset}) { ## TODO: And if supported
6170                !!!cp ('t335');
6171                $self->{change_encoding}
6172                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6173                
6174                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6175                    ->set_user_data (manakai_has_reference =>
6176                                         $token->{attributes}->{charset}
6177                                             ->{has_reference});
6178              } elsif ($token->{attributes}->{content}) {
6179                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
6180                if ($token->{attributes}->{content}->{value}
6181                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6182                        [\x09-\x0D\x20]*=
6183                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6184                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6185                  !!!cp ('t336');
6186                  $self->{change_encoding}
6187                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6188                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6189                      ->set_user_data (manakai_has_reference =>
6190                                           $token->{attributes}->{content}
6191                                                 ->{has_reference});
6192              }              }
6193              }
6194            } else {
6195              if ($token->{attributes}->{charset}) {
6196                !!!cp ('t337');
6197                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6198                    ->set_user_data (manakai_has_reference =>
6199                                         $token->{attributes}->{charset}
6200                                             ->{has_reference});
6201              }
6202              if ($token->{attributes}->{content}) {
6203                !!!cp ('t338');
6204                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6205                    ->set_user_data (manakai_has_reference =>
6206                                         $token->{attributes}->{content}
6207                                             ->{has_reference});
6208              }
6209            }
6210    
6211              #          !!!ack ('t338.1');
6212            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6213              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6214              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6215              !!!next-token;          !!!cp ('t341');
6216              redo B;          ## NOTE: This is an "as if in head" code clone
6217            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6218              if ($token->{tag_name} eq 'noframes') {          next B;
6219                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6220                redo B;          !!!parse-error (type => 'in body:body', token => $token);
6221              } else {                
6222                #          if (@{$self->{open_elements}} == 1 or
6223                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6224              !!!cp ('t342');
6225              ## Ignore the token
6226            } else {
6227              my $body_el = $self->{open_elements}->[1]->[0];
6228              for my $attr_name (keys %{$token->{attributes}}) {
6229                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6230                  !!!cp ('t343');
6231                  $body_el->set_attribute_ns
6232                    (undef, [undef, $attr_name],
6233                     $token->{attributes}->{$attr_name}->{value});
6234              }              }
6235            } elsif ($token->{type} eq 'end tag') {            }
6236              if ($token->{tag_name} eq 'html') {          }
6237                $phase = 'trailing end';          !!!nack ('t343.1');
6238            !!!next-token;
6239            next B;
6240          } elsif ({
6241                    address => 1, blockquote => 1, center => 1, dir => 1,
6242                    div => 1, dl => 1, fieldset => 1,
6243                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6244                    menu => 1, ol => 1, p => 1, ul => 1,
6245                    pre => 1, listing => 1,
6246                    form => 1,
6247                    table => 1,
6248                    hr => 1,
6249                   }->{$token->{tag_name}}) {
6250            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6251              !!!cp ('t350');
6252              !!!parse-error (type => 'in form:form', token => $token);
6253              ## Ignore the token
6254              !!!nack ('t350.1');
6255              !!!next-token;
6256              next B;
6257            }
6258    
6259            ## has a p element in scope
6260            INSCOPE: for (reverse @{$self->{open_elements}}) {
6261              if ($_->[1] & P_EL) {
6262                !!!cp ('t344');
6263                !!!back-token; # <form>
6264                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6265                          line => $token->{line}, column => $token->{column}};
6266                next B;
6267              } elsif ($_->[1] & SCOPING_EL) {
6268                !!!cp ('t345');
6269                last INSCOPE;
6270              }
6271            } # INSCOPE
6272              
6273            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6274            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6275              !!!nack ('t346.1');
6276              !!!next-token;
6277              if ($token->{type} == CHARACTER_TOKEN) {
6278                $token->{data} =~ s/^\x0A//;
6279                unless (length $token->{data}) {
6280                  !!!cp ('t346');
6281                !!!next-token;                !!!next-token;
               redo B;  
6282              } else {              } else {
6283                #                !!!cp ('t349');
6284              }              }
6285            } else {            } else {
6286              #              !!!cp ('t348');
6287            }            }
6288            } elsif ($token->{tag_name} eq 'form') {
6289              !!!cp ('t347.1');
6290              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6291    
6292              !!!nack ('t347.2');
6293              !!!next-token;
6294            } elsif ($token->{tag_name} eq 'table') {
6295              !!!cp ('t382');
6296              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6297                        
6298            if (defined $token->{tag_name}) {            $self->{insertion_mode} = IN_TABLE_IM;
6299              !!!parse-error (type => 'after frameset:'.$token->{tag_name});  
6300              !!!nack ('t382.1');
6301              !!!next-token;
6302            } elsif ($token->{tag_name} eq 'hr') {
6303              !!!cp ('t386');
6304              pop @{$self->{open_elements}};
6305            
6306              !!!nack ('t386.1');
6307              !!!next-token;
6308            } else {
6309              !!!nack ('t347.1');
6310              !!!next-token;
6311            }
6312            next B;
6313          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6314            ## has a p element in scope
6315            INSCOPE: for (reverse @{$self->{open_elements}}) {
6316              if ($_->[1] & P_EL) {
6317                !!!cp ('t353');
6318                !!!back-token; # <x>
6319                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6320                          line => $token->{line}, column => $token->{column}};
6321                next B;
6322              } elsif ($_->[1] & SCOPING_EL) {
6323                !!!cp ('t354');
6324                last INSCOPE;
6325              }
6326            } # INSCOPE
6327              
6328            ## Step 1
6329            my $i = -1;
6330            my $node = $self->{open_elements}->[$i];
6331            my $li_or_dtdd = {li => {li => 1},
6332                              dt => {dt => 1, dd => 1},
6333                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6334            LI: {
6335              ## Step 2
6336              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6337                if ($i != -1) {
6338                  !!!cp ('t355');
6339                  !!!parse-error (type => 'not closed',
6340                                  value => $self->{open_elements}->[-1]->[0]
6341                                      ->manakai_local_name,
6342                                  token => $token);
6343                } else {
6344                  !!!cp ('t356');
6345                }
6346                splice @{$self->{open_elements}}, $i;
6347                last LI;
6348            } else {            } else {
6349              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
6350              }
6351              
6352              ## Step 3
6353              if (not ($node->[1] & FORMATTING_EL) and
6354                  #not $phrasing_category->{$node->[1]} and
6355                  ($node->[1] & SPECIAL_EL or
6356                   $node->[1] & SCOPING_EL) and
6357                  not ($node->[1] & ADDRESS_EL) and
6358                  not ($node->[1] & DIV_EL)) {
6359                !!!cp ('t358');
6360                last LI;
6361              }
6362              
6363              !!!cp ('t359');
6364              ## Step 4
6365              $i--;
6366              $node = $self->{open_elements}->[$i];
6367              redo LI;
6368            } # LI
6369              
6370            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6371            !!!nack ('t359.1');
6372            !!!next-token;
6373            next B;
6374          } elsif ($token->{tag_name} eq 'plaintext') {
6375            ## has a p element in scope
6376            INSCOPE: for (reverse @{$self->{open_elements}}) {
6377              if ($_->[1] & P_EL) {
6378                !!!cp ('t367');
6379                !!!back-token; # <plaintext>
6380                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6381                          line => $token->{line}, column => $token->{column}};
6382                next B;
6383              } elsif ($_->[1] & SCOPING_EL) {
6384                !!!cp ('t368');
6385                last INSCOPE;
6386            }            }
6387            } # INSCOPE
6388              
6389            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6390              
6391            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6392              
6393            !!!nack ('t368.1');
6394            !!!next-token;
6395            next B;
6396          } elsif ($token->{tag_name} eq 'a') {
6397            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6398              my $node = $active_formatting_elements->[$i];
6399              if ($node->[1] & A_EL) {
6400                !!!cp ('t371');
6401                !!!parse-error (type => 'in a:a', token => $token);
6402                
6403                !!!back-token; # <a>
6404                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6405                          line => $token->{line}, column => $token->{column}};
6406                $formatting_end_tag->($token);
6407                
6408                AFE2: for (reverse 0..$#$active_formatting_elements) {
6409                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6410                    !!!cp ('t372');
6411                    splice @$active_formatting_elements, $_, 1;
6412                    last AFE2;
6413                  }
6414                } # AFE2
6415                OE: for (reverse 0..$#{$self->{open_elements}}) {
6416                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6417                    !!!cp ('t373');
6418                    splice @{$self->{open_elements}}, $_, 1;
6419                    last OE;
6420                  }
6421                } # OE
6422                last AFE;
6423              } elsif ($node->[0] eq '#marker') {
6424                !!!cp ('t374');
6425                last AFE;
6426              }
6427            } # AFE
6428              
6429            $reconstruct_active_formatting_elements->($insert_to_current);
6430    
6431            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6432            push @$active_formatting_elements, $self->{open_elements}->[-1];
6433    
6434            !!!nack ('t374.1');
6435            !!!next-token;
6436            next B;
6437          } elsif ($token->{tag_name} eq 'nobr') {
6438            $reconstruct_active_formatting_elements->($insert_to_current);
6439    
6440            ## has a |nobr| element in scope
6441            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6442              my $node = $self->{open_elements}->[$_];
6443              if ($node->[1] & NOBR_EL) {
6444                !!!cp ('t376');
6445                !!!parse-error (type => 'in nobr:nobr', token => $token);
6446                !!!back-token; # <nobr>
6447                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6448                          line => $token->{line}, column => $token->{column}};
6449                next B;
6450              } elsif ($node->[1] & SCOPING_EL) {
6451                !!!cp ('t377');
6452                last INSCOPE;
6453              }
6454            } # INSCOPE
6455            
6456            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6457            push @$active_formatting_elements, $self->{open_elements}->[-1];
6458            
6459            !!!nack ('t377.1');
6460            !!!next-token;
6461            next B;
6462          } elsif ($token->{tag_name} eq 'button') {
6463            ## has a button element in scope
6464            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6465              my $node = $self->{open_elements}->[$_];
6466              if ($node->[1] & BUTTON_EL) {
6467                !!!cp ('t378');
6468                !!!parse-error (type => 'in button:button', token => $token);
6469                !!!back-token; # <button>
6470                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6471                          line => $token->{line}, column => $token->{column}};
6472                next B;
6473              } elsif ($node->[1] & SCOPING_EL) {
6474                !!!cp ('t379');
6475                last INSCOPE;
6476              }
6477            } # INSCOPE
6478              
6479            $reconstruct_active_formatting_elements->($insert_to_current);
6480              
6481            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6482    
6483            ## TODO: associate with $self->{form_element} if defined
6484    
6485            push @$active_formatting_elements, ['#marker', ''];
6486    
6487            !!!nack ('t379.1');
6488            !!!next-token;
6489            next B;
6490          } elsif ({
6491                    xmp => 1,
6492                    iframe => 1,
6493                    noembed => 1,
6494                    noframes => 1,
6495                    noscript => 0, ## TODO: 1 if scripting is enabled
6496                   }->{$token->{tag_name}}) {
6497            if ($token->{tag_name} eq 'xmp') {
6498              !!!cp ('t381');
6499              $reconstruct_active_formatting_elements->($insert_to_current);
6500            } else {
6501              !!!cp ('t399');
6502            }
6503            ## NOTE: There is an "as if in body" code clone.
6504            $parse_rcdata->(CDATA_CONTENT_MODEL);
6505            next B;
6506          } elsif ($token->{tag_name} eq 'isindex') {
6507            !!!parse-error (type => 'isindex', token => $token);
6508            
6509            if (defined $self->{form_element}) {
6510              !!!cp ('t389');
6511            ## Ignore the token            ## Ignore the token
6512              !!!nack ('t389'); ## NOTE: Not acknowledged.
6513            !!!next-token;            !!!next-token;
6514            redo B;            next B;
6515            } else {
6516              my $at = $token->{attributes};
6517              my $form_attrs;
6518              $form_attrs->{action} = $at->{action} if $at->{action};
6519              my $prompt_attr = $at->{prompt};
6520              $at->{name} = {name => 'name', value => 'isindex'};
6521              delete $at->{action};
6522              delete $at->{prompt};
6523              my @tokens = (
6524                            {type => START_TAG_TOKEN, tag_name => 'form',
6525                             attributes => $form_attrs,
6526                             line => $token->{line}, column => $token->{column}},
6527                            {type => START_TAG_TOKEN, tag_name => 'hr',
6528                             line => $token->{line}, column => $token->{column}},
6529                            {type => START_TAG_TOKEN, tag_name => 'p',
6530                             line => $token->{line}, column => $token->{column}},
6531                            {type => START_TAG_TOKEN, tag_name => 'label',
6532                             line => $token->{line}, column => $token->{column}},
6533                           );
6534              if ($prompt_attr) {
6535                !!!cp ('t390');
6536                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6537                               #line => $token->{line}, column => $token->{column},
6538                              };
6539              } else {
6540                !!!cp ('t391');
6541                push @tokens, {type => CHARACTER_TOKEN,
6542                               data => 'This is a searchable index. Insert your search keywords here: ',
6543                               #line => $token->{line}, column => $token->{column},
6544                              }; # SHOULD
6545                ## TODO: make this configurable
6546              }
6547              push @tokens,
6548                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6549                             line => $token->{line}, column => $token->{column}},
6550                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6551                            {type => END_TAG_TOKEN, tag_name => 'label',
6552                             line => $token->{line}, column => $token->{column}},
6553                            {type => END_TAG_TOKEN, tag_name => 'p',
6554                             line => $token->{line}, column => $token->{column}},
6555                            {type => START_TAG_TOKEN, tag_name => 'hr',
6556                             line => $token->{line}, column => $token->{column}},
6557                            {type => END_TAG_TOKEN, tag_name => 'form',
6558                             line => $token->{line}, column => $token->{column}};
6559              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6560              !!!back-token (@tokens);
6561              !!!next-token;
6562              next B;
6563            }
6564          } elsif ($token->{tag_name} eq 'textarea') {
6565            my $tag_name = $token->{tag_name};
6566            my $el;
6567            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6568            
6569            ## TODO: $self->{form_element} if defined
6570            $self->{content_model} = RCDATA_CONTENT_MODEL;
6571            delete $self->{escape}; # MUST
6572            
6573            $insert->($el);
6574            
6575            my $text = '';
6576            !!!nack ('t392.1');
6577            !!!next-token;
6578            if ($token->{type} == CHARACTER_TOKEN) {
6579              $token->{data} =~ s/^\x0A//;
6580              unless (length $token->{data}) {
6581                !!!cp ('t392');
6582                !!!next-token;
6583              } else {
6584                !!!cp ('t393');
6585              }
6586            } else {
6587              !!!cp ('t394');
6588            }
6589            while ($token->{type} == CHARACTER_TOKEN) {
6590              !!!cp ('t395');
6591              $text .= $token->{data};
6592              !!!next-token;
6593            }
6594            if (length $text) {
6595              !!!cp ('t396');
6596              $el->manakai_append_text ($text);
6597            }
6598            
6599            $self->{content_model} = PCDATA_CONTENT_MODEL;
6600            
6601            if ($token->{type} == END_TAG_TOKEN and
6602                $token->{tag_name} eq $tag_name) {
6603              !!!cp ('t397');
6604              ## Ignore the token
6605            } else {
6606              !!!cp ('t398');
6607              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6608            }
6609            !!!next-token;
6610            next B;
6611          } elsif ($token->{tag_name} eq 'math' or
6612                   $token->{tag_name} eq 'svg') {
6613            $reconstruct_active_formatting_elements->($insert_to_current);
6614    
6615            ## ISSUE: An issue in spec there          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6616    
6617            ## "adjust foreign attributes" - done in insert-element-f
6618            
6619            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6620            
6621            if ($self->{self_closing}) {
6622              pop @{$self->{open_elements}};
6623              !!!ack ('t398.1');
6624          } else {          } else {
6625            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t398.2');
6626              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6627              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6628              ## mode, "in body" (not "in foreign content") secondary insertion
6629              ## mode, maybe.
6630          }          }
6631        }  
6632      } elsif ($phase eq 'trailing end') {          !!!next-token;
6633        ## states in the main stage is preserved yet # MUST          next B;
6634                } elsif ({
6635        if ($token->{type} eq 'DOCTYPE') {                  caption => 1, col => 1, colgroup => 1, frame => 1,
6636          !!!parse-error (type => 'after html:#DOCTYPE');                  frameset => 1, head => 1, option => 1, optgroup => 1,
6637                    tbody => 1, td => 1, tfoot => 1, th => 1,
6638                    thead => 1, tr => 1,
6639                   }->{$token->{tag_name}}) {
6640            !!!cp ('t401');
6641            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6642          ## Ignore the token          ## Ignore the token
6643            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6644          !!!next-token;          !!!next-token;
6645          redo B;          next B;
6646        } elsif ($token->{type} eq 'comment') {          
6647          my $comment = $self->{document}->create_comment ($token->{data});          ## ISSUE: An issue on HTML5 new elements in the spec.
6648          $self->{document}->append_child ($comment);        } else {
6649            if ($token->{tag_name} eq 'image') {
6650              !!!cp ('t384');
6651              !!!parse-error (type => 'image', token => $token);
6652              $token->{tag_name} = 'img';
6653            } else {
6654              !!!cp ('t385');
6655            }
6656    
6657            ## NOTE: There is an "as if <br>" code clone.
6658            $reconstruct_active_formatting_elements->($insert_to_current);
6659            
6660            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6661    
6662            if ({
6663                 applet => 1, marquee => 1, object => 1,
6664                }->{$token->{tag_name}}) {
6665              !!!cp ('t380');
6666              push @$active_formatting_elements, ['#marker', ''];
6667              !!!nack ('t380.1');
6668            } elsif ({
6669                      b => 1, big => 1, em => 1, font => 1, i => 1,
6670                      s => 1, small => 1, strile => 1,
6671                      strong => 1, tt => 1, u => 1,
6672                     }->{$token->{tag_name}}) {
6673              !!!cp ('t375');
6674              push @$active_formatting_elements, $self->{open_elements}->[-1];
6675              !!!nack ('t375.1');
6676            } elsif ($token->{tag_name} eq 'input') {
6677              !!!cp ('t388');
6678              ## TODO: associate with $self->{form_element} if defined
6679              pop @{$self->{open_elements}};
6680              !!!ack ('t388.2');
6681            } elsif ({
6682                      area => 1, basefont => 1, bgsound => 1, br => 1,
6683                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6684                      #image => 1,
6685                     }->{$token->{tag_name}}) {
6686              !!!cp ('t388.1');
6687              pop @{$self->{open_elements}};
6688              !!!ack ('t388.3');
6689            } elsif ($token->{tag_name} eq 'select') {
6690              ## TODO: associate with $self->{form_element} if defined
6691            
6692              if ($self->{insertion_mode} & TABLE_IMS or
6693                  $self->{insertion_mode} & BODY_TABLE_IMS or
6694                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6695                !!!cp ('t400.1');
6696                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6697              } else {
6698                !!!cp ('t400.2');
6699                $self->{insertion_mode} = IN_SELECT_IM;
6700              }
6701              !!!nack ('t400.3');
6702            } else {
6703              !!!nack ('t402');
6704            }
6705            
6706          !!!next-token;          !!!next-token;
6707          redo B;          next B;
6708        } elsif ($token->{type} eq 'character') {        }
6709          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6710            my $data = $1;        if ($token->{tag_name} eq 'body') {
6711            ## As if in the main phase.          ## has a |body| element in scope
6712            ## NOTE: The insertion mode in the main phase          my $i;
6713            ## just before the phase has been changed to the trailing          INSCOPE: {
6714            ## end phase is either "after body" or "after frameset".            for (reverse @{$self->{open_elements}}) {
6715            $reconstruct_active_formatting_elements->($insert_to_current)              if ($_->[1] & BODY_EL) {
6716              if $phase eq 'main';                !!!cp ('t405');
6717                  $i = $_;
6718                  last INSCOPE;
6719                } elsif ($_->[1] & SCOPING_EL) {
6720                  !!!cp ('t405.1');
6721                  last;
6722                }
6723              }
6724    
6725              !!!parse-error (type => 'start tag not allowed',
6726                              value => $token->{tag_name}, token => $token);
6727              ## NOTE: Ignore the token.
6728              !!!next-token;
6729              next B;
6730            } # INSCOPE
6731    
6732            for (@{$self->{open_elements}}) {
6733              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6734                !!!cp ('t403');
6735                !!!parse-error (type => 'not closed',
6736                                value => $_->[0]->manakai_local_name,
6737                                token => $token);
6738                last;
6739              } else {
6740                !!!cp ('t404');
6741              }
6742            }
6743    
6744            $self->{insertion_mode} = AFTER_BODY_IM;
6745            !!!next-token;
6746            next B;
6747          } elsif ($token->{tag_name} eq 'html') {
6748            ## TODO: Update this code.  It seems that the code below is not
6749            ## up-to-date, though it has same effect as speced.
6750            if (@{$self->{open_elements}} > 1 and
6751                $self->{open_elements}->[1]->[1] & BODY_EL) {
6752              ## ISSUE: There is an issue in the spec.
6753              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6754                !!!cp ('t406');
6755                !!!parse-error (type => 'not closed',
6756                                value => $self->{open_elements}->[1]->[0]
6757                                    ->manakai_local_name,
6758                                token => $token);
6759              } else {
6760                !!!cp ('t407');
6761              }
6762              $self->{insertion_mode} = AFTER_BODY_IM;
6763              ## reprocess
6764              next B;
6765            } else {
6766              !!!cp ('t408');
6767              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6768              ## Ignore the token
6769              !!!next-token;
6770              next B;
6771            }
6772          } elsif ({
6773                    address => 1, blockquote => 1, center => 1, dir => 1,
6774                    div => 1, dl => 1, fieldset => 1, listing => 1,
6775                    menu => 1, ol => 1, pre => 1, ul => 1,
6776                    dd => 1, dt => 1, li => 1,
6777                    applet => 1, button => 1, marquee => 1, object => 1,
6778                   }->{$token->{tag_name}}) {
6779            ## has an element in scope
6780            my $i;
6781            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6782              my $node = $self->{open_elements}->[$_];
6783              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6784                !!!cp ('t410');
6785                $i = $_;
6786                last INSCOPE;
6787              } elsif ($node->[1] & SCOPING_EL) {
6788                !!!cp ('t411');
6789                last INSCOPE;
6790              }
6791            } # INSCOPE
6792    
6793            unless (defined $i) { # has an element in scope
6794              !!!cp ('t413');
6795              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6796            } else {
6797              ## Step 1. generate implied end tags
6798              while ({
6799                      dd => ($token->{tag_name} ne 'dd'),
6800                      dt => ($token->{tag_name} ne 'dt'),
6801                      li => ($token->{tag_name} ne 'li'),
6802                      p => 1,
6803                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6804                !!!cp ('t409');
6805                pop @{$self->{open_elements}};
6806              }
6807    
6808              ## Step 2.
6809              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6810                      ne $token->{tag_name}) {
6811                !!!cp ('t412');
6812                !!!parse-error (type => 'not closed',
6813                                value => $self->{open_elements}->[-1]->[0]
6814                                    ->manakai_local_name,
6815                                token => $token);
6816              } else {
6817                !!!cp ('t414');
6818              }
6819    
6820              ## Step 3.
6821              splice @{$self->{open_elements}}, $i;
6822    
6823              ## Step 4.
6824              $clear_up_to_marker->()
6825                  if {
6826                    applet => 1, button => 1, marquee => 1, object => 1,
6827                  }->{$token->{tag_name}};
6828            }
6829            !!!next-token;
6830            next B;
6831          } elsif ($token->{tag_name} eq 'form') {
6832            undef $self->{form_element};
6833    
6834            ## has an element in scope
6835            my $i;
6836            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6837              my $node = $self->{open_elements}->[$_];
6838              if ($node->[1] & FORM_EL) {
6839                !!!cp ('t418');
6840                $i = $_;
6841                last INSCOPE;
6842              } elsif ($node->[1] & SCOPING_EL) {
6843                !!!cp ('t419');
6844                last INSCOPE;
6845              }
6846            } # INSCOPE
6847    
6848            unless (defined $i) { # has an element in scope
6849              !!!cp ('t421');
6850              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6851            } else {
6852              ## Step 1. generate implied end tags
6853              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6854                !!!cp ('t417');
6855                pop @{$self->{open_elements}};
6856              }
6857                        
6858            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
6859              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6860                      ne $token->{tag_name}) {
6861                !!!cp ('t417.1');
6862                !!!parse-error (type => 'not closed',
6863                                value => $self->{open_elements}->[-1]->[0]
6864                                    ->manakai_local_name,
6865                                token => $token);
6866              } else {
6867                !!!cp ('t420');
6868              }  
6869                        
6870            unless (length $token->{data}) {            ## Step 3.
6871              !!!next-token;            splice @{$self->{open_elements}}, $i;
6872              redo B;          }
6873    
6874            !!!next-token;
6875            next B;
6876          } elsif ({
6877                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6878                   }->{$token->{tag_name}}) {
6879            ## has an element in scope
6880            my $i;
6881            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6882              my $node = $self->{open_elements}->[$_];
6883              if ($node->[1] & HEADING_EL) {
6884                !!!cp ('t423');
6885                $i = $_;
6886                last INSCOPE;
6887              } elsif ($node->[1] & SCOPING_EL) {
6888                !!!cp ('t424');
6889                last INSCOPE;
6890              }
6891            } # INSCOPE
6892    
6893            unless (defined $i) { # has an element in scope
6894              !!!cp ('t425.1');
6895              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6896            } else {
6897              ## Step 1. generate implied end tags
6898              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6899                !!!cp ('t422');
6900                pop @{$self->{open_elements}};
6901              }
6902              
6903              ## Step 2.
6904              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6905                      ne $token->{tag_name}) {
6906                !!!cp ('t425');
6907                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6908              } else {
6909                !!!cp ('t426');
6910            }            }
6911    
6912              ## Step 3.
6913              splice @{$self->{open_elements}}, $i;
6914          }          }
6915            
6916            !!!next-token;
6917            next B;
6918          } elsif ($token->{tag_name} eq 'p') {
6919            ## has an element in scope
6920            my $i;
6921            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6922              my $node = $self->{open_elements}->[$_];
6923              if ($node->[1] & P_EL) {
6924                !!!cp ('t410.1');
6925                $i = $_;
6926                last INSCOPE;
6927              } elsif ($node->[1] & SCOPING_EL) {
6928                !!!cp ('t411.1');
6929                last INSCOPE;
6930              }
6931            } # INSCOPE
6932    
6933          !!!parse-error (type => 'after html:#character');          if (defined $i) {
6934          $phase = 'main';            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6935          ## reprocess                    ne $token->{tag_name}) {
6936          redo B;              !!!cp ('t412.1');
6937        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
6938                 $token->{type} eq 'end tag') {                              value => $self->{open_elements}->[-1]->[0]
6939          !!!parse-error (type => 'after html:'.$token->{tag_name});                                  ->manakai_local_name,
6940          $phase = 'main';                              token => $token);
6941          ## reprocess            } else {
6942          redo B;              !!!cp ('t414.1');
6943        } elsif ($token->{type} eq 'end-of-file') {            }
6944          ## Stop parsing  
6945          last B;            splice @{$self->{open_elements}}, $i;
6946            } else {
6947              !!!cp ('t413.1');
6948              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6949    
6950              !!!cp ('t415.1');
6951              ## As if <p>, then reprocess the current token
6952              my $el;
6953              !!!create-element ($el, $HTML_NS, 'p',, $token);
6954              $insert->($el);
6955              ## NOTE: Not inserted into |$self->{open_elements}|.
6956            }
6957    
6958            !!!next-token;
6959            next B;
6960          } elsif ({
6961                    a => 1,
6962                    b => 1, big => 1, em => 1, font => 1, i => 1,
6963                    nobr => 1, s => 1, small => 1, strile => 1,
6964                    strong => 1, tt => 1, u => 1,
6965                   }->{$token->{tag_name}}) {
6966            !!!cp ('t427');
6967            $formatting_end_tag->($token);
6968            next B;
6969          } elsif ($token->{tag_name} eq 'br') {
6970            !!!cp ('t428');
6971            !!!parse-error (type => 'unmatched end tag:br', token => $token);
6972    
6973            ## As if <br>
6974            $reconstruct_active_formatting_elements->($insert_to_current);
6975            
6976            my $el;
6977            !!!create-element ($el, $HTML_NS, 'br',, $token);
6978            $insert->($el);
6979            
6980            ## Ignore the token.
6981            !!!next-token;
6982            next B;
6983          } elsif ({
6984                    caption => 1, col => 1, colgroup => 1, frame => 1,
6985                    frameset => 1, head => 1, option => 1, optgroup => 1,
6986                    tbody => 1, td => 1, tfoot => 1, th => 1,
6987                    thead => 1, tr => 1,
6988                    area => 1, basefont => 1, bgsound => 1,
6989                    embed => 1, hr => 1, iframe => 1, image => 1,
6990                    img => 1, input => 1, isindex => 1, noembed => 1,
6991                    noframes => 1, param => 1, select => 1, spacer => 1,
6992                    table => 1, textarea => 1, wbr => 1,
6993                    noscript => 0, ## TODO: if scripting is enabled
6994                   }->{$token->{tag_name}}) {
6995            !!!cp ('t429');
6996            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6997            ## Ignore the token
6998            !!!next-token;
6999            next B;
7000            
7001            ## ISSUE: Issue on HTML5 new elements in spec
7002            
7003        } else {        } else {
7004          die "$0: $token->{type}: Unknown token";          ## Step 1
7005            my $node_i = -1;
7006            my $node = $self->{open_elements}->[$node_i];
7007    
7008            ## Step 2
7009            S2: {
7010              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7011                ## Step 1
7012                ## generate implied end tags
7013                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7014                  !!!cp ('t430');
7015                  ## ISSUE: Can this case be reached?
7016                  pop @{$self->{open_elements}};
7017                }
7018            
7019                ## Step 2
7020                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7021                        ne $token->{tag_name}) {
7022                  !!!cp ('t431');
7023                  ## NOTE: <x><y></x>
7024                  !!!parse-error (type => 'not closed',
7025                                  value => $self->{open_elements}->[-1]->[0]
7026                                      ->manakai_local_name,
7027                                  token => $token);
7028                } else {
7029                  !!!cp ('t432');
7030                }
7031                
7032                ## Step 3
7033                splice @{$self->{open_elements}}, $node_i;
7034    
7035                !!!next-token;
7036                last S2;
7037              } else {
7038                ## Step 3
7039                if (not ($node->[1] & FORMATTING_EL) and
7040                    #not $phrasing_category->{$node->[1]} and
7041                    ($node->[1] & SPECIAL_EL or
7042                     $node->[1] & SCOPING_EL)) {
7043                  !!!cp ('t433');
7044                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7045                  ## Ignore the token
7046                  !!!next-token;
7047                  last S2;
7048                }
7049    
7050                !!!cp ('t434');
7051              }
7052              
7053              ## Step 4
7054              $node_i--;
7055              $node = $self->{open_elements}->[$node_i];
7056              
7057              ## Step 5;
7058              redo S2;
7059            } # S2
7060            next B;
7061        }        }
7062      }      }
7063        next B;
7064      } continue { # B
7065        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7066          ## NOTE: The code below is executed in cases where it does not have
7067          ## to be, but it it is harmless even in those cases.
7068          ## has an element in scope
7069          INSCOPE: {
7070            for (reverse 0..$#{$self->{open_elements}}) {
7071              my $node = $self->{open_elements}->[$_];
7072              if ($node->[1] & FOREIGN_EL) {
7073                last INSCOPE;
7074              } elsif ($node->[1] & SCOPING_EL) {
7075                last;
7076              }
7077            }
7078            
7079            ## NOTE: No foreign element in scope.
7080            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7081          } # INSCOPE
7082        }
7083    } # B    } # B
7084    
7085    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4843  sub set_inner_html ($$$) { Line 7093  sub set_inner_html ($$$) {
7093    my $s = \$_[0];    my $s = \$_[0];
7094    my $onerror = $_[1];    my $onerror = $_[1];
7095    
7096      ## ISSUE: Should {confident} be true?
7097    
7098    my $nt = $node->node_type;    my $nt = $node->node_type;
7099    if ($nt == 9) {    if ($nt == 9) {
7100      # MUST      # MUST
# Line 4865  sub set_inner_html ($$$) { Line 7117  sub set_inner_html ($$$) {
7117      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7118    
7119      ## Step 1 # MUST      ## Step 1 # MUST
7120      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
7121      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
7122        $doc->manakai_is_html (1);
7123      my $p = $class->new;      my $p = $class->new;
7124      $p->{document} = $doc;      $p->{document} = $doc;
7125    
7126      ## Step 9 # MUST      ## Step 8 # MUST
7127      my $i = 0;      my $i = 0;
7128      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7129      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7130      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7131        my $self = shift;        my $self = shift;
7132        $self->{next_input_character} = -1 and return if $i >= length $$s;  
7133        $self->{next_input_character} = ord substr $$s, $i++, 1;        pop @{$self->{prev_char}};
7134        $column++;        unshift @{$self->{prev_char}}, $self->{next_char};
7135    
7136        if ($self->{next_input_character} == 0x000A) { # LF        $self->{next_char} = -1 and return if $i >= length $$s;
7137          $line++;        $self->{next_char} = ord substr $$s, $i++, 1;
7138          $column = 0;  
7139        } elsif ($self->{next_input_character} == 0x000D) { # CR        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7140          if ($i >= length $$s) {        $p->{column}++;
7141            #  
7142          } else {        if ($self->{next_char} == 0x000A) { # LF
7143            my $next_char = ord substr $$s, $i++, 1;          $p->{line}++;
7144            if ($next_char == 0x000A) { # LF          $p->{column} = 0;
7145              #          !!!cp ('i1');
7146            } else {        } elsif ($self->{next_char} == 0x000D) { # CR
7147              push @{$self->{char}}, $next_char;          $i++ if substr ($$s, $i, 1) eq "\x0A";
7148            }          $self->{next_char} = 0x000A; # LF # MUST
7149          }          $p->{line}++;
7150          $self->{next_input_character} = 0x000A; # LF # MUST          $p->{column} = 0;
7151          $line++;          !!!cp ('i2');
7152          $column = 0;        } elsif ($self->{next_char} > 0x10FFFF) {
7153        } elsif ($self->{next_input_character} > 0x10FFFF) {          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7154          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          !!!cp ('i3');
7155        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_char} == 0x0000) { # NULL
7156          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          !!!cp ('i4');
7157            !!!parse-error (type => 'NULL');
7158            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7159          } elsif ($self->{next_char} <= 0x0008 or
7160                   (0x000E <= $self->{next_char} and
7161                    $self->{next_char} <= 0x001F) or
7162                   (0x007F <= $self->{next_char} and
7163                    $self->{next_char} <= 0x009F) or
7164                   (0xD800 <= $self->{next_char} and
7165                    $self->{next_char} <= 0xDFFF) or
7166                   (0xFDD0 <= $self->{next_char} and
7167                    $self->{next_char} <= 0xFDDF) or
7168                   {
7169                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7170                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7171                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7172                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7173                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7174                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7175                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7176                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7177                    0x10FFFE => 1, 0x10FFFF => 1,
7178                   }->{$self->{next_char}}) {
7179            !!!cp ('i4.1');
7180            !!!parse-error (type => 'control char', level => $self->{must_level});
7181    ## TODO: error type documentation
7182        }        }
7183      };      };
7184        $p->{prev_char} = [-1, -1, -1];
7185        $p->{next_char} = -1;
7186            
7187      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7188        my (%opt) = @_;        my (%opt) = @_;
7189        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7190          my $column = $opt{column};
7191          if (defined $opt{token} and defined $opt{token}->{line}) {
7192            $line = $opt{token}->{line};
7193            $column = $opt{token}->{column};
7194          }
7195          warn "Parse error ($opt{type}) at line $line column $column\n";
7196      };      };
7197      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7198        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7199      };      };
7200            
7201      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7202      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7203    
7204      ## Step 2      ## Step 2
7205      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7206      $p->{content_model_flag} = {      $p->{content_model} = {
7207        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7208        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7209        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7210        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7211        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7212        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7213        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7214        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7215        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7216        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7217      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7218         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7219            unless defined $p->{content_model};
7220            ## ISSUE: What is "the name of the element"? local name?
7221    
7222      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7223          ## TODO: Foreign element OK?
7224    
7225      ## Step 4      ## Step 3
7226      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7227        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7228    
7229      ## Step 5 # MUST      ## Step 4 # MUST
7230      $doc->append_child ($root);      $doc->append_child ($root);
7231    
7232      ## Step 6 # MUST      ## Step 5 # MUST
7233      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7234    
7235      undef $p->{head_element};      undef $p->{head_element};
7236    
7237      ## Step 7 # MUST      ## Step 6 # MUST
7238      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7239    
7240      ## Step 8 # MUST      ## Step 7 # MUST
7241      my $anode = $node;      my $anode = $node;
7242      AN: while (defined $anode) {      AN: while (defined $anode) {
7243        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7244          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7245          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7246            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7247                !!!cp ('i5');
7248              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7249              last AN;              last AN;
7250            }            }
# Line 4963  sub set_inner_html ($$$) { Line 7253  sub set_inner_html ($$$) {
7253        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7254      } # AN      } # AN
7255            
7256      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7257      {      {
7258        my $self = $p;        my $self = $p;
7259        !!!next-token;        !!!next-token;
7260      }      }
7261      $p->_tree_construction_main;      $p->_tree_construction_main;
7262    
7263      ## Step 11 # MUST      ## Step 10 # MUST
7264      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7265      for (@cn) {      for (@cn) {
7266        $node->remove_child ($_);        $node->remove_child ($_);
7267      }      }
7268      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7269    
7270      ## Step 12 # MUST      ## Step 11 # MUST
7271      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7272      for (@cn) {      for (@cn) {
7273          $this_doc->adopt_node ($_);
7274        $node->append_child ($_);        $node->append_child ($_);
7275      }      }
7276      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
7277    
7278      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7279    
7280        delete $p->{parse_error}; # delete loop
7281    } else {    } else {
7282      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";
7283    }    }
# Line 4993  sub set_inner_html ($$$) { Line 7285  sub set_inner_html ($$$) {
7285    
7286  } # tree construction stage  } # tree construction stage
7287    
7288  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7289    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  
7290    
7291  1;  1;
7292  # $Date$  # $Date$

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.133

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24