/[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.7 by wakaba, Wed May 30 12:24:50 2007 UTC revision 1.134 by wakaba, Sat May 17 05:34:23 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    
 ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>  
299  my $c1_entity_char = {  my $c1_entity_char = {
300       128, 8364,    0x80 => 0x20AC,
301       129, 65533,    0x81 => 0xFFFD,
302       130, 8218,    0x82 => 0x201A,
303       131, 402,    0x83 => 0x0192,
304       132, 8222,    0x84 => 0x201E,
305       133, 8230,    0x85 => 0x2026,
306       134, 8224,    0x86 => 0x2020,
307       135, 8225,    0x87 => 0x2021,
308       136, 710,    0x88 => 0x02C6,
309       137, 8240,    0x89 => 0x2030,
310       138, 352,    0x8A => 0x0160,
311       139, 8249,    0x8B => 0x2039,
312       140, 338,    0x8C => 0x0152,
313       141, 65533,    0x8D => 0xFFFD,
314       142, 381,    0x8E => 0x017D,
315       143, 65533,    0x8F => 0xFFFD,
316       144, 65533,    0x90 => 0xFFFD,
317       145, 8216,    0x91 => 0x2018,
318       146, 8217,    0x92 => 0x2019,
319       147, 8220,    0x93 => 0x201C,
320       148, 8221,    0x94 => 0x201D,
321       149, 8226,    0x95 => 0x2022,
322       150, 8211,    0x96 => 0x2013,
323       151, 8212,    0x97 => 0x2014,
324       152, 732,    0x98 => 0x02DC,
325       153, 8482,    0x99 => 0x2122,
326       154, 353,    0x9A => 0x0161,
327       155, 8250,    0x9B => 0x203A,
328       156, 339,    0x9C => 0x0153,
329       157, 65533,    0x9D => 0xFFFD,
330       158, 382,    0x9E => 0x017E,
331       159, 376,    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,    my $onerror = $_[2] || sub {
341    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,      my (%opt) = @_;
342    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,      warn "Parse error ($opt{type})\n";
343    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    };
344    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    $self->{parse_error} = $onerror; # updated later by parse_char_string
345  };  
346  my $scoping_category = {    ## HTML5 encoding sniffing algorithm
347    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    require Message::Charset::Info;
348    table => 1, td => 1, th => 1,    my $charset;
349  };    my ($e, $e_status);
350  my $formatting_category = {  
351    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,    SNIFFING: {
352    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
353  };      ## Step 1
354  # $phrasing_category: all other elements      if (defined $charset_name) {
355          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
356    
357          ## ISSUE: Unsupported encoding is not ignored according to the spec.
358          ($e, $e_status) = $charset->get_perl_encoding
359              (allow_error_reporting => 1,
360               allow_fallback => 1);
361          if ($e) {
362            $self->{confident} = 1;
363            last SNIFFING;
364          }
365        }
366    
367        ## Step 2
368        # wait
369    
370        ## Step 3
371        my $head = substr ($$bytes_s, 0, 3);
372        if ($head =~ /^\xFE\xFF/) {
373          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
374          ($e, $e_status) = $charset->get_perl_encoding
375              (allow_error_reporting => 1,
376               allow_fallback => 1);
377          $self->{confident} = 1;
378          last SNIFFING;
379        } elsif ($head =~ /^\xFF\xFE/) {
380          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
381          ($e, $e_status) = $charset->get_perl_encoding
382              (allow_error_reporting => 1,
383               allow_fallback => 1);
384          $self->{confident} = 1;
385          last SNIFFING;
386        } elsif ($head eq "\xEF\xBB\xBF") {
387          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
388          ($e, $e_status) = $charset->get_perl_encoding
389              (allow_error_reporting => 1,
390               allow_fallback => 1);
391          $self->{confident} = 1;
392          last SNIFFING;
393        }
394    
395        ## Step 4
396        ## TODO: <meta charset>
397    
398        ## Step 5
399        ## TODO: from history
400    
401        ## Step 6
402        require Whatpm::Charset::UniversalCharDet;
403        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
404            (substr ($$bytes_s, 0, 1024));
405        if (defined $charset_name) {
406          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
407    
408          ## ISSUE: Unsupported encoding is not ignored according to the spec.
409          ($e, $e_status) = $charset->get_perl_encoding
410              (allow_error_reporting => 1,
411               allow_fallback => 1);
412          if ($e) {
413            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
414                            value => $charset_name,
415                            level => $self->{info_level},
416                            line => 1, column => 1);
417            $self->{confident} = 0;
418            last SNIFFING;
419          }
420        }
421    
422        ## Step 7: default
423        ## TODO: Make this configurable.
424        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
425            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
426            ## detectable in the step 6.
427        ($e, $e_status) = $charset->get_perl_encoding (allow_error_reporting => 1,
428                                                       allow_fallback => 1);
429        !!!parse-error (type => 'sniffing:default', ## TODO: type name
430                        value => 'windows-1252',
431                        level => $self->{info_level},
432                        line => 1, column => 1);
433        $self->{confident} = 0;
434      } # SNIFFING
435    
436      $self->{input_encoding} = $charset->get_iana_name;
437      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
438        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
439                        value => $e->name,
440                        level => $self->{unsupported_level},
441                        line => 1, column => 1);
442      } elsif (not ($e_status &
443                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
444        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
445                        value => $self->{input_encoding},
446                        level => $self->{unsupported_level},
447                        line => 1, column => 1);
448      }
449      $s = \ $e->decode ($$bytes_s);
450    
451      $self->{change_encoding} = sub {
452        my $self = shift;
453        $charset_name = shift;
454        my $token = shift;
455    
456        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
457        ($e, $e_status) = $charset->get_perl_encoding
458            (allow_error_reporting => 1, allow_fallback => 1);
459        
460        if ($e) { # if supported
461          ## "Change the encoding" algorithm:
462    
463          ## Step 1    
464          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
465            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
466            ($e, $e_status) = $charset->get_perl_encoding;
467          }
468          $charset_name = $charset->get_iana_name;
469          
470          ## Step 2
471          if (defined $self->{input_encoding} and
472              $self->{input_encoding} eq $charset_name) {
473            !!!parse-error (type => 'charset label:matching', ## TODO: type
474                            value => $charset_name,
475                            level => $self->{info_level});
476            $self->{confident} = 1;
477            return;
478          }
479    
480          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
481              ':'.$charset_name, level => 'w', token => $token);
482          
483          ## Step 3
484          # if (can) {
485            ## change the encoding on the fly.
486            #$self->{confident} = 1;
487            #return;
488          # }
489          
490          ## Step 4
491          throw Whatpm::HTML::RestartParser ();
492        }
493      }; # $self->{change_encoding}
494    
495      my @args = @_; shift @args; # $s
496      my $return;
497      try {
498        $return = $self->parse_char_string ($s, @args);  
499      } catch Whatpm::HTML::RestartParser with {
500        ## NOTE: Invoked after {change_encoding}.
501    
502        $self->{input_encoding} = $charset->get_iana_name;
503        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
504          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
505                          value => $e->name,
506                          level => $self->{unsupported_level},
507                          line => 1, column => 1);
508        } elsif (not ($e_status &
509                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
510          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
511                          value => $self->{input_encoding},
512                          level => $self->{unsupported_level},
513                          line => 1, column => 1);
514        }
515        $s = \ $e->decode ($$bytes_s);
516        $self->{confident} = 1;
517        $return = $self->parse_char_string ($s, @args);
518      };
519      return $return;
520    } # parse_byte_string
521    
522    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
523    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
524    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
525    ## because the core part of our HTML parser expects a string of character,
526    ## not a string of bytes or code units or anything which might contain a BOM.
527    ## Therefore, any parser interface that accepts a string of bytes,
528    ## such as |parse_byte_string| in this module, must ensure that it does
529    ## strip the BOM and never strip any ZWNBSP.
530    
531    *parse_char_string = \&parse_string;
532    
533  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
534    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
535    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
536    $self->{document} = $_[1];    $self->{document} = $_[1];
537      @{$self->{document}->child_nodes} = ();
538    
539    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
540    
541      $self->{confident} = 1 unless exists $self->{confident};
542      $self->{document}->input_encoding ($self->{input_encoding})
543          if defined $self->{input_encoding};
544    
545    my $i = 0;    my $i = 0;
546    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
547    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
548    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
549      my $self = shift;      my $self = shift;
550      $self->{next_input_character} = -1 and return if $i >= length $$s;  
551      $self->{next_input_character} = ord substr $$s, $i++, 1;      pop @{$self->{prev_char}};
552      $column++;      unshift @{$self->{prev_char}}, $self->{next_char};
553    
554        $self->{next_char} = -1 and return if $i >= length $$s;
555        $self->{next_char} = ord substr $$s, $i++, 1;
556    
557        ($self->{line_prev}, $self->{column_prev})
558            = ($self->{line}, $self->{column});
559        $self->{column}++;
560            
561      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
562        $line++;        !!!cp ('j1');
563        $column = 0;        $self->{line}++;
564      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
565        if ($i >= length $$s) {      } elsif ($self->{next_char} == 0x000D) { # CR
566          #        !!!cp ('j2');
567        } else {        $i++ if substr ($$s, $i, 1) eq "\x0A";
568          my $next_char = ord substr $$s, $i++, 1;        $self->{next_char} = 0x000A; # LF # MUST
569          if ($next_char == 0x000A) { # LF        $self->{line}++;
570            #        $self->{column} = 0;
571          } else {      } elsif ($self->{next_char} > 0x10FFFF) {
572            push @{$self->{char}}, $next_char;        !!!cp ('j3');
573          }        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
574        }      } elsif ($self->{next_char} == 0x0000) { # NULL
575        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j4');
576        $line++;        !!!parse-error (type => 'NULL');
577        $column = 0;        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
578      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} <= 0x0008 or
579        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST               (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
580      } elsif ($self->{next_input_character} == 0x0000) { # NULL               (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
581        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST               (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
582                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
583                 {
584                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
585                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
586                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
587                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
588                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
589                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
590                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
591                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
592                  0x10FFFE => 1, 0x10FFFF => 1,
593                 }->{$self->{next_char}}) {
594          !!!cp ('j5');
595          !!!parse-error (type => 'control char', level => $self->{must_level});
596    ## TODO: error type documentation
597      }      }
598    };    };
599      $self->{prev_char} = [-1, -1, -1];
600      $self->{next_char} = -1;
601    
602    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
603      my (%opt) = @_;      my (%opt) = @_;
604      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
605        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
606        warn "Parse error ($opt{type}) at line $line column $column\n";
607    };    };
608    $self->{parse_error} = sub {    $self->{parse_error} = sub {
609      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
610    };    };
611    
612    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 391  sub parse_string ($$$;$) { Line 614  sub parse_string ($$$;$) {
614    $self->_construct_tree;    $self->_construct_tree;
615    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
616    
617      delete $self->{parse_error}; # remove loop
618    
619    return $self->{document};    return $self->{document};
620  } # parse_string  } # parse_string
621    
622  sub new ($) {  sub new ($) {
623    my $class = shift;    my $class = shift;
624    my $self = bless {}, $class;    my $self = bless {
625    $self->{set_next_input_character} = sub {      must_level => 'm',
626      $self->{next_input_character} = -1;      should_level => 's',
627        good_level => 'w',
628        warn_level => 'w',
629        info_level => 'i',
630        unsupported_level => 'u',
631      }, $class;
632      $self->{set_next_char} = sub {
633        $self->{next_char} = -1;
634    };    };
635    $self->{parse_error} = sub {    $self->{parse_error} = sub {
636      #      #
637    };    };
638      $self->{change_encoding} = sub {
639        # if ($_[0] is a supported encoding) {
640        #   run "change the encoding" algorithm;
641        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
642        # }
643      };
644      $self->{application_cache_selection} = sub {
645        #
646      };
647    return $self;    return $self;
648  } # new  } # new
649    
650    sub CM_ENTITY () { 0b001 } # & markup in data
651    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
652    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
653    
654    sub PLAINTEXT_CONTENT_MODEL () { 0 }
655    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
656    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
657    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
658    
659    sub DATA_STATE () { 0 }
660    sub ENTITY_DATA_STATE () { 1 }
661    sub TAG_OPEN_STATE () { 2 }
662    sub CLOSE_TAG_OPEN_STATE () { 3 }
663    sub TAG_NAME_STATE () { 4 }
664    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
665    sub ATTRIBUTE_NAME_STATE () { 6 }
666    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
667    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
668    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
669    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
670    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
671    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
672    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
673    sub COMMENT_START_STATE () { 14 }
674    sub COMMENT_START_DASH_STATE () { 15 }
675    sub COMMENT_STATE () { 16 }
676    sub COMMENT_END_STATE () { 17 }
677    sub COMMENT_END_DASH_STATE () { 18 }
678    sub BOGUS_COMMENT_STATE () { 19 }
679    sub DOCTYPE_STATE () { 20 }
680    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
681    sub DOCTYPE_NAME_STATE () { 22 }
682    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
683    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
684    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
685    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
686    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
687    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
688    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
689    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
690    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
691    sub BOGUS_DOCTYPE_STATE () { 32 }
692    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
693    sub SELF_CLOSING_START_TAG_STATE () { 34 }
694    sub CDATA_BLOCK_STATE () { 35 }
695    
696    sub DOCTYPE_TOKEN () { 1 }
697    sub COMMENT_TOKEN () { 2 }
698    sub START_TAG_TOKEN () { 3 }
699    sub END_TAG_TOKEN () { 4 }
700    sub END_OF_FILE_TOKEN () { 5 }
701    sub CHARACTER_TOKEN () { 6 }
702    
703    sub AFTER_HTML_IMS () { 0b100 }
704    sub HEAD_IMS ()       { 0b1000 }
705    sub BODY_IMS ()       { 0b10000 }
706    sub BODY_TABLE_IMS () { 0b100000 }
707    sub TABLE_IMS ()      { 0b1000000 }
708    sub ROW_IMS ()        { 0b10000000 }
709    sub BODY_AFTER_IMS () { 0b100000000 }
710    sub FRAME_IMS ()      { 0b1000000000 }
711    sub SELECT_IMS ()     { 0b10000000000 }
712    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
713        ## NOTE: "in foreign content" insertion mode is special; it is combined
714        ## with the secondary insertion mode.  In this parser, they are stored
715        ## together in the bit-or'ed form.
716    
717    ## NOTE: "initial" and "before html" insertion modes have no constants.
718    
719    ## NOTE: "after after body" insertion mode.
720    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
721    
722    ## NOTE: "after after frameset" insertion mode.
723    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
724    
725    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
726    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
727    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
728    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
729    sub IN_BODY_IM () { BODY_IMS }
730    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
731    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
732    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
733    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
734    sub IN_TABLE_IM () { TABLE_IMS }
735    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
736    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
737    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
738    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
739    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
740    sub IN_COLUMN_GROUP_IM () { 0b10 }
741    
742  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
743    
744  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
745    my $self = shift;    my $self = shift;
746    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
747    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
748    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
749    undef $self->{current_attribute};    undef $self->{current_attribute};
750    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
751    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
752      delete $self->{self_closing};
753    $self->{char} = [];    $self->{char} = [];
754    # $self->{next_input_character}    # $self->{next_char}
755    !!!next-input-character;    !!!next-input-character;
756    $self->{token} = [];    $self->{token} = [];
757      # $self->{escape}
758  } # _initialize_tokenizer  } # _initialize_tokenizer
759    
760  ## A token has:  ## A token has:
761  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
762  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
763  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
764      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
765  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
766  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
767  ##   ->{data} (comment, character)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
768    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
769  ## Macros  ##        ->{name}
770  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
771  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
772  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
773    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
774    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
775    ##     while the token is pushed back to the stack.
776    
777    ## ISSUE: "When a DOCTYPE token is created, its
778    ## <i>self-closing flag</i> must be unset (its other state is that it
779    ## be set), and its attributes list must be empty.": Wrong subject?
780    
781  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
782    
# Line 444  sub _initialize_tokenizer ($) { Line 786  sub _initialize_tokenizer ($) {
786  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
787  ## and removed from the list.  ## and removed from the list.
788    
789    ## NOTE: HTML5 "Writing HTML documents" section, applied to
790    ## documents and not to user agents and conformance checkers,
791    ## contains some requirements that are not detected by the
792    ## parsing algorithm:
793    ## - Some requirements on character encoding declarations. ## TODO
794    ## - "Elements MUST NOT contain content that their content model disallows."
795    ##   ... Some are parse error, some are not (will be reported by c.c.).
796    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
797    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
798    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
799    
800    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
801    ## be detected by the HTML5 parsing algorithm:
802    ## - Text,
803    
804  sub _get_next_token ($) {  sub _get_next_token ($) {
805    my $self = shift;    my $self = shift;
806    
807      if ($self->{self_closing}) {
808        !!!parse-error (type => 'nestc', token => $self->{current_token});
809        ## NOTE: The |self_closing| flag is only set by start tag token.
810        ## In addition, when a start tag token is emitted, it is always set to
811        ## |current_token|.
812        delete $self->{self_closing};
813      }
814    
815    if (@{$self->{token}}) {    if (@{$self->{token}}) {
816        $self->{self_closing} = $self->{token}->[0]->{self_closing};
817      return shift @{$self->{token}};      return shift @{$self->{token}};
818    }    }
819    
820    A: {    A: {
821      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
822        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
823          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
824              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
825            $self->{state} = 'entity data';            !!!cp (1);
826              $self->{state} = ENTITY_DATA_STATE;
827            !!!next-input-character;            !!!next-input-character;
828            redo A;            redo A;
829          } else {          } else {
830              !!!cp (2);
831            #            #
832          }          }
833        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x002D) { # -
834          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
835            $self->{state} = 'tag open';            unless ($self->{escape}) {
836                if ($self->{prev_char}->[0] == 0x002D and # -
837                    $self->{prev_char}->[1] == 0x0021 and # !
838                    $self->{prev_char}->[2] == 0x003C) { # <
839                  !!!cp (3);
840                  $self->{escape} = 1;
841                } else {
842                  !!!cp (4);
843                }
844              } else {
845                !!!cp (5);
846              }
847            }
848            
849            #
850          } elsif ($self->{next_char} == 0x003C) { # <
851            if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
852                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
853                 not $self->{escape})) {
854              !!!cp (6);
855              $self->{state} = TAG_OPEN_STATE;
856            !!!next-input-character;            !!!next-input-character;
857            redo A;            redo A;
858          } else {          } else {
859              !!!cp (7);
860            #            #
861          }          }
862        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
863          !!!emit ({type => 'end-of-file'});          if ($self->{escape} and
864                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
865              if ($self->{prev_char}->[0] == 0x002D and # -
866                  $self->{prev_char}->[1] == 0x002D) { # -
867                !!!cp (8);
868                delete $self->{escape};
869              } else {
870                !!!cp (9);
871              }
872            } else {
873              !!!cp (10);
874            }
875            
876            #
877          } elsif ($self->{next_char} == -1) {
878            !!!cp (11);
879            !!!emit ({type => END_OF_FILE_TOKEN,
880                      line => $self->{line}, column => $self->{column}});
881          last A; ## TODO: ok?          last A; ## TODO: ok?
882          } else {
883            !!!cp (12);
884        }        }
885        # Anything else        # Anything else
886        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
887                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
888                       line => $self->{line}, column => $self->{column},
889                      };
890        ## Stay in the data state        ## Stay in the data state
891        !!!next-input-character;        !!!next-input-character;
892    
893        !!!emit ($token);        !!!emit ($token);
894    
895        redo A;        redo A;
896      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
897        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
898    
899          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
900                
901        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
902    
903        $self->{state} = 'data';        $self->{state} = DATA_STATE;
904        # next-input-character is already done        # next-input-character is already done
905    
906        unless (defined $token) {        unless (defined $token) {
907          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
908            !!!emit ({type => CHARACTER_TOKEN, data => '&',
909                      line => $l, column => $c,
910                     });
911        } else {        } else {
912            !!!cp (14);
913          !!!emit ($token);          !!!emit ($token);
914        }        }
915    
916        redo A;        redo A;
917      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
918        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
919            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
920          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
921            !!!next-input-character;            !!!next-input-character;
922            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
923            redo A;            redo A;
924          } else {          } else {
925              !!!cp (16);
926            ## reconsume            ## reconsume
927            $self->{state} = 'data';            $self->{state} = DATA_STATE;
928    
929            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
930                        line => $self->{line_prev},
931                        column => $self->{column_prev},
932                       });
933    
934            redo A;            redo A;
935          }          }
936        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
937          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
938            $self->{state} = 'markup declaration open';            !!!cp (17);
939              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
940            !!!next-input-character;            !!!next-input-character;
941            redo A;            redo A;
942          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
943            $self->{state} = 'close tag open';            !!!cp (18);
944              $self->{state} = CLOSE_TAG_OPEN_STATE;
945            !!!next-input-character;            !!!next-input-character;
946            redo A;            redo A;
947          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
948                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
949              !!!cp (19);
950            $self->{current_token}            $self->{current_token}
951              = {type => 'start tag',              = {type => START_TAG_TOKEN,
952                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
953            $self->{state} = 'tag name';                 line => $self->{line_prev},
954                   column => $self->{column_prev}};
955              $self->{state} = TAG_NAME_STATE;
956            !!!next-input-character;            !!!next-input-character;
957            redo A;            redo A;
958          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
959                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
960            $self->{current_token} = {type => 'start tag',            !!!cp (20);
961                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
962            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
963                                        line => $self->{line_prev},
964                                        column => $self->{column_prev}};
965              $self->{state} = TAG_NAME_STATE;
966            !!!next-input-character;            !!!next-input-character;
967            redo A;            redo A;
968          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
969            !!!parse-error (type => 'empty start tag');            !!!cp (21);
970            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
971                              line => $self->{line_prev},
972                              column => $self->{column_prev});
973              $self->{state} = DATA_STATE;
974            !!!next-input-character;            !!!next-input-character;
975    
976            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
977                        line => $self->{line_prev},
978                        column => $self->{column_prev},
979                       });
980    
981            redo A;            redo A;
982          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
983            !!!parse-error (type => 'pio');            !!!cp (22);
984            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
985            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
986                              column => $self->{column_prev});
987              $self->{state} = BOGUS_COMMENT_STATE;
988              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
989                                        line => $self->{line_prev},
990                                        column => $self->{column_prev},
991                                       };
992              ## $self->{next_char} is intentionally left as is
993            redo A;            redo A;
994          } else {          } else {
995              !!!cp (23);
996            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
997            $self->{state} = 'data';            $self->{state} = DATA_STATE;
998            ## reconsume            ## reconsume
999    
1000            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1001                        line => $self->{line_prev},
1002                        column => $self->{column_prev},
1003                       });
1004    
1005            redo A;            redo A;
1006          }          }
1007        } else {        } else {
1008          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1009        }        }
1010      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1011        if ($self->{content_model_flag} eq 'RCDATA' or        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1012            $self->{content_model_flag} eq 'CDATA') {        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1013          my @next_char;          if (defined $self->{last_emitted_start_tag_name}) {
1014          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
1015            push @next_char, $self->{next_input_character};            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1016            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);            my @next_char;
1017            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
1018            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              push @next_char, $self->{next_char};
1019              !!!next-input-character;              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1020              next TAGNAME;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1021            } else {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1022              !!!parse-error (type => 'unmatched end tag');                !!!cp (24);
1023              $self->{next_input_character} = shift @next_char; # reconsume                !!!next-input-character;
1024                  next TAGNAME;
1025                } else {
1026                  !!!cp (25);
1027                  $self->{next_char} = shift @next_char; # reconsume
1028                  !!!back-next-input-character (@next_char);
1029                  $self->{state} = DATA_STATE;
1030    
1031                  !!!emit ({type => CHARACTER_TOKEN, data => '</',
1032                            line => $l, column => $c,
1033                           });
1034      
1035                  redo A;
1036                }
1037              }
1038              push @next_char, $self->{next_char};
1039          
1040              unless ($self->{next_char} == 0x0009 or # HT
1041                      $self->{next_char} == 0x000A or # LF
1042                      $self->{next_char} == 0x000B or # VT
1043                      $self->{next_char} == 0x000C or # FF
1044                      $self->{next_char} == 0x0020 or # SP
1045                      $self->{next_char} == 0x003E or # >
1046                      $self->{next_char} == 0x002F or # /
1047                      $self->{next_char} == -1) {
1048                !!!cp (26);
1049                $self->{next_char} = shift @next_char; # reconsume
1050              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1051              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1052                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1053              !!!emit ({type => 'character', data => '</'});                        line => $l, column => $c,
1054                         });
1055              redo A;              redo A;
1056              } else {
1057                !!!cp (27);
1058                $self->{next_char} = shift @next_char;
1059                !!!back-next-input-character (@next_char);
1060                # and consume...
1061            }            }
         }  
         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;  
1062          } else {          } else {
1063            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
1064            !!!back-next-input-character (@next_char);            !!!cp (28);
1065            # and consume...            # next-input-character is already done
1066              $self->{state} = DATA_STATE;
1067              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1068                        line => $l, column => $c,
1069                       });
1070              redo A;
1071          }          }
1072        }        }
1073                
1074        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1075            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1076          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1077                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1078          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1079          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1080          redo A;                 line => $l, column => $c};
1081        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1082                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1083          $self->{current_token} = {type => 'end tag',          redo A;
1084                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1085          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1086          !!!next-input-character;          !!!cp (30);
1087          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1088        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1089          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1090          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1091            !!!next-input-character;
1092            redo A;
1093          } elsif ($self->{next_char} == 0x003E) { # >
1094            !!!cp (31);
1095            !!!parse-error (type => 'empty end tag',
1096                            line => $self->{line_prev}, ## "<" in "</>"
1097                            column => $self->{column_prev} - 1);
1098            $self->{state} = DATA_STATE;
1099          !!!next-input-character;          !!!next-input-character;
1100          redo A;          redo A;
1101        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1102            !!!cp (32);
1103          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1104          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1105          # reconsume          # reconsume
1106    
1107          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1108                      line => $l, column => $c,
1109                     });
1110    
1111          redo A;          redo A;
1112        } else {        } else {
1113            !!!cp (33);
1114          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1115          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1116          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1117          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1118        }                                    column => $self->{column_prev} - 1,
1119      } elsif ($self->{state} eq 'tag name') {                                   };
1120        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1121            $self->{next_input_character} == 0x000A or # LF          redo A;
1122            $self->{next_input_character} == 0x000B or # VT        }
1123            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1124            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1125          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1126          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1127          redo A;            $self->{next_char} == 0x000C or # FF
1128        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1129          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1130            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1131            !!!next-input-character;
1132            redo A;
1133          } elsif ($self->{next_char} == 0x003E) { # >
1134            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1135              !!!cp (35);
1136            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1137          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1138            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1139            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1140              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1141            }            #  !!! cp (36);
1142              #  !!! parse-error (type => 'end tag attribute');
1143              #} else {
1144                !!!cp (37);
1145              #}
1146          } else {          } else {
1147            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1148          }          }
1149          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1150          !!!next-input-character;          !!!next-input-character;
1151    
1152          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1153    
1154          redo A;          redo A;
1155        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1156                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1157          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1158            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1159            # start tag or end tag            # start tag or end tag
1160          ## Stay in this state          ## Stay in this state
1161          !!!next-input-character;          !!!next-input-character;
1162          redo A;          redo A;
1163        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1164          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1165          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1166              !!!cp (39);
1167            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1168          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1169            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1170            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1171              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1172            }            #  !!! cp (40);
1173              #  !!! parse-error (type => 'end tag attribute');
1174              #} else {
1175                !!!cp (41);
1176              #}
1177          } else {          } else {
1178            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1179          }          }
1180          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1181          # reconsume          # reconsume
1182    
1183          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1184    
1185          redo A;          redo A;
1186        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1187            !!!cp (42);
1188            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1189          !!!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  
1190          redo A;          redo A;
1191        } else {        } else {
1192          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1193            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1194            # start tag or end tag            # start tag or end tag
1195          ## Stay in the state          ## Stay in the state
1196          !!!next-input-character;          !!!next-input-character;
1197          redo A;          redo A;
1198        }        }
1199      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1200        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1201            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1202            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1203            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1204            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1205            !!!cp (45);
1206          ## Stay in the state          ## Stay in the state
1207          !!!next-input-character;          !!!next-input-character;
1208          redo A;          redo A;
1209        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1210          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1211              !!!cp (46);
1212            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1213          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1214            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1215            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1216                !!!cp (47);
1217              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1218              } else {
1219                !!!cp (48);
1220            }            }
1221          } else {          } else {
1222            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1223          }          }
1224          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1225          !!!next-input-character;          !!!next-input-character;
1226    
1227          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1228    
1229          redo A;          redo A;
1230        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1231                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1232          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1233                                value => ''};          $self->{current_attribute}
1234          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1235          !!!next-input-character;                 value => '',
1236          redo A;                 line => $self->{line}, column => $self->{column}};
1237        } elsif ($self->{next_input_character} == 0x002F) { # /          $self->{state} = ATTRIBUTE_NAME_STATE;
1238            !!!next-input-character;
1239            redo A;
1240          } elsif ($self->{next_char} == 0x002F) { # /
1241            !!!cp (50);
1242            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1243          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
1244          redo A;          redo A;
1245        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1246          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1247          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1248              !!!cp (52);
1249            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1250          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1251            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1252            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1253                !!!cp (53);
1254              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1255              } else {
1256                !!!cp (54);
1257            }            }
1258          } else {          } else {
1259            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1260          }          }
1261          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1262          # reconsume          # reconsume
1263    
1264          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1265    
1266          redo A;          redo A;
1267        } else {        } else {
1268          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1269                                value => ''};               0x0022 => 1, # "
1270          $self->{state} = 'attribute name';               0x0027 => 1, # '
1271                 0x003D => 1, # =
1272                }->{$self->{next_char}}) {
1273              !!!cp (55);
1274              !!!parse-error (type => 'bad attribute name');
1275            } else {
1276              !!!cp (56);
1277            }
1278            $self->{current_attribute}
1279                = {name => chr ($self->{next_char}),
1280                   value => '',
1281                   line => $self->{line}, column => $self->{column}};
1282            $self->{state} = ATTRIBUTE_NAME_STATE;
1283          !!!next-input-character;          !!!next-input-character;
1284          redo A;          redo A;
1285        }        }
1286      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1287        my $before_leave = sub {        my $before_leave = sub {
1288          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1289              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1290            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1291              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1292            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1293          } else {          } else {
1294              !!!cp (58);
1295            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1296              = $self->{current_attribute};              = $self->{current_attribute};
1297          }          }
1298        }; # $before_leave        }; # $before_leave
1299    
1300        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1301            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1302            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1303            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1304            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1305            !!!cp (59);
1306          $before_leave->();          $before_leave->();
1307          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1308          !!!next-input-character;          !!!next-input-character;
1309          redo A;          redo A;
1310        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1311            !!!cp (60);
1312          $before_leave->();          $before_leave->();
1313          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1314          !!!next-input-character;          !!!next-input-character;
1315          redo A;          redo A;
1316        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1317          $before_leave->();          $before_leave->();
1318          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1319              !!!cp (61);
1320            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1321          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1322            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
1323              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1324            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1325              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1326            }            }
1327          } else {          } else {
1328            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1329          }          }
1330          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1331          !!!next-input-character;          !!!next-input-character;
1332    
1333          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1334    
1335          redo A;          redo A;
1336        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1337                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1338          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1339            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1340          ## Stay in the state          ## Stay in the state
1341          !!!next-input-character;          !!!next-input-character;
1342          redo A;          redo A;
1343        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1344            !!!cp (64);
1345          $before_leave->();          $before_leave->();
1346            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1347          !!!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  
1348          redo A;          redo A;
1349        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1350          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1351          $before_leave->();          $before_leave->();
1352          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1353              !!!cp (66);
1354            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1355          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1356            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1357            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1358                !!!cp (67);
1359              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1360              } else {
1361                ## NOTE: This state should never be reached.
1362                !!!cp (68);
1363            }            }
1364          } else {          } else {
1365            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1366          }          }
1367          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1368          # reconsume          # reconsume
1369    
1370          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1371    
1372          redo A;          redo A;
1373        } else {        } else {
1374          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1375                $self->{next_char} == 0x0027) { # '
1376              !!!cp (69);
1377              !!!parse-error (type => 'bad attribute name');
1378            } else {
1379              !!!cp (70);
1380            }
1381            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1382          ## Stay in the state          ## Stay in the state
1383          !!!next-input-character;          !!!next-input-character;
1384          redo A;          redo A;
1385        }        }
1386      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1387        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1388            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1389            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1390            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1391            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1392            !!!cp (71);
1393          ## Stay in the state          ## Stay in the state
1394          !!!next-input-character;          !!!next-input-character;
1395          redo A;          redo A;
1396        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1397          $self->{state} = 'before attribute value';          !!!cp (72);
1398            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1399          !!!next-input-character;          !!!next-input-character;
1400          redo A;          redo A;
1401        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1402          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1403              !!!cp (73);
1404            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1405          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1406            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1407            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1408                !!!cp (74);
1409              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1410              } else {
1411                ## NOTE: This state should never be reached.
1412                !!!cp (75);
1413            }            }
1414          } else {          } else {
1415            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1416          }          }
1417          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1418          !!!next-input-character;          !!!next-input-character;
1419    
1420          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1421    
1422          redo A;          redo A;
1423        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1424                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1425          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1426                                value => ''};          $self->{current_attribute}
1427          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1428                   value => '',
1429                   line => $self->{line}, column => $self->{column}};
1430            $self->{state} = ATTRIBUTE_NAME_STATE;
1431            !!!next-input-character;
1432            redo A;
1433          } elsif ($self->{next_char} == 0x002F) { # /
1434            !!!cp (77);
1435            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1436          !!!next-input-character;          !!!next-input-character;
1437          redo A;          redo A;
1438        } 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) {  
1439          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1440          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1441              !!!cp (79);
1442            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1443          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1444            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1445            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1446                !!!cp (80);
1447              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1448              } else {
1449                ## NOTE: This state should never be reached.
1450                !!!cp (81);
1451            }            }
1452          } else {          } else {
1453            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1454          }          }
1455          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1456          # reconsume          # reconsume
1457    
1458          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1459    
1460          redo A;          redo A;
1461        } else {        } else {
1462          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1463                                value => ''};          $self->{current_attribute}
1464          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char}),
1465                   value => '',
1466                   line => $self->{line}, column => $self->{column}};
1467            $self->{state} = ATTRIBUTE_NAME_STATE;
1468          !!!next-input-character;          !!!next-input-character;
1469          redo A;                  redo A;        
1470        }        }
1471      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1472        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1473            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1474            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1475            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1476            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1477            !!!cp (83);
1478          ## Stay in the state          ## Stay in the state
1479          !!!next-input-character;          !!!next-input-character;
1480          redo A;          redo A;
1481        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1482          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1483            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1484          !!!next-input-character;          !!!next-input-character;
1485          redo A;          redo A;
1486        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1487          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1488            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1489          ## reconsume          ## reconsume
1490          redo A;          redo A;
1491        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1492          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1493            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1494          !!!next-input-character;          !!!next-input-character;
1495          redo A;          redo A;
1496        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1497          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1498              !!!cp (87);
1499            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1500          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1501            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1502            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1503                !!!cp (88);
1504              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1505              } else {
1506                ## NOTE: This state should never be reached.
1507                !!!cp (89);
1508            }            }
1509          } else {          } else {
1510            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1511          }          }
1512          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1513          !!!next-input-character;          !!!next-input-character;
1514    
1515          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1516    
1517          redo A;          redo A;
1518        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1519          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1520          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1521              !!!cp (90);
1522            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1523          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1524            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1525            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1526                !!!cp (91);
1527              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1528              } else {
1529                ## NOTE: This state should never be reached.
1530                !!!cp (92);
1531            }            }
1532          } else {          } else {
1533            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1534          }          }
1535          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1536          ## reconsume          ## reconsume
1537    
1538          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1539    
1540          redo A;          redo A;
1541        } else {        } else {
1542          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1543          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1544              !!!parse-error (type => 'bad attribute value');
1545            } else {
1546              !!!cp (94);
1547            }
1548            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1549            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1550          !!!next-input-character;          !!!next-input-character;
1551          redo A;          redo A;
1552        }        }
1553      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1554        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1555          $self->{state} = 'before attribute name';          !!!cp (95);
1556            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1557          !!!next-input-character;          !!!next-input-character;
1558          redo A;          redo A;
1559        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1560          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1561          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1562            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1563          !!!next-input-character;          !!!next-input-character;
1564          redo A;          redo A;
1565        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1566          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1567          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1568              !!!cp (97);
1569            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1570          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1571            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1572            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1573                !!!cp (98);
1574              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1575              } else {
1576                ## NOTE: This state should never be reached.
1577                !!!cp (99);
1578            }            }
1579          } else {          } else {
1580            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1581          }          }
1582          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1583          ## reconsume          ## reconsume
1584    
1585          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1586    
1587          redo A;          redo A;
1588        } else {        } else {
1589          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1590            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1591          ## Stay in the state          ## Stay in the state
1592          !!!next-input-character;          !!!next-input-character;
1593          redo A;          redo A;
1594        }        }
1595      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1596        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1597          $self->{state} = 'before attribute name';          !!!cp (101);
1598            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1599          !!!next-input-character;          !!!next-input-character;
1600          redo A;          redo A;
1601        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1602          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1603          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1604            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1605          !!!next-input-character;          !!!next-input-character;
1606          redo A;          redo A;
1607        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1608          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1609          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1610              !!!cp (103);
1611            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1612          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1613            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1614            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1615                !!!cp (104);
1616              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1617              } else {
1618                ## NOTE: This state should never be reached.
1619                !!!cp (105);
1620            }            }
1621          } else {          } else {
1622            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1623          }          }
1624          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1625          ## reconsume          ## reconsume
1626    
1627          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1628    
1629          redo A;          redo A;
1630        } else {        } else {
1631          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1632            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1633          ## Stay in the state          ## Stay in the state
1634          !!!next-input-character;          !!!next-input-character;
1635          redo A;          redo A;
1636        }        }
1637      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1638        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1639            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1640            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1641            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1642            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1643          $self->{state} = 'before attribute name';          !!!cp (107);
1644          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1645          redo A;          !!!next-input-character;
1646        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1647          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1648          $self->{state} = 'entity in attribute value';          !!!cp (108);
1649          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1650          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1651        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1652          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1653          } elsif ($self->{next_char} == 0x003E) { # >
1654            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1655              !!!cp (109);
1656            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1657          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1658            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1659            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1660                !!!cp (110);
1661              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1662              } else {
1663                ## NOTE: This state should never be reached.
1664                !!!cp (111);
1665            }            }
1666          } else {          } else {
1667            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1668          }          }
1669          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1670          !!!next-input-character;          !!!next-input-character;
1671    
1672          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1673    
1674          redo A;          redo A;
1675        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1676          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1677          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1678              !!!cp (112);
1679            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1680          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1681            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1682            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1683                !!!cp (113);
1684              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1685              } else {
1686                ## NOTE: This state should never be reached.
1687                !!!cp (114);
1688            }            }
1689          } else {          } else {
1690            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1691          }          }
1692          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1693          ## reconsume          ## reconsume
1694    
1695          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1696    
1697          redo A;          redo A;
1698        } else {        } else {
1699          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1700                 0x0022 => 1, # "
1701                 0x0027 => 1, # '
1702                 0x003D => 1, # =
1703                }->{$self->{next_char}}) {
1704              !!!cp (115);
1705              !!!parse-error (type => 'bad attribute value');
1706            } else {
1707              !!!cp (116);
1708            }
1709            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1710          ## Stay in the state          ## Stay in the state
1711          !!!next-input-character;          !!!next-input-character;
1712          redo A;          redo A;
1713        }        }
1714      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1715        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity
1716              (1,
1717               $self->{last_attribute_value_state}
1718                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1719               $self->{last_attribute_value_state}
1720                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1721               -1);
1722    
1723        unless (defined $token) {        unless (defined $token) {
1724            !!!cp (117);
1725          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1726        } else {        } else {
1727            !!!cp (118);
1728          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1729            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1730          ## 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"
1731        }        }
1732    
1733        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1734        # next-input-character is already done        # next-input-character is already done
1735        redo A;        redo A;
1736      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1737          if ($self->{next_char} == 0x0009 or # HT
1738              $self->{next_char} == 0x000A or # LF
1739              $self->{next_char} == 0x000B or # VT
1740              $self->{next_char} == 0x000C or # FF
1741              $self->{next_char} == 0x0020) { # SP
1742            !!!cp (118);
1743            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1744            !!!next-input-character;
1745            redo A;
1746          } elsif ($self->{next_char} == 0x003E) { # >
1747            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1748              !!!cp (119);
1749              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1750            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1751              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1752              if ($self->{current_token}->{attributes}) {
1753                !!!cp (120);
1754                !!!parse-error (type => 'end tag attribute');
1755              } else {
1756                ## NOTE: This state should never be reached.
1757                !!!cp (121);
1758              }
1759            } else {
1760              die "$0: $self->{current_token}->{type}: Unknown token type";
1761            }
1762            $self->{state} = DATA_STATE;
1763            !!!next-input-character;
1764    
1765            !!!emit ($self->{current_token}); # start tag or end tag
1766    
1767            redo A;
1768          } elsif ($self->{next_char} == 0x002F) { # /
1769            !!!cp (122);
1770            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1771            !!!next-input-character;
1772            redo A;
1773          } else {
1774            !!!cp ('124.1');
1775            !!!parse-error (type => 'no space between attributes');
1776            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1777            ## reconsume
1778            redo A;
1779          }
1780        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1781          if ($self->{next_char} == 0x003E) { # >
1782            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1783              !!!cp ('124.2');
1784              !!!parse-error (type => 'nestc', token => $self->{current_token});
1785              ## TODO: Different type than slash in start tag
1786              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1787              if ($self->{current_token}->{attributes}) {
1788                !!!cp ('124.4');
1789                !!!parse-error (type => 'end tag attribute');
1790              } else {
1791                !!!cp ('124.5');
1792              }
1793              ## TODO: Test |<title></title/>|
1794            } else {
1795              !!!cp ('124.3');
1796              $self->{self_closing} = 1;
1797            }
1798    
1799            $self->{state} = DATA_STATE;
1800            !!!next-input-character;
1801    
1802            !!!emit ($self->{current_token}); # start tag or end tag
1803    
1804            redo A;
1805          } else {
1806            !!!cp ('124.4');
1807            !!!parse-error (type => 'nestc');
1808            ## TODO: This error type is wrong.
1809            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1810            ## Reconsume.
1811            redo A;
1812          }
1813        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1814        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1815                
1816        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1817          #my $token = {type => COMMENT_TOKEN, data => ''};
1818    
1819        BC: {        BC: {
1820          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1821            $self->{state} = 'data';            !!!cp (124);
1822              $self->{state} = DATA_STATE;
1823            !!!next-input-character;            !!!next-input-character;
1824    
1825            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1826    
1827            redo A;            redo A;
1828          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1829            $self->{state} = 'data';            !!!cp (125);
1830              $self->{state} = DATA_STATE;
1831            ## reconsume            ## reconsume
1832    
1833            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1834    
1835            redo A;            redo A;
1836          } else {          } else {
1837            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1838              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1839            !!!next-input-character;            !!!next-input-character;
1840            redo BC;            redo BC;
1841          }          }
1842        } # BC        } # BC
1843      } elsif ($self->{state} eq 'markup declaration open') {  
1844          die "$0: _get_next_token: unexpected case [BC]";
1845        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1846        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1847    
1848          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1849    
1850        my @next_char;        my @next_char;
1851        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1852                
1853        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1854          !!!next-input-character;          !!!next-input-character;
1855          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1856          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1857            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1858            $self->{state} = 'comment';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1859                                        line => $l, column => $c,
1860                                       };
1861              $self->{state} = COMMENT_START_STATE;
1862            !!!next-input-character;            !!!next-input-character;
1863            redo A;            redo A;
1864            } else {
1865              !!!cp (128);
1866          }          }
1867        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1868                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1869          !!!next-input-character;          !!!next-input-character;
1870          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1871          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1872              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1873            !!!next-input-character;            !!!next-input-character;
1874            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1875            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1876                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1877              !!!next-input-character;              !!!next-input-character;
1878              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1879              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1880                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1881                !!!next-input-character;                !!!next-input-character;
1882                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1883                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1884                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1885                  !!!next-input-character;                  !!!next-input-character;
1886                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1887                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1888                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1889                    !!!next-input-character;                    !!!next-input-character;
1890                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1891                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1892                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1893                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1894                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1895                        $self->{state} = DOCTYPE_STATE;
1896                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1897                                                  quirks => 1,
1898                                                  line => $l, column => $c,
1899                                                 };
1900                      !!!next-input-character;                      !!!next-input-character;
1901                      redo A;                      redo A;
1902                      } else {
1903                        !!!cp (130);
1904                    }                    }
1905                    } else {
1906                      !!!cp (131);
1907                  }                  }
1908                  } else {
1909                    !!!cp (132);
1910                }                }
1911                } else {
1912                  !!!cp (133);
1913              }              }
1914              } else {
1915                !!!cp (134);
1916            }            }
1917            } else {
1918              !!!cp (135);
1919          }          }
1920          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1921                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1922                   $self->{next_char} == 0x005B) { # [
1923            !!!next-input-character;
1924            push @next_char, $self->{next_char};
1925            if ($self->{next_char} == 0x0043) { # C
1926              !!!next-input-character;
1927              push @next_char, $self->{next_char};
1928              if ($self->{next_char} == 0x0044) { # D
1929                !!!next-input-character;
1930                push @next_char, $self->{next_char};
1931                if ($self->{next_char} == 0x0041) { # A
1932                  !!!next-input-character;
1933                  push @next_char, $self->{next_char};
1934                  if ($self->{next_char} == 0x0054) { # T
1935                    !!!next-input-character;
1936                    push @next_char, $self->{next_char};
1937                    if ($self->{next_char} == 0x0041) { # A
1938                      !!!next-input-character;
1939                      push @next_char, $self->{next_char};
1940                      if ($self->{next_char} == 0x005B) { # [
1941                        !!!cp (135.1);
1942                        $self->{state} = CDATA_BLOCK_STATE;
1943                        !!!next-input-character;
1944                        redo A;
1945                      } else {
1946                        !!!cp (135.2);
1947                      }
1948                    } else {
1949                      !!!cp (135.3);
1950                    }
1951                  } else {
1952                    !!!cp (135.4);                
1953                  }
1954                } else {
1955                  !!!cp (135.5);
1956                }
1957              } else {
1958                !!!cp (135.6);
1959              }
1960            } else {
1961              !!!cp (135.7);
1962            }
1963          } else {
1964            !!!cp (136);
1965        }        }
1966    
1967        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1968        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1969        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1970        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1971          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1972                                    line => $l, column => $c,
1973                                   };
1974        redo A;        redo A;
1975                
1976        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1977        ## 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?
1978      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1979        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1980          $self->{state} = 'comment dash';          !!!cp (137);
1981            $self->{state} = COMMENT_START_DASH_STATE;
1982            !!!next-input-character;
1983            redo A;
1984          } elsif ($self->{next_char} == 0x003E) { # >
1985            !!!cp (138);
1986            !!!parse-error (type => 'bogus comment');
1987            $self->{state} = DATA_STATE;
1988            !!!next-input-character;
1989    
1990            !!!emit ($self->{current_token}); # comment
1991    
1992            redo A;
1993          } elsif ($self->{next_char} == -1) {
1994            !!!cp (139);
1995            !!!parse-error (type => 'unclosed comment');
1996            $self->{state} = DATA_STATE;
1997            ## reconsume
1998    
1999            !!!emit ($self->{current_token}); # comment
2000    
2001            redo A;
2002          } else {
2003            !!!cp (140);
2004            $self->{current_token}->{data} # comment
2005                .= chr ($self->{next_char});
2006            $self->{state} = COMMENT_STATE;
2007          !!!next-input-character;          !!!next-input-character;
2008          redo A;          redo A;
2009        } elsif ($self->{next_input_character} == -1) {        }
2010        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2011          if ($self->{next_char} == 0x002D) { # -
2012            !!!cp (141);
2013            $self->{state} = COMMENT_END_STATE;
2014            !!!next-input-character;
2015            redo A;
2016          } elsif ($self->{next_char} == 0x003E) { # >
2017            !!!cp (142);
2018            !!!parse-error (type => 'bogus comment');
2019            $self->{state} = DATA_STATE;
2020            !!!next-input-character;
2021    
2022            !!!emit ($self->{current_token}); # comment
2023    
2024            redo A;
2025          } elsif ($self->{next_char} == -1) {
2026            !!!cp (143);
2027          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2028          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2029          ## reconsume          ## reconsume
2030    
2031          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2032    
2033          redo A;          redo A;
2034        } else {        } else {
2035          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (144);
2036            $self->{current_token}->{data} # comment
2037                .= '-' . chr ($self->{next_char});
2038            $self->{state} = COMMENT_STATE;
2039            !!!next-input-character;
2040            redo A;
2041          }
2042        } elsif ($self->{state} == COMMENT_STATE) {
2043          if ($self->{next_char} == 0x002D) { # -
2044            !!!cp (145);
2045            $self->{state} = COMMENT_END_DASH_STATE;
2046            !!!next-input-character;
2047            redo A;
2048          } elsif ($self->{next_char} == -1) {
2049            !!!cp (146);
2050            !!!parse-error (type => 'unclosed comment');
2051            $self->{state} = DATA_STATE;
2052            ## reconsume
2053    
2054            !!!emit ($self->{current_token}); # comment
2055    
2056            redo A;
2057          } else {
2058            !!!cp (147);
2059            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2060          ## Stay in the state          ## Stay in the state
2061          !!!next-input-character;          !!!next-input-character;
2062          redo A;          redo A;
2063        }        }
2064      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2065        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2066          $self->{state} = 'comment end';          !!!cp (148);
2067            $self->{state} = COMMENT_END_STATE;
2068          !!!next-input-character;          !!!next-input-character;
2069          redo A;          redo A;
2070        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2071            !!!cp (149);
2072          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2073          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2074          ## reconsume          ## reconsume
2075    
2076          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2077    
2078          redo A;          redo A;
2079        } else {        } else {
2080          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2081          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2082            $self->{state} = COMMENT_STATE;
2083          !!!next-input-character;          !!!next-input-character;
2084          redo A;          redo A;
2085        }        }
2086      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2087        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2088          $self->{state} = 'data';          !!!cp (151);
2089            $self->{state} = DATA_STATE;
2090          !!!next-input-character;          !!!next-input-character;
2091    
2092          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2093    
2094          redo A;          redo A;
2095        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2096          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2097            !!!parse-error (type => 'dash in comment',
2098                            line => $self->{line_prev},
2099                            column => $self->{column_prev});
2100          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2101          ## Stay in the state          ## Stay in the state
2102          !!!next-input-character;          !!!next-input-character;
2103          redo A;          redo A;
2104        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2105            !!!cp (153);
2106          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2107          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2108          ## reconsume          ## reconsume
2109    
2110          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2111    
2112          redo A;          redo A;
2113        } else {        } else {
2114          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2115          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2116          $self->{state} = 'comment';                          line => $self->{line_prev},
2117                            column => $self->{column_prev});
2118            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2119            $self->{state} = COMMENT_STATE;
2120          !!!next-input-character;          !!!next-input-character;
2121          redo A;          redo A;
2122        }        }
2123      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2124        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2125            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2126            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2127            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2128            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2129          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2130            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2131          !!!next-input-character;          !!!next-input-character;
2132          redo A;          redo A;
2133        } else {        } else {
2134            !!!cp (156);
2135          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2136          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2137          ## reconsume          ## reconsume
2138          redo A;          redo A;
2139        }        }
2140      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2141        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2142            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2143            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2144            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2145            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2146            !!!cp (157);
2147          ## Stay in the state          ## Stay in the state
2148          !!!next-input-character;          !!!next-input-character;
2149          redo A;          redo A;
2150        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
2151                 $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) { # >  
2152          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2153          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2154          !!!next-input-character;          !!!next-input-character;
2155    
2156          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2157    
2158          redo A;          redo A;
2159        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2160            !!!cp (159);
2161          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2162          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2163          ## reconsume          ## reconsume
2164    
2165          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2166    
2167          redo A;          redo A;
2168        } else {        } else {
2169          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (160);
2170                            name => chr ($self->{next_input_character}),          $self->{current_token}->{name} = chr $self->{next_char};
2171                            error => 1};          delete $self->{current_token}->{quirks};
2172  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2173          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2174          !!!next-input-character;          !!!next-input-character;
2175          redo A;          redo A;
2176        }        }
2177      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2178        if ($self->{next_input_character} == 0x0009 or # HT  ## ISSUE: Redundant "First," in the spec.
2179            $self->{next_input_character} == 0x000A or # LF        if ($self->{next_char} == 0x0009 or # HT
2180            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000A or # LF
2181            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000B or # VT
2182            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x000C or # FF
2183          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE            $self->{next_char} == 0x0020) { # SP
2184          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2185            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2186          !!!next-input-character;          !!!next-input-character;
2187          redo A;          redo A;
2188        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2189          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (162);
2190          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2191          !!!next-input-character;          !!!next-input-character;
2192    
2193          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2194    
2195          redo A;          redo A;
2196        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
2197                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (163);
2198          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed DOCTYPE');
2199          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{state} = DATA_STATE;
2200            ## reconsume
2201    
2202            $self->{current_token}->{quirks} = 1;
2203            !!!emit ($self->{current_token}); # DOCTYPE
2204    
2205            redo A;
2206          } else {
2207            !!!cp (164);
2208            $self->{current_token}->{name}
2209              .= chr ($self->{next_char}); # DOCTYPE
2210            ## Stay in the state
2211            !!!next-input-character;
2212            redo A;
2213          }
2214        } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2215          if ($self->{next_char} == 0x0009 or # HT
2216              $self->{next_char} == 0x000A or # LF
2217              $self->{next_char} == 0x000B or # VT
2218              $self->{next_char} == 0x000C or # FF
2219              $self->{next_char} == 0x0020) { # SP
2220            !!!cp (165);
2221          ## Stay in the state          ## Stay in the state
2222          !!!next-input-character;          !!!next-input-character;
2223          redo A;          redo A;
2224        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2225            !!!cp (166);
2226            $self->{state} = DATA_STATE;
2227            !!!next-input-character;
2228    
2229            !!!emit ($self->{current_token}); # DOCTYPE
2230    
2231            redo A;
2232          } elsif ($self->{next_char} == -1) {
2233            !!!cp (167);
2234          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2235          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
2236          ## reconsume          ## reconsume
2237    
2238          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
2239          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
2240    
2241          redo A;          redo A;
2242          } elsif ($self->{next_char} == 0x0050 or # P
2243                   $self->{next_char} == 0x0070) { # p
2244            !!!next-input-character;
2245            if ($self->{next_char} == 0x0055 or # U
2246                $self->{next_char} == 0x0075) { # u
2247              !!!next-input-character;
2248              if ($self->{next_char} == 0x0042 or # B
2249                  $self->{next_char} == 0x0062) { # b
2250                !!!next-input-character;
2251                if ($self->{next_char} == 0x004C or # L
2252                    $self->{next_char} == 0x006C) { # l
2253                  !!!next-input-character;
2254                  if ($self->{next_char} == 0x0049 or # I
2255                      $self->{next_char} == 0x0069) { # i
2256                    !!!next-input-character;
2257                    if ($self->{next_char} == 0x0043 or # C
2258                        $self->{next_char} == 0x0063) { # c
2259                      !!!cp (168);
2260                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2261                      !!!next-input-character;
2262                      redo A;
2263                    } else {
2264                      !!!cp (169);
2265                    }
2266                  } else {
2267                    !!!cp (170);
2268                  }
2269                } else {
2270                  !!!cp (171);
2271                }
2272              } else {
2273                !!!cp (172);
2274              }
2275            } else {
2276              !!!cp (173);
2277            }
2278    
2279            #
2280          } elsif ($self->{next_char} == 0x0053 or # S
2281                   $self->{next_char} == 0x0073) { # s
2282            !!!next-input-character;
2283            if ($self->{next_char} == 0x0059 or # Y
2284                $self->{next_char} == 0x0079) { # y
2285              !!!next-input-character;
2286              if ($self->{next_char} == 0x0053 or # S
2287                  $self->{next_char} == 0x0073) { # s
2288                !!!next-input-character;
2289                if ($self->{next_char} == 0x0054 or # T
2290                    $self->{next_char} == 0x0074) { # t
2291                  !!!next-input-character;
2292                  if ($self->{next_char} == 0x0045 or # E
2293                      $self->{next_char} == 0x0065) { # e
2294                    !!!next-input-character;
2295                    if ($self->{next_char} == 0x004D or # M
2296                        $self->{next_char} == 0x006D) { # m
2297                      !!!cp (174);
2298                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2299                      !!!next-input-character;
2300                      redo A;
2301                    } else {
2302                      !!!cp (175);
2303                    }
2304                  } else {
2305                    !!!cp (176);
2306                  }
2307                } else {
2308                  !!!cp (177);
2309                }
2310              } else {
2311                !!!cp (178);
2312              }
2313            } else {
2314              !!!cp (179);
2315            }
2316    
2317            #
2318        } else {        } else {
2319          $self->{current_token}->{name}          !!!cp (180);
2320            .= chr ($self->{next_input_character}); # DOCTYPE          !!!next-input-character;
2321          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          #
2322          }
2323    
2324          !!!parse-error (type => 'string after DOCTYPE name');
2325          $self->{current_token}->{quirks} = 1;
2326    
2327          $self->{state} = BOGUS_DOCTYPE_STATE;
2328          # next-input-character is already done
2329          redo A;
2330        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2331          if ({
2332                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2333                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2334              }->{$self->{next_char}}) {
2335            !!!cp (181);
2336            ## Stay in the state
2337            !!!next-input-character;
2338            redo A;
2339          } elsif ($self->{next_char} eq 0x0022) { # "
2340            !!!cp (182);
2341            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2342            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2343            !!!next-input-character;
2344            redo A;
2345          } elsif ($self->{next_char} eq 0x0027) { # '
2346            !!!cp (183);
2347            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2348            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2349            !!!next-input-character;
2350            redo A;
2351          } elsif ($self->{next_char} eq 0x003E) { # >
2352            !!!cp (184);
2353            !!!parse-error (type => 'no PUBLIC literal');
2354    
2355            $self->{state} = DATA_STATE;
2356            !!!next-input-character;
2357    
2358            $self->{current_token}->{quirks} = 1;
2359            !!!emit ($self->{current_token}); # DOCTYPE
2360    
2361            redo A;
2362          } elsif ($self->{next_char} == -1) {
2363            !!!cp (185);
2364            !!!parse-error (type => 'unclosed DOCTYPE');
2365    
2366            $self->{state} = DATA_STATE;
2367            ## reconsume
2368    
2369            $self->{current_token}->{quirks} = 1;
2370            !!!emit ($self->{current_token}); # DOCTYPE
2371    
2372            redo A;
2373          } else {
2374            !!!cp (186);
2375            !!!parse-error (type => 'string after PUBLIC');
2376            $self->{current_token}->{quirks} = 1;
2377    
2378            $self->{state} = BOGUS_DOCTYPE_STATE;
2379            !!!next-input-character;
2380            redo A;
2381          }
2382        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2383          if ($self->{next_char} == 0x0022) { # "
2384            !!!cp (187);
2385            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2386            !!!next-input-character;
2387            redo A;
2388          } elsif ($self->{next_char} == 0x003E) { # >
2389            !!!cp (188);
2390            !!!parse-error (type => 'unclosed PUBLIC literal');
2391    
2392            $self->{state} = DATA_STATE;
2393            !!!next-input-character;
2394    
2395            $self->{current_token}->{quirks} = 1;
2396            !!!emit ($self->{current_token}); # DOCTYPE
2397    
2398            redo A;
2399          } elsif ($self->{next_char} == -1) {
2400            !!!cp (189);
2401            !!!parse-error (type => 'unclosed PUBLIC literal');
2402    
2403            $self->{state} = DATA_STATE;
2404            ## reconsume
2405    
2406            $self->{current_token}->{quirks} = 1;
2407            !!!emit ($self->{current_token}); # DOCTYPE
2408    
2409            redo A;
2410          } else {
2411            !!!cp (190);
2412            $self->{current_token}->{public_identifier} # DOCTYPE
2413                .= chr $self->{next_char};
2414          ## Stay in the state          ## Stay in the state
2415          !!!next-input-character;          !!!next-input-character;
2416          redo A;          redo A;
2417        }        }
2418      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2419        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0027) { # '
2420            $self->{next_input_character} == 0x000A or # LF          !!!cp (191);
2421            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2422            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
2423            $self->{next_input_character} == 0x0020) { # SP          redo A;
2424          } elsif ($self->{next_char} == 0x003E) { # >
2425            !!!cp (192);
2426            !!!parse-error (type => 'unclosed PUBLIC literal');
2427    
2428            $self->{state} = DATA_STATE;
2429            !!!next-input-character;
2430    
2431            $self->{current_token}->{quirks} = 1;
2432            !!!emit ($self->{current_token}); # DOCTYPE
2433    
2434            redo A;
2435          } elsif ($self->{next_char} == -1) {
2436            !!!cp (193);
2437            !!!parse-error (type => 'unclosed PUBLIC literal');
2438    
2439            $self->{state} = DATA_STATE;
2440            ## reconsume
2441    
2442            $self->{current_token}->{quirks} = 1;
2443            !!!emit ($self->{current_token}); # DOCTYPE
2444    
2445            redo A;
2446          } else {
2447            !!!cp (194);
2448            $self->{current_token}->{public_identifier} # DOCTYPE
2449                .= chr $self->{next_char};
2450            ## Stay in the state
2451            !!!next-input-character;
2452            redo A;
2453          }
2454        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_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 (195);
2460          ## Stay in the state          ## Stay in the state
2461          !!!next-input-character;          !!!next-input-character;
2462          redo A;          redo A;
2463        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x0022) { # "
2464          $self->{state} = 'data';          !!!cp (196);
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 (197);
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 (198);
2477            $self->{state} = DATA_STATE;
2478          !!!next-input-character;          !!!next-input-character;
2479    
2480          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2481    
2482          redo A;          redo A;
2483        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2484            !!!cp (199);
2485          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2486          $self->{state} = 'data';  
2487            $self->{state} = DATA_STATE;
2488          ## reconsume          ## reconsume
2489    
2490            $self->{current_token}->{quirks} = 1;
2491          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2492    
2493          redo A;          redo A;
2494        } else {        } else {
2495          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (200);
2496          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after PUBLIC literal');
2497          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2498    
2499            $self->{state} = BOGUS_DOCTYPE_STATE;
2500          !!!next-input-character;          !!!next-input-character;
2501          redo A;          redo A;
2502        }        }
2503      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2504        if ($self->{next_input_character} == 0x003E) { # >        if ({
2505          $self->{state} = 'data';              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2506                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2507              }->{$self->{next_char}}) {
2508            !!!cp (201);
2509            ## Stay in the state
2510            !!!next-input-character;
2511            redo A;
2512          } elsif ($self->{next_char} == 0x0022) { # "
2513            !!!cp (202);
2514            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2515            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2516            !!!next-input-character;
2517            redo A;
2518          } elsif ($self->{next_char} == 0x0027) { # '
2519            !!!cp (203);
2520            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2521            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2522            !!!next-input-character;
2523            redo A;
2524          } elsif ($self->{next_char} == 0x003E) { # >
2525            !!!cp (204);
2526            !!!parse-error (type => 'no SYSTEM literal');
2527            $self->{state} = DATA_STATE;
2528          !!!next-input-character;          !!!next-input-character;
2529    
2530            $self->{current_token}->{quirks} = 1;
2531          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2532    
2533          redo A;          redo A;
2534        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2535            !!!cp (205);
2536          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2537          $self->{state} = 'data';  
2538            $self->{state} = DATA_STATE;
2539            ## reconsume
2540    
2541            $self->{current_token}->{quirks} = 1;
2542            !!!emit ($self->{current_token}); # DOCTYPE
2543    
2544            redo A;
2545          } else {
2546            !!!cp (206);
2547            !!!parse-error (type => 'string after SYSTEM');
2548            $self->{current_token}->{quirks} = 1;
2549    
2550            $self->{state} = BOGUS_DOCTYPE_STATE;
2551            !!!next-input-character;
2552            redo A;
2553          }
2554        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2555          if ($self->{next_char} == 0x0022) { # "
2556            !!!cp (207);
2557            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2558            !!!next-input-character;
2559            redo A;
2560          } elsif ($self->{next_char} == 0x003E) { # >
2561            !!!cp (208);
2562            !!!parse-error (type => 'unclosed PUBLIC literal');
2563    
2564            $self->{state} = DATA_STATE;
2565            !!!next-input-character;
2566    
2567            $self->{current_token}->{quirks} = 1;
2568            !!!emit ($self->{current_token}); # DOCTYPE
2569    
2570            redo A;
2571          } elsif ($self->{next_char} == -1) {
2572            !!!cp (209);
2573            !!!parse-error (type => 'unclosed SYSTEM literal');
2574    
2575            $self->{state} = DATA_STATE;
2576          ## reconsume          ## reconsume
2577    
2578            $self->{current_token}->{quirks} = 1;
2579          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2580    
2581          redo A;          redo A;
2582        } else {        } else {
2583            !!!cp (210);
2584            $self->{current_token}->{system_identifier} # DOCTYPE
2585                .= chr $self->{next_char};
2586          ## Stay in the state          ## Stay in the state
2587          !!!next-input-character;          !!!next-input-character;
2588          redo A;          redo A;
2589        }        }
2590        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2591          if ($self->{next_char} == 0x0027) { # '
2592            !!!cp (211);
2593            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2594            !!!next-input-character;
2595            redo A;
2596          } elsif ($self->{next_char} == 0x003E) { # >
2597            !!!cp (212);
2598            !!!parse-error (type => 'unclosed PUBLIC literal');
2599    
2600            $self->{state} = DATA_STATE;
2601            !!!next-input-character;
2602    
2603            $self->{current_token}->{quirks} = 1;
2604            !!!emit ($self->{current_token}); # DOCTYPE
2605    
2606            redo A;
2607          } elsif ($self->{next_char} == -1) {
2608            !!!cp (213);
2609            !!!parse-error (type => 'unclosed SYSTEM literal');
2610    
2611            $self->{state} = DATA_STATE;
2612            ## reconsume
2613    
2614            $self->{current_token}->{quirks} = 1;
2615            !!!emit ($self->{current_token}); # DOCTYPE
2616    
2617            redo A;
2618          } else {
2619            !!!cp (214);
2620            $self->{current_token}->{system_identifier} # DOCTYPE
2621                .= chr $self->{next_char};
2622            ## Stay in the state
2623            !!!next-input-character;
2624            redo A;
2625          }
2626        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2627          if ({
2628                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2629                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2630              }->{$self->{next_char}}) {
2631            !!!cp (215);
2632            ## Stay in the state
2633            !!!next-input-character;
2634            redo A;
2635          } elsif ($self->{next_char} == 0x003E) { # >
2636            !!!cp (216);
2637            $self->{state} = DATA_STATE;
2638            !!!next-input-character;
2639    
2640            !!!emit ($self->{current_token}); # DOCTYPE
2641    
2642            redo A;
2643          } elsif ($self->{next_char} == -1) {
2644            !!!cp (217);
2645            !!!parse-error (type => 'unclosed DOCTYPE');
2646    
2647            $self->{state} = DATA_STATE;
2648            ## reconsume
2649    
2650            $self->{current_token}->{quirks} = 1;
2651            !!!emit ($self->{current_token}); # DOCTYPE
2652    
2653            redo A;
2654          } else {
2655            !!!cp (218);
2656            !!!parse-error (type => 'string after SYSTEM literal');
2657            #$self->{current_token}->{quirks} = 1;
2658    
2659            $self->{state} = BOGUS_DOCTYPE_STATE;
2660            !!!next-input-character;
2661            redo A;
2662          }
2663        } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2664          if ($self->{next_char} == 0x003E) { # >
2665            !!!cp (219);
2666            $self->{state} = DATA_STATE;
2667            !!!next-input-character;
2668    
2669            !!!emit ($self->{current_token}); # DOCTYPE
2670    
2671            redo A;
2672          } elsif ($self->{next_char} == -1) {
2673            !!!cp (220);
2674            !!!parse-error (type => 'unclosed DOCTYPE');
2675            $self->{state} = DATA_STATE;
2676            ## reconsume
2677    
2678            !!!emit ($self->{current_token}); # DOCTYPE
2679    
2680            redo A;
2681          } else {
2682            !!!cp (221);
2683            ## Stay in the state
2684            !!!next-input-character;
2685            redo A;
2686          }
2687        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2688          my $s = '';
2689          
2690          my ($l, $c) = ($self->{line}, $self->{column});
2691    
2692          CS: while ($self->{next_char} != -1) {
2693            if ($self->{next_char} == 0x005D) { # ]
2694              !!!next-input-character;
2695              if ($self->{next_char} == 0x005D) { # ]
2696                !!!next-input-character;
2697                MDC: {
2698                  if ($self->{next_char} == 0x003E) { # >
2699                    !!!cp (221.1);
2700                    !!!next-input-character;
2701                    last CS;
2702                  } elsif ($self->{next_char} == 0x005D) { # ]
2703                    !!!cp (221.2);
2704                    $s .= ']';
2705                    !!!next-input-character;
2706                    redo MDC;
2707                  } else {
2708                    !!!cp (221.3);
2709                    $s .= ']]';
2710                    #
2711                  }
2712                } # MDC
2713              } else {
2714                !!!cp (221.4);
2715                $s .= ']';
2716                #
2717              }
2718            } else {
2719              !!!cp (221.5);
2720              #
2721            }
2722            $s .= chr $self->{next_char};
2723            !!!next-input-character;
2724          } # CS
2725    
2726          $self->{state} = DATA_STATE;
2727          ## next-input-character done or EOF, which is reconsumed.
2728    
2729          if (length $s) {
2730            !!!cp (221.6);
2731            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2732                      line => $l, column => $c});
2733          } else {
2734            !!!cp (221.7);
2735          }
2736    
2737          redo A;
2738    
2739          ## ISSUE: "text tokens" in spec.
2740          ## TODO: Streaming support
2741      } else {      } else {
2742        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2743      }      }
# Line 1490  sub _get_next_token ($) { Line 2746  sub _get_next_token ($) {
2746    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2747  } # _get_next_token  } # _get_next_token
2748    
2749  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2750    my $self = shift;    my ($self, $in_attr, $additional) = @_;
2751      
2752    if ($self->{next_input_character} == 0x0023) { # #    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2753    
2754      if ({
2755           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2756           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2757           $additional => 1,
2758          }->{$self->{next_char}}) {
2759        !!!cp (1001);
2760        ## Don't consume
2761        ## No error
2762        return undef;
2763      } elsif ($self->{next_char} == 0x0023) { # #
2764      !!!next-input-character;      !!!next-input-character;
2765      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2766          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2767        my $num;        my $code;
2768        X: {        X: {
2769          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2770          !!!next-input-character;          !!!next-input-character;
2771          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2772              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2773            $num ||= 0;            !!!cp (1002);
2774            $num *= 0x10;            $code ||= 0;
2775            $num += $self->{next_input_character} - 0x0030;            $code *= 0x10;
2776              $code += $self->{next_char} - 0x0030;
2777            redo X;            redo X;
2778          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2779                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2780            ## ISSUE: the spec says U+0078, which is apparently incorrect            !!!cp (1003);
2781            $num ||= 0;            $code ||= 0;
2782            $num *= 0x10;            $code *= 0x10;
2783            $num += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2784            redo X;            redo X;
2785          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2786                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2787            ## ISSUE: the spec says U+0058, which is apparently incorrect            !!!cp (1004);
2788            $num ||= 0;            $code ||= 0;
2789            $num *= 0x10;            $code *= 0x10;
2790            $num += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2791            redo X;            redo X;
2792          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2793            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2794            $self->{next_input_character} = 0x0023; # #            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2795            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char, $self->{next_char});
2796              $self->{next_char} = 0x0023; # #
2797            return undef;            return undef;
2798          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2799              !!!cp (1006);
2800            !!!next-input-character;            !!!next-input-character;
2801          } else {          } else {
2802            !!!parse-error (type => 'no refc');            !!!cp (1007);
2803              !!!parse-error (type => 'no refc', line => $l, column => $c);
2804          }          }
2805    
2806          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2807          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!cp (1008);
2808          if ($num > 1114111 or $num == 0) {            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2809            $num = 0xFFFD; # REPLACEMENT CHARACTER            $code = 0xFFFD;
2810            ## ISSUE: Why this is not an error?          } elsif ($code > 0x10FFFF) {
2811          } elsif (0x80 <= $num and $num <= 0x9F) {            !!!cp (1009);
2812            ## NOTE: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2813            ## ISSUE: Not in the spec yet; parse error?            $code = 0xFFFD;
2814            $num = $c1_entity_char->{$num};          } elsif ($code == 0x000D) {
2815          }            !!!cp (1010);
2816              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2817          return {type => 'character', data => chr $num};            $code = 0x000A;
2818            } elsif (0x80 <= $code and $code <= 0x9F) {
2819              !!!cp (1011);
2820              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2821              $code = $c1_entity_char->{$code};
2822            }
2823    
2824            return {type => CHARACTER_TOKEN, data => chr $code,
2825                    has_reference => 1,
2826                    line => $l, column => $c,
2827                   };
2828        } # X        } # X
2829      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2830               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2831        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2832        !!!next-input-character;        !!!next-input-character;
2833                
2834        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2835                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2836            !!!cp (1012);
2837          $code *= 10;          $code *= 10;
2838          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2839                    
2840          !!!next-input-character;          !!!next-input-character;
2841        }        }
2842    
2843        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2844            !!!cp (1013);
2845          !!!next-input-character;          !!!next-input-character;
2846        } else {        } else {
2847          !!!parse-error (type => 'no refc');          !!!cp (1014);
2848            !!!parse-error (type => 'no refc', line => $l, column => $c);
2849        }        }
2850    
2851        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2852        if ($code > 1114111 or $code == 0) {          !!!cp (1015);
2853          $code = 0xFFFD; # REPLACEMENT CHARACTER          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2854          ## ISSUE: Why this is not an error?          $code = 0xFFFD;
2855          } elsif ($code > 0x10FFFF) {
2856            !!!cp (1016);
2857            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2858            $code = 0xFFFD;
2859          } elsif ($code == 0x000D) {
2860            !!!cp (1017);
2861            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2862            $code = 0x000A;
2863        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2864          ## NOTE: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>          !!!cp (1018);
2865          ## ISSUE: Not in the spec yet; parse error?          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2866          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2867        }        }
2868                
2869        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2870                  line => $l, column => $c,
2871                 };
2872      } else {      } else {
2873        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2874        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2875        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2876          $self->{next_char} = 0x0023; # #
2877        return undef;        return undef;
2878      }      }
2879    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2880              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2881             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2882              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2883      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2884      !!!next-input-character;      !!!next-input-character;
2885    
2886      my $value = $entity_name;      my $value = $entity_name;
2887      my $match;      my $match = 0;
2888        require Whatpm::_NamedEntityList;
2889        our $EntityChar;
2890    
2891      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2892             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2893             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_char} and # a
2894               $self->{next_input_character} <= 0x005A) or               $self->{next_char} <= 0x005A) or # x
2895              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_char} and # a
2896               $self->{next_input_character} <= 0x007A) or               $self->{next_char} <= 0x007A) or # z
2897              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_char} and # 0
2898               $self->{next_input_character} <= 0x0039))) {               $self->{next_char} <= 0x0039) or # 9
2899        $entity_name .= chr $self->{next_input_character};              $self->{next_char} == 0x003B)) { # ;
2900        if (defined $entity_char->{$entity_name}) {        $entity_name .= chr $self->{next_char};
2901          $value = $entity_char->{$entity_name};        if (defined $EntityChar->{$entity_name}) {
2902          $match = 1;          if ($self->{next_char} == 0x003B) { # ;
2903              !!!cp (1020);
2904              $value = $EntityChar->{$entity_name};
2905              $match = 1;
2906              !!!next-input-character;
2907              last;
2908            } else {
2909              !!!cp (1021);
2910              $value = $EntityChar->{$entity_name};
2911              $match = -1;
2912              !!!next-input-character;
2913            }
2914        } else {        } else {
2915          $value .= chr $self->{next_input_character};          !!!cp (1022);
2916            $value .= chr $self->{next_char};
2917            $match *= 2;
2918            !!!next-input-character;
2919        }        }
       !!!next-input-character;  
2920      }      }
2921            
2922      if ($match) {      if ($match > 0) {
2923        if ($self->{next_input_character} == 0x003B) { # ;        !!!cp (1023);
2924          !!!next-input-character;        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2925                  line => $l, column => $c,
2926                 };
2927        } elsif ($match < 0) {
2928          !!!parse-error (type => 'no refc', line => $l, column => $c);
2929          if ($in_attr and $match < -1) {
2930            !!!cp (1024);
2931            return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2932                    line => $l, column => $c,
2933                   };
2934        } else {        } else {
2935          !!!parse-error (type => 'refc');          !!!cp (1025);
2936            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2937                    line => $l, column => $c,
2938                   };
2939        }        }
   
       return {type => 'character', data => $value};  
2940      } else {      } else {
2941        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2942        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2943        !!!back-token ({type => 'character', data => $value});        ## NOTE: "No characters are consumed" in the spec.
2944        return undef;        return {type => CHARACTER_TOKEN, data => '&'.$value,
2945                  line => $l, column => $c,
2946                 };
2947      }      }
2948    } else {    } else {
2949        !!!cp (1027);
2950      ## no characters are consumed      ## no characters are consumed
2951      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2952      return undef;      return undef;
2953    }    }
2954  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1636  sub _initialize_tree_constructor ($) { Line 2959  sub _initialize_tree_constructor ($) {
2959    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
2960    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
2961    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
2962    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
2963  } # _initialize_tree_constructor  } # _initialize_tree_constructor
2964    
2965  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1663  sub _construct_tree ($) { Line 2986  sub _construct_tree ($) {
2986        
2987    !!!next-token;    !!!next-token;
2988    
   $self->{insertion_mode} = 'before head';  
2989    undef $self->{form_element};    undef $self->{form_element};
2990    undef $self->{head_element};    undef $self->{head_element};
2991    $self->{open_elements} = [];    $self->{open_elements} = [];
2992    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2993    
2994      ## NOTE: The "initial" insertion mode.
2995    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2996    
2997      ## NOTE: The "before html" insertion mode.
2998    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2999      $self->{insertion_mode} = BEFORE_HEAD_IM;
3000    
3001      ## NOTE: The "before head" insertion mode and so on.
3002    $self->_tree_construction_main;    $self->_tree_construction_main;
3003  } # _construct_tree  } # _construct_tree
3004    
3005  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3006    my $self = shift;    my $self = shift;
3007    B: {  
3008        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
3009          if ($token->{error}) {  
3010            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
3011            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
3012          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3013          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
3014            ($token->{name});        ## language.
3015          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
3016          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
3017          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/;
3018          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
3019          return;            defined $token->{public_identifier} or
3020        } elsif ({            defined $token->{system_identifier}) {
3021                  comment => 1,          !!!cp ('t1');
3022                  'start tag' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3023                  'end tag' => 1,        } elsif ($doctype_name ne 'HTML') {
3024                  'end-of-file' => 1,          !!!cp ('t2');
3025                 }->{$token->{type}}) {          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3026          ## ISSUE: Spec currently left this case undefined.          !!!parse-error (type => 'not HTML5', token => $token);
3027          !!!parse-error (type => 'missing DOCTYPE');        } else {
3028          #$phase = 'root element';          !!!cp ('t3');
3029          ## reprocess        }
3030          #redo B;        
3031          return;        my $doctype = $self->{document}->create_document_type_definition
3032        } elsif ($token->{type} eq 'character') {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3033          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        ## NOTE: Default value for both |public_id| and |system_id| attributes
3034            $self->{document}->manakai_append_text ($1);        ## are empty strings, so that we don't set any value in missing cases.
3035            ## ISSUE: DOM3 Core does not allow Document > Text        $doctype->public_id ($token->{public_identifier})
3036            unless (length $token->{data}) {            if defined $token->{public_identifier};
3037              ## Stay in the phase        $doctype->system_id ($token->{system_identifier})
3038              !!!next-token;            if defined $token->{system_identifier};
3039              redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
3040          ## ISSUE: internalSubset = null??
3041          $self->{document}->append_child ($doctype);
3042          
3043          if ($token->{quirks} or $doctype_name ne 'HTML') {
3044            !!!cp ('t4');
3045            $self->{document}->manakai_compat_mode ('quirks');
3046          } elsif (defined $token->{public_identifier}) {
3047            my $pubid = $token->{public_identifier};
3048            $pubid =~ tr/a-z/A-z/;
3049            if ({
3050              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
3051              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
3052              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
3053              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
3054              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
3055              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
3056              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
3057              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
3058              "-//IETF//DTD HTML 2.0//EN" => 1,
3059              "-//IETF//DTD HTML 2.1E//EN" => 1,
3060              "-//IETF//DTD HTML 3.0//EN" => 1,
3061              "-//IETF//DTD HTML 3.0//EN//" => 1,
3062              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
3063              "-//IETF//DTD HTML 3.2//EN" => 1,
3064              "-//IETF//DTD HTML 3//EN" => 1,
3065              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
3066              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
3067              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
3068              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
3069              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
3070              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
3071              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
3072              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
3073              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
3074              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
3075              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
3076              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
3077              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
3078              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
3079              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
3080              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
3081              "-//IETF//DTD HTML STRICT//EN" => 1,
3082              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
3083              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
3084              "-//IETF//DTD HTML//EN" => 1,
3085              "-//IETF//DTD HTML//EN//2.0" => 1,
3086              "-//IETF//DTD HTML//EN//3.0" => 1,
3087              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
3088              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
3089              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
3090              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
3091              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
3092              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
3093              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
3094              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
3095              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
3096              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
3097              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
3098              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
3099              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
3100              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
3101              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
3102              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
3103              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
3104              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
3105              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
3106              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
3107              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
3108              "-//W3C//DTD HTML 3.2//EN" => 1,
3109              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
3110              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
3111              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
3112              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
3113              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
3114              "-//W3C//DTD W3 HTML//EN" => 1,
3115              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
3116              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
3117              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
3118              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
3119              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
3120              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
3121              "HTML" => 1,
3122            }->{$pubid}) {
3123              !!!cp ('t5');
3124              $self->{document}->manakai_compat_mode ('quirks');
3125            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
3126                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
3127              if (defined $token->{system_identifier}) {
3128                !!!cp ('t6');
3129                $self->{document}->manakai_compat_mode ('quirks');
3130              } else {
3131                !!!cp ('t7');
3132                $self->{document}->manakai_compat_mode ('limited quirks');
3133            }            }
3134            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
3135                     $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
3136              !!!cp ('t8');
3137              $self->{document}->manakai_compat_mode ('limited quirks');
3138            } else {
3139              !!!cp ('t9');
3140          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
3141        } else {        } else {
3142          die "$0: $token->{type}: Unknown token";          !!!cp ('t10');
3143        }        }
3144      } # B        if (defined $token->{system_identifier}) {
3145            my $sysid = $token->{system_identifier};
3146            $sysid =~ tr/A-Z/a-z/;
3147            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3148              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
3149              $self->{document}->manakai_compat_mode ('quirks');
3150              !!!cp ('t11');
3151            } else {
3152              !!!cp ('t12');
3153            }
3154          } else {
3155            !!!cp ('t13');
3156          }
3157          
3158          ## Go to the "before html" insertion mode.
3159          !!!next-token;
3160          return;
3161        } elsif ({
3162                  START_TAG_TOKEN, 1,
3163                  END_TAG_TOKEN, 1,
3164                  END_OF_FILE_TOKEN, 1,
3165                 }->{$token->{type}}) {
3166          !!!cp ('t14');
3167          !!!parse-error (type => 'no DOCTYPE', token => $token);
3168          $self->{document}->manakai_compat_mode ('quirks');
3169          ## Go to the "before html" insertion mode.
3170          ## reprocess
3171          !!!ack-later;
3172          return;
3173        } elsif ($token->{type} == CHARACTER_TOKEN) {
3174          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3175            ## Ignore the token
3176    
3177            unless (length $token->{data}) {
3178              !!!cp ('t15');
3179              ## Stay in the insertion mode.
3180              !!!next-token;
3181              redo INITIAL;
3182            } else {
3183              !!!cp ('t16');
3184            }
3185          } else {
3186            !!!cp ('t17');
3187          }
3188    
3189          !!!parse-error (type => 'no DOCTYPE', token => $token);
3190          $self->{document}->manakai_compat_mode ('quirks');
3191          ## Go to the "before html" insertion mode.
3192          ## reprocess
3193          return;
3194        } elsif ($token->{type} == COMMENT_TOKEN) {
3195          !!!cp ('t18');
3196          my $comment = $self->{document}->create_comment ($token->{data});
3197          $self->{document}->append_child ($comment);
3198          
3199          ## Stay in the insertion mode.
3200          !!!next-token;
3201          redo INITIAL;
3202        } else {
3203          die "$0: $token->{type}: Unknown token type";
3204        }
3205      } # INITIAL
3206    
3207      die "$0: _tree_construction_initial: This should be never reached";
3208  } # _tree_construction_initial  } # _tree_construction_initial
3209    
3210  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3211    my $self = shift;    my $self = shift;
3212    
3213      ## NOTE: "before html" insertion mode.
3214        
3215    B: {    B: {
3216        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3217          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3218            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3219          ## Ignore the token          ## Ignore the token
3220          ## Stay in the phase          ## Stay in the insertion mode.
3221          !!!next-token;          !!!next-token;
3222          redo B;          redo B;
3223        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3224            !!!cp ('t20');
3225          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3226          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3227          ## Stay in the phase          ## Stay in the insertion mode.
3228          !!!next-token;          !!!next-token;
3229          redo B;          redo B;
3230        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3231          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3232            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
3233            ## ISSUE: DOM3 Core does not allow Document > Text  
3234            unless (length $token->{data}) {            unless (length $token->{data}) {
3235              ## Stay in the phase              !!!cp ('t21');
3236                ## Stay in the insertion mode.
3237              !!!next-token;              !!!next-token;
3238              redo B;              redo B;
3239              } else {
3240                !!!cp ('t22');
3241            }            }
3242            } else {
3243              !!!cp ('t23');
3244          }          }
3245    
3246            $self->{application_cache_selection}->(undef);
3247    
3248          #          #
3249          } elsif ($token->{type} == START_TAG_TOKEN) {
3250            if ($token->{tag_name} eq 'html') {
3251              my $root_element;
3252              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3253              $self->{document}->append_child ($root_element);
3254              push @{$self->{open_elements}},
3255                  [$root_element, $el_category->{html}];
3256    
3257              if ($token->{attributes}->{manifest}) {
3258                !!!cp ('t24');
3259                $self->{application_cache_selection}
3260                    ->($token->{attributes}->{manifest}->{value});
3261                ## ISSUE: Spec is unclear on relative references.
3262                ## According to Hixie (#whatwg 2008-03-19), it should be
3263                ## resolved against the base URI of the document in HTML
3264                ## or xml:base of the element in XHTML.
3265              } else {
3266                !!!cp ('t25');
3267                $self->{application_cache_selection}->(undef);
3268              }
3269    
3270              !!!nack ('t25c');
3271    
3272              !!!next-token;
3273              return; ## Go to the "before head" insertion mode.
3274            } else {
3275              !!!cp ('t25.1');
3276              #
3277            }
3278        } elsif ({        } elsif ({
3279                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3280                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3281                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3282          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3283          #          #
3284        } else {        } else {
3285          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3286        }        }
3287        my $root_element; !!!create-element ($root_element, 'html');  
3288        $self->{document}->append_child ($root_element);      my $root_element;
3289        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3290        #$phase = 'main';      $self->{document}->append_child ($root_element);
3291        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3292        #redo B;  
3293        return;      $self->{application_cache_selection}->(undef);
3294    
3295        ## NOTE: Reprocess the token.
3296        !!!ack-later;
3297        return; ## Go to the "before head" insertion mode.
3298    
3299        ## ISSUE: There is an issue in the spec
3300    } # B    } # B
3301    
3302      die "$0: _tree_construction_root_element: This should never be reached";
3303  } # _tree_construction_root_element  } # _tree_construction_root_element
3304    
3305  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1782  sub _reset_insertion_mode ($) { Line 3314  sub _reset_insertion_mode ($) {
3314            
3315      ## Step 3      ## Step 3
3316      S3: {      S3: {
3317        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3318        if (defined $self->{inner_html_node}) {          $last = 1;
3319          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
3320              $self->{inner_html_node}->[1] eq 'th') {            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
3321            #              !!!cp ('t27');
3322          } else {              #
3323            $node = $self->{inner_html_node};            } else {
3324                !!!cp ('t28');
3325                $node = $self->{inner_html_node};
3326              }
3327          }          }
3328        }        }
3329            
3330        ## Step 4..13      ## Step 4..14
3331        my $new_mode = {      my $new_mode;
3332                        select => 'in select',      if ($node->[1] & FOREIGN_EL) {
3333                        td => 'in cell',        ## NOTE: Strictly spaking, the line below only applies to MathML and
3334                        th => 'in cell',        ## SVG elements.  Currently the HTML syntax supports only MathML and
3335                        tr => 'in row',        ## SVG elements as foreigners.
3336                        tbody => 'in table body',        $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3337                        thead => 'in table head',        ## ISSUE: What is set as the secondary insertion mode?
3338                        tfoot => 'in table foot',      } else {
3339                        caption => 'in caption',        $new_mode = {
3340                        colgroup => 'in column group',                        select => IN_SELECT_IM,
3341                        table => 'in table',                        ## NOTE: |option| and |optgroup| do not set
3342                        head => 'in body', # not in head!                        ## insertion mode to "in select" by themselves.
3343                        body => 'in body',                        td => IN_CELL_IM,
3344                        frameset => 'in frameset',                        th => IN_CELL_IM,
3345                       }->{$node->[1]};                        tr => IN_ROW_IM,
3346        $self->{insertion_mode} = $new_mode and return if defined $new_mode;                        tbody => IN_TABLE_BODY_IM,
3347                          thead => IN_TABLE_BODY_IM,
3348                          tfoot => IN_TABLE_BODY_IM,
3349                          caption => IN_CAPTION_IM,
3350                          colgroup => IN_COLUMN_GROUP_IM,
3351                          table => IN_TABLE_IM,
3352                          head => IN_BODY_IM, # not in head!
3353                          body => IN_BODY_IM,
3354                          frameset => IN_FRAMESET_IM,
3355                         }->{$node->[0]->manakai_local_name};
3356        }
3357        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3358                
3359        ## Step 14        ## Step 15
3360        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3361          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3362            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3363              $self->{insertion_mode} = BEFORE_HEAD_IM;
3364          } else {          } else {
3365            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3366              !!!cp ('t30');
3367              $self->{insertion_mode} = AFTER_HEAD_IM;
3368          }          }
3369          return;          return;
3370          } else {
3371            !!!cp ('t31');
3372        }        }
3373                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3374        ## Step 16        ## Step 16
3375          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3376          
3377          ## Step 17
3378        $i--;        $i--;
3379        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3380                
3381        ## Step 17        ## Step 18
3382        redo S3;        redo S3;
3383      } # S3      } # S3
3384    
3385      die "$0: _reset_insertion_mode: This line should never be reached";
3386  } # _reset_insertion_mode  } # _reset_insertion_mode
3387    
3388  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3389    my $self = shift;    my $self = shift;
3390    
   my $phase = 'main';  
   
3391    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3392    
3393    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1853  sub _tree_construction_main ($) { Line 3404  sub _tree_construction_main ($) {
3404      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3405      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3406        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3407            !!!cp ('t32');
3408          return;          return;
3409        }        }
3410      }      }
# Line 1867  sub _tree_construction_main ($) { Line 3419  sub _tree_construction_main ($) {
3419    
3420        ## Step 6        ## Step 6
3421        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3422            !!!cp ('t33_1');
3423          #          #
3424        } else {        } else {
3425          my $in_open_elements;          my $in_open_elements;
3426          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3427            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3428                !!!cp ('t33');
3429              $in_open_elements = 1;              $in_open_elements = 1;
3430              last OE;              last OE;
3431            }            }
3432          }          }
3433          if ($in_open_elements) {          if ($in_open_elements) {
3434              !!!cp ('t34');
3435            #            #
3436          } else {          } else {
3437              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3438              !!!cp ('t35');
3439            redo S4;            redo S4;
3440          }          }
3441        }        }
# Line 1901  sub _tree_construction_main ($) { Line 3458  sub _tree_construction_main ($) {
3458    
3459        ## Step 11        ## Step 11
3460        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3461            !!!cp ('t36');
3462          ## Step 7'          ## Step 7'
3463          $i++;          $i++;
3464          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3465                    
3466          redo S7;          redo S7;
3467        }        }
3468    
3469          !!!cp ('t37');
3470      } # S7      } # S7
3471    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3472    
3473    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3474      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3475        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3476            !!!cp ('t38');
3477          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3478          return;          return;
3479        }        }
3480      }      }
3481    
3482        !!!cp ('t39');
3483    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3484    
3485    my $style_start_tag = sub {    my $insert;
3486      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});  
3487      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
3488      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
3489       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
3490        ->append_child ($style_el);      ## Step 1
3491      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
3492                      my $el;
3493        !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3494    
3495        ## Step 2
3496        $insert->($el);
3497    
3498        ## Step 3
3499        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3500        delete $self->{escape}; # MUST
3501    
3502        ## Step 4
3503      my $text = '';      my $text = '';
3504        !!!nack ('t40.1');
3505      !!!next-token;      !!!next-token;
3506      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3507          !!!cp ('t40');
3508        $text .= $token->{data};        $text .= $token->{data};
3509        !!!next-token;        !!!next-token;
3510      } # stop if non-character token or tokenizer stops tokenising      }
3511    
3512        ## Step 5
3513      if (length $text) {      if (length $text) {
3514        $style_el->manakai_append_text ($text);        !!!cp ('t41');
3515          my $text = $self->{document}->create_text_node ($text);
3516          $el->append_child ($text);
3517      }      }
3518        
3519      $self->{content_model_flag} = 'PCDATA';      ## Step 6
3520                      $self->{content_model} = PCDATA_CONTENT_MODEL;
3521      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
3522        ## Step 7
3523        if ($token->{type} == END_TAG_TOKEN and
3524            $token->{tag_name} eq $start_tag_name) {
3525          !!!cp ('t42');
3526        ## Ignore the token        ## Ignore the token
3527      } else {      } else {
3528        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
3529        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3530            !!!cp ('t43');
3531            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3532          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3533            !!!cp ('t44');
3534            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3535          } else {
3536            die "$0: $content_model_flag in parse_rcdata";
3537          }
3538      }      }
3539      !!!next-token;      !!!next-token;
3540    }; # $style_start_tag    }; # $parse_rcdata
3541    
3542    my $script_start_tag = sub {    my $script_start_tag = sub () {
3543      my $script_el;      my $script_el;
3544      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3545      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3546    
3547      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3548        delete $self->{escape}; # MUST
3549            
3550      my $text = '';      my $text = '';
3551        !!!nack ('t45.1');
3552      !!!next-token;      !!!next-token;
3553      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3554          !!!cp ('t45');
3555        $text .= $token->{data};        $text .= $token->{data};
3556        !!!next-token;        !!!next-token;
3557      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3558      if (length $text) {      if (length $text) {
3559          !!!cp ('t46');
3560        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3561      }      }
3562                                
3563      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3564    
3565      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3566          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3567          !!!cp ('t47');
3568        ## Ignore the token        ## Ignore the token
3569      } else {      } else {
3570        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3571          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3572        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3573        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3574      }      }
3575            
3576      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3577          !!!cp ('t49');
3578        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3579      } else {      } else {
3580          !!!cp ('t50');
3581        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3582        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3583          
3584        (($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);  
3585                
3586        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
3587                
# Line 1993  sub _tree_construction_main ($) { Line 3591  sub _tree_construction_main ($) {
3591      !!!next-token;      !!!next-token;
3592    }; # $script_start_tag    }; # $script_start_tag
3593    
3594      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3595      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3596      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3597    
3598    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3599      my $tag_name = shift;      my $end_tag_token = shift;
3600        my $tag_name = $end_tag_token->{tag_name};
3601    
3602        ## NOTE: The adoption agency algorithm (AAA).
3603    
3604      FET: {      FET: {
3605        ## Step 1        ## Step 1
3606        my $formatting_element;        my $formatting_element;
3607        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3608        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3609          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3610              !!!cp ('t52');
3611              last AFE;
3612            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3613                         eq $tag_name) {
3614              !!!cp ('t51');
3615            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3616            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3617            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3618          }          }
3619        } # AFE        } # AFE
3620        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3621          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3622            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3623          ## Ignore the token          ## Ignore the token
3624          !!!next-token;          !!!next-token;
3625          return;          return;
# Line 2022  sub _tree_construction_main ($) { Line 3631  sub _tree_construction_main ($) {
3631          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3632          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3633            if ($in_scope) {            if ($in_scope) {
3634                !!!cp ('t54');
3635              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3636              last INSCOPE;              last INSCOPE;
3637            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3638              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3639                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3640                                token => $end_tag_token);
3641              ## Ignore the token              ## Ignore the token
3642              !!!next-token;              !!!next-token;
3643              return;              return;
3644            }            }
3645          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3646                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3647            $in_scope = 0;            $in_scope = 0;
3648          }          }
3649        } # INSCOPE        } # INSCOPE
3650        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3651          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3652            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3653                            token => $end_tag_token);
3654          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3655          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3656          return;          return;
3657        }        }
3658        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3659          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3660            !!!parse-error (type => 'not closed',
3661                            value => $self->{open_elements}->[-1]->[0]
3662                                ->manakai_local_name,
3663                            token => $end_tag_token);
3664        }        }
3665                
3666        ## Step 2        ## Step 2
# Line 2052  sub _tree_construction_main ($) { Line 3668  sub _tree_construction_main ($) {
3668        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3669        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3670          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3671          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3672              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3673              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3674               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3675              !!!cp ('t59');
3676            $furthest_block = $node;            $furthest_block = $node;
3677            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3678          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3679              !!!cp ('t60');
3680            last OE;            last OE;
3681          }          }
3682        } # OE        } # OE
3683                
3684        ## Step 3        ## Step 3
3685        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3686            !!!cp ('t61');
3687          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3688          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3689          !!!next-token;          !!!next-token;
# Line 2077  sub _tree_construction_main ($) { Line 3696  sub _tree_construction_main ($) {
3696        ## Step 5        ## Step 5
3697        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3698        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3699            !!!cp ('t62');
3700          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3701        }        }
3702                
# Line 2099  sub _tree_construction_main ($) { Line 3719  sub _tree_construction_main ($) {
3719          S7S2: {          S7S2: {
3720            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3721              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3722                  !!!cp ('t63');
3723                $node_i_in_active = $_;                $node_i_in_active = $_;
3724                last S7S2;                last S7S2;
3725              }              }
# Line 2112  sub _tree_construction_main ($) { Line 3733  sub _tree_construction_main ($) {
3733                    
3734          ## Step 4          ## Step 4
3735          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3736              !!!cp ('t64');
3737            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3738          }          }
3739                    
3740          ## Step 5          ## Step 5
3741          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3742              !!!cp ('t65');
3743            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3744            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3745            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2134  sub _tree_construction_main ($) { Line 3757  sub _tree_construction_main ($) {
3757        } # S7          } # S7  
3758                
3759        ## Step 8        ## Step 8
3760        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3761            my $foster_parent_element;
3762            my $next_sibling;
3763            OE: for (reverse 0..$#{$self->{open_elements}}) {
3764              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3765                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3766                                 if (defined $parent and $parent->node_type == 1) {
3767                                   !!!cp ('t65.1');
3768                                   $foster_parent_element = $parent;
3769                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3770                                 } else {
3771                                   !!!cp ('t65.2');
3772                                   $foster_parent_element
3773                                     = $self->{open_elements}->[$_ - 1]->[0];
3774                                 }
3775                                 last OE;
3776                               }
3777                             } # OE
3778                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3779                               unless defined $foster_parent_element;
3780            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3781            $open_tables->[-1]->[1] = 1; # tainted
3782          } else {
3783            !!!cp ('t65.3');
3784            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3785          }
3786                
3787        ## Step 9        ## Step 9
3788        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2151  sub _tree_construction_main ($) { Line 3799  sub _tree_construction_main ($) {
3799        my $i;        my $i;
3800        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3801          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3802              !!!cp ('t66');
3803            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3804            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3805          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3806              !!!cp ('t67');
3807            $i = $_;            $i = $_;
3808          }          }
3809        } # AFE        } # AFE
# Line 2163  sub _tree_construction_main ($) { Line 3813  sub _tree_construction_main ($) {
3813        undef $i;        undef $i;
3814        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3815          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3816              !!!cp ('t68');
3817            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3818            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3819          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3820              !!!cp ('t69');
3821            $i = $_;            $i = $_;
3822          }          }
3823        } # OE        } # OE
# Line 2176  sub _tree_construction_main ($) { Line 3828  sub _tree_construction_main ($) {
3828      } # FET      } # FET
3829    }; # $formatting_end_tag    }; # $formatting_end_tag
3830    
3831    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3832      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3833    }; # $insert_to_current    }; # $insert_to_current
3834    
3835    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3836                         my $child = shift;      my $child = shift;
3837                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3838                              table => 1, tbody => 1, tfoot => 1,        # MUST
3839                              thead => 1, tr => 1,        my $foster_parent_element;
3840                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3841                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3842                           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') {  
3843                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3844                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3845                                   !!!cp ('t70');
3846                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3847                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3848                               } else {                               } else {
3849                                   !!!cp ('t71');
3850                                 $foster_parent_element                                 $foster_parent_element
3851                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3852                               }                               }
# Line 2206  sub _tree_construction_main ($) { Line 3857  sub _tree_construction_main ($) {
3857                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3858                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3859                             ($child, $next_sibling);                             ($child, $next_sibling);
3860                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3861                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3862                         }        !!!cp ('t72');
3863          $self->{open_elements}->[-1]->[0]->append_child ($child);
3864        }
3865    }; # $insert_to_foster    }; # $insert_to_foster
3866    
3867    my $in_body = sub {    B: while (1) {
3868      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
3869      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
3870        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3871          $script_start_tag->();        ## Ignore the token
3872          return;        ## Stay in the phase
3873        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
3874          $style_start_tag->();        next B;
3875          return;      } elsif ($token->{type} == START_TAG_TOKEN and
3876        } elsif ({               $token->{tag_name} eq 'html') {
3877                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3878                 }->{$token->{tag_name}}) {          !!!cp ('t79');
3879          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html:html', token => $token);
3880          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
3881          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3882          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
3883          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html:html', token => $token);
3884            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3885          } else {        } else {
3886            $insert->($el);          !!!cp ('t81');
3887          }        }
3888            
3889          !!!next-token;        !!!cp ('t82');
3890          return;        !!!parse-error (type => 'not first start tag', token => $token);
3891        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
3892          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
3893          ## NOTE: There is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3894          my $title_el;            !!!cp ('t84');
3895          !!!create-element ($title_el, 'title', $token->{attributes});            $top_el->set_attribute_ns
3896          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              (undef, [undef, $attr_name],
3897            ->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') {  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
3898          }          }
3899                    }
3900          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!nack ('t84.1');
3901                    !!!next-token;
3902          next B;
3903        } elsif ($token->{type} == COMMENT_TOKEN) {
3904          my $comment = $self->{document}->create_comment ($token->{data});
3905          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3906            !!!cp ('t85');
3907            $self->{document}->append_child ($comment);
3908          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3909            !!!cp ('t86');
3910            $self->{open_elements}->[0]->[0]->append_child ($comment);
3911          } else {
3912            !!!cp ('t87');
3913            $self->{open_elements}->[-1]->[0]->append_child ($comment);
3914          }
3915          !!!next-token;
3916          next B;
3917        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3918          if ($token->{type} == CHARACTER_TOKEN) {
3919            !!!cp ('t87.1');
3920            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3921          !!!next-token;          !!!next-token;
3922          return;          next B;
3923        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3924          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3925            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3926            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3927              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
3928                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3929              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
3930              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
3931              $formatting_end_tag->($token->{tag_name});            #
3932                        } elsif ({
3933              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3934                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3935                  splice @$active_formatting_elements, $_, 1;                    embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3936                  last AFE2;                    h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3937                }                    li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3938              } # AFE2                    ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3939              OE: for (reverse 0..$#{$self->{open_elements}}) {                    sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3940                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    var => 1,
3941                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
3942                  last OE;            !!!cp ('t87.2');
3943                }            !!!parse-error (type => 'not closed',
3944              } # OE                            value => $self->{open_elements}->[-1]->[0]
3945              last AFE;                                ->manakai_local_name,
3946            } elsif ($node->[0] eq '#marker') {                            token => $token);
3947              last AFE;  
3948              pop @{$self->{open_elements}}
3949                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3950    
3951              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3952              ## Reprocess.
3953              next B;
3954            } else {
3955              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3956              my $tag_name = $token->{tag_name};
3957              if ($nsuri eq $SVG_NS) {
3958                $tag_name = {
3959                   altglyph => 'altGlyph',
3960                   altglyphdef => 'altGlyphDef',
3961                   altglyphitem => 'altGlyphItem',
3962                   animatecolor => 'animateColor',
3963                   animatemotion => 'animateMotion',
3964                   animatetransform => 'animateTransform',
3965                   clippath => 'clipPath',
3966                   feblend => 'feBlend',
3967                   fecolormatrix => 'feColorMatrix',
3968                   fecomponenttransfer => 'feComponentTransfer',
3969                   fecomposite => 'feComposite',
3970                   feconvolvematrix => 'feConvolveMatrix',
3971                   fediffuselighting => 'feDiffuseLighting',
3972                   fedisplacementmap => 'feDisplacementMap',
3973                   fedistantlight => 'feDistantLight',
3974                   feflood => 'feFlood',
3975                   fefunca => 'feFuncA',
3976                   fefuncb => 'feFuncB',
3977                   fefuncg => 'feFuncG',
3978                   fefuncr => 'feFuncR',
3979                   fegaussianblur => 'feGaussianBlur',
3980                   feimage => 'feImage',
3981                   femerge => 'feMerge',
3982                   femergenode => 'feMergeNode',
3983                   femorphology => 'feMorphology',
3984                   feoffset => 'feOffset',
3985                   fepointlight => 'fePointLight',
3986                   fespecularlighting => 'feSpecularLighting',
3987                   fespotlight => 'feSpotLight',
3988                   fetile => 'feTile',
3989                   feturbulence => 'feTurbulence',
3990                   foreignobject => 'foreignObject',
3991                   glyphref => 'glyphRef',
3992                   lineargradient => 'linearGradient',
3993                   radialgradient => 'radialGradient',
3994                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
3995                   textpath => 'textPath',  
3996                }->{$tag_name} || $tag_name;
3997            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
3998    
3999          !!!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];  
4000    
4001          !!!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', ''];  
4002    
4003          !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4004          return;  
4005        } elsif ($token->{tag_name} eq 'marquee' or            if ($self->{self_closing}) {
4006                 $token->{tag_name} eq 'object') {              pop @{$self->{open_elements}};
4007          $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 = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
4008            } else {            } else {
4009              !!!parse-error (type => 'in RCDATA:#'.$token->{type});              !!!cp ('t87.4');
4010            }            }
4011            ## ISSUE: And ignore?  
4012              !!!next-token;
4013              next B;
4014          }          }
4015          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4016          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4017        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
4018          $reconstruct_active_formatting_elements->($insert_to_current);          #
4019                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4020          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4021                    !!!cp ('t87.6');
4022          $self->{insertion_mode} = 'in select';          #
4023          !!!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.  
4024        } else {        } else {
4025          $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;  
4026        }        }
4027      } elsif ($token->{type} eq 'end tag') {      }
4028        if ($token->{tag_name} eq 'body') {  
4029          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      if ($self->{insertion_mode} & HEAD_IMS) {
4030            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
4031            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4032              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4033            }              !!!cp ('t88.2');
4034            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4035            !!!next-token;            } else {
4036            return;              !!!cp ('t88.1');
4037          } else {              ## Ignore the token.
4038            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
4039            ## 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;  
4040            }            }
4041          } # INSCOPE            unless (length $token->{data}) {
4042                        !!!cp ('t88');
4043          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
4044            !!!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;  
4045            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4046          }          }
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
4047    
4048          ## Step 2          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4049          S2: {            !!!cp ('t89');
4050            if ($node->[1] eq $token->{tag_name}) {            ## As if <head>
4051              ## Step 1            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4052              ## generate implied end tags            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4053              if ({            push @{$self->{open_elements}},
4054                   dd => 1, dt => 1, li => 1, p => 1,                [$self->{head_element}, $el_category->{head}];
4055                   td => 1, th => 1, tr => 1,  
4056                  }->{$self->{open_elements}->[-1]->[1]}) {            ## Reprocess in the "in head" insertion mode...
4057                !!!back-token;            pop @{$self->{open_elements}};
4058                $token = {type => 'end tag',  
4059                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST            ## Reprocess in the "after head" insertion mode...
4060                return;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4061              }            !!!cp ('t90');
4062                      ## As if </noscript>
4063              ## Step 2            pop @{$self->{open_elements}};
4064              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;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
4065                        
4066            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4067            redo S2;            ## As if </head>
4068          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
4069    
4070    B: {            ## Reprocess in the "after head" insertion mode...
4071      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4072        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
4073          !!!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]);  
         }  
4074    
4075          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4076          last B;          } else {
4077              !!!cp ('t92');
4078            }
4079    
4080          ## ISSUE: There is an issue in the spec.          ## "after head" insertion mode
4081        } else {          ## As if <body>
4082          if ($self->{insertion_mode} eq 'before head') {          !!!insert-element ('body',, $token);
4083            if ($token->{type} eq 'character') {          $self->{insertion_mode} = IN_BODY_IM;
4084              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## reprocess
4085                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          next B;
4086                unless (length $token->{data}) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4087                  !!!next-token;          if ($token->{tag_name} eq 'head') {
4088                  redo B;            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4089                }              !!!cp ('t93');
4090              }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4091              ## As if <head>              $self->{open_elements}->[-1]->[0]->append_child
4092              !!!create-element ($self->{head_element}, 'head');                  ($self->{head_element});
4093              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              push @{$self->{open_elements}},
4094              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  [$self->{head_element}, $el_category->{head}];
4095              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = IN_HEAD_IM;
4096              ## 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);  
4097              !!!next-token;              !!!next-token;
4098              redo B;              next B;
4099            } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4100              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;  
               }  
             }  
               
4101              #              #
4102            } elsif ($token->{type} eq 'comment') {            } else {
4103              my $comment = $self->{document}->create_comment ($token->{data});              !!!cp ('t95');
4104              $self->{open_elements}->[-1]->[0]->append_child ($comment);              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4105                ## Ignore the token
4106                !!!nack ('t95.1');
4107              !!!next-token;              !!!next-token;
4108              redo B;              next B;
4109            } elsif ($token->{type} eq 'start tag') {            }
4110              if ($token->{tag_name} eq 'title') {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4111                ## NOTE: There is an "as if in head" code clone            !!!cp ('t96');
4112                my $title_el;            ## As if <head>
4113                !!!create-element ($title_el, 'title', $token->{attributes});            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4114                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4115                  ->append_child ($title_el);            push @{$self->{open_elements}},
4116                $self->{content_model_flag} = 'RCDATA';                [$self->{head_element}, $el_category->{head}];
4117    
4118                my $text = '';            $self->{insertion_mode} = IN_HEAD_IM;
4119                !!!next-token;            ## Reprocess in the "in head" insertion mode...
4120                while ($token->{type} eq 'character') {          } else {
4121                  $text .= $token->{data};            !!!cp ('t97');
4122                  !!!next-token;          }
4123                }  
4124                if (length $text) {              if ($token->{tag_name} eq 'base') {
4125                  $title_el->manakai_append_text ($text);                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4126                }                  !!!cp ('t98');
4127                                  ## As if </noscript>
4128                $self->{content_model_flag} = 'PCDATA';                  pop @{$self->{open_elements}};
4129                    !!!parse-error (type => 'in noscript:base', token => $token);
4130                                
4131                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
4132                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
4133                } else {                } else {
4134                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
4135                }                }
               !!!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);  
4136    
4137                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
4138                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4139              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
4140                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4141                ## Ignore the token                  push @{$self->{open_elements}},
4142                !!!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}};  
4143                } else {                } else {
4144                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
4145                }                }
4146                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4147                !!!next-token;                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4148                redo B;                pop @{$self->{open_elements}} # <head>
4149              } elsif ($token->{tag_name} eq 'html') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4150                #                !!!nack ('t101.1');
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
4151                !!!next-token;                !!!next-token;
4152                redo B;                next B;
4153              }              } elsif ($token->{tag_name} eq 'link') {
4154            } else {                ## NOTE: There is a "as if in head" code clone.
4155              #                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4156            }                  !!!cp ('t102');
4157                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4158            if ($self->{open_elements}->[-1]->[1] eq 'head') {                  push @{$self->{open_elements}},
4159              ## As if </head>                      [$self->{head_element}, $el_category->{head}];
4160              pop @{$self->{open_elements}};                } else {
4161            }                  !!!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;  
4162                }                }
4163              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4164                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4165              #                pop @{$self->{open_elements}} # <head>
4166            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4167              my $comment = $self->{document}->create_comment ($token->{data});                !!!ack ('t103.1');
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
4168                !!!next-token;                !!!next-token;
4169                redo B;                next B;
4170              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
4171                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
4172                        script => 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4173                       }->{$token->{tag_name}}) {                  !!!cp ('t104');
4174                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4175                $self->{insertion_mode} = 'in head';                  push @{$self->{open_elements}},
4176                ## reprocess                      [$self->{head_element}, $el_category->{head}];
4177                redo B;                } else {
4178              } else {                  !!!cp ('t105');
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
4179                }                }
4180              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4181                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4182    
4183              !!!parse-error (type => 'in table:#character');                unless ($self->{confident}) {
4184                    if ($token->{attributes}->{charset}) {
4185              ## As if in body, but insert into foster parent element                    !!!cp ('t106');
4186              ## ISSUE: Spec says that "whenever a node would be inserted                    ## NOTE: Whether the encoding is supported or not is handled
4187              ## into the current node" while characters might not be                    ## in the {change_encoding} callback.
4188              ## result in a new Text node.                    $self->{change_encoding}
4189              $reconstruct_active_formatting_elements->($insert_to_foster);                        ->($self, $token->{attributes}->{charset}->{value},
4190                                         $token);
4191              if ({                    
4192                   table => 1, tbody => 1, tfoot => 1,                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4193                   thead => 1, tr => 1,                        ->set_user_data (manakai_has_reference =>
4194                  }->{$self->{open_elements}->[-1]->[1]}) {                                             $token->{attributes}->{charset}
4195                # MUST                                                 ->{has_reference});
4196                my $foster_parent_element;                  } elsif ($token->{attributes}->{content}) {
4197                my $next_sibling;                    if ($token->{attributes}->{content}->{value}
4198                my $prev_sibling;                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4199                OE: for (reverse 0..$#{$self->{open_elements}}) {                            [\x09-\x0D\x20]*=
4200                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4201                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4202                    if (defined $parent and $parent->node_type == 1) {                      !!!cp ('t107');
4203                      $foster_parent_element = $parent;                      ## NOTE: Whether the encoding is supported or not is handled
4204                      $next_sibling = $self->{open_elements}->[$_]->[0];                      ## in the {change_encoding} callback.
4205                      $prev_sibling = $next_sibling->previous_sibling;                      $self->{change_encoding}
4206                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4207                               $token);
4208                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4209                            ->set_user_data (manakai_has_reference =>
4210                                                 $token->{attributes}->{content}
4211                                                       ->{has_reference});
4212                    } else {                    } else {
4213                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
4214                    }                    }
                   last OE;  
4215                  }                  }
               } # 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});  
4216                } else {                } else {
4217                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
4218                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
4219                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4220                }                        ->set_user_data (manakai_has_reference =>
4221              } else {                                             $token->{attributes}->{charset}
4222                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
4223              }                  }
4224                                if ($token->{attributes}->{content}) {
4225              !!!next-token;                    !!!cp ('t110');
4226              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4227            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
4228              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
4229              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
4230              !!!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}};  
4231                }                }
4232    
4233                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
4234                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4235                  !!!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}};  
4236                !!!next-token;                !!!next-token;
4237                redo B;                next B;
4238              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
4239                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4240                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
4241                       }->{$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]);  
4242                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4243                    !!!parse-error (type => 'in noscript:title', token => $token);
4244                  
4245                    $self->{insertion_mode} = IN_HEAD_IM;
4246                    ## Reprocess in the "in head" insertion mode...
4247                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4248                    !!!cp ('t112');
4249                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4250                    push @{$self->{open_elements}},
4251                        [$self->{head_element}, $el_category->{head}];
4252                  } else {
4253                    !!!cp ('t113');
4254                }                }
4255    
4256                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
4257                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
4258                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
4259                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4260                redo B;                pop @{$self->{open_elements}} # <head>
4261              } elsif ($token->{tag_name} eq 'table') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4262                ## NOTE: There are code clones for this "table in table"                next B;
4263                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } elsif ($token->{tag_name} eq 'style') {
4264                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4265                ## As if </table>                ## insertion mode IN_HEAD_IM)
4266                ## have a table element in table scope                ## NOTE: There is a "as if in head" code clone.
4267                my $i;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4268                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!cp ('t114');
4269                  my $node = $self->{open_elements}->[$_];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4270                  if ($node->[1] eq 'table') {                  push @{$self->{open_elements}},
4271                    $i = $_;                      [$self->{head_element}, $el_category->{head}];
4272                    last INSCOPE;                } else {
4273                  } elsif ({                  !!!cp ('t115');
4274                            table => 1, html => 1,                }
4275                           }->{$node->[1]}) {                $parse_rcdata->(CDATA_CONTENT_MODEL);
4276                    last INSCOPE;                pop @{$self->{open_elements}} # <head>
4277                  }                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4278                } # INSCOPE                next B;
4279                unless (defined $i) {              } elsif ($token->{tag_name} eq 'noscript') {
4280                  !!!parse-error (type => 'unmatched end tag:table');                if ($self->{insertion_mode} == IN_HEAD_IM) {
4281                  ## Ignore tokens </table><table>                  !!!cp ('t116');
4282                    ## NOTE: and scripting is disalbed
4283                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4284                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4285                    !!!nack ('t116.1');
4286                    !!!next-token;
4287                    next B;
4288                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4289                    !!!cp ('t117');
4290                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4291                    ## Ignore the token
4292                    !!!nack ('t117.1');
4293                  !!!next-token;                  !!!next-token;
4294                  redo B;                  next B;
4295                  } else {
4296                    !!!cp ('t118');
4297                    #
4298                }                }
4299                } elsif ($token->{tag_name} eq 'script') {
4300                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4301                    !!!cp ('t119');
4302                    ## As if </noscript>
4303                    pop @{$self->{open_elements}};
4304                    !!!parse-error (type => 'in noscript:script', token => $token);
4305                                
4306                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
4307                if ({                  ## Reprocess in the "in head" insertion mode...
4308                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4309                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
4310                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4311                  !!!back-token; # <table>                  push @{$self->{open_elements}},
4312                  $token = {type => 'end tag', tag_name => 'table'};                      [$self->{head_element}, $el_category->{head}];
4313                  !!!back-token;                } else {
4314                  $token = {type => 'end tag',                  !!!cp ('t121');
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4315                }                }
4316    
4317                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
4318                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
4319                  pop @{$self->{open_elements}} # <head>
4320                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4321                  next B;
4322                } elsif ($token->{tag_name} eq 'body' or
4323                         $token->{tag_name} eq 'frameset') {
4324                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4325                    !!!cp ('t122');
4326                    ## As if </noscript>
4327                    pop @{$self->{open_elements}};
4328                    !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4329                    
4330                    ## Reprocess in the "in head" insertion mode...
4331                    ## As if </head>
4332                    pop @{$self->{open_elements}};
4333                    
4334                    ## Reprocess in the "after head" insertion mode...
4335                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4336                    !!!cp ('t124');
4337                    pop @{$self->{open_elements}};
4338                    
4339                    ## Reprocess in the "after head" insertion mode...
4340                  } else {
4341                    !!!cp ('t125');
4342                }                }
4343    
4344                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
4345                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4346                  if ($token->{tag_name} eq 'body') {
4347                    !!!cp ('t126');
4348                    $self->{insertion_mode} = IN_BODY_IM;
4349                  } elsif ($token->{tag_name} eq 'frameset') {
4350                    !!!cp ('t127');
4351                    $self->{insertion_mode} = IN_FRAMESET_IM;
4352                  } else {
4353                    die "$0: tag name: $self->{tag_name}";
4354                  }
4355                  !!!nack ('t127.1');
4356                  !!!next-token;
4357                  next B;
4358                } else {
4359                  !!!cp ('t128');
4360                  #
4361                }
4362    
4363                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4364                  !!!cp ('t129');
4365                  ## As if </noscript>
4366                  pop @{$self->{open_elements}};
4367                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4368                  
4369                  ## Reprocess in the "in head" insertion mode...
4370                  ## As if </head>
4371                  pop @{$self->{open_elements}};
4372    
4373                ## reprocess                ## Reprocess in the "after head" insertion mode...
4374                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4375                  !!!cp ('t130');
4376                  ## As if </head>
4377                  pop @{$self->{open_elements}};
4378    
4379                  ## Reprocess in the "after head" insertion mode...
4380              } else {              } else {
4381                #                !!!cp ('t131');
4382              }              }
4383            } elsif ($token->{type} eq 'end tag') {  
4384              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
4385                ## have a table element in table scope              ## As if <body>
4386                my $i;              !!!insert-element ('body',, $token);
4387                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
4388                  my $node = $self->{open_elements}->[$_];              ## reprocess
4389                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
4390                    $i = $_;              next B;
4391                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
4392                  } elsif ({              if ($token->{tag_name} eq 'head') {
4393                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4394                           }->{$node->[1]}) {                  !!!cp ('t132');
4395                    last INSCOPE;                  ## As if <head>
4396                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4397                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4398                unless (defined $i) {                  push @{$self->{open_elements}},
4399                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
4400    
4401                    ## Reprocess in the "in head" insertion mode...
4402                    pop @{$self->{open_elements}};
4403                    $self->{insertion_mode} = AFTER_HEAD_IM;
4404                    !!!next-token;
4405                    next B;
4406                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4407                    !!!cp ('t133');
4408                    ## As if </noscript>
4409                    pop @{$self->{open_elements}};
4410                    !!!parse-error (type => 'in noscript:/head', token => $token);
4411                    
4412                    ## Reprocess in the "in head" insertion mode...
4413                    pop @{$self->{open_elements}};
4414                    $self->{insertion_mode} = AFTER_HEAD_IM;
4415                    !!!next-token;
4416                    next B;
4417                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4418                    !!!cp ('t134');
4419                    pop @{$self->{open_elements}};
4420                    $self->{insertion_mode} = AFTER_HEAD_IM;
4421                    !!!next-token;
4422                    next B;
4423                  } else {
4424                    !!!cp ('t135');
4425                    #
4426                  }
4427                } elsif ($token->{tag_name} eq 'noscript') {
4428                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4429                    !!!cp ('t136');
4430                    pop @{$self->{open_elements}};
4431                    $self->{insertion_mode} = IN_HEAD_IM;
4432                    !!!next-token;
4433                    next B;
4434                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4435                    !!!cp ('t137');
4436                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4437                    ## Ignore the token ## ISSUE: An issue in the spec.
4438                    !!!next-token;
4439                    next B;
4440                  } else {
4441                    !!!cp ('t138');
4442                    #
4443                  }
4444                } elsif ({
4445                          body => 1, html => 1,
4446                         }->{$token->{tag_name}}) {
4447                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4448                    !!!cp ('t139');
4449                    ## As if <head>
4450                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4451                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4452                    push @{$self->{open_elements}},
4453                        [$self->{head_element}, $el_category->{head}];
4454    
4455                    $self->{insertion_mode} = IN_HEAD_IM;
4456                    ## Reprocess in the "in head" insertion mode...
4457                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4458                    !!!cp ('t140');
4459                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4460                  ## Ignore the token                  ## Ignore the token
4461                  !!!next-token;                  !!!next-token;
4462                  redo B;                  next B;
4463                  } else {
4464                    !!!cp ('t141');
4465                }                }
4466                                
4467                ## generate implied end tags                #
4468                if ({              } elsif ({
4469                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
4470                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
4471                    }->{$self->{open_elements}->[-1]->[1]}) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4472                  !!!back-token;                  !!!cp ('t142');
4473                  $token = {type => 'end tag',                  ## As if <head>
4474                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4475                  redo B;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4476                    push @{$self->{open_elements}},
4477                        [$self->{head_element}, $el_category->{head}];
4478    
4479                    $self->{insertion_mode} = IN_HEAD_IM;
4480                    ## Reprocess in the "in head" insertion mode...
4481                  } else {
4482                    !!!cp ('t143');
4483                }                }
4484    
4485                if ($self->{open_elements}->[-1]->[1] ne 'table') {                #
4486                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } else {
4487                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4488                    !!!cp ('t144');
4489                    #
4490                  } else {
4491                    !!!cp ('t145');
4492                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4493                    ## Ignore the token
4494                    !!!next-token;
4495                    next B;
4496                }                }
4497                }
4498    
4499                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4500                  !!!cp ('t146');
4501                  ## As if </noscript>
4502                  pop @{$self->{open_elements}};
4503                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4504                  
4505                  ## Reprocess in the "in head" insertion mode...
4506                  ## As if </head>
4507                  pop @{$self->{open_elements}};
4508    
4509                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
4510                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4511                  !!!cp ('t147');
4512                  ## As if </head>
4513                  pop @{$self->{open_elements}};
4514    
4515                  ## Reprocess in the "after head" insertion mode...
4516                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4517    ## ISSUE: This case cannot be reached?
4518                  !!!cp ('t148');
4519                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4520                  ## Ignore the token ## ISSUE: An issue in the spec.
4521                !!!next-token;                !!!next-token;
4522                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;  
4523              } else {              } else {
4524                #                !!!cp ('t149');
4525              }              }
           } else {  
             #  
           }  
4526    
4527            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
4528            $in_body->($insert_to_foster);              ## As if <body>
4529            redo B;              !!!insert-element ('body',, $token);
4530          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
4531            if ($token->{type} eq 'character') {              ## reprocess
4532              ## NOTE: This is a code clone of "character in body".              next B;
4533          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4534            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4535              !!!cp ('t149.1');
4536    
4537              ## NOTE: As if <head>
4538              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4539              $self->{open_elements}->[-1]->[0]->append_child
4540                  ($self->{head_element});
4541              #push @{$self->{open_elements}},
4542              #    [$self->{head_element}, $el_category->{head}];
4543              #$self->{insertion_mode} = IN_HEAD_IM;
4544              ## NOTE: Reprocess.
4545    
4546              ## NOTE: As if </head>
4547              #pop @{$self->{open_elements}};
4548              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4549              ## NOTE: Reprocess.
4550              
4551              #
4552            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4553              !!!cp ('t149.2');
4554    
4555              ## NOTE: As if </head>
4556              pop @{$self->{open_elements}};
4557              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4558              ## NOTE: Reprocess.
4559    
4560              #
4561            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4562              !!!cp ('t149.3');
4563    
4564              !!!parse-error (type => 'in noscript:#eof', token => $token);
4565    
4566              ## As if </noscript>
4567              pop @{$self->{open_elements}};
4568              #$self->{insertion_mode} = IN_HEAD_IM;
4569              ## NOTE: Reprocess.
4570    
4571              ## NOTE: As if </head>
4572              pop @{$self->{open_elements}};
4573              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4574              ## NOTE: Reprocess.
4575    
4576              #
4577            } else {
4578              !!!cp ('t149.4');
4579              #
4580            }
4581    
4582            ## NOTE: As if <body>
4583            !!!insert-element ('body',, $token);
4584            $self->{insertion_mode} = IN_BODY_IM;
4585            ## NOTE: Reprocess.
4586            next B;
4587          } else {
4588            die "$0: $token->{type}: Unknown token type";
4589          }
4590    
4591              ## ISSUE: An issue in the spec.
4592        } elsif ($self->{insertion_mode} & BODY_IMS) {
4593              if ($token->{type} == CHARACTER_TOKEN) {
4594                !!!cp ('t150');
4595                ## NOTE: There is a code clone of "character in body".
4596              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4597                            
4598              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4599    
4600              !!!next-token;              !!!next-token;
4601              redo B;              next B;
4602            } 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') {  
4603              if ({              if ({
4604                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4605                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4606                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4607                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
4608                    ## have an element in table scope
4609                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
4610                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
4611                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
4612                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
4613                  my $node = $self->{open_elements}->[$_];  
4614                  if ($node->[1] eq 'caption') {                      ## Close the cell
4615                    $i = $_;                      !!!back-token; # <x>
4616                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
4617                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
4618                            table => 1, html => 1,                                line => $token->{line},
4619                           }->{$node->[1]}) {                                column => $token->{column}};
4620                    last INSCOPE;                      next B;
4621                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4622                        !!!cp ('t152');
4623                        ## ISSUE: This case can never be reached, maybe.
4624                        last;
4625                      }
4626                  }                  }
4627                } # INSCOPE  
4628                unless (defined $i) {                  !!!cp ('t153');
4629                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
4630                        value => $token->{tag_name}, token => $token);
4631                  ## Ignore the token                  ## Ignore the token
4632                    !!!nack ('t153.1');
4633                  !!!next-token;                  !!!next-token;
4634                  redo B;                  next B;
4635                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4636                                  !!!parse-error (type => 'not closed:caption', token => $token);
4637                ## generate implied end tags                  
4638                if ({                  ## NOTE: As if </caption>.
4639                     dd => 1, dt => 1, li => 1, p => 1,                  ## have a table element in table scope
4640                     td => 1, th => 1, tr => 1,                  my $i;
4641                    }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: {
4642                  !!!back-token; # <?>                    for (reverse 0..$#{$self->{open_elements}}) {
4643                  $token = {type => 'end tag', tag_name => 'caption'};                      my $node = $self->{open_elements}->[$_];
4644                  !!!back-token;                      if ($node->[1] & CAPTION_EL) {
4645                  $token = {type => 'end tag',                        !!!cp ('t155');
4646                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        $i = $_;
4647                  redo B;                        last INSCOPE;
4648                }                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4649                          !!!cp ('t156');
4650                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        last;
4651                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      }
4652                }                    }
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
4653    
4654                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
4655                      !!!parse-error (type => 'start tag not allowed',
4656                                      value => $token->{tag_name}, token => $token);
4657                      ## Ignore the token
4658                      !!!nack ('t157.1');
4659                      !!!next-token;
4660                      next B;
4661                    } # INSCOPE
4662                    
4663                    ## generate implied end tags
4664                    while ($self->{open_elements}->[-1]->[1]
4665                               & END_TAG_OPTIONAL_EL) {
4666                      !!!cp ('t158');
4667                      pop @{$self->{open_elements}};
4668                    }
4669    
4670                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4671                redo B;                    !!!cp ('t159');
4672                      !!!parse-error (type => 'not closed',
4673                                      value => $self->{open_elements}->[-1]->[0]
4674                                          ->manakai_local_name,
4675                                      token => $token);
4676                    } else {
4677                      !!!cp ('t160');
4678                    }
4679                    
4680                    splice @{$self->{open_elements}}, $i;
4681                    
4682                    $clear_up_to_marker->();
4683                    
4684                    $self->{insertion_mode} = IN_TABLE_IM;
4685                    
4686                    ## reprocess
4687                    !!!ack-later;
4688                    next B;
4689                  } else {
4690                    !!!cp ('t161');
4691                    #
4692                  }
4693              } else {              } else {
4694                  !!!cp ('t162');
4695                #                #
4696              }              }
4697            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4698              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4699                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
4700                my $i;                  ## have an element in table scope
4701                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
4702                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4703                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4704                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4705                    last INSCOPE;                      !!!cp ('t163');
4706                  } elsif ({                      $i = $_;
4707                            table => 1, html => 1,                      last INSCOPE;
4708                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4709                    last INSCOPE;                      !!!cp ('t164');
4710                        last INSCOPE;
4711                      }
4712                    } # INSCOPE
4713                      unless (defined $i) {
4714                        !!!cp ('t165');
4715                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4716                        ## Ignore the token
4717                        !!!next-token;
4718                        next B;
4719                      }
4720                    
4721                    ## generate implied end tags
4722                    while ($self->{open_elements}->[-1]->[1]
4723                               & END_TAG_OPTIONAL_EL) {
4724                      !!!cp ('t166');
4725                      pop @{$self->{open_elements}};
4726                  }                  }
4727                } # INSCOPE  
4728                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4729                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
4730                      !!!cp ('t167');
4731                      !!!parse-error (type => 'not closed',
4732                                      value => $self->{open_elements}->[-1]->[0]
4733                                          ->manakai_local_name,
4734                                      token => $token);
4735                    } else {
4736                      !!!cp ('t168');
4737                    }
4738                    
4739                    splice @{$self->{open_elements}}, $i;
4740                    
4741                    $clear_up_to_marker->();
4742                    
4743                    $self->{insertion_mode} = IN_ROW_IM;
4744                    
4745                    !!!next-token;
4746                    next B;
4747                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4748                    !!!cp ('t169');
4749                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4750                  ## Ignore the token                  ## Ignore the token
4751                  !!!next-token;                  !!!next-token;
4752                  redo B;                  next B;
4753                }                } else {
4754                                  !!!cp ('t170');
4755                ## 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;  
4756                }                }
4757                } elsif ($token->{tag_name} eq 'caption') {
4758                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
4759                    ## have a table element in table scope
4760                    my $i;
4761                    INSCOPE: {
4762                      for (reverse 0..$#{$self->{open_elements}}) {
4763                        my $node = $self->{open_elements}->[$_];
4764                        if ($node->[1] & CAPTION_EL) {
4765                          !!!cp ('t171');
4766                          $i = $_;
4767                          last INSCOPE;
4768                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
4769                          !!!cp ('t172');
4770                          last;
4771                        }
4772                      }
4773    
4774                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
4775                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
4776                                      value => $token->{tag_name}, token => $token);
4777                      ## Ignore the token
4778                      !!!next-token;
4779                      next B;
4780                    } # INSCOPE
4781                    
4782                    ## generate implied end tags
4783                    while ($self->{open_elements}->[-1]->[1]
4784                               & END_TAG_OPTIONAL_EL) {
4785                      !!!cp ('t174');
4786                      pop @{$self->{open_elements}};
4787                    }
4788                    
4789                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4790                      !!!cp ('t175');
4791                      !!!parse-error (type => 'not closed',
4792                                      value => $self->{open_elements}->[-1]->[0]
4793                                          ->manakai_local_name,
4794                                      token => $token);
4795                    } else {
4796                      !!!cp ('t176');
4797                    }
4798                    
4799                    splice @{$self->{open_elements}}, $i;
4800                    
4801                    $clear_up_to_marker->();
4802                    
4803                    $self->{insertion_mode} = IN_TABLE_IM;
4804                    
4805                    !!!next-token;
4806                    next B;
4807                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4808                    !!!cp ('t177');
4809                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4810                    ## Ignore the token
4811                    !!!next-token;
4812                    next B;
4813                  } else {
4814                    !!!cp ('t178');
4815                    #
4816                }                }
4817                } elsif ({
4818                          table => 1, tbody => 1, tfoot => 1,
4819                          thead => 1, tr => 1,
4820                         }->{$token->{tag_name}} and
4821                         $self->{insertion_mode} == IN_CELL_IM) {
4822                  ## have an element in table scope
4823                  my $i;
4824                  my $tn;
4825                  INSCOPE: {
4826                    for (reverse 0..$#{$self->{open_elements}}) {
4827                      my $node = $self->{open_elements}->[$_];
4828                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4829                        !!!cp ('t179');
4830                        $i = $_;
4831    
4832                        ## Close the cell
4833                        !!!back-token; # </x>
4834                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
4835                                  line => $token->{line},
4836                                  column => $token->{column}};
4837                        next B;
4838                      } elsif ($node->[1] & TABLE_CELL_EL) {
4839                        !!!cp ('t180');
4840                        $tn = $node->[0]->manakai_local_name;
4841                        ## NOTE: There is exactly one |td| or |th| element
4842                        ## in scope in the stack of open elements by definition.
4843                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4844                        ## ISSUE: Can this be reached?
4845                        !!!cp ('t181');
4846                        last;
4847                      }
4848                    }
4849    
4850                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
4851                    !!!parse-error (type => 'unmatched end tag',
4852                $clear_up_to_marker->();                      value => $token->{tag_name}, token => $token);
4853                    ## Ignore the token
4854                $self->{insertion_mode} = 'in table';                  !!!next-token;
4855                    next B;
4856                !!!next-token;                } # INSCOPE
4857                redo B;              } elsif ($token->{tag_name} eq 'table' and
4858              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4859                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4860    
4861                ## As if </caption>                ## As if </caption>
4862                ## have a table element in table scope                ## have a table element in table scope
4863                my $i;                my $i;
4864                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4865                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4866                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4867                      !!!cp ('t184');
4868                    $i = $_;                    $i = $_;
4869                    last INSCOPE;                    last INSCOPE;
4870                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4871                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4872                    last INSCOPE;                    last INSCOPE;
4873                  }                  }
4874                } # INSCOPE                } # INSCOPE
4875                unless (defined $i) {                unless (defined $i) {
4876                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4877                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4878                  ## Ignore the token                  ## Ignore the token
4879                  !!!next-token;                  !!!next-token;
4880                  redo B;                  next B;
4881                }                }
4882                                
4883                ## generate implied end tags                ## generate implied end tags
4884                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4885                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4886                     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;  
4887                }                }
4888    
4889                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4890                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4891                    !!!parse-error (type => 'not closed',
4892                                    value => $self->{open_elements}->[-1]->[0]
4893                                        ->manakai_local_name,
4894                                    token => $token);
4895                  } else {
4896                    !!!cp ('t189');
4897                }                }
4898    
4899                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4900    
4901                $clear_up_to_marker->();                $clear_up_to_marker->();
4902    
4903                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4904    
4905                ## reprocess                ## reprocess
4906                redo B;                next B;
4907              } elsif ({              } elsif ({
4908                        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,  
4909                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4910                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4911                ## Ignore the token                  !!!cp ('t190');
4912                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');  
4913                  ## Ignore the token                  ## Ignore the token
4914                  !!!next-token;                  !!!next-token;
4915                  redo B;                  next B;
4916                } else {                } else {
4917                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
4918                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
4919                }                }
4920              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
4921                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
4922                          thead => 1, tr => 1,
4923                         }->{$token->{tag_name}} and
4924                         $self->{insertion_mode} == IN_CAPTION_IM) {
4925                  !!!cp ('t192');
4926                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4927                ## Ignore the token                ## Ignore the token
4928                !!!next-token;                !!!next-token;
4929                redo B;                next B;
4930              } else {              } else {
4931                #                !!!cp ('t193');
4932                  #
4933              }              }
4934            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4935              #          for my $entry (@{$self->{open_elements}}) {
4936              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4937                !!!cp ('t75');
4938                !!!parse-error (type => 'in body:#eof', token => $token);
4939                last;
4940            }            }
4941            }
4942    
4943            ## As if </colgroup>          ## Stop parsing.
4944            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
4945              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
4946              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
4947          }
4948    
4949          $insert = $insert_to_current;
4950          #
4951        } elsif ($self->{insertion_mode} & TABLE_IMS) {
4952          if ($token->{type} == CHARACTER_TOKEN) {
4953            if (not $open_tables->[-1]->[1] and # tainted
4954                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4955              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4956                  
4957              unless (length $token->{data}) {
4958                !!!cp ('t194');
4959              !!!next-token;              !!!next-token;
4960              redo B;              next B;
4961            } else {            } else {
4962              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
4963            }            }
4964          } 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;  
               }  
             }  
4965    
4966              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4967    
4968              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4969              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
4970              ## into the current node" while characters might not be              ## into the current node" while characters might not be
4971              ## result in a new Text node.              ## result in a new Text node.
4972              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4973                
4974              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]}) {  
4975                # MUST                # MUST
4976                my $foster_parent_element;                my $foster_parent_element;
4977                my $next_sibling;                my $next_sibling;
4978                my $prev_sibling;                my $prev_sibling;
4979                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4980                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4981                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4982                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4983                        !!!cp ('t196');
4984                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4985                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4986                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4987                    } else {                    } else {
4988                        !!!cp ('t197');
4989                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4990                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4991                    }                    }
# Line 3727  sub _tree_construction_main ($) { Line 4997  sub _tree_construction_main ($) {
4997                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4998                if (defined $prev_sibling and                if (defined $prev_sibling and
4999                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5000                    !!!cp ('t198');
5001                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5002                } else {                } else {
5003                    !!!cp ('t199');
5004                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5005                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5006                     $next_sibling);                     $next_sibling);
5007                }                }
5008              } else {            $open_tables->[-1]->[1] = 1; # tainted
5009                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5010              }            !!!cp ('t200');
5011              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5012            }
5013                            
5014              !!!next-token;          !!!next-token;
5015              redo B;          next B;
5016            } 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') {  
5017              if ({              if ({
5018                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
5019                   th => 1, td => 1,                   th => 1, td => 1,
5020                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5021                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5022                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
5023                    while (not ($self->{open_elements}->[-1]->[1]
5024                                    & TABLE_SCOPING_EL)) {
5025                      !!!cp ('t201');
5026                      pop @{$self->{open_elements}};
5027                    }
5028                    
5029                    !!!insert-element ('tbody',, $token);
5030                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5031                    ## reprocess in the "in table body" insertion mode...
5032                }                }
5033    
5034                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5035                while (not {                  unless ($token->{tag_name} eq 'tr') {
5036                  tbody => 1, tfoot => 1, thead => 1, html => 1,                    !!!cp ('t202');
5037                }->{$self->{open_elements}->[-1]->[1]}) {                    !!!parse-error (type => 'missing start tag:tr', token => $token);
5038                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  }
5039                    
5040                    ## Clear back to table body context
5041                    while (not ($self->{open_elements}->[-1]->[1]
5042                                    & TABLE_ROWS_SCOPING_EL)) {
5043                      !!!cp ('t203');
5044                      ## ISSUE: Can this case be reached?
5045                      pop @{$self->{open_elements}};
5046                    }
5047                    
5048                    $self->{insertion_mode} = IN_ROW_IM;
5049                    if ($token->{tag_name} eq 'tr') {
5050                      !!!cp ('t204');
5051                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5052                      !!!nack ('t204');
5053                      !!!next-token;
5054                      next B;
5055                    } else {
5056                      !!!cp ('t205');
5057                      !!!insert-element ('tr',, $token);
5058                      ## reprocess in the "in row" insertion mode
5059                    }
5060                  } else {
5061                    !!!cp ('t206');
5062                  }
5063    
5064                  ## Clear back to table row context
5065                  while (not ($self->{open_elements}->[-1]->[1]
5066                                  & TABLE_ROW_SCOPING_EL)) {
5067                    !!!cp ('t207');
5068                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5069                }                }
5070                                
5071                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5072                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5073                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5074                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5075                } else {                
5076                  !!!insert-element ('tr');                !!!nack ('t207.1');
5077                  ## reprocess                !!!next-token;
5078                }                next B;
               redo B;  
5079              } elsif ({              } elsif ({
5080                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5081                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5082                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5083                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5084                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5085                my $i;                  ## As if </tr>
5086                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5087                  my $node = $self->{open_elements}->[$_];                  my $i;
5088                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5089                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5090                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5091                    $i = $_;                      !!!cp ('t208');
5092                    last INSCOPE;                      $i = $_;
5093                  } elsif ({                      last INSCOPE;
5094                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5095                           }->{$node->[1]}) {                      !!!cp ('t209');
5096                    last INSCOPE;                      last INSCOPE;
5097                      }
5098                    } # INSCOPE
5099                    unless (defined $i) {
5100                      !!!cp ('t210');
5101    ## TODO: This type is wrong.
5102                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5103                      ## Ignore the token
5104                      !!!nack ('t210.1');
5105                      !!!next-token;
5106                      next B;
5107                    }
5108                    
5109                    ## Clear back to table row context
5110                    while (not ($self->{open_elements}->[-1]->[1]
5111                                    & TABLE_ROW_SCOPING_EL)) {
5112                      !!!cp ('t211');
5113                      ## ISSUE: Can this case be reached?
5114                      pop @{$self->{open_elements}};
5115                    }
5116                    
5117                    pop @{$self->{open_elements}}; # tr
5118                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5119                    if ($token->{tag_name} eq 'tr') {
5120                      !!!cp ('t212');
5121                      ## reprocess
5122                      !!!ack-later;
5123                      next B;
5124                    } else {
5125                      !!!cp ('t213');
5126                      ## reprocess in the "in table body" insertion mode...
5127                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5128                }                }
5129    
5130                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5131                while (not {                  ## have an element in table scope
5132                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5133                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5134                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5135                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5136                        !!!cp ('t214');
5137                        $i = $_;
5138                        last INSCOPE;
5139                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5140                        !!!cp ('t215');
5141                        last INSCOPE;
5142                      }
5143                    } # INSCOPE
5144                    unless (defined $i) {
5145                      !!!cp ('t216');
5146    ## TODO: This erorr type ios wrong.
5147                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5148                      ## Ignore the token
5149                      !!!nack ('t216.1');
5150                      !!!next-token;
5151                      next B;
5152                    }
5153    
5154                    ## Clear back to table body context
5155                    while (not ($self->{open_elements}->[-1]->[1]
5156                                    & TABLE_ROWS_SCOPING_EL)) {
5157                      !!!cp ('t217');
5158                      ## ISSUE: Can this state be reached?
5159                      pop @{$self->{open_elements}};
5160                    }
5161                    
5162                    ## As if <{current node}>
5163                    ## have an element in table scope
5164                    ## true by definition
5165                    
5166                    ## Clear back to table body context
5167                    ## nop by definition
5168                    
5169                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5170                    $self->{insertion_mode} = IN_TABLE_IM;
5171                    ## reprocess in "in table" insertion mode...
5172                  } else {
5173                    !!!cp ('t218');
5174                }                }
5175    
5176                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5177                ## have an element in table scope                  ## Clear back to table context
5178                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5179                                    & TABLE_SCOPING_EL)) {
5180                ## Clear back to table body context                    !!!cp ('t219');
5181                ## nop by definition                    ## ISSUE: Can this state be reached?
5182                      pop @{$self->{open_elements}};
5183                pop @{$self->{open_elements}};                  }
5184                $self->{insertion_mode} = 'in table';                  
5185                ## reprocess                  !!!insert-element ('colgroup',, $token);
5186                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5187                    ## reprocess
5188                    !!!ack-later;
5189                    next B;
5190                  } elsif ({
5191                            caption => 1,
5192                            colgroup => 1,
5193                            tbody => 1, tfoot => 1, thead => 1,
5194                           }->{$token->{tag_name}}) {
5195                    ## Clear back to table context
5196                    while (not ($self->{open_elements}->[-1]->[1]
5197                                    & TABLE_SCOPING_EL)) {
5198                      !!!cp ('t220');
5199                      ## ISSUE: Can this state be reached?
5200                      pop @{$self->{open_elements}};
5201                    }
5202                    
5203                    push @$active_formatting_elements, ['#marker', '']
5204                        if $token->{tag_name} eq 'caption';
5205                    
5206                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5207                    $self->{insertion_mode} = {
5208                                               caption => IN_CAPTION_IM,
5209                                               colgroup => IN_COLUMN_GROUP_IM,
5210                                               tbody => IN_TABLE_BODY_IM,
5211                                               tfoot => IN_TABLE_BODY_IM,
5212                                               thead => IN_TABLE_BODY_IM,
5213                                              }->{$token->{tag_name}};
5214                    !!!next-token;
5215                    !!!nack ('t220.1');
5216                    next B;
5217                  } else {
5218                    die "$0: in table: <>: $token->{tag_name}";
5219                  }
5220              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5221                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5222                !!!parse-error (type => 'not closed:table');                                value => $self->{open_elements}->[-1]->[0]
5223                                      ->manakai_local_name,
5224                                  token => $token);
5225    
5226                ## As if </table>                ## As if </table>
5227                ## have a table element in table scope                ## have a table element in table scope
5228                my $i;                my $i;
5229                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5230                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5231                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5232                      !!!cp ('t221');
5233                    $i = $_;                    $i = $_;
5234                    last INSCOPE;                    last INSCOPE;
5235                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5236                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5237                    last INSCOPE;                    last INSCOPE;
5238                  }                  }
5239                } # INSCOPE                } # INSCOPE
5240                unless (defined $i) {                unless (defined $i) {
5241                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5242    ## TODO: The following is wrong, maybe.
5243                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5244                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5245                    !!!nack ('t223.1');
5246                  !!!next-token;                  !!!next-token;
5247                  redo B;                  next B;
5248                }                }
5249                                
5250    ## TODO: Followings are removed from the latest spec.
5251                ## generate implied end tags                ## generate implied end tags
5252                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5253                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5254                     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;  
5255                }                }
5256    
5257                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5258                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5259                    ## NOTE: |<table><tr><table>|
5260                    !!!parse-error (type => 'not closed',
5261                                    value => $self->{open_elements}->[-1]->[0]
5262                                        ->manakai_local_name,
5263                                    token => $token);
5264                  } else {
5265                    !!!cp ('t226');
5266                }                }
5267    
5268                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5269                  pop @{$open_tables};
5270    
5271                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5272    
5273                ## reprocess            ## reprocess
5274                redo B;            !!!ack-later;
5275              } else {            next B;
5276                #          } elsif ($token->{tag_name} eq 'style') {
5277              }            if (not $open_tables->[-1]->[1]) { # tainted
5278            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5279              if ({              ## NOTE: This is a "as if in head" code clone.
5280                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5281                  }->{$token->{tag_name}}) {              next B;
5282                ## have an element in table scope            } else {
5283                my $i;              !!!cp ('t227.7');
5284                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5285                  my $node = $self->{open_elements}->[$_];            }
5286                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5287                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5288                    last INSCOPE;              !!!cp ('t227.6');
5289                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5290                            table => 1, html => 1,              $script_start_tag->();
5291                           }->{$node->[1]}) {              next B;
5292                    last INSCOPE;            } else {
5293                  }              !!!cp ('t227.5');
5294                } # INSCOPE              #
5295                unless (defined $i) {            }
5296                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5297                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5298                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5299                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5300                }                if ($type eq 'hidden') {
5301                    !!!cp ('t227.3');
5302                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5303    
5304                ## 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}};  
               }  
5305    
5306                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;  
               }  
5307    
               ## 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]);  
5308                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
5309    
5310                pop @{$self->{open_elements}};                  !!!next-token;
5311                $self->{insertion_mode} = 'in table';                  !!!ack ('t227.2.1');
5312                ## reprocess                  next B;
5313                redo B;                } else {
5314              } elsif ({                  !!!cp ('t227.2');
5315                        body => 1, caption => 1, col => 1, colgroup => 1,                  #
5316                        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;  
5317              } else {              } else {
5318                  !!!cp ('t227.1');
5319                #                #
5320              }              }
5321            } else {            } else {
5322                !!!cp ('t227.4');
5323              #              #
5324            }            }
5325                      } else {
5326            ## As if in table            !!!cp ('t227');
5327            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5328            $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');  
5329    
5330              ## 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';  
5331    
5332                push @$active_formatting_elements, ['#marker', ''];          $insert = $insert_to_foster;
5333                          #
5334                !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
5335                redo B;              if ($token->{tag_name} eq 'tr' and
5336              } 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>  
5337                ## have an element in table scope                ## have an element in table scope
5338                my $i;                my $i;
5339                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5340                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5341                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5342                      !!!cp ('t228');
5343                    $i = $_;                    $i = $_;
5344                    last INSCOPE;                    last INSCOPE;
5345                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5346                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5347                    last INSCOPE;                    last INSCOPE;
5348                  }                  }
5349                } # INSCOPE                } # INSCOPE
5350                unless (defined $i) {                unless (defined $i) {
5351                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5352                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5353                  ## Ignore the token                  ## Ignore the token
5354                    !!!nack ('t230.1');
5355                  !!!next-token;                  !!!next-token;
5356                  redo B;                  next B;
5357                  } else {
5358                    !!!cp ('t232');
5359                }                }
5360    
5361                ## Clear back to table row context                ## Clear back to table row context
5362                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5363                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5364                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5365                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5366                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5367                }                }
5368    
5369                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5370                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5371                ## reprocess                !!!next-token;
5372                redo B;                !!!nack ('t231.1');
5373                  next B;
5374              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5375                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5376                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5377                    ## have an element in table scope
5378                ## As if </table>                  my $i;
5379                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5380                my $i;                    my $node = $self->{open_elements}->[$_];
5381                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5382                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5383                  if ($node->[1] eq 'table') {                      $i = $_;
5384                    $i = $_;                      last INSCOPE;
5385                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5386                  } elsif ({                      !!!cp ('t234');
5387                            table => 1, html => 1,                      last INSCOPE;
5388                           }->{$node->[1]}) {                    }
5389                    last INSCOPE;                  } # INSCOPE
5390                    unless (defined $i) {
5391                      !!!cp ('t235');
5392    ## TODO: The following is wrong.
5393                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5394                      ## Ignore the token
5395                      !!!nack ('t236.1');
5396                      !!!next-token;
5397                      next B;
5398                  }                  }
5399                } # INSCOPE                  
5400                unless (defined $i) {                  ## Clear back to table row context
5401                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5402                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5403                  !!!next-token;                    !!!cp ('t236');
5404                  redo B;  ## ISSUE: Can this state be reached?
5405                }                    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;  
5406                  }                  }
5407                } # INSCOPE                  
5408                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5409                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5410                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5411                  !!!next-token;                }
5412                  redo B;  
5413                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5414                    ## have an element in table scope
5415                ## Clear back to table row context                  my $i;
5416                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5417                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5418                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5419                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5420                        $i = $_;
5421                        last INSCOPE;
5422                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5423                        !!!cp ('t238');
5424                        last INSCOPE;
5425                      }
5426                    } # INSCOPE
5427                    unless (defined $i) {
5428                      !!!cp ('t239');
5429                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5430                      ## Ignore the token
5431                      !!!nack ('t239.1');
5432                      !!!next-token;
5433                      next B;
5434                    }
5435                    
5436                    ## Clear back to table body context
5437                    while (not ($self->{open_elements}->[-1]->[1]
5438                                    & TABLE_ROWS_SCOPING_EL)) {
5439                      !!!cp ('t240');
5440                      pop @{$self->{open_elements}};
5441                    }
5442                    
5443                    ## As if <{current node}>
5444                    ## have an element in table scope
5445                    ## true by definition
5446                    
5447                    ## Clear back to table body context
5448                    ## nop by definition
5449                    
5450                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5451                    $self->{insertion_mode} = IN_TABLE_IM;
5452                    ## reprocess in the "in table" insertion mode...
5453                }                }
5454    
5455                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
5456                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
5457                !!!next-token;                ## the code for <table> in the "in table" insertion mode
5458                redo B;                ## is synced with it.
5459              } elsif ($token->{tag_name} eq 'table') {  
5460                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
5461                my $i;                my $i;
5462                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5463                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5464                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
5465                      !!!cp ('t241');
5466                    $i = $_;                    $i = $_;
5467                    last INSCOPE;                    last INSCOPE;
5468                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5469                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5470                    last INSCOPE;                    last INSCOPE;
5471                  }                  }
5472                } # INSCOPE                } # INSCOPE
5473                unless (defined $i) {                unless (defined $i) {
5474                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
5475                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5476                  ## Ignore the token                  ## Ignore the token
5477                    !!!nack ('t243.1');
5478                  !!!next-token;                  !!!next-token;
5479                  redo B;                  next B;
5480                }                }
5481                    
5482                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
5483                while (not {                pop @{$open_tables};
5484                  tr => 1, html => 1,                
5485                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
5486                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
5487                  pop @{$self->{open_elements}};                !!!next-token;
5488                }                next B;
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
5489              } elsif ({              } elsif ({
5490                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5491                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
5492                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
5493                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
5494                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5495                  my $node = $self->{open_elements}->[$_];                  my $i;
5496                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5497                    $i = $_;                    my $node = $self->{open_elements}->[$_];
5498                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5499                  } elsif ({                      !!!cp ('t247');
5500                            table => 1, html => 1,                      $i = $_;
5501                           }->{$node->[1]}) {                      last INSCOPE;
5502                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5503                        !!!cp ('t248');
5504                        last INSCOPE;
5505                      }
5506                    } # INSCOPE
5507                      unless (defined $i) {
5508                        !!!cp ('t249');
5509                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5510                        ## Ignore the token
5511                        !!!nack ('t249.1');
5512                        !!!next-token;
5513                        next B;
5514                      }
5515                    
5516                    ## As if </tr>
5517                    ## have an element in table scope
5518                    my $i;
5519                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5520                      my $node = $self->{open_elements}->[$_];
5521                      if ($node->[1] & TABLE_ROW_EL) {
5522                        !!!cp ('t250');
5523                        $i = $_;
5524                        last INSCOPE;
5525                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5526                        !!!cp ('t251');
5527                        last INSCOPE;
5528                      }
5529                    } # INSCOPE
5530                      unless (defined $i) {
5531                        !!!cp ('t252');
5532                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5533                        ## Ignore the token
5534                        !!!nack ('t252.1');
5535                        !!!next-token;
5536                        next B;
5537                      }
5538                    
5539                    ## Clear back to table row context
5540                    while (not ($self->{open_elements}->[-1]->[1]
5541                                    & TABLE_ROW_SCOPING_EL)) {
5542                      !!!cp ('t253');
5543    ## ISSUE: Can this case be reached?
5544                      pop @{$self->{open_elements}};
5545                  }                  }
5546                } # INSCOPE                  
5547                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5548                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5549                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
5550                }                }
5551    
               ## As if </tr>  
5552                ## have an element in table scope                ## have an element in table scope
5553                my $i;                my $i;
5554                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5555                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5556                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5557                      !!!cp ('t254');
5558                    $i = $_;                    $i = $_;
5559                    last INSCOPE;                    last INSCOPE;
5560                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5561                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5562                    last INSCOPE;                    last INSCOPE;
5563                  }                  }
5564                } # INSCOPE                } # INSCOPE
5565                unless (defined $i) {                unless (defined $i) {
5566                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
5567                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5568                  ## Ignore the token                  ## Ignore the token
5569                    !!!nack ('t256.1');
5570                  !!!next-token;                  !!!next-token;
5571                  redo B;                  next B;
5572                }                }
5573    
5574                ## Clear back to table row context                ## Clear back to table body context
5575                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5576                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5577                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5578                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5579                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5580                }                }
5581    
5582                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
5583                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
5584                ## reprocess                !!!nack ('t257.1');
5585                redo B;                !!!next-token;
5586                  next B;
5587              } elsif ({              } elsif ({
5588                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5589                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5590                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5591                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5592                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5593                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5594                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5595                !!!next-token;            ## Ignore the token
5596                redo B;            !!!nack ('t258.1');
5597              } else {             !!!next-token;
5598                #            next B;
5599              }          } else {
5600            } else {            !!!cp ('t259');
5601              #            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
           }  
5602    
5603            ## As if in table            $insert = $insert_to_foster;
5604            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5605            $in_body->($insert_to_foster);          }
5606            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5607          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5608            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
5609              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
5610              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
5611                          #
5612              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5613              !!!cp ('t259.2');
5614              #
5615            }
5616    
5617              !!!next-token;          ## Stop parsing
5618              redo B;          last B;
5619            } elsif ($token->{type} eq 'comment') {        } else {
5620              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
5621              my $comment = $self->{document}->create_comment ($token->{data});        }
5622              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5623              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
5624              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5625            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5626              if ({                unless (length $token->{data}) {
5627                   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  
5628                  !!!next-token;                  !!!next-token;
5629                  redo B;                  next B;
5630                }                }
5631                }
5632                ## Close the cell              
5633                !!!back-token; # <?>              !!!cp ('t261');
5634                $token = {type => 'end tag', tag_name => $tn};              #
5635                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
5636              } else {              if ($token->{tag_name} eq 'col') {
5637                  !!!cp ('t262');
5638                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5639                  pop @{$self->{open_elements}};
5640                  !!!ack ('t262.1');
5641                  !!!next-token;
5642                  next B;
5643                } else {
5644                  !!!cp ('t263');
5645                #                #
5646              }              }
5647            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5648              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
5649                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5650                my $i;                  !!!cp ('t264');
5651                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});  
5652                  ## Ignore the token                  ## Ignore the token
5653                  !!!next-token;                  !!!next-token;
5654                  redo B;                  next B;
5655                }                } else {
5656                                  !!!cp ('t265');
5657                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
5658                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
5659                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
5660                     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]);  
5661                }                }
5662                } elsif ($token->{tag_name} eq 'col') {
5663                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
5664                  !!!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});  
5665                ## Ignore the token                ## Ignore the token
5666                !!!next-token;                !!!next-token;
5667                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;  
5668              } else {              } else {
5669                #                !!!cp ('t267');
5670                  #
5671              }              }
5672          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5673            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5674                @{$self->{open_elements}} == 1) { # redundant, maybe
5675              !!!cp ('t270.2');
5676              ## Stop parsing.
5677              last B;
5678            } else {
5679              ## NOTE: As if </colgroup>.
5680              !!!cp ('t270.1');
5681              pop @{$self->{open_elements}}; # colgroup
5682              $self->{insertion_mode} = IN_TABLE_IM;
5683              ## Reprocess.
5684              next B;
5685            }
5686          } else {
5687            die "$0: $token->{type}: Unknown token type";
5688          }
5689    
5690              ## As if </colgroup>
5691              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5692                !!!cp ('t269');
5693    ## TODO: Wrong error type?
5694                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5695                ## Ignore the token
5696                !!!nack ('t269.1');
5697                !!!next-token;
5698                next B;
5699            } else {            } else {
5700              #              !!!cp ('t270');
5701                pop @{$self->{open_elements}}; # colgroup
5702                $self->{insertion_mode} = IN_TABLE_IM;
5703                !!!ack-later;
5704                ## reprocess
5705                next B;
5706              }
5707        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5708          if ($token->{type} == CHARACTER_TOKEN) {
5709            !!!cp ('t271');
5710            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5711            !!!next-token;
5712            next B;
5713          } elsif ($token->{type} == START_TAG_TOKEN) {
5714            if ($token->{tag_name} eq 'option') {
5715              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5716                !!!cp ('t272');
5717                ## As if </option>
5718                pop @{$self->{open_elements}};
5719              } else {
5720                !!!cp ('t273');
5721            }            }
             
           $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}};  
               }  
5722    
5723                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5724                !!!next-token;            !!!nack ('t273.1');
5725                redo B;            !!!next-token;
5726              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5727                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5728                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5729                  pop @{$self->{open_elements}};              !!!cp ('t274');
5730                }              ## As if </option>
5731                pop @{$self->{open_elements}};
5732              } else {
5733                !!!cp ('t275');
5734              }
5735    
5736                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5737                  ## As if </optgroup>              !!!cp ('t276');
5738                  pop @{$self->{open_elements}};              ## As if </optgroup>
5739                }              pop @{$self->{open_elements}};
5740              } else {
5741                !!!cp ('t277');
5742              }
5743    
5744                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5745                !!!next-token;            !!!nack ('t277.1');
5746                redo B;            !!!next-token;
5747              } elsif ($token->{tag_name} eq 'select') {            next B;
5748                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5749                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5750                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5751                my $i;                    {
5752                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5753                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5754                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5755                    $i = $_;                    }->{$token->{tag_name}})) {
5756                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5757                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5758                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5759                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5760                    last INSCOPE;            ## have an element in table scope
5761                  }            my $i;
5762                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5763                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5764                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5765                  ## Ignore the token                !!!cp ('t278');
5766                  !!!next-token;                $i = $_;
5767                  redo B;                last INSCOPE;
5768                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5769                  !!!cp ('t279');
5770                  last INSCOPE;
5771                }
5772              } # INSCOPE
5773              unless (defined $i) {
5774                !!!cp ('t280');
5775                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5776                ## Ignore the token
5777                !!!nack ('t280.1');
5778                !!!next-token;
5779                next B;
5780              }
5781                                
5782                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5783              splice @{$self->{open_elements}}, $i;
5784    
5785                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5786    
5787                !!!next-token;            if ($token->{tag_name} eq 'select') {
5788                redo B;              !!!nack ('t281.2');
5789              } else {              !!!next-token;
5790                #              next B;
5791              } else {
5792                !!!cp ('t281.1');
5793                !!!ack-later;
5794                ## Reprocess the token.
5795                next B;
5796              }
5797            } else {
5798              !!!cp ('t282');
5799              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5800              ## Ignore the token
5801              !!!nack ('t282.1');
5802              !!!next-token;
5803              next B;
5804            }
5805          } elsif ($token->{type} == END_TAG_TOKEN) {
5806            if ($token->{tag_name} eq 'optgroup') {
5807              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5808                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5809                !!!cp ('t283');
5810                ## As if </option>
5811                splice @{$self->{open_elements}}, -2;
5812              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5813                !!!cp ('t284');
5814                pop @{$self->{open_elements}};
5815              } else {
5816                !!!cp ('t285');
5817                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5818                ## Ignore the token
5819              }
5820              !!!nack ('t285.1');
5821              !!!next-token;
5822              next B;
5823            } elsif ($token->{tag_name} eq 'option') {
5824              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5825                !!!cp ('t286');
5826                pop @{$self->{open_elements}};
5827              } else {
5828                !!!cp ('t287');
5829                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5830                ## Ignore the token
5831              }
5832              !!!nack ('t287.1');
5833              !!!next-token;
5834              next B;
5835            } elsif ($token->{tag_name} eq 'select') {
5836              ## have an element in table scope
5837              my $i;
5838              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5839                my $node = $self->{open_elements}->[$_];
5840                if ($node->[1] & SELECT_EL) {
5841                  !!!cp ('t288');
5842                  $i = $_;
5843                  last INSCOPE;
5844                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5845                  !!!cp ('t289');
5846                  last INSCOPE;
5847              }              }
5848            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
5849              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
5850                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
5851                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5852                  ## As if </option>              ## Ignore the token
5853                  splice @{$self->{open_elements}}, -2;              !!!nack ('t290.1');
5854                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!next-token;
5855                  pop @{$self->{open_elements}};              next B;
5856                } 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;  
               }  
5857                                
5858                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5859              splice @{$self->{open_elements}}, $i;
5860    
5861                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5862    
5863                !!!next-token;            !!!nack ('t291.1');
5864                redo B;            !!!next-token;
5865              } elsif ({            next B;
5866                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5867                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5868                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5869                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5870                                   }->{$token->{tag_name}}) {
5871                ## have an element in table scope  ## TODO: The following is wrong?
5872                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;  
               }  
5873                                
5874                ## As if </select>            ## have an element in table scope
5875                ## have an element in table scope            my $i;
5876                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5877                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
5878                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5879                  if ($node->[1] eq 'select') {                !!!cp ('t292');
5880                    $i = $_;                $i = $_;
5881                    last INSCOPE;                last INSCOPE;
5882                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5883                            table => 1, html => 1,                !!!cp ('t293');
5884                           }->{$node->[1]}) {                last INSCOPE;
5885                    last INSCOPE;              }
5886                  }            } # INSCOPE
5887                } # INSCOPE            unless (defined $i) {
5888                unless (defined $i) {              !!!cp ('t294');
5889                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
5890                  ## Ignore the </select> token              !!!nack ('t294.1');
5891                  !!!next-token; ## TODO: ok?              !!!next-token;
5892                  redo B;              next B;
5893                }            }
5894                                
5895                splice @{$self->{open_elements}}, $i;            ## As if </select>
5896              ## have an element in table scope
5897                $self->_reset_insertion_mode;            undef $i;
5898              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5899                ## reprocess              my $node = $self->{open_elements}->[$_];
5900                redo B;              if ($node->[1] & SELECT_EL) {
5901              } else {                !!!cp ('t295');
5902                #                $i = $_;
5903                  last INSCOPE;
5904                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5905    ## ISSUE: Can this state be reached?
5906                  !!!cp ('t296');
5907                  last INSCOPE;
5908              }              }
5909            } else {            } # INSCOPE
5910              #            unless (defined $i) {
5911                !!!cp ('t297');
5912    ## TODO: The following error type is correct?
5913                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5914                ## Ignore the </select> token
5915                !!!nack ('t297.1');
5916                !!!next-token; ## TODO: ok?
5917                next B;
5918            }            }
5919                  
5920              !!!cp ('t298');
5921              splice @{$self->{open_elements}}, $i;
5922    
5923              $self->_reset_insertion_mode;
5924    
5925            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
5926              ## reprocess
5927              next B;
5928            } else {
5929              !!!cp ('t299');
5930              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5931            ## Ignore the token            ## Ignore the token
5932              !!!nack ('t299.3');
5933            !!!next-token;            !!!next-token;
5934            redo B;            next B;
5935          } elsif ($self->{insertion_mode} eq 'after body') {          }
5936            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5937              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5938                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
5939                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
5940              !!!parse-error (type => 'in body:#eof', token => $token);
5941            } else {
5942              !!!cp ('t299.2');
5943            }
5944    
5945            ## Stop parsing.
5946            last B;
5947          } else {
5948            die "$0: $token->{type}: Unknown token type";
5949          }
5950        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5951          if ($token->{type} == CHARACTER_TOKEN) {
5952            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5953              my $data = $1;
5954              ## As if in body
5955              $reconstruct_active_formatting_elements->($insert_to_current);
5956                                
5957                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5958              
5959              unless (length $token->{data}) {
5960                !!!cp ('t300');
5961                !!!next-token;
5962                next B;
5963              }
5964            }
5965            
5966            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5967              !!!cp ('t301');
5968              !!!parse-error (type => 'after html:#character', token => $token);
5969    
5970                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
5971                  !!!next-token;          } else {
5972                  redo B;            !!!cp ('t302');
5973                }          }
5974              }          
5975                        ## "after body" insertion mode
5976              #          !!!parse-error (type => 'after body:#character', token => $token);
5977              !!!parse-error (type => 'after body:#'.$token->{type});  
5978            } elsif ($token->{type} eq 'comment') {          $self->{insertion_mode} = IN_BODY_IM;
5979              my $comment = $self->{document}->create_comment ($token->{data});          ## reprocess
5980              $self->{open_elements}->[0]->[0]->append_child ($comment);          next B;
5981          } elsif ($token->{type} == START_TAG_TOKEN) {
5982            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5983              !!!cp ('t303');
5984              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5985              
5986              ## Reprocess in the "after body" insertion mode.
5987            } else {
5988              !!!cp ('t304');
5989            }
5990    
5991            ## "after body" insertion mode
5992            !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5993    
5994            $self->{insertion_mode} = IN_BODY_IM;
5995            !!!ack-later;
5996            ## reprocess
5997            next B;
5998          } elsif ($token->{type} == END_TAG_TOKEN) {
5999            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6000              !!!cp ('t305');
6001              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6002              
6003              $self->{insertion_mode} = AFTER_BODY_IM;
6004              ## Reprocess in the "after body" insertion mode.
6005            } else {
6006              !!!cp ('t306');
6007            }
6008    
6009            ## "after body" insertion mode
6010            if ($token->{tag_name} eq 'html') {
6011              if (defined $self->{inner_html_node}) {
6012                !!!cp ('t307');
6013                !!!parse-error (type => 'unmatched end tag:html', token => $token);
6014                ## Ignore the token
6015              !!!next-token;              !!!next-token;
6016              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});  
             }  
6017            } else {            } else {
6018              !!!parse-error (type => 'after body:#'.$token->{type});              !!!cp ('t308');
6019                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6020                !!!next-token;
6021                next B;
6022            }            }
6023            } else {
6024              !!!cp ('t309');
6025              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6026    
6027            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
6028            ## reprocess            ## reprocess
6029            redo B;            next B;
6030          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6031            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6032              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          !!!cp ('t309.2');
6033                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          ## Stop parsing
6034            last B;
6035          } else {
6036            die "$0: $token->{type}: Unknown token type";
6037          }
6038        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6039          if ($token->{type} == CHARACTER_TOKEN) {
6040            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6041              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6042              
6043              unless (length $token->{data}) {
6044                !!!cp ('t310');
6045                !!!next-token;
6046                next B;
6047              }
6048            }
6049            
6050            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6051              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6052                !!!cp ('t311');
6053                !!!parse-error (type => 'in frameset:#character', token => $token);
6054              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6055                !!!cp ('t312');
6056                !!!parse-error (type => 'after frameset:#character', token => $token);
6057              } else { # "after html frameset"
6058                !!!cp ('t313');
6059                !!!parse-error (type => 'after html:#character', token => $token);
6060    
6061                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6062                ## Reprocess in the "after frameset" insertion mode.
6063                !!!parse-error (type => 'after frameset:#character', token => $token);
6064              }
6065              
6066              ## Ignore the token.
6067              if (length $token->{data}) {
6068                !!!cp ('t314');
6069                ## reprocess the rest of characters
6070              } else {
6071                !!!cp ('t315');
6072                !!!next-token;
6073              }
6074              next B;
6075            }
6076            
6077            die qq[$0: Character "$token->{data}"];
6078          } elsif ($token->{type} == START_TAG_TOKEN) {
6079            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6080              !!!cp ('t316');
6081              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6082    
6083              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6084              ## Process in the "after frameset" insertion mode.
6085            } else {
6086              !!!cp ('t317');
6087            }
6088    
6089            if ($token->{tag_name} eq 'frameset' and
6090                $self->{insertion_mode} == IN_FRAMESET_IM) {
6091              !!!cp ('t318');
6092              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6093              !!!nack ('t318.1');
6094              !!!next-token;
6095              next B;
6096            } elsif ($token->{tag_name} eq 'frame' and
6097                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6098              !!!cp ('t319');
6099              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6100              pop @{$self->{open_elements}};
6101              !!!ack ('t319.1');
6102              !!!next-token;
6103              next B;
6104            } elsif ($token->{tag_name} eq 'noframes') {
6105              !!!cp ('t320');
6106              ## NOTE: As if in body.
6107              $parse_rcdata->(CDATA_CONTENT_MODEL);
6108              next B;
6109            } else {
6110              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6111                !!!cp ('t321');
6112                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6113              } else {
6114                !!!cp ('t322');
6115                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6116              }
6117              ## Ignore the token
6118              !!!nack ('t322.1');
6119              !!!next-token;
6120              next B;
6121            }
6122          } elsif ($token->{type} == END_TAG_TOKEN) {
6123            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6124              !!!cp ('t323');
6125              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6126    
6127                unless (length $token->{data}) {            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6128                  !!!next-token;            ## Process in the "after frameset" insertion mode.
6129                  redo B;          } else {
6130                }            !!!cp ('t324');
6131              }          }
6132    
6133              #          if ($token->{tag_name} eq 'frameset' and
6134            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6135              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6136              $self->{open_elements}->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
6137                !!!cp ('t325');
6138                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6139                ## Ignore the token
6140              !!!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 {  
               #  
             }  
6141            } else {            } else {
6142              #              !!!cp ('t326');
6143                pop @{$self->{open_elements}};
6144                !!!next-token;
6145            }            }
6146              
6147            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
6148              !!!parse-error (type => 'in frameset:'.$token->{tag_name});                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6149                !!!cp ('t327');
6150                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6151              } else {
6152                !!!cp ('t328');
6153              }
6154              next B;
6155            } elsif ($token->{tag_name} eq 'html' and
6156                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6157              !!!cp ('t329');
6158              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6159              !!!next-token;
6160              next B;
6161            } else {
6162              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6163                !!!cp ('t330');
6164                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6165            } else {            } else {
6166              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t331');
6167                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6168            }            }
6169            ## Ignore the token            ## Ignore the token
6170            !!!next-token;            !!!next-token;
6171            redo B;            next B;
6172          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6173            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6174              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6175                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                  @{$self->{open_elements}} == 1) { # redundant, maybe
6176              !!!cp ('t331.1');
6177              !!!parse-error (type => 'in body:#eof', token => $token);
6178            } else {
6179              !!!cp ('t331.2');
6180            }
6181            
6182            ## Stop parsing
6183            last B;
6184          } else {
6185            die "$0: $token->{type}: Unknown token type";
6186          }
6187    
6188                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
6189                  !!!next-token;      } else {
6190                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
6191                }      }
6192    
6193        ## "in body" insertion mode
6194        if ($token->{type} == START_TAG_TOKEN) {
6195          if ($token->{tag_name} eq 'script') {
6196            !!!cp ('t332');
6197            ## NOTE: This is an "as if in head" code clone
6198            $script_start_tag->();
6199            next B;
6200          } elsif ($token->{tag_name} eq 'style') {
6201            !!!cp ('t333');
6202            ## NOTE: This is an "as if in head" code clone
6203            $parse_rcdata->(CDATA_CONTENT_MODEL);
6204            next B;
6205          } elsif ({
6206                    base => 1, link => 1,
6207                   }->{$token->{tag_name}}) {
6208            !!!cp ('t334');
6209            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6210            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6211            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6212            !!!ack ('t334.1');
6213            !!!next-token;
6214            next B;
6215          } elsif ($token->{tag_name} eq 'meta') {
6216            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6217            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6218            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6219    
6220            unless ($self->{confident}) {
6221              if ($token->{attributes}->{charset}) {
6222                !!!cp ('t335');
6223                ## NOTE: Whether the encoding is supported or not is handled
6224                ## in the {change_encoding} callback.
6225                $self->{change_encoding}
6226                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6227                
6228                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6229                    ->set_user_data (manakai_has_reference =>
6230                                         $token->{attributes}->{charset}
6231                                             ->{has_reference});
6232              } elsif ($token->{attributes}->{content}) {
6233                if ($token->{attributes}->{content}->{value}
6234                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6235                        [\x09-\x0D\x20]*=
6236                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6237                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6238                  !!!cp ('t336');
6239                  ## NOTE: Whether the encoding is supported or not is handled
6240                  ## in the {change_encoding} callback.
6241                  $self->{change_encoding}
6242                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6243                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6244                      ->set_user_data (manakai_has_reference =>
6245                                           $token->{attributes}->{content}
6246                                                 ->{has_reference});
6247              }              }
6248              }
6249            } else {
6250              if ($token->{attributes}->{charset}) {
6251                !!!cp ('t337');
6252                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6253                    ->set_user_data (manakai_has_reference =>
6254                                         $token->{attributes}->{charset}
6255                                             ->{has_reference});
6256              }
6257              if ($token->{attributes}->{content}) {
6258                !!!cp ('t338');
6259                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6260                    ->set_user_data (manakai_has_reference =>
6261                                         $token->{attributes}->{content}
6262                                             ->{has_reference});
6263              }
6264            }
6265    
6266              #          !!!ack ('t338.1');
6267            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6268              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6269              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6270              !!!next-token;          !!!cp ('t341');
6271              redo B;          ## NOTE: This is an "as if in head" code clone
6272            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6273              if ($token->{tag_name} eq 'noframes') {          next B;
6274                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6275                redo B;          !!!parse-error (type => 'in body:body', token => $token);
6276              } else {                
6277                #          if (@{$self->{open_elements}} == 1 or
6278                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6279              !!!cp ('t342');
6280              ## Ignore the token
6281            } else {
6282              my $body_el = $self->{open_elements}->[1]->[0];
6283              for my $attr_name (keys %{$token->{attributes}}) {
6284                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6285                  !!!cp ('t343');
6286                  $body_el->set_attribute_ns
6287                    (undef, [undef, $attr_name],
6288                     $token->{attributes}->{$attr_name}->{value});
6289              }              }
6290            } elsif ($token->{type} eq 'end tag') {            }
6291              if ($token->{tag_name} eq 'html') {          }
6292                $phase = 'trailing end';          !!!nack ('t343.1');
6293            !!!next-token;
6294            next B;
6295          } elsif ({
6296                    address => 1, blockquote => 1, center => 1, dir => 1,
6297                    div => 1, dl => 1, fieldset => 1,
6298                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6299                    menu => 1, ol => 1, p => 1, ul => 1,
6300                    pre => 1, listing => 1,
6301                    form => 1,
6302                    table => 1,
6303                    hr => 1,
6304                   }->{$token->{tag_name}}) {
6305            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6306              !!!cp ('t350');
6307              !!!parse-error (type => 'in form:form', token => $token);
6308              ## Ignore the token
6309              !!!nack ('t350.1');
6310              !!!next-token;
6311              next B;
6312            }
6313    
6314            ## has a p element in scope
6315            INSCOPE: for (reverse @{$self->{open_elements}}) {
6316              if ($_->[1] & P_EL) {
6317                !!!cp ('t344');
6318                !!!back-token; # <form>
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 ('t345');
6324                last INSCOPE;
6325              }
6326            } # INSCOPE
6327              
6328            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6329            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6330              !!!nack ('t346.1');
6331              !!!next-token;
6332              if ($token->{type} == CHARACTER_TOKEN) {
6333                $token->{data} =~ s/^\x0A//;
6334                unless (length $token->{data}) {
6335                  !!!cp ('t346');
6336                !!!next-token;                !!!next-token;
               redo B;  
6337              } else {              } else {
6338                #                !!!cp ('t349');
6339              }              }
6340            } else {            } else {
6341              #              !!!cp ('t348');
6342            }            }
6343            } elsif ($token->{tag_name} eq 'form') {
6344              !!!cp ('t347.1');
6345              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6346    
6347              !!!nack ('t347.2');
6348              !!!next-token;
6349            } elsif ($token->{tag_name} eq 'table') {
6350              !!!cp ('t382');
6351              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6352                        
6353            if (defined $token->{tag_name}) {            $self->{insertion_mode} = IN_TABLE_IM;
6354              !!!parse-error (type => 'after frameset:'.$token->{tag_name});  
6355              !!!nack ('t382.1');
6356              !!!next-token;
6357            } elsif ($token->{tag_name} eq 'hr') {
6358              !!!cp ('t386');
6359              pop @{$self->{open_elements}};
6360            
6361              !!!nack ('t386.1');
6362              !!!next-token;
6363            } else {
6364              !!!nack ('t347.1');
6365              !!!next-token;
6366            }
6367            next B;
6368          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6369            ## has a p element in scope
6370            INSCOPE: for (reverse @{$self->{open_elements}}) {
6371              if ($_->[1] & P_EL) {
6372                !!!cp ('t353');
6373                !!!back-token; # <x>
6374                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6375                          line => $token->{line}, column => $token->{column}};
6376                next B;
6377              } elsif ($_->[1] & SCOPING_EL) {
6378                !!!cp ('t354');
6379                last INSCOPE;
6380              }
6381            } # INSCOPE
6382              
6383            ## Step 1
6384            my $i = -1;
6385            my $node = $self->{open_elements}->[$i];
6386            my $li_or_dtdd = {li => {li => 1},
6387                              dt => {dt => 1, dd => 1},
6388                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6389            LI: {
6390              ## Step 2
6391              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6392                if ($i != -1) {
6393                  !!!cp ('t355');
6394                  !!!parse-error (type => 'not closed',
6395                                  value => $self->{open_elements}->[-1]->[0]
6396                                      ->manakai_local_name,
6397                                  token => $token);
6398                } else {
6399                  !!!cp ('t356');
6400                }
6401                splice @{$self->{open_elements}}, $i;
6402                last LI;
6403            } else {            } else {
6404              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
6405              }
6406              
6407              ## Step 3
6408              if (not ($node->[1] & FORMATTING_EL) and
6409                  #not $phrasing_category->{$node->[1]} and
6410                  ($node->[1] & SPECIAL_EL or
6411                   $node->[1] & SCOPING_EL) and
6412                  not ($node->[1] & ADDRESS_EL) and
6413                  not ($node->[1] & DIV_EL)) {
6414                !!!cp ('t358');
6415                last LI;
6416              }
6417              
6418              !!!cp ('t359');
6419              ## Step 4
6420              $i--;
6421              $node = $self->{open_elements}->[$i];
6422              redo LI;
6423            } # LI
6424              
6425            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6426            !!!nack ('t359.1');
6427            !!!next-token;
6428            next B;
6429          } elsif ($token->{tag_name} eq 'plaintext') {
6430            ## has a p element in scope
6431            INSCOPE: for (reverse @{$self->{open_elements}}) {
6432              if ($_->[1] & P_EL) {
6433                !!!cp ('t367');
6434                !!!back-token; # <plaintext>
6435                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6436                          line => $token->{line}, column => $token->{column}};
6437                next B;
6438              } elsif ($_->[1] & SCOPING_EL) {
6439                !!!cp ('t368');
6440                last INSCOPE;
6441              }
6442            } # INSCOPE
6443              
6444            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6445              
6446            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6447              
6448            !!!nack ('t368.1');
6449            !!!next-token;
6450            next B;
6451          } elsif ($token->{tag_name} eq 'a') {
6452            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6453              my $node = $active_formatting_elements->[$i];
6454              if ($node->[1] & A_EL) {
6455                !!!cp ('t371');
6456                !!!parse-error (type => 'in a:a', token => $token);
6457                
6458                !!!back-token; # <a>
6459                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6460                          line => $token->{line}, column => $token->{column}};
6461                $formatting_end_tag->($token);
6462                
6463                AFE2: for (reverse 0..$#$active_formatting_elements) {
6464                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6465                    !!!cp ('t372');
6466                    splice @$active_formatting_elements, $_, 1;
6467                    last AFE2;
6468                  }
6469                } # AFE2
6470                OE: for (reverse 0..$#{$self->{open_elements}}) {
6471                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6472                    !!!cp ('t373');
6473                    splice @{$self->{open_elements}}, $_, 1;
6474                    last OE;
6475                  }
6476                } # OE
6477                last AFE;
6478              } elsif ($node->[0] eq '#marker') {
6479                !!!cp ('t374');
6480                last AFE;
6481              }
6482            } # AFE
6483              
6484            $reconstruct_active_formatting_elements->($insert_to_current);
6485    
6486            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6487            push @$active_formatting_elements, $self->{open_elements}->[-1];
6488    
6489            !!!nack ('t374.1');
6490            !!!next-token;
6491            next B;
6492          } elsif ($token->{tag_name} eq 'nobr') {
6493            $reconstruct_active_formatting_elements->($insert_to_current);
6494    
6495            ## has a |nobr| element in scope
6496            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6497              my $node = $self->{open_elements}->[$_];
6498              if ($node->[1] & NOBR_EL) {
6499                !!!cp ('t376');
6500                !!!parse-error (type => 'in nobr:nobr', token => $token);
6501                !!!back-token; # <nobr>
6502                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6503                          line => $token->{line}, column => $token->{column}};
6504                next B;
6505              } elsif ($node->[1] & SCOPING_EL) {
6506                !!!cp ('t377');
6507                last INSCOPE;
6508              }
6509            } # INSCOPE
6510            
6511            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6512            push @$active_formatting_elements, $self->{open_elements}->[-1];
6513            
6514            !!!nack ('t377.1');
6515            !!!next-token;
6516            next B;
6517          } elsif ($token->{tag_name} eq 'button') {
6518            ## has a button element in scope
6519            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6520              my $node = $self->{open_elements}->[$_];
6521              if ($node->[1] & BUTTON_EL) {
6522                !!!cp ('t378');
6523                !!!parse-error (type => 'in button:button', token => $token);
6524                !!!back-token; # <button>
6525                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6526                          line => $token->{line}, column => $token->{column}};
6527                next B;
6528              } elsif ($node->[1] & SCOPING_EL) {
6529                !!!cp ('t379');
6530                last INSCOPE;
6531            }            }
6532            } # INSCOPE
6533              
6534            $reconstruct_active_formatting_elements->($insert_to_current);
6535              
6536            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6537    
6538            ## TODO: associate with $self->{form_element} if defined
6539    
6540            push @$active_formatting_elements, ['#marker', ''];
6541    
6542            !!!nack ('t379.1');
6543            !!!next-token;
6544            next B;
6545          } elsif ({
6546                    xmp => 1,
6547                    iframe => 1,
6548                    noembed => 1,
6549                    noframes => 1,
6550                    noscript => 0, ## TODO: 1 if scripting is enabled
6551                   }->{$token->{tag_name}}) {
6552            if ($token->{tag_name} eq 'xmp') {
6553              !!!cp ('t381');
6554              $reconstruct_active_formatting_elements->($insert_to_current);
6555            } else {
6556              !!!cp ('t399');
6557            }
6558            ## NOTE: There is an "as if in body" code clone.
6559            $parse_rcdata->(CDATA_CONTENT_MODEL);
6560            next B;
6561          } elsif ($token->{tag_name} eq 'isindex') {
6562            !!!parse-error (type => 'isindex', token => $token);
6563            
6564            if (defined $self->{form_element}) {
6565              !!!cp ('t389');
6566            ## Ignore the token            ## Ignore the token
6567              !!!nack ('t389'); ## NOTE: Not acknowledged.
6568            !!!next-token;            !!!next-token;
6569            redo B;            next B;
6570            } else {
6571              my $at = $token->{attributes};
6572              my $form_attrs;
6573              $form_attrs->{action} = $at->{action} if $at->{action};
6574              my $prompt_attr = $at->{prompt};
6575              $at->{name} = {name => 'name', value => 'isindex'};
6576              delete $at->{action};
6577              delete $at->{prompt};
6578              my @tokens = (
6579                            {type => START_TAG_TOKEN, tag_name => 'form',
6580                             attributes => $form_attrs,
6581                             line => $token->{line}, column => $token->{column}},
6582                            {type => START_TAG_TOKEN, tag_name => 'hr',
6583                             line => $token->{line}, column => $token->{column}},
6584                            {type => START_TAG_TOKEN, tag_name => 'p',
6585                             line => $token->{line}, column => $token->{column}},
6586                            {type => START_TAG_TOKEN, tag_name => 'label',
6587                             line => $token->{line}, column => $token->{column}},
6588                           );
6589              if ($prompt_attr) {
6590                !!!cp ('t390');
6591                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6592                               #line => $token->{line}, column => $token->{column},
6593                              };
6594              } else {
6595                !!!cp ('t391');
6596                push @tokens, {type => CHARACTER_TOKEN,
6597                               data => 'This is a searchable index. Insert your search keywords here: ',
6598                               #line => $token->{line}, column => $token->{column},
6599                              }; # SHOULD
6600                ## TODO: make this configurable
6601              }
6602              push @tokens,
6603                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6604                             line => $token->{line}, column => $token->{column}},
6605                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6606                            {type => END_TAG_TOKEN, tag_name => 'label',
6607                             line => $token->{line}, column => $token->{column}},
6608                            {type => END_TAG_TOKEN, tag_name => 'p',
6609                             line => $token->{line}, column => $token->{column}},
6610                            {type => START_TAG_TOKEN, tag_name => 'hr',
6611                             line => $token->{line}, column => $token->{column}},
6612                            {type => END_TAG_TOKEN, tag_name => 'form',
6613                             line => $token->{line}, column => $token->{column}};
6614              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6615              !!!back-token (@tokens);
6616              !!!next-token;
6617              next B;
6618            }
6619          } elsif ($token->{tag_name} eq 'textarea') {
6620            my $tag_name = $token->{tag_name};
6621            my $el;
6622            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6623            
6624            ## TODO: $self->{form_element} if defined
6625            $self->{content_model} = RCDATA_CONTENT_MODEL;
6626            delete $self->{escape}; # MUST
6627            
6628            $insert->($el);
6629            
6630            my $text = '';
6631            !!!nack ('t392.1');
6632            !!!next-token;
6633            if ($token->{type} == CHARACTER_TOKEN) {
6634              $token->{data} =~ s/^\x0A//;
6635              unless (length $token->{data}) {
6636                !!!cp ('t392');
6637                !!!next-token;
6638              } else {
6639                !!!cp ('t393');
6640              }
6641            } else {
6642              !!!cp ('t394');
6643            }
6644            while ($token->{type} == CHARACTER_TOKEN) {
6645              !!!cp ('t395');
6646              $text .= $token->{data};
6647              !!!next-token;
6648            }
6649            if (length $text) {
6650              !!!cp ('t396');
6651              $el->manakai_append_text ($text);
6652            }
6653            
6654            $self->{content_model} = PCDATA_CONTENT_MODEL;
6655            
6656            if ($token->{type} == END_TAG_TOKEN and
6657                $token->{tag_name} eq $tag_name) {
6658              !!!cp ('t397');
6659              ## Ignore the token
6660            } else {
6661              !!!cp ('t398');
6662              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6663            }
6664            !!!next-token;
6665            next B;
6666          } elsif ($token->{tag_name} eq 'math' or
6667                   $token->{tag_name} eq 'svg') {
6668            $reconstruct_active_formatting_elements->($insert_to_current);
6669    
6670            ## ISSUE: An issue in spec there          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6671    
6672            ## "adjust foreign attributes" - done in insert-element-f
6673            
6674            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6675            
6676            if ($self->{self_closing}) {
6677              pop @{$self->{open_elements}};
6678              !!!ack ('t398.1');
6679          } else {          } else {
6680            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t398.2');
6681              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6682              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6683              ## mode, "in body" (not "in foreign content") secondary insertion
6684              ## mode, maybe.
6685          }          }
6686        }  
6687      } elsif ($phase eq 'trailing end') {          !!!next-token;
6688        ## states in the main stage is preserved yet # MUST          next B;
6689                } elsif ({
6690        if ($token->{type} eq 'DOCTYPE') {                  caption => 1, col => 1, colgroup => 1, frame => 1,
6691          !!!parse-error (type => 'after html:#DOCTYPE');                  frameset => 1, head => 1, option => 1, optgroup => 1,
6692                    tbody => 1, td => 1, tfoot => 1, th => 1,
6693                    thead => 1, tr => 1,
6694                   }->{$token->{tag_name}}) {
6695            !!!cp ('t401');
6696            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6697          ## Ignore the token          ## Ignore the token
6698            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6699          !!!next-token;          !!!next-token;
6700          redo B;          next B;
6701        } elsif ($token->{type} eq 'comment') {          
6702          my $comment = $self->{document}->create_comment ($token->{data});          ## ISSUE: An issue on HTML5 new elements in the spec.
6703          $self->{document}->append_child ($comment);        } else {
6704            if ($token->{tag_name} eq 'image') {
6705              !!!cp ('t384');
6706              !!!parse-error (type => 'image', token => $token);
6707              $token->{tag_name} = 'img';
6708            } else {
6709              !!!cp ('t385');
6710            }
6711    
6712            ## NOTE: There is an "as if <br>" code clone.
6713            $reconstruct_active_formatting_elements->($insert_to_current);
6714            
6715            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6716    
6717            if ({
6718                 applet => 1, marquee => 1, object => 1,
6719                }->{$token->{tag_name}}) {
6720              !!!cp ('t380');
6721              push @$active_formatting_elements, ['#marker', ''];
6722              !!!nack ('t380.1');
6723            } elsif ({
6724                      b => 1, big => 1, em => 1, font => 1, i => 1,
6725                      s => 1, small => 1, strile => 1,
6726                      strong => 1, tt => 1, u => 1,
6727                     }->{$token->{tag_name}}) {
6728              !!!cp ('t375');
6729              push @$active_formatting_elements, $self->{open_elements}->[-1];
6730              !!!nack ('t375.1');
6731            } elsif ($token->{tag_name} eq 'input') {
6732              !!!cp ('t388');
6733              ## TODO: associate with $self->{form_element} if defined
6734              pop @{$self->{open_elements}};
6735              !!!ack ('t388.2');
6736            } elsif ({
6737                      area => 1, basefont => 1, bgsound => 1, br => 1,
6738                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6739                      #image => 1,
6740                     }->{$token->{tag_name}}) {
6741              !!!cp ('t388.1');
6742              pop @{$self->{open_elements}};
6743              !!!ack ('t388.3');
6744            } elsif ($token->{tag_name} eq 'select') {
6745              ## TODO: associate with $self->{form_element} if defined
6746            
6747              if ($self->{insertion_mode} & TABLE_IMS or
6748                  $self->{insertion_mode} & BODY_TABLE_IMS or
6749                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6750                !!!cp ('t400.1');
6751                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6752              } else {
6753                !!!cp ('t400.2');
6754                $self->{insertion_mode} = IN_SELECT_IM;
6755              }
6756              !!!nack ('t400.3');
6757            } else {
6758              !!!nack ('t402');
6759            }
6760            
6761          !!!next-token;          !!!next-token;
6762          redo B;          next B;
6763        } elsif ($token->{type} eq 'character') {        }
6764          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6765            my $data = $1;        if ($token->{tag_name} eq 'body') {
6766            ## As if in the main phase.          ## has a |body| element in scope
6767            ## NOTE: The insertion mode in the main phase          my $i;
6768            ## just before the phase has been changed to the trailing          INSCOPE: {
6769            ## end phase is either "after body" or "after frameset".            for (reverse @{$self->{open_elements}}) {
6770            $reconstruct_active_formatting_elements->($insert_to_current)              if ($_->[1] & BODY_EL) {
6771              if $phase eq 'main';                !!!cp ('t405');
6772                  $i = $_;
6773                  last INSCOPE;
6774                } elsif ($_->[1] & SCOPING_EL) {
6775                  !!!cp ('t405.1');
6776                  last;
6777                }
6778              }
6779    
6780              !!!parse-error (type => 'start tag not allowed',
6781                              value => $token->{tag_name}, token => $token);
6782              ## NOTE: Ignore the token.
6783              !!!next-token;
6784              next B;
6785            } # INSCOPE
6786    
6787            for (@{$self->{open_elements}}) {
6788              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6789                !!!cp ('t403');
6790                !!!parse-error (type => 'not closed',
6791                                value => $_->[0]->manakai_local_name,
6792                                token => $token);
6793                last;
6794              } else {
6795                !!!cp ('t404');
6796              }
6797            }
6798    
6799            $self->{insertion_mode} = AFTER_BODY_IM;
6800            !!!next-token;
6801            next B;
6802          } elsif ($token->{tag_name} eq 'html') {
6803            ## TODO: Update this code.  It seems that the code below is not
6804            ## up-to-date, though it has same effect as speced.
6805            if (@{$self->{open_elements}} > 1 and
6806                $self->{open_elements}->[1]->[1] & BODY_EL) {
6807              ## ISSUE: There is an issue in the spec.
6808              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6809                !!!cp ('t406');
6810                !!!parse-error (type => 'not closed',
6811                                value => $self->{open_elements}->[1]->[0]
6812                                    ->manakai_local_name,
6813                                token => $token);
6814              } else {
6815                !!!cp ('t407');
6816              }
6817              $self->{insertion_mode} = AFTER_BODY_IM;
6818              ## reprocess
6819              next B;
6820            } else {
6821              !!!cp ('t408');
6822              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6823              ## Ignore the token
6824              !!!next-token;
6825              next B;
6826            }
6827          } elsif ({
6828                    address => 1, blockquote => 1, center => 1, dir => 1,
6829                    div => 1, dl => 1, fieldset => 1, listing => 1,
6830                    menu => 1, ol => 1, pre => 1, ul => 1,
6831                    dd => 1, dt => 1, li => 1,
6832                    applet => 1, button => 1, marquee => 1, object => 1,
6833                   }->{$token->{tag_name}}) {
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->[0]->manakai_local_name eq $token->{tag_name}) {
6839                !!!cp ('t410');
6840                $i = $_;
6841                last INSCOPE;
6842              } elsif ($node->[1] & SCOPING_EL) {
6843                !!!cp ('t411');
6844                last INSCOPE;
6845              }
6846            } # INSCOPE
6847    
6848            unless (defined $i) { # has an element in scope
6849              !!!cp ('t413');
6850              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6851            } else {
6852              ## Step 1. generate implied end tags
6853              while ({
6854                      dd => ($token->{tag_name} ne 'dd'),
6855                      dt => ($token->{tag_name} ne 'dt'),
6856                      li => ($token->{tag_name} ne 'li'),
6857                      p => 1,
6858                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6859                !!!cp ('t409');
6860                pop @{$self->{open_elements}};
6861              }
6862    
6863              ## Step 2.
6864              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6865                      ne $token->{tag_name}) {
6866                !!!cp ('t412');
6867                !!!parse-error (type => 'not closed',
6868                                value => $self->{open_elements}->[-1]->[0]
6869                                    ->manakai_local_name,
6870                                token => $token);
6871              } else {
6872                !!!cp ('t414');
6873              }
6874    
6875              ## Step 3.
6876              splice @{$self->{open_elements}}, $i;
6877    
6878              ## Step 4.
6879              $clear_up_to_marker->()
6880                  if {
6881                    applet => 1, button => 1, marquee => 1, object => 1,
6882                  }->{$token->{tag_name}};
6883            }
6884            !!!next-token;
6885            next B;
6886          } elsif ($token->{tag_name} eq 'form') {
6887            undef $self->{form_element};
6888    
6889            ## has an element in scope
6890            my $i;
6891            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6892              my $node = $self->{open_elements}->[$_];
6893              if ($node->[1] & FORM_EL) {
6894                !!!cp ('t418');
6895                $i = $_;
6896                last INSCOPE;
6897              } elsif ($node->[1] & SCOPING_EL) {
6898                !!!cp ('t419');
6899                last INSCOPE;
6900              }
6901            } # INSCOPE
6902    
6903            unless (defined $i) { # has an element in scope
6904              !!!cp ('t421');
6905              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6906            } else {
6907              ## Step 1. generate implied end tags
6908              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6909                !!!cp ('t417');
6910                pop @{$self->{open_elements}};
6911              }
6912                        
6913            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
6914              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6915                      ne $token->{tag_name}) {
6916                !!!cp ('t417.1');
6917                !!!parse-error (type => 'not closed',
6918                                value => $self->{open_elements}->[-1]->[0]
6919                                    ->manakai_local_name,
6920                                token => $token);
6921              } else {
6922                !!!cp ('t420');
6923              }  
6924                        
6925            unless (length $token->{data}) {            ## Step 3.
6926              !!!next-token;            splice @{$self->{open_elements}}, $i;
6927              redo B;          }
6928    
6929            !!!next-token;
6930            next B;
6931          } elsif ({
6932                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6933                   }->{$token->{tag_name}}) {
6934            ## has an element in scope
6935            my $i;
6936            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6937              my $node = $self->{open_elements}->[$_];
6938              if ($node->[1] & HEADING_EL) {
6939                !!!cp ('t423');
6940                $i = $_;
6941                last INSCOPE;
6942              } elsif ($node->[1] & SCOPING_EL) {
6943                !!!cp ('t424');
6944                last INSCOPE;
6945              }
6946            } # INSCOPE
6947    
6948            unless (defined $i) { # has an element in scope
6949              !!!cp ('t425.1');
6950              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6951            } else {
6952              ## Step 1. generate implied end tags
6953              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6954                !!!cp ('t422');
6955                pop @{$self->{open_elements}};
6956              }
6957              
6958              ## Step 2.
6959              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6960                      ne $token->{tag_name}) {
6961                !!!cp ('t425');
6962                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6963              } else {
6964                !!!cp ('t426');
6965            }            }
6966    
6967              ## Step 3.
6968              splice @{$self->{open_elements}}, $i;
6969          }          }
6970            
6971            !!!next-token;
6972            next B;
6973          } elsif ($token->{tag_name} eq 'p') {
6974            ## has an element in scope
6975            my $i;
6976            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6977              my $node = $self->{open_elements}->[$_];
6978              if ($node->[1] & P_EL) {
6979                !!!cp ('t410.1');
6980                $i = $_;
6981                last INSCOPE;
6982              } elsif ($node->[1] & SCOPING_EL) {
6983                !!!cp ('t411.1');
6984                last INSCOPE;
6985              }
6986            } # INSCOPE
6987    
6988          !!!parse-error (type => 'after html:#character');          if (defined $i) {
6989          $phase = 'main';            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6990          ## reprocess                    ne $token->{tag_name}) {
6991          redo B;              !!!cp ('t412.1');
6992        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
6993                 $token->{type} eq 'end tag') {                              value => $self->{open_elements}->[-1]->[0]
6994          !!!parse-error (type => 'after html:'.$token->{tag_name});                                  ->manakai_local_name,
6995          $phase = 'main';                              token => $token);
6996          ## reprocess            } else {
6997          redo B;              !!!cp ('t414.1');
6998        } elsif ($token->{type} eq 'end-of-file') {            }
6999          ## Stop parsing  
7000          last B;            splice @{$self->{open_elements}}, $i;
7001            } else {
7002              !!!cp ('t413.1');
7003              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7004    
7005              !!!cp ('t415.1');
7006              ## As if <p>, then reprocess the current token
7007              my $el;
7008              !!!create-element ($el, $HTML_NS, 'p',, $token);
7009              $insert->($el);
7010              ## NOTE: Not inserted into |$self->{open_elements}|.
7011            }
7012    
7013            !!!next-token;
7014            next B;
7015          } elsif ({
7016                    a => 1,
7017                    b => 1, big => 1, em => 1, font => 1, i => 1,
7018                    nobr => 1, s => 1, small => 1, strile => 1,
7019                    strong => 1, tt => 1, u => 1,
7020                   }->{$token->{tag_name}}) {
7021            !!!cp ('t427');
7022            $formatting_end_tag->($token);
7023            next B;
7024          } elsif ($token->{tag_name} eq 'br') {
7025            !!!cp ('t428');
7026            !!!parse-error (type => 'unmatched end tag:br', token => $token);
7027    
7028            ## As if <br>
7029            $reconstruct_active_formatting_elements->($insert_to_current);
7030            
7031            my $el;
7032            !!!create-element ($el, $HTML_NS, 'br',, $token);
7033            $insert->($el);
7034            
7035            ## Ignore the token.
7036            !!!next-token;
7037            next B;
7038          } elsif ({
7039                    caption => 1, col => 1, colgroup => 1, frame => 1,
7040                    frameset => 1, head => 1, option => 1, optgroup => 1,
7041                    tbody => 1, td => 1, tfoot => 1, th => 1,
7042                    thead => 1, tr => 1,
7043                    area => 1, basefont => 1, bgsound => 1,
7044                    embed => 1, hr => 1, iframe => 1, image => 1,
7045                    img => 1, input => 1, isindex => 1, noembed => 1,
7046                    noframes => 1, param => 1, select => 1, spacer => 1,
7047                    table => 1, textarea => 1, wbr => 1,
7048                    noscript => 0, ## TODO: if scripting is enabled
7049                   }->{$token->{tag_name}}) {
7050            !!!cp ('t429');
7051            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7052            ## Ignore the token
7053            !!!next-token;
7054            next B;
7055            
7056            ## ISSUE: Issue on HTML5 new elements in spec
7057            
7058        } else {        } else {
7059          die "$0: $token->{type}: Unknown token";          ## Step 1
7060            my $node_i = -1;
7061            my $node = $self->{open_elements}->[$node_i];
7062    
7063            ## Step 2
7064            S2: {
7065              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7066                ## Step 1
7067                ## generate implied end tags
7068                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7069                  !!!cp ('t430');
7070                  ## ISSUE: Can this case be reached?
7071                  pop @{$self->{open_elements}};
7072                }
7073            
7074                ## Step 2
7075                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7076                        ne $token->{tag_name}) {
7077                  !!!cp ('t431');
7078                  ## NOTE: <x><y></x>
7079                  !!!parse-error (type => 'not closed',
7080                                  value => $self->{open_elements}->[-1]->[0]
7081                                      ->manakai_local_name,
7082                                  token => $token);
7083                } else {
7084                  !!!cp ('t432');
7085                }
7086                
7087                ## Step 3
7088                splice @{$self->{open_elements}}, $node_i;
7089    
7090                !!!next-token;
7091                last S2;
7092              } else {
7093                ## Step 3
7094                if (not ($node->[1] & FORMATTING_EL) and
7095                    #not $phrasing_category->{$node->[1]} and
7096                    ($node->[1] & SPECIAL_EL or
7097                     $node->[1] & SCOPING_EL)) {
7098                  !!!cp ('t433');
7099                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7100                  ## Ignore the token
7101                  !!!next-token;
7102                  last S2;
7103                }
7104    
7105                !!!cp ('t434');
7106              }
7107              
7108              ## Step 4
7109              $node_i--;
7110              $node = $self->{open_elements}->[$node_i];
7111              
7112              ## Step 5;
7113              redo S2;
7114            } # S2
7115            next B;
7116        }        }
7117      }      }
7118        next B;
7119      } continue { # B
7120        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7121          ## NOTE: The code below is executed in cases where it does not have
7122          ## to be, but it it is harmless even in those cases.
7123          ## has an element in scope
7124          INSCOPE: {
7125            for (reverse 0..$#{$self->{open_elements}}) {
7126              my $node = $self->{open_elements}->[$_];
7127              if ($node->[1] & FOREIGN_EL) {
7128                last INSCOPE;
7129              } elsif ($node->[1] & SCOPING_EL) {
7130                last;
7131              }
7132            }
7133            
7134            ## NOTE: No foreign element in scope.
7135            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7136          } # INSCOPE
7137        }
7138    } # B    } # B
7139    
7140    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4824  sub set_inner_html ($$$) { Line 7148  sub set_inner_html ($$$) {
7148    my $s = \$_[0];    my $s = \$_[0];
7149    my $onerror = $_[1];    my $onerror = $_[1];
7150    
7151      ## ISSUE: Should {confident} be true?
7152    
7153    my $nt = $node->node_type;    my $nt = $node->node_type;
7154    if ($nt == 9) {    if ($nt == 9) {
7155      # MUST      # MUST
# Line 4846  sub set_inner_html ($$$) { Line 7172  sub set_inner_html ($$$) {
7172      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7173    
7174      ## Step 1 # MUST      ## Step 1 # MUST
7175      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
7176      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
7177        $doc->manakai_is_html (1);
7178      my $p = $class->new;      my $p = $class->new;
7179      $p->{document} = $doc;      $p->{document} = $doc;
7180    
7181      ## Step 9 # MUST      ## Step 8 # MUST
7182      my $i = 0;      my $i = 0;
7183      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7184      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7185      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7186        my $self = shift;        my $self = shift;
7187        $self->{next_input_character} = -1 and return if $i >= length $$s;  
7188        $self->{next_input_character} = ord substr $$s, $i++, 1;        pop @{$self->{prev_char}};
7189        $column++;        unshift @{$self->{prev_char}}, $self->{next_char};
7190    
7191        if ($self->{next_input_character} == 0x000A) { # LF        $self->{next_char} = -1 and return if $i >= length $$s;
7192          $line++;        $self->{next_char} = ord substr $$s, $i++, 1;
7193          $column = 0;  
7194        } elsif ($self->{next_input_character} == 0x000D) { # CR        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7195          if ($i >= length $$s) {        $p->{column}++;
7196            #  
7197          } else {        if ($self->{next_char} == 0x000A) { # LF
7198            my $next_char = ord substr $$s, $i++, 1;          $p->{line}++;
7199            if ($next_char == 0x000A) { # LF          $p->{column} = 0;
7200              #          !!!cp ('i1');
7201            } else {        } elsif ($self->{next_char} == 0x000D) { # CR
7202              push @{$self->{char}}, $next_char;          $i++ if substr ($$s, $i, 1) eq "\x0A";
7203            }          $self->{next_char} = 0x000A; # LF # MUST
7204          }          $p->{line}++;
7205          $self->{next_input_character} = 0x000A; # LF # MUST          $p->{column} = 0;
7206          $line++;          !!!cp ('i2');
7207          $column = 0;        } elsif ($self->{next_char} > 0x10FFFF) {
7208        } elsif ($self->{next_input_character} > 0x10FFFF) {          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7209          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          !!!cp ('i3');
7210        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_char} == 0x0000) { # NULL
7211          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          !!!cp ('i4');
7212            !!!parse-error (type => 'NULL');
7213            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7214          } elsif ($self->{next_char} <= 0x0008 or
7215                   (0x000E <= $self->{next_char} and
7216                    $self->{next_char} <= 0x001F) or
7217                   (0x007F <= $self->{next_char} and
7218                    $self->{next_char} <= 0x009F) or
7219                   (0xD800 <= $self->{next_char} and
7220                    $self->{next_char} <= 0xDFFF) or
7221                   (0xFDD0 <= $self->{next_char} and
7222                    $self->{next_char} <= 0xFDDF) or
7223                   {
7224                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7225                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7226                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7227                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7228                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7229                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7230                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7231                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7232                    0x10FFFE => 1, 0x10FFFF => 1,
7233                   }->{$self->{next_char}}) {
7234            !!!cp ('i4.1');
7235            !!!parse-error (type => 'control char', level => $self->{must_level});
7236    ## TODO: error type documentation
7237        }        }
7238      };      };
7239        $p->{prev_char} = [-1, -1, -1];
7240        $p->{next_char} = -1;
7241            
7242      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7243        my (%opt) = @_;        my (%opt) = @_;
7244        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7245          my $column = $opt{column};
7246          if (defined $opt{token} and defined $opt{token}->{line}) {
7247            $line = $opt{token}->{line};
7248            $column = $opt{token}->{column};
7249          }
7250          warn "Parse error ($opt{type}) at line $line column $column\n";
7251      };      };
7252      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7253        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7254      };      };
7255            
7256      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7257      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7258    
7259      ## Step 2      ## Step 2
7260      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7261      $p->{content_model_flag} = {      $p->{content_model} = {
7262        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7263        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7264        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7265        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7266        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7267        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7268        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7269        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7270        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7271        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7272      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7273         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7274            unless defined $p->{content_model};
7275            ## ISSUE: What is "the name of the element"? local name?
7276    
7277      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7278          ## TODO: Foreign element OK?
7279    
7280      ## Step 4      ## Step 3
7281      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7282        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7283    
7284      ## Step 5 # MUST      ## Step 4 # MUST
7285      $doc->append_child ($root);      $doc->append_child ($root);
7286    
7287      ## Step 6 # MUST      ## Step 5 # MUST
7288      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7289    
7290      undef $p->{head_element};      undef $p->{head_element};
7291    
7292      ## Step 7 # MUST      ## Step 6 # MUST
7293      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7294    
7295      ## Step 8 # MUST      ## Step 7 # MUST
7296      my $anode = $node;      my $anode = $node;
7297      AN: while (defined $anode) {      AN: while (defined $anode) {
7298        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7299          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7300          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7301            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7302                !!!cp ('i5');
7303              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7304              last AN;              last AN;
7305            }            }
# Line 4944  sub set_inner_html ($$$) { Line 7308  sub set_inner_html ($$$) {
7308        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7309      } # AN      } # AN
7310            
7311      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7312      {      {
7313        my $self = $p;        my $self = $p;
7314        !!!next-token;        !!!next-token;
7315      }      }
7316      $p->_tree_construction_main;      $p->_tree_construction_main;
7317    
7318      ## Step 11 # MUST      ## Step 10 # MUST
7319      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7320      for (@cn) {      for (@cn) {
7321        $node->remove_child ($_);        $node->remove_child ($_);
7322      }      }
7323      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7324    
7325      ## Step 12 # MUST      ## Step 11 # MUST
7326      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7327      for (@cn) {      for (@cn) {
7328          $this_doc->adopt_node ($_);
7329        $node->append_child ($_);        $node->append_child ($_);
7330      }      }
7331      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
7332    
7333      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7334    
7335        delete $p->{parse_error}; # delete loop
7336    } else {    } else {
7337      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";
7338    }    }
# Line 4974  sub set_inner_html ($$$) { Line 7340  sub set_inner_html ($$$) {
7340    
7341  } # tree construction stage  } # tree construction stage
7342    
7343  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7344    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  
7345    
7346  1;  1;
7347  # $Date$  # $Date$

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.134

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24