/[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.6 by wakaba, Sat May 26 08:12:34 2007 UTC revision 1.140 by wakaba, Sat May 24 09:59:52 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    require IO::Handle;
15    
16    my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
17    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
18    my $SVG_NS = q<http://www.w3.org/2000/svg>;
19    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
20    my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
21    my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
22    
23    sub A_EL () { 0b1 }
24    sub ADDRESS_EL () { 0b10 }
25    sub BODY_EL () { 0b100 }
26    sub BUTTON_EL () { 0b1000 }
27    sub CAPTION_EL () { 0b10000 }
28    sub DD_EL () { 0b100000 }
29    sub DIV_EL () { 0b1000000 }
30    sub DT_EL () { 0b10000000 }
31    sub FORM_EL () { 0b100000000 }
32    sub FORMATTING_EL () { 0b1000000000 }
33    sub FRAMESET_EL () { 0b10000000000 }
34    sub HEADING_EL () { 0b100000000000 }
35    sub HTML_EL () { 0b1000000000000 }
36    sub LI_EL () { 0b10000000000000 }
37    sub NOBR_EL () { 0b100000000000000 }
38    sub OPTION_EL () { 0b1000000000000000 }
39    sub OPTGROUP_EL () { 0b10000000000000000 }
40    sub P_EL () { 0b100000000000000000 }
41    sub SELECT_EL () { 0b1000000000000000000 }
42    sub TABLE_EL () { 0b10000000000000000000 }
43    sub TABLE_CELL_EL () { 0b100000000000000000000 }
44    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
45    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
46    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
47    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
48    sub FOREIGN_EL () { 0b10000000000000000000000000 }
49    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
50    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
51    
52    sub TABLE_ROWS_EL () {
53      TABLE_EL |
54      TABLE_ROW_EL |
55      TABLE_ROW_GROUP_EL
56    }
57    
58    sub END_TAG_OPTIONAL_EL () {
59      DD_EL |
60      DT_EL |
61      LI_EL |
62      P_EL
63    }
64    
65    sub ALL_END_TAG_OPTIONAL_EL () {
66      END_TAG_OPTIONAL_EL |
67      BODY_EL |
68      HTML_EL |
69      TABLE_CELL_EL |
70      TABLE_ROW_EL |
71      TABLE_ROW_GROUP_EL
72    }
73    
74    sub SCOPING_EL () {
75      BUTTON_EL |
76      CAPTION_EL |
77      HTML_EL |
78      TABLE_EL |
79      TABLE_CELL_EL |
80      MISC_SCOPING_EL
81    }
82    
83    sub TABLE_SCOPING_EL () {
84      HTML_EL |
85      TABLE_EL
86    }
87    
88    sub TABLE_ROWS_SCOPING_EL () {
89      HTML_EL |
90      TABLE_ROW_GROUP_EL
91    }
92    
93    sub TABLE_ROW_SCOPING_EL () {
94      HTML_EL |
95      TABLE_ROW_EL
96    }
97    
98    sub SPECIAL_EL () {
99      ADDRESS_EL |
100      BODY_EL |
101      DIV_EL |
102      END_TAG_OPTIONAL_EL |
103      FORM_EL |
104      FRAMESET_EL |
105      HEADING_EL |
106      OPTION_EL |
107      OPTGROUP_EL |
108      SELECT_EL |
109      TABLE_ROW_EL |
110      TABLE_ROW_GROUP_EL |
111      MISC_SPECIAL_EL
112    }
113    
114    my $el_category = {
115      a => A_EL | FORMATTING_EL,
116      address => ADDRESS_EL,
117      applet => MISC_SCOPING_EL,
118      area => MISC_SPECIAL_EL,
119      b => FORMATTING_EL,
120      base => MISC_SPECIAL_EL,
121      basefont => MISC_SPECIAL_EL,
122      bgsound => MISC_SPECIAL_EL,
123      big => FORMATTING_EL,
124      blockquote => MISC_SPECIAL_EL,
125      body => BODY_EL,
126      br => MISC_SPECIAL_EL,
127      button => BUTTON_EL,
128      caption => CAPTION_EL,
129      center => MISC_SPECIAL_EL,
130      col => MISC_SPECIAL_EL,
131      colgroup => MISC_SPECIAL_EL,
132      dd => DD_EL,
133      dir => MISC_SPECIAL_EL,
134      div => DIV_EL,
135      dl => MISC_SPECIAL_EL,
136      dt => DT_EL,
137      em => FORMATTING_EL,
138      embed => MISC_SPECIAL_EL,
139      fieldset => MISC_SPECIAL_EL,
140      font => FORMATTING_EL,
141      form => FORM_EL,
142      frame => MISC_SPECIAL_EL,
143      frameset => FRAMESET_EL,
144      h1 => HEADING_EL,
145      h2 => HEADING_EL,
146      h3 => HEADING_EL,
147      h4 => HEADING_EL,
148      h5 => HEADING_EL,
149      h6 => HEADING_EL,
150      head => MISC_SPECIAL_EL,
151      hr => MISC_SPECIAL_EL,
152      html => HTML_EL,
153      i => FORMATTING_EL,
154      iframe => MISC_SPECIAL_EL,
155      img => MISC_SPECIAL_EL,
156      input => MISC_SPECIAL_EL,
157      isindex => MISC_SPECIAL_EL,
158      li => LI_EL,
159      link => MISC_SPECIAL_EL,
160      listing => MISC_SPECIAL_EL,
161      marquee => MISC_SCOPING_EL,
162      menu => MISC_SPECIAL_EL,
163      meta => MISC_SPECIAL_EL,
164      nobr => NOBR_EL | FORMATTING_EL,
165      noembed => MISC_SPECIAL_EL,
166      noframes => MISC_SPECIAL_EL,
167      noscript => MISC_SPECIAL_EL,
168      object => MISC_SCOPING_EL,
169      ol => MISC_SPECIAL_EL,
170      optgroup => OPTGROUP_EL,
171      option => OPTION_EL,
172      p => P_EL,
173      param => MISC_SPECIAL_EL,
174      plaintext => MISC_SPECIAL_EL,
175      pre => MISC_SPECIAL_EL,
176      s => FORMATTING_EL,
177      script => MISC_SPECIAL_EL,
178      select => SELECT_EL,
179      small => FORMATTING_EL,
180      spacer => MISC_SPECIAL_EL,
181      strike => FORMATTING_EL,
182      strong => FORMATTING_EL,
183      style => MISC_SPECIAL_EL,
184      table => TABLE_EL,
185      tbody => TABLE_ROW_GROUP_EL,
186      td => TABLE_CELL_EL,
187      textarea => MISC_SPECIAL_EL,
188      tfoot => TABLE_ROW_GROUP_EL,
189      th => TABLE_CELL_EL,
190      thead => TABLE_ROW_GROUP_EL,
191      title => MISC_SPECIAL_EL,
192      tr => TABLE_ROW_EL,
193      tt => FORMATTING_EL,
194      u => FORMATTING_EL,
195      ul => MISC_SPECIAL_EL,
196      wbr => MISC_SPECIAL_EL,
197    };
198    
199    my $el_category_f = {
200      $MML_NS => {
201        'annotation-xml' => MML_AXML_EL,
202        mi => FOREIGN_FLOW_CONTENT_EL,
203        mo => FOREIGN_FLOW_CONTENT_EL,
204        mn => FOREIGN_FLOW_CONTENT_EL,
205        ms => FOREIGN_FLOW_CONTENT_EL,
206        mtext => FOREIGN_FLOW_CONTENT_EL,
207      },
208      $SVG_NS => {
209        foreignObject => FOREIGN_FLOW_CONTENT_EL,
210        desc => FOREIGN_FLOW_CONTENT_EL,
211        title => FOREIGN_FLOW_CONTENT_EL,
212      },
213      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
214    };
215    
216  my $permitted_slash_tag_name = {  my $svg_attr_name = {
217    base => 1,    attributetype => 'attributeType',
218    link => 1,    basefrequency => 'baseFrequency',
219    meta => 1,    baseprofile => 'baseProfile',
220    hr => 1,    calcmode => 'calcMode',
221    br => 1,    clippathunits => 'clipPathUnits',
222    img=> 1,    contentscripttype => 'contentScriptType',
223    embed => 1,    contentstyletype => 'contentStyleType',
224    param => 1,    diffuseconstant => 'diffuseConstant',
225    area => 1,    edgemode => 'edgeMode',
226    col => 1,    externalresourcesrequired => 'externalResourcesRequired',
227    input => 1,    fecolormatrix => 'feColorMatrix',
228      fecomposite => 'feComposite',
229      fegaussianblur => 'feGaussianBlur',
230      femorphology => 'feMorphology',
231      fetile => 'feTile',
232      filterres => 'filterRes',
233      filterunits => 'filterUnits',
234      glyphref => 'glyphRef',
235      gradienttransform => 'gradientTransform',
236      gradientunits => 'gradientUnits',
237      kernelmatrix => 'kernelMatrix',
238      kernelunitlength => 'kernelUnitLength',
239      keypoints => 'keyPoints',
240      keysplines => 'keySplines',
241      keytimes => 'keyTimes',
242      lengthadjust => 'lengthAdjust',
243      limitingconeangle => 'limitingConeAngle',
244      markerheight => 'markerHeight',
245      markerunits => 'markerUnits',
246      markerwidth => 'markerWidth',
247      maskcontentunits => 'maskContentUnits',
248      maskunits => 'maskUnits',
249      numoctaves => 'numOctaves',
250      pathlength => 'pathLength',
251      patterncontentunits => 'patternContentUnits',
252      patterntransform => 'patternTransform',
253      patternunits => 'patternUnits',
254      pointsatx => 'pointsAtX',
255      pointsaty => 'pointsAtY',
256      pointsatz => 'pointsAtZ',
257      preservealpha => 'preserveAlpha',
258      preserveaspectratio => 'preserveAspectRatio',
259      primitiveunits => 'primitiveUnits',
260      refx => 'refX',
261      refy => 'refY',
262      repeatcount => 'repeatCount',
263      repeatdur => 'repeatDur',
264      requiredextensions => 'requiredExtensions',
265      specularconstant => 'specularConstant',
266      specularexponent => 'specularExponent',
267      spreadmethod => 'spreadMethod',
268      startoffset => 'startOffset',
269      stddeviation => 'stdDeviation',
270      stitchtiles => 'stitchTiles',
271      surfacescale => 'surfaceScale',
272      systemlanguage => 'systemLanguage',
273      tablevalues => 'tableValues',
274      targetx => 'targetX',
275      targety => 'targetY',
276      textlength => 'textLength',
277      viewbox => 'viewBox',
278      viewtarget => 'viewTarget',
279      xchannelselector => 'xChannelSelector',
280      ychannelselector => 'yChannelSelector',
281      zoomandpan => 'zoomAndPan',
282  };  };
283    
284  my $entity_char = {  my $foreign_attr_xname = {
285    AElig => "\x{00C6}",    'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
286    Aacute => "\x{00C1}",    'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
287    Acirc => "\x{00C2}",    'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
288    Agrave => "\x{00C0}",    'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
289    Alpha => "\x{0391}",    'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
290    Aring => "\x{00C5}",    'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
291    Atilde => "\x{00C3}",    'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
292    Auml => "\x{00C4}",    'xml:base' => [$XML_NS, ['xml', 'base']],
293    Beta => "\x{0392}",    'xml:lang' => [$XML_NS, ['xml', 'lang']],
294    Ccedil => "\x{00C7}",    'xml:space' => [$XML_NS, ['xml', 'space']],
295    Chi => "\x{03A7}",    'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
296    Dagger => "\x{2021}",    'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
297    Delta => "\x{0394}",  };
298    ETH => "\x{00D0}",  
299    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  
300    
 ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>  
301  my $c1_entity_char = {  my $c1_entity_char = {
302       128, 8364,    0x80 => 0x20AC,
303       129, 65533,    0x81 => 0xFFFD,
304       130, 8218,    0x82 => 0x201A,
305       131, 402,    0x83 => 0x0192,
306       132, 8222,    0x84 => 0x201E,
307       133, 8230,    0x85 => 0x2026,
308       134, 8224,    0x86 => 0x2020,
309       135, 8225,    0x87 => 0x2021,
310       136, 710,    0x88 => 0x02C6,
311       137, 8240,    0x89 => 0x2030,
312       138, 352,    0x8A => 0x0160,
313       139, 8249,    0x8B => 0x2039,
314       140, 338,    0x8C => 0x0152,
315       141, 65533,    0x8D => 0xFFFD,
316       142, 381,    0x8E => 0x017D,
317       143, 65533,    0x8F => 0xFFFD,
318       144, 65533,    0x90 => 0xFFFD,
319       145, 8216,    0x91 => 0x2018,
320       146, 8217,    0x92 => 0x2019,
321       147, 8220,    0x93 => 0x201C,
322       148, 8221,    0x94 => 0x201D,
323       149, 8226,    0x95 => 0x2022,
324       150, 8211,    0x96 => 0x2013,
325       151, 8212,    0x97 => 0x2014,
326       152, 732,    0x98 => 0x02DC,
327       153, 8482,    0x99 => 0x2122,
328       154, 353,    0x9A => 0x0161,
329       155, 8250,    0x9B => 0x203A,
330       156, 339,    0x9C => 0x0153,
331       157, 65533,    0x9D => 0xFFFD,
332       158, 382,    0x9E => 0x017E,
333       159, 376,    0x9F => 0x0178,
334  }; # $c1_entity_char  }; # $c1_entity_char
335    
336  my $special_category = {  sub parse_byte_string ($$$$;$) {
337    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = shift;
338    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
339    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
340    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
341    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  } # parse_byte_string
342    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
343    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  sub parse_byte_stream ($$$$;$) {
344    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    my $self = ref $_[0] ? shift : shift->new;
345    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $charset_name = shift;
346    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    my $byte_stream = $_[0];
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   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,  
 };  
 # $phrasing_category: all other elements  
347    
348  sub parse_string ($$$;$) {    my $onerror = $_[2] || sub {
349    my $self = shift->new;      my (%opt) = @_;
350    my $s = \$_[0];      warn "Parse error ($opt{type})\n";
351      };
352      $self->{parse_error} = $onerror; # updated later by parse_char_string
353    
354      ## HTML5 encoding sniffing algorithm
355      require Message::Charset::Info;
356      my $charset;
357      my $buffer;
358      my ($char_stream, $e_status);
359    
360      SNIFFING: {
361    
362        ## Step 1
363        if (defined $charset_name) {
364          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
365    
366          ## ISSUE: Unsupported encoding is not ignored according to the spec.
367          ($char_stream, $e_status) = $charset->get_decode_handle
368              ($byte_stream, allow_error_reporting => 1,
369               allow_fallback => 1);
370          if ($char_stream) {
371            $self->{confident} = 1;
372            last SNIFFING;
373          } else {
374            ## TODO: unsupported error
375          }
376        }
377    
378        ## Step 2
379        my $byte_buffer = '';
380        for (1..1024) {
381          my $char = $byte_stream->getc;
382          last unless defined $char;
383          $byte_buffer .= $char;
384        } ## TODO: timeout
385    
386        ## Step 3
387        if ($byte_buffer =~ /^\xFE\xFF/) {
388          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
389          ($char_stream, $e_status) = $charset->get_decode_handle
390              ($byte_stream, allow_error_reporting => 1,
391               allow_fallback => 1, byte_buffer => \$byte_buffer);
392          $self->{confident} = 1;
393          last SNIFFING;
394        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
395          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
396          ($char_stream, $e_status) = $charset->get_decode_handle
397              ($byte_stream, allow_error_reporting => 1,
398               allow_fallback => 1, byte_buffer => \$byte_buffer);
399          $self->{confident} = 1;
400          last SNIFFING;
401        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
403          ($char_stream, $e_status) = $charset->get_decode_handle
404              ($byte_stream, allow_error_reporting => 1,
405               allow_fallback => 1, byte_buffer => \$byte_buffer);
406          $self->{confident} = 1;
407          last SNIFFING;
408        }
409    
410        ## Step 4
411        ## TODO: <meta charset>
412    
413        ## Step 5
414        ## TODO: from history
415    
416        ## Step 6
417        require Whatpm::Charset::UniversalCharDet;
418        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
419            ($byte_buffer);
420        if (defined $charset_name) {
421          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
422    
423          ## ISSUE: Unsupported encoding is not ignored according to the spec.
424          require Whatpm::Charset::DecodeHandle;
425          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
426              ($byte_stream);
427          ($char_stream, $e_status) = $charset->get_decode_handle
428              ($buffer, allow_error_reporting => 1,
429               allow_fallback => 1, byte_buffer => \$byte_buffer);
430          if ($char_stream) {
431            $buffer->{buffer} = $byte_buffer;
432            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
433                            value => $charset_name,
434                            level => $self->{info_level},
435                            line => 1, column => 1);
436            $self->{confident} = 0;
437            last SNIFFING;
438          }
439        }
440    
441        ## Step 7: default
442        ## TODO: Make this configurable.
443        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
444            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
445            ## detectable in the step 6.
446        require Whatpm::Charset::DecodeHandle;
447        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
448            ($byte_stream);
449        ($char_stream, $e_status)
450            = $charset->get_decode_handle ($buffer,
451                                           allow_error_reporting => 1,
452                                           allow_fallback => 1,
453                                           byte_buffer => \$byte_buffer);
454        $buffer->{buffer} = $byte_buffer;
455        !!!parse-error (type => 'sniffing:default', ## TODO: type name
456                        value => 'windows-1252',
457                        level => $self->{info_level},
458                        line => 1, column => 1);
459        $self->{confident} = 0;
460      } # SNIFFING
461    
462      $self->{input_encoding} = $charset->get_iana_name;
463      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
464        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
465                        value => $self->{input_encoding},
466                        level => $self->{unsupported_level},
467                        line => 1, column => 1);
468      } elsif (not ($e_status &
469                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
470        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
471                        value => $self->{input_encoding},
472                        level => $self->{unsupported_level},
473                        line => 1, column => 1);
474      }
475    
476      $self->{change_encoding} = sub {
477        my $self = shift;
478        $charset_name = shift;
479        my $token = shift;
480    
481        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
482        ($char_stream, $e_status) = $charset->get_decode_handle
483            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
484             byte_buffer => \ $buffer->{buffer});
485        
486        if ($char_stream) { # if supported
487          ## "Change the encoding" algorithm:
488    
489          ## Step 1    
490          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
491            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
492            ($char_stream, $e_status) = $charset->get_decode_handle
493                ($byte_stream,
494                 byte_buffer => \ $buffer->{buffer});
495          }
496          $charset_name = $charset->get_iana_name;
497          
498          ## Step 2
499          if (defined $self->{input_encoding} and
500              $self->{input_encoding} eq $charset_name) {
501            !!!parse-error (type => 'charset label:matching', ## TODO: type
502                            value => $charset_name,
503                            level => $self->{info_level});
504            $self->{confident} = 1;
505            return;
506          }
507    
508          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
509              ':'.$charset_name, level => 'w', token => $token);
510          
511          ## Step 3
512          # if (can) {
513            ## change the encoding on the fly.
514            #$self->{confident} = 1;
515            #return;
516          # }
517          
518          ## Step 4
519          throw Whatpm::HTML::RestartParser ();
520        }
521      }; # $self->{change_encoding}
522    
523      my $char_onerror = sub {
524        my (undef, $type, %opt) = @_;
525        !!!parse-error (%opt, type => $type,
526                        line => $self->{line}, column => $self->{column} + 1);
527        if ($opt{octets}) {
528          ${$opt{octets}} = "\x{FFFD}"; # relacement character
529        }
530      };
531      $char_stream->onerror ($char_onerror);
532    
533      my @args = @_; shift @args; # $s
534      my $return;
535      try {
536        $return = $self->parse_char_stream ($char_stream, @args);  
537      } catch Whatpm::HTML::RestartParser with {
538        ## NOTE: Invoked after {change_encoding}.
539    
540        $self->{input_encoding} = $charset->get_iana_name;
541        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
542          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
543                          value => $self->{input_encoding},
544                          level => $self->{unsupported_level},
545                          line => 1, column => 1);
546        } elsif (not ($e_status &
547                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
548          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
549                          value => $self->{input_encoding},
550                          level => $self->{unsupported_level},
551                          line => 1, column => 1);
552        }
553        $self->{confident} = 1;
554        $char_stream->onerror ($char_onerror);
555        $return = $self->parse_char_stream ($char_stream, @args);
556      };
557      return $return;
558    } # parse_byte_stream
559    
560    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
561    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
562    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
563    ## because the core part of our HTML parser expects a string of character,
564    ## not a string of bytes or code units or anything which might contain a BOM.
565    ## Therefore, any parser interface that accepts a string of bytes,
566    ## such as |parse_byte_string| in this module, must ensure that it does
567    ## strip the BOM and never strip any ZWNBSP.
568    
569    sub parse_char_string ($$$;$) {
570      my $self = shift;
571      require utf8;
572      my $s = ref $_[0] ? $_[0] : \($_[0]);
573      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
574      return $self->parse_char_stream ($input, @_[1..$#_]);
575    } # parse_char_string
576    *parse_string = \&parse_char_string;
577    
578    sub parse_char_stream ($$$;$) {
579      my $self = ref $_[0] ? shift : shift->new;
580      my $input = $_[0];
581    $self->{document} = $_[1];    $self->{document} = $_[1];
582      @{$self->{document}->child_nodes} = ();
583    
584    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
585    
586      $self->{confident} = 1 unless exists $self->{confident};
587      $self->{document}->input_encoding ($self->{input_encoding})
588          if defined $self->{input_encoding};
589    
590    my $i = 0;    my $i = 0;
591    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
592    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
593    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
594      my $self = shift;      my $self = shift;
595      $self->{next_input_character} = -1 and return if $i >= length $$s;  
596      $self->{next_input_character} = ord substr $$s, $i++, 1;      pop @{$self->{prev_char}};
597      $column++;      unshift @{$self->{prev_char}}, $self->{next_char};
598    
599        my $char;
600        if (defined $self->{next_next_char}) {
601          $char = $self->{next_next_char};
602          delete $self->{next_next_char};
603        } else {
604          $char = $input->getc;
605        }
606        $self->{next_char} = -1 and return unless defined $char;
607        $self->{next_char} = ord $char;
608    
609        ($self->{line_prev}, $self->{column_prev})
610            = ($self->{line}, $self->{column});
611        $self->{column}++;
612            
613      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
614        $line++;        !!!cp ('j1');
615        $column = 0;        $self->{line}++;
616      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
617        if ($i >= length $$s) {      } elsif ($self->{next_char} == 0x000D) { # CR
618          #        !!!cp ('j2');
619        } else {        my $next = $input->getc;
620          my $next_char = ord substr $$s, $i++, 1;        if (defined $next and $next ne "\x0A") {
621          if ($next_char == 0x000A) { # LF          $self->{next_next_char} = $next;
622            #        }
623          } else {        $self->{next_char} = 0x000A; # LF # MUST
624            push @{$self->{char}}, $next_char;        $self->{line}++;
625          }        $self->{column} = 0;
626        }      } elsif ($self->{next_char} > 0x10FFFF) {
627        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j3');
628        $line++;        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
629        $column = 0;      } elsif ($self->{next_char} == 0x0000) { # NULL
630      } elsif ($self->{next_input_character} > 0x10FFFF) {        !!!cp ('j4');
631        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        !!!parse-error (type => 'NULL');
632      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
633        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST      } elsif ($self->{next_char} <= 0x0008 or
634                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
635                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
636                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
637                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
638                 {
639                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
640                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
641                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
642                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
643                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
644                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
645                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
646                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
647                  0x10FFFE => 1, 0x10FFFF => 1,
648                 }->{$self->{next_char}}) {
649          !!!cp ('j5');
650          !!!parse-error (type => 'control char', level => $self->{must_level});
651    ## TODO: error type documentation
652      }      }
653    };    };
654      $self->{prev_char} = [-1, -1, -1];
655      $self->{next_char} = -1;
656    
657    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
658      my (%opt) = @_;      my (%opt) = @_;
659      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
660        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
661        warn "Parse error ($opt{type}) at line $line column $column\n";
662    };    };
663    $self->{parse_error} = sub {    $self->{parse_error} = sub {
664      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
665    };    };
666    
667    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 391  sub parse_string ($$$;$) { Line 669  sub parse_string ($$$;$) {
669    $self->_construct_tree;    $self->_construct_tree;
670    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
671    
672      delete $self->{parse_error}; # remove loop
673    
674    return $self->{document};    return $self->{document};
675  } # parse_string  } # parse_char_stream
676    
677  sub new ($) {  sub new ($) {
678    my $class = shift;    my $class = shift;
679    my $self = bless {}, $class;    my $self = bless {
680    $self->{set_next_input_character} = sub {      must_level => 'm',
681      $self->{next_input_character} = -1;      should_level => 's',
682        good_level => 'w',
683        warn_level => 'w',
684        info_level => 'i',
685        unsupported_level => 'u',
686      }, $class;
687      $self->{set_next_char} = sub {
688        $self->{next_char} = -1;
689    };    };
690    $self->{parse_error} = sub {    $self->{parse_error} = sub {
691      #      #
692    };    };
693      $self->{change_encoding} = sub {
694        # if ($_[0] is a supported encoding) {
695        #   run "change the encoding" algorithm;
696        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
697        # }
698      };
699      $self->{application_cache_selection} = sub {
700        #
701      };
702    return $self;    return $self;
703  } # new  } # new
704    
705    sub CM_ENTITY () { 0b001 } # & markup in data
706    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
707    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
708    
709    sub PLAINTEXT_CONTENT_MODEL () { 0 }
710    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
711    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
712    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
713    
714    sub DATA_STATE () { 0 }
715    sub ENTITY_DATA_STATE () { 1 }
716    sub TAG_OPEN_STATE () { 2 }
717    sub CLOSE_TAG_OPEN_STATE () { 3 }
718    sub TAG_NAME_STATE () { 4 }
719    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
720    sub ATTRIBUTE_NAME_STATE () { 6 }
721    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
722    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
723    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
724    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
725    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
726    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
727    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
728    sub COMMENT_START_STATE () { 14 }
729    sub COMMENT_START_DASH_STATE () { 15 }
730    sub COMMENT_STATE () { 16 }
731    sub COMMENT_END_STATE () { 17 }
732    sub COMMENT_END_DASH_STATE () { 18 }
733    sub BOGUS_COMMENT_STATE () { 19 }
734    sub DOCTYPE_STATE () { 20 }
735    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
736    sub DOCTYPE_NAME_STATE () { 22 }
737    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
738    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
739    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
740    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
741    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
742    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
743    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
744    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
745    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
746    sub BOGUS_DOCTYPE_STATE () { 32 }
747    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
748    sub SELF_CLOSING_START_TAG_STATE () { 34 }
749    sub CDATA_BLOCK_STATE () { 35 }
750    
751    sub DOCTYPE_TOKEN () { 1 }
752    sub COMMENT_TOKEN () { 2 }
753    sub START_TAG_TOKEN () { 3 }
754    sub END_TAG_TOKEN () { 4 }
755    sub END_OF_FILE_TOKEN () { 5 }
756    sub CHARACTER_TOKEN () { 6 }
757    
758    sub AFTER_HTML_IMS () { 0b100 }
759    sub HEAD_IMS ()       { 0b1000 }
760    sub BODY_IMS ()       { 0b10000 }
761    sub BODY_TABLE_IMS () { 0b100000 }
762    sub TABLE_IMS ()      { 0b1000000 }
763    sub ROW_IMS ()        { 0b10000000 }
764    sub BODY_AFTER_IMS () { 0b100000000 }
765    sub FRAME_IMS ()      { 0b1000000000 }
766    sub SELECT_IMS ()     { 0b10000000000 }
767    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
768        ## NOTE: "in foreign content" insertion mode is special; it is combined
769        ## with the secondary insertion mode.  In this parser, they are stored
770        ## together in the bit-or'ed form.
771    
772    ## NOTE: "initial" and "before html" insertion modes have no constants.
773    
774    ## NOTE: "after after body" insertion mode.
775    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
776    
777    ## NOTE: "after after frameset" insertion mode.
778    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
779    
780    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
781    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
782    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
783    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
784    sub IN_BODY_IM () { BODY_IMS }
785    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
786    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
787    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
788    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
789    sub IN_TABLE_IM () { TABLE_IMS }
790    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
791    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
792    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
793    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
794    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
795    sub IN_COLUMN_GROUP_IM () { 0b10 }
796    
797  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
798    
799  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
800    my $self = shift;    my $self = shift;
801    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
802    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
803    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
804    undef $self->{current_attribute};    undef $self->{current_attribute};
805    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
806    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
807      delete $self->{self_closing};
808    $self->{char} = [];    $self->{char} = [];
809    # $self->{next_input_character}    # $self->{next_char}
810    !!!next-input-character;    !!!next-input-character;
811    $self->{token} = [];    $self->{token} = [];
812      # $self->{escape}
813  } # _initialize_tokenizer  } # _initialize_tokenizer
814    
815  ## A token has:  ## A token has:
816  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
817  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
818  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
819      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
820  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
821  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
822  ##   ->{data} (comment, character)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
823    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
824  ## Macros  ##        ->{name}
825  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
826  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
827  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
828    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
829    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
830    ##     while the token is pushed back to the stack.
831    
832    ## ISSUE: "When a DOCTYPE token is created, its
833    ## <i>self-closing flag</i> must be unset (its other state is that it
834    ## be set), and its attributes list must be empty.": Wrong subject?
835    
836  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
837    
# Line 444  sub _initialize_tokenizer ($) { Line 841  sub _initialize_tokenizer ($) {
841  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
842  ## and removed from the list.  ## and removed from the list.
843    
844    ## NOTE: HTML5 "Writing HTML documents" section, applied to
845    ## documents and not to user agents and conformance checkers,
846    ## contains some requirements that are not detected by the
847    ## parsing algorithm:
848    ## - Some requirements on character encoding declarations. ## TODO
849    ## - "Elements MUST NOT contain content that their content model disallows."
850    ##   ... Some are parse error, some are not (will be reported by c.c.).
851    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
852    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
853    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
854    
855    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
856    ## be detected by the HTML5 parsing algorithm:
857    ## - Text,
858    
859  sub _get_next_token ($) {  sub _get_next_token ($) {
860    my $self = shift;    my $self = shift;
861    
862      if ($self->{self_closing}) {
863        !!!parse-error (type => 'nestc', token => $self->{current_token});
864        ## NOTE: The |self_closing| flag is only set by start tag token.
865        ## In addition, when a start tag token is emitted, it is always set to
866        ## |current_token|.
867        delete $self->{self_closing};
868      }
869    
870    if (@{$self->{token}}) {    if (@{$self->{token}}) {
871        $self->{self_closing} = $self->{token}->[0]->{self_closing};
872      return shift @{$self->{token}};      return shift @{$self->{token}};
873    }    }
874    
875    A: {    A: {
876      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
877        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
878          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
879              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
880            $self->{state} = 'entity data';            !!!cp (1);
881              $self->{state} = ENTITY_DATA_STATE;
882            !!!next-input-character;            !!!next-input-character;
883            redo A;            redo A;
884          } else {          } else {
885              !!!cp (2);
886            #            #
887          }          }
888        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x002D) { # -
889          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
890            $self->{state} = 'tag open';            unless ($self->{escape}) {
891                if ($self->{prev_char}->[0] == 0x002D and # -
892                    $self->{prev_char}->[1] == 0x0021 and # !
893                    $self->{prev_char}->[2] == 0x003C) { # <
894                  !!!cp (3);
895                  $self->{escape} = 1;
896                } else {
897                  !!!cp (4);
898                }
899              } else {
900                !!!cp (5);
901              }
902            }
903            
904            #
905          } elsif ($self->{next_char} == 0x003C) { # <
906            if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
907                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
908                 not $self->{escape})) {
909              !!!cp (6);
910              $self->{state} = TAG_OPEN_STATE;
911            !!!next-input-character;            !!!next-input-character;
912            redo A;            redo A;
913          } else {          } else {
914              !!!cp (7);
915            #            #
916          }          }
917        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
918          !!!emit ({type => 'end-of-file'});          if ($self->{escape} and
919                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
920              if ($self->{prev_char}->[0] == 0x002D and # -
921                  $self->{prev_char}->[1] == 0x002D) { # -
922                !!!cp (8);
923                delete $self->{escape};
924              } else {
925                !!!cp (9);
926              }
927            } else {
928              !!!cp (10);
929            }
930            
931            #
932          } elsif ($self->{next_char} == -1) {
933            !!!cp (11);
934            !!!emit ({type => END_OF_FILE_TOKEN,
935                      line => $self->{line}, column => $self->{column}});
936          last A; ## TODO: ok?          last A; ## TODO: ok?
937          } else {
938            !!!cp (12);
939        }        }
940        # Anything else        # Anything else
941        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
942                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
943                       line => $self->{line}, column => $self->{column},
944                      };
945        ## Stay in the data state        ## Stay in the data state
946        !!!next-input-character;        !!!next-input-character;
947    
948        !!!emit ($token);        !!!emit ($token);
949    
950        redo A;        redo A;
951      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
952        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
953    
954          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
955                
956        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
957    
958        $self->{state} = 'data';        $self->{state} = DATA_STATE;
959        # next-input-character is already done        # next-input-character is already done
960    
961        unless (defined $token) {        unless (defined $token) {
962          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
963            !!!emit ({type => CHARACTER_TOKEN, data => '&',
964                      line => $l, column => $c,
965                     });
966        } else {        } else {
967            !!!cp (14);
968          !!!emit ($token);          !!!emit ($token);
969        }        }
970    
971        redo A;        redo A;
972      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
973        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
974            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
975          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
976            !!!next-input-character;            !!!next-input-character;
977            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
978            redo A;            redo A;
979          } else {          } else {
980              !!!cp (16);
981            ## reconsume            ## reconsume
982            $self->{state} = 'data';            $self->{state} = DATA_STATE;
983    
984            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
985                        line => $self->{line_prev},
986                        column => $self->{column_prev},
987                       });
988    
989            redo A;            redo A;
990          }          }
991        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
992          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
993            $self->{state} = 'markup declaration open';            !!!cp (17);
994              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
995            !!!next-input-character;            !!!next-input-character;
996            redo A;            redo A;
997          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
998            $self->{state} = 'close tag open';            !!!cp (18);
999              $self->{state} = CLOSE_TAG_OPEN_STATE;
1000            !!!next-input-character;            !!!next-input-character;
1001            redo A;            redo A;
1002          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1003                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1004              !!!cp (19);
1005            $self->{current_token}            $self->{current_token}
1006              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1007                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1008            $self->{state} = 'tag name';                 line => $self->{line_prev},
1009                   column => $self->{column_prev}};
1010              $self->{state} = TAG_NAME_STATE;
1011            !!!next-input-character;            !!!next-input-character;
1012            redo A;            redo A;
1013          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1014                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1015            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1016                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1017            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1018                                        line => $self->{line_prev},
1019                                        column => $self->{column_prev}};
1020              $self->{state} = TAG_NAME_STATE;
1021            !!!next-input-character;            !!!next-input-character;
1022            redo A;            redo A;
1023          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1024            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1025            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1026                              line => $self->{line_prev},
1027                              column => $self->{column_prev});
1028              $self->{state} = DATA_STATE;
1029            !!!next-input-character;            !!!next-input-character;
1030    
1031            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1032                        line => $self->{line_prev},
1033                        column => $self->{column_prev},
1034                       });
1035    
1036            redo A;            redo A;
1037          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1038            !!!parse-error (type => 'pio');            !!!cp (22);
1039            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1040            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1041                              column => $self->{column_prev});
1042              $self->{state} = BOGUS_COMMENT_STATE;
1043              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1044                                        line => $self->{line_prev},
1045                                        column => $self->{column_prev},
1046                                       };
1047              ## $self->{next_char} is intentionally left as is
1048            redo A;            redo A;
1049          } else {          } else {
1050            !!!parse-error (type => 'bare stago');            !!!cp (23);
1051            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1052                              line => $self->{line_prev},
1053                              column => $self->{column_prev});
1054              $self->{state} = DATA_STATE;
1055            ## reconsume            ## reconsume
1056    
1057            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1058                        line => $self->{line_prev},
1059                        column => $self->{column_prev},
1060                       });
1061    
1062            redo A;            redo A;
1063          }          }
1064        } else {        } else {
1065          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1066        }        }
1067      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1068        if ($self->{content_model_flag} eq 'RCDATA' or        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1069            $self->{content_model_flag} eq 'CDATA') {        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1070          my @next_char;          if (defined $self->{last_emitted_start_tag_name}) {
1071          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
1072            push @next_char, $self->{next_input_character};            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1073            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);            my @next_char;
1074            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
1075            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              push @next_char, $self->{next_char};
1076              !!!next-input-character;              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1077              next TAGNAME;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1078            } else {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1079              !!!parse-error (type => 'unmatched end tag');                !!!cp (24);
1080              $self->{next_input_character} = shift @next_char; # reconsume                !!!next-input-character;
1081                  next TAGNAME;
1082                } else {
1083                  !!!cp (25);
1084                  $self->{next_char} = shift @next_char; # reconsume
1085                  !!!back-next-input-character (@next_char);
1086                  $self->{state} = DATA_STATE;
1087    
1088                  !!!emit ({type => CHARACTER_TOKEN, data => '</',
1089                            line => $l, column => $c,
1090                           });
1091      
1092                  redo A;
1093                }
1094              }
1095              push @next_char, $self->{next_char};
1096          
1097              unless ($self->{next_char} == 0x0009 or # HT
1098                      $self->{next_char} == 0x000A or # LF
1099                      $self->{next_char} == 0x000B or # VT
1100                      $self->{next_char} == 0x000C or # FF
1101                      $self->{next_char} == 0x0020 or # SP
1102                      $self->{next_char} == 0x003E or # >
1103                      $self->{next_char} == 0x002F or # /
1104                      $self->{next_char} == -1) {
1105                !!!cp (26);
1106                $self->{next_char} = shift @next_char; # reconsume
1107              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1108              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1109                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1110              !!!emit ({type => 'character', data => '</'});                        line => $l, column => $c,
1111                         });
1112              redo A;              redo A;
1113              } else {
1114                !!!cp (27);
1115                $self->{next_char} = shift @next_char;
1116                !!!back-next-input-character (@next_char);
1117                # and consume...
1118            }            }
         }  
         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;  
1119          } else {          } else {
1120            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
1121            !!!back-next-input-character (@next_char);            !!!cp (28);
1122            # and consume...            # next-input-character is already done
1123              $self->{state} = DATA_STATE;
1124              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1125                        line => $l, column => $c,
1126                       });
1127              redo A;
1128          }          }
1129        }        }
1130                
1131        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1132            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1133          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1134                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1135          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1136                   tag_name => chr ($self->{next_char} + 0x0020),
1137                   line => $l, column => $c};
1138            $self->{state} = TAG_NAME_STATE;
1139            !!!next-input-character;
1140            redo A;
1141          } elsif (0x0061 <= $self->{next_char} and
1142                   $self->{next_char} <= 0x007A) { # a..z
1143            !!!cp (30);
1144            $self->{current_token} = {type => END_TAG_TOKEN,
1145                                      tag_name => chr ($self->{next_char}),
1146                                      line => $l, column => $c};
1147            $self->{state} = TAG_NAME_STATE;
1148            !!!next-input-character;
1149            redo A;
1150          } elsif ($self->{next_char} == 0x003E) { # >
1151            !!!cp (31);
1152            !!!parse-error (type => 'empty end tag',
1153                            line => $self->{line_prev}, ## "<" in "</>"
1154                            column => $self->{column_prev} - 1);
1155            $self->{state} = DATA_STATE;
1156          !!!next-input-character;          !!!next-input-character;
1157          redo A;          redo A;
1158        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
1159                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (32);
         $self->{current_token} = {type => 'end tag',  
                           tag_name => chr ($self->{next_input_character})};  
         $self->{state} = 'tag name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         !!!parse-error (type => 'empty end tag');  
         $self->{state} = 'data';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1160          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1161          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1162          # reconsume          # reconsume
1163    
1164          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1165                      line => $l, column => $c,
1166                     });
1167    
1168          redo A;          redo A;
1169        } else {        } else {
1170            !!!cp (33);
1171          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1172          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1173          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1174          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1175        }                                    column => $self->{column_prev} - 1,
1176      } elsif ($self->{state} eq 'tag name') {                                   };
1177        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1178            $self->{next_input_character} == 0x000A or # LF          redo A;
1179            $self->{next_input_character} == 0x000B or # VT        }
1180            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1181            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1182          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1183          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1184          redo A;            $self->{next_char} == 0x000C or # FF
1185        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1186          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1187            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1188            !!!next-input-character;
1189            redo A;
1190          } elsif ($self->{next_char} == 0x003E) { # >
1191            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1192              !!!cp (35);
1193            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1194          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1195            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1196            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1197              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1198            }            #  !!! cp (36);
1199              #  !!! parse-error (type => 'end tag attribute');
1200              #} else {
1201                !!!cp (37);
1202              #}
1203          } else {          } else {
1204            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1205          }          }
1206          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1207          !!!next-input-character;          !!!next-input-character;
1208    
1209          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1210    
1211          redo A;          redo A;
1212        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1213                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1214          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1215            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1216            # start tag or end tag            # start tag or end tag
1217          ## Stay in this state          ## Stay in this state
1218          !!!next-input-character;          !!!next-input-character;
1219          redo A;          redo A;
1220        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1221          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1222          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1223              !!!cp (39);
1224            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1225          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1226            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1227            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1228              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1229            }            #  !!! cp (40);
1230              #  !!! parse-error (type => 'end tag attribute');
1231              #} else {
1232                !!!cp (41);
1233              #}
1234          } else {          } else {
1235            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1236          }          }
1237          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1238          # reconsume          # reconsume
1239    
1240          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1241    
1242          redo A;          redo A;
1243        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1244            !!!cp (42);
1245            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1246          !!!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  
1247          redo A;          redo A;
1248        } else {        } else {
1249          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1250            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1251            # start tag or end tag            # start tag or end tag
1252          ## Stay in the state          ## Stay in the state
1253          !!!next-input-character;          !!!next-input-character;
1254          redo A;          redo A;
1255        }        }
1256      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1257        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1258            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1259            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1260            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1261            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1262            !!!cp (45);
1263          ## Stay in the state          ## Stay in the state
1264          !!!next-input-character;          !!!next-input-character;
1265          redo A;          redo A;
1266        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1267          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1268              !!!cp (46);
1269            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1270          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1271            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1272            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1273                !!!cp (47);
1274              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1275              } else {
1276                !!!cp (48);
1277            }            }
1278          } else {          } else {
1279            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1280          }          }
1281          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1282          !!!next-input-character;          !!!next-input-character;
1283    
1284          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1285    
1286          redo A;          redo A;
1287        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1288                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1289          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1290                                value => ''};          $self->{current_attribute}
1291          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1292                   value => '',
1293                   line => $self->{line}, column => $self->{column}};
1294            $self->{state} = ATTRIBUTE_NAME_STATE;
1295            !!!next-input-character;
1296            redo A;
1297          } elsif ($self->{next_char} == 0x002F) { # /
1298            !!!cp (50);
1299            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1300          !!!next-input-character;          !!!next-input-character;
1301          redo A;          redo A;
1302        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003C or # <  
                $self->{next_input_character} == -1) {  
1303          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1304          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1305              !!!cp (52);
1306            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1307          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1308            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1309            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1310                !!!cp (53);
1311              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1312              } else {
1313                !!!cp (54);
1314            }            }
1315          } else {          } else {
1316            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1317          }          }
1318          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1319          # reconsume          # reconsume
1320    
1321          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1322    
1323          redo A;          redo A;
1324        } else {        } else {
1325          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1326                                value => ''};               0x0022 => 1, # "
1327          $self->{state} = 'attribute name';               0x0027 => 1, # '
1328                 0x003D => 1, # =
1329                }->{$self->{next_char}}) {
1330              !!!cp (55);
1331              !!!parse-error (type => 'bad attribute name');
1332            } else {
1333              !!!cp (56);
1334            }
1335            $self->{current_attribute}
1336                = {name => chr ($self->{next_char}),
1337                   value => '',
1338                   line => $self->{line}, column => $self->{column}};
1339            $self->{state} = ATTRIBUTE_NAME_STATE;
1340          !!!next-input-character;          !!!next-input-character;
1341          redo A;          redo A;
1342        }        }
1343      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1344        my $before_leave = sub {        my $before_leave = sub {
1345          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1346              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1347            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1348              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1349            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1350          } else {          } else {
1351              !!!cp (58);
1352            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1353              = $self->{current_attribute};              = $self->{current_attribute};
1354          }          }
1355        }; # $before_leave        }; # $before_leave
1356    
1357        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1358            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1359            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1360            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1361            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1362            !!!cp (59);
1363          $before_leave->();          $before_leave->();
1364          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1365          !!!next-input-character;          !!!next-input-character;
1366          redo A;          redo A;
1367        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1368            !!!cp (60);
1369          $before_leave->();          $before_leave->();
1370          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1371          !!!next-input-character;          !!!next-input-character;
1372          redo A;          redo A;
1373        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1374          $before_leave->();          $before_leave->();
1375          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1376              !!!cp (61);
1377            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1378          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1379            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
1380              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1381            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1382              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1383            }            }
1384          } else {          } else {
1385            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1386          }          }
1387          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1388          !!!next-input-character;          !!!next-input-character;
1389    
1390          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1391    
1392          redo A;          redo A;
1393        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1394                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1395          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1396            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1397          ## Stay in the state          ## Stay in the state
1398          !!!next-input-character;          !!!next-input-character;
1399          redo A;          redo A;
1400        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1401            !!!cp (64);
1402          $before_leave->();          $before_leave->();
1403            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1404          !!!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  
1405          redo A;          redo A;
1406        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1407          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1408          $before_leave->();          $before_leave->();
1409          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1410              !!!cp (66);
1411            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1412          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1413            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1414            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1415                !!!cp (67);
1416              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1417              } else {
1418                ## NOTE: This state should never be reached.
1419                !!!cp (68);
1420            }            }
1421          } else {          } else {
1422            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1423          }          }
1424          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1425          # reconsume          # reconsume
1426    
1427          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1428    
1429          redo A;          redo A;
1430        } else {        } else {
1431          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1432                $self->{next_char} == 0x0027) { # '
1433              !!!cp (69);
1434              !!!parse-error (type => 'bad attribute name');
1435            } else {
1436              !!!cp (70);
1437            }
1438            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1439          ## Stay in the state          ## Stay in the state
1440          !!!next-input-character;          !!!next-input-character;
1441          redo A;          redo A;
1442        }        }
1443      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1444        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1445            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1446            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1447            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1448            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1449            !!!cp (71);
1450          ## Stay in the state          ## Stay in the state
1451          !!!next-input-character;          !!!next-input-character;
1452          redo A;          redo A;
1453        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1454          $self->{state} = 'before attribute value';          !!!cp (72);
1455            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1456          !!!next-input-character;          !!!next-input-character;
1457          redo A;          redo A;
1458        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1459          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1460              !!!cp (73);
1461            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1462          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1463            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1464            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1465                !!!cp (74);
1466              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1467              } else {
1468                ## NOTE: This state should never be reached.
1469                !!!cp (75);
1470            }            }
1471          } else {          } else {
1472            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1473          }          }
1474          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1475          !!!next-input-character;          !!!next-input-character;
1476    
1477          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1478    
1479          redo A;          redo A;
1480        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1481                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1482          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1483                                value => ''};          $self->{current_attribute}
1484          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1485          !!!next-input-character;                 value => '',
1486          redo A;                 line => $self->{line}, column => $self->{column}};
1487        } elsif ($self->{next_input_character} == 0x002F) { # /          $self->{state} = ATTRIBUTE_NAME_STATE;
1488            !!!next-input-character;
1489            redo A;
1490          } elsif ($self->{next_char} == 0x002F) { # /
1491            !!!cp (77);
1492            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1493          !!!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  
1494          redo A;          redo A;
1495        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1496          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1497          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1498              !!!cp (79);
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 (80);
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 (81);
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          # reconsume          # reconsume
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        } else {        } else {
1519          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1520                                value => ''};          $self->{current_attribute}
1521          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char}),
1522                   value => '',
1523                   line => $self->{line}, column => $self->{column}};
1524            $self->{state} = ATTRIBUTE_NAME_STATE;
1525          !!!next-input-character;          !!!next-input-character;
1526          redo A;                  redo A;        
1527        }        }
1528      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1529        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1530            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1531            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1532            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1533            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1534            !!!cp (83);
1535          ## Stay in the state          ## Stay in the state
1536          !!!next-input-character;          !!!next-input-character;
1537          redo A;          redo A;
1538        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1539          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1540            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1541          !!!next-input-character;          !!!next-input-character;
1542          redo A;          redo A;
1543        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1544          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1545            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1546          ## reconsume          ## reconsume
1547          redo A;          redo A;
1548        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1549          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1550            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1551          !!!next-input-character;          !!!next-input-character;
1552          redo A;          redo A;
1553        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1554          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1555              !!!cp (87);
1556            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1557          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1558            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1559            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1560                !!!cp (88);
1561              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1562              } else {
1563                ## NOTE: This state should never be reached.
1564                !!!cp (89);
1565            }            }
1566          } else {          } else {
1567            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1568          }          }
1569          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1570          !!!next-input-character;          !!!next-input-character;
1571    
1572          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1573    
1574          redo A;          redo A;
1575        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1576          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1577          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1578              !!!cp (90);
1579            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1580          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1581            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1582            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1583                !!!cp (91);
1584              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1585              } else {
1586                ## NOTE: This state should never be reached.
1587                !!!cp (92);
1588            }            }
1589          } else {          } else {
1590            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1591          }          }
1592          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1593          ## reconsume          ## reconsume
1594    
1595          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1596    
1597          redo A;          redo A;
1598        } else {        } else {
1599          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1600          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1601              !!!parse-error (type => 'bad attribute value');
1602            } else {
1603              !!!cp (94);
1604            }
1605            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1606            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1607          !!!next-input-character;          !!!next-input-character;
1608          redo A;          redo A;
1609        }        }
1610      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1611        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1612          $self->{state} = 'before attribute name';          !!!cp (95);
1613            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1614          !!!next-input-character;          !!!next-input-character;
1615          redo A;          redo A;
1616        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1617          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1618          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1619            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1620          !!!next-input-character;          !!!next-input-character;
1621          redo A;          redo A;
1622        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1623          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1624          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1625              !!!cp (97);
1626            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1627          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1628            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1629            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1630                !!!cp (98);
1631              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1632              } else {
1633                ## NOTE: This state should never be reached.
1634                !!!cp (99);
1635            }            }
1636          } else {          } else {
1637            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1638          }          }
1639          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1640          ## reconsume          ## reconsume
1641    
1642          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1643    
1644          redo A;          redo A;
1645        } else {        } else {
1646          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1647            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1648          ## Stay in the state          ## Stay in the state
1649          !!!next-input-character;          !!!next-input-character;
1650          redo A;          redo A;
1651        }        }
1652      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1653        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1654          $self->{state} = 'before attribute name';          !!!cp (101);
1655            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1656          !!!next-input-character;          !!!next-input-character;
1657          redo A;          redo A;
1658        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1659          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1660          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1661            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1662          !!!next-input-character;          !!!next-input-character;
1663          redo A;          redo A;
1664        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1665          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1666          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1667              !!!cp (103);
1668            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1669          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1670            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1671            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1672                !!!cp (104);
1673              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1674              } else {
1675                ## NOTE: This state should never be reached.
1676                !!!cp (105);
1677            }            }
1678          } else {          } else {
1679            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1680          }          }
1681          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1682          ## reconsume          ## reconsume
1683    
1684          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1685    
1686          redo A;          redo A;
1687        } else {        } else {
1688          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1689            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1690          ## Stay in the state          ## Stay in the state
1691          !!!next-input-character;          !!!next-input-character;
1692          redo A;          redo A;
1693        }        }
1694      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1695        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1696            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1697            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1698            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1699            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1700          $self->{state} = 'before attribute name';          !!!cp (107);
1701          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1702          redo A;          !!!next-input-character;
1703        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1704          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1705          $self->{state} = 'entity in attribute value';          !!!cp (108);
1706          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1707          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1708        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1709          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1710          } elsif ($self->{next_char} == 0x003E) { # >
1711            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1712              !!!cp (109);
1713            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1714          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1715            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1716            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1717                !!!cp (110);
1718              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1719              } else {
1720                ## NOTE: This state should never be reached.
1721                !!!cp (111);
1722            }            }
1723          } else {          } else {
1724            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1725          }          }
1726          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1727          !!!next-input-character;          !!!next-input-character;
1728    
1729          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1730    
1731          redo A;          redo A;
1732        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1733          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1734          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1735              !!!cp (112);
1736            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1737          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1738            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1739            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1740                !!!cp (113);
1741              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1742              } else {
1743                ## NOTE: This state should never be reached.
1744                !!!cp (114);
1745            }            }
1746          } else {          } else {
1747            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1748          }          }
1749          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1750          ## reconsume          ## reconsume
1751    
1752          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1753    
1754          redo A;          redo A;
1755        } else {        } else {
1756          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1757                 0x0022 => 1, # "
1758                 0x0027 => 1, # '
1759                 0x003D => 1, # =
1760                }->{$self->{next_char}}) {
1761              !!!cp (115);
1762              !!!parse-error (type => 'bad attribute value');
1763            } else {
1764              !!!cp (116);
1765            }
1766            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1767          ## Stay in the state          ## Stay in the state
1768          !!!next-input-character;          !!!next-input-character;
1769          redo A;          redo A;
1770        }        }
1771      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1772        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity
1773              (1,
1774               $self->{last_attribute_value_state}
1775                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1776               $self->{last_attribute_value_state}
1777                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1778               -1);
1779    
1780        unless (defined $token) {        unless (defined $token) {
1781            !!!cp (117);
1782          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1783        } else {        } else {
1784            !!!cp (118);
1785          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1786            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1787          ## 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"
1788        }        }
1789    
1790        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1791        # next-input-character is already done        # next-input-character is already done
1792        redo A;        redo A;
1793      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1794          if ($self->{next_char} == 0x0009 or # HT
1795              $self->{next_char} == 0x000A or # LF
1796              $self->{next_char} == 0x000B or # VT
1797              $self->{next_char} == 0x000C or # FF
1798              $self->{next_char} == 0x0020) { # SP
1799            !!!cp (118);
1800            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1801            !!!next-input-character;
1802            redo A;
1803          } elsif ($self->{next_char} == 0x003E) { # >
1804            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1805              !!!cp (119);
1806              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1807            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1808              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1809              if ($self->{current_token}->{attributes}) {
1810                !!!cp (120);
1811                !!!parse-error (type => 'end tag attribute');
1812              } else {
1813                ## NOTE: This state should never be reached.
1814                !!!cp (121);
1815              }
1816            } else {
1817              die "$0: $self->{current_token}->{type}: Unknown token type";
1818            }
1819            $self->{state} = DATA_STATE;
1820            !!!next-input-character;
1821    
1822            !!!emit ($self->{current_token}); # start tag or end tag
1823    
1824            redo A;
1825          } elsif ($self->{next_char} == 0x002F) { # /
1826            !!!cp (122);
1827            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1828            !!!next-input-character;
1829            redo A;
1830          } else {
1831            !!!cp ('124.1');
1832            !!!parse-error (type => 'no space between attributes');
1833            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1834            ## reconsume
1835            redo A;
1836          }
1837        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1838          if ($self->{next_char} == 0x003E) { # >
1839            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1840              !!!cp ('124.2');
1841              !!!parse-error (type => 'nestc', token => $self->{current_token});
1842              ## TODO: Different type than slash in start tag
1843              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1844              if ($self->{current_token}->{attributes}) {
1845                !!!cp ('124.4');
1846                !!!parse-error (type => 'end tag attribute');
1847              } else {
1848                !!!cp ('124.5');
1849              }
1850              ## TODO: Test |<title></title/>|
1851            } else {
1852              !!!cp ('124.3');
1853              $self->{self_closing} = 1;
1854            }
1855    
1856            $self->{state} = DATA_STATE;
1857            !!!next-input-character;
1858    
1859            !!!emit ($self->{current_token}); # start tag or end tag
1860    
1861            redo A;
1862          } else {
1863            !!!cp ('124.4');
1864            !!!parse-error (type => 'nestc');
1865            ## TODO: This error type is wrong.
1866            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1867            ## Reconsume.
1868            redo A;
1869          }
1870        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1871        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1872                
1873        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1874          #my $token = {type => COMMENT_TOKEN, data => ''};
1875    
1876        BC: {        BC: {
1877          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1878            $self->{state} = 'data';            !!!cp (124);
1879              $self->{state} = DATA_STATE;
1880            !!!next-input-character;            !!!next-input-character;
1881    
1882            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1883    
1884            redo A;            redo A;
1885          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1886            $self->{state} = 'data';            !!!cp (125);
1887              $self->{state} = DATA_STATE;
1888            ## reconsume            ## reconsume
1889    
1890            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1891    
1892            redo A;            redo A;
1893          } else {          } else {
1894            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1895              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1896            !!!next-input-character;            !!!next-input-character;
1897            redo BC;            redo BC;
1898          }          }
1899        } # BC        } # BC
1900      } elsif ($self->{state} eq 'markup declaration open') {  
1901          die "$0: _get_next_token: unexpected case [BC]";
1902        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1903        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1904    
1905          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1906    
1907        my @next_char;        my @next_char;
1908        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1909                
1910        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1911          !!!next-input-character;          !!!next-input-character;
1912          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1913          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1914            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1915            $self->{state} = 'comment';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1916                                        line => $l, column => $c,
1917                                       };
1918              $self->{state} = COMMENT_START_STATE;
1919            !!!next-input-character;            !!!next-input-character;
1920            redo A;            redo A;
1921            } else {
1922              !!!cp (128);
1923          }          }
1924        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1925                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1926          !!!next-input-character;          !!!next-input-character;
1927          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1928          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1929              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1930            !!!next-input-character;            !!!next-input-character;
1931            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1932            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1933                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1934              !!!next-input-character;              !!!next-input-character;
1935              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1936              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1937                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1938                !!!next-input-character;                !!!next-input-character;
1939                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1940                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1941                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1942                  !!!next-input-character;                  !!!next-input-character;
1943                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1944                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1945                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1946                    !!!next-input-character;                    !!!next-input-character;
1947                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1948                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1949                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1950                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1951                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1952                        $self->{state} = DOCTYPE_STATE;
1953                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1954                                                  quirks => 1,
1955                                                  line => $l, column => $c,
1956                                                 };
1957                      !!!next-input-character;                      !!!next-input-character;
1958                      redo A;                      redo A;
1959                      } else {
1960                        !!!cp (130);
1961                    }                    }
1962                    } else {
1963                      !!!cp (131);
1964                  }                  }
1965                  } else {
1966                    !!!cp (132);
1967                }                }
1968                } else {
1969                  !!!cp (133);
1970              }              }
1971              } else {
1972                !!!cp (134);
1973            }            }
1974            } else {
1975              !!!cp (135);
1976          }          }
1977          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1978                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1979                   $self->{next_char} == 0x005B) { # [
1980            !!!next-input-character;
1981            push @next_char, $self->{next_char};
1982            if ($self->{next_char} == 0x0043) { # C
1983              !!!next-input-character;
1984              push @next_char, $self->{next_char};
1985              if ($self->{next_char} == 0x0044) { # D
1986                !!!next-input-character;
1987                push @next_char, $self->{next_char};
1988                if ($self->{next_char} == 0x0041) { # A
1989                  !!!next-input-character;
1990                  push @next_char, $self->{next_char};
1991                  if ($self->{next_char} == 0x0054) { # T
1992                    !!!next-input-character;
1993                    push @next_char, $self->{next_char};
1994                    if ($self->{next_char} == 0x0041) { # A
1995                      !!!next-input-character;
1996                      push @next_char, $self->{next_char};
1997                      if ($self->{next_char} == 0x005B) { # [
1998                        !!!cp (135.1);
1999                        $self->{state} = CDATA_BLOCK_STATE;
2000                        !!!next-input-character;
2001                        redo A;
2002                      } else {
2003                        !!!cp (135.2);
2004                      }
2005                    } else {
2006                      !!!cp (135.3);
2007                    }
2008                  } else {
2009                    !!!cp (135.4);                
2010                  }
2011                } else {
2012                  !!!cp (135.5);
2013                }
2014              } else {
2015                !!!cp (135.6);
2016              }
2017            } else {
2018              !!!cp (135.7);
2019            }
2020          } else {
2021            !!!cp (136);
2022        }        }
2023    
2024        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
2025        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2026        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2027        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
2028          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2029                                    line => $l, column => $c,
2030                                   };
2031        redo A;        redo A;
2032                
2033        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2034        ## 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?
2035      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
2036        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2037          $self->{state} = 'comment dash';          !!!cp (137);
2038            $self->{state} = COMMENT_START_DASH_STATE;
2039            !!!next-input-character;
2040            redo A;
2041          } elsif ($self->{next_char} == 0x003E) { # >
2042            !!!cp (138);
2043            !!!parse-error (type => 'bogus comment');
2044            $self->{state} = DATA_STATE;
2045            !!!next-input-character;
2046    
2047            !!!emit ($self->{current_token}); # comment
2048    
2049            redo A;
2050          } elsif ($self->{next_char} == -1) {
2051            !!!cp (139);
2052            !!!parse-error (type => 'unclosed comment');
2053            $self->{state} = DATA_STATE;
2054            ## reconsume
2055    
2056            !!!emit ($self->{current_token}); # comment
2057    
2058            redo A;
2059          } else {
2060            !!!cp (140);
2061            $self->{current_token}->{data} # comment
2062                .= chr ($self->{next_char});
2063            $self->{state} = COMMENT_STATE;
2064            !!!next-input-character;
2065            redo A;
2066          }
2067        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2068          if ($self->{next_char} == 0x002D) { # -
2069            !!!cp (141);
2070            $self->{state} = COMMENT_END_STATE;
2071            !!!next-input-character;
2072            redo A;
2073          } elsif ($self->{next_char} == 0x003E) { # >
2074            !!!cp (142);
2075            !!!parse-error (type => 'bogus comment');
2076            $self->{state} = DATA_STATE;
2077            !!!next-input-character;
2078    
2079            !!!emit ($self->{current_token}); # comment
2080    
2081            redo A;
2082          } elsif ($self->{next_char} == -1) {
2083            !!!cp (143);
2084            !!!parse-error (type => 'unclosed comment');
2085            $self->{state} = DATA_STATE;
2086            ## reconsume
2087    
2088            !!!emit ($self->{current_token}); # comment
2089    
2090            redo A;
2091          } else {
2092            !!!cp (144);
2093            $self->{current_token}->{data} # comment
2094                .= '-' . chr ($self->{next_char});
2095            $self->{state} = COMMENT_STATE;
2096          !!!next-input-character;          !!!next-input-character;
2097          redo A;          redo A;
2098        } elsif ($self->{next_input_character} == -1) {        }
2099        } elsif ($self->{state} == COMMENT_STATE) {
2100          if ($self->{next_char} == 0x002D) { # -
2101            !!!cp (145);
2102            $self->{state} = COMMENT_END_DASH_STATE;
2103            !!!next-input-character;
2104            redo A;
2105          } elsif ($self->{next_char} == -1) {
2106            !!!cp (146);
2107          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2108          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2109          ## reconsume          ## reconsume
2110    
2111          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2112    
2113          redo A;          redo A;
2114        } else {        } else {
2115          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2116            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2117          ## Stay in the state          ## Stay in the state
2118          !!!next-input-character;          !!!next-input-character;
2119          redo A;          redo A;
2120        }        }
2121      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2122        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2123          $self->{state} = 'comment end';          !!!cp (148);
2124            $self->{state} = COMMENT_END_STATE;
2125          !!!next-input-character;          !!!next-input-character;
2126          redo A;          redo A;
2127        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2128            !!!cp (149);
2129          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2130          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2131          ## reconsume          ## reconsume
2132    
2133          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2134    
2135          redo A;          redo A;
2136        } else {        } else {
2137          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2138          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2139            $self->{state} = COMMENT_STATE;
2140          !!!next-input-character;          !!!next-input-character;
2141          redo A;          redo A;
2142        }        }
2143      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2144        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2145          $self->{state} = 'data';          !!!cp (151);
2146            $self->{state} = DATA_STATE;
2147          !!!next-input-character;          !!!next-input-character;
2148    
2149          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2150    
2151          redo A;          redo A;
2152        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2153          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2154            !!!parse-error (type => 'dash in comment',
2155                            line => $self->{line_prev},
2156                            column => $self->{column_prev});
2157          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2158          ## Stay in the state          ## Stay in the state
2159          !!!next-input-character;          !!!next-input-character;
2160          redo A;          redo A;
2161        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2162            !!!cp (153);
2163          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2164          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2165          ## reconsume          ## reconsume
2166    
2167          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2168    
2169          redo A;          redo A;
2170        } else {        } else {
2171          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2172          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2173          $self->{state} = 'comment';                          line => $self->{line_prev},
2174                            column => $self->{column_prev});
2175            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2176            $self->{state} = COMMENT_STATE;
2177          !!!next-input-character;          !!!next-input-character;
2178          redo A;          redo A;
2179        }        }
2180      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2181        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2182            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2183            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2184            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2185            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2186          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2187            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2188          !!!next-input-character;          !!!next-input-character;
2189          redo A;          redo A;
2190        } else {        } else {
2191            !!!cp (156);
2192          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2193          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2194          ## reconsume          ## reconsume
2195          redo A;          redo A;
2196        }        }
2197      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2198        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2199            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2200            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2201            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2202            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2203            !!!cp (157);
2204          ## Stay in the state          ## Stay in the state
2205          !!!next-input-character;          !!!next-input-character;
2206          redo A;          redo A;
2207        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
2208                 $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) { # >  
2209          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2210          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2211          !!!next-input-character;          !!!next-input-character;
2212    
2213          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2214    
2215          redo A;          redo A;
2216        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2217            !!!cp (159);
2218          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2219          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2220          ## reconsume          ## reconsume
2221    
2222          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2223    
2224          redo A;          redo A;
2225        } else {        } else {
2226          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (160);
2227                            name => chr ($self->{next_input_character}),          $self->{current_token}->{name} = chr $self->{next_char};
2228                            error => 1};          delete $self->{current_token}->{quirks};
2229  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2230          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2231          !!!next-input-character;          !!!next-input-character;
2232          redo A;          redo A;
2233        }        }
2234      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2235        if ($self->{next_input_character} == 0x0009 or # HT  ## ISSUE: Redundant "First," in the spec.
2236            $self->{next_input_character} == 0x000A or # LF        if ($self->{next_char} == 0x0009 or # HT
2237            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000A or # LF
2238            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000B or # VT
2239            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x000C or # FF
2240          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE            $self->{next_char} == 0x0020) { # SP
2241          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2242            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2243          !!!next-input-character;          !!!next-input-character;
2244          redo A;          redo A;
2245        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2246          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (162);
2247          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2248          !!!next-input-character;          !!!next-input-character;
2249    
2250          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2251    
2252          redo A;          redo A;
2253        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
2254                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (163);
2255          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed DOCTYPE');
2256          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{state} = DATA_STATE;
2257            ## reconsume
2258    
2259            $self->{current_token}->{quirks} = 1;
2260            !!!emit ($self->{current_token}); # DOCTYPE
2261    
2262            redo A;
2263          } else {
2264            !!!cp (164);
2265            $self->{current_token}->{name}
2266              .= chr ($self->{next_char}); # DOCTYPE
2267          ## Stay in the state          ## Stay in the state
2268          !!!next-input-character;          !!!next-input-character;
2269          redo A;          redo A;
2270        } elsif ($self->{next_input_character} == -1) {        }
2271        } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2272          if ($self->{next_char} == 0x0009 or # HT
2273              $self->{next_char} == 0x000A or # LF
2274              $self->{next_char} == 0x000B or # VT
2275              $self->{next_char} == 0x000C or # FF
2276              $self->{next_char} == 0x0020) { # SP
2277            !!!cp (165);
2278            ## Stay in the state
2279            !!!next-input-character;
2280            redo A;
2281          } elsif ($self->{next_char} == 0x003E) { # >
2282            !!!cp (166);
2283            $self->{state} = DATA_STATE;
2284            !!!next-input-character;
2285    
2286            !!!emit ($self->{current_token}); # DOCTYPE
2287    
2288            redo A;
2289          } elsif ($self->{next_char} == -1) {
2290            !!!cp (167);
2291          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2292          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
2293          ## reconsume          ## reconsume
2294    
2295          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
2296          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
2297    
2298          redo A;          redo A;
2299          } elsif ($self->{next_char} == 0x0050 or # P
2300                   $self->{next_char} == 0x0070) { # p
2301            !!!next-input-character;
2302            if ($self->{next_char} == 0x0055 or # U
2303                $self->{next_char} == 0x0075) { # u
2304              !!!next-input-character;
2305              if ($self->{next_char} == 0x0042 or # B
2306                  $self->{next_char} == 0x0062) { # b
2307                !!!next-input-character;
2308                if ($self->{next_char} == 0x004C or # L
2309                    $self->{next_char} == 0x006C) { # l
2310                  !!!next-input-character;
2311                  if ($self->{next_char} == 0x0049 or # I
2312                      $self->{next_char} == 0x0069) { # i
2313                    !!!next-input-character;
2314                    if ($self->{next_char} == 0x0043 or # C
2315                        $self->{next_char} == 0x0063) { # c
2316                      !!!cp (168);
2317                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2318                      !!!next-input-character;
2319                      redo A;
2320                    } else {
2321                      !!!cp (169);
2322                    }
2323                  } else {
2324                    !!!cp (170);
2325                  }
2326                } else {
2327                  !!!cp (171);
2328                }
2329              } else {
2330                !!!cp (172);
2331              }
2332            } else {
2333              !!!cp (173);
2334            }
2335    
2336            #
2337          } elsif ($self->{next_char} == 0x0053 or # S
2338                   $self->{next_char} == 0x0073) { # s
2339            !!!next-input-character;
2340            if ($self->{next_char} == 0x0059 or # Y
2341                $self->{next_char} == 0x0079) { # y
2342              !!!next-input-character;
2343              if ($self->{next_char} == 0x0053 or # S
2344                  $self->{next_char} == 0x0073) { # s
2345                !!!next-input-character;
2346                if ($self->{next_char} == 0x0054 or # T
2347                    $self->{next_char} == 0x0074) { # t
2348                  !!!next-input-character;
2349                  if ($self->{next_char} == 0x0045 or # E
2350                      $self->{next_char} == 0x0065) { # e
2351                    !!!next-input-character;
2352                    if ($self->{next_char} == 0x004D or # M
2353                        $self->{next_char} == 0x006D) { # m
2354                      !!!cp (174);
2355                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2356                      !!!next-input-character;
2357                      redo A;
2358                    } else {
2359                      !!!cp (175);
2360                    }
2361                  } else {
2362                    !!!cp (176);
2363                  }
2364                } else {
2365                  !!!cp (177);
2366                }
2367              } else {
2368                !!!cp (178);
2369              }
2370            } else {
2371              !!!cp (179);
2372            }
2373    
2374            #
2375        } else {        } else {
2376          $self->{current_token}->{name}          !!!cp (180);
2377            .= chr ($self->{next_input_character}); # DOCTYPE          !!!next-input-character;
2378          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          #
2379          }
2380    
2381          !!!parse-error (type => 'string after DOCTYPE name');
2382          $self->{current_token}->{quirks} = 1;
2383    
2384          $self->{state} = BOGUS_DOCTYPE_STATE;
2385          # next-input-character is already done
2386          redo A;
2387        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2388          if ({
2389                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2390                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2391              }->{$self->{next_char}}) {
2392            !!!cp (181);
2393            ## Stay in the state
2394            !!!next-input-character;
2395            redo A;
2396          } elsif ($self->{next_char} eq 0x0022) { # "
2397            !!!cp (182);
2398            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2399            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2400            !!!next-input-character;
2401            redo A;
2402          } elsif ($self->{next_char} eq 0x0027) { # '
2403            !!!cp (183);
2404            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2405            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2406            !!!next-input-character;
2407            redo A;
2408          } elsif ($self->{next_char} eq 0x003E) { # >
2409            !!!cp (184);
2410            !!!parse-error (type => 'no PUBLIC literal');
2411    
2412            $self->{state} = DATA_STATE;
2413            !!!next-input-character;
2414    
2415            $self->{current_token}->{quirks} = 1;
2416            !!!emit ($self->{current_token}); # DOCTYPE
2417    
2418            redo A;
2419          } elsif ($self->{next_char} == -1) {
2420            !!!cp (185);
2421            !!!parse-error (type => 'unclosed DOCTYPE');
2422    
2423            $self->{state} = DATA_STATE;
2424            ## reconsume
2425    
2426            $self->{current_token}->{quirks} = 1;
2427            !!!emit ($self->{current_token}); # DOCTYPE
2428    
2429            redo A;
2430          } else {
2431            !!!cp (186);
2432            !!!parse-error (type => 'string after PUBLIC');
2433            $self->{current_token}->{quirks} = 1;
2434    
2435            $self->{state} = BOGUS_DOCTYPE_STATE;
2436            !!!next-input-character;
2437            redo A;
2438          }
2439        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2440          if ($self->{next_char} == 0x0022) { # "
2441            !!!cp (187);
2442            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2443            !!!next-input-character;
2444            redo A;
2445          } elsif ($self->{next_char} == 0x003E) { # >
2446            !!!cp (188);
2447            !!!parse-error (type => 'unclosed PUBLIC literal');
2448    
2449            $self->{state} = DATA_STATE;
2450            !!!next-input-character;
2451    
2452            $self->{current_token}->{quirks} = 1;
2453            !!!emit ($self->{current_token}); # DOCTYPE
2454    
2455            redo A;
2456          } elsif ($self->{next_char} == -1) {
2457            !!!cp (189);
2458            !!!parse-error (type => 'unclosed PUBLIC literal');
2459    
2460            $self->{state} = DATA_STATE;
2461            ## reconsume
2462    
2463            $self->{current_token}->{quirks} = 1;
2464            !!!emit ($self->{current_token}); # DOCTYPE
2465    
2466            redo A;
2467          } else {
2468            !!!cp (190);
2469            $self->{current_token}->{public_identifier} # DOCTYPE
2470                .= chr $self->{next_char};
2471            ## Stay in the state
2472            !!!next-input-character;
2473            redo A;
2474          }
2475        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2476          if ($self->{next_char} == 0x0027) { # '
2477            !!!cp (191);
2478            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2479            !!!next-input-character;
2480            redo A;
2481          } elsif ($self->{next_char} == 0x003E) { # >
2482            !!!cp (192);
2483            !!!parse-error (type => 'unclosed PUBLIC literal');
2484    
2485            $self->{state} = DATA_STATE;
2486            !!!next-input-character;
2487    
2488            $self->{current_token}->{quirks} = 1;
2489            !!!emit ($self->{current_token}); # DOCTYPE
2490    
2491            redo A;
2492          } elsif ($self->{next_char} == -1) {
2493            !!!cp (193);
2494            !!!parse-error (type => 'unclosed PUBLIC literal');
2495    
2496            $self->{state} = DATA_STATE;
2497            ## reconsume
2498    
2499            $self->{current_token}->{quirks} = 1;
2500            !!!emit ($self->{current_token}); # DOCTYPE
2501    
2502            redo A;
2503          } else {
2504            !!!cp (194);
2505            $self->{current_token}->{public_identifier} # DOCTYPE
2506                .= chr $self->{next_char};
2507            ## Stay in the state
2508            !!!next-input-character;
2509            redo A;
2510          }
2511        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2512          if ({
2513                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2514                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2515              }->{$self->{next_char}}) {
2516            !!!cp (195);
2517            ## Stay in the state
2518            !!!next-input-character;
2519            redo A;
2520          } elsif ($self->{next_char} == 0x0022) { # "
2521            !!!cp (196);
2522            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2523            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2524            !!!next-input-character;
2525            redo A;
2526          } elsif ($self->{next_char} == 0x0027) { # '
2527            !!!cp (197);
2528            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2529            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2530            !!!next-input-character;
2531            redo A;
2532          } elsif ($self->{next_char} == 0x003E) { # >
2533            !!!cp (198);
2534            $self->{state} = DATA_STATE;
2535            !!!next-input-character;
2536    
2537            !!!emit ($self->{current_token}); # DOCTYPE
2538    
2539            redo A;
2540          } elsif ($self->{next_char} == -1) {
2541            !!!cp (199);
2542            !!!parse-error (type => 'unclosed DOCTYPE');
2543    
2544            $self->{state} = DATA_STATE;
2545            ## reconsume
2546    
2547            $self->{current_token}->{quirks} = 1;
2548            !!!emit ($self->{current_token}); # DOCTYPE
2549    
2550            redo A;
2551          } else {
2552            !!!cp (200);
2553            !!!parse-error (type => 'string after PUBLIC literal');
2554            $self->{current_token}->{quirks} = 1;
2555    
2556            $self->{state} = BOGUS_DOCTYPE_STATE;
2557            !!!next-input-character;
2558            redo A;
2559          }
2560        } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2561          if ({
2562                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2563                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2564              }->{$self->{next_char}}) {
2565            !!!cp (201);
2566            ## Stay in the state
2567            !!!next-input-character;
2568            redo A;
2569          } elsif ($self->{next_char} == 0x0022) { # "
2570            !!!cp (202);
2571            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2572            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2573            !!!next-input-character;
2574            redo A;
2575          } elsif ($self->{next_char} == 0x0027) { # '
2576            !!!cp (203);
2577            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2578            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2579            !!!next-input-character;
2580            redo A;
2581          } elsif ($self->{next_char} == 0x003E) { # >
2582            !!!cp (204);
2583            !!!parse-error (type => 'no SYSTEM literal');
2584            $self->{state} = DATA_STATE;
2585            !!!next-input-character;
2586    
2587            $self->{current_token}->{quirks} = 1;
2588            !!!emit ($self->{current_token}); # DOCTYPE
2589    
2590            redo A;
2591          } elsif ($self->{next_char} == -1) {
2592            !!!cp (205);
2593            !!!parse-error (type => 'unclosed DOCTYPE');
2594    
2595            $self->{state} = DATA_STATE;
2596            ## reconsume
2597    
2598            $self->{current_token}->{quirks} = 1;
2599            !!!emit ($self->{current_token}); # DOCTYPE
2600    
2601            redo A;
2602          } else {
2603            !!!cp (206);
2604            !!!parse-error (type => 'string after SYSTEM');
2605            $self->{current_token}->{quirks} = 1;
2606    
2607            $self->{state} = BOGUS_DOCTYPE_STATE;
2608            !!!next-input-character;
2609            redo A;
2610          }
2611        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2612          if ($self->{next_char} == 0x0022) { # "
2613            !!!cp (207);
2614            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2615            !!!next-input-character;
2616            redo A;
2617          } elsif ($self->{next_char} == 0x003E) { # >
2618            !!!cp (208);
2619            !!!parse-error (type => 'unclosed PUBLIC literal');
2620    
2621            $self->{state} = DATA_STATE;
2622            !!!next-input-character;
2623    
2624            $self->{current_token}->{quirks} = 1;
2625            !!!emit ($self->{current_token}); # DOCTYPE
2626    
2627            redo A;
2628          } elsif ($self->{next_char} == -1) {
2629            !!!cp (209);
2630            !!!parse-error (type => 'unclosed SYSTEM literal');
2631    
2632            $self->{state} = DATA_STATE;
2633            ## reconsume
2634    
2635            $self->{current_token}->{quirks} = 1;
2636            !!!emit ($self->{current_token}); # DOCTYPE
2637    
2638            redo A;
2639          } else {
2640            !!!cp (210);
2641            $self->{current_token}->{system_identifier} # DOCTYPE
2642                .= chr $self->{next_char};
2643          ## Stay in the state          ## Stay in the state
2644          !!!next-input-character;          !!!next-input-character;
2645          redo A;          redo A;
2646        }        }
2647      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2648        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0027) { # '
2649            $self->{next_input_character} == 0x000A or # LF          !!!cp (211);
2650            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2651            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
2652            $self->{next_input_character} == 0x0020) { # SP          redo A;
2653          } elsif ($self->{next_char} == 0x003E) { # >
2654            !!!cp (212);
2655            !!!parse-error (type => 'unclosed PUBLIC literal');
2656    
2657            $self->{state} = DATA_STATE;
2658            !!!next-input-character;
2659    
2660            $self->{current_token}->{quirks} = 1;
2661            !!!emit ($self->{current_token}); # DOCTYPE
2662    
2663            redo A;
2664          } elsif ($self->{next_char} == -1) {
2665            !!!cp (213);
2666            !!!parse-error (type => 'unclosed SYSTEM literal');
2667    
2668            $self->{state} = DATA_STATE;
2669            ## reconsume
2670    
2671            $self->{current_token}->{quirks} = 1;
2672            !!!emit ($self->{current_token}); # DOCTYPE
2673    
2674            redo A;
2675          } else {
2676            !!!cp (214);
2677            $self->{current_token}->{system_identifier} # DOCTYPE
2678                .= chr $self->{next_char};
2679          ## Stay in the state          ## Stay in the state
2680          !!!next-input-character;          !!!next-input-character;
2681          redo A;          redo A;
2682        } elsif ($self->{next_input_character} == 0x003E) { # >        }
2683          $self->{state} = 'data';      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2684          if ({
2685                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2686                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2687              }->{$self->{next_char}}) {
2688            !!!cp (215);
2689            ## Stay in the state
2690            !!!next-input-character;
2691            redo A;
2692          } elsif ($self->{next_char} == 0x003E) { # >
2693            !!!cp (216);
2694            $self->{state} = DATA_STATE;
2695          !!!next-input-character;          !!!next-input-character;
2696    
2697          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2698    
2699          redo A;          redo A;
2700        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2701            !!!cp (217);
2702          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2703          $self->{state} = 'data';  
2704            $self->{state} = DATA_STATE;
2705          ## reconsume          ## reconsume
2706    
2707            $self->{current_token}->{quirks} = 1;
2708          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2709    
2710          redo A;          redo A;
2711        } else {        } else {
2712          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (218);
2713          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after SYSTEM literal');
2714          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2715    
2716            $self->{state} = BOGUS_DOCTYPE_STATE;
2717          !!!next-input-character;          !!!next-input-character;
2718          redo A;          redo A;
2719        }        }
2720      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2721        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2722          $self->{state} = 'data';          !!!cp (219);
2723            $self->{state} = DATA_STATE;
2724          !!!next-input-character;          !!!next-input-character;
2725    
2726          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2727    
2728          redo A;          redo A;
2729        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2730            !!!cp (220);
2731          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2732          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2733          ## reconsume          ## reconsume
2734    
2735          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2736    
2737          redo A;          redo A;
2738        } else {        } else {
2739            !!!cp (221);
2740          ## Stay in the state          ## Stay in the state
2741          !!!next-input-character;          !!!next-input-character;
2742          redo A;          redo A;
2743        }        }
2744        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2745          my $s = '';
2746          
2747          my ($l, $c) = ($self->{line}, $self->{column});
2748    
2749          CS: while ($self->{next_char} != -1) {
2750            if ($self->{next_char} == 0x005D) { # ]
2751              !!!next-input-character;
2752              if ($self->{next_char} == 0x005D) { # ]
2753                !!!next-input-character;
2754                MDC: {
2755                  if ($self->{next_char} == 0x003E) { # >
2756                    !!!cp (221.1);
2757                    !!!next-input-character;
2758                    last CS;
2759                  } elsif ($self->{next_char} == 0x005D) { # ]
2760                    !!!cp (221.2);
2761                    $s .= ']';
2762                    !!!next-input-character;
2763                    redo MDC;
2764                  } else {
2765                    !!!cp (221.3);
2766                    $s .= ']]';
2767                    #
2768                  }
2769                } # MDC
2770              } else {
2771                !!!cp (221.4);
2772                $s .= ']';
2773                #
2774              }
2775            } else {
2776              !!!cp (221.5);
2777              #
2778            }
2779            $s .= chr $self->{next_char};
2780            !!!next-input-character;
2781          } # CS
2782    
2783          $self->{state} = DATA_STATE;
2784          ## next-input-character done or EOF, which is reconsumed.
2785    
2786          if (length $s) {
2787            !!!cp (221.6);
2788            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2789                      line => $l, column => $c});
2790          } else {
2791            !!!cp (221.7);
2792          }
2793    
2794          redo A;
2795    
2796          ## ISSUE: "text tokens" in spec.
2797          ## TODO: Streaming support
2798      } else {      } else {
2799        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2800      }      }
# Line 1490  sub _get_next_token ($) { Line 2803  sub _get_next_token ($) {
2803    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2804  } # _get_next_token  } # _get_next_token
2805    
2806  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2807    my $self = shift;    my ($self, $in_attr, $additional) = @_;
2808      
2809    if ($self->{next_input_character} == 0x0023) { # #    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2810    
2811      if ({
2812           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2813           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2814           $additional => 1,
2815          }->{$self->{next_char}}) {
2816        !!!cp (1001);
2817        ## Don't consume
2818        ## No error
2819        return undef;
2820      } elsif ($self->{next_char} == 0x0023) { # #
2821      !!!next-input-character;      !!!next-input-character;
2822      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2823          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2824        my $num;        my $code;
2825        X: {        X: {
2826          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2827          !!!next-input-character;          !!!next-input-character;
2828          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2829              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2830            $num ||= 0;            !!!cp (1002);
2831            $num *= 0x10;            $code ||= 0;
2832            $num += $self->{next_input_character} - 0x0030;            $code *= 0x10;
2833              $code += $self->{next_char} - 0x0030;
2834            redo X;            redo X;
2835          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2836                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2837            ## ISSUE: the spec says U+0078, which is apparently incorrect            !!!cp (1003);
2838            $num ||= 0;            $code ||= 0;
2839            $num *= 0x10;            $code *= 0x10;
2840            $num += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2841            redo X;            redo X;
2842          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2843                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2844            ## ISSUE: the spec says U+0058, which is apparently incorrect            !!!cp (1004);
2845            $num ||= 0;            $code ||= 0;
2846            $num *= 0x10;            $code *= 0x10;
2847            $num += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2848            redo X;            redo X;
2849          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2850            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2851            $self->{next_input_character} = 0x0023; # #            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2852            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char, $self->{next_char});
2853              $self->{next_char} = 0x0023; # #
2854            return undef;            return undef;
2855          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2856              !!!cp (1006);
2857            !!!next-input-character;            !!!next-input-character;
2858          } else {          } else {
2859            !!!parse-error (type => 'no refc');            !!!cp (1007);
2860          }            !!!parse-error (type => 'no refc', line => $l, column => $c);
   
         ## TODO: check the definition for |a valid Unicode character|.  
         ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>  
         if ($num > 1114111 or $num == 0) {  
           $num = 0xFFFD; # REPLACEMENT CHARACTER  
           ## ISSUE: Why this is not an error?  
         } elsif (0x80 <= $num and $num <= 0x9F) {  
           ## NOTE: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>  
           ## ISSUE: Not in the spec yet; parse error?  
           $num = $c1_entity_char->{$num};  
2861          }          }
2862    
2863          return {type => 'character', data => chr $num};          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2864              !!!cp (1008);
2865              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2866              $code = 0xFFFD;
2867            } elsif ($code > 0x10FFFF) {
2868              !!!cp (1009);
2869              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2870              $code = 0xFFFD;
2871            } elsif ($code == 0x000D) {
2872              !!!cp (1010);
2873              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2874              $code = 0x000A;
2875            } elsif (0x80 <= $code and $code <= 0x9F) {
2876              !!!cp (1011);
2877              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2878              $code = $c1_entity_char->{$code};
2879            }
2880    
2881            return {type => CHARACTER_TOKEN, data => chr $code,
2882                    has_reference => 1,
2883                    line => $l, column => $c,
2884                   };
2885        } # X        } # X
2886      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2887               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2888        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2889        !!!next-input-character;        !!!next-input-character;
2890                
2891        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2892                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2893            !!!cp (1012);
2894          $code *= 10;          $code *= 10;
2895          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2896                    
2897          !!!next-input-character;          !!!next-input-character;
2898        }        }
2899    
2900        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2901            !!!cp (1013);
2902          !!!next-input-character;          !!!next-input-character;
2903        } else {        } else {
2904          !!!parse-error (type => 'no refc');          !!!cp (1014);
2905            !!!parse-error (type => 'no refc', line => $l, column => $c);
2906        }        }
2907    
2908        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2909        if ($code > 1114111 or $code == 0) {          !!!cp (1015);
2910          $code = 0xFFFD; # REPLACEMENT CHARACTER          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2911          ## ISSUE: Why this is not an error?          $code = 0xFFFD;
2912          } elsif ($code > 0x10FFFF) {
2913            !!!cp (1016);
2914            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2915            $code = 0xFFFD;
2916          } elsif ($code == 0x000D) {
2917            !!!cp (1017);
2918            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2919            $code = 0x000A;
2920        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2921          ## NOTE: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>          !!!cp (1018);
2922          ## ISSUE: Not in the spec yet; parse error?          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2923          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2924        }        }
2925                
2926        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2927                  line => $l, column => $c,
2928                 };
2929      } else {      } else {
2930        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2931        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2932        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2933          $self->{next_char} = 0x0023; # #
2934        return undef;        return undef;
2935      }      }
2936    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2937              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2938             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2939              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2940      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2941      !!!next-input-character;      !!!next-input-character;
2942    
2943      my $value = $entity_name;      my $value = $entity_name;
2944      my $match;      my $match = 0;
2945        require Whatpm::_NamedEntityList;
2946        our $EntityChar;
2947    
2948      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2949             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2950             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_char} and # a
2951               $self->{next_input_character} <= 0x005A) or               $self->{next_char} <= 0x005A) or # x
2952              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_char} and # a
2953               $self->{next_input_character} <= 0x007A) or               $self->{next_char} <= 0x007A) or # z
2954              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_char} and # 0
2955               $self->{next_input_character} <= 0x0039))) {               $self->{next_char} <= 0x0039) or # 9
2956        $entity_name .= chr $self->{next_input_character};              $self->{next_char} == 0x003B)) { # ;
2957        if (defined $entity_char->{$entity_name}) {        $entity_name .= chr $self->{next_char};
2958          $value = $entity_char->{$entity_name};        if (defined $EntityChar->{$entity_name}) {
2959          $match = 1;          if ($self->{next_char} == 0x003B) { # ;
2960              !!!cp (1020);
2961              $value = $EntityChar->{$entity_name};
2962              $match = 1;
2963              !!!next-input-character;
2964              last;
2965            } else {
2966              !!!cp (1021);
2967              $value = $EntityChar->{$entity_name};
2968              $match = -1;
2969              !!!next-input-character;
2970            }
2971        } else {        } else {
2972          $value .= chr $self->{next_input_character};          !!!cp (1022);
2973            $value .= chr $self->{next_char};
2974            $match *= 2;
2975            !!!next-input-character;
2976        }        }
       !!!next-input-character;  
2977      }      }
2978            
2979      if ($match) {      if ($match > 0) {
2980        if ($self->{next_input_character} == 0x003B) { # ;        !!!cp (1023);
2981          !!!next-input-character;        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2982                  line => $l, column => $c,
2983                 };
2984        } elsif ($match < 0) {
2985          !!!parse-error (type => 'no refc', line => $l, column => $c);
2986          if ($in_attr and $match < -1) {
2987            !!!cp (1024);
2988            return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2989                    line => $l, column => $c,
2990                   };
2991        } else {        } else {
2992          !!!parse-error (type => 'refc');          !!!cp (1025);
2993            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2994                    line => $l, column => $c,
2995                   };
2996        }        }
   
       return {type => 'character', data => $value};  
2997      } else {      } else {
2998        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2999        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3000        !!!back-token ({type => 'character', data => $value});        ## NOTE: "No characters are consumed" in the spec.
3001        return undef;        return {type => CHARACTER_TOKEN, data => '&'.$value,
3002                  line => $l, column => $c,
3003                 };
3004      }      }
3005    } else {    } else {
3006        !!!cp (1027);
3007      ## no characters are consumed      ## no characters are consumed
3008      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3009      return undef;      return undef;
3010    }    }
3011  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1636  sub _initialize_tree_constructor ($) { Line 3016  sub _initialize_tree_constructor ($) {
3016    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
3017    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3018    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3019    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
3020  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3021    
3022  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1663  sub _construct_tree ($) { Line 3043  sub _construct_tree ($) {
3043        
3044    !!!next-token;    !!!next-token;
3045    
   $self->{insertion_mode} = 'before head';  
3046    undef $self->{form_element};    undef $self->{form_element};
3047    undef $self->{head_element};    undef $self->{head_element};
3048    $self->{open_elements} = [];    $self->{open_elements} = [];
3049    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3050    
3051      ## NOTE: The "initial" insertion mode.
3052    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3053    
3054      ## NOTE: The "before html" insertion mode.
3055    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3056      $self->{insertion_mode} = BEFORE_HEAD_IM;
3057    
3058      ## NOTE: The "before head" insertion mode and so on.
3059    $self->_tree_construction_main;    $self->_tree_construction_main;
3060  } # _construct_tree  } # _construct_tree
3061    
3062  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3063    my $self = shift;    my $self = shift;
3064    B: {  
3065        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
3066          if ($token->{error}) {  
3067            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
3068            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
3069          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3070          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
3071            ($token->{name});        ## language.
3072          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
3073          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
3074          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/;
3075          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
3076          return;            defined $token->{public_identifier} or
3077        } elsif ({            defined $token->{system_identifier}) {
3078                  comment => 1,          !!!cp ('t1');
3079                  'start tag' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3080                  'end tag' => 1,        } elsif ($doctype_name ne 'HTML') {
3081                  'end-of-file' => 1,          !!!cp ('t2');
3082                 }->{$token->{type}}) {          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3083          ## ISSUE: Spec currently left this case undefined.          !!!parse-error (type => 'not HTML5', token => $token);
3084          !!!parse-error (type => 'missing DOCTYPE');        } else {
3085          #$phase = 'root element';          !!!cp ('t3');
3086          ## reprocess        }
3087          #redo B;        
3088          return;        my $doctype = $self->{document}->create_document_type_definition
3089        } elsif ($token->{type} eq 'character') {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3090          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        ## NOTE: Default value for both |public_id| and |system_id| attributes
3091            $self->{document}->manakai_append_text ($1);        ## are empty strings, so that we don't set any value in missing cases.
3092            ## ISSUE: DOM3 Core does not allow Document > Text        $doctype->public_id ($token->{public_identifier})
3093            unless (length $token->{data}) {            if defined $token->{public_identifier};
3094              ## Stay in the phase        $doctype->system_id ($token->{system_identifier})
3095              !!!next-token;            if defined $token->{system_identifier};
3096              redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
3097          ## ISSUE: internalSubset = null??
3098          $self->{document}->append_child ($doctype);
3099          
3100          if ($token->{quirks} or $doctype_name ne 'HTML') {
3101            !!!cp ('t4');
3102            $self->{document}->manakai_compat_mode ('quirks');
3103          } elsif (defined $token->{public_identifier}) {
3104            my $pubid = $token->{public_identifier};
3105            $pubid =~ tr/a-z/A-z/;
3106            if ({
3107              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
3108              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
3109              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
3110              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
3111              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
3112              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
3113              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
3114              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
3115              "-//IETF//DTD HTML 2.0//EN" => 1,
3116              "-//IETF//DTD HTML 2.1E//EN" => 1,
3117              "-//IETF//DTD HTML 3.0//EN" => 1,
3118              "-//IETF//DTD HTML 3.0//EN//" => 1,
3119              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
3120              "-//IETF//DTD HTML 3.2//EN" => 1,
3121              "-//IETF//DTD HTML 3//EN" => 1,
3122              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
3123              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
3124              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
3125              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
3126              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
3127              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
3128              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
3129              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
3130              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
3131              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
3132              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
3133              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
3134              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
3135              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
3136              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
3137              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
3138              "-//IETF//DTD HTML STRICT//EN" => 1,
3139              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
3140              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
3141              "-//IETF//DTD HTML//EN" => 1,
3142              "-//IETF//DTD HTML//EN//2.0" => 1,
3143              "-//IETF//DTD HTML//EN//3.0" => 1,
3144              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
3145              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
3146              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
3147              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
3148              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
3149              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
3150              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
3151              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
3152              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
3153              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
3154              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
3155              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
3156              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
3157              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
3158              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
3159              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
3160              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
3161              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
3162              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
3163              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
3164              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
3165              "-//W3C//DTD HTML 3.2//EN" => 1,
3166              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
3167              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
3168              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
3169              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
3170              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
3171              "-//W3C//DTD W3 HTML//EN" => 1,
3172              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
3173              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
3174              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
3175              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
3176              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
3177              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
3178              "HTML" => 1,
3179            }->{$pubid}) {
3180              !!!cp ('t5');
3181              $self->{document}->manakai_compat_mode ('quirks');
3182            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
3183                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
3184              if (defined $token->{system_identifier}) {
3185                !!!cp ('t6');
3186                $self->{document}->manakai_compat_mode ('quirks');
3187              } else {
3188                !!!cp ('t7');
3189                $self->{document}->manakai_compat_mode ('limited quirks');
3190            }            }
3191            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
3192                     $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
3193              !!!cp ('t8');
3194              $self->{document}->manakai_compat_mode ('limited quirks');
3195            } else {
3196              !!!cp ('t9');
3197          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
3198        } else {        } else {
3199          die "$0: $token->{type}: Unknown token";          !!!cp ('t10');
3200        }        }
3201      } # B        if (defined $token->{system_identifier}) {
3202            my $sysid = $token->{system_identifier};
3203            $sysid =~ tr/A-Z/a-z/;
3204            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3205              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
3206              $self->{document}->manakai_compat_mode ('quirks');
3207              !!!cp ('t11');
3208            } else {
3209              !!!cp ('t12');
3210            }
3211          } else {
3212            !!!cp ('t13');
3213          }
3214          
3215          ## Go to the "before html" insertion mode.
3216          !!!next-token;
3217          return;
3218        } elsif ({
3219                  START_TAG_TOKEN, 1,
3220                  END_TAG_TOKEN, 1,
3221                  END_OF_FILE_TOKEN, 1,
3222                 }->{$token->{type}}) {
3223          !!!cp ('t14');
3224          !!!parse-error (type => 'no DOCTYPE', token => $token);
3225          $self->{document}->manakai_compat_mode ('quirks');
3226          ## Go to the "before html" insertion mode.
3227          ## reprocess
3228          !!!ack-later;
3229          return;
3230        } elsif ($token->{type} == CHARACTER_TOKEN) {
3231          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3232            ## Ignore the token
3233    
3234            unless (length $token->{data}) {
3235              !!!cp ('t15');
3236              ## Stay in the insertion mode.
3237              !!!next-token;
3238              redo INITIAL;
3239            } else {
3240              !!!cp ('t16');
3241            }
3242          } else {
3243            !!!cp ('t17');
3244          }
3245    
3246          !!!parse-error (type => 'no DOCTYPE', token => $token);
3247          $self->{document}->manakai_compat_mode ('quirks');
3248          ## Go to the "before html" insertion mode.
3249          ## reprocess
3250          return;
3251        } elsif ($token->{type} == COMMENT_TOKEN) {
3252          !!!cp ('t18');
3253          my $comment = $self->{document}->create_comment ($token->{data});
3254          $self->{document}->append_child ($comment);
3255          
3256          ## Stay in the insertion mode.
3257          !!!next-token;
3258          redo INITIAL;
3259        } else {
3260          die "$0: $token->{type}: Unknown token type";
3261        }
3262      } # INITIAL
3263    
3264      die "$0: _tree_construction_initial: This should be never reached";
3265  } # _tree_construction_initial  } # _tree_construction_initial
3266    
3267  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3268    my $self = shift;    my $self = shift;
3269    
3270      ## NOTE: "before html" insertion mode.
3271        
3272    B: {    B: {
3273        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3274          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3275            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3276          ## Ignore the token          ## Ignore the token
3277          ## Stay in the phase          ## Stay in the insertion mode.
3278          !!!next-token;          !!!next-token;
3279          redo B;          redo B;
3280        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3281            !!!cp ('t20');
3282          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3283          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3284          ## Stay in the phase          ## Stay in the insertion mode.
3285          !!!next-token;          !!!next-token;
3286          redo B;          redo B;
3287        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3288          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3289            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
3290            ## ISSUE: DOM3 Core does not allow Document > Text  
3291            unless (length $token->{data}) {            unless (length $token->{data}) {
3292              ## Stay in the phase              !!!cp ('t21');
3293                ## Stay in the insertion mode.
3294              !!!next-token;              !!!next-token;
3295              redo B;              redo B;
3296              } else {
3297                !!!cp ('t22');
3298            }            }
3299            } else {
3300              !!!cp ('t23');
3301          }          }
3302    
3303            $self->{application_cache_selection}->(undef);
3304    
3305          #          #
3306          } elsif ($token->{type} == START_TAG_TOKEN) {
3307            if ($token->{tag_name} eq 'html') {
3308              my $root_element;
3309              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3310              $self->{document}->append_child ($root_element);
3311              push @{$self->{open_elements}},
3312                  [$root_element, $el_category->{html}];
3313    
3314              if ($token->{attributes}->{manifest}) {
3315                !!!cp ('t24');
3316                $self->{application_cache_selection}
3317                    ->($token->{attributes}->{manifest}->{value});
3318                ## ISSUE: Spec is unclear on relative references.
3319                ## According to Hixie (#whatwg 2008-03-19), it should be
3320                ## resolved against the base URI of the document in HTML
3321                ## or xml:base of the element in XHTML.
3322              } else {
3323                !!!cp ('t25');
3324                $self->{application_cache_selection}->(undef);
3325              }
3326    
3327              !!!nack ('t25c');
3328    
3329              !!!next-token;
3330              return; ## Go to the "before head" insertion mode.
3331            } else {
3332              !!!cp ('t25.1');
3333              #
3334            }
3335        } elsif ({        } elsif ({
3336                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3337                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3338                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3339          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3340          #          #
3341        } else {        } else {
3342          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3343        }        }
3344        my $root_element; !!!create-element ($root_element, 'html');  
3345        $self->{document}->append_child ($root_element);      my $root_element;
3346        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3347        #$phase = 'main';      $self->{document}->append_child ($root_element);
3348        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3349        #redo B;  
3350        return;      $self->{application_cache_selection}->(undef);
3351    
3352        ## NOTE: Reprocess the token.
3353        !!!ack-later;
3354        return; ## Go to the "before head" insertion mode.
3355    
3356        ## ISSUE: There is an issue in the spec
3357    } # B    } # B
3358    
3359      die "$0: _tree_construction_root_element: This should never be reached";
3360  } # _tree_construction_root_element  } # _tree_construction_root_element
3361    
3362  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1782  sub _reset_insertion_mode ($) { Line 3371  sub _reset_insertion_mode ($) {
3371            
3372      ## Step 3      ## Step 3
3373      S3: {      S3: {
3374        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3375        if (defined $self->{inner_html_node}) {          $last = 1;
3376          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
3377              $self->{inner_html_node}->[1] eq 'th') {            !!!cp ('t28');
3378              $node = $self->{inner_html_node};
3379            } else {
3380              die "_reset_insertion_mode: t27";
3381            }
3382          }
3383          
3384          ## Step 4..14
3385          my $new_mode;
3386          if ($node->[1] & FOREIGN_EL) {
3387            !!!cp ('t28.1');
3388            ## NOTE: Strictly spaking, the line below only applies to MathML and
3389            ## SVG elements.  Currently the HTML syntax supports only MathML and
3390            ## SVG elements as foreigners.
3391            $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3392            ## ISSUE: What is set as the secondary insertion mode?
3393          } elsif ($node->[1] & TABLE_CELL_EL) {
3394            if ($last) {
3395              !!!cp ('t28.2');
3396            #            #
3397          } else {          } else {
3398            $node = $self->{inner_html_node};            !!!cp ('t28.3');
3399              $new_mode = IN_CELL_IM;
3400          }          }
3401          } else {
3402            !!!cp ('t28.4');
3403            $new_mode = {
3404                          select => IN_SELECT_IM,
3405                          ## NOTE: |option| and |optgroup| do not set
3406                          ## insertion mode to "in select" by themselves.
3407                          tr => IN_ROW_IM,
3408                          tbody => IN_TABLE_BODY_IM,
3409                          thead => IN_TABLE_BODY_IM,
3410                          tfoot => IN_TABLE_BODY_IM,
3411                          caption => IN_CAPTION_IM,
3412                          colgroup => IN_COLUMN_GROUP_IM,
3413                          table => IN_TABLE_IM,
3414                          head => IN_BODY_IM, # not in head!
3415                          body => IN_BODY_IM,
3416                          frameset => IN_FRAMESET_IM,
3417                         }->{$node->[0]->manakai_local_name};
3418        }        }
       
       ## Step 4..13  
       my $new_mode = {  
                       select => 'in select',  
                       td => 'in cell',  
                       th => 'in cell',  
                       tr => 'in row',  
                       tbody => 'in table body',  
                       thead => 'in table head',  
                       tfoot => 'in table foot',  
                       caption => 'in caption',  
                       colgroup => 'in column group',  
                       table => 'in table',  
                       head => 'in body', # not in head!  
                       body => 'in body',  
                       frameset => 'in frameset',  
                      }->{$node->[1]};  
3419        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3420                
3421        ## Step 14        ## Step 15
3422        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3423          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3424            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3425              $self->{insertion_mode} = BEFORE_HEAD_IM;
3426          } else {          } else {
3427            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3428              !!!cp ('t30');
3429              $self->{insertion_mode} = AFTER_HEAD_IM;
3430          }          }
3431          return;          return;
3432          } else {
3433            !!!cp ('t31');
3434        }        }
3435                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3436        ## Step 16        ## Step 16
3437          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3438          
3439          ## Step 17
3440        $i--;        $i--;
3441        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3442                
3443        ## Step 17        ## Step 18
3444        redo S3;        redo S3;
3445      } # S3      } # S3
3446    
3447      die "$0: _reset_insertion_mode: This line should never be reached";
3448  } # _reset_insertion_mode  } # _reset_insertion_mode
3449    
3450  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3451    my $self = shift;    my $self = shift;
3452    
   my $phase = 'main';  
   
3453    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3454    
3455    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1853  sub _tree_construction_main ($) { Line 3466  sub _tree_construction_main ($) {
3466      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3467      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3468        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3469            !!!cp ('t32');
3470          return;          return;
3471        }        }
3472      }      }
# Line 1867  sub _tree_construction_main ($) { Line 3481  sub _tree_construction_main ($) {
3481    
3482        ## Step 6        ## Step 6
3483        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3484            !!!cp ('t33_1');
3485          #          #
3486        } else {        } else {
3487          my $in_open_elements;          my $in_open_elements;
3488          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3489            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3490                !!!cp ('t33');
3491              $in_open_elements = 1;              $in_open_elements = 1;
3492              last OE;              last OE;
3493            }            }
3494          }          }
3495          if ($in_open_elements) {          if ($in_open_elements) {
3496              !!!cp ('t34');
3497            #            #
3498          } else {          } else {
3499              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3500              !!!cp ('t35');
3501            redo S4;            redo S4;
3502          }          }
3503        }        }
# Line 1901  sub _tree_construction_main ($) { Line 3520  sub _tree_construction_main ($) {
3520    
3521        ## Step 11        ## Step 11
3522        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3523            !!!cp ('t36');
3524          ## Step 7'          ## Step 7'
3525          $i++;          $i++;
3526          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3527                    
3528          redo S7;          redo S7;
3529        }        }
3530    
3531          !!!cp ('t37');
3532      } # S7      } # S7
3533    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3534    
3535    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3536      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3537        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3538            !!!cp ('t38');
3539          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3540          return;          return;
3541        }        }
3542      }      }
3543    
3544        !!!cp ('t39');
3545    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3546    
3547    my $style_start_tag = sub {    my $insert;
3548      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});  
3549      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
3550      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
3551       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
3552        ->append_child ($style_el);      ## Step 1
3553      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
3554                      my $el;
3555        !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3556    
3557        ## Step 2
3558        $insert->($el);
3559    
3560        ## Step 3
3561        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3562        delete $self->{escape}; # MUST
3563    
3564        ## Step 4
3565      my $text = '';      my $text = '';
3566        !!!nack ('t40.1');
3567      !!!next-token;      !!!next-token;
3568      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3569          !!!cp ('t40');
3570        $text .= $token->{data};        $text .= $token->{data};
3571        !!!next-token;        !!!next-token;
3572      } # stop if non-character token or tokenizer stops tokenising      }
3573    
3574        ## Step 5
3575      if (length $text) {      if (length $text) {
3576        $style_el->manakai_append_text ($text);        !!!cp ('t41');
3577          my $text = $self->{document}->create_text_node ($text);
3578          $el->append_child ($text);
3579      }      }
3580        
3581      $self->{content_model_flag} = 'PCDATA';      ## Step 6
3582                      $self->{content_model} = PCDATA_CONTENT_MODEL;
3583      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
3584        ## Step 7
3585        if ($token->{type} == END_TAG_TOKEN and
3586            $token->{tag_name} eq $start_tag_name) {
3587          !!!cp ('t42');
3588        ## Ignore the token        ## Ignore the token
3589      } else {      } else {
3590        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
3591        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3592            !!!cp ('t43');
3593            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3594          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3595            !!!cp ('t44');
3596            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3597          } else {
3598            die "$0: $content_model_flag in parse_rcdata";
3599          }
3600      }      }
3601      !!!next-token;      !!!next-token;
3602    }; # $style_start_tag    }; # $parse_rcdata
3603    
3604    my $script_start_tag = sub {    my $script_start_tag = sub () {
3605      my $script_el;      my $script_el;
3606      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3607      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3608    
3609      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3610        delete $self->{escape}; # MUST
3611            
3612      my $text = '';      my $text = '';
3613        !!!nack ('t45.1');
3614      !!!next-token;      !!!next-token;
3615      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3616          !!!cp ('t45');
3617        $text .= $token->{data};        $text .= $token->{data};
3618        !!!next-token;        !!!next-token;
3619      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3620      if (length $text) {      if (length $text) {
3621          !!!cp ('t46');
3622        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3623      }      }
3624                                
3625      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3626    
3627      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3628          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3629          !!!cp ('t47');
3630        ## Ignore the token        ## Ignore the token
3631      } else {      } else {
3632        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3633          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3634        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3635        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3636      }      }
3637            
3638      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3639          !!!cp ('t49');
3640        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3641      } else {      } else {
3642          !!!cp ('t50');
3643        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3644        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3645          
3646        (($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);  
3647                
3648        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
3649                
# Line 1993  sub _tree_construction_main ($) { Line 3653  sub _tree_construction_main ($) {
3653      !!!next-token;      !!!next-token;
3654    }; # $script_start_tag    }; # $script_start_tag
3655    
3656      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3657      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3658      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3659    
3660    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3661      my $tag_name = shift;      my $end_tag_token = shift;
3662        my $tag_name = $end_tag_token->{tag_name};
3663    
3664        ## NOTE: The adoption agency algorithm (AAA).
3665    
3666      FET: {      FET: {
3667        ## Step 1        ## Step 1
3668        my $formatting_element;        my $formatting_element;
3669        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3670        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3671          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3672              !!!cp ('t52');
3673              last AFE;
3674            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3675                         eq $tag_name) {
3676              !!!cp ('t51');
3677            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3678            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3679            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3680          }          }
3681        } # AFE        } # AFE
3682        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3683          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3684            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3685          ## Ignore the token          ## Ignore the token
3686          !!!next-token;          !!!next-token;
3687          return;          return;
# Line 2022  sub _tree_construction_main ($) { Line 3693  sub _tree_construction_main ($) {
3693          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3694          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3695            if ($in_scope) {            if ($in_scope) {
3696                !!!cp ('t54');
3697              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3698              last INSCOPE;              last INSCOPE;
3699            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3700              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3701                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3702                                token => $end_tag_token);
3703              ## Ignore the token              ## Ignore the token
3704              !!!next-token;              !!!next-token;
3705              return;              return;
3706            }            }
3707          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3708                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3709            $in_scope = 0;            $in_scope = 0;
3710          }          }
3711        } # INSCOPE        } # INSCOPE
3712        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3713          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3714            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3715                            token => $end_tag_token);
3716          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3717          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3718          return;          return;
3719        }        }
3720        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3721          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3722            !!!parse-error (type => 'not closed',
3723                            value => $self->{open_elements}->[-1]->[0]
3724                                ->manakai_local_name,
3725                            token => $end_tag_token);
3726        }        }
3727                
3728        ## Step 2        ## Step 2
# Line 2052  sub _tree_construction_main ($) { Line 3730  sub _tree_construction_main ($) {
3730        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3731        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3732          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3733          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3734              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3735              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3736               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3737              !!!cp ('t59');
3738            $furthest_block = $node;            $furthest_block = $node;
3739            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3740          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3741              !!!cp ('t60');
3742            last OE;            last OE;
3743          }          }
3744        } # OE        } # OE
3745                
3746        ## Step 3        ## Step 3
3747        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3748            !!!cp ('t61');
3749          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3750          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3751          !!!next-token;          !!!next-token;
# Line 2077  sub _tree_construction_main ($) { Line 3758  sub _tree_construction_main ($) {
3758        ## Step 5        ## Step 5
3759        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3760        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3761            !!!cp ('t62');
3762          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3763        }        }
3764                
# Line 2099  sub _tree_construction_main ($) { Line 3781  sub _tree_construction_main ($) {
3781          S7S2: {          S7S2: {
3782            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3783              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3784                  !!!cp ('t63');
3785                $node_i_in_active = $_;                $node_i_in_active = $_;
3786                last S7S2;                last S7S2;
3787              }              }
# Line 2112  sub _tree_construction_main ($) { Line 3795  sub _tree_construction_main ($) {
3795                    
3796          ## Step 4          ## Step 4
3797          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3798              !!!cp ('t64');
3799            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3800          }          }
3801                    
3802          ## Step 5          ## Step 5
3803          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3804              !!!cp ('t65');
3805            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3806            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3807            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2134  sub _tree_construction_main ($) { Line 3819  sub _tree_construction_main ($) {
3819        } # S7          } # S7  
3820                
3821        ## Step 8        ## Step 8
3822        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3823            my $foster_parent_element;
3824            my $next_sibling;
3825            OE: for (reverse 0..$#{$self->{open_elements}}) {
3826              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3827                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3828                                 if (defined $parent and $parent->node_type == 1) {
3829                                   !!!cp ('t65.1');
3830                                   $foster_parent_element = $parent;
3831                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3832                                 } else {
3833                                   !!!cp ('t65.2');
3834                                   $foster_parent_element
3835                                     = $self->{open_elements}->[$_ - 1]->[0];
3836                                 }
3837                                 last OE;
3838                               }
3839                             } # OE
3840                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3841                               unless defined $foster_parent_element;
3842            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3843            $open_tables->[-1]->[1] = 1; # tainted
3844          } else {
3845            !!!cp ('t65.3');
3846            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3847          }
3848                
3849        ## Step 9        ## Step 9
3850        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2151  sub _tree_construction_main ($) { Line 3861  sub _tree_construction_main ($) {
3861        my $i;        my $i;
3862        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3863          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3864              !!!cp ('t66');
3865            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3866            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3867          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3868              !!!cp ('t67');
3869            $i = $_;            $i = $_;
3870          }          }
3871        } # AFE        } # AFE
# Line 2163  sub _tree_construction_main ($) { Line 3875  sub _tree_construction_main ($) {
3875        undef $i;        undef $i;
3876        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3877          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3878              !!!cp ('t68');
3879            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3880            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3881          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3882              !!!cp ('t69');
3883            $i = $_;            $i = $_;
3884          }          }
3885        } # OE        } # OE
# Line 2176  sub _tree_construction_main ($) { Line 3890  sub _tree_construction_main ($) {
3890      } # FET      } # FET
3891    }; # $formatting_end_tag    }; # $formatting_end_tag
3892    
3893    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3894      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3895    }; # $insert_to_current    }; # $insert_to_current
3896    
3897    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3898                         my $child = shift;      my $child = shift;
3899                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3900                              table => 1, tbody => 1, tfoot => 1,        # MUST
3901                              thead => 1, tr => 1,        my $foster_parent_element;
3902                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3903                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3904                           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') {  
3905                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3906                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3907                                   !!!cp ('t70');
3908                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3909                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3910                               } else {                               } else {
3911                                   !!!cp ('t71');
3912                                 $foster_parent_element                                 $foster_parent_element
3913                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3914                               }                               }
# Line 2206  sub _tree_construction_main ($) { Line 3919  sub _tree_construction_main ($) {
3919                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3920                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3921                             ($child, $next_sibling);                             ($child, $next_sibling);
3922                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3923                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3924                         }        !!!cp ('t72');
3925          $self->{open_elements}->[-1]->[0]->append_child ($child);
3926        }
3927    }; # $insert_to_foster    }; # $insert_to_foster
3928    
3929    my $in_body = sub {    B: while (1) {
3930      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
3931      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
3932        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3933          $script_start_tag->();        ## Ignore the token
3934          return;        ## Stay in the phase
3935        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
3936          $style_start_tag->();        next B;
3937          return;      } elsif ($token->{type} == START_TAG_TOKEN and
3938        } elsif ({               $token->{tag_name} eq 'html') {
3939                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3940                 }->{$token->{tag_name}}) {          !!!cp ('t79');
3941          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html:html', token => $token);
3942          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
3943          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3944          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
3945          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html:html', token => $token);
3946            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3947          } else {        } else {
3948            $insert->($el);          !!!cp ('t81');
3949          }        }
3950            
3951          !!!next-token;        !!!cp ('t82');
3952          return;        !!!parse-error (type => 'not first start tag', token => $token);
3953        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
3954          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
3955          ## NOTE: There is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3956          my $title_el;            !!!cp ('t84');
3957          !!!create-element ($title_el, 'title', $token->{attributes});            $top_el->set_attribute_ns
3958          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              (undef, [undef, $attr_name],
3959            ->append_child ($title_el);               $token->{attributes}->{$attr_name}->{value});
         $self->{content_model_flag} = 'RCDATA';  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
3960          }          }
3961                    }
3962          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!nack ('t84.1');
3963                    !!!next-token;
3964          next B;
3965        } elsif ($token->{type} == COMMENT_TOKEN) {
3966          my $comment = $self->{document}->create_comment ($token->{data});
3967          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3968            !!!cp ('t85');
3969            $self->{document}->append_child ($comment);
3970          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3971            !!!cp ('t86');
3972            $self->{open_elements}->[0]->[0]->append_child ($comment);
3973          } else {
3974            !!!cp ('t87');
3975            $self->{open_elements}->[-1]->[0]->append_child ($comment);
3976          }
3977          !!!next-token;
3978          next B;
3979        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3980          if ($token->{type} == CHARACTER_TOKEN) {
3981            !!!cp ('t87.1');
3982            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3983          !!!next-token;          !!!next-token;
3984          return;          next B;
3985        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3986          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3987            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3988            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3989              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
3990                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3991              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
3992              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
3993              $formatting_end_tag->($token->{tag_name});            #
3994                        } elsif ({
3995              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3996                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3997                  splice @$active_formatting_elements, $_, 1;                    embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3998                  last AFE2;                    h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3999                }                    li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
4000              } # AFE2                    ruby => 1, s => 1, small => 1, span => 1, strong => 1,
4001              OE: for (reverse 0..$#{$self->{open_elements}}) {                    sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
4002                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    var => 1,
4003                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4004                  last OE;            !!!cp ('t87.2');
4005                }            !!!parse-error (type => 'not closed',
4006              } # OE                            value => $self->{open_elements}->[-1]->[0]
4007              last AFE;                                ->manakai_local_name,
4008            } elsif ($node->[0] eq '#marker') {                            token => $token);
4009              last AFE;  
4010              pop @{$self->{open_elements}}
4011                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4012    
4013              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4014              ## Reprocess.
4015              next B;
4016            } else {
4017              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4018              my $tag_name = $token->{tag_name};
4019              if ($nsuri eq $SVG_NS) {
4020                $tag_name = {
4021                   altglyph => 'altGlyph',
4022                   altglyphdef => 'altGlyphDef',
4023                   altglyphitem => 'altGlyphItem',
4024                   animatecolor => 'animateColor',
4025                   animatemotion => 'animateMotion',
4026                   animatetransform => 'animateTransform',
4027                   clippath => 'clipPath',
4028                   feblend => 'feBlend',
4029                   fecolormatrix => 'feColorMatrix',
4030                   fecomponenttransfer => 'feComponentTransfer',
4031                   fecomposite => 'feComposite',
4032                   feconvolvematrix => 'feConvolveMatrix',
4033                   fediffuselighting => 'feDiffuseLighting',
4034                   fedisplacementmap => 'feDisplacementMap',
4035                   fedistantlight => 'feDistantLight',
4036                   feflood => 'feFlood',
4037                   fefunca => 'feFuncA',
4038                   fefuncb => 'feFuncB',
4039                   fefuncg => 'feFuncG',
4040                   fefuncr => 'feFuncR',
4041                   fegaussianblur => 'feGaussianBlur',
4042                   feimage => 'feImage',
4043                   femerge => 'feMerge',
4044                   femergenode => 'feMergeNode',
4045                   femorphology => 'feMorphology',
4046                   feoffset => 'feOffset',
4047                   fepointlight => 'fePointLight',
4048                   fespecularlighting => 'feSpecularLighting',
4049                   fespotlight => 'feSpotLight',
4050                   fetile => 'feTile',
4051                   feturbulence => 'feTurbulence',
4052                   foreignobject => 'foreignObject',
4053                   glyphref => 'glyphRef',
4054                   lineargradient => 'linearGradient',
4055                   radialgradient => 'radialGradient',
4056                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4057                   textpath => 'textPath',  
4058                }->{$tag_name} || $tag_name;
4059            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4060    
4061          !!!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];  
4062    
4063          !!!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', ''];  
4064    
4065          !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4066          return;  
4067        } elsif ($token->{tag_name} eq 'marquee' or            if ($self->{self_closing}) {
4068                 $token->{tag_name} eq 'object') {              pop @{$self->{open_elements}};
4069          $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});  
4070            } else {            } else {
4071              !!!parse-error (type => 'in RCDATA:#'.$token->{type});              !!!cp ('t87.4');
4072            }            }
4073            ## ISSUE: And ignore?  
4074              !!!next-token;
4075              next B;
4076          }          }
4077          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4078          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4079        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
4080          $reconstruct_active_formatting_elements->($insert_to_current);          #
4081                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4082          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4083                    !!!cp ('t87.6');
4084          $self->{insertion_mode} = 'in select';          #
4085          !!!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.  
4086        } else {        } else {
4087          $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;  
4088        }        }
4089      } elsif ($token->{type} eq 'end tag') {      }
4090        if ($token->{tag_name} eq 'body') {  
4091          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      if ($self->{insertion_mode} & HEAD_IMS) {
4092            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
4093            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4094              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4095            }              !!!cp ('t88.2');
4096            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4097            !!!next-token;            } else {
4098            return;              !!!cp ('t88.1');
4099          } else {              ## Ignore the token.
4100            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
4101            ## 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;  
4102            }            }
4103          } # INSCOPE            unless (length $token->{data}) {
4104                        !!!cp ('t88');
4105          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
4106            !!!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;  
4107            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4108          }          }
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
4109    
4110              !!!next-token;          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4111              last S2;            !!!cp ('t89');
4112            } else {            ## As if <head>
4113              ## Step 3            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4114              if (not $formatting_category->{$node->[1]} and            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4115                  #not $phrasing_category->{$node->[1]} and            push @{$self->{open_elements}},
4116                  ($special_category->{$node->[1]} or                [$self->{head_element}, $el_category->{head}];
4117                   $scoping_category->{$node->[1]})) {  
4118                !!!parse-error (type => 'not closed:'.$node->[1]);            ## Reprocess in the "in head" insertion mode...
4119                ## Ignore the token            pop @{$self->{open_elements}};
4120                !!!next-token;  
4121                last S2;            ## Reprocess in the "after head" insertion mode...
4122              }          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4123            }            !!!cp ('t90');
4124              ## As if </noscript>
4125              pop @{$self->{open_elements}};
4126              !!!parse-error (type => 'in noscript:#character', token => $token);
4127                        
4128            ## Step 4            ## Reprocess in the "in head" insertion mode...
4129            $node_i--;            ## As if </head>
4130            $node = $self->{open_elements}->[$node_i];            pop @{$self->{open_elements}};
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
4131    
4132    B: {            ## Reprocess in the "after head" insertion mode...
4133      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4134        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
4135          !!!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]);  
         }  
4136    
4137          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4138          last B;          } else {
4139              !!!cp ('t92');
4140            }
4141    
4142          ## ISSUE: There is an issue in the spec.          ## "after head" insertion mode
4143        } else {          ## As if <body>
4144          if ($self->{insertion_mode} eq 'before head') {          !!!insert-element ('body',, $token);
4145            if ($token->{type} eq 'character') {          $self->{insertion_mode} = IN_BODY_IM;
4146              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## reprocess
4147                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          next B;
4148                unless (length $token->{data}) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4149                  !!!next-token;          if ($token->{tag_name} eq 'head') {
4150                  redo B;            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4151                }              !!!cp ('t93');
4152              }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4153              ## As if <head>              $self->{open_elements}->[-1]->[0]->append_child
4154              !!!create-element ($self->{head_element}, 'head');                  ($self->{head_element});
4155              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              push @{$self->{open_elements}},
4156              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  [$self->{head_element}, $el_category->{head}];
4157              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = IN_HEAD_IM;
4158              ## 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);  
4159              !!!next-token;              !!!next-token;
4160              redo B;              next B;
4161            } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4162              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              !!!cp ('t93.2');
4163              !!!create-element ($self->{head_element}, 'head', $attr);              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4164              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              ## Ignore the token
4165              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!nack ('t93.3');
4166              $self->{insertion_mode} = 'in head';              !!!next-token;
4167              if ($token->{tag_name} eq 'head') {              next B;
               !!!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;  
             }  
4168            } else {            } else {
4169              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4170            }              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4171          } elsif ($self->{insertion_mode} eq 'in head') {              ## Ignore the token
4172            if ($token->{type} eq 'character') {              !!!nack ('t95.1');
             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);  
4173              !!!next-token;              !!!next-token;
4174              redo B;              next B;
4175            } elsif ($token->{type} eq 'start tag') {            }
4176              if ($token->{tag_name} eq 'title') {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4177                ## NOTE: There is an "as if in head" code clone            !!!cp ('t96');
4178                my $title_el;            ## As if <head>
4179                !!!create-element ($title_el, 'title', $token->{attributes});            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4180                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4181                  ->append_child ($title_el);            push @{$self->{open_elements}},
4182                $self->{content_model_flag} = 'RCDATA';                [$self->{head_element}, $el_category->{head}];
4183    
4184                my $text = '';            $self->{insertion_mode} = IN_HEAD_IM;
4185                !!!next-token;            ## Reprocess in the "in head" insertion mode...
4186                while ($token->{type} eq 'character') {          } else {
4187                  $text .= $token->{data};            !!!cp ('t97');
4188                  !!!next-token;          }
4189                }  
4190                if (length $text) {              if ($token->{tag_name} eq 'base') {
4191                  $title_el->manakai_append_text ($text);                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4192                }                  !!!cp ('t98');
4193                                  ## As if </noscript>
4194                $self->{content_model_flag} = 'PCDATA';                  pop @{$self->{open_elements}};
4195                    !!!parse-error (type => 'in noscript:base', token => $token);
4196                                
4197                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
4198                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
4199                } else {                } else {
4200                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
4201                }                }
               !!!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);  
4202    
4203                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
4204                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4205              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
4206                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4207                ## Ignore the token                  push @{$self->{open_elements}},
4208                !!!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}};  
4209                } else {                } else {
4210                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
4211                }                }
4212                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4213                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4214                  pop @{$self->{open_elements}} # <head>
4215                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4216                  !!!nack ('t101.1');
4217                !!!next-token;                !!!next-token;
4218                redo B;                next B;
4219              } elsif ($token->{tag_name} eq 'html') {              } elsif ($token->{tag_name} eq 'link') {
4220                #                ## NOTE: There is a "as if in head" code clone.
4221              } else {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4222                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t102');
4223                ## Ignore the token                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4224                !!!next-token;                  push @{$self->{open_elements}},
4225                redo B;                      [$self->{head_element}, $el_category->{head}];
4226              }                } else {
4227            } else {                  !!!cp ('t103');
             #  
           }  
   
           if ($self->{open_elements}->[-1]->[1] eq 'head') {  
             ## As if </head>  
             pop @{$self->{open_elements}};  
           }  
           $self->{insertion_mode} = 'after head';  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
4228                }                }
4229              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4230                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4231              #                pop @{$self->{open_elements}} # <head>
4232            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4233              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';  
4234                !!!next-token;                !!!next-token;
4235                redo B;                next B;
4236              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
4237                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
4238                        script => 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4239                       }->{$token->{tag_name}}) {                  !!!cp ('t104');
4240                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4241                $self->{insertion_mode} = 'in head';                  push @{$self->{open_elements}},
4242                ## reprocess                      [$self->{head_element}, $el_category->{head}];
4243                redo B;                } else {
4244              } 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;  
4245                }                }
4246              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4247                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
             !!!parse-error (type => 'in table:#character');  
4248    
4249              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
4250              ## ISSUE: Spec says that "whenever a node would be inserted                  if ($token->{attributes}->{charset}) {
4251              ## into the current node" while characters might not be                    !!!cp ('t106');
4252              ## result in a new Text node.                    ## NOTE: Whether the encoding is supported or not is handled
4253              $reconstruct_active_formatting_elements->($insert_to_foster);                    ## in the {change_encoding} callback.
4254                                  $self->{change_encoding}
4255              if ({                        ->($self, $token->{attributes}->{charset}->{value},
4256                   table => 1, tbody => 1, tfoot => 1,                           $token);
4257                   thead => 1, tr => 1,                    
4258                  }->{$self->{open_elements}->[-1]->[1]}) {                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4259                # MUST                        ->set_user_data (manakai_has_reference =>
4260                my $foster_parent_element;                                             $token->{attributes}->{charset}
4261                my $next_sibling;                                                 ->{has_reference});
4262                my $prev_sibling;                  } elsif ($token->{attributes}->{content}) {
4263                OE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($token->{attributes}->{content}->{value}
4264                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4265                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            [\x09-\x0D\x20]*=
4266                    if (defined $parent and $parent->node_type == 1) {                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4267                      $foster_parent_element = $parent;                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4268                      $next_sibling = $self->{open_elements}->[$_]->[0];                      !!!cp ('t107');
4269                      $prev_sibling = $next_sibling->previous_sibling;                      ## NOTE: Whether the encoding is supported or not is handled
4270                        ## in the {change_encoding} callback.
4271                        $self->{change_encoding}
4272                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4273                               $token);
4274                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4275                            ->set_user_data (manakai_has_reference =>
4276                                                 $token->{attributes}->{content}
4277                                                       ->{has_reference});
4278                    } else {                    } else {
4279                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
4280                    }                    }
                   last OE;  
4281                  }                  }
               } # 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});  
4282                } else {                } else {
4283                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
4284                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
4285                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4286                }                        ->set_user_data (manakai_has_reference =>
4287              } else {                                             $token->{attributes}->{charset}
4288                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
4289              }                  }
4290                                if ($token->{attributes}->{content}) {
4291              !!!next-token;                    !!!cp ('t110');
4292              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4293            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
4294              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
4295              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
4296              !!!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}};  
4297                }                }
4298    
4299                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
4300                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4301                  !!!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}};  
4302                !!!next-token;                !!!next-token;
4303                redo B;                next B;
4304              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
4305                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4306                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
4307                       }->{$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]);  
4308                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4309                    !!!parse-error (type => 'in noscript:title', token => $token);
4310                  
4311                    $self->{insertion_mode} = IN_HEAD_IM;
4312                    ## Reprocess in the "in head" insertion mode...
4313                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4314                    !!!cp ('t112');
4315                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4316                    push @{$self->{open_elements}},
4317                        [$self->{head_element}, $el_category->{head}];
4318                  } else {
4319                    !!!cp ('t113');
4320                }                }
4321    
4322                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
4323                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
4324                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
4325                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4326                redo B;                pop @{$self->{open_elements}} # <head>
4327              } elsif ($token->{tag_name} eq 'table') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4328                ## NOTE: There are code clones for this "table in table"                next B;
4329                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } elsif ($token->{tag_name} eq 'style') {
4330                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4331                ## As if </table>                ## insertion mode IN_HEAD_IM)
4332                ## have a table element in table scope                ## NOTE: There is a "as if in head" code clone.
4333                my $i;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4334                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!cp ('t114');
4335                  my $node = $self->{open_elements}->[$_];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4336                  if ($node->[1] eq 'table') {                  push @{$self->{open_elements}},
4337                    $i = $_;                      [$self->{head_element}, $el_category->{head}];
4338                    last INSCOPE;                } else {
4339                  } elsif ({                  !!!cp ('t115');
4340                            table => 1, html => 1,                }
4341                           }->{$node->[1]}) {                $parse_rcdata->(CDATA_CONTENT_MODEL);
4342                    last INSCOPE;                pop @{$self->{open_elements}} # <head>
4343                  }                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4344                } # INSCOPE                next B;
4345                unless (defined $i) {              } elsif ($token->{tag_name} eq 'noscript') {
4346                  !!!parse-error (type => 'unmatched end tag:table');                if ($self->{insertion_mode} == IN_HEAD_IM) {
4347                  ## Ignore tokens </table><table>                  !!!cp ('t116');
4348                    ## NOTE: and scripting is disalbed
4349                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4350                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4351                    !!!nack ('t116.1');
4352                  !!!next-token;                  !!!next-token;
4353                  redo B;                  next B;
4354                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4355                    !!!cp ('t117');
4356                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4357                    ## Ignore the token
4358                    !!!nack ('t117.1');
4359                    !!!next-token;
4360                    next B;
4361                  } else {
4362                    !!!cp ('t118');
4363                    #
4364                }                }
4365                } elsif ($token->{tag_name} eq 'script') {
4366                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4367                    !!!cp ('t119');
4368                    ## As if </noscript>
4369                    pop @{$self->{open_elements}};
4370                    !!!parse-error (type => 'in noscript:script', token => $token);
4371                                
4372                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
4373                if ({                  ## Reprocess in the "in head" insertion mode...
4374                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4375                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
4376                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4377                  !!!back-token; # <table>                  push @{$self->{open_elements}},
4378                  $token = {type => 'end tag', tag_name => 'table'};                      [$self->{head_element}, $el_category->{head}];
4379                  !!!back-token;                } else {
4380                  $token = {type => 'end tag',                  !!!cp ('t121');
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4381                }                }
4382    
4383                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
4384                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
4385                  pop @{$self->{open_elements}} # <head>
4386                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4387                  next B;
4388                } elsif ($token->{tag_name} eq 'body' or
4389                         $token->{tag_name} eq 'frameset') {
4390                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4391                    !!!cp ('t122');
4392                    ## As if </noscript>
4393                    pop @{$self->{open_elements}};
4394                    !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4395                    
4396                    ## Reprocess in the "in head" insertion mode...
4397                    ## As if </head>
4398                    pop @{$self->{open_elements}};
4399                    
4400                    ## Reprocess in the "after head" insertion mode...
4401                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4402                    !!!cp ('t124');
4403                    pop @{$self->{open_elements}};
4404                    
4405                    ## Reprocess in the "after head" insertion mode...
4406                  } else {
4407                    !!!cp ('t125');
4408                }                }
4409    
4410                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
4411                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4412                  if ($token->{tag_name} eq 'body') {
4413                    !!!cp ('t126');
4414                    $self->{insertion_mode} = IN_BODY_IM;
4415                  } elsif ($token->{tag_name} eq 'frameset') {
4416                    !!!cp ('t127');
4417                    $self->{insertion_mode} = IN_FRAMESET_IM;
4418                  } else {
4419                    die "$0: tag name: $self->{tag_name}";
4420                  }
4421                  !!!nack ('t127.1');
4422                  !!!next-token;
4423                  next B;
4424                } else {
4425                  !!!cp ('t128');
4426                  #
4427                }
4428    
4429                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4430                  !!!cp ('t129');
4431                  ## As if </noscript>
4432                  pop @{$self->{open_elements}};
4433                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4434                  
4435                  ## Reprocess in the "in head" insertion mode...
4436                  ## As if </head>
4437                  pop @{$self->{open_elements}};
4438    
4439                ## reprocess                ## Reprocess in the "after head" insertion mode...
4440                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4441                  !!!cp ('t130');
4442                  ## As if </head>
4443                  pop @{$self->{open_elements}};
4444    
4445                  ## Reprocess in the "after head" insertion mode...
4446              } else {              } else {
4447                #                !!!cp ('t131');
4448              }              }
4449            } elsif ($token->{type} eq 'end tag') {  
4450              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
4451                ## have a table element in table scope              ## As if <body>
4452                my $i;              !!!insert-element ('body',, $token);
4453                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
4454                  my $node = $self->{open_elements}->[$_];              ## reprocess
4455                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
4456                    $i = $_;              next B;
4457                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
4458                  } elsif ({              if ($token->{tag_name} eq 'head') {
4459                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4460                           }->{$node->[1]}) {                  !!!cp ('t132');
4461                    last INSCOPE;                  ## As if <head>
4462                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4463                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4464                unless (defined $i) {                  push @{$self->{open_elements}},
4465                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
4466    
4467                    ## Reprocess in the "in head" insertion mode...
4468                    pop @{$self->{open_elements}};
4469                    $self->{insertion_mode} = AFTER_HEAD_IM;
4470                    !!!next-token;
4471                    next B;
4472                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4473                    !!!cp ('t133');
4474                    ## As if </noscript>
4475                    pop @{$self->{open_elements}};
4476                    !!!parse-error (type => 'in noscript:/head', token => $token);
4477                    
4478                    ## Reprocess in the "in head" insertion mode...
4479                    pop @{$self->{open_elements}};
4480                    $self->{insertion_mode} = AFTER_HEAD_IM;
4481                    !!!next-token;
4482                    next B;
4483                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4484                    !!!cp ('t134');
4485                    pop @{$self->{open_elements}};
4486                    $self->{insertion_mode} = AFTER_HEAD_IM;
4487                    !!!next-token;
4488                    next B;
4489                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4490                    !!!cp ('t134.1');
4491                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4492                  ## Ignore the token                  ## Ignore the token
4493                  !!!next-token;                  !!!next-token;
4494                  redo B;                  next B;
4495                  } else {
4496                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4497                }                }
4498                              } elsif ($token->{tag_name} eq 'noscript') {
4499                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4500                if ({                  !!!cp ('t136');
4501                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}};
4502                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
4503                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!next-token;
4504                  !!!back-token;                  next B;
4505                  $token = {type => 'end tag',                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4506                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4507                  redo B;                  !!!cp ('t137');
4508                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4509                    ## Ignore the token ## ISSUE: An issue in the spec.
4510                    !!!next-token;
4511                    next B;
4512                  } else {
4513                    !!!cp ('t138');
4514                    #
4515                }                }
4516                } elsif ({
4517                if ($self->{open_elements}->[-1]->[1] ne 'table') {                        body => 1, html => 1,
4518                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                       }->{$token->{tag_name}}) {
4519                  if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4520                      $self->{insertion_mode} == IN_HEAD_IM or
4521                      $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4522                    !!!cp ('t140');
4523                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4524                    ## Ignore the token
4525                    !!!next-token;
4526                    next B;
4527                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4528                    !!!cp ('t140.1');
4529                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4530                    ## Ignore the token
4531                    !!!next-token;
4532                    next B;
4533                  } else {
4534                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4535                }                }
4536                } elsif ($token->{tag_name} eq 'p') {
4537                  !!!cp ('t142');
4538                  !!!parse-error (type => 'unmatched end tag:p', token => $token);
4539                  ## Ignore the token
4540                  !!!next-token;
4541                  next B;
4542                } elsif ($token->{tag_name} eq 'br') {
4543                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4544                    !!!cp ('t142.2');
4545                    ## (before head) as if <head>, (in head) as if </head>
4546                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4547                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4548                    $self->{insertion_mode} = AFTER_HEAD_IM;
4549      
4550                    ## Reprocess in the "after head" insertion mode...
4551                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4552                    !!!cp ('t143.2');
4553                    ## As if </head>
4554                    pop @{$self->{open_elements}};
4555                    $self->{insertion_mode} = AFTER_HEAD_IM;
4556      
4557                    ## Reprocess in the "after head" insertion mode...
4558                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4559                    !!!cp ('t143.3');
4560                    ## ISSUE: Two parse errors for <head><noscript></br>
4561                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4562                    ## As if </noscript>
4563                    pop @{$self->{open_elements}};
4564                    $self->{insertion_mode} = IN_HEAD_IM;
4565    
4566                splice @{$self->{open_elements}}, $i;                  ## Reprocess in the "in head" insertion mode...
4567                    ## As if </head>
4568                    pop @{$self->{open_elements}};
4569                    $self->{insertion_mode} = AFTER_HEAD_IM;
4570    
4571                $self->_reset_insertion_mode;                  ## Reprocess in the "after head" insertion mode...
4572                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4573                    !!!cp ('t143.4');
4574                    #
4575                  } else {
4576                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4577                  }
4578    
4579                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4580                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4581                  ## Ignore the token
4582                !!!next-token;                !!!next-token;
4583                redo B;                next B;
4584              } elsif ({              } else {
4585                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t145');
4586                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4587                ## Ignore the token                ## Ignore the token
4588                !!!next-token;                !!!next-token;
4589                redo B;                next B;
4590                }
4591    
4592                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4593                  !!!cp ('t146');
4594                  ## As if </noscript>
4595                  pop @{$self->{open_elements}};
4596                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4597                  
4598                  ## Reprocess in the "in head" insertion mode...
4599                  ## As if </head>
4600                  pop @{$self->{open_elements}};
4601    
4602                  ## Reprocess in the "after head" insertion mode...
4603                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4604                  !!!cp ('t147');
4605                  ## As if </head>
4606                  pop @{$self->{open_elements}};
4607    
4608                  ## Reprocess in the "after head" insertion mode...
4609                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4610    ## ISSUE: This case cannot be reached?
4611                  !!!cp ('t148');
4612                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4613                  ## Ignore the token ## ISSUE: An issue in the spec.
4614                  !!!next-token;
4615                  next B;
4616              } else {              } else {
4617                #                !!!cp ('t149');
4618              }              }
           } else {  
             #  
           }  
4619    
4620            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
4621            $in_body->($insert_to_foster);              ## As if <body>
4622            redo B;              !!!insert-element ('body',, $token);
4623          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
4624            if ($token->{type} eq 'character') {              ## reprocess
4625              ## NOTE: This is a code clone of "character in body".              next B;
4626          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4627            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4628              !!!cp ('t149.1');
4629    
4630              ## NOTE: As if <head>
4631              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4632              $self->{open_elements}->[-1]->[0]->append_child
4633                  ($self->{head_element});
4634              #push @{$self->{open_elements}},
4635              #    [$self->{head_element}, $el_category->{head}];
4636              #$self->{insertion_mode} = IN_HEAD_IM;
4637              ## NOTE: Reprocess.
4638    
4639              ## NOTE: As if </head>
4640              #pop @{$self->{open_elements}};
4641              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4642              ## NOTE: Reprocess.
4643              
4644              #
4645            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4646              !!!cp ('t149.2');
4647    
4648              ## NOTE: As if </head>
4649              pop @{$self->{open_elements}};
4650              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4651              ## NOTE: Reprocess.
4652    
4653              #
4654            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4655              !!!cp ('t149.3');
4656    
4657              !!!parse-error (type => 'in noscript:#eof', token => $token);
4658    
4659              ## As if </noscript>
4660              pop @{$self->{open_elements}};
4661              #$self->{insertion_mode} = IN_HEAD_IM;
4662              ## NOTE: Reprocess.
4663    
4664              ## NOTE: As if </head>
4665              pop @{$self->{open_elements}};
4666              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4667              ## NOTE: Reprocess.
4668    
4669              #
4670            } else {
4671              !!!cp ('t149.4');
4672              #
4673            }
4674    
4675            ## NOTE: As if <body>
4676            !!!insert-element ('body',, $token);
4677            $self->{insertion_mode} = IN_BODY_IM;
4678            ## NOTE: Reprocess.
4679            next B;
4680          } else {
4681            die "$0: $token->{type}: Unknown token type";
4682          }
4683    
4684              ## ISSUE: An issue in the spec.
4685        } elsif ($self->{insertion_mode} & BODY_IMS) {
4686              if ($token->{type} == CHARACTER_TOKEN) {
4687                !!!cp ('t150');
4688                ## NOTE: There is a code clone of "character in body".
4689              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4690                            
4691              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4692    
4693              !!!next-token;              !!!next-token;
4694              redo B;              next B;
4695            } 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') {  
4696              if ({              if ({
4697                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4698                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4699                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4700                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
4701                    ## have an element in table scope
4702                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
4703                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
4704                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
4705                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
4706                  my $node = $self->{open_elements}->[$_];  
4707                  if ($node->[1] eq 'caption') {                      ## Close the cell
4708                    $i = $_;                      !!!back-token; # <x>
4709                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
4710                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
4711                            table => 1, html => 1,                                line => $token->{line},
4712                           }->{$node->[1]}) {                                column => $token->{column}};
4713                    last INSCOPE;                      next B;
4714                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4715                        !!!cp ('t152');
4716                        ## ISSUE: This case can never be reached, maybe.
4717                        last;
4718                      }
4719                  }                  }
4720                } # INSCOPE  
4721                unless (defined $i) {                  !!!cp ('t153');
4722                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
4723                        value => $token->{tag_name}, token => $token);
4724                  ## Ignore the token                  ## Ignore the token
4725                    !!!nack ('t153.1');
4726                  !!!next-token;                  !!!next-token;
4727                  redo B;                  next B;
4728                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4729                                  !!!parse-error (type => 'not closed:caption', token => $token);
4730                ## generate implied end tags                  
4731                if ({                  ## NOTE: As if </caption>.
4732                     dd => 1, dt => 1, li => 1, p => 1,                  ## have a table element in table scope
4733                     td => 1, th => 1, tr => 1,                  my $i;
4734                    }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: {
4735                  !!!back-token; # <?>                    for (reverse 0..$#{$self->{open_elements}}) {
4736                  $token = {type => 'end tag', tag_name => 'caption'};                      my $node = $self->{open_elements}->[$_];
4737                  !!!back-token;                      if ($node->[1] & CAPTION_EL) {
4738                  $token = {type => 'end tag',                        !!!cp ('t155');
4739                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        $i = $_;
4740                  redo B;                        last INSCOPE;
4741                }                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4742                          !!!cp ('t156');
4743                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        last;
4744                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      }
4745                }                    }
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
4746    
4747                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
4748                      !!!parse-error (type => 'start tag not allowed',
4749                                      value => $token->{tag_name}, token => $token);
4750                      ## Ignore the token
4751                      !!!nack ('t157.1');
4752                      !!!next-token;
4753                      next B;
4754                    } # INSCOPE
4755                    
4756                    ## generate implied end tags
4757                    while ($self->{open_elements}->[-1]->[1]
4758                               & END_TAG_OPTIONAL_EL) {
4759                      !!!cp ('t158');
4760                      pop @{$self->{open_elements}};
4761                    }
4762    
4763                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4764                redo B;                    !!!cp ('t159');
4765                      !!!parse-error (type => 'not closed',
4766                                      value => $self->{open_elements}->[-1]->[0]
4767                                          ->manakai_local_name,
4768                                      token => $token);
4769                    } else {
4770                      !!!cp ('t160');
4771                    }
4772                    
4773                    splice @{$self->{open_elements}}, $i;
4774                    
4775                    $clear_up_to_marker->();
4776                    
4777                    $self->{insertion_mode} = IN_TABLE_IM;
4778                    
4779                    ## reprocess
4780                    !!!ack-later;
4781                    next B;
4782                  } else {
4783                    !!!cp ('t161');
4784                    #
4785                  }
4786              } else {              } else {
4787                  !!!cp ('t162');
4788                #                #
4789              }              }
4790            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4791              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4792                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
4793                my $i;                  ## have an element in table scope
4794                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
4795                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4796                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4797                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4798                    last INSCOPE;                      !!!cp ('t163');
4799                  } elsif ({                      $i = $_;
4800                            table => 1, html => 1,                      last INSCOPE;
4801                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4802                    last INSCOPE;                      !!!cp ('t164');
4803                        last INSCOPE;
4804                      }
4805                    } # INSCOPE
4806                      unless (defined $i) {
4807                        !!!cp ('t165');
4808                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4809                        ## Ignore the token
4810                        !!!next-token;
4811                        next B;
4812                      }
4813                    
4814                    ## generate implied end tags
4815                    while ($self->{open_elements}->[-1]->[1]
4816                               & END_TAG_OPTIONAL_EL) {
4817                      !!!cp ('t166');
4818                      pop @{$self->{open_elements}};
4819                  }                  }
4820                } # INSCOPE  
4821                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4822                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
4823                      !!!cp ('t167');
4824                      !!!parse-error (type => 'not closed',
4825                                      value => $self->{open_elements}->[-1]->[0]
4826                                          ->manakai_local_name,
4827                                      token => $token);
4828                    } else {
4829                      !!!cp ('t168');
4830                    }
4831                    
4832                    splice @{$self->{open_elements}}, $i;
4833                    
4834                    $clear_up_to_marker->();
4835                    
4836                    $self->{insertion_mode} = IN_ROW_IM;
4837                    
4838                    !!!next-token;
4839                    next B;
4840                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4841                    !!!cp ('t169');
4842                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4843                  ## Ignore the token                  ## Ignore the token
4844                  !!!next-token;                  !!!next-token;
4845                  redo B;                  next B;
4846                }                } else {
4847                                  !!!cp ('t170');
4848                ## 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;  
4849                }                }
4850                } elsif ($token->{tag_name} eq 'caption') {
4851                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
4852                    ## have a table element in table scope
4853                    my $i;
4854                    INSCOPE: {
4855                      for (reverse 0..$#{$self->{open_elements}}) {
4856                        my $node = $self->{open_elements}->[$_];
4857                        if ($node->[1] & CAPTION_EL) {
4858                          !!!cp ('t171');
4859                          $i = $_;
4860                          last INSCOPE;
4861                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
4862                          !!!cp ('t172');
4863                          last;
4864                        }
4865                      }
4866    
4867                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
4868                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
4869                                      value => $token->{tag_name}, token => $token);
4870                      ## Ignore the token
4871                      !!!next-token;
4872                      next B;
4873                    } # INSCOPE
4874                    
4875                    ## generate implied end tags
4876                    while ($self->{open_elements}->[-1]->[1]
4877                               & END_TAG_OPTIONAL_EL) {
4878                      !!!cp ('t174');
4879                      pop @{$self->{open_elements}};
4880                    }
4881                    
4882                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4883                      !!!cp ('t175');
4884                      !!!parse-error (type => 'not closed',
4885                                      value => $self->{open_elements}->[-1]->[0]
4886                                          ->manakai_local_name,
4887                                      token => $token);
4888                    } else {
4889                      !!!cp ('t176');
4890                    }
4891                    
4892                    splice @{$self->{open_elements}}, $i;
4893                    
4894                    $clear_up_to_marker->();
4895                    
4896                    $self->{insertion_mode} = IN_TABLE_IM;
4897                    
4898                    !!!next-token;
4899                    next B;
4900                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4901                    !!!cp ('t177');
4902                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4903                    ## Ignore the token
4904                    !!!next-token;
4905                    next B;
4906                  } else {
4907                    !!!cp ('t178');
4908                    #
4909                }                }
4910                } elsif ({
4911                          table => 1, tbody => 1, tfoot => 1,
4912                          thead => 1, tr => 1,
4913                         }->{$token->{tag_name}} and
4914                         $self->{insertion_mode} == IN_CELL_IM) {
4915                  ## have an element in table scope
4916                  my $i;
4917                  my $tn;
4918                  INSCOPE: {
4919                    for (reverse 0..$#{$self->{open_elements}}) {
4920                      my $node = $self->{open_elements}->[$_];
4921                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4922                        !!!cp ('t179');
4923                        $i = $_;
4924    
4925                        ## Close the cell
4926                        !!!back-token; # </x>
4927                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
4928                                  line => $token->{line},
4929                                  column => $token->{column}};
4930                        next B;
4931                      } elsif ($node->[1] & TABLE_CELL_EL) {
4932                        !!!cp ('t180');
4933                        $tn = $node->[0]->manakai_local_name;
4934                        ## NOTE: There is exactly one |td| or |th| element
4935                        ## in scope in the stack of open elements by definition.
4936                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4937                        ## ISSUE: Can this be reached?
4938                        !!!cp ('t181');
4939                        last;
4940                      }
4941                    }
4942    
4943                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
4944                    !!!parse-error (type => 'unmatched end tag',
4945                $clear_up_to_marker->();                      value => $token->{tag_name}, token => $token);
4946                    ## Ignore the token
4947                $self->{insertion_mode} = 'in table';                  !!!next-token;
4948                    next B;
4949                !!!next-token;                } # INSCOPE
4950                redo B;              } elsif ($token->{tag_name} eq 'table' and
4951              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4952                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4953    
4954                ## As if </caption>                ## As if </caption>
4955                ## have a table element in table scope                ## have a table element in table scope
4956                my $i;                my $i;
4957                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4958                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4959                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4960                      !!!cp ('t184');
4961                    $i = $_;                    $i = $_;
4962                    last INSCOPE;                    last INSCOPE;
4963                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4964                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4965                    last INSCOPE;                    last INSCOPE;
4966                  }                  }
4967                } # INSCOPE                } # INSCOPE
4968                unless (defined $i) {                unless (defined $i) {
4969                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4970                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4971                  ## Ignore the token                  ## Ignore the token
4972                  !!!next-token;                  !!!next-token;
4973                  redo B;                  next B;
4974                }                }
4975                                
4976                ## generate implied end tags                ## generate implied end tags
4977                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4978                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4979                     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;  
4980                }                }
4981    
4982                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4983                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4984                    !!!parse-error (type => 'not closed',
4985                                    value => $self->{open_elements}->[-1]->[0]
4986                                        ->manakai_local_name,
4987                                    token => $token);
4988                  } else {
4989                    !!!cp ('t189');
4990                }                }
4991    
4992                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4993    
4994                $clear_up_to_marker->();                $clear_up_to_marker->();
4995    
4996                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4997    
4998                ## reprocess                ## reprocess
4999                redo B;                next B;
5000              } elsif ({              } elsif ({
5001                        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,  
5002                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5003                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5004                ## Ignore the token                  !!!cp ('t190');
5005                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');  
5006                  ## Ignore the token                  ## Ignore the token
5007                  !!!next-token;                  !!!next-token;
5008                  redo B;                  next B;
5009                } else {                } else {
5010                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
5011                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
5012                }                }
5013              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
5014                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
5015                          thead => 1, tr => 1,
5016                         }->{$token->{tag_name}} and
5017                         $self->{insertion_mode} == IN_CAPTION_IM) {
5018                  !!!cp ('t192');
5019                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5020                ## Ignore the token                ## Ignore the token
5021                !!!next-token;                !!!next-token;
5022                redo B;                next B;
5023              } else {              } else {
5024                #                !!!cp ('t193');
5025                  #
5026              }              }
5027            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5028              #          for my $entry (@{$self->{open_elements}}) {
5029              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5030                !!!cp ('t75');
5031                !!!parse-error (type => 'in body:#eof', token => $token);
5032                last;
5033            }            }
5034            }
5035    
5036            ## As if </colgroup>          ## Stop parsing.
5037            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
5038              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
5039              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
5040          }
5041    
5042          $insert = $insert_to_current;
5043          #
5044        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5045          if ($token->{type} == CHARACTER_TOKEN) {
5046            if (not $open_tables->[-1]->[1] and # tainted
5047                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5048              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5049                  
5050              unless (length $token->{data}) {
5051                !!!cp ('t194');
5052              !!!next-token;              !!!next-token;
5053              redo B;              next B;
5054            } else {            } else {
5055              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5056            }            }
5057          } 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;  
               }  
             }  
5058    
5059              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5060    
5061              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5062              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5063              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5064              ## result in a new Text node.              ## result in a new Text node.
5065              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5066                
5067              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]}) {  
5068                # MUST                # MUST
5069                my $foster_parent_element;                my $foster_parent_element;
5070                my $next_sibling;                my $next_sibling;
5071                my $prev_sibling;                my $prev_sibling;
5072                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5073                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5074                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5075                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5076                        !!!cp ('t196');
5077                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5078                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5079                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5080                    } else {                    } else {
5081                        !!!cp ('t197');
5082                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5083                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5084                    }                    }
# Line 3725  sub _tree_construction_main ($) { Line 5090  sub _tree_construction_main ($) {
5090                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5091                if (defined $prev_sibling and                if (defined $prev_sibling and
5092                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5093                    !!!cp ('t198');
5094                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5095                } else {                } else {
5096                    !!!cp ('t199');
5097                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5098                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5099                     $next_sibling);                     $next_sibling);
5100                }                }
5101              } else {            $open_tables->[-1]->[1] = 1; # tainted
5102                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5103              }            !!!cp ('t200');
5104              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5105            }
5106                            
5107              !!!next-token;          !!!next-token;
5108              redo B;          next B;
5109            } 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') {  
5110              if ({              if ({
5111                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
5112                   th => 1, td => 1,                   th => 1, td => 1,
5113                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5114                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5115                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
5116                    while (not ($self->{open_elements}->[-1]->[1]
5117                                    & TABLE_SCOPING_EL)) {
5118                      !!!cp ('t201');
5119                      pop @{$self->{open_elements}};
5120                    }
5121                    
5122                    !!!insert-element ('tbody',, $token);
5123                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5124                    ## reprocess in the "in table body" insertion mode...
5125                }                }
5126    
5127                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5128                while (not {                  unless ($token->{tag_name} eq 'tr') {
5129                  tbody => 1, tfoot => 1, thead => 1, html => 1,                    !!!cp ('t202');
5130                }->{$self->{open_elements}->[-1]->[1]}) {                    !!!parse-error (type => 'missing start tag:tr', token => $token);
5131                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  }
5132                    
5133                    ## Clear back to table body context
5134                    while (not ($self->{open_elements}->[-1]->[1]
5135                                    & TABLE_ROWS_SCOPING_EL)) {
5136                      !!!cp ('t203');
5137                      ## ISSUE: Can this case be reached?
5138                      pop @{$self->{open_elements}};
5139                    }
5140                    
5141                    $self->{insertion_mode} = IN_ROW_IM;
5142                    if ($token->{tag_name} eq 'tr') {
5143                      !!!cp ('t204');
5144                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5145                      !!!nack ('t204');
5146                      !!!next-token;
5147                      next B;
5148                    } else {
5149                      !!!cp ('t205');
5150                      !!!insert-element ('tr',, $token);
5151                      ## reprocess in the "in row" insertion mode
5152                    }
5153                  } else {
5154                    !!!cp ('t206');
5155                  }
5156    
5157                  ## Clear back to table row context
5158                  while (not ($self->{open_elements}->[-1]->[1]
5159                                  & TABLE_ROW_SCOPING_EL)) {
5160                    !!!cp ('t207');
5161                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5162                }                }
5163                                
5164                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5165                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5166                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5167                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5168                } else {                
5169                  !!!insert-element ('tr');                !!!nack ('t207.1');
5170                  ## reprocess                !!!next-token;
5171                }                next B;
               redo B;  
5172              } elsif ({              } elsif ({
5173                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5174                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5175                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5176                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5177                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5178                my $i;                  ## As if </tr>
5179                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5180                  my $node = $self->{open_elements}->[$_];                  my $i;
5181                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5182                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5183                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5184                    $i = $_;                      !!!cp ('t208');
5185                    last INSCOPE;                      $i = $_;
5186                  } elsif ({                      last INSCOPE;
5187                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5188                           }->{$node->[1]}) {                      !!!cp ('t209');
5189                    last INSCOPE;                      last INSCOPE;
5190                      }
5191                    } # INSCOPE
5192                    unless (defined $i) {
5193                      !!!cp ('t210');
5194    ## TODO: This type is wrong.
5195                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5196                      ## Ignore the token
5197                      !!!nack ('t210.1');
5198                      !!!next-token;
5199                      next B;
5200                    }
5201                    
5202                    ## Clear back to table row context
5203                    while (not ($self->{open_elements}->[-1]->[1]
5204                                    & TABLE_ROW_SCOPING_EL)) {
5205                      !!!cp ('t211');
5206                      ## ISSUE: Can this case be reached?
5207                      pop @{$self->{open_elements}};
5208                    }
5209                    
5210                    pop @{$self->{open_elements}}; # tr
5211                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5212                    if ($token->{tag_name} eq 'tr') {
5213                      !!!cp ('t212');
5214                      ## reprocess
5215                      !!!ack-later;
5216                      next B;
5217                    } else {
5218                      !!!cp ('t213');
5219                      ## reprocess in the "in table body" insertion mode...
5220                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5221                }                }
5222    
5223                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5224                while (not {                  ## have an element in table scope
5225                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5226                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5227                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5228                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5229                        !!!cp ('t214');
5230                        $i = $_;
5231                        last INSCOPE;
5232                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5233                        !!!cp ('t215');
5234                        last INSCOPE;
5235                      }
5236                    } # INSCOPE
5237                    unless (defined $i) {
5238                      !!!cp ('t216');
5239    ## TODO: This erorr type ios wrong.
5240                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5241                      ## Ignore the token
5242                      !!!nack ('t216.1');
5243                      !!!next-token;
5244                      next B;
5245                    }
5246    
5247                    ## Clear back to table body context
5248                    while (not ($self->{open_elements}->[-1]->[1]
5249                                    & TABLE_ROWS_SCOPING_EL)) {
5250                      !!!cp ('t217');
5251                      ## ISSUE: Can this state be reached?
5252                      pop @{$self->{open_elements}};
5253                    }
5254                    
5255                    ## As if <{current node}>
5256                    ## have an element in table scope
5257                    ## true by definition
5258                    
5259                    ## Clear back to table body context
5260                    ## nop by definition
5261                    
5262                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5263                    $self->{insertion_mode} = IN_TABLE_IM;
5264                    ## reprocess in "in table" insertion mode...
5265                  } else {
5266                    !!!cp ('t218');
5267                }                }
5268    
5269                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5270                ## have an element in table scope                  ## Clear back to table context
5271                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5272                                    & TABLE_SCOPING_EL)) {
5273                ## Clear back to table body context                    !!!cp ('t219');
5274                ## nop by definition                    ## ISSUE: Can this state be reached?
5275                      pop @{$self->{open_elements}};
5276                pop @{$self->{open_elements}};                  }
5277                $self->{insertion_mode} = 'in table';                  
5278                ## reprocess                  !!!insert-element ('colgroup',, $token);
5279                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5280                    ## reprocess
5281                    !!!ack-later;
5282                    next B;
5283                  } elsif ({
5284                            caption => 1,
5285                            colgroup => 1,
5286                            tbody => 1, tfoot => 1, thead => 1,
5287                           }->{$token->{tag_name}}) {
5288                    ## Clear back to table context
5289                    while (not ($self->{open_elements}->[-1]->[1]
5290                                    & TABLE_SCOPING_EL)) {
5291                      !!!cp ('t220');
5292                      ## ISSUE: Can this state be reached?
5293                      pop @{$self->{open_elements}};
5294                    }
5295                    
5296                    push @$active_formatting_elements, ['#marker', '']
5297                        if $token->{tag_name} eq 'caption';
5298                    
5299                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5300                    $self->{insertion_mode} = {
5301                                               caption => IN_CAPTION_IM,
5302                                               colgroup => IN_COLUMN_GROUP_IM,
5303                                               tbody => IN_TABLE_BODY_IM,
5304                                               tfoot => IN_TABLE_BODY_IM,
5305                                               thead => IN_TABLE_BODY_IM,
5306                                              }->{$token->{tag_name}};
5307                    !!!next-token;
5308                    !!!nack ('t220.1');
5309                    next B;
5310                  } else {
5311                    die "$0: in table: <>: $token->{tag_name}";
5312                  }
5313              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5314                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5315                !!!parse-error (type => 'not closed:table');                                value => $self->{open_elements}->[-1]->[0]
5316                                      ->manakai_local_name,
5317                                  token => $token);
5318    
5319                ## As if </table>                ## As if </table>
5320                ## have a table element in table scope                ## have a table element in table scope
5321                my $i;                my $i;
5322                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5323                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5324                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5325                      !!!cp ('t221');
5326                    $i = $_;                    $i = $_;
5327                    last INSCOPE;                    last INSCOPE;
5328                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5329                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5330                    last INSCOPE;                    last INSCOPE;
5331                  }                  }
5332                } # INSCOPE                } # INSCOPE
5333                unless (defined $i) {                unless (defined $i) {
5334                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5335    ## TODO: The following is wrong, maybe.
5336                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5337                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5338                    !!!nack ('t223.1');
5339                  !!!next-token;                  !!!next-token;
5340                  redo B;                  next B;
5341                }                }
5342                                
5343    ## TODO: Followings are removed from the latest spec.
5344                ## generate implied end tags                ## generate implied end tags
5345                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5346                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5347                     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;  
5348                }                }
5349    
5350                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5351                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5352                    ## NOTE: |<table><tr><table>|
5353                    !!!parse-error (type => 'not closed',
5354                                    value => $self->{open_elements}->[-1]->[0]
5355                                        ->manakai_local_name,
5356                                    token => $token);
5357                  } else {
5358                    !!!cp ('t226');
5359                }                }
5360    
5361                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5362                  pop @{$open_tables};
5363    
5364                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5365    
5366                ## reprocess            ## reprocess
5367                redo B;            !!!ack-later;
5368              } else {            next B;
5369                #          } elsif ($token->{tag_name} eq 'style') {
5370              }            if (not $open_tables->[-1]->[1]) { # tainted
5371            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5372              if ({              ## NOTE: This is a "as if in head" code clone.
5373                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5374                  }->{$token->{tag_name}}) {              next B;
5375                ## have an element in table scope            } else {
5376                my $i;              !!!cp ('t227.7');
5377                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5378                  my $node = $self->{open_elements}->[$_];            }
5379                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5380                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5381                    last INSCOPE;              !!!cp ('t227.6');
5382                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5383                            table => 1, html => 1,              $script_start_tag->();
5384                           }->{$node->[1]}) {              next B;
5385                    last INSCOPE;            } else {
5386                  }              !!!cp ('t227.5');
5387                } # INSCOPE              #
5388                unless (defined $i) {            }
5389                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5390                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5391                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5392                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5393                }                if ($type eq 'hidden') {
5394                    !!!cp ('t227.3');
5395                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5396    
5397                ## 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}};  
               }  
5398    
5399                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;  
               }  
5400    
               ## 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]);  
5401                  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  
5402    
5403                pop @{$self->{open_elements}};                  !!!next-token;
5404                $self->{insertion_mode} = 'in table';                  !!!ack ('t227.2.1');
5405                ## reprocess                  next B;
5406                redo B;                } else {
5407              } elsif ({                  !!!cp ('t227.2');
5408                        body => 1, caption => 1, col => 1, colgroup => 1,                  #
5409                        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;  
5410              } else {              } else {
5411                  !!!cp ('t227.1');
5412                #                #
5413              }              }
5414            } else {            } else {
5415                !!!cp ('t227.4');
5416              #              #
5417            }            }
5418                      } else {
5419            ## As if in table            !!!cp ('t227');
5420            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5421            $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;  
               }  
             }  
5422    
5423              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5424    
5425              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
5426              ## ISSUE: Spec says that "whenever a node would be inserted          #
5427              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
5428              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
5429              $reconstruct_active_formatting_elements->($insert_to_foster);                  $self->{insertion_mode} == IN_ROW_IM) {
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
5430                ## have an element in table scope                ## have an element in table scope
5431                my $i;                my $i;
5432                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5433                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5434                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5435                      !!!cp ('t228');
5436                    $i = $_;                    $i = $_;
5437                    last INSCOPE;                    last INSCOPE;
5438                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5439                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5440                    last INSCOPE;                    last INSCOPE;
5441                  }                  }
5442                } # INSCOPE                } # INSCOPE
5443                unless (defined $i) {                unless (defined $i) {
5444                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5445                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5446                  ## Ignore the token                  ## Ignore the token
5447                    !!!nack ('t230.1');
5448                  !!!next-token;                  !!!next-token;
5449                  redo B;                  next B;
5450                  } else {
5451                    !!!cp ('t232');
5452                }                }
5453    
5454                ## Clear back to table row context                ## Clear back to table row context
5455                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5456                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5457                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5458                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5459                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5460                }                }
5461    
5462                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5463                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5464                ## reprocess                !!!next-token;
5465                redo B;                !!!nack ('t231.1');
5466                  next B;
5467              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5468                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5469                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5470                    ## have an element in table scope
5471                ## As if </table>                  my $i;
5472                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5473                my $i;                    my $node = $self->{open_elements}->[$_];
5474                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5475                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5476                  if ($node->[1] eq 'table') {                      $i = $_;
5477                    $i = $_;                      last INSCOPE;
5478                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5479                  } elsif ({                      !!!cp ('t234');
5480                            table => 1, html => 1,                      last INSCOPE;
5481                           }->{$node->[1]}) {                    }
5482                    last INSCOPE;                  } # INSCOPE
5483                    unless (defined $i) {
5484                      !!!cp ('t235');
5485    ## TODO: The following is wrong.
5486                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5487                      ## Ignore the token
5488                      !!!nack ('t236.1');
5489                      !!!next-token;
5490                      next B;
5491                  }                  }
5492                } # INSCOPE                  
5493                unless (defined $i) {                  ## Clear back to table row context
5494                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5495                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5496                  !!!next-token;                    !!!cp ('t236');
5497                  redo B;  ## ISSUE: Can this state be reached?
5498                }                    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;  
5499                  }                  }
5500                } # INSCOPE                  
5501                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5502                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5503                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5504                  !!!next-token;                }
5505                  redo B;  
5506                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5507                    ## have an element in table scope
5508                ## Clear back to table row context                  my $i;
5509                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5510                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5511                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5512                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5513                        $i = $_;
5514                        last INSCOPE;
5515                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5516                        !!!cp ('t238');
5517                        last INSCOPE;
5518                      }
5519                    } # INSCOPE
5520                    unless (defined $i) {
5521                      !!!cp ('t239');
5522                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5523                      ## Ignore the token
5524                      !!!nack ('t239.1');
5525                      !!!next-token;
5526                      next B;
5527                    }
5528                    
5529                    ## Clear back to table body context
5530                    while (not ($self->{open_elements}->[-1]->[1]
5531                                    & TABLE_ROWS_SCOPING_EL)) {
5532                      !!!cp ('t240');
5533                      pop @{$self->{open_elements}};
5534                    }
5535                    
5536                    ## As if <{current node}>
5537                    ## have an element in table scope
5538                    ## true by definition
5539                    
5540                    ## Clear back to table body context
5541                    ## nop by definition
5542                    
5543                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5544                    $self->{insertion_mode} = IN_TABLE_IM;
5545                    ## reprocess in the "in table" insertion mode...
5546                }                }
5547    
5548                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
5549                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
5550                !!!next-token;                ## the code for <table> in the "in table" insertion mode
5551                redo B;                ## is synced with it.
5552              } elsif ($token->{tag_name} eq 'table') {  
5553                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
5554                my $i;                my $i;
5555                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5556                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5557                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
5558                      !!!cp ('t241');
5559                    $i = $_;                    $i = $_;
5560                    last INSCOPE;                    last INSCOPE;
5561                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5562                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5563                    last INSCOPE;                    last INSCOPE;
5564                  }                  }
5565                } # INSCOPE                } # INSCOPE
5566                unless (defined $i) {                unless (defined $i) {
5567                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
5568                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5569                  ## Ignore the token                  ## Ignore the token
5570                    !!!nack ('t243.1');
5571                  !!!next-token;                  !!!next-token;
5572                  redo B;                  next B;
5573                }                }
5574                    
5575                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
5576                while (not {                pop @{$open_tables};
5577                  tr => 1, html => 1,                
5578                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
5579                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
5580                  pop @{$self->{open_elements}};                !!!next-token;
5581                }                next B;
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
5582              } elsif ({              } elsif ({
5583                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5584                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
5585                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
5586                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
5587                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5588                  my $node = $self->{open_elements}->[$_];                  my $i;
5589                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5590                    $i = $_;                    my $node = $self->{open_elements}->[$_];
5591                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5592                  } elsif ({                      !!!cp ('t247');
5593                            table => 1, html => 1,                      $i = $_;
5594                           }->{$node->[1]}) {                      last INSCOPE;
5595                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5596                        !!!cp ('t248');
5597                        last INSCOPE;
5598                      }
5599                    } # INSCOPE
5600                      unless (defined $i) {
5601                        !!!cp ('t249');
5602                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5603                        ## Ignore the token
5604                        !!!nack ('t249.1');
5605                        !!!next-token;
5606                        next B;
5607                      }
5608                    
5609                    ## As if </tr>
5610                    ## have an element in table scope
5611                    my $i;
5612                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5613                      my $node = $self->{open_elements}->[$_];
5614                      if ($node->[1] & TABLE_ROW_EL) {
5615                        !!!cp ('t250');
5616                        $i = $_;
5617                        last INSCOPE;
5618                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5619                        !!!cp ('t251');
5620                        last INSCOPE;
5621                      }
5622                    } # INSCOPE
5623                      unless (defined $i) {
5624                        !!!cp ('t252');
5625                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5626                        ## Ignore the token
5627                        !!!nack ('t252.1');
5628                        !!!next-token;
5629                        next B;
5630                      }
5631                    
5632                    ## Clear back to table row context
5633                    while (not ($self->{open_elements}->[-1]->[1]
5634                                    & TABLE_ROW_SCOPING_EL)) {
5635                      !!!cp ('t253');
5636    ## ISSUE: Can this case be reached?
5637                      pop @{$self->{open_elements}};
5638                  }                  }
5639                } # INSCOPE                  
5640                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5641                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5642                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
5643                }                }
5644    
               ## As if </tr>  
5645                ## have an element in table scope                ## have an element in table scope
5646                my $i;                my $i;
5647                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5648                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5649                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5650                      !!!cp ('t254');
5651                    $i = $_;                    $i = $_;
5652                    last INSCOPE;                    last INSCOPE;
5653                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5654                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5655                    last INSCOPE;                    last INSCOPE;
5656                  }                  }
5657                } # INSCOPE                } # INSCOPE
5658                unless (defined $i) {                unless (defined $i) {
5659                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
5660                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5661                  ## Ignore the token                  ## Ignore the token
5662                    !!!nack ('t256.1');
5663                  !!!next-token;                  !!!next-token;
5664                  redo B;                  next B;
5665                }                }
5666    
5667                ## Clear back to table row context                ## Clear back to table body context
5668                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5669                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5670                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5671                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5672                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5673                }                }
5674    
5675                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
5676                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
5677                ## reprocess                !!!nack ('t257.1');
5678                redo B;                !!!next-token;
5679                  next B;
5680              } elsif ({              } elsif ({
5681                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5682                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5683                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5684                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5685                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5686                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5687                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5688                !!!next-token;            ## Ignore the token
5689                redo B;            !!!nack ('t258.1');
5690              } else {             !!!next-token;
5691                #            next B;
5692              }          } else {
5693            } else {            !!!cp ('t259');
5694              #            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
           }  
5695    
5696            ## As if in table            $insert = $insert_to_foster;
5697            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5698            $in_body->($insert_to_foster);          }
5699            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5700          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5701            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
5702              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
5703              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
5704                          #
5705              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5706              !!!cp ('t259.2');
5707              #
5708            }
5709    
5710              !!!next-token;          ## Stop parsing
5711              redo B;          last B;
5712            } elsif ($token->{type} eq 'comment') {        } else {
5713              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
5714              my $comment = $self->{document}->create_comment ($token->{data});        }
5715              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5716              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
5717              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5718            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5719              if ({                unless (length $token->{data}) {
5720                   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  
5721                  !!!next-token;                  !!!next-token;
5722                  redo B;                  next B;
5723                }                }
5724                }
5725                ## Close the cell              
5726                !!!back-token; # <?>              !!!cp ('t261');
5727                $token = {type => 'end tag', tag_name => $tn};              #
5728                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
5729              } else {              if ($token->{tag_name} eq 'col') {
5730                  !!!cp ('t262');
5731                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5732                  pop @{$self->{open_elements}};
5733                  !!!ack ('t262.1');
5734                  !!!next-token;
5735                  next B;
5736                } else {
5737                  !!!cp ('t263');
5738                #                #
5739              }              }
5740            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5741              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
5742                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5743                my $i;                  !!!cp ('t264');
5744                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});  
5745                  ## Ignore the token                  ## Ignore the token
5746                  !!!next-token;                  !!!next-token;
5747                  redo B;                  next B;
5748                }                } else {
5749                                  !!!cp ('t265');
5750                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
5751                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
5752                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
5753                     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]);  
5754                }                }
5755                } elsif ($token->{tag_name} eq 'col') {
5756                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
5757                  !!!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});  
5758                ## Ignore the token                ## Ignore the token
5759                !!!next-token;                !!!next-token;
5760                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;  
5761              } else {              } else {
5762                #                !!!cp ('t267');
5763                  #
5764              }              }
5765          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5766            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5767                @{$self->{open_elements}} == 1) { # redundant, maybe
5768              !!!cp ('t270.2');
5769              ## Stop parsing.
5770              last B;
5771            } else {
5772              ## NOTE: As if </colgroup>.
5773              !!!cp ('t270.1');
5774              pop @{$self->{open_elements}}; # colgroup
5775              $self->{insertion_mode} = IN_TABLE_IM;
5776              ## Reprocess.
5777              next B;
5778            }
5779          } else {
5780            die "$0: $token->{type}: Unknown token type";
5781          }
5782    
5783              ## As if </colgroup>
5784              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5785                !!!cp ('t269');
5786    ## TODO: Wrong error type?
5787                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5788                ## Ignore the token
5789                !!!nack ('t269.1');
5790                !!!next-token;
5791                next B;
5792            } else {            } else {
5793              #              !!!cp ('t270');
5794                pop @{$self->{open_elements}}; # colgroup
5795                $self->{insertion_mode} = IN_TABLE_IM;
5796                !!!ack-later;
5797                ## reprocess
5798                next B;
5799              }
5800        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5801          if ($token->{type} == CHARACTER_TOKEN) {
5802            !!!cp ('t271');
5803            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5804            !!!next-token;
5805            next B;
5806          } elsif ($token->{type} == START_TAG_TOKEN) {
5807            if ($token->{tag_name} eq 'option') {
5808              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5809                !!!cp ('t272');
5810                ## As if </option>
5811                pop @{$self->{open_elements}};
5812              } else {
5813                !!!cp ('t273');
5814            }            }
             
           $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}};  
               }  
5815    
5816                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5817                !!!next-token;            !!!nack ('t273.1');
5818                redo B;            !!!next-token;
5819              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5820                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5821                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5822                  pop @{$self->{open_elements}};              !!!cp ('t274');
5823                }              ## As if </option>
5824                pop @{$self->{open_elements}};
5825              } else {
5826                !!!cp ('t275');
5827              }
5828    
5829                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5830                  ## As if </optgroup>              !!!cp ('t276');
5831                  pop @{$self->{open_elements}};              ## As if </optgroup>
5832                }              pop @{$self->{open_elements}};
5833              } else {
5834                !!!cp ('t277');
5835              }
5836    
5837                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5838                !!!next-token;            !!!nack ('t277.1');
5839                redo B;            !!!next-token;
5840              } elsif ($token->{tag_name} eq 'select') {            next B;
5841                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5842                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5843                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5844                my $i;                    {
5845                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5846                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5847                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5848                    $i = $_;                    }->{$token->{tag_name}})) {
5849                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5850                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5851                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5852                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5853                    last INSCOPE;            ## have an element in table scope
5854                  }            my $i;
5855                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5856                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5857                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5858                  ## Ignore the token                !!!cp ('t278');
5859                  !!!next-token;                $i = $_;
5860                  redo B;                last INSCOPE;
5861                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5862                  !!!cp ('t279');
5863                  last INSCOPE;
5864                }
5865              } # INSCOPE
5866              unless (defined $i) {
5867                !!!cp ('t280');
5868                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5869                ## Ignore the token
5870                !!!nack ('t280.1');
5871                !!!next-token;
5872                next B;
5873              }
5874                                
5875                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5876              splice @{$self->{open_elements}}, $i;
5877    
5878                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5879    
5880                !!!next-token;            if ($token->{tag_name} eq 'select') {
5881                redo B;              !!!nack ('t281.2');
5882              } else {              !!!next-token;
5883                #              next B;
5884              } else {
5885                !!!cp ('t281.1');
5886                !!!ack-later;
5887                ## Reprocess the token.
5888                next B;
5889              }
5890            } else {
5891              !!!cp ('t282');
5892              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5893              ## Ignore the token
5894              !!!nack ('t282.1');
5895              !!!next-token;
5896              next B;
5897            }
5898          } elsif ($token->{type} == END_TAG_TOKEN) {
5899            if ($token->{tag_name} eq 'optgroup') {
5900              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5901                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5902                !!!cp ('t283');
5903                ## As if </option>
5904                splice @{$self->{open_elements}}, -2;
5905              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5906                !!!cp ('t284');
5907                pop @{$self->{open_elements}};
5908              } else {
5909                !!!cp ('t285');
5910                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5911                ## Ignore the token
5912              }
5913              !!!nack ('t285.1');
5914              !!!next-token;
5915              next B;
5916            } elsif ($token->{tag_name} eq 'option') {
5917              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5918                !!!cp ('t286');
5919                pop @{$self->{open_elements}};
5920              } else {
5921                !!!cp ('t287');
5922                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5923                ## Ignore the token
5924              }
5925              !!!nack ('t287.1');
5926              !!!next-token;
5927              next B;
5928            } elsif ($token->{tag_name} eq 'select') {
5929              ## have an element in table scope
5930              my $i;
5931              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5932                my $node = $self->{open_elements}->[$_];
5933                if ($node->[1] & SELECT_EL) {
5934                  !!!cp ('t288');
5935                  $i = $_;
5936                  last INSCOPE;
5937                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5938                  !!!cp ('t289');
5939                  last INSCOPE;
5940              }              }
5941            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
5942              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
5943                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
5944                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5945                  ## As if </option>              ## Ignore the token
5946                  splice @{$self->{open_elements}}, -2;              !!!nack ('t290.1');
5947                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!next-token;
5948                  pop @{$self->{open_elements}};              next B;
5949                } 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;  
               }  
5950                                
5951                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5952              splice @{$self->{open_elements}}, $i;
5953    
5954                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5955    
5956                !!!next-token;            !!!nack ('t291.1');
5957                redo B;            !!!next-token;
5958              } elsif ({            next B;
5959                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5960                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5961                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5962                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5963                                   }->{$token->{tag_name}}) {
5964                ## have an element in table scope  ## TODO: The following is wrong?
5965                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;  
               }  
5966                                
5967                ## As if </select>            ## have an element in table scope
5968                ## have an element in table scope            my $i;
5969                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5970                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
5971                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5972                  if ($node->[1] eq 'select') {                !!!cp ('t292');
5973                    $i = $_;                $i = $_;
5974                    last INSCOPE;                last INSCOPE;
5975                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5976                            table => 1, html => 1,                !!!cp ('t293');
5977                           }->{$node->[1]}) {                last INSCOPE;
5978                    last INSCOPE;              }
5979                  }            } # INSCOPE
5980                } # INSCOPE            unless (defined $i) {
5981                unless (defined $i) {              !!!cp ('t294');
5982                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
5983                  ## Ignore the </select> token              !!!nack ('t294.1');
5984                  !!!next-token; ## TODO: ok?              !!!next-token;
5985                  redo B;              next B;
5986                }            }
5987                                
5988                splice @{$self->{open_elements}}, $i;            ## As if </select>
5989              ## have an element in table scope
5990                $self->_reset_insertion_mode;            undef $i;
5991              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5992                ## reprocess              my $node = $self->{open_elements}->[$_];
5993                redo B;              if ($node->[1] & SELECT_EL) {
5994              } else {                !!!cp ('t295');
5995                #                $i = $_;
5996                  last INSCOPE;
5997                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5998    ## ISSUE: Can this state be reached?
5999                  !!!cp ('t296');
6000                  last INSCOPE;
6001              }              }
6002            } else {            } # INSCOPE
6003              #            unless (defined $i) {
6004                !!!cp ('t297');
6005    ## TODO: The following error type is correct?
6006                !!!parse-error (type => 'unmatched end tag:select', token => $token);
6007                ## Ignore the </select> token
6008                !!!nack ('t297.1');
6009                !!!next-token; ## TODO: ok?
6010                next B;
6011            }            }
6012                  
6013              !!!cp ('t298');
6014              splice @{$self->{open_elements}}, $i;
6015    
6016              $self->_reset_insertion_mode;
6017    
6018            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
6019              ## reprocess
6020              next B;
6021            } else {
6022              !!!cp ('t299');
6023              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6024            ## Ignore the token            ## Ignore the token
6025              !!!nack ('t299.3');
6026            !!!next-token;            !!!next-token;
6027            redo B;            next B;
6028          } elsif ($self->{insertion_mode} eq 'after body') {          }
6029            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6030              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6031                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
6032                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
6033              !!!parse-error (type => 'in body:#eof', token => $token);
6034            } else {
6035              !!!cp ('t299.2');
6036            }
6037    
6038            ## Stop parsing.
6039            last B;
6040          } else {
6041            die "$0: $token->{type}: Unknown token type";
6042          }
6043        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6044          if ($token->{type} == CHARACTER_TOKEN) {
6045            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6046              my $data = $1;
6047              ## As if in body
6048              $reconstruct_active_formatting_elements->($insert_to_current);
6049                                
6050                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6051              
6052              unless (length $token->{data}) {
6053                !!!cp ('t300');
6054                !!!next-token;
6055                next B;
6056              }
6057            }
6058            
6059            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6060              !!!cp ('t301');
6061              !!!parse-error (type => 'after html:#character', token => $token);
6062    
6063                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
6064                  !!!next-token;          } else {
6065                  redo B;            !!!cp ('t302');
6066                }          }
6067              }          
6068                        ## "after body" insertion mode
6069              #          !!!parse-error (type => 'after body:#character', token => $token);
6070              !!!parse-error (type => 'after body:#'.$token->{type});  
6071            } elsif ($token->{type} eq 'comment') {          $self->{insertion_mode} = IN_BODY_IM;
6072              my $comment = $self->{document}->create_comment ($token->{data});          ## reprocess
6073              $self->{open_elements}->[0]->[0]->append_child ($comment);          next B;
6074          } elsif ($token->{type} == START_TAG_TOKEN) {
6075            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6076              !!!cp ('t303');
6077              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6078              
6079              ## Reprocess in the "after body" insertion mode.
6080            } else {
6081              !!!cp ('t304');
6082            }
6083    
6084            ## "after body" insertion mode
6085            !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6086    
6087            $self->{insertion_mode} = IN_BODY_IM;
6088            !!!ack-later;
6089            ## reprocess
6090            next B;
6091          } elsif ($token->{type} == END_TAG_TOKEN) {
6092            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6093              !!!cp ('t305');
6094              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6095              
6096              $self->{insertion_mode} = AFTER_BODY_IM;
6097              ## Reprocess in the "after body" insertion mode.
6098            } else {
6099              !!!cp ('t306');
6100            }
6101    
6102            ## "after body" insertion mode
6103            if ($token->{tag_name} eq 'html') {
6104              if (defined $self->{inner_html_node}) {
6105                !!!cp ('t307');
6106                !!!parse-error (type => 'unmatched end tag:html', token => $token);
6107                ## Ignore the token
6108              !!!next-token;              !!!next-token;
6109              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});  
             }  
6110            } else {            } else {
6111              !!!parse-error (type => 'after body:#'.$token->{type});              !!!cp ('t308');
6112                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6113                !!!next-token;
6114                next B;
6115            }            }
6116            } else {
6117              !!!cp ('t309');
6118              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6119    
6120            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
6121            ## reprocess            ## reprocess
6122            redo B;            next B;
6123          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6124            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6125              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          !!!cp ('t309.2');
6126                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          ## Stop parsing
6127            last B;
6128          } else {
6129            die "$0: $token->{type}: Unknown token type";
6130          }
6131        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6132          if ($token->{type} == CHARACTER_TOKEN) {
6133            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6134              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6135              
6136              unless (length $token->{data}) {
6137                !!!cp ('t310');
6138                !!!next-token;
6139                next B;
6140              }
6141            }
6142            
6143            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6144              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6145                !!!cp ('t311');
6146                !!!parse-error (type => 'in frameset:#character', token => $token);
6147              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6148                !!!cp ('t312');
6149                !!!parse-error (type => 'after frameset:#character', token => $token);
6150              } else { # "after html frameset"
6151                !!!cp ('t313');
6152                !!!parse-error (type => 'after html:#character', token => $token);
6153    
6154                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6155                ## Reprocess in the "after frameset" insertion mode.
6156                !!!parse-error (type => 'after frameset:#character', token => $token);
6157              }
6158              
6159              ## Ignore the token.
6160              if (length $token->{data}) {
6161                !!!cp ('t314');
6162                ## reprocess the rest of characters
6163              } else {
6164                !!!cp ('t315');
6165                !!!next-token;
6166              }
6167              next B;
6168            }
6169            
6170            die qq[$0: Character "$token->{data}"];
6171          } elsif ($token->{type} == START_TAG_TOKEN) {
6172            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6173              !!!cp ('t316');
6174              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6175    
6176              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6177              ## Process in the "after frameset" insertion mode.
6178            } else {
6179              !!!cp ('t317');
6180            }
6181    
6182            if ($token->{tag_name} eq 'frameset' and
6183                $self->{insertion_mode} == IN_FRAMESET_IM) {
6184              !!!cp ('t318');
6185              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6186              !!!nack ('t318.1');
6187              !!!next-token;
6188              next B;
6189            } elsif ($token->{tag_name} eq 'frame' and
6190                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6191              !!!cp ('t319');
6192              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6193              pop @{$self->{open_elements}};
6194              !!!ack ('t319.1');
6195              !!!next-token;
6196              next B;
6197            } elsif ($token->{tag_name} eq 'noframes') {
6198              !!!cp ('t320');
6199              ## NOTE: As if in body.
6200              $parse_rcdata->(CDATA_CONTENT_MODEL);
6201              next B;
6202            } else {
6203              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6204                !!!cp ('t321');
6205                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6206              } else {
6207                !!!cp ('t322');
6208                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6209              }
6210              ## Ignore the token
6211              !!!nack ('t322.1');
6212              !!!next-token;
6213              next B;
6214            }
6215          } elsif ($token->{type} == END_TAG_TOKEN) {
6216            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6217              !!!cp ('t323');
6218              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6219    
6220                unless (length $token->{data}) {            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6221                  !!!next-token;            ## Process in the "after frameset" insertion mode.
6222                  redo B;          } else {
6223                }            !!!cp ('t324');
6224              }          }
6225    
6226              #          if ($token->{tag_name} eq 'frameset' and
6227            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6228              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6229              $self->{open_elements}->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
6230                !!!cp ('t325');
6231                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6232                ## Ignore the token
6233              !!!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 {  
               #  
             }  
6234            } else {            } else {
6235              #              !!!cp ('t326');
6236                pop @{$self->{open_elements}};
6237                !!!next-token;
6238            }            }
6239              
6240            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
6241              !!!parse-error (type => 'in frameset:'.$token->{tag_name});                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6242                !!!cp ('t327');
6243                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6244            } else {            } else {
6245              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t328');
6246              }
6247              next B;
6248            } elsif ($token->{tag_name} eq 'html' and
6249                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6250              !!!cp ('t329');
6251              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6252              !!!next-token;
6253              next B;
6254            } else {
6255              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6256                !!!cp ('t330');
6257                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6258              } else {
6259                !!!cp ('t331');
6260                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6261            }            }
6262            ## Ignore the token            ## Ignore the token
6263            !!!next-token;            !!!next-token;
6264            redo B;            next B;
6265          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6266            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6267              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6268                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                  @{$self->{open_elements}} == 1) { # redundant, maybe
6269              !!!cp ('t331.1');
6270              !!!parse-error (type => 'in body:#eof', token => $token);
6271            } else {
6272              !!!cp ('t331.2');
6273            }
6274            
6275            ## Stop parsing
6276            last B;
6277          } else {
6278            die "$0: $token->{type}: Unknown token type";
6279          }
6280    
6281                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
6282                  !!!next-token;      } else {
6283                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
6284                }      }
6285    
6286        ## "in body" insertion mode
6287        if ($token->{type} == START_TAG_TOKEN) {
6288          if ($token->{tag_name} eq 'script') {
6289            !!!cp ('t332');
6290            ## NOTE: This is an "as if in head" code clone
6291            $script_start_tag->();
6292            next B;
6293          } elsif ($token->{tag_name} eq 'style') {
6294            !!!cp ('t333');
6295            ## NOTE: This is an "as if in head" code clone
6296            $parse_rcdata->(CDATA_CONTENT_MODEL);
6297            next B;
6298          } elsif ({
6299                    base => 1, link => 1,
6300                   }->{$token->{tag_name}}) {
6301            !!!cp ('t334');
6302            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6303            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6304            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6305            !!!ack ('t334.1');
6306            !!!next-token;
6307            next B;
6308          } elsif ($token->{tag_name} eq 'meta') {
6309            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6310            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6311            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6312    
6313            unless ($self->{confident}) {
6314              if ($token->{attributes}->{charset}) {
6315                !!!cp ('t335');
6316                ## NOTE: Whether the encoding is supported or not is handled
6317                ## in the {change_encoding} callback.
6318                $self->{change_encoding}
6319                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6320                
6321                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6322                    ->set_user_data (manakai_has_reference =>
6323                                         $token->{attributes}->{charset}
6324                                             ->{has_reference});
6325              } elsif ($token->{attributes}->{content}) {
6326                if ($token->{attributes}->{content}->{value}
6327                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6328                        [\x09-\x0D\x20]*=
6329                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6330                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6331                  !!!cp ('t336');
6332                  ## NOTE: Whether the encoding is supported or not is handled
6333                  ## in the {change_encoding} callback.
6334                  $self->{change_encoding}
6335                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6336                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6337                      ->set_user_data (manakai_has_reference =>
6338                                           $token->{attributes}->{content}
6339                                                 ->{has_reference});
6340              }              }
6341              }
6342            } else {
6343              if ($token->{attributes}->{charset}) {
6344                !!!cp ('t337');
6345                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6346                    ->set_user_data (manakai_has_reference =>
6347                                         $token->{attributes}->{charset}
6348                                             ->{has_reference});
6349              }
6350              if ($token->{attributes}->{content}) {
6351                !!!cp ('t338');
6352                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6353                    ->set_user_data (manakai_has_reference =>
6354                                         $token->{attributes}->{content}
6355                                             ->{has_reference});
6356              }
6357            }
6358    
6359              #          !!!ack ('t338.1');
6360            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6361              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6362              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6363              !!!next-token;          !!!cp ('t341');
6364              redo B;          ## NOTE: This is an "as if in head" code clone
6365            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6366              if ($token->{tag_name} eq 'noframes') {          next B;
6367                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6368                redo B;          !!!parse-error (type => 'in body:body', token => $token);
6369              } else {                
6370                #          if (@{$self->{open_elements}} == 1 or
6371                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6372              !!!cp ('t342');
6373              ## Ignore the token
6374            } else {
6375              my $body_el = $self->{open_elements}->[1]->[0];
6376              for my $attr_name (keys %{$token->{attributes}}) {
6377                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6378                  !!!cp ('t343');
6379                  $body_el->set_attribute_ns
6380                    (undef, [undef, $attr_name],
6381                     $token->{attributes}->{$attr_name}->{value});
6382              }              }
6383            } elsif ($token->{type} eq 'end tag') {            }
6384              if ($token->{tag_name} eq 'html') {          }
6385                $phase = 'trailing end';          !!!nack ('t343.1');
6386            !!!next-token;
6387            next B;
6388          } elsif ({
6389                    address => 1, blockquote => 1, center => 1, dir => 1,
6390                    div => 1, dl => 1, fieldset => 1,
6391                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6392                    menu => 1, ol => 1, p => 1, ul => 1,
6393                    pre => 1, listing => 1,
6394                    form => 1,
6395                    table => 1,
6396                    hr => 1,
6397                   }->{$token->{tag_name}}) {
6398            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6399              !!!cp ('t350');
6400              !!!parse-error (type => 'in form:form', token => $token);
6401              ## Ignore the token
6402              !!!nack ('t350.1');
6403              !!!next-token;
6404              next B;
6405            }
6406    
6407            ## has a p element in scope
6408            INSCOPE: for (reverse @{$self->{open_elements}}) {
6409              if ($_->[1] & P_EL) {
6410                !!!cp ('t344');
6411                !!!back-token; # <form>
6412                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6413                          line => $token->{line}, column => $token->{column}};
6414                next B;
6415              } elsif ($_->[1] & SCOPING_EL) {
6416                !!!cp ('t345');
6417                last INSCOPE;
6418              }
6419            } # INSCOPE
6420              
6421            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6422            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6423              !!!nack ('t346.1');
6424              !!!next-token;
6425              if ($token->{type} == CHARACTER_TOKEN) {
6426                $token->{data} =~ s/^\x0A//;
6427                unless (length $token->{data}) {
6428                  !!!cp ('t346');
6429                !!!next-token;                !!!next-token;
               redo B;  
6430              } else {              } else {
6431                #                !!!cp ('t349');
6432              }              }
6433            } else {            } else {
6434              #              !!!cp ('t348');
6435              }
6436            } elsif ($token->{tag_name} eq 'form') {
6437              !!!cp ('t347.1');
6438              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6439    
6440              !!!nack ('t347.2');
6441              !!!next-token;
6442            } elsif ($token->{tag_name} eq 'table') {
6443              !!!cp ('t382');
6444              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6445              
6446              $self->{insertion_mode} = IN_TABLE_IM;
6447    
6448              !!!nack ('t382.1');
6449              !!!next-token;
6450            } elsif ($token->{tag_name} eq 'hr') {
6451              !!!cp ('t386');
6452              pop @{$self->{open_elements}};
6453            
6454              !!!nack ('t386.1');
6455              !!!next-token;
6456            } else {
6457              !!!nack ('t347.1');
6458              !!!next-token;
6459            }
6460            next B;
6461          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6462            ## has a p element in scope
6463            INSCOPE: for (reverse @{$self->{open_elements}}) {
6464              if ($_->[1] & P_EL) {
6465                !!!cp ('t353');
6466                !!!back-token; # <x>
6467                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6468                          line => $token->{line}, column => $token->{column}};
6469                next B;
6470              } elsif ($_->[1] & SCOPING_EL) {
6471                !!!cp ('t354');
6472                last INSCOPE;
6473            }            }
6474            } # INSCOPE
6475                        
6476            if (defined $token->{tag_name}) {          ## Step 1
6477              !!!parse-error (type => 'after frameset:'.$token->{tag_name});          my $i = -1;
6478            my $node = $self->{open_elements}->[$i];
6479            my $li_or_dtdd = {li => {li => 1},
6480                              dt => {dt => 1, dd => 1},
6481                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6482            LI: {
6483              ## Step 2
6484              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6485                if ($i != -1) {
6486                  !!!cp ('t355');
6487                  !!!parse-error (type => 'not closed',
6488                                  value => $self->{open_elements}->[-1]->[0]
6489                                      ->manakai_local_name,
6490                                  token => $token);
6491                } else {
6492                  !!!cp ('t356');
6493                }
6494                splice @{$self->{open_elements}}, $i;
6495                last LI;
6496            } else {            } else {
6497              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
6498            }            }
6499              
6500              ## Step 3
6501              if (not ($node->[1] & FORMATTING_EL) and
6502                  #not $phrasing_category->{$node->[1]} and
6503                  ($node->[1] & SPECIAL_EL or
6504                   $node->[1] & SCOPING_EL) and
6505                  not ($node->[1] & ADDRESS_EL) and
6506                  not ($node->[1] & DIV_EL)) {
6507                !!!cp ('t358');
6508                last LI;
6509              }
6510              
6511              !!!cp ('t359');
6512              ## Step 4
6513              $i--;
6514              $node = $self->{open_elements}->[$i];
6515              redo LI;
6516            } # LI
6517              
6518            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6519            !!!nack ('t359.1');
6520            !!!next-token;
6521            next B;
6522          } elsif ($token->{tag_name} eq 'plaintext') {
6523            ## has a p element in scope
6524            INSCOPE: for (reverse @{$self->{open_elements}}) {
6525              if ($_->[1] & P_EL) {
6526                !!!cp ('t367');
6527                !!!back-token; # <plaintext>
6528                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6529                          line => $token->{line}, column => $token->{column}};
6530                next B;
6531              } elsif ($_->[1] & SCOPING_EL) {
6532                !!!cp ('t368');
6533                last INSCOPE;
6534              }
6535            } # INSCOPE
6536              
6537            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6538              
6539            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6540              
6541            !!!nack ('t368.1');
6542            !!!next-token;
6543            next B;
6544          } elsif ($token->{tag_name} eq 'a') {
6545            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6546              my $node = $active_formatting_elements->[$i];
6547              if ($node->[1] & A_EL) {
6548                !!!cp ('t371');
6549                !!!parse-error (type => 'in a:a', token => $token);
6550                
6551                !!!back-token; # <a>
6552                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6553                          line => $token->{line}, column => $token->{column}};
6554                $formatting_end_tag->($token);
6555                
6556                AFE2: for (reverse 0..$#$active_formatting_elements) {
6557                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6558                    !!!cp ('t372');
6559                    splice @$active_formatting_elements, $_, 1;
6560                    last AFE2;
6561                  }
6562                } # AFE2
6563                OE: for (reverse 0..$#{$self->{open_elements}}) {
6564                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6565                    !!!cp ('t373');
6566                    splice @{$self->{open_elements}}, $_, 1;
6567                    last OE;
6568                  }
6569                } # OE
6570                last AFE;
6571              } elsif ($node->[0] eq '#marker') {
6572                !!!cp ('t374');
6573                last AFE;
6574              }
6575            } # AFE
6576              
6577            $reconstruct_active_formatting_elements->($insert_to_current);
6578    
6579            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6580            push @$active_formatting_elements, $self->{open_elements}->[-1];
6581    
6582            !!!nack ('t374.1');
6583            !!!next-token;
6584            next B;
6585          } elsif ($token->{tag_name} eq 'nobr') {
6586            $reconstruct_active_formatting_elements->($insert_to_current);
6587    
6588            ## has a |nobr| element in scope
6589            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6590              my $node = $self->{open_elements}->[$_];
6591              if ($node->[1] & NOBR_EL) {
6592                !!!cp ('t376');
6593                !!!parse-error (type => 'in nobr:nobr', token => $token);
6594                !!!back-token; # <nobr>
6595                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6596                          line => $token->{line}, column => $token->{column}};
6597                next B;
6598              } elsif ($node->[1] & SCOPING_EL) {
6599                !!!cp ('t377');
6600                last INSCOPE;
6601              }
6602            } # INSCOPE
6603            
6604            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6605            push @$active_formatting_elements, $self->{open_elements}->[-1];
6606            
6607            !!!nack ('t377.1');
6608            !!!next-token;
6609            next B;
6610          } elsif ($token->{tag_name} eq 'button') {
6611            ## has a button element in scope
6612            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6613              my $node = $self->{open_elements}->[$_];
6614              if ($node->[1] & BUTTON_EL) {
6615                !!!cp ('t378');
6616                !!!parse-error (type => 'in button:button', token => $token);
6617                !!!back-token; # <button>
6618                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6619                          line => $token->{line}, column => $token->{column}};
6620                next B;
6621              } elsif ($node->[1] & SCOPING_EL) {
6622                !!!cp ('t379');
6623                last INSCOPE;
6624              }
6625            } # INSCOPE
6626              
6627            $reconstruct_active_formatting_elements->($insert_to_current);
6628              
6629            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6630    
6631            ## TODO: associate with $self->{form_element} if defined
6632    
6633            push @$active_formatting_elements, ['#marker', ''];
6634    
6635            !!!nack ('t379.1');
6636            !!!next-token;
6637            next B;
6638          } elsif ({
6639                    xmp => 1,
6640                    iframe => 1,
6641                    noembed => 1,
6642                    noframes => 1,
6643                    noscript => 0, ## TODO: 1 if scripting is enabled
6644                   }->{$token->{tag_name}}) {
6645            if ($token->{tag_name} eq 'xmp') {
6646              !!!cp ('t381');
6647              $reconstruct_active_formatting_elements->($insert_to_current);
6648            } else {
6649              !!!cp ('t399');
6650            }
6651            ## NOTE: There is an "as if in body" code clone.
6652            $parse_rcdata->(CDATA_CONTENT_MODEL);
6653            next B;
6654          } elsif ($token->{tag_name} eq 'isindex') {
6655            !!!parse-error (type => 'isindex', token => $token);
6656            
6657            if (defined $self->{form_element}) {
6658              !!!cp ('t389');
6659            ## Ignore the token            ## Ignore the token
6660              !!!nack ('t389'); ## NOTE: Not acknowledged.
6661            !!!next-token;            !!!next-token;
6662            redo B;            next B;
6663            } else {
6664              my $at = $token->{attributes};
6665              my $form_attrs;
6666              $form_attrs->{action} = $at->{action} if $at->{action};
6667              my $prompt_attr = $at->{prompt};
6668              $at->{name} = {name => 'name', value => 'isindex'};
6669              delete $at->{action};
6670              delete $at->{prompt};
6671              my @tokens = (
6672                            {type => START_TAG_TOKEN, tag_name => 'form',
6673                             attributes => $form_attrs,
6674                             line => $token->{line}, column => $token->{column}},
6675                            {type => START_TAG_TOKEN, tag_name => 'hr',
6676                             line => $token->{line}, column => $token->{column}},
6677                            {type => START_TAG_TOKEN, tag_name => 'p',
6678                             line => $token->{line}, column => $token->{column}},
6679                            {type => START_TAG_TOKEN, tag_name => 'label',
6680                             line => $token->{line}, column => $token->{column}},
6681                           );
6682              if ($prompt_attr) {
6683                !!!cp ('t390');
6684                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6685                               #line => $token->{line}, column => $token->{column},
6686                              };
6687              } else {
6688                !!!cp ('t391');
6689                push @tokens, {type => CHARACTER_TOKEN,
6690                               data => 'This is a searchable index. Insert your search keywords here: ',
6691                               #line => $token->{line}, column => $token->{column},
6692                              }; # SHOULD
6693                ## TODO: make this configurable
6694              }
6695              push @tokens,
6696                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6697                             line => $token->{line}, column => $token->{column}},
6698                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6699                            {type => END_TAG_TOKEN, tag_name => 'label',
6700                             line => $token->{line}, column => $token->{column}},
6701                            {type => END_TAG_TOKEN, tag_name => 'p',
6702                             line => $token->{line}, column => $token->{column}},
6703                            {type => START_TAG_TOKEN, tag_name => 'hr',
6704                             line => $token->{line}, column => $token->{column}},
6705                            {type => END_TAG_TOKEN, tag_name => 'form',
6706                             line => $token->{line}, column => $token->{column}};
6707              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6708              !!!back-token (@tokens);
6709              !!!next-token;
6710              next B;
6711            }
6712          } elsif ($token->{tag_name} eq 'textarea') {
6713            my $tag_name = $token->{tag_name};
6714            my $el;
6715            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6716            
6717            ## TODO: $self->{form_element} if defined
6718            $self->{content_model} = RCDATA_CONTENT_MODEL;
6719            delete $self->{escape}; # MUST
6720            
6721            $insert->($el);
6722            
6723            my $text = '';
6724            !!!nack ('t392.1');
6725            !!!next-token;
6726            if ($token->{type} == CHARACTER_TOKEN) {
6727              $token->{data} =~ s/^\x0A//;
6728              unless (length $token->{data}) {
6729                !!!cp ('t392');
6730                !!!next-token;
6731              } else {
6732                !!!cp ('t393');
6733              }
6734            } else {
6735              !!!cp ('t394');
6736            }
6737            while ($token->{type} == CHARACTER_TOKEN) {
6738              !!!cp ('t395');
6739              $text .= $token->{data};
6740              !!!next-token;
6741            }
6742            if (length $text) {
6743              !!!cp ('t396');
6744              $el->manakai_append_text ($text);
6745            }
6746            
6747            $self->{content_model} = PCDATA_CONTENT_MODEL;
6748            
6749            if ($token->{type} == END_TAG_TOKEN and
6750                $token->{tag_name} eq $tag_name) {
6751              !!!cp ('t397');
6752              ## Ignore the token
6753            } else {
6754              !!!cp ('t398');
6755              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6756            }
6757            !!!next-token;
6758            next B;
6759          } elsif ($token->{tag_name} eq 'math' or
6760                   $token->{tag_name} eq 'svg') {
6761            $reconstruct_active_formatting_elements->($insert_to_current);
6762    
6763            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6764    
6765            ## ISSUE: An issue in spec there          ## "adjust foreign attributes" - done in insert-element-f
6766            
6767            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6768            
6769            if ($self->{self_closing}) {
6770              pop @{$self->{open_elements}};
6771              !!!ack ('t398.1');
6772          } else {          } else {
6773            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t398.2');
6774              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6775              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6776              ## mode, "in body" (not "in foreign content") secondary insertion
6777              ## mode, maybe.
6778          }          }
6779        }  
6780      } elsif ($phase eq 'trailing end') {          !!!next-token;
6781        ## states in the main stage is preserved yet # MUST          next B;
6782                } elsif ({
6783        if ($token->{type} eq 'DOCTYPE') {                  caption => 1, col => 1, colgroup => 1, frame => 1,
6784          !!!parse-error (type => 'after html:#DOCTYPE');                  frameset => 1, head => 1, option => 1, optgroup => 1,
6785                    tbody => 1, td => 1, tfoot => 1, th => 1,
6786                    thead => 1, tr => 1,
6787                   }->{$token->{tag_name}}) {
6788            !!!cp ('t401');
6789            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6790          ## Ignore the token          ## Ignore the token
6791            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6792          !!!next-token;          !!!next-token;
6793          redo B;          next B;
6794        } elsif ($token->{type} eq 'comment') {          
6795          my $comment = $self->{document}->create_comment ($token->{data});          ## ISSUE: An issue on HTML5 new elements in the spec.
6796          $self->{document}->append_child ($comment);        } else {
6797            if ($token->{tag_name} eq 'image') {
6798              !!!cp ('t384');
6799              !!!parse-error (type => 'image', token => $token);
6800              $token->{tag_name} = 'img';
6801            } else {
6802              !!!cp ('t385');
6803            }
6804    
6805            ## NOTE: There is an "as if <br>" code clone.
6806            $reconstruct_active_formatting_elements->($insert_to_current);
6807            
6808            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6809    
6810            if ({
6811                 applet => 1, marquee => 1, object => 1,
6812                }->{$token->{tag_name}}) {
6813              !!!cp ('t380');
6814              push @$active_formatting_elements, ['#marker', ''];
6815              !!!nack ('t380.1');
6816            } elsif ({
6817                      b => 1, big => 1, em => 1, font => 1, i => 1,
6818                      s => 1, small => 1, strile => 1,
6819                      strong => 1, tt => 1, u => 1,
6820                     }->{$token->{tag_name}}) {
6821              !!!cp ('t375');
6822              push @$active_formatting_elements, $self->{open_elements}->[-1];
6823              !!!nack ('t375.1');
6824            } elsif ($token->{tag_name} eq 'input') {
6825              !!!cp ('t388');
6826              ## TODO: associate with $self->{form_element} if defined
6827              pop @{$self->{open_elements}};
6828              !!!ack ('t388.2');
6829            } elsif ({
6830                      area => 1, basefont => 1, bgsound => 1, br => 1,
6831                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6832                      #image => 1,
6833                     }->{$token->{tag_name}}) {
6834              !!!cp ('t388.1');
6835              pop @{$self->{open_elements}};
6836              !!!ack ('t388.3');
6837            } elsif ($token->{tag_name} eq 'select') {
6838              ## TODO: associate with $self->{form_element} if defined
6839            
6840              if ($self->{insertion_mode} & TABLE_IMS or
6841                  $self->{insertion_mode} & BODY_TABLE_IMS or
6842                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6843                !!!cp ('t400.1');
6844                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6845              } else {
6846                !!!cp ('t400.2');
6847                $self->{insertion_mode} = IN_SELECT_IM;
6848              }
6849              !!!nack ('t400.3');
6850            } else {
6851              !!!nack ('t402');
6852            }
6853            
6854          !!!next-token;          !!!next-token;
6855          redo B;          next B;
6856        } elsif ($token->{type} eq 'character') {        }
6857          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6858            my $data = $1;        if ($token->{tag_name} eq 'body') {
6859            ## As if in the main phase.          ## has a |body| element in scope
6860            ## NOTE: The insertion mode in the main phase          my $i;
6861            ## just before the phase has been changed to the trailing          INSCOPE: {
6862            ## end phase is either "after body" or "after frameset".            for (reverse @{$self->{open_elements}}) {
6863            $reconstruct_active_formatting_elements->($insert_to_current)              if ($_->[1] & BODY_EL) {
6864              if $phase eq 'main';                !!!cp ('t405');
6865                  $i = $_;
6866                  last INSCOPE;
6867                } elsif ($_->[1] & SCOPING_EL) {
6868                  !!!cp ('t405.1');
6869                  last;
6870                }
6871              }
6872    
6873              !!!parse-error (type => 'start tag not allowed',
6874                              value => $token->{tag_name}, token => $token);
6875              ## NOTE: Ignore the token.
6876              !!!next-token;
6877              next B;
6878            } # INSCOPE
6879    
6880            for (@{$self->{open_elements}}) {
6881              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6882                !!!cp ('t403');
6883                !!!parse-error (type => 'not closed',
6884                                value => $_->[0]->manakai_local_name,
6885                                token => $token);
6886                last;
6887              } else {
6888                !!!cp ('t404');
6889              }
6890            }
6891    
6892            $self->{insertion_mode} = AFTER_BODY_IM;
6893            !!!next-token;
6894            next B;
6895          } elsif ($token->{tag_name} eq 'html') {
6896            ## TODO: Update this code.  It seems that the code below is not
6897            ## up-to-date, though it has same effect as speced.
6898            if (@{$self->{open_elements}} > 1 and
6899                $self->{open_elements}->[1]->[1] & BODY_EL) {
6900              ## ISSUE: There is an issue in the spec.
6901              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6902                !!!cp ('t406');
6903                !!!parse-error (type => 'not closed',
6904                                value => $self->{open_elements}->[1]->[0]
6905                                    ->manakai_local_name,
6906                                token => $token);
6907              } else {
6908                !!!cp ('t407');
6909              }
6910              $self->{insertion_mode} = AFTER_BODY_IM;
6911              ## reprocess
6912              next B;
6913            } else {
6914              !!!cp ('t408');
6915              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6916              ## Ignore the token
6917              !!!next-token;
6918              next B;
6919            }
6920          } elsif ({
6921                    address => 1, blockquote => 1, center => 1, dir => 1,
6922                    div => 1, dl => 1, fieldset => 1, listing => 1,
6923                    menu => 1, ol => 1, pre => 1, ul => 1,
6924                    dd => 1, dt => 1, li => 1,
6925                    applet => 1, button => 1, marquee => 1, object => 1,
6926                   }->{$token->{tag_name}}) {
6927            ## has an element in scope
6928            my $i;
6929            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6930              my $node = $self->{open_elements}->[$_];
6931              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6932                !!!cp ('t410');
6933                $i = $_;
6934                last INSCOPE;
6935              } elsif ($node->[1] & SCOPING_EL) {
6936                !!!cp ('t411');
6937                last INSCOPE;
6938              }
6939            } # INSCOPE
6940    
6941            unless (defined $i) { # has an element in scope
6942              !!!cp ('t413');
6943              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6944            } else {
6945              ## Step 1. generate implied end tags
6946              while ({
6947                      dd => ($token->{tag_name} ne 'dd'),
6948                      dt => ($token->{tag_name} ne 'dt'),
6949                      li => ($token->{tag_name} ne 'li'),
6950                      p => 1,
6951                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6952                !!!cp ('t409');
6953                pop @{$self->{open_elements}};
6954              }
6955    
6956              ## Step 2.
6957              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6958                      ne $token->{tag_name}) {
6959                !!!cp ('t412');
6960                !!!parse-error (type => 'not closed',
6961                                value => $self->{open_elements}->[-1]->[0]
6962                                    ->manakai_local_name,
6963                                token => $token);
6964              } else {
6965                !!!cp ('t414');
6966              }
6967    
6968              ## Step 3.
6969              splice @{$self->{open_elements}}, $i;
6970    
6971              ## Step 4.
6972              $clear_up_to_marker->()
6973                  if {
6974                    applet => 1, button => 1, marquee => 1, object => 1,
6975                  }->{$token->{tag_name}};
6976            }
6977            !!!next-token;
6978            next B;
6979          } elsif ($token->{tag_name} eq 'form') {
6980            undef $self->{form_element};
6981    
6982            ## has an element in scope
6983            my $i;
6984            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6985              my $node = $self->{open_elements}->[$_];
6986              if ($node->[1] & FORM_EL) {
6987                !!!cp ('t418');
6988                $i = $_;
6989                last INSCOPE;
6990              } elsif ($node->[1] & SCOPING_EL) {
6991                !!!cp ('t419');
6992                last INSCOPE;
6993              }
6994            } # INSCOPE
6995    
6996            unless (defined $i) { # has an element in scope
6997              !!!cp ('t421');
6998              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6999            } else {
7000              ## Step 1. generate implied end tags
7001              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7002                !!!cp ('t417');
7003                pop @{$self->{open_elements}};
7004              }
7005                        
7006            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
7007              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7008                      ne $token->{tag_name}) {
7009                !!!cp ('t417.1');
7010                !!!parse-error (type => 'not closed',
7011                                value => $self->{open_elements}->[-1]->[0]
7012                                    ->manakai_local_name,
7013                                token => $token);
7014              } else {
7015                !!!cp ('t420');
7016              }  
7017                        
7018            unless (length $token->{data}) {            ## Step 3.
7019              !!!next-token;            splice @{$self->{open_elements}}, $i;
7020              redo B;          }
7021    
7022            !!!next-token;
7023            next B;
7024          } elsif ({
7025                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7026                   }->{$token->{tag_name}}) {
7027            ## has an element in scope
7028            my $i;
7029            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7030              my $node = $self->{open_elements}->[$_];
7031              if ($node->[1] & HEADING_EL) {
7032                !!!cp ('t423');
7033                $i = $_;
7034                last INSCOPE;
7035              } elsif ($node->[1] & SCOPING_EL) {
7036                !!!cp ('t424');
7037                last INSCOPE;
7038              }
7039            } # INSCOPE
7040    
7041            unless (defined $i) { # has an element in scope
7042              !!!cp ('t425.1');
7043              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7044            } else {
7045              ## Step 1. generate implied end tags
7046              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7047                !!!cp ('t422');
7048                pop @{$self->{open_elements}};
7049              }
7050              
7051              ## Step 2.
7052              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7053                      ne $token->{tag_name}) {
7054                !!!cp ('t425');
7055                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7056              } else {
7057                !!!cp ('t426');
7058            }            }
7059    
7060              ## Step 3.
7061              splice @{$self->{open_elements}}, $i;
7062          }          }
7063            
7064            !!!next-token;
7065            next B;
7066          } elsif ($token->{tag_name} eq 'p') {
7067            ## has an element in scope
7068            my $i;
7069            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7070              my $node = $self->{open_elements}->[$_];
7071              if ($node->[1] & P_EL) {
7072                !!!cp ('t410.1');
7073                $i = $_;
7074                last INSCOPE;
7075              } elsif ($node->[1] & SCOPING_EL) {
7076                !!!cp ('t411.1');
7077                last INSCOPE;
7078              }
7079            } # INSCOPE
7080    
7081          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7082          $phase = 'main';            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7083          ## reprocess                    ne $token->{tag_name}) {
7084          redo B;              !!!cp ('t412.1');
7085        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
7086                 $token->{type} eq 'end tag') {                              value => $self->{open_elements}->[-1]->[0]
7087          !!!parse-error (type => 'after html:'.$token->{tag_name});                                  ->manakai_local_name,
7088          $phase = 'main';                              token => $token);
7089          ## reprocess            } else {
7090          redo B;              !!!cp ('t414.1');
7091        } elsif ($token->{type} eq 'end-of-file') {            }
7092          ## Stop parsing  
7093          last B;            splice @{$self->{open_elements}}, $i;
7094            } else {
7095              !!!cp ('t413.1');
7096              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7097    
7098              !!!cp ('t415.1');
7099              ## As if <p>, then reprocess the current token
7100              my $el;
7101              !!!create-element ($el, $HTML_NS, 'p',, $token);
7102              $insert->($el);
7103              ## NOTE: Not inserted into |$self->{open_elements}|.
7104            }
7105    
7106            !!!next-token;
7107            next B;
7108          } elsif ({
7109                    a => 1,
7110                    b => 1, big => 1, em => 1, font => 1, i => 1,
7111                    nobr => 1, s => 1, small => 1, strile => 1,
7112                    strong => 1, tt => 1, u => 1,
7113                   }->{$token->{tag_name}}) {
7114            !!!cp ('t427');
7115            $formatting_end_tag->($token);
7116            next B;
7117          } elsif ($token->{tag_name} eq 'br') {
7118            !!!cp ('t428');
7119            !!!parse-error (type => 'unmatched end tag:br', token => $token);
7120    
7121            ## As if <br>
7122            $reconstruct_active_formatting_elements->($insert_to_current);
7123            
7124            my $el;
7125            !!!create-element ($el, $HTML_NS, 'br',, $token);
7126            $insert->($el);
7127            
7128            ## Ignore the token.
7129            !!!next-token;
7130            next B;
7131          } elsif ({
7132                    caption => 1, col => 1, colgroup => 1, frame => 1,
7133                    frameset => 1, head => 1, option => 1, optgroup => 1,
7134                    tbody => 1, td => 1, tfoot => 1, th => 1,
7135                    thead => 1, tr => 1,
7136                    area => 1, basefont => 1, bgsound => 1,
7137                    embed => 1, hr => 1, iframe => 1, image => 1,
7138                    img => 1, input => 1, isindex => 1, noembed => 1,
7139                    noframes => 1, param => 1, select => 1, spacer => 1,
7140                    table => 1, textarea => 1, wbr => 1,
7141                    noscript => 0, ## TODO: if scripting is enabled
7142                   }->{$token->{tag_name}}) {
7143            !!!cp ('t429');
7144            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7145            ## Ignore the token
7146            !!!next-token;
7147            next B;
7148            
7149            ## ISSUE: Issue on HTML5 new elements in spec
7150            
7151        } else {        } else {
7152          die "$0: $token->{type}: Unknown token";          ## Step 1
7153            my $node_i = -1;
7154            my $node = $self->{open_elements}->[$node_i];
7155    
7156            ## Step 2
7157            S2: {
7158              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7159                ## Step 1
7160                ## generate implied end tags
7161                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7162                  !!!cp ('t430');
7163                  ## ISSUE: Can this case be reached?
7164                  pop @{$self->{open_elements}};
7165                }
7166            
7167                ## Step 2
7168                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7169                        ne $token->{tag_name}) {
7170                  !!!cp ('t431');
7171                  ## NOTE: <x><y></x>
7172                  !!!parse-error (type => 'not closed',
7173                                  value => $self->{open_elements}->[-1]->[0]
7174                                      ->manakai_local_name,
7175                                  token => $token);
7176                } else {
7177                  !!!cp ('t432');
7178                }
7179                
7180                ## Step 3
7181                splice @{$self->{open_elements}}, $node_i;
7182    
7183                !!!next-token;
7184                last S2;
7185              } else {
7186                ## Step 3
7187                if (not ($node->[1] & FORMATTING_EL) and
7188                    #not $phrasing_category->{$node->[1]} and
7189                    ($node->[1] & SPECIAL_EL or
7190                     $node->[1] & SCOPING_EL)) {
7191                  !!!cp ('t433');
7192                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7193                  ## Ignore the token
7194                  !!!next-token;
7195                  last S2;
7196                }
7197    
7198                !!!cp ('t434');
7199              }
7200              
7201              ## Step 4
7202              $node_i--;
7203              $node = $self->{open_elements}->[$node_i];
7204              
7205              ## Step 5;
7206              redo S2;
7207            } # S2
7208            next B;
7209        }        }
7210      }      }
7211        next B;
7212      } continue { # B
7213        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7214          ## NOTE: The code below is executed in cases where it does not have
7215          ## to be, but it it is harmless even in those cases.
7216          ## has an element in scope
7217          INSCOPE: {
7218            for (reverse 0..$#{$self->{open_elements}}) {
7219              my $node = $self->{open_elements}->[$_];
7220              if ($node->[1] & FOREIGN_EL) {
7221                last INSCOPE;
7222              } elsif ($node->[1] & SCOPING_EL) {
7223                last;
7224              }
7225            }
7226            
7227            ## NOTE: No foreign element in scope.
7228            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7229          } # INSCOPE
7230        }
7231    } # B    } # B
7232    
7233    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4822  sub set_inner_html ($$$) { Line 7241  sub set_inner_html ($$$) {
7241    my $s = \$_[0];    my $s = \$_[0];
7242    my $onerror = $_[1];    my $onerror = $_[1];
7243    
7244      ## ISSUE: Should {confident} be true?
7245    
7246    my $nt = $node->node_type;    my $nt = $node->node_type;
7247    if ($nt == 9) {    if ($nt == 9) {
7248      # MUST      # MUST
# Line 4844  sub set_inner_html ($$$) { Line 7265  sub set_inner_html ($$$) {
7265      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7266    
7267      ## Step 1 # MUST      ## Step 1 # MUST
7268      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
7269      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
7270        $doc->manakai_is_html (1);
7271      my $p = $class->new;      my $p = $class->new;
7272      $p->{document} = $doc;      $p->{document} = $doc;
7273    
7274      ## Step 9 # MUST      ## Step 8 # MUST
7275      my $i = 0;      my $i = 0;
7276      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7277      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7278      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7279        my $self = shift;        my $self = shift;
7280        $self->{next_input_character} = -1 and return if $i >= length $$s;  
7281        $self->{next_input_character} = ord substr $$s, $i++, 1;        pop @{$self->{prev_char}};
7282        $column++;        unshift @{$self->{prev_char}}, $self->{next_char};
7283    
7284        if ($self->{next_input_character} == 0x000A) { # LF        $self->{next_char} = -1 and return if $i >= length $$s;
7285          $line++;        $self->{next_char} = ord substr $$s, $i++, 1;
7286          $column = 0;  
7287        } elsif ($self->{next_input_character} == 0x000D) { # CR        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7288          if ($i >= length $$s) {        $p->{column}++;
7289            #  
7290          } else {        if ($self->{next_char} == 0x000A) { # LF
7291            my $next_char = ord substr $$s, $i++, 1;          $p->{line}++;
7292            if ($next_char == 0x000A) { # LF          $p->{column} = 0;
7293              #          !!!cp ('i1');
7294            } else {        } elsif ($self->{next_char} == 0x000D) { # CR
7295              push @{$self->{char}}, $next_char;          $i++ if substr ($$s, $i, 1) eq "\x0A";
7296            }          $self->{next_char} = 0x000A; # LF # MUST
7297          }          $p->{line}++;
7298          $self->{next_input_character} = 0x000A; # LF # MUST          $p->{column} = 0;
7299          $line++;          !!!cp ('i2');
7300          $column = 0;        } elsif ($self->{next_char} > 0x10FFFF) {
7301        } elsif ($self->{next_input_character} > 0x10FFFF) {          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7302          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          !!!cp ('i3');
7303        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_char} == 0x0000) { # NULL
7304          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          !!!cp ('i4');
7305            !!!parse-error (type => 'NULL');
7306            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7307          } elsif ($self->{next_char} <= 0x0008 or
7308                   (0x000E <= $self->{next_char} and
7309                    $self->{next_char} <= 0x001F) or
7310                   (0x007F <= $self->{next_char} and
7311                    $self->{next_char} <= 0x009F) or
7312                   (0xD800 <= $self->{next_char} and
7313                    $self->{next_char} <= 0xDFFF) or
7314                   (0xFDD0 <= $self->{next_char} and
7315                    $self->{next_char} <= 0xFDDF) or
7316                   {
7317                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7318                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7319                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7320                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7321                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7322                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7323                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7324                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7325                    0x10FFFE => 1, 0x10FFFF => 1,
7326                   }->{$self->{next_char}}) {
7327            !!!cp ('i4.1');
7328            !!!parse-error (type => 'control char', level => $self->{must_level});
7329    ## TODO: error type documentation
7330        }        }
7331      };      };
7332        $p->{prev_char} = [-1, -1, -1];
7333        $p->{next_char} = -1;
7334            
7335      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7336        my (%opt) = @_;        my (%opt) = @_;
7337        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7338          my $column = $opt{column};
7339          if (defined $opt{token} and defined $opt{token}->{line}) {
7340            $line = $opt{token}->{line};
7341            $column = $opt{token}->{column};
7342          }
7343          warn "Parse error ($opt{type}) at line $line column $column\n";
7344      };      };
7345      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7346        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7347      };      };
7348            
7349      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7350      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7351    
7352      ## Step 2      ## Step 2
7353      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7354      $p->{content_model_flag} = {      $p->{content_model} = {
7355        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7356        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7357        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7358        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7359        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7360        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7361        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7362        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7363        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7364        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7365      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7366         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7367            unless defined $p->{content_model};
7368            ## ISSUE: What is "the name of the element"? local name?
7369    
7370      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7371          ## TODO: Foreign element OK?
7372    
7373      ## Step 4      ## Step 3
7374      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7375        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7376    
7377      ## Step 5 # MUST      ## Step 4 # MUST
7378      $doc->append_child ($root);      $doc->append_child ($root);
7379    
7380      ## Step 6 # MUST      ## Step 5 # MUST
7381      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7382    
7383      undef $p->{head_element};      undef $p->{head_element};
7384    
7385      ## Step 7 # MUST      ## Step 6 # MUST
7386      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7387    
7388      ## Step 8 # MUST      ## Step 7 # MUST
7389      my $anode = $node;      my $anode = $node;
7390      AN: while (defined $anode) {      AN: while (defined $anode) {
7391        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7392          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7393          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7394            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7395                !!!cp ('i5');
7396              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7397              last AN;              last AN;
7398            }            }
# Line 4942  sub set_inner_html ($$$) { Line 7401  sub set_inner_html ($$$) {
7401        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7402      } # AN      } # AN
7403            
7404      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7405      {      {
7406        my $self = $p;        my $self = $p;
7407        !!!next-token;        !!!next-token;
7408      }      }
7409      $p->_tree_construction_main;      $p->_tree_construction_main;
7410    
7411      ## Step 11 # MUST      ## Step 10 # MUST
7412      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7413      for (@cn) {      for (@cn) {
7414        $node->remove_child ($_);        $node->remove_child ($_);
7415      }      }
7416      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7417    
7418      ## Step 12 # MUST      ## Step 11 # MUST
7419      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7420      for (@cn) {      for (@cn) {
7421          $this_doc->adopt_node ($_);
7422        $node->append_child ($_);        $node->append_child ($_);
7423      }      }
7424      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
7425    
7426      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7427    
7428        delete $p->{parse_error}; # delete loop
7429    } else {    } else {
7430      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";
7431    }    }
# Line 4972  sub set_inner_html ($$$) { Line 7433  sub set_inner_html ($$$) {
7433    
7434  } # tree construction stage  } # tree construction stage
7435    
7436  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7437    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  
7438    
7439  1;  1;
7440  # $Date$  # $Date$

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.140

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24