/[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.86 by wakaba, Thu Mar 6 15:56:52 2008 UTC revision 1.210 by wakaba, Tue Oct 14 13:24:52 2008 UTC
# Line 3  use strict; Line 3  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);  use Error qw(:try);
5    
6    use Whatpm::HTML::Tokenizer;
7    
8    ## NOTE: This module don't check all HTML5 parse errors; character
9    ## encoding related parse errors are expected to be handled by relevant
10    ## modules.
11    ## Parse errors for control characters that are not allowed in HTML5
12    ## documents, for surrogate code points, and for noncharacter code
13    ## points, as well as U+FFFD substitions for characters whose code points
14    ## is higher than U+10FFFF may be detected by combining the parser with
15    ## the checker implemented by Whatpm::Charset::UnicodeChecker (for its
16    ## usage example, see |t/HTML-tree.t| in the Whatpm package or the
17    ## WebHACC::Language::HTML module in the WebHACC package).
18    
19  ## ISSUE:  ## ISSUE:
20  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
21  ## doc.write ('');  ## doc.write ('');
22  ## alert (doc.compatMode);  ## alert (doc.compatMode);
23    
24  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  require IO::Handle;
25  ## TODO: 1252 parse error (revision 1264)  
26  ## TODO: 8859-11 = 874 (revision 1271)  ## Namespace URLs
27    
28  my $permitted_slash_tag_name = {  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
29    base => 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
30    link => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
31    meta => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
32    hr => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
33    br => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
34    img => 1,  
35    embed => 1,  ## Element categories
36    param => 1,  
37    area => 1,  ## Bits 12-15
38    col => 1,  sub SPECIAL_EL () { 0b1_000000000000000 }
39    input => 1,  sub SCOPING_EL () { 0b1_00000000000000 }
40    sub FORMATTING_EL () { 0b1_0000000000000 }
41    sub PHRASING_EL () { 0b1_000000000000 }
42    
43    ## Bits 10-11
44    #sub FOREIGN_EL () { 0b1_00000000000 } # see Whatpm::HTML::Tokenizer
45    sub FOREIGN_FLOW_CONTENT_EL () { 0b1_0000000000 }
46    
47    ## Bits 6-9
48    sub TABLE_SCOPING_EL () { 0b1_000000000 }
49    sub TABLE_ROWS_SCOPING_EL () { 0b1_00000000 }
50    sub TABLE_ROW_SCOPING_EL () { 0b1_0000000 }
51    sub TABLE_ROWS_EL () { 0b1_000000 }
52    
53    ## Bit 5
54    sub ADDRESS_DIV_P_EL () { 0b1_00000 }
55    
56    ## NOTE: Used in </body> and EOF algorithms.
57    ## Bit 4
58    sub ALL_END_TAG_OPTIONAL_EL () { 0b1_0000 }
59    
60    ## NOTE: Used in "generate implied end tags" algorithm.
61    ## NOTE: There is a code where a modified version of
62    ## END_TAG_OPTIONAL_EL is used in "generate implied end tags"
63    ## implementation (search for the algorithm name).
64    ## Bit 3
65    sub END_TAG_OPTIONAL_EL () { 0b1_000 }
66    
67    ## Bits 0-2
68    
69    sub MISC_SPECIAL_EL () { SPECIAL_EL | 0b000 }
70    sub FORM_EL () { SPECIAL_EL | 0b001 }
71    sub FRAMESET_EL () { SPECIAL_EL | 0b010 }
72    sub HEADING_EL () { SPECIAL_EL | 0b011 }
73    sub SELECT_EL () { SPECIAL_EL | 0b100 }
74    sub SCRIPT_EL () { SPECIAL_EL | 0b101 }
75    
76    sub ADDRESS_DIV_EL () { SPECIAL_EL | ADDRESS_DIV_P_EL | 0b001 }
77    sub BODY_EL () { SPECIAL_EL | ALL_END_TAG_OPTIONAL_EL | 0b001 }
78    
79    sub DTDD_EL () {
80      SPECIAL_EL |
81      END_TAG_OPTIONAL_EL |
82      ALL_END_TAG_OPTIONAL_EL |
83      0b010
84    }
85    sub LI_EL () {
86      SPECIAL_EL |
87      END_TAG_OPTIONAL_EL |
88      ALL_END_TAG_OPTIONAL_EL |
89      0b100
90    }
91    sub P_EL () {
92      SPECIAL_EL |
93      ADDRESS_DIV_P_EL |
94      END_TAG_OPTIONAL_EL |
95      ALL_END_TAG_OPTIONAL_EL |
96      0b001
97    }
98    
99    sub TABLE_ROW_EL () {
100      SPECIAL_EL |
101      TABLE_ROWS_EL |
102      TABLE_ROW_SCOPING_EL |
103      ALL_END_TAG_OPTIONAL_EL |
104      0b001
105    }
106    sub TABLE_ROW_GROUP_EL () {
107      SPECIAL_EL |
108      TABLE_ROWS_EL |
109      TABLE_ROWS_SCOPING_EL |
110      ALL_END_TAG_OPTIONAL_EL |
111      0b001
112    }
113    
114    sub MISC_SCOPING_EL () { SCOPING_EL | 0b000 }
115    sub BUTTON_EL () { SCOPING_EL | 0b001 }
116    sub CAPTION_EL () { SCOPING_EL | 0b010 }
117    sub HTML_EL () {
118      SCOPING_EL |
119      TABLE_SCOPING_EL |
120      TABLE_ROWS_SCOPING_EL |
121      TABLE_ROW_SCOPING_EL |
122      ALL_END_TAG_OPTIONAL_EL |
123      0b001
124    }
125    sub TABLE_EL () {
126      SCOPING_EL |
127      TABLE_ROWS_EL |
128      TABLE_SCOPING_EL |
129      0b001
130    }
131    sub TABLE_CELL_EL () {
132      SCOPING_EL |
133      TABLE_ROW_SCOPING_EL |
134      ALL_END_TAG_OPTIONAL_EL |
135      0b001
136    }
137    
138    sub MISC_FORMATTING_EL () { FORMATTING_EL | 0b000 }
139    sub A_EL () { FORMATTING_EL | 0b001 }
140    sub NOBR_EL () { FORMATTING_EL | 0b010 }
141    
142    sub RUBY_EL () { PHRASING_EL | 0b001 }
143    
144    ## ISSUE: ALL_END_TAG_OPTIONAL_EL?
145    sub OPTGROUP_EL () { PHRASING_EL | END_TAG_OPTIONAL_EL | 0b001 }
146    sub OPTION_EL () { PHRASING_EL | END_TAG_OPTIONAL_EL | 0b010 }
147    sub RUBY_COMPONENT_EL () { PHRASING_EL | END_TAG_OPTIONAL_EL | 0b100 }
148    
149    sub MML_AXML_EL () { PHRASING_EL | FOREIGN_EL | 0b001 }
150    
151    my $el_category = {
152      a => A_EL,
153      address => ADDRESS_DIV_EL,
154      applet => MISC_SCOPING_EL,
155      area => MISC_SPECIAL_EL,
156      article => MISC_SPECIAL_EL,
157      aside => MISC_SPECIAL_EL,
158      b => FORMATTING_EL,
159      base => MISC_SPECIAL_EL,
160      basefont => MISC_SPECIAL_EL,
161      bgsound => MISC_SPECIAL_EL,
162      big => FORMATTING_EL,
163      blockquote => MISC_SPECIAL_EL,
164      body => BODY_EL,
165      br => MISC_SPECIAL_EL,
166      button => BUTTON_EL,
167      caption => CAPTION_EL,
168      center => MISC_SPECIAL_EL,
169      col => MISC_SPECIAL_EL,
170      colgroup => MISC_SPECIAL_EL,
171      command => MISC_SPECIAL_EL,
172      datagrid => MISC_SPECIAL_EL,
173      dd => DTDD_EL,
174      details => MISC_SPECIAL_EL,
175      dialog => MISC_SPECIAL_EL,
176      dir => MISC_SPECIAL_EL,
177      div => ADDRESS_DIV_EL,
178      dl => MISC_SPECIAL_EL,
179      dt => DTDD_EL,
180      em => FORMATTING_EL,
181      embed => MISC_SPECIAL_EL,
182      eventsource => MISC_SPECIAL_EL,
183      fieldset => MISC_SPECIAL_EL,
184      figure => MISC_SPECIAL_EL,
185      font => FORMATTING_EL,
186      footer => MISC_SPECIAL_EL,
187      form => FORM_EL,
188      frame => MISC_SPECIAL_EL,
189      frameset => FRAMESET_EL,
190      h1 => HEADING_EL,
191      h2 => HEADING_EL,
192      h3 => HEADING_EL,
193      h4 => HEADING_EL,
194      h5 => HEADING_EL,
195      h6 => HEADING_EL,
196      head => MISC_SPECIAL_EL,
197      header => MISC_SPECIAL_EL,
198      hr => MISC_SPECIAL_EL,
199      html => HTML_EL,
200      i => FORMATTING_EL,
201      iframe => MISC_SPECIAL_EL,
202      img => MISC_SPECIAL_EL,
203      #image => MISC_SPECIAL_EL, ## NOTE: Commented out in the spec.
204      input => MISC_SPECIAL_EL,
205      isindex => MISC_SPECIAL_EL,
206      li => LI_EL,
207      link => MISC_SPECIAL_EL,
208      listing => MISC_SPECIAL_EL,
209      marquee => MISC_SCOPING_EL,
210      menu => MISC_SPECIAL_EL,
211      meta => MISC_SPECIAL_EL,
212      nav => MISC_SPECIAL_EL,
213      nobr => NOBR_EL,
214      noembed => MISC_SPECIAL_EL,
215      noframes => MISC_SPECIAL_EL,
216      noscript => MISC_SPECIAL_EL,
217      object => MISC_SCOPING_EL,
218      ol => MISC_SPECIAL_EL,
219      optgroup => OPTGROUP_EL,
220      option => OPTION_EL,
221      p => P_EL,
222      param => MISC_SPECIAL_EL,
223      plaintext => MISC_SPECIAL_EL,
224      pre => MISC_SPECIAL_EL,
225      rp => RUBY_COMPONENT_EL,
226      rt => RUBY_COMPONENT_EL,
227      ruby => RUBY_EL,
228      s => FORMATTING_EL,
229      script => MISC_SPECIAL_EL,
230      select => SELECT_EL,
231      section => MISC_SPECIAL_EL,
232      small => FORMATTING_EL,
233      spacer => MISC_SPECIAL_EL,
234      strike => FORMATTING_EL,
235      strong => FORMATTING_EL,
236      style => MISC_SPECIAL_EL,
237      table => TABLE_EL,
238      tbody => TABLE_ROW_GROUP_EL,
239      td => TABLE_CELL_EL,
240      textarea => MISC_SPECIAL_EL,
241      tfoot => TABLE_ROW_GROUP_EL,
242      th => TABLE_CELL_EL,
243      thead => TABLE_ROW_GROUP_EL,
244      title => MISC_SPECIAL_EL,
245      tr => TABLE_ROW_EL,
246      tt => FORMATTING_EL,
247      u => FORMATTING_EL,
248      ul => MISC_SPECIAL_EL,
249      wbr => MISC_SPECIAL_EL,
250  };  };
251    
252  my $c1_entity_char = {  my $el_category_f = {
253    0x80 => 0x20AC,    $MML_NS => {
254    0x81 => 0xFFFD,      'annotation-xml' => MML_AXML_EL,
255    0x82 => 0x201A,      mi => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
256    0x83 => 0x0192,      mo => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
257    0x84 => 0x201E,      mn => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
258    0x85 => 0x2026,      ms => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
259    0x86 => 0x2020,      mtext => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
260    0x87 => 0x2021,    },
261    0x88 => 0x02C6,    $SVG_NS => {
262    0x89 => 0x2030,      foreignObject => SCOPING_EL | FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
263    0x8A => 0x0160,      desc => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
264    0x8B => 0x2039,      title => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
265    0x8C => 0x0152,    },
266    0x8D => 0xFFFD,    ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
   0x8E => 0x017D,  
   0x8F => 0xFFFD,  
   0x90 => 0xFFFD,  
   0x91 => 0x2018,  
   0x92 => 0x2019,  
   0x93 => 0x201C,  
   0x94 => 0x201D,  
   0x95 => 0x2022,  
   0x96 => 0x2013,  
   0x97 => 0x2014,  
   0x98 => 0x02DC,  
   0x99 => 0x2122,  
   0x9A => 0x0161,  
   0x9B => 0x203A,  
   0x9C => 0x0153,  
   0x9D => 0xFFFD,  
   0x9E => 0x017E,  
   0x9F => 0x0178,  
 }; # $c1_entity_char  
   
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
267  };  };
268  my $scoping_category = {  
269    button => 1, caption => 1, html => 1, marquee => 1, object => 1,  my $svg_attr_name = {
270    table => 1, td => 1, th => 1,    attributename => 'attributeName',
271      attributetype => 'attributeType',
272      basefrequency => 'baseFrequency',
273      baseprofile => 'baseProfile',
274      calcmode => 'calcMode',
275      clippathunits => 'clipPathUnits',
276      contentscripttype => 'contentScriptType',
277      contentstyletype => 'contentStyleType',
278      diffuseconstant => 'diffuseConstant',
279      edgemode => 'edgeMode',
280      externalresourcesrequired => 'externalResourcesRequired',
281      filterres => 'filterRes',
282      filterunits => 'filterUnits',
283      glyphref => 'glyphRef',
284      gradienttransform => 'gradientTransform',
285      gradientunits => 'gradientUnits',
286      kernelmatrix => 'kernelMatrix',
287      kernelunitlength => 'kernelUnitLength',
288      keypoints => 'keyPoints',
289      keysplines => 'keySplines',
290      keytimes => 'keyTimes',
291      lengthadjust => 'lengthAdjust',
292      limitingconeangle => 'limitingConeAngle',
293      markerheight => 'markerHeight',
294      markerunits => 'markerUnits',
295      markerwidth => 'markerWidth',
296      maskcontentunits => 'maskContentUnits',
297      maskunits => 'maskUnits',
298      numoctaves => 'numOctaves',
299      pathlength => 'pathLength',
300      patterncontentunits => 'patternContentUnits',
301      patterntransform => 'patternTransform',
302      patternunits => 'patternUnits',
303      pointsatx => 'pointsAtX',
304      pointsaty => 'pointsAtY',
305      pointsatz => 'pointsAtZ',
306      preservealpha => 'preserveAlpha',
307      preserveaspectratio => 'preserveAspectRatio',
308      primitiveunits => 'primitiveUnits',
309      refx => 'refX',
310      refy => 'refY',
311      repeatcount => 'repeatCount',
312      repeatdur => 'repeatDur',
313      requiredextensions => 'requiredExtensions',
314      requiredfeatures => 'requiredFeatures',
315      specularconstant => 'specularConstant',
316      specularexponent => 'specularExponent',
317      spreadmethod => 'spreadMethod',
318      startoffset => 'startOffset',
319      stddeviation => 'stdDeviation',
320      stitchtiles => 'stitchTiles',
321      surfacescale => 'surfaceScale',
322      systemlanguage => 'systemLanguage',
323      tablevalues => 'tableValues',
324      targetx => 'targetX',
325      targety => 'targetY',
326      textlength => 'textLength',
327      viewbox => 'viewBox',
328      viewtarget => 'viewTarget',
329      xchannelselector => 'xChannelSelector',
330      ychannelselector => 'yChannelSelector',
331      zoomandpan => 'zoomAndPan',
332  };  };
333  my $formatting_category = {  
334    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  my $foreign_attr_xname = {
335    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,    'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
336      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
337      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
338      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
339      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
340      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
341      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
342      'xml:base' => [$XML_NS, ['xml', 'base']],
343      'xml:lang' => [$XML_NS, ['xml', 'lang']],
344      'xml:space' => [$XML_NS, ['xml', 'space']],
345      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
346      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
347  };  };
348  # $phrasing_category: all other elements  
349    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
350    
351    ## TODO: Invoke the reset algorithm when a resettable element is
352    ## created (cf. HTML5 revision 2259).
353    
354  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
355      my $self = shift;
356      my $charset_name = shift;
357      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
358      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
359    } # parse_byte_string
360    
361    sub parse_byte_stream ($$$$;$$) {
362      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
363    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
364    my $charset = shift;    my $charset_name = shift;
365    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
366    
367    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
368      my $self = shift;      my (%opt) = @_;
369      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
370      ## TODO: if $charset is supported    };
371      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
372    
373      ## "Change the encoding" algorithm:    my $get_wrapper = $_[3] || sub ($) {
374        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
375      ## Step 1        };
376      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?  
377        $charset = 'utf-8';    ## HTML5 encoding sniffing algorithm
378      require Message::Charset::Info;
379      my $charset;
380      my $buffer;
381      my ($char_stream, $e_status);
382    
383      SNIFFING: {
384        ## NOTE: By setting |allow_fallback| option true when the
385        ## |get_decode_handle| method is invoked, we ignore what the HTML5
386        ## spec requires, i.e. unsupported encoding should be ignored.
387          ## TODO: We should not do this unless the parser is invoked
388          ## in the conformance checking mode, in which this behavior
389          ## would be useful.
390    
391        ## Step 1
392        if (defined $charset_name) {
393          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
394              ## TODO: Is this ok?  Transfer protocol's parameter should be
395              ## interpreted in its semantics?
396    
397          ($char_stream, $e_status) = $charset->get_decode_handle
398              ($byte_stream, allow_error_reporting => 1,
399               allow_fallback => 1);
400          if ($char_stream) {
401            $self->{confident} = 1;
402            last SNIFFING;
403          } else {
404            !!!parse-error (type => 'charset:not supported',
405                            layer => 'encode',
406                            line => 1, column => 1,
407                            value => $charset_name,
408                            level => $self->{level}->{uncertain});
409          }
410      }      }
411    
412      ## Step 2      ## Step 2
413      if (defined $self->{input_encoding} and      my $byte_buffer = '';
414          $self->{input_encoding} eq $charset) {      for (1..1024) {
415          my $char = $byte_stream->getc;
416          last unless defined $char;
417          $byte_buffer .= $char;
418        } ## TODO: timeout
419    
420        ## Step 3
421        if ($byte_buffer =~ /^\xFE\xFF/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426        $self->{confident} = 1;        $self->{confident} = 1;
427        return;        last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
436          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
437          ($char_stream, $e_status) = $charset->get_decode_handle
438              ($byte_stream, allow_error_reporting => 1,
439               allow_fallback => 1, byte_buffer => \$byte_buffer);
440          $self->{confident} = 1;
441          last SNIFFING;
442      }      }
443    
444      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
445          ':'.$charset, level => 'w');      ## TODO: <meta charset>
446    
447      ## Step 3      ## Step 5
448      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
449    
450      ## Step 4      ## Step 6
451      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
452        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
453            ($byte_buffer);
454        if (defined $charset_name) {
455          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
456    
457          require Whatpm::Charset::DecodeHandle;
458          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
459              ($byte_stream);
460          ($char_stream, $e_status) = $charset->get_decode_handle
461              ($buffer, allow_error_reporting => 1,
462               allow_fallback => 1, byte_buffer => \$byte_buffer);
463          if ($char_stream) {
464            $buffer->{buffer} = $byte_buffer;
465            !!!parse-error (type => 'sniffing:chardet',
466                            text => $charset_name,
467                            level => $self->{level}->{info},
468                            layer => 'encode',
469                            line => 1, column => 1);
470            $self->{confident} = 0;
471            last SNIFFING;
472          }
473        }
474    
475        ## Step 7: default
476        ## TODO: Make this configurable.
477        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
478            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
479            ## detectable in the step 6.
480        require Whatpm::Charset::DecodeHandle;
481        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
482            ($byte_stream);
483        ($char_stream, $e_status)
484            = $charset->get_decode_handle ($buffer,
485                                           allow_error_reporting => 1,
486                                           allow_fallback => 1,
487                                           byte_buffer => \$byte_buffer);
488        $buffer->{buffer} = $byte_buffer;
489        !!!parse-error (type => 'sniffing:default',
490                        text => 'windows-1252',
491                        level => $self->{level}->{info},
492                        line => 1, column => 1,
493                        layer => 'encode');
494        $self->{confident} = 0;
495      } # SNIFFING
496    
497      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
498        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
499        !!!parse-error (type => 'chardecode:fallback',
500                        #text => $self->{input_encoding},
501                        level => $self->{level}->{uncertain},
502                        line => 1, column => 1,
503                        layer => 'encode');
504      } elsif (not ($e_status &
505                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
506        $self->{input_encoding} = $charset->get_iana_name;
507        !!!parse-error (type => 'chardecode:no error',
508                        text => $self->{input_encoding},
509                        level => $self->{level}->{uncertain},
510                        line => 1, column => 1,
511                        layer => 'encode');
512      } else {
513        $self->{input_encoding} = $charset->get_iana_name;
514      }
515    
516      $self->{change_encoding} = sub {
517        my $self = shift;
518        $charset_name = shift;
519        my $token = shift;
520    
521        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
522        ($char_stream, $e_status) = $charset->get_decode_handle
523            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
524             byte_buffer => \ $buffer->{buffer});
525        
526        if ($char_stream) { # if supported
527          ## "Change the encoding" algorithm:
528    
529          ## Step 1    
530          if ($charset->{category} &
531              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
532            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
533            ($char_stream, $e_status) = $charset->get_decode_handle
534                ($byte_stream,
535                 byte_buffer => \ $buffer->{buffer});
536          }
537          $charset_name = $charset->get_iana_name;
538          
539          ## Step 2
540          if (defined $self->{input_encoding} and
541              $self->{input_encoding} eq $charset_name) {
542            !!!parse-error (type => 'charset label:matching',
543                            text => $charset_name,
544                            level => $self->{level}->{info});
545            $self->{confident} = 1;
546            return;
547          }
548    
549          !!!parse-error (type => 'charset label detected',
550                          text => $self->{input_encoding},
551                          value => $charset_name,
552                          level => $self->{level}->{warn},
553                          token => $token);
554          
555          ## Step 3
556          # if (can) {
557            ## change the encoding on the fly.
558            #$self->{confident} = 1;
559            #return;
560          # }
561          
562          ## Step 4
563          throw Whatpm::HTML::RestartParser ();
564        }
565    }; # $self->{change_encoding}    }; # $self->{change_encoding}
566    
567    my @args = @_; shift @args; # $s    my $char_onerror = sub {
568        my (undef, $type, %opt) = @_;
569        !!!parse-error (layer => 'encode',
570                        line => $self->{line}, column => $self->{column} + 1,
571                        %opt, type => $type);
572        if ($opt{octets}) {
573          ${$opt{octets}} = "\x{FFFD}"; # relacement character
574        }
575      };
576    
577      my $wrapped_char_stream = $get_wrapper->($char_stream);
578      $wrapped_char_stream->onerror ($char_onerror);
579    
580      my @args = ($_[1], $_[2]); # $doc, $onerror - $get_wrapper = undef;
581    my $return;    my $return;
582    try {    try {
583      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
584    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
585      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
586      $s = \ (Encode::decode ($charset, $$bytes_s));      
587      $self->{input_encoding} = $charset; ## TODO: normalize      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
588          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
589          !!!parse-error (type => 'chardecode:fallback',
590                          level => $self->{level}->{uncertain},
591                          #text => $self->{input_encoding},
592                          line => 1, column => 1,
593                          layer => 'encode');
594        } elsif (not ($e_status &
595                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
596          $self->{input_encoding} = $charset->get_iana_name;
597          !!!parse-error (type => 'chardecode:no error',
598                          text => $self->{input_encoding},
599                          level => $self->{level}->{uncertain},
600                          line => 1, column => 1,
601                          layer => 'encode');
602        } else {
603          $self->{input_encoding} = $charset->get_iana_name;
604        }
605      $self->{confident} = 1;      $self->{confident} = 1;
606      $return = $self->parse_char_string ($s, @args);  
607        $wrapped_char_stream = $get_wrapper->($char_stream);
608        $wrapped_char_stream->onerror ($char_onerror);
609    
610        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
611    };    };
612    return $return;    return $return;
613  } # parse_byte_string  } # parse_byte_stream
614    
615  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
616  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 162  sub parse_byte_string ($$$$;$) { Line 621  sub parse_byte_string ($$$$;$) {
621  ## such as |parse_byte_string| in this module, must ensure that it does  ## such as |parse_byte_string| in this module, must ensure that it does
622  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
623    
624  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$$) {
625      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
626      my $self = shift;
627      my $s = ref $_[0] ? $_[0] : \($_[0]);
628      require Whatpm::Charset::DecodeHandle;
629      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
630      return $self->parse_char_stream ($input, @_[1..$#_]);
631    } # parse_char_string
632    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
633    
634  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$$) {
635    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
636    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
637    $self->{document} = $_[1];    $self->{document} = $_[1];
638    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
639    
# Line 175  sub parse_string ($$$;$) { Line 642  sub parse_string ($$$;$) {
642    $self->{confident} = 1 unless exists $self->{confident};    $self->{confident} = 1 unless exists $self->{confident};
643    $self->{document}->input_encoding ($self->{input_encoding})    $self->{document}->input_encoding ($self->{input_encoding})
644        if defined $self->{input_encoding};        if defined $self->{input_encoding};
645    ## TODO: |{input_encoding}| is needless?
646    
647    my $i = 0;    $self->{line_prev} = $self->{line} = 1;
648    my $line = 1;    $self->{column_prev} = -1;
649    my $column = 0;    $self->{column} = 0;
650    $self->{set_next_char} = sub {    $self->{set_nc} = sub {
651      my $self = shift;      my $self = shift;
652    
653      pop @{$self->{prev_char}};      my $char = '';
654      unshift @{$self->{prev_char}}, $self->{next_char};      if (defined $self->{next_nc}) {
655          $char = $self->{next_nc};
656          delete $self->{next_nc};
657          $self->{nc} = ord $char;
658        } else {
659          $self->{char_buffer} = '';
660          $self->{char_buffer_pos} = 0;
661    
662          my $count = $input->manakai_read_until
663             ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/, $self->{char_buffer_pos});
664          if ($count) {
665            $self->{line_prev} = $self->{line};
666            $self->{column_prev} = $self->{column};
667            $self->{column}++;
668            $self->{nc}
669                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
670            return;
671          }
672    
673          if ($input->read ($char, 1)) {
674            $self->{nc} = ord $char;
675          } else {
676            $self->{nc} = -1;
677            return;
678          }
679        }
680    
681      $self->{next_char} = -1 and return if $i >= length $$s;      ($self->{line_prev}, $self->{column_prev})
682      $self->{next_char} = ord substr $$s, $i++, 1;          = ($self->{line}, $self->{column});
683      $column++;      $self->{column}++;
684            
685      if ($self->{next_char} == 0x000A) { # LF      if ($self->{nc} == 0x000A) { # LF
686        $line++;        !!!cp ('j1');
687        $column = 0;        $self->{line}++;
688      } elsif ($self->{next_char} == 0x000D) { # CR        $self->{column} = 0;
689        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{nc} == 0x000D) { # CR
690        $self->{next_char} = 0x000A; # LF # MUST        !!!cp ('j2');
691        $line++;  ## TODO: support for abort/streaming
692        $column = 0;        my $next = '';
693      } elsif ($self->{next_char} > 0x10FFFF) {        if ($input->read ($next, 1) and $next ne "\x0A") {
694        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_nc} = $next;
695      } elsif ($self->{next_char} == 0x0000) { # NULL        }
696          $self->{nc} = 0x000A; # LF # MUST
697          $self->{line}++;
698          $self->{column} = 0;
699        } elsif ($self->{nc} == 0x0000) { # NULL
700          !!!cp ('j4');
701        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
702        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
703      }      }
704    };    };
705    $self->{prev_char} = [-1, -1, -1];  
706    $self->{next_char} = -1;    $self->{read_until} = sub {
707        #my ($scalar, $specials_range, $offset) = @_;
708        return 0 if defined $self->{next_nc};
709    
710        my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
711        my $offset = $_[2] || 0;
712    
713        if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
714          pos ($self->{char_buffer}) = $self->{char_buffer_pos};
715          if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
716            substr ($_[0], $offset)
717                = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
718            my $count = $+[0] - $-[0];
719            if ($count) {
720              $self->{column} += $count;
721              $self->{char_buffer_pos} += $count;
722              $self->{line_prev} = $self->{line};
723              $self->{column_prev} = $self->{column} - 1;
724              $self->{nc} = -1;
725            }
726            return $count;
727          } else {
728            return 0;
729          }
730        } else {
731          my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
732          if ($count) {
733            $self->{column} += $count;
734            $self->{line_prev} = $self->{line};
735            $self->{column_prev} = $self->{column} - 1;
736            $self->{nc} = -1;
737          }
738          return $count;
739        }
740      }; # $self->{read_until}
741    
742    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
743      my (%opt) = @_;      my (%opt) = @_;
744      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
745        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
746        warn "Parse error ($opt{type}) at line $line column $column\n";
747    };    };
748    $self->{parse_error} = sub {    $self->{parse_error} = sub {
749      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
750    };    };
751    
752      my $char_onerror = sub {
753        my (undef, $type, %opt) = @_;
754        !!!parse-error (layer => 'encode',
755                        line => $self->{line}, column => $self->{column} + 1,
756                        %opt, type => $type);
757      }; # $char_onerror
758    
759      if ($_[3]) {
760        $input = $_[3]->($input);
761        $input->onerror ($char_onerror);
762      } else {
763        $input->onerror ($char_onerror) unless defined $input->onerror;
764      }
765    
766    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
767    $self->_initialize_tree_constructor;    $self->_initialize_tree_constructor;
768    $self->_construct_tree;    $self->_construct_tree;
769    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
770    
771      delete $self->{parse_error}; # remove loop
772    
773    return $self->{document};    return $self->{document};
774  } # parse_string  } # parse_char_stream
775    
776  sub new ($) {  sub new ($) {
777    my $class = shift;    my $class = shift;
778    my $self = bless {}, $class;    my $self = bless {
779    $self->{set_next_char} = sub {      level => {must => 'm',
780      $self->{next_char} = -1;                should => 's',
781                  warn => 'w',
782                  info => 'i',
783                  uncertain => 'u'},
784      }, $class;
785      $self->{set_nc} = sub {
786        $self->{nc} = -1;
787    };    };
788    $self->{parse_error} = sub {    $self->{parse_error} = sub {
789      #      #
# Line 244  sub new ($) { Line 800  sub new ($) {
800    return $self;    return $self;
801  } # new  } # new
802    
803  sub CM_ENTITY () { 0b001 } # & markup in data  ## Insertion modes
 sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)  
 sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)  
   
 sub PLAINTEXT_CONTENT_MODEL () { 0 }  
 sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }  
 sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  
 sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  
   
 sub DATA_STATE () { 0 }  
 sub ENTITY_DATA_STATE () { 1 }  
 sub TAG_OPEN_STATE () { 2 }  
 sub CLOSE_TAG_OPEN_STATE () { 3 }  
 sub TAG_NAME_STATE () { 4 }  
 sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }  
 sub ATTRIBUTE_NAME_STATE () { 6 }  
 sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }  
 sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }  
 sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  
 sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  
 sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  
 sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  
 sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  
 sub COMMENT_START_STATE () { 14 }  
 sub COMMENT_START_DASH_STATE () { 15 }  
 sub COMMENT_STATE () { 16 }  
 sub COMMENT_END_STATE () { 17 }  
 sub COMMENT_END_DASH_STATE () { 18 }  
 sub BOGUS_COMMENT_STATE () { 19 }  
 sub DOCTYPE_STATE () { 20 }  
 sub BEFORE_DOCTYPE_NAME_STATE () { 21 }  
 sub DOCTYPE_NAME_STATE () { 22 }  
 sub AFTER_DOCTYPE_NAME_STATE () { 23 }  
 sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }  
 sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }  
 sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }  
 sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }  
 sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }  
 sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }  
 sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }  
 sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  
 sub BOGUS_DOCTYPE_STATE () { 32 }  
 sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  
   
 sub DOCTYPE_TOKEN () { 1 }  
 sub COMMENT_TOKEN () { 2 }  
 sub START_TAG_TOKEN () { 3 }  
 sub END_TAG_TOKEN () { 4 }  
 sub END_OF_FILE_TOKEN () { 5 }  
 sub CHARACTER_TOKEN () { 6 }  
804    
805  sub AFTER_HTML_IMS () { 0b100 }  sub AFTER_HTML_IMS () { 0b100 }
806  sub HEAD_IMS ()       { 0b1000 }  sub HEAD_IMS ()       { 0b1000 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 810  sub TABLE_IMS ()      { 0b1000000 }
810  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
811  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
812  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
813    sub SELECT_IMS ()     { 0b10000000000 }
814    #sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 } # see Whatpm::HTML::Tokenizer
815        ## NOTE: "in foreign content" insertion mode is special; it is combined
816        ## with the secondary insertion mode.  In this parser, they are stored
817        ## together in the bit-or'ed form.
818    sub IN_CDATA_RCDATA_IM () { 0b1000000000000 }
819        ## NOTE: "in CDATA/RCDATA" insertion mode is also special; it is
820        ## combined with the original insertion mode.  In thie parser,
821        ## they are stored together in the bit-or'ed form.
822    
823    sub IM_MASK () { 0b11111111111 }
824    
825  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
826    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 843  sub IN_TABLE_IM () { TABLE_IMS }
843  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
844  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
845  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
846  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
847    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
848  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
849    
 ## Implementations MUST act as if state machine in the spec  
   
 sub _initialize_tokenizer ($) {  
   my $self = shift;  
   $self->{state} = DATA_STATE; # MUST  
   $self->{content_model} = PCDATA_CONTENT_MODEL; # be  
   undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE  
   undef $self->{current_attribute};  
   undef $self->{last_emitted_start_tag_name};  
   undef $self->{last_attribute_value_state};  
   $self->{char} = [];  
   # $self->{next_char}  
   !!!next-input-character;  
   $self->{token} = [];  
   # $self->{escape}  
 } # _initialize_tokenizer  
   
 ## A token has:  
 ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,  
 ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN  
 ##   ->{name} (DOCTYPE_TOKEN)  
 ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  
 ##   ->{public_identifier} (DOCTYPE_TOKEN)  
 ##   ->{system_identifier} (DOCTYPE_TOKEN)  
 ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag  
 ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  
 ##        ->{name}  
 ##        ->{value}  
 ##        ->{has_reference} == 1 or 0  
 ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  
   
 ## Emitted token MUST immediately be handled by the tree construction state.  
   
 ## Before each step, UA MAY check to see if either one of the scripts in  
 ## "list of scripts that will execute as soon as possible" or the first  
 ## script in the "list of scripts that will execute asynchronously",  
 ## has completed loading.  If one has, then it MUST be executed  
 ## and removed from the list.  
   
 ## NOTE: HTML5 "Writing HTML documents" section, applied to  
 ## documents and not to user agents and conformance checkers,  
 ## contains some requirements that are not detected by the  
 ## parsing algorithm:  
 ## - Some requirements on character encoding declarations. ## TODO  
 ## - "Elements MUST NOT contain content that their content model disallows."  
 ##   ... Some are parse error, some are not (will be reported by c.c.).  
 ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO  
 ## - Text (in elements, attributes, and comments) SHOULD NOT contain  
 ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)  
   
 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot  
 ## be detected by the HTML5 parsing algorithm:  
 ## - Text,  
   
 sub _get_next_token ($) {  
   my $self = shift;  
   if (@{$self->{token}}) {  
     return shift @{$self->{token}};  
   }  
   
   A: {  
     if ($self->{state} == DATA_STATE) {  
       if ($self->{next_char} == 0x0026) { # &  
         if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA  
             not $self->{escape}) {  
           !!!cp (1);  
           $self->{state} = ENTITY_DATA_STATE;  
           !!!next-input-character;  
           redo A;  
         } else {  
           !!!cp (2);  
           #  
         }  
       } elsif ($self->{next_char} == 0x002D) { # -  
         if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA  
           unless ($self->{escape}) {  
             if ($self->{prev_char}->[0] == 0x002D and # -  
                 $self->{prev_char}->[1] == 0x0021 and # !  
                 $self->{prev_char}->[2] == 0x003C) { # <  
               !!!cp (3);  
               $self->{escape} = 1;  
             } else {  
               !!!cp (4);  
             }  
           } else {  
             !!!cp (5);  
           }  
         }  
           
         #  
       } elsif ($self->{next_char} == 0x003C) { # <  
         if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA  
             (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA  
              not $self->{escape})) {  
           !!!cp (6);  
           $self->{state} = TAG_OPEN_STATE;  
           !!!next-input-character;  
           redo A;  
         } else {  
           !!!cp (7);  
           #  
         }  
       } elsif ($self->{next_char} == 0x003E) { # >  
         if ($self->{escape} and  
             ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA  
           if ($self->{prev_char}->[0] == 0x002D and # -  
               $self->{prev_char}->[1] == 0x002D) { # -  
             !!!cp (8);  
             delete $self->{escape};  
           } else {  
             !!!cp (9);  
           }  
         } else {  
           !!!cp (10);  
         }  
           
         #  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (11);  
         !!!emit ({type => END_OF_FILE_TOKEN});  
         last A; ## TODO: ok?  
       } else {  
         !!!cp (12);  
       }  
       # Anything else  
       my $token = {type => CHARACTER_TOKEN,  
                    data => chr $self->{next_char}};  
       ## Stay in the data state  
       !!!next-input-character;  
   
       !!!emit ($token);  
   
       redo A;  
     } elsif ($self->{state} == ENTITY_DATA_STATE) {  
       ## (cannot happen in CDATA state)  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);  
   
       $self->{state} = DATA_STATE;  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!cp (13);  
         !!!emit ({type => CHARACTER_TOKEN, data => '&'});  
       } else {  
         !!!cp (14);  
         !!!emit ($token);  
       }  
   
       redo A;  
     } elsif ($self->{state} == TAG_OPEN_STATE) {  
       if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA  
         if ($self->{next_char} == 0x002F) { # /  
           !!!cp (15);  
           !!!next-input-character;  
           $self->{state} = CLOSE_TAG_OPEN_STATE;  
           redo A;  
         } else {  
           !!!cp (16);  
           ## reconsume  
           $self->{state} = DATA_STATE;  
   
           !!!emit ({type => CHARACTER_TOKEN, data => '<'});  
   
           redo A;  
         }  
       } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA  
         if ($self->{next_char} == 0x0021) { # !  
           !!!cp (17);  
           $self->{state} = MARKUP_DECLARATION_OPEN_STATE;  
           !!!next-input-character;  
           redo A;  
         } elsif ($self->{next_char} == 0x002F) { # /  
           !!!cp (18);  
           $self->{state} = CLOSE_TAG_OPEN_STATE;  
           !!!next-input-character;  
           redo A;  
         } elsif (0x0041 <= $self->{next_char} and  
                  $self->{next_char} <= 0x005A) { # A..Z  
           !!!cp (19);  
           $self->{current_token}  
             = {type => START_TAG_TOKEN,  
                tag_name => chr ($self->{next_char} + 0x0020)};  
           $self->{state} = TAG_NAME_STATE;  
           !!!next-input-character;  
           redo A;  
         } elsif (0x0061 <= $self->{next_char} and  
                  $self->{next_char} <= 0x007A) { # a..z  
           !!!cp (20);  
           $self->{current_token} = {type => START_TAG_TOKEN,  
                             tag_name => chr ($self->{next_char})};  
           $self->{state} = TAG_NAME_STATE;  
           !!!next-input-character;  
           redo A;  
         } elsif ($self->{next_char} == 0x003E) { # >  
           !!!cp (21);  
           !!!parse-error (type => 'empty start tag');  
           $self->{state} = DATA_STATE;  
           !!!next-input-character;  
   
           !!!emit ({type => CHARACTER_TOKEN, data => '<>'});  
   
           redo A;  
         } elsif ($self->{next_char} == 0x003F) { # ?  
           !!!cp (22);  
           !!!parse-error (type => 'pio');  
           $self->{state} = BOGUS_COMMENT_STATE;  
           ## $self->{next_char} is intentionally left as is  
           redo A;  
         } else {  
           !!!cp (23);  
           !!!parse-error (type => 'bare stago');  
           $self->{state} = DATA_STATE;  
           ## reconsume  
   
           !!!emit ({type => CHARACTER_TOKEN, data => '<'});  
   
           redo A;  
         }  
       } else {  
         die "$0: $self->{content_model} in tag open";  
       }  
     } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {  
       if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA  
         if (defined $self->{last_emitted_start_tag_name}) {  
           ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>  
           my @next_char;  
           TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
             push @next_char, $self->{next_char};  
             my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
             my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
             if ($self->{next_char} == $c or $self->{next_char} == $C) {  
               !!!cp (24);  
               !!!next-input-character;  
               next TAGNAME;  
             } else {  
               !!!cp (25);  
               $self->{next_char} = shift @next_char; # reconsume  
               !!!back-next-input-character (@next_char);  
               $self->{state} = DATA_STATE;  
   
               !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
     
               redo A;  
             }  
           }  
           push @next_char, $self->{next_char};  
         
           unless ($self->{next_char} == 0x0009 or # HT  
                   $self->{next_char} == 0x000A or # LF  
                   $self->{next_char} == 0x000B or # VT  
                   $self->{next_char} == 0x000C or # FF  
                   $self->{next_char} == 0x0020 or # SP  
                   $self->{next_char} == 0x003E or # >  
                   $self->{next_char} == 0x002F or # /  
                   $self->{next_char} == -1) {  
             !!!cp (26);  
             $self->{next_char} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = DATA_STATE;  
             !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
             redo A;  
           } else {  
             !!!cp (27);  
             $self->{next_char} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
         } else {  
           ## No start tag token has ever been emitted  
           !!!cp (28);  
           # next-input-character is already done  
           $self->{state} = DATA_STATE;  
           !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
           redo A;  
         }  
       }  
         
       if (0x0041 <= $self->{next_char} and  
           $self->{next_char} <= 0x005A) { # A..Z  
         !!!cp (29);  
         $self->{current_token} = {type => END_TAG_TOKEN,  
                           tag_name => chr ($self->{next_char} + 0x0020)};  
         $self->{state} = TAG_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif (0x0061 <= $self->{next_char} and  
                $self->{next_char} <= 0x007A) { # a..z  
         !!!cp (30);  
         $self->{current_token} = {type => END_TAG_TOKEN,  
                           tag_name => chr ($self->{next_char})};  
         $self->{state} = TAG_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         !!!cp (31);  
         !!!parse-error (type => 'empty end tag');  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (32);  
         !!!parse-error (type => 'bare etago');  
         $self->{state} = DATA_STATE;  
         # reconsume  
   
         !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
   
         redo A;  
       } else {  
         !!!cp (33);  
         !!!parse-error (type => 'bogus end tag');  
         $self->{state} = BOGUS_COMMENT_STATE;  
         ## $self->{next_char} is intentionally left as is  
         redo A;  
       }  
     } elsif ($self->{state} == TAG_NAME_STATE) {  
       if ($self->{next_char} == 0x0009 or # HT  
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
         !!!cp (34);  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           !!!cp (35);  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           #if ($self->{current_token}->{attributes}) {  
           #  ## NOTE: This should never be reached.  
           #  !!! cp (36);  
           #  !!! parse-error (type => 'end tag attribute');  
           #} else {  
             !!!cp (37);  
           #}  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } elsif (0x0041 <= $self->{next_char} and  
                $self->{next_char} <= 0x005A) { # A..Z  
         !!!cp (38);  
         $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);  
           # start tag or end tag  
         ## Stay in this state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           !!!cp (39);  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           #if ($self->{current_token}->{attributes}) {  
           #  ## NOTE: This state should never be reached.  
           #  !!! cp (40);  
           #  !!! parse-error (type => 'end tag attribute');  
           #} else {  
             !!!cp (41);  
           #}  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         # reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } elsif ($self->{next_char} == 0x002F) { # /  
         !!!next-input-character;  
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (42);  
           #  
         } else {  
           !!!cp (43);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
         redo A;  
       } else {  
         !!!cp (44);  
         $self->{current_token}->{tag_name} .= chr $self->{next_char};  
           # start tag or end tag  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {  
       if ($self->{next_char} == 0x0009 or # HT  
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
         !!!cp (45);  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           !!!cp (46);  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!cp (47);  
             !!!parse-error (type => 'end tag attribute');  
           } else {  
             !!!cp (48);  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } elsif (0x0041 <= $self->{next_char} and  
                $self->{next_char} <= 0x005A) { # A..Z  
         !!!cp (49);  
         $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),  
                               value => ''};  
         $self->{state} = ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x002F) { # /  
         !!!next-input-character;  
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (50);  
           #  
         } else {  
           !!!cp (51);  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           !!!cp (52);  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!cp (53);  
             !!!parse-error (type => 'end tag attribute');  
           } else {  
             !!!cp (54);  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         # reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } else {  
         if ({  
              0x0022 => 1, # "  
              0x0027 => 1, # '  
              0x003D => 1, # =  
             }->{$self->{next_char}}) {  
           !!!cp (55);  
           !!!parse-error (type => 'bad attribute name');  
         } else {  
           !!!cp (56);  
         }  
         $self->{current_attribute} = {name => chr ($self->{next_char}),  
                               value => ''};  
         $self->{state} = ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {  
       my $before_leave = sub {  
         if (exists $self->{current_token}->{attributes} # start tag or end tag  
             ->{$self->{current_attribute}->{name}}) { # MUST  
           !!!cp (57);  
           !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});  
           ## Discard $self->{current_attribute} # MUST  
         } else {  
           !!!cp (58);  
           $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}  
             = $self->{current_attribute};  
         }  
       }; # $before_leave  
   
       if ($self->{next_char} == 0x0009 or # HT  
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
         !!!cp (59);  
         $before_leave->();  
         $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003D) { # =  
         !!!cp (60);  
         $before_leave->();  
         $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         $before_leave->();  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           !!!cp (61);  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           !!!cp (62);  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } elsif (0x0041 <= $self->{next_char} and  
                $self->{next_char} <= 0x005A) { # A..Z  
         !!!cp (63);  
         $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x002F) { # /  
         $before_leave->();  
         !!!next-input-character;  
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (64);  
           #  
         } else {  
           !!!cp (65);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         $before_leave->();  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           !!!cp (66);  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!cp (67);  
             !!!parse-error (type => 'end tag attribute');  
           } else {  
             ## NOTE: This state should never be reached.  
             !!!cp (68);  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         # reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } else {  
         if ($self->{next_char} == 0x0022 or # "  
             $self->{next_char} == 0x0027) { # '  
           !!!cp (69);  
           !!!parse-error (type => 'bad attribute name');  
         } else {  
           !!!cp (70);  
         }  
         $self->{current_attribute}->{name} .= chr ($self->{next_char});  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {  
       if ($self->{next_char} == 0x0009 or # HT  
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
         !!!cp (71);  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003D) { # =  
         !!!cp (72);  
         $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           !!!cp (73);  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!cp (74);  
             !!!parse-error (type => 'end tag attribute');  
           } else {  
             ## NOTE: This state should never be reached.  
             !!!cp (75);  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } elsif (0x0041 <= $self->{next_char} and  
                $self->{next_char} <= 0x005A) { # A..Z  
         !!!cp (76);  
         $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),  
                               value => ''};  
         $self->{state} = ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x002F) { # /  
         !!!next-input-character;  
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (77);  
           #  
         } else {  
           !!!cp (78);  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           !!!cp (79);  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!cp (80);  
             !!!parse-error (type => 'end tag attribute');  
           } else {  
             ## NOTE: This state should never be reached.  
             !!!cp (81);  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         # reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } else {  
         !!!cp (82);  
         $self->{current_attribute} = {name => chr ($self->{next_char}),  
                               value => ''};  
         $self->{state} = ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;          
       }  
     } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {  
       if ($self->{next_char} == 0x0009 or # HT  
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP        
         !!!cp (83);  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x0022) { # "  
         !!!cp (84);  
         $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x0026) { # &  
         !!!cp (85);  
         $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;  
         ## reconsume  
         redo A;  
       } elsif ($self->{next_char} == 0x0027) { # '  
         !!!cp (86);  
         $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           !!!cp (87);  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!cp (88);  
             !!!parse-error (type => 'end tag attribute');  
           } else {  
             ## NOTE: This state should never be reached.  
             !!!cp (89);  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           !!!cp (90);  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!cp (91);  
             !!!parse-error (type => 'end tag attribute');  
           } else {  
             ## NOTE: This state should never be reached.  
             !!!cp (92);  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } else {  
         if ($self->{next_char} == 0x003D) { # =  
           !!!cp (93);  
           !!!parse-error (type => 'bad attribute value');  
         } else {  
           !!!cp (94);  
         }  
         $self->{current_attribute}->{value} .= chr ($self->{next_char});  
         $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {  
       if ($self->{next_char} == 0x0022) { # "  
         !!!cp (95);  
         $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x0026) { # &  
         !!!cp (96);  
         $self->{last_attribute_value_state} = $self->{state};  
         $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!parse-error (type => 'unclosed attribute value');  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           !!!cp (97);  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!cp (98);  
             !!!parse-error (type => 'end tag attribute');  
           } else {  
             ## NOTE: This state should never be reached.  
             !!!cp (99);  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } else {  
         !!!cp (100);  
         $self->{current_attribute}->{value} .= chr ($self->{next_char});  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {  
       if ($self->{next_char} == 0x0027) { # '  
         !!!cp (101);  
         $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x0026) { # &  
         !!!cp (102);  
         $self->{last_attribute_value_state} = $self->{state};  
         $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!parse-error (type => 'unclosed attribute value');  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           !!!cp (103);  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!cp (104);  
             !!!parse-error (type => 'end tag attribute');  
           } else {  
             ## NOTE: This state should never be reached.  
             !!!cp (105);  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } else {  
         !!!cp (106);  
         $self->{current_attribute}->{value} .= chr ($self->{next_char});  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {  
       if ($self->{next_char} == 0x0009 or # HT  
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # HT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
         !!!cp (107);  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x0026) { # &  
         !!!cp (108);  
         $self->{last_attribute_value_state} = $self->{state};  
         $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           !!!cp (109);  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!cp (110);  
             !!!parse-error (type => 'end tag attribute');  
           } else {  
             ## NOTE: This state should never be reached.  
             !!!cp (111);  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           !!!cp (112);  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!cp (113);  
             !!!parse-error (type => 'end tag attribute');  
           } else {  
             ## NOTE: This state should never be reached.  
             !!!cp (114);  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } else {  
         if ({  
              0x0022 => 1, # "  
              0x0027 => 1, # '  
              0x003D => 1, # =  
             }->{$self->{next_char}}) {  
           !!!cp (115);  
           !!!parse-error (type => 'bad attribute value');  
         } else {  
           !!!cp (116);  
         }  
         $self->{current_attribute}->{value} .= chr ($self->{next_char});  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {  
       my $token = $self->_tokenize_attempt_to_consume_an_entity  
           (1,  
            $self->{last_attribute_value_state}  
              == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "  
            $self->{last_attribute_value_state}  
              == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '  
            -1);  
   
       unless (defined $token) {  
         !!!cp (117);  
         $self->{current_attribute}->{value} .= '&';  
       } else {  
         !!!cp (118);  
         $self->{current_attribute}->{value} .= $token->{data};  
         $self->{current_attribute}->{has_reference} = $token->{has_reference};  
         ## ISSUE: spec says "append the returned character token to the current attribute's value"  
       }  
   
       $self->{state} = $self->{last_attribute_value_state};  
       # next-input-character is already done  
       redo A;  
     } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {  
       if ($self->{next_char} == 0x0009 or # HT  
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
         !!!cp (118);  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           !!!cp (119);  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!cp (120);  
             !!!parse-error (type => 'end tag attribute');  
           } else {  
             ## NOTE: This state should never be reached.  
             !!!cp (121);  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } elsif ($self->{next_char} == 0x002F) { # /  
         !!!next-input-character;  
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (122);  
           #  
         } else {  
           !!!cp (123);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
         redo A;  
       } else {  
         !!!cp (124);  
         !!!parse-error (type => 'no space between attributes');  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         ## reconsume  
         redo A;  
       }  
     } elsif ($self->{state} == BOGUS_COMMENT_STATE) {  
       ## (only happen if PCDATA state)  
         
       my $token = {type => COMMENT_TOKEN, data => ''};  
   
       BC: {  
         if ($self->{next_char} == 0x003E) { # >  
           !!!cp (124);  
           $self->{state} = DATA_STATE;  
           !!!next-input-character;  
   
           !!!emit ($token);  
   
           redo A;  
         } elsif ($self->{next_char} == -1) {  
           !!!cp (125);  
           $self->{state} = DATA_STATE;  
           ## reconsume  
   
           !!!emit ($token);  
   
           redo A;  
         } else {  
           !!!cp (126);  
           $token->{data} .= chr ($self->{next_char});  
           !!!next-input-character;  
           redo BC;  
         }  
       } # BC  
   
       die "$0: _get_next_token: unexpected case [BC]";  
     } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {  
       ## (only happen if PCDATA state)  
   
       my @next_char;  
       push @next_char, $self->{next_char};  
         
       if ($self->{next_char} == 0x002D) { # -  
         !!!next-input-character;  
         push @next_char, $self->{next_char};  
         if ($self->{next_char} == 0x002D) { # -  
           !!!cp (127);  
           $self->{current_token} = {type => COMMENT_TOKEN, data => ''};  
           $self->{state} = COMMENT_START_STATE;  
           !!!next-input-character;  
           redo A;  
         } else {  
           !!!cp (128);  
         }  
       } elsif ($self->{next_char} == 0x0044 or # D  
                $self->{next_char} == 0x0064) { # d  
         !!!next-input-character;  
         push @next_char, $self->{next_char};  
         if ($self->{next_char} == 0x004F or # O  
             $self->{next_char} == 0x006F) { # o  
           !!!next-input-character;  
           push @next_char, $self->{next_char};  
           if ($self->{next_char} == 0x0043 or # C  
               $self->{next_char} == 0x0063) { # c  
             !!!next-input-character;  
             push @next_char, $self->{next_char};  
             if ($self->{next_char} == 0x0054 or # T  
                 $self->{next_char} == 0x0074) { # t  
               !!!next-input-character;  
               push @next_char, $self->{next_char};  
               if ($self->{next_char} == 0x0059 or # Y  
                   $self->{next_char} == 0x0079) { # y  
                 !!!next-input-character;  
                 push @next_char, $self->{next_char};  
                 if ($self->{next_char} == 0x0050 or # P  
                     $self->{next_char} == 0x0070) { # p  
                   !!!next-input-character;  
                   push @next_char, $self->{next_char};  
                   if ($self->{next_char} == 0x0045 or # E  
                       $self->{next_char} == 0x0065) { # e  
                     !!!cp (129);  
                     ## TODO: What a stupid code this is!  
                     $self->{state} = DOCTYPE_STATE;  
                     !!!next-input-character;  
                     redo A;  
                   } else {  
                     !!!cp (130);  
                   }  
                 } else {  
                   !!!cp (131);  
                 }  
               } else {  
                 !!!cp (132);  
               }  
             } else {  
               !!!cp (133);  
             }  
           } else {  
             !!!cp (134);  
           }  
         } else {  
           !!!cp (135);  
         }  
       } else {  
         !!!cp (136);  
       }  
   
       !!!parse-error (type => 'bogus comment');  
       $self->{next_char} = shift @next_char;  
       !!!back-next-input-character (@next_char);  
       $self->{state} = BOGUS_COMMENT_STATE;  
       redo A;  
         
       ## ISSUE: typos in spec: chacacters, is is a parse error  
       ## 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?  
     } elsif ($self->{state} == COMMENT_START_STATE) {  
       if ($self->{next_char} == 0x002D) { # -  
         !!!cp (137);  
         $self->{state} = COMMENT_START_DASH_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         !!!cp (138);  
         !!!parse-error (type => 'bogus comment');  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (139);  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } else {  
         !!!cp (140);  
         $self->{current_token}->{data} # comment  
             .= chr ($self->{next_char});  
         $self->{state} = COMMENT_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == COMMENT_START_DASH_STATE) {  
       if ($self->{next_char} == 0x002D) { # -  
         !!!cp (141);  
         $self->{state} = COMMENT_END_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         !!!cp (142);  
         !!!parse-error (type => 'bogus comment');  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (143);  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } else {  
         !!!cp (144);  
         $self->{current_token}->{data} # comment  
             .= '-' . chr ($self->{next_char});  
         $self->{state} = COMMENT_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == COMMENT_STATE) {  
       if ($self->{next_char} == 0x002D) { # -  
         !!!cp (145);  
         $self->{state} = COMMENT_END_DASH_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (146);  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } else {  
         !!!cp (147);  
         $self->{current_token}->{data} .= chr ($self->{next_char}); # comment  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == COMMENT_END_DASH_STATE) {  
       if ($self->{next_char} == 0x002D) { # -  
         !!!cp (148);  
         $self->{state} = COMMENT_END_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (149);  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } else {  
         !!!cp (150);  
         $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment  
         $self->{state} = COMMENT_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == COMMENT_END_STATE) {  
       if ($self->{next_char} == 0x003E) { # >  
         !!!cp (151);  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } elsif ($self->{next_char} == 0x002D) { # -  
         !!!cp (152);  
         !!!parse-error (type => 'dash in comment');  
         $self->{current_token}->{data} .= '-'; # comment  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (153);  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } else {  
         !!!cp (154);  
         !!!parse-error (type => 'dash in comment');  
         $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment  
         $self->{state} = COMMENT_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == DOCTYPE_STATE) {  
       if ($self->{next_char} == 0x0009 or # HT  
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
         !!!cp (155);  
         $self->{state} = BEFORE_DOCTYPE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } else {  
         !!!cp (156);  
         !!!parse-error (type => 'no space before DOCTYPE name');  
         $self->{state} = BEFORE_DOCTYPE_NAME_STATE;  
         ## reconsume  
         redo A;  
       }  
     } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {  
       if ($self->{next_char} == 0x0009 or # HT  
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
         !!!cp (157);  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         !!!cp (158);  
         !!!parse-error (type => 'no DOCTYPE name');  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (159);  
         !!!parse-error (type => 'no DOCTYPE name');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});  
   
         redo A;  
       } else {  
         !!!cp (160);  
         $self->{current_token}  
             = {type => DOCTYPE_TOKEN,  
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
 ## ISSUE: "Set the token's name name to the" in the spec  
         $self->{state} = DOCTYPE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == DOCTYPE_NAME_STATE) {  
 ## ISSUE: Redundant "First," in the spec.  
       if ($self->{next_char} == 0x0009 or # HT  
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
         !!!cp (161);  
         $self->{state} = AFTER_DOCTYPE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         !!!cp (162);  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (163);  
         !!!parse-error (type => 'unclosed DOCTYPE');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!cp (164);  
         $self->{current_token}->{name}  
           .= chr ($self->{next_char}); # DOCTYPE  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {  
       if ($self->{next_char} == 0x0009 or # HT  
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
         !!!cp (165);  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         !!!cp (166);  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (167);  
         !!!parse-error (type => 'unclosed DOCTYPE');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_char} == 0x0050 or # P  
                $self->{next_char} == 0x0070) { # p  
         !!!next-input-character;  
         if ($self->{next_char} == 0x0055 or # U  
             $self->{next_char} == 0x0075) { # u  
           !!!next-input-character;  
           if ($self->{next_char} == 0x0042 or # B  
               $self->{next_char} == 0x0062) { # b  
             !!!next-input-character;  
             if ($self->{next_char} == 0x004C or # L  
                 $self->{next_char} == 0x006C) { # l  
               !!!next-input-character;  
               if ($self->{next_char} == 0x0049 or # I  
                   $self->{next_char} == 0x0069) { # i  
                 !!!next-input-character;  
                 if ($self->{next_char} == 0x0043 or # C  
                     $self->{next_char} == 0x0063) { # c  
                   !!!cp (168);  
                   $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 } else {  
                   !!!cp (169);  
                 }  
               } else {  
                 !!!cp (170);  
               }  
             } else {  
               !!!cp (171);  
             }  
           } else {  
             !!!cp (172);  
           }  
         } else {  
           !!!cp (173);  
         }  
   
         #  
       } elsif ($self->{next_char} == 0x0053 or # S  
                $self->{next_char} == 0x0073) { # s  
         !!!next-input-character;  
         if ($self->{next_char} == 0x0059 or # Y  
             $self->{next_char} == 0x0079) { # y  
           !!!next-input-character;  
           if ($self->{next_char} == 0x0053 or # S  
               $self->{next_char} == 0x0073) { # s  
             !!!next-input-character;  
             if ($self->{next_char} == 0x0054 or # T  
                 $self->{next_char} == 0x0074) { # t  
               !!!next-input-character;  
               if ($self->{next_char} == 0x0045 or # E  
                   $self->{next_char} == 0x0065) { # e  
                 !!!next-input-character;  
                 if ($self->{next_char} == 0x004D or # M  
                     $self->{next_char} == 0x006D) { # m  
                   !!!cp (174);  
                   $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 } else {  
                   !!!cp (175);  
                 }  
               } else {  
                 !!!cp (176);  
               }  
             } else {  
               !!!cp (177);  
             }  
           } else {  
             !!!cp (178);  
           }  
         } else {  
           !!!cp (179);  
         }  
   
         #  
       } else {  
         !!!cp (180);  
         !!!next-input-character;  
         #  
       }  
   
       !!!parse-error (type => 'string after DOCTYPE name');  
       $self->{current_token}->{quirks} = 1;  
   
       $self->{state} = BOGUS_DOCTYPE_STATE;  
       # next-input-character is already done  
       redo A;  
     } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {  
       if ({  
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
         !!!cp (181);  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} eq 0x0022) { # "  
         !!!cp (182);  
         $self->{current_token}->{public_identifier} = ''; # DOCTYPE  
         $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} eq 0x0027) { # '  
         !!!cp (183);  
         $self->{current_token}->{public_identifier} = ''; # DOCTYPE  
         $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} eq 0x003E) { # >  
         !!!cp (184);  
         !!!parse-error (type => 'no PUBLIC literal');  
   
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (185);  
         !!!parse-error (type => 'unclosed DOCTYPE');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!cp (186);  
         !!!parse-error (type => 'string after PUBLIC');  
         $self->{current_token}->{quirks} = 1;  
   
         $self->{state} = BOGUS_DOCTYPE_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {  
       if ($self->{next_char} == 0x0022) { # "  
         !!!cp (187);  
         $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         !!!cp (188);  
         !!!parse-error (type => 'unclosed PUBLIC literal');  
   
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (189);  
         !!!parse-error (type => 'unclosed PUBLIC literal');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!cp (190);  
         $self->{current_token}->{public_identifier} # DOCTYPE  
             .= chr $self->{next_char};  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {  
       if ($self->{next_char} == 0x0027) { # '  
         !!!cp (191);  
         $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         !!!cp (192);  
         !!!parse-error (type => 'unclosed PUBLIC literal');  
   
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (193);  
         !!!parse-error (type => 'unclosed PUBLIC literal');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!cp (194);  
         $self->{current_token}->{public_identifier} # DOCTYPE  
             .= chr $self->{next_char};  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {  
       if ({  
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
         !!!cp (195);  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x0022) { # "  
         !!!cp (196);  
         $self->{current_token}->{system_identifier} = ''; # DOCTYPE  
         $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x0027) { # '  
         !!!cp (197);  
         $self->{current_token}->{system_identifier} = ''; # DOCTYPE  
         $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         !!!cp (198);  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (199);  
         !!!parse-error (type => 'unclosed DOCTYPE');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!cp (200);  
         !!!parse-error (type => 'string after PUBLIC literal');  
         $self->{current_token}->{quirks} = 1;  
   
         $self->{state} = BOGUS_DOCTYPE_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {  
       if ({  
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
         !!!cp (201);  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x0022) { # "  
         !!!cp (202);  
         $self->{current_token}->{system_identifier} = ''; # DOCTYPE  
         $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x0027) { # '  
         !!!cp (203);  
         $self->{current_token}->{system_identifier} = ''; # DOCTYPE  
         $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         !!!cp (204);  
         !!!parse-error (type => 'no SYSTEM literal');  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (205);  
         !!!parse-error (type => 'unclosed DOCTYPE');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!cp (206);  
         !!!parse-error (type => 'string after SYSTEM');  
         $self->{current_token}->{quirks} = 1;  
   
         $self->{state} = BOGUS_DOCTYPE_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {  
       if ($self->{next_char} == 0x0022) { # "  
         !!!cp (207);  
         $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         !!!cp (208);  
         !!!parse-error (type => 'unclosed PUBLIC literal');  
   
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (209);  
         !!!parse-error (type => 'unclosed SYSTEM literal');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!cp (210);  
         $self->{current_token}->{system_identifier} # DOCTYPE  
             .= chr $self->{next_char};  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {  
       if ($self->{next_char} == 0x0027) { # '  
         !!!cp (211);  
         $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         !!!cp (212);  
         !!!parse-error (type => 'unclosed PUBLIC literal');  
   
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (213);  
         !!!parse-error (type => 'unclosed SYSTEM literal');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!cp (214);  
         $self->{current_token}->{system_identifier} # DOCTYPE  
             .= chr $self->{next_char};  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {  
       if ({  
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
         !!!cp (215);  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_char} == 0x003E) { # >  
         !!!cp (216);  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (217);  
         !!!parse-error (type => 'unclosed DOCTYPE');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         $self->{current_token}->{quirks} = 1;  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!cp (218);  
         !!!parse-error (type => 'string after SYSTEM literal');  
         #$self->{current_token}->{quirks} = 1;  
   
         $self->{state} = BOGUS_DOCTYPE_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {  
       if ($self->{next_char} == 0x003E) { # >  
         !!!cp (219);  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_char} == -1) {  
         !!!cp (220);  
         !!!parse-error (type => 'unclosed DOCTYPE');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!cp (221);  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } else {  
       die "$0: $self->{state}: Unknown state";  
     }  
   } # A    
   
   die "$0: _get_next_token: unexpected case";  
 } # _get_next_token  
   
 sub _tokenize_attempt_to_consume_an_entity ($$$) {  
   my ($self, $in_attr, $additional) = @_;  
   
   if ({  
        0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,  
        0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR  
        $additional => 1,  
       }->{$self->{next_char}}) {  
     !!!cp (1001);  
     ## Don't consume  
     ## No error  
     return undef;  
   } elsif ($self->{next_char} == 0x0023) { # #  
     !!!next-input-character;  
     if ($self->{next_char} == 0x0078 or # x  
         $self->{next_char} == 0x0058) { # X  
       my $code;  
       X: {  
         my $x_char = $self->{next_char};  
         !!!next-input-character;  
         if (0x0030 <= $self->{next_char} and  
             $self->{next_char} <= 0x0039) { # 0..9  
           !!!cp (1002);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0030;  
           redo X;  
         } elsif (0x0061 <= $self->{next_char} and  
                  $self->{next_char} <= 0x0066) { # a..f  
           !!!cp (1003);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_char} and  
                  $self->{next_char} <= 0x0046) { # A..F  
           !!!cp (1004);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $code) { # no hexadecimal digit  
           !!!cp (1005);  
           !!!parse-error (type => 'bare hcro');  
           !!!back-next-input-character ($x_char, $self->{next_char});  
           $self->{next_char} = 0x0023; # #  
           return undef;  
         } elsif ($self->{next_char} == 0x003B) { # ;  
           !!!cp (1006);  
           !!!next-input-character;  
         } else {  
           !!!cp (1007);  
           !!!parse-error (type => 'no refc');  
         }  
   
         if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {  
           !!!cp (1008);  
           !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);  
           $code = 0xFFFD;  
         } elsif ($code > 0x10FFFF) {  
           !!!cp (1009);  
           !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);  
           $code = 0xFFFD;  
         } elsif ($code == 0x000D) {  
           !!!cp (1010);  
           !!!parse-error (type => 'CR character reference');  
           $code = 0x000A;  
         } elsif (0x80 <= $code and $code <= 0x9F) {  
           !!!cp (1011);  
           !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);  
           $code = $c1_entity_char->{$code};  
         }  
   
         return {type => CHARACTER_TOKEN, data => chr $code,  
                 has_reference => 1};  
       } # X  
     } elsif (0x0030 <= $self->{next_char} and  
              $self->{next_char} <= 0x0039) { # 0..9  
       my $code = $self->{next_char} - 0x0030;  
       !!!next-input-character;  
         
       while (0x0030 <= $self->{next_char} and  
                 $self->{next_char} <= 0x0039) { # 0..9  
         !!!cp (1012);  
         $code *= 10;  
         $code += $self->{next_char} - 0x0030;  
           
         !!!next-input-character;  
       }  
   
       if ($self->{next_char} == 0x003B) { # ;  
         !!!cp (1013);  
         !!!next-input-character;  
       } else {  
         !!!cp (1014);  
         !!!parse-error (type => 'no refc');  
       }  
   
       if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {  
         !!!cp (1015);  
         !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);  
         $code = 0xFFFD;  
       } elsif ($code > 0x10FFFF) {  
         !!!cp (1016);  
         !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);  
         $code = 0xFFFD;  
       } elsif ($code == 0x000D) {  
         !!!cp (1017);  
         !!!parse-error (type => 'CR character reference');  
         $code = 0x000A;  
       } elsif (0x80 <= $code and $code <= 0x9F) {  
         !!!cp (1018);  
         !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);  
         $code = $c1_entity_char->{$code};  
       }  
         
       return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};  
     } else {  
       !!!cp (1019);  
       !!!parse-error (type => 'bare nero');  
       !!!back-next-input-character ($self->{next_char});  
       $self->{next_char} = 0x0023; # #  
       return undef;  
     }  
   } elsif ((0x0041 <= $self->{next_char} and  
             $self->{next_char} <= 0x005A) or  
            (0x0061 <= $self->{next_char} and  
             $self->{next_char} <= 0x007A)) {  
     my $entity_name = chr $self->{next_char};  
     !!!next-input-character;  
   
     my $value = $entity_name;  
     my $match = 0;  
     require Whatpm::_NamedEntityList;  
     our $EntityChar;  
   
     while (length $entity_name < 10 and  
            ## NOTE: Some number greater than the maximum length of entity name  
            ((0x0041 <= $self->{next_char} and # a  
              $self->{next_char} <= 0x005A) or # x  
             (0x0061 <= $self->{next_char} and # a  
              $self->{next_char} <= 0x007A) or # z  
             (0x0030 <= $self->{next_char} and # 0  
              $self->{next_char} <= 0x0039) or # 9  
             $self->{next_char} == 0x003B)) { # ;  
       $entity_name .= chr $self->{next_char};  
       if (defined $EntityChar->{$entity_name}) {  
         if ($self->{next_char} == 0x003B) { # ;  
           !!!cp (1020);  
           $value = $EntityChar->{$entity_name};  
           $match = 1;  
           !!!next-input-character;  
           last;  
         } else {  
           !!!cp (1021);  
           $value = $EntityChar->{$entity_name};  
           $match = -1;  
           !!!next-input-character;  
         }  
       } else {  
         !!!cp (1022);  
         $value .= chr $self->{next_char};  
         $match *= 2;  
         !!!next-input-character;  
       }  
     }  
       
     if ($match > 0) {  
       !!!cp (1023);  
       return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};  
     } elsif ($match < 0) {  
       !!!parse-error (type => 'no refc');  
       if ($in_attr and $match < -1) {  
         !!!cp (1024);  
         return {type => CHARACTER_TOKEN, data => '&'.$entity_name};  
       } else {  
         !!!cp (1025);  
         return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};  
       }  
     } else {  
       !!!cp (1026);  
       !!!parse-error (type => 'bare ero');  
       ## NOTE: "No characters are consumed" in the spec.  
       return {type => CHARACTER_TOKEN, data => '&'.$value};  
     }  
   } else {  
     !!!cp (1027);  
     ## no characters are consumed  
     !!!parse-error (type => 'bare ero');  
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
   
850  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
851    my $self = shift;    my $self = shift;
852    ## NOTE: $self->{document} MUST be specified before this method is called    ## NOTE: $self->{document} MUST be specified before this method is called
# Line 2400  sub _initialize_tree_constructor ($) { Line 854  sub _initialize_tree_constructor ($) {
854    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
855    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
856    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
857      $self->{document}->set_user_data (manakai_source_line => 1);
858      $self->{document}->set_user_data (manakai_source_column => 1);
859  } # _initialize_tree_constructor  } # _initialize_tree_constructor
860    
861  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2419  sub _construct_tree ($) { Line 875  sub _construct_tree ($) {
875    ## When an interactive UA render the $self->{document} available    ## When an interactive UA render the $self->{document} available
876    ## to the user, or when it begin accepting user input, are    ## to the user, or when it begin accepting user input, are
877    ## not defined.    ## not defined.
   
   ## Append a character: collect it and all subsequent consecutive  
   ## characters and insert one Text node whose data is concatenation  
   ## of all those characters. # MUST  
878        
879    !!!next-token;    !!!next-token;
880    
881    undef $self->{form_element};    undef $self->{form_element};
882    undef $self->{head_element};    undef $self->{head_element};
883      undef $self->{head_element_inserted};
884    $self->{open_elements} = [];    $self->{open_elements} = [];
885    undef $self->{inner_html_node};    undef $self->{inner_html_node};
886      undef $self->{ignore_newline};
887    
888    ## NOTE: The "initial" insertion mode.    ## NOTE: The "initial" insertion mode.
889    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
# Line 2454  sub _tree_construction_initial ($) { Line 908  sub _tree_construction_initial ($) {
908        ## language.        ## language.
909        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
910        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
911        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
912        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
913            defined $token->{public_identifier} or            defined $token->{sysid}) {
           defined $token->{system_identifier}) {  
914          !!!cp ('t1');          !!!cp ('t1');
915          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
916        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
917          !!!cp ('t2');          !!!cp ('t2');
918          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!parse-error (type => 'not HTML5', token => $token);
919          !!!parse-error (type => 'not HTML5');        } elsif (defined $token->{pubid}) {
920            if ($token->{pubid} eq 'XSLT-compat') {
921              !!!cp ('t1.2');
922              !!!parse-error (type => 'XSLT-compat', token => $token,
923                              level => $self->{level}->{should});
924            } else {
925              !!!parse-error (type => 'not HTML5', token => $token);
926            }
927        } else {        } else {
928          !!!cp ('t3');          !!!cp ('t3');
929            #
930        }        }
931                
932        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
933          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
934        $doctype->public_id ($token->{public_identifier})        ## NOTE: Default value for both |public_id| and |system_id| attributes
935            if defined $token->{public_identifier};        ## are empty strings, so that we don't set any value in missing cases.
936        $doctype->system_id ($token->{system_identifier})        $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
937            if defined $token->{system_identifier};        $doctype->system_id ($token->{sysid}) if defined $token->{sysid};
938        ## NOTE: Other DocumentType attributes are null or empty lists.        ## NOTE: Other DocumentType attributes are null or empty lists.
939        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
940        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
# Line 2481  sub _tree_construction_initial ($) { Line 942  sub _tree_construction_initial ($) {
942        if ($token->{quirks} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
943          !!!cp ('t4');          !!!cp ('t4');
944          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
945        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{pubid}) {
946          my $pubid = $token->{public_identifier};          my $pubid = $token->{pubid};
947          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
948          if ({          my $prefix = [
949            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
950            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
951            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
952            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
953            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
954            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
955            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
956            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
957            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
958            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
959            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
960            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
961            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
962            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
963            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
964            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
965            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
966            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
967            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
968            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
969            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
970            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
971            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
972            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
973            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
974            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
975            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
976            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
977            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
978            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
979            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
980            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
981            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
982            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
983            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
984            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
985            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
986            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
987            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
988            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
989            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
990            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
991            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
992            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
993            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
994            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
995            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
996            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
997            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
998            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
999            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
1000            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
1001            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
1002            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
1003            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
1004            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
1005            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
1006            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
1007            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
1008            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
1009            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
1010            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
1011            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
1012            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
1013            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
1014            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
1015            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,              $pubid eq "HTML") {
           "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,  
           "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,  
           "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,  
           "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,  
           "HTML" => 1,  
         }->{$pubid}) {  
1016            !!!cp ('t5');            !!!cp ('t5');
1017            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
1018          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
1019                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
1020            if (defined $token->{system_identifier}) {            if (defined $token->{sysid}) {
1021              !!!cp ('t6');              !!!cp ('t6');
1022              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
1023            } else {            } else {
1024              !!!cp ('t7');              !!!cp ('t7');
1025              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
1026            }            }
1027          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
1028                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
1029            !!!cp ('t8');            !!!cp ('t8');
1030            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
1031          } else {          } else {
# Line 2579  sub _tree_construction_initial ($) { Line 1034  sub _tree_construction_initial ($) {
1034        } else {        } else {
1035          !!!cp ('t10');          !!!cp ('t10');
1036        }        }
1037        if (defined $token->{system_identifier}) {        if (defined $token->{sysid}) {
1038          my $sysid = $token->{system_identifier};          my $sysid = $token->{sysid};
1039          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
1040          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
1041            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
1042              ## marked as quirks.
1043            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
1044            !!!cp ('t11');            !!!cp ('t11');
1045          } else {          } else {
# Line 2602  sub _tree_construction_initial ($) { Line 1058  sub _tree_construction_initial ($) {
1058                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
1059               }->{$token->{type}}) {               }->{$token->{type}}) {
1060        !!!cp ('t14');        !!!cp ('t14');
1061        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
1062        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
1063        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
1064        ## reprocess        ## reprocess
1065          !!!ack-later;
1066        return;        return;
1067      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
1068        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
1069          ## Ignore the token          ## Ignore the token
1070    
1071          unless (length $token->{data}) {          unless (length $token->{data}) {
# Line 2623  sub _tree_construction_initial ($) { Line 1080  sub _tree_construction_initial ($) {
1080          !!!cp ('t17');          !!!cp ('t17');
1081        }        }
1082    
1083        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
1084        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
1085        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
1086        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 1109  sub _tree_construction_root_element ($)
1109    B: {    B: {
1110        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
1111          !!!cp ('t19');          !!!cp ('t19');
1112          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
1113          ## Ignore the token          ## Ignore the token
1114          ## Stay in the insertion mode.          ## Stay in the insertion mode.
1115          !!!next-token;          !!!next-token;
# Line 2665  sub _tree_construction_root_element ($) Line 1122  sub _tree_construction_root_element ($)
1122          !!!next-token;          !!!next-token;
1123          redo B;          redo B;
1124        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
1125          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
1126            ## Ignore the token.            ## Ignore the token.
1127    
1128            unless (length $token->{data}) {            unless (length $token->{data}) {
# Line 2686  sub _tree_construction_root_element ($) Line 1143  sub _tree_construction_root_element ($)
1143        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
1144          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
1145            my $root_element;            my $root_element;
1146            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
1147            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
1148            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
1149                  [$root_element, $el_category->{html}];
1150    
1151            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
1152              !!!cp ('t24');              !!!cp ('t24');
1153              $self->{application_cache_selection}              $self->{application_cache_selection}
1154                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
1155              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
1156                ## According to Hixie (#whatwg 2008-03-19), it should be
1157                ## resolved against the base URI of the document in HTML
1158                ## or xml:base of the element in XHTML.
1159            } else {            } else {
1160              !!!cp ('t25');              !!!cp ('t25');
1161              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
1162            }            }
1163    
1164              !!!nack ('t25c');
1165    
1166            !!!next-token;            !!!next-token;
1167            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
1168          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 1179  sub _tree_construction_root_element ($)
1179          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
1180        }        }
1181    
1182      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
1183        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
1184      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
1185      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
1186    
1187      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
1188    
1189      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
1190        !!!ack-later;
1191      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
   
     ## ISSUE: There is an issue in the spec  
1192    } # B    } # B
1193    
1194    die "$0: _tree_construction_root_element: This should never be reached";    die "$0: _tree_construction_root_element: This should never be reached";
# Line 2743  sub _reset_insertion_mode ($) { Line 1206  sub _reset_insertion_mode ($) {
1206            
1207      ## Step 3      ## Step 3
1208      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
1209        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
1210          $last = 1;          $last = 1;
1211          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
1212            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
1213                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
1214              !!!cp ('t27');          } else {
1215              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
1216          }          }
1217        }        }
1218              
1219        ## Step 4..13        ## Step 4..14
1220        my $new_mode = {        my $new_mode;
1221          if ($node->[1] & FOREIGN_EL) {
1222            !!!cp ('t28.1');
1223            ## NOTE: Strictly spaking, the line below only applies to MathML and
1224            ## SVG elements.  Currently the HTML syntax supports only MathML and
1225            ## SVG elements as foreigners.
1226            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
1227          } elsif ($node->[1] == TABLE_CELL_EL) {
1228            if ($last) {
1229              !!!cp ('t28.2');
1230              #
1231            } else {
1232              !!!cp ('t28.3');
1233              $new_mode = IN_CELL_IM;
1234            }
1235          } else {
1236            !!!cp ('t28.4');
1237            $new_mode = {
1238                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
1239                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
1240                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
1241                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
1242                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
1243                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2779  sub _reset_insertion_mode ($) { Line 1248  sub _reset_insertion_mode ($) {
1248                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
1249                        body => IN_BODY_IM,                        body => IN_BODY_IM,
1250                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
1251                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
1252          }
1253        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
1254                
1255        ## Step 14        ## Step 15
1256        if ($node->[1] eq 'html') {        if ($node->[1] == HTML_EL) {
1257          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
1258            !!!cp ('t29');            !!!cp ('t29');
1259            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2797  sub _reset_insertion_mode ($) { Line 1267  sub _reset_insertion_mode ($) {
1267          !!!cp ('t31');          !!!cp ('t31');
1268        }        }
1269                
1270        ## Step 15        ## Step 16
1271        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
1272                
1273        ## Step 16        ## Step 17
1274        $i--;        $i--;
1275        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
1276                
1277        ## Step 17        ## Step 18
1278        redo S3;        redo S3;
1279      } # S3      } # S3
1280    
# Line 2908  sub _tree_construction_main ($) { Line 1378  sub _tree_construction_main ($) {
1378      !!!cp ('t39');      !!!cp ('t39');
1379    }; # $clear_up_to_marker    }; # $clear_up_to_marker
1380    
1381    my $parse_rcdata = sub ($$) {    my $insert;
1382      my ($content_model_flag, $insert) = @_;  
1383      my $parse_rcdata = sub ($) {
1384        my ($content_model_flag) = @_;
1385    
1386      ## Step 1      ## Step 1
1387      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
1388      my $el;      !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
     !!!create-element ($el, $start_tag_name, $token->{attributes});  
1389    
1390      ## Step 2      ## Step 2
     $insert->($el); # /context node/->append_child ($el)  
   
     ## Step 3  
1391      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
1392      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
1393    
1394      ## Step 4      ## Step 3, 4
1395      my $text = '';      $self->{insertion_mode} |= IN_CDATA_RCDATA_IM;
     !!!next-token;  
     while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing  
       !!!cp ('t40');  
       $text .= $token->{data};  
       !!!next-token;  
     }  
   
     ## Step 5  
     if (length $text) {  
       !!!cp ('t41');  
       my $text = $self->{document}->create_text_node ($text);  
       $el->append_child ($text);  
     }  
   
     ## Step 6  
     $self->{content_model} = PCDATA_CONTENT_MODEL;  
1396    
1397      ## Step 7      !!!nack ('t40.1');
     if ($token->{type} == END_TAG_TOKEN and  
         $token->{tag_name} eq $start_tag_name) {  
       !!!cp ('t42');  
       ## Ignore the token  
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!cp ('t43');  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!cp ('t44');  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
     } else {  
       die "$0: $content_model_flag in parse_rcdata";  
     }  
1398      !!!next-token;      !!!next-token;
1399    }; # $parse_rcdata    }; # $parse_rcdata
1400    
1401    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
1402      my $insert = $_[0];      ## Step 1
1403      my $script_el;      my $script_el;
1404      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
1405    
1406        ## Step 2
1407      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
1408    
1409        ## Step 3
1410        ## TODO: Mark as "already executed", if ...
1411    
1412        ## Step 4
1413        $insert->($script_el);
1414    
1415        ## ISSUE: $script_el is not put into the stack
1416        push @{$self->{open_elements}}, [$script_el, $el_category->{script}];
1417    
1418        ## Step 5
1419      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
1420      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
       
     my $text = '';  
     !!!next-token;  
     while ($token->{type} == CHARACTER_TOKEN) {  
       !!!cp ('t45');  
       $text .= $token->{data};  
       !!!next-token;  
     } # stop if non-character token or tokenizer stops tokenising  
     if (length $text) {  
       !!!cp ('t46');  
       $script_el->manakai_append_text ($text);  
     }  
                 
     $self->{content_model} = PCDATA_CONTENT_MODEL;  
1421    
1422      if ($token->{type} == END_TAG_TOKEN and      ## Step 6-7
1423          $token->{tag_name} eq 'script') {      $self->{insertion_mode} |= IN_CDATA_RCDATA_IM;
       !!!cp ('t47');  
       ## Ignore the token  
     } else {  
       !!!cp ('t48');  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
       ## ISSUE: And ignore?  
       ## TODO: mark as "already executed"  
     }  
       
     if (defined $self->{inner_html_node}) {  
       !!!cp ('t49');  
       ## TODO: mark as "already executed"  
     } else {  
       !!!cp ('t50');  
       ## TODO: $old_insertion_point = current insertion point  
       ## TODO: insertion point = just before the next input character  
1424    
1425        $insert->($script_el);      !!!nack ('t40.2');
         
       ## TODO: insertion point = $old_insertion_point (might be "undefined")  
         
       ## TODO: if there is a script that will execute as soon as the parser resume, then...  
     }  
       
1426      !!!next-token;      !!!next-token;
1427    }; # $script_start_tag    }; # $script_start_tag
1428    
1429      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
1430      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
1431      ## NOTE: $open_tables->[-1]->[2] is set false when non-Text node inserted.
1432      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
1433    
1434    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
1435      my $tag_name = shift;      my $end_tag_token = shift;
1436        my $tag_name = $end_tag_token->{tag_name};
1437    
1438        ## NOTE: The adoption agency algorithm (AAA).
1439    
1440      FET: {      FET: {
1441        ## Step 1        ## Step 1
1442        my $formatting_element;        my $formatting_element;
1443        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
1444        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
1445          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
1446              !!!cp ('t52');
1447              last AFE;
1448            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
1449                         eq $tag_name) {
1450            !!!cp ('t51');            !!!cp ('t51');
1451            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
1452            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
1453            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
1454          }          }
1455        } # AFE        } # AFE
1456        unless (defined $formatting_element) {        unless (defined $formatting_element) {
1457          !!!cp ('t53');          !!!cp ('t53');
1458          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
1459          ## Ignore the token          ## Ignore the token
1460          !!!next-token;          !!!next-token;
1461          return;          return;
# Line 3048  sub _tree_construction_main ($) { Line 1472  sub _tree_construction_main ($) {
1472              last INSCOPE;              last INSCOPE;
1473            } else { # in open elements but not in scope            } else { # in open elements but not in scope
1474              !!!cp ('t55');              !!!cp ('t55');
1475              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
1476                                text => $token->{tag_name},
1477                                token => $end_tag_token);
1478              ## Ignore the token              ## Ignore the token
1479              !!!next-token;              !!!next-token;
1480              return;              return;
1481            }            }
1482          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
1483            !!!cp ('t56');            !!!cp ('t56');
1484            $in_scope = 0;            $in_scope = 0;
1485          }          }
1486        } # INSCOPE        } # INSCOPE
1487        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
1488          !!!cp ('t57');          !!!cp ('t57');
1489          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
1490                            text => $token->{tag_name},
1491                            token => $end_tag_token);
1492          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
1493          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
1494          return;          return;
1495        }        }
1496        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
1497          !!!cp ('t58');          !!!cp ('t58');
1498          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
1499                            text => $self->{open_elements}->[-1]->[0]
1500                                ->manakai_local_name,
1501                            token => $end_tag_token);
1502        }        }
1503                
1504        ## Step 2        ## Step 2
# Line 3078  sub _tree_construction_main ($) { Line 1506  sub _tree_construction_main ($) {
1506        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
1507        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
1508          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
1509          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
1510              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
1511              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
1512               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
1513            !!!cp ('t59');            !!!cp ('t59');
1514            $furthest_block = $node;            $furthest_block = $node;
1515            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
1516              ## NOTE: The topmost (eldest) node.
1517          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
1518            !!!cp ('t60');            !!!cp ('t60');
1519            last OE;            last OE;
# Line 3167  sub _tree_construction_main ($) { Line 1596  sub _tree_construction_main ($) {
1596        } # S7          } # S7  
1597                
1598        ## Step 8        ## Step 8
1599        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
1600            my $foster_parent_element;
1601            my $next_sibling;
1602            OE: for (reverse 0..$#{$self->{open_elements}}) {
1603              if ($self->{open_elements}->[$_]->[1] == TABLE_EL) {
1604                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
1605                                 if (defined $parent and $parent->node_type == 1) {
1606                                   !!!cp ('t65.1');
1607                                   $foster_parent_element = $parent;
1608                                   $next_sibling = $self->{open_elements}->[$_]->[0];
1609                                 } else {
1610                                   !!!cp ('t65.2');
1611                                   $foster_parent_element
1612                                     = $self->{open_elements}->[$_ - 1]->[0];
1613                                 }
1614                                 last OE;
1615                               }
1616                             } # OE
1617                             $foster_parent_element = $self->{open_elements}->[0]->[0]
1618                               unless defined $foster_parent_element;
1619            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
1620            $open_tables->[-1]->[1] = 1; # tainted
1621          } else {
1622            !!!cp ('t65.3');
1623            $common_ancestor_node->[0]->append_child ($last_node->[0]);
1624          }
1625                
1626        ## Step 9        ## Step 9
1627        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3206  sub _tree_construction_main ($) { Line 1660  sub _tree_construction_main ($) {
1660            $i = $_;            $i = $_;
1661          }          }
1662        } # OE        } # OE
1663        splice @{$self->{open_elements}}, $i + 1, 1, $clone;        splice @{$self->{open_elements}}, $i + 1, 0, $clone;
1664                
1665        ## Step 14        ## Step 14
1666        redo FET;        redo FET;
1667      } # FET      } # FET
1668    }; # $formatting_end_tag    }; # $formatting_end_tag
1669    
1670    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
1671      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
1672    }; # $insert_to_current    }; # $insert_to_current
1673    
1674    my $insert_to_foster = sub {    my $insert_to_foster = sub {
1675                         my $child = shift;      my $child = shift;
1676                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
1677                              table => 1, tbody => 1, tfoot => 1,        # MUST
1678                              thead => 1, tr => 1,        my $foster_parent_element;
1679                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
1680                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
1681                           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') {  
1682                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
1683                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
1684                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3245  sub _tree_construction_main ($) { Line 1696  sub _tree_construction_main ($) {
1696                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
1697                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
1698                             ($child, $next_sibling);                             ($child, $next_sibling);
1699                         } else {        $open_tables->[-1]->[1] = 1; # tainted
1700                           !!!cp ('t72');      } else {
1701                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
1702                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
1703        }
1704    }; # $insert_to_foster    }; # $insert_to_foster
1705    
1706    my $insert;    ## NOTE: Insert a character (MUST): When a character is inserted, if
1707      ## the last node that was inserted by the parser is a Text node and
1708      ## the character has to be inserted after that node, then the
1709      ## character is appended to the Text node.  However, if any other
1710      ## node is inserted by the parser, then a new Text node is created
1711      ## and the character is appended as that Text node.  If I'm not
1712      ## wrong, for a parser with scripting disabled, there are only two
1713      ## cases where this occurs.  One is the case where an element node
1714      ## is inserted to the |head| element.  This is covered by using the
1715      ## |$self->{head_element_inserted}| flag.  Another is the case where
1716      ## an element or comment is inserted into the |table| subtree while
1717      ## foster parenting happens.  This is covered by using the [2] flag
1718      ## of the |$open_tables| structure.  All other cases are handled
1719      ## simply by calling |manakai_append_text| method.
1720    
1721      ## TODO: |<body><script>document.write("a<br>");
1722      ## document.body.removeChild (document.body.lastChild);
1723      ## document.write ("b")</script>|
1724    
1725    B: {    B: while (1) {
1726      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
1727        !!!cp ('t73');        !!!cp ('t73');
1728        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
1729        ## Ignore the token        ## Ignore the token
1730        ## Stay in the phase        ## Stay in the phase
1731        !!!next-token;        !!!next-token;
1732        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         while ({  
                 dd => 1, dt => 1, li => 1, p => 1,  
                }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           pop @{$self->{open_elements}};  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!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') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
1733      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
1734               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
1735        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
1736          !!!cp ('t79');          !!!cp ('t79');
1737          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
1738          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
1739        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
1740          !!!cp ('t80');          !!!cp ('t80');
1741          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
1742          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
1743        } else {        } else {
1744          !!!cp ('t81');          !!!cp ('t81');
1745        }        }
1746    
1747        !!!cp ('t82');        !!!cp ('t82');
1748        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
1749        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
1750        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
1751          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3318  sub _tree_construction_main ($) { Line 1755  sub _tree_construction_main ($) {
1755               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
1756          }          }
1757        }        }
1758          !!!nack ('t84.1');
1759        !!!next-token;        !!!next-token;
1760        redo B;        next B;
1761      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
1762        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
1763        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3331  sub _tree_construction_main ($) { Line 1769  sub _tree_construction_main ($) {
1769        } else {        } else {
1770          !!!cp ('t87');          !!!cp ('t87');
1771          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
1772            $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
1773        }        }
1774        !!!next-token;        !!!next-token;
1775        redo B;        next B;
1776      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_CDATA_RCDATA_IM) {
1777        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
1778          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          $token->{data} =~ s/^\x0A// if $self->{ignore_newline};
1779            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          delete $self->{ignore_newline};
1780    
1781            if (length $token->{data}) {
1782              !!!cp ('t43');
1783              $self->{open_elements}->[-1]->[0]->manakai_append_text
1784                  ($token->{data});
1785            } else {
1786              !!!cp ('t43.1');
1787            }
1788            !!!next-token;
1789            next B;
1790          } elsif ($token->{type} == END_TAG_TOKEN) {
1791            delete $self->{ignore_newline};
1792    
1793            if ($token->{tag_name} eq 'script') {
1794              !!!cp ('t50');
1795              
1796              ## Para 1-2
1797              my $script = pop @{$self->{open_elements}};
1798              
1799              ## Para 3
1800              $self->{insertion_mode} &= ~ IN_CDATA_RCDATA_IM;
1801    
1802              ## Para 4
1803              ## TODO: $old_insertion_point = $current_insertion_point;
1804              ## TODO: $current_insertion_point = just before $self->{nc};
1805    
1806              ## Para 5
1807              ## TODO: Run the $script->[0].
1808    
1809              ## Para 6
1810              ## TODO: $current_insertion_point = $old_insertion_point;
1811    
1812              ## Para 7
1813              ## TODO: if ($pending_external_script) {
1814                ## TODO: ...
1815              ## TODO: }
1816    
1817              !!!next-token;
1818              next B;
1819            } else {
1820              !!!cp ('t42');
1821    
1822              pop @{$self->{open_elements}};
1823    
1824              $self->{insertion_mode} &= ~ IN_CDATA_RCDATA_IM;
1825              !!!next-token;
1826              next B;
1827            }
1828          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
1829            delete $self->{ignore_newline};
1830    
1831            !!!cp ('t44');
1832            !!!parse-error (type => 'not closed',
1833                            text => $self->{open_elements}->[-1]->[0]
1834                                ->manakai_local_name,
1835                            token => $token);
1836    
1837            #if ($self->{open_elements}->[-1]->[1] == SCRIPT_EL) {
1838            #  ## TODO: Mark as "already executed"
1839            #}
1840    
1841            pop @{$self->{open_elements}};
1842    
1843            $self->{insertion_mode} &= ~ IN_CDATA_RCDATA_IM;
1844            ## Reprocess.
1845            next B;
1846          } else {
1847            die "$0: $token->{type}: In CDATA/RCDATA: Unknown token type";        
1848          }
1849        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
1850          if ($token->{type} == CHARACTER_TOKEN) {
1851            !!!cp ('t87.1');
1852            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
1853            !!!next-token;
1854            next B;
1855          } elsif ($token->{type} == START_TAG_TOKEN) {
1856            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
1857                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
1858                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
1859                ($token->{tag_name} eq 'svg' and
1860                 $self->{open_elements}->[-1]->[1] == MML_AXML_EL)) {
1861              ## NOTE: "using the rules for secondary insertion mode"then"continue"
1862              !!!cp ('t87.2');
1863              #
1864            } elsif ({
1865                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
1866                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
1867                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
1868                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
1869                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
1870                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
1871                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
1872                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
1873                     }->{$token->{tag_name}}) {
1874              !!!cp ('t87.2');
1875              !!!parse-error (type => 'not closed',
1876                              text => $self->{open_elements}->[-1]->[0]
1877                                  ->manakai_local_name,
1878                              token => $token);
1879    
1880              pop @{$self->{open_elements}}
1881                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
1882    
1883              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
1884              ## Reprocess.
1885              next B;
1886            } else {
1887              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
1888              my $tag_name = $token->{tag_name};
1889              if ($nsuri eq $SVG_NS) {
1890                $tag_name = {
1891                   altglyph => 'altGlyph',
1892                   altglyphdef => 'altGlyphDef',
1893                   altglyphitem => 'altGlyphItem',
1894                   animatecolor => 'animateColor',
1895                   animatemotion => 'animateMotion',
1896                   animatetransform => 'animateTransform',
1897                   clippath => 'clipPath',
1898                   feblend => 'feBlend',
1899                   fecolormatrix => 'feColorMatrix',
1900                   fecomponenttransfer => 'feComponentTransfer',
1901                   fecomposite => 'feComposite',
1902                   feconvolvematrix => 'feConvolveMatrix',
1903                   fediffuselighting => 'feDiffuseLighting',
1904                   fedisplacementmap => 'feDisplacementMap',
1905                   fedistantlight => 'feDistantLight',
1906                   feflood => 'feFlood',
1907                   fefunca => 'feFuncA',
1908                   fefuncb => 'feFuncB',
1909                   fefuncg => 'feFuncG',
1910                   fefuncr => 'feFuncR',
1911                   fegaussianblur => 'feGaussianBlur',
1912                   feimage => 'feImage',
1913                   femerge => 'feMerge',
1914                   femergenode => 'feMergeNode',
1915                   femorphology => 'feMorphology',
1916                   feoffset => 'feOffset',
1917                   fepointlight => 'fePointLight',
1918                   fespecularlighting => 'feSpecularLighting',
1919                   fespotlight => 'feSpotLight',
1920                   fetile => 'feTile',
1921                   feturbulence => 'feTurbulence',
1922                   foreignobject => 'foreignObject',
1923                   glyphref => 'glyphRef',
1924                   lineargradient => 'linearGradient',
1925                   radialgradient => 'radialGradient',
1926                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
1927                   textpath => 'textPath',  
1928                }->{$tag_name} || $tag_name;
1929              }
1930    
1931              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
1932    
1933              ## "adjust foreign attributes" - done in insert-element-f
1934    
1935              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
1936    
1937              if ($self->{self_closing}) {
1938                pop @{$self->{open_elements}};
1939                !!!ack ('t87.3');
1940              } else {
1941                !!!cp ('t87.4');
1942              }
1943    
1944              !!!next-token;
1945              next B;
1946            }
1947          } elsif ($token->{type} == END_TAG_TOKEN) {
1948            ## NOTE: "using the rules for secondary insertion mode" then "continue"
1949            !!!cp ('t87.5');
1950            #
1951          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
1952            !!!cp ('t87.6');
1953            !!!parse-error (type => 'not closed',
1954                            text => $self->{open_elements}->[-1]->[0]
1955                                ->manakai_local_name,
1956                            token => $token);
1957    
1958            pop @{$self->{open_elements}}
1959                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
1960    
1961            ## NOTE: |<span><svg>| ... two parse errors, |<svg>| ... a parse error.
1962    
1963            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
1964            ## Reprocess.
1965            next B;
1966          } else {
1967            die "$0: $token->{type}: Unknown token type";        
1968          }
1969        }
1970    
1971        if ($self->{insertion_mode} & HEAD_IMS) {
1972          if ($token->{type} == CHARACTER_TOKEN) {
1973            if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
1974              unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
1975                if ($self->{head_element_inserted}) {
1976                  !!!cp ('t88.3');
1977                  $self->{open_elements}->[-1]->[0]->append_child
1978                    ($self->{document}->create_text_node ($1));
1979                  delete $self->{head_element_inserted};
1980                  ## NOTE: |</head> <link> |
1981                  #
1982                } else {
1983                  !!!cp ('t88.2');
1984                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
1985                  ## NOTE: |</head> &#x20;|
1986                  #
1987                }
1988              } else {
1989                !!!cp ('t88.1');
1990                ## Ignore the token.
1991                #
1992              }
1993            unless (length $token->{data}) {            unless (length $token->{data}) {
1994              !!!cp ('t88');              !!!cp ('t88');
1995              !!!next-token;              !!!next-token;
1996              redo B;              next B;
1997            }            }
1998    ## TODO: set $token->{column} appropriately
1999          }          }
2000    
2001          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2002            !!!cp ('t89');            !!!cp ('t89');
2003            ## As if <head>            ## As if <head>
2004            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2005            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2006            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
2007                  [$self->{head_element}, $el_category->{head}];
2008    
2009            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
2010            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3360  sub _tree_construction_main ($) { Line 2014  sub _tree_construction_main ($) {
2014            !!!cp ('t90');            !!!cp ('t90');
2015            ## As if </noscript>            ## As if </noscript>
2016            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2017            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
2018                        
2019            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
2020            ## As if </head>            ## As if </head>
# Line 3376  sub _tree_construction_main ($) { Line 2030  sub _tree_construction_main ($) {
2030            !!!cp ('t92');            !!!cp ('t92');
2031          }          }
2032    
2033              ## "after head" insertion mode          ## "after head" insertion mode
2034              ## As if <body>          ## As if <body>
2035              !!!insert-element ('body');          !!!insert-element ('body',, $token);
2036              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
2037              ## reprocess          ## reprocess
2038              redo B;          next B;
2039            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
2040              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
2041                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2042                  !!!cp ('t93');              !!!cp ('t93');
2043                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
2044                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
2045                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
2046                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
2047                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
2048                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
2049                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
2050                  !!!cp ('t94');              !!!next-token;
2051                  #              next B;
2052                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2053                  !!!cp ('t95');              !!!cp ('t93.2');
2054                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
2055                  ## Ignore the token                              token => $token);
2056                  !!!next-token;              ## Ignore the token
2057                  redo B;              !!!nack ('t93.3');
2058                }              !!!next-token;
2059              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
2060                !!!cp ('t96');            } else {
2061                ## As if <head>              !!!cp ('t95');
2062                !!!create-element ($self->{head_element}, 'head');              !!!parse-error (type => 'in head:head',
2063                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
2064                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
2065                !!!nack ('t95.1');
2066                !!!next-token;
2067                next B;
2068              }
2069            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2070              !!!cp ('t96');
2071              ## As if <head>
2072              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2073              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2074              push @{$self->{open_elements}},
2075                  [$self->{head_element}, $el_category->{head}];
2076    
2077                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
2078                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
2079              } else {          } else {
2080                !!!cp ('t97');            !!!cp ('t97');
2081              }          }
2082    
2083              if ($token->{tag_name} eq 'base') {          if ($token->{tag_name} eq 'base') {
2084                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {            if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2085                  !!!cp ('t98');              !!!cp ('t98');
2086                  ## As if </noscript>              ## As if </noscript>
2087                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
2088                  !!!parse-error (type => 'in noscript:base');              !!!parse-error (type => 'in noscript', text => 'base',
2089                                              token => $token);
2090                  $self->{insertion_mode} = IN_HEAD_IM;            
2091                  ## Reprocess in the "in head" insertion mode...              $self->{insertion_mode} = IN_HEAD_IM;
2092                } else {              ## Reprocess in the "in head" insertion mode...
2093                  !!!cp ('t99');            } else {
2094                }              !!!cp ('t99');
2095              }
2096    
2097                ## NOTE: There is a "as if in head" code clone.            ## NOTE: There is a "as if in head" code clone.
2098                if ($self->{insertion_mode} == AFTER_HEAD_IM) {            if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2099                  !!!cp ('t100');              !!!cp ('t100');
2100                  !!!parse-error (type => 'after head:'.$token->{tag_name});              !!!parse-error (type => 'after head',
2101                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                              text => $token->{tag_name}, token => $token);
2102                } else {              push @{$self->{open_elements}},
2103                  !!!cp ('t101');                  [$self->{head_element}, $el_category->{head}];
2104                }              $self->{head_element_inserted} = 1;
2105                !!!insert-element ($token->{tag_name}, $token->{attributes});            } else {
2106                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.              !!!cp ('t101');
2107                pop @{$self->{open_elements}}            }
2108                    if $self->{insertion_mode} == AFTER_HEAD_IM;            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2109                !!!next-token;            pop @{$self->{open_elements}};
2110                redo B;            pop @{$self->{open_elements}} # <head>
2111              } elsif ($token->{tag_name} eq 'link') {                if $self->{insertion_mode} == AFTER_HEAD_IM;
2112                ## NOTE: There is a "as if in head" code clone.            !!!nack ('t101.1');
2113                if ($self->{insertion_mode} == AFTER_HEAD_IM) {            !!!next-token;
2114                  !!!cp ('t102');            next B;
2115                  !!!parse-error (type => 'after head:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'link') {
2116                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            ## NOTE: There is a "as if in head" code clone.
2117                } else {            if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2118                  !!!cp ('t103');              !!!cp ('t102');
2119                }              !!!parse-error (type => 'after head',
2120                !!!insert-element ($token->{tag_name}, $token->{attributes});                              text => $token->{tag_name}, token => $token);
2121                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.              push @{$self->{open_elements}},
2122                pop @{$self->{open_elements}}                  [$self->{head_element}, $el_category->{head}];
2123                    if $self->{insertion_mode} == AFTER_HEAD_IM;              $self->{head_element_inserted} = 1;
2124                !!!next-token;            } else {
2125                redo B;              !!!cp ('t103');
2126              } elsif ($token->{tag_name} eq 'meta') {            }
2127                ## NOTE: There is a "as if in head" code clone.            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2128                if ($self->{insertion_mode} == AFTER_HEAD_IM) {            pop @{$self->{open_elements}};
2129                  !!!cp ('t104');            pop @{$self->{open_elements}} # <head>
2130                  !!!parse-error (type => 'after head:'.$token->{tag_name});                if $self->{insertion_mode} == AFTER_HEAD_IM;
2131                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!ack ('t103.1');
2132                } else {            !!!next-token;
2133                  !!!cp ('t105');            next B;
2134                }          } elsif ($token->{tag_name} eq 'command' or
2135                !!!insert-element ($token->{tag_name}, $token->{attributes});                   $token->{tag_name} eq 'eventsource') {
2136                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.            if ($self->{insertion_mode} == IN_HEAD_IM) {
2137                ## NOTE: If the insertion mode at the time of the emission
2138                ## of the token was "before head", $self->{insertion_mode}
2139                ## is already changed to |IN_HEAD_IM|.
2140    
2141                ## NOTE: There is a "as if in head" code clone.
2142                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2143                pop @{$self->{open_elements}};
2144                pop @{$self->{open_elements}} # <head>
2145                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2146                !!!ack ('t103.2');
2147                !!!next-token;
2148                next B;
2149              } else {
2150                ## NOTE: "in head noscript" or "after head" insertion mode
2151                ## - in these cases, these tags are treated as same as
2152                ## normal in-body tags.
2153                !!!cp ('t103.3');
2154                #
2155              }
2156            } elsif ($token->{tag_name} eq 'meta') {
2157              ## NOTE: There is a "as if in head" code clone.
2158              if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2159                !!!cp ('t104');
2160                !!!parse-error (type => 'after head',
2161                                text => $token->{tag_name}, token => $token);
2162                push @{$self->{open_elements}},
2163                    [$self->{head_element}, $el_category->{head}];
2164                $self->{head_element_inserted} = 1;
2165              } else {
2166                !!!cp ('t105');
2167              }
2168              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2169              my $meta_el = pop @{$self->{open_elements}};
2170    
2171                unless ($self->{confident}) {                unless ($self->{confident}) {
2172                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
2173                    !!!cp ('t106');                    !!!cp ('t106');
2174                      ## NOTE: Whether the encoding is supported or not is handled
2175                      ## in the {change_encoding} callback.
2176                    $self->{change_encoding}                    $self->{change_encoding}
2177                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
2178                             $token);
2179                                        
2180                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2181                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
2182                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
2183                                                 ->{has_reference});                                                 ->{has_reference});
2184                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
2185                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
2186                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
2187                            [\x09-\x0D\x20]*=                            [\x09\x0A\x0C\x0D\x20]*=
2188                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2189                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09\x0A\x0C\x0D\x20]
2190                               [^\x09\x0A\x0C\x0D\x20\x3B]*))/x) {
2191                      !!!cp ('t107');                      !!!cp ('t107');
2192                        ## NOTE: Whether the encoding is supported or not is handled
2193                        ## in the {change_encoding} callback.
2194                      $self->{change_encoding}                      $self->{change_encoding}
2195                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
2196                               $token);
2197                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2198                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
2199                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3514  sub _tree_construction_main ($) { Line 2219  sub _tree_construction_main ($) {
2219                  }                  }
2220                }                }
2221    
2222                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
2223                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2224                  !!!ack ('t110.1');
2225                !!!next-token;                !!!next-token;
2226                redo B;                next B;
2227              } elsif ($token->{tag_name} eq 'title') {          } elsif ($token->{tag_name} eq 'title') {
2228                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {            if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2229                  !!!cp ('t111');              !!!cp ('t111');
2230                  ## As if </noscript>              ## As if </noscript>
2231                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
2232                  !!!parse-error (type => 'in noscript:title');              !!!parse-error (type => 'in noscript', text => 'title',
2233                                              token => $token);
2234                  $self->{insertion_mode} = IN_HEAD_IM;            
2235                  ## Reprocess in the "in head" insertion mode...              $self->{insertion_mode} = IN_HEAD_IM;
2236                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              ## Reprocess in the "in head" insertion mode...
2237                  !!!cp ('t112');            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2238                  !!!parse-error (type => 'after head:'.$token->{tag_name});              !!!cp ('t112');
2239                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!parse-error (type => 'after head',
2240                } else {                              text => $token->{tag_name}, token => $token);
2241                  !!!cp ('t113');              push @{$self->{open_elements}},
2242                }                  [$self->{head_element}, $el_category->{head}];
2243                $self->{head_element_inserted} = 1;
2244              } else {
2245                !!!cp ('t113');
2246              }
2247    
2248                ## NOTE: There is a "as if in head" code clone.            ## NOTE: There is a "as if in head" code clone.
2249                my $parent = defined $self->{head_element} ? $self->{head_element}            $parse_rcdata->(RCDATA_CONTENT_MODEL);
2250                    : $self->{open_elements}->[-1]->[0];            ## ISSUE: A spec bug [Bug 6038]
2251                $parse_rcdata->(RCDATA_CONTENT_MODEL,            splice @{$self->{open_elements}}, -2, 1, () # <head>
2252                                sub { $parent->append_child ($_[0]) });                if ($self->{insertion_mode} & IM_MASK) == AFTER_HEAD_IM;
2253                pop @{$self->{open_elements}}            next B;
2254                    if $self->{insertion_mode} == AFTER_HEAD_IM;          } elsif ($token->{tag_name} eq 'style' or
2255                redo B;                   $token->{tag_name} eq 'noframes') {
2256              } elsif ($token->{tag_name} eq 'style') {            ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2257                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and            ## insertion mode IN_HEAD_IM)
2258                ## insertion mode IN_HEAD_IM)            ## NOTE: There is a "as if in head" code clone.
2259                ## NOTE: There is a "as if in head" code clone.            if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2260                if ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!cp ('t114');
2261                  !!!cp ('t114');              !!!parse-error (type => 'after head',
2262                  !!!parse-error (type => 'after head:'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
2263                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              push @{$self->{open_elements}},
2264                } else {                  [$self->{head_element}, $el_category->{head}];
2265                  !!!cp ('t115');              $self->{head_element_inserted} = 1;
2266                }            } else {
2267                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);              !!!cp ('t115');
2268                pop @{$self->{open_elements}}            }
2269                    if $self->{insertion_mode} == AFTER_HEAD_IM;            $parse_rcdata->(CDATA_CONTENT_MODEL);
2270                redo B;            ## ISSUE: A spec bug [Bug 6038]
2271              } elsif ($token->{tag_name} eq 'noscript') {            splice @{$self->{open_elements}}, -2, 1, () # <head>
2272                  if ($self->{insertion_mode} & IM_MASK) == AFTER_HEAD_IM;
2273              next B;
2274            } elsif ($token->{tag_name} eq 'noscript') {
2275                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
2276                  !!!cp ('t116');                  !!!cp ('t116');
2277                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
2278                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2279                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2280                    !!!nack ('t116.1');
2281                  !!!next-token;                  !!!next-token;
2282                  redo B;                  next B;
2283                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2284                  !!!cp ('t117');                  !!!cp ('t117');
2285                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript', text => 'noscript',
2286                                    token => $token);
2287                  ## Ignore the token                  ## Ignore the token
2288                    !!!nack ('t117.1');
2289                  !!!next-token;                  !!!next-token;
2290                  redo B;                  next B;
2291                } else {                } else {
2292                  !!!cp ('t118');                  !!!cp ('t118');
2293                  #                  #
2294                }                }
2295              } elsif ($token->{tag_name} eq 'script') {          } elsif ($token->{tag_name} eq 'script') {
2296                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {            if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2297                  !!!cp ('t119');              !!!cp ('t119');
2298                  ## As if </noscript>              ## As if </noscript>
2299                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
2300                  !!!parse-error (type => 'in noscript:script');              !!!parse-error (type => 'in noscript', text => 'script',
2301                                              token => $token);
2302                  $self->{insertion_mode} = IN_HEAD_IM;            
2303                  ## Reprocess in the "in head" insertion mode...              $self->{insertion_mode} = IN_HEAD_IM;
2304                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              ## Reprocess in the "in head" insertion mode...
2305                  !!!cp ('t120');            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2306                  !!!parse-error (type => 'after head:'.$token->{tag_name});              !!!cp ('t120');
2307                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!parse-error (type => 'after head',
2308                } else {                              text => $token->{tag_name}, token => $token);
2309                  !!!cp ('t121');              push @{$self->{open_elements}},
2310                }                  [$self->{head_element}, $el_category->{head}];
2311                $self->{head_element_inserted} = 1;
2312              } else {
2313                !!!cp ('t121');
2314              }
2315    
2316                ## NOTE: There is a "as if in head" code clone.            ## NOTE: There is a "as if in head" code clone.
2317                $script_start_tag->($insert_to_current);            $script_start_tag->();
2318                pop @{$self->{open_elements}}            ## ISSUE: A spec bug  [Bug 6038]
2319                    if $self->{insertion_mode} == AFTER_HEAD_IM;            splice @{$self->{open_elements}}, -2, 1 # <head>
2320                redo B;                if ($self->{insertion_mode} & IM_MASK) == AFTER_HEAD_IM;
2321              } elsif ($token->{tag_name} eq 'body' or            next B;
2322                       $token->{tag_name} eq 'frameset') {          } elsif ($token->{tag_name} eq 'body' or
2323                     $token->{tag_name} eq 'frameset') {
2324                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2325                  !!!cp ('t122');                  !!!cp ('t122');
2326                  ## As if </noscript>                  ## As if </noscript>
2327                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2328                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
2329                                    text => $token->{tag_name}, token => $token);
2330                                    
2331                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2332                  ## As if </head>                  ## As if </head>
# Line 3621  sub _tree_construction_main ($) { Line 2343  sub _tree_construction_main ($) {
2343                }                }
2344    
2345                ## "after head" insertion mode                ## "after head" insertion mode
2346                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2347                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
2348                  !!!cp ('t126');                  !!!cp ('t126');
2349                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3631  sub _tree_construction_main ($) { Line 2353  sub _tree_construction_main ($) {
2353                } else {                } else {
2354                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
2355                }                }
2356                  !!!nack ('t127.1');
2357                !!!next-token;                !!!next-token;
2358                redo B;                next B;
2359              } else {              } else {
2360                !!!cp ('t128');                !!!cp ('t128');
2361                #                #
# Line 3642  sub _tree_construction_main ($) { Line 2365  sub _tree_construction_main ($) {
2365                !!!cp ('t129');                !!!cp ('t129');
2366                ## As if </noscript>                ## As if </noscript>
2367                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2368                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
2369                                  text => $token->{tag_name}, token => $token);
2370                                
2371                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
2372                ## As if </head>                ## As if </head>
# Line 3661  sub _tree_construction_main ($) { Line 2385  sub _tree_construction_main ($) {
2385    
2386              ## "after head" insertion mode              ## "after head" insertion mode
2387              ## As if <body>              ## As if <body>
2388              !!!insert-element ('body');              !!!insert-element ('body',, $token);
2389              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
2390              ## reprocess              ## reprocess
2391              redo B;              !!!ack-later;
2392                next B;
2393            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
2394              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2395                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2396                  !!!cp ('t132');                  !!!cp ('t132');
2397                  ## As if <head>                  ## As if <head>
2398                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2399                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2400                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
2401                        [$self->{head_element}, $el_category->{head}];
2402    
2403                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2404                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2405                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
2406                  !!!next-token;                  !!!next-token;
2407                  redo B;                  next B;
2408                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2409                  !!!cp ('t133');                  !!!cp ('t133');
2410                  ## As if </noscript>                  ## As if </noscript>
2411                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2412                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/',
2413                                    text => 'head', token => $token);
2414                                    
2415                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2416                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2417                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
2418                  !!!next-token;                  !!!next-token;
2419                  redo B;                  next B;
2420                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2421                  !!!cp ('t134');                  !!!cp ('t134');
2422                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2423                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
2424                  !!!next-token;                  !!!next-token;
2425                  redo B;                  next B;
2426                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2427                    !!!cp ('t134.1');
2428                    !!!parse-error (type => 'unmatched end tag', text => 'head',
2429                                    token => $token);
2430                    ## Ignore the token
2431                    !!!next-token;
2432                    next B;
2433                } else {                } else {
2434                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
2435                }                }
2436              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
2437                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3706  sub _tree_construction_main ($) { Line 2439  sub _tree_construction_main ($) {
2439                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2440                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
2441                  !!!next-token;                  !!!next-token;
2442                  redo B;                  next B;
2443                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
2444                           $self->{insertion_mode} == AFTER_HEAD_IM) {
2445                  !!!cp ('t137');                  !!!cp ('t137');
2446                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag',
2447                                    text => 'noscript', token => $token);
2448                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
2449                  !!!next-token;                  !!!next-token;
2450                  redo B;                  next B;
2451                } else {                } else {
2452                  !!!cp ('t138');                  !!!cp ('t138');
2453                  #                  #
# Line 3720  sub _tree_construction_main ($) { Line 2455  sub _tree_construction_main ($) {
2455              } elsif ({              } elsif ({
2456                        body => 1, html => 1,                        body => 1, html => 1,
2457                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2458                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                ## TODO: This branch is entirely redundant.
2459                  !!!cp ('t139');                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
2460                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_IM or
2461                  !!!create-element ($self->{head_element}, 'head');                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
                 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
   
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {  
2462                  !!!cp ('t140');                  !!!cp ('t140');
2463                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
2464                                    text => $token->{tag_name}, token => $token);
2465                    ## Ignore the token
2466                    !!!next-token;
2467                    next B;
2468                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2469                    !!!cp ('t140.1');
2470                    !!!parse-error (type => 'unmatched end tag',
2471                                    text => $token->{tag_name}, token => $token);
2472                  ## Ignore the token                  ## Ignore the token
2473                  !!!next-token;                  !!!next-token;
2474                  redo B;                  next B;
2475                } else {                } else {
2476                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
2477                }                }
2478                              } elsif ($token->{tag_name} eq 'p') {
2479                #                !!!cp ('t142');
2480              } elsif ({                !!!parse-error (type => 'unmatched end tag',
2481                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
2482                       }->{$token->{tag_name}}) {                ## Ignore the token
2483                  !!!next-token;
2484                  next B;
2485                } elsif ($token->{tag_name} eq 'br') {
2486                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2487                  !!!cp ('t142');                  !!!cp ('t142.2');
2488                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
2489                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2490                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2491                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
2492      
2493                    ## Reprocess in the "after head" insertion mode...
2494                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2495                    !!!cp ('t143.2');
2496                    ## As if </head>
2497                    pop @{$self->{open_elements}};
2498                    $self->{insertion_mode} = AFTER_HEAD_IM;
2499      
2500                    ## Reprocess in the "after head" insertion mode...
2501                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2502                    !!!cp ('t143.3');
2503                    ## ISSUE: Two parse errors for <head><noscript></br>
2504                    !!!parse-error (type => 'unmatched end tag',
2505                                    text => 'br', token => $token);
2506                    ## As if </noscript>
2507                    pop @{$self->{open_elements}};
2508                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
2509    
2510                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2511                } else {                  ## As if </head>
2512                  !!!cp ('t143');                  pop @{$self->{open_elements}};
2513                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
2514    
2515                #                  ## Reprocess in the "after head" insertion mode...
2516              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2517                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
2518                  #                  #
2519                } else {                } else {
2520                  !!!cp ('t145');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
2521                }                }
2522    
2523                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
2524                  !!!parse-error (type => 'unmatched end tag',
2525                                  text => 'br', token => $token);
2526                  ## Ignore the token
2527                  !!!next-token;
2528                  next B;
2529                } else {
2530                  !!!cp ('t145');
2531                  !!!parse-error (type => 'unmatched end tag',
2532                                  text => $token->{tag_name}, token => $token);
2533                  ## Ignore the token
2534                  !!!next-token;
2535                  next B;
2536              }              }
2537    
2538              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2539                !!!cp ('t146');                !!!cp ('t146');
2540                ## As if </noscript>                ## As if </noscript>
2541                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2542                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
2543                                  text => $token->{tag_name}, token => $token);
2544                                
2545                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
2546                ## As if </head>                ## As if </head>
# Line 3790  sub _tree_construction_main ($) { Line 2556  sub _tree_construction_main ($) {
2556              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2557  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
2558                !!!cp ('t148');                !!!cp ('t148');
2559                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
2560                                  text => $token->{tag_name}, token => $token);
2561                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
2562                !!!next-token;                !!!next-token;
2563                redo B;                next B;
2564              } else {              } else {
2565                !!!cp ('t149');                !!!cp ('t149');
2566              }              }
2567    
2568              ## "after head" insertion mode              ## "after head" insertion mode
2569              ## As if <body>              ## As if <body>
2570              !!!insert-element ('body');              !!!insert-element ('body',, $token);
2571              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
2572              ## reprocess              ## reprocess
2573              redo B;              next B;
2574            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2575              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2576            }            !!!cp ('t149.1');
2577    
2578              ## NOTE: As if <head>
2579              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2580              $self->{open_elements}->[-1]->[0]->append_child
2581                  ($self->{head_element});
2582              #push @{$self->{open_elements}},
2583              #    [$self->{head_element}, $el_category->{head}];
2584              #$self->{insertion_mode} = IN_HEAD_IM;
2585              ## NOTE: Reprocess.
2586    
2587              ## NOTE: As if </head>
2588              #pop @{$self->{open_elements}};
2589              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
2590              ## NOTE: Reprocess.
2591              
2592              #
2593            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2594              !!!cp ('t149.2');
2595    
2596              ## NOTE: As if </head>
2597              pop @{$self->{open_elements}};
2598              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
2599              ## NOTE: Reprocess.
2600    
2601              #
2602            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2603              !!!cp ('t149.3');
2604    
2605              !!!parse-error (type => 'in noscript:#eof', token => $token);
2606    
2607              ## As if </noscript>
2608              pop @{$self->{open_elements}};
2609              #$self->{insertion_mode} = IN_HEAD_IM;
2610              ## NOTE: Reprocess.
2611    
2612              ## NOTE: As if </head>
2613              pop @{$self->{open_elements}};
2614              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
2615              ## NOTE: Reprocess.
2616    
2617            ## ISSUE: An issue in the spec.            #
2618            } else {
2619              !!!cp ('t149.4');
2620              #
2621            }
2622    
2623            ## NOTE: As if <body>
2624            !!!insert-element ('body',, $token);
2625            $self->{insertion_mode} = IN_BODY_IM;
2626            ## NOTE: Reprocess.
2627            next B;
2628          } else {
2629            die "$0: $token->{type}: Unknown token type";
2630          }
2631      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
2632            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
2633              !!!cp ('t150');              !!!cp ('t150');
# Line 3818  sub _tree_construction_main ($) { Line 2637  sub _tree_construction_main ($) {
2637              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
2638    
2639              !!!next-token;              !!!next-token;
2640              redo B;              next B;
2641            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
2642              if ({              if ({
2643                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
2644                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
2645                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
2646                if ($self->{insertion_mode} == IN_CELL_IM) {                if (($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2647                  ## have an element in table scope                  ## have an element in table scope
2648                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
2649                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
2650                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] == TABLE_CELL_EL) {
2651                      !!!cp ('t151');                      !!!cp ('t151');
2652                      $tn = $node->[1];  
2653                      last INSCOPE;                      ## Close the cell
2654                    } elsif ({                      !!!back-token; # <x>
2655                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
2656                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
2657                                  line => $token->{line},
2658                                  column => $token->{column}};
2659                        next B;
2660                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
2661                      !!!cp ('t152');                      !!!cp ('t152');
2662                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
2663                    }                      last;
                 } # INSCOPE  
                   unless (defined $tn) {  
                     !!!cp ('t153');  
 ## TODO: This error type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
2664                    }                    }
2665                    }
2666    
2667                    !!!cp ('t153');
2668                    !!!parse-error (type => 'start tag not allowed',
2669                        text => $token->{tag_name}, token => $token);
2670                    ## Ignore the token
2671                    !!!nack ('t153.1');
2672                    !!!next-token;
2673                    next B;
2674                  } elsif (($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2675                    !!!parse-error (type => 'not closed', text => 'caption',
2676                                    token => $token);
2677                                    
2678                  !!!cp ('t154');                  ## NOTE: As if </caption>.
                 ## Close the cell  
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
                 redo B;  
               } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {  
                 !!!parse-error (type => 'not closed:caption');  
                   
                 ## As if </caption>  
2679                  ## have a table element in table scope                  ## have a table element in table scope
2680                  my $i;                  my $i;
2681                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
2682                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
2683                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
2684                      !!!cp ('t155');                      if ($node->[1] == CAPTION_EL) {
2685                      $i = $_;                        !!!cp ('t155');
2686                      last INSCOPE;                        $i = $_;
2687                    } elsif ({                        last INSCOPE;
2688                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
2689                             }->{$node->[1]}) {                        !!!cp ('t156');
2690                      !!!cp ('t156');                        last;
2691                      last INSCOPE;                      }
2692                    }                    }
2693    
2694                      !!!cp ('t157');
2695                      !!!parse-error (type => 'start tag not allowed',
2696                                      text => $token->{tag_name}, token => $token);
2697                      ## Ignore the token
2698                      !!!nack ('t157.1');
2699                      !!!next-token;
2700                      next B;
2701                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
 ## TODO: this type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
2702                                    
2703                  ## generate implied end tags                  ## generate implied end tags
2704                  while ({                  while ($self->{open_elements}->[-1]->[1]
2705                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
2706                    !!!cp ('t158');                    !!!cp ('t158');
2707                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
2708                  }                  }
2709    
2710                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] == CAPTION_EL) {
2711                    !!!cp ('t159');                    !!!cp ('t159');
2712                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
2713                                      text => $self->{open_elements}->[-1]->[0]
2714                                          ->manakai_local_name,
2715                                      token => $token);
2716                  } else {                  } else {
2717                    !!!cp ('t160');                    !!!cp ('t160');
2718                  }                  }
# Line 3904  sub _tree_construction_main ($) { Line 2724  sub _tree_construction_main ($) {
2724                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
2725                                    
2726                  ## reprocess                  ## reprocess
2727                  redo B;                  !!!ack-later;
2728                    next B;
2729                } else {                } else {
2730                  !!!cp ('t161');                  !!!cp ('t161');
2731                  #                  #
# Line 3915  sub _tree_construction_main ($) { Line 2736  sub _tree_construction_main ($) {
2736              }              }
2737            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
2738              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
2739                if ($self->{insertion_mode} == IN_CELL_IM) {                if (($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2740                  ## have an element in table scope                  ## have an element in table scope
2741                  my $i;                  my $i;
2742                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2743                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
2744                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
2745                      !!!cp ('t163');                      !!!cp ('t163');
2746                      $i = $_;                      $i = $_;
2747                      last INSCOPE;                      last INSCOPE;
2748                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
2749                      !!!cp ('t164');                      !!!cp ('t164');
2750                      last INSCOPE;                      last INSCOPE;
2751                    }                    }
2752                  } # INSCOPE                  } # INSCOPE
2753                    unless (defined $i) {                    unless (defined $i) {
2754                      !!!cp ('t165');                      !!!cp ('t165');
2755                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
2756                                        text => $token->{tag_name},
2757                                        token => $token);
2758                      ## Ignore the token                      ## Ignore the token
2759                      !!!next-token;                      !!!next-token;
2760                      redo B;                      next B;
2761                    }                    }
2762                                    
2763                  ## generate implied end tags                  ## generate implied end tags
2764                  while ({                  while ($self->{open_elements}->[-1]->[1]
2765                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
2766                    !!!cp ('t166');                    !!!cp ('t166');
2767                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
2768                  }                  }
2769    
2770                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
2771                            ne $token->{tag_name}) {
2772                    !!!cp ('t167');                    !!!cp ('t167');
2773                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
2774                                      text => $self->{open_elements}->[-1]->[0]
2775                                          ->manakai_local_name,
2776                                      token => $token);
2777                  } else {                  } else {
2778                    !!!cp ('t168');                    !!!cp ('t168');
2779                  }                  }
# Line 3961  sub _tree_construction_main ($) { Line 2785  sub _tree_construction_main ($) {
2785                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
2786                                    
2787                  !!!next-token;                  !!!next-token;
2788                  redo B;                  next B;
2789                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif (($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2790                  !!!cp ('t169');                  !!!cp ('t169');
2791                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
2792                                    text => $token->{tag_name}, token => $token);
2793                  ## Ignore the token                  ## Ignore the token
2794                  !!!next-token;                  !!!next-token;
2795                  redo B;                  next B;
2796                } else {                } else {
2797                  !!!cp ('t170');                  !!!cp ('t170');
2798                  #                  #
2799                }                }
2800              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
2801                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if (($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2802                  ## have a table element in table scope                  ## have a table element in table scope
2803                  my $i;                  my $i;
2804                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
2805                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
2806                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
2807                      !!!cp ('t171');                      if ($node->[1] == CAPTION_EL) {
2808                      $i = $_;                        !!!cp ('t171');
2809                      last INSCOPE;                        $i = $_;
2810                    } elsif ({                        last INSCOPE;
2811                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
2812                             }->{$node->[1]}) {                        !!!cp ('t172');
2813                      !!!cp ('t172');                        last;
2814                      last INSCOPE;                      }
2815                    }                    }
2816    
2817                      !!!cp ('t173');
2818                      !!!parse-error (type => 'unmatched end tag',
2819                                      text => $token->{tag_name}, token => $token);
2820                      ## Ignore the token
2821                      !!!next-token;
2822                      next B;
2823                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
2824                                    
2825                  ## generate implied end tags                  ## generate implied end tags
2826                  while ({                  while ($self->{open_elements}->[-1]->[1]
2827                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
2828                    !!!cp ('t174');                    !!!cp ('t174');
2829                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
2830                  }                  }
2831                                    
2832                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] == CAPTION_EL) {
2833                    !!!cp ('t175');                    !!!cp ('t175');
2834                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
2835                                      text => $self->{open_elements}->[-1]->[0]
2836                                          ->manakai_local_name,
2837                                      token => $token);
2838                  } else {                  } else {
2839                    !!!cp ('t176');                    !!!cp ('t176');
2840                  }                  }
# Line 4019  sub _tree_construction_main ($) { Line 2846  sub _tree_construction_main ($) {
2846                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
2847                                    
2848                  !!!next-token;                  !!!next-token;
2849                  redo B;                  next B;
2850                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif (($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2851                  !!!cp ('t177');                  !!!cp ('t177');
2852                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
2853                                    text => $token->{tag_name}, token => $token);
2854                  ## Ignore the token                  ## Ignore the token
2855                  !!!next-token;                  !!!next-token;
2856                  redo B;                  next B;
2857                } else {                } else {
2858                  !!!cp ('t178');                  !!!cp ('t178');
2859                  #                  #
# Line 4034  sub _tree_construction_main ($) { Line 2862  sub _tree_construction_main ($) {
2862                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
2863                        thead => 1, tr => 1,                        thead => 1, tr => 1,
2864                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
2865                       $self->{insertion_mode} == IN_CELL_IM) {                       ($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2866                ## have an element in table scope                ## have an element in table scope
2867                my $i;                my $i;
2868                my $tn;                my $tn;
2869                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
2870                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
2871                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
2872                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
2873                    $i = $_;                      !!!cp ('t179');
2874                    last INSCOPE;                      $i = $_;
2875                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
2876                    !!!cp ('t180');                      ## Close the cell
2877                    $tn = $node->[1];                      !!!back-token; # </x>
2878                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
2879                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
2880                  } elsif ({                                column => $token->{column}};
2881                            table => 1, html => 1,                      next B;
2882                           }->{$node->[1]}) {                    } elsif ($node->[1] == TABLE_CELL_EL) {
2883                    !!!cp ('t181');                      !!!cp ('t180');
2884                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
2885                        ## NOTE: There is exactly one |td| or |th| element
2886                        ## in scope in the stack of open elements by definition.
2887                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
2888                        ## ISSUE: Can this be reached?
2889                        !!!cp ('t181');
2890                        last;
2891                      }
2892                  }                  }
2893                } # INSCOPE  
               unless (defined $i) {  
2894                  !!!cp ('t182');                  !!!cp ('t182');
2895                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
2896                        text => $token->{tag_name}, token => $token);
2897                  ## Ignore the token                  ## Ignore the token
2898                  !!!next-token;                  !!!next-token;
2899                  redo B;                  next B;
2900                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
2901              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
2902                       $self->{insertion_mode} == IN_CAPTION_IM) {                       ($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2903                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
2904                                  token => $token);
2905    
2906                ## As if </caption>                ## As if </caption>
2907                ## have a table element in table scope                ## have a table element in table scope
2908                my $i;                my $i;
2909                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2910                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
2911                  if ($node->[1] eq 'caption') {                  if ($node->[1] == CAPTION_EL) {
2912                    !!!cp ('t184');                    !!!cp ('t184');
2913                    $i = $_;                    $i = $_;
2914                    last INSCOPE;                    last INSCOPE;
2915                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
2916                    !!!cp ('t185');                    !!!cp ('t185');
2917                    last INSCOPE;                    last INSCOPE;
2918                  }                  }
2919                } # INSCOPE                } # INSCOPE
2920                unless (defined $i) {                unless (defined $i) {
2921                  !!!cp ('t186');                  !!!cp ('t186');
2922                  !!!parse-error (type => 'unmatched end tag:caption');          ## TODO: Wrong error type?
2923                    !!!parse-error (type => 'unmatched end tag',
2924                                    text => 'caption', token => $token);
2925                  ## Ignore the token                  ## Ignore the token
2926                  !!!next-token;                  !!!next-token;
2927                  redo B;                  next B;
2928                }                }
2929                                
2930                ## generate implied end tags                ## generate implied end tags
2931                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
2932                  !!!cp ('t187');                  !!!cp ('t187');
2933                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2934                }                }
2935    
2936                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] == CAPTION_EL) {
2937                  !!!cp ('t188');                  !!!cp ('t188');
2938                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
2939                                    text => $self->{open_elements}->[-1]->[0]
2940                                        ->manakai_local_name,
2941                                    token => $token);
2942                } else {                } else {
2943                  !!!cp ('t189');                  !!!cp ('t189');
2944                }                }
# Line 4120  sub _tree_construction_main ($) { Line 2950  sub _tree_construction_main ($) {
2950                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
2951    
2952                ## reprocess                ## reprocess
2953                redo B;                next B;
2954              } elsif ({              } elsif ({
2955                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
2956                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2957                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
2958                  !!!cp ('t190');                  !!!cp ('t190');
2959                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
2960                                    text => $token->{tag_name}, token => $token);
2961                  ## Ignore the token                  ## Ignore the token
2962                  !!!next-token;                  !!!next-token;
2963                  redo B;                  next B;
2964                } else {                } else {
2965                  !!!cp ('t191');                  !!!cp ('t191');
2966                  #                  #
2967                }                }
2968              } elsif ({          } elsif ({
2969                        tbody => 1, tfoot => 1,                    tbody => 1, tfoot => 1,
2970                        thead => 1, tr => 1,                    thead => 1, tr => 1,
2971                       }->{$token->{tag_name}} and                   }->{$token->{tag_name}} and
2972                       $self->{insertion_mode} == IN_CAPTION_IM) {                   ($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2973                !!!cp ('t192');            !!!cp ('t192');
2974                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
2975                ## Ignore the token                            text => $token->{tag_name}, token => $token);
2976                !!!next-token;            ## Ignore the token
2977                redo B;            !!!next-token;
2978              } else {            next B;
2979                !!!cp ('t193');          } else {
2980                #            !!!cp ('t193');
2981              }            #
2982            }
2983          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2984            for my $entry (@{$self->{open_elements}}) {
2985              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
2986                !!!cp ('t75');
2987                !!!parse-error (type => 'in body:#eof', token => $token);
2988                last;
2989              }
2990            }
2991    
2992            ## Stop parsing.
2993            last B;
2994        } else {        } else {
2995          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2996        }        }
# Line 4156  sub _tree_construction_main ($) { Line 2999  sub _tree_construction_main ($) {
2999        #        #
3000      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3001        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3002              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
3003                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3004              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3005                                
3006                unless (length $token->{data}) {            unless (length $token->{data}) {
3007                  !!!cp ('t194');              !!!cp ('t194');
3008                  !!!next-token;              !!!next-token;
3009                  redo B;              next B;
3010                } else {            } else {
3011                  !!!cp ('t195');              !!!cp ('t195');
3012                }            }
3013              }          }
3014    
3015              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
3016    
3017              ## As if in body, but insert into foster parent element          ## NOTE: As if in body, but insert into the foster parent element.
3018              ## ISSUE: Spec says that "whenever a node would be inserted          $reconstruct_active_formatting_elements->($insert_to_foster);
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
3019                            
3020              if ({          if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3021                   table => 1, tbody => 1, tfoot => 1,            # MUST
3022                   thead => 1, tr => 1,            my $foster_parent_element;
3023                  }->{$self->{open_elements}->[-1]->[1]}) {            my $next_sibling;
3024                # MUST            my $prev_sibling;
3025                my $foster_parent_element;            OE: for (reverse 0..$#{$self->{open_elements}}) {
3026                my $next_sibling;              if ($self->{open_elements}->[$_]->[1] == TABLE_EL) {
3027                my $prev_sibling;                my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3028                OE: for (reverse 0..$#{$self->{open_elements}}) {                if (defined $parent and $parent->node_type == 1) {
3029                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $foster_parent_element = $parent;
3030                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  !!!cp ('t196');
3031                    if (defined $parent and $parent->node_type == 1) {                  $next_sibling = $self->{open_elements}->[$_]->[0];
3032                      !!!cp ('t196');                  $prev_sibling = $next_sibling->previous_sibling;
3033                      $foster_parent_element = $parent;                  #
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     !!!cp ('t197');  
                     $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) {  
                 !!!cp ('t198');  
                 $prev_sibling->manakai_append_text ($token->{data});  
3034                } else {                } else {
3035                  !!!cp ('t199');                  !!!cp ('t197');
3036                  $foster_parent_element->insert_before                  $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3037                    ($self->{document}->create_text_node ($token->{data}),                  $prev_sibling = $foster_parent_element->last_child;
3038                     $next_sibling);                  #
3039                }                }
3040              } else {                last OE;
               !!!cp ('t200');  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
3041              }              }
3042              } # OE
3043              $foster_parent_element = $self->{open_elements}->[0]->[0] and
3044              $prev_sibling = $foster_parent_element->last_child
3045                  unless defined $foster_parent_element;
3046              undef $prev_sibling unless $open_tables->[-1]->[2]; # ~node inserted
3047              if (defined $prev_sibling and
3048                  $prev_sibling->node_type == 3) {
3049                !!!cp ('t198');
3050                $prev_sibling->manakai_append_text ($token->{data});
3051              } else {
3052                !!!cp ('t199');
3053                $foster_parent_element->insert_before
3054                    ($self->{document}->create_text_node ($token->{data}),
3055                     $next_sibling);
3056              }
3057              $open_tables->[-1]->[1] = 1; # tainted
3058              $open_tables->[-1]->[2] = 1; # ~node inserted
3059            } else {
3060              ## NOTE: Fragment case or in a foster parent'ed element
3061              ## (e.g. |<table><span>a|).  In fragment case, whether the
3062              ## character is appended to existing node or a new node is
3063              ## created is irrelevant, since the foster parent'ed nodes
3064              ## are discarded and fragment parsing does not invoke any
3065              ## script.
3066              !!!cp ('t200');
3067              $self->{open_elements}->[-1]->[0]->manakai_append_text
3068                  ($token->{data});
3069            }
3070                            
3071              !!!next-token;          !!!next-token;
3072              redo B;          next B;
3073        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3074              if ({          if ({
3075                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => (($self->{insertion_mode} & IM_MASK) != IN_ROW_IM),
3076                   th => 1, td => 1,               th => 1, td => 1,
3077                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
3078                if ($self->{insertion_mode} == IN_TABLE_IM) {            if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_IM) {
3079                  ## Clear back to table context              ## Clear back to table context
3080                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
3081                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
3082                    !!!cp ('t201');                !!!cp ('t201');
3083                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                pop @{$self->{open_elements}};
3084                    pop @{$self->{open_elements}};              }
3085                  }              
3086                                !!!insert-element ('tbody',, $token);
3087                  !!!insert-element ('tbody');              $self->{insertion_mode} = IN_TABLE_BODY_IM;
3088                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              ## reprocess in the "in table body" insertion mode...
3089                  ## reprocess in the "in table body" insertion mode...            }
3090                }            
3091              if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_BODY_IM) {
3092                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {              unless ($token->{tag_name} eq 'tr') {
3093                  unless ($token->{tag_name} eq 'tr') {                !!!cp ('t202');
3094                    !!!cp ('t202');                !!!parse-error (type => 'missing start tag:tr', token => $token);
3095                    !!!parse-error (type => 'missing start tag:tr');              }
                 }  
3096                                    
3097                  ## Clear back to table body context              ## Clear back to table body context
3098                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
3099                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
3100                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
3101                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
3102                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
3103                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              }
                   pop @{$self->{open_elements}};  
                 }  
3104                                    
3105                  $self->{insertion_mode} = IN_ROW_IM;              $self->{insertion_mode} = IN_ROW_IM;
3106                  if ($token->{tag_name} eq 'tr') {              if ($token->{tag_name} eq 'tr') {
3107                    !!!cp ('t204');                !!!cp ('t204');
3108                    !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3109                    !!!next-token;                $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3110                    redo B;                !!!nack ('t204');
3111                  } else {                !!!next-token;
3112                    !!!cp ('t205');                next B;
3113                    !!!insert-element ('tr');              } else {
3114                    ## reprocess in the "in row" insertion mode                !!!cp ('t205');
3115                  }                !!!insert-element ('tr',, $token);
3116                } else {                ## reprocess in the "in row" insertion mode
3117                  !!!cp ('t206');              }
3118                }            } else {
3119                !!!cp ('t206');
3120              }
3121    
3122                ## Clear back to table row context                ## Clear back to table row context
3123                while (not {                while (not ($self->{open_elements}->[-1]->[1]
3124                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
3125                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3126                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3127                }                }
3128                                
3129                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3130                $self->{insertion_mode} = IN_CELL_IM;            $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3131              $self->{insertion_mode} = IN_CELL_IM;
3132    
3133                push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
3134                                
3135              !!!nack ('t207.1');
3136              !!!next-token;
3137              next B;
3138            } elsif ({
3139                      caption => 1, col => 1, colgroup => 1,
3140                      tbody => 1, tfoot => 1, thead => 1,
3141                      tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3142                     }->{$token->{tag_name}}) {
3143              if (($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3144                ## As if </tr>
3145                ## have an element in table scope
3146                my $i;
3147                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3148                  my $node = $self->{open_elements}->[$_];
3149                  if ($node->[1] == TABLE_ROW_EL) {
3150                    !!!cp ('t208');
3151                    $i = $_;
3152                    last INSCOPE;
3153                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
3154                    !!!cp ('t209');
3155                    last INSCOPE;
3156                  }
3157                } # INSCOPE
3158                unless (defined $i) {
3159                  !!!cp ('t210');
3160                  ## TODO: This type is wrong.
3161                  !!!parse-error (type => 'unmacthed end tag',
3162                                  text => $token->{tag_name}, token => $token);
3163                  ## Ignore the token
3164                  !!!nack ('t210.1');
3165                !!!next-token;                !!!next-token;
3166                redo B;                next B;
3167              } elsif ({              }
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1,  
                       tr => 1, # $self->{insertion_mode} == IN_ROW_IM  
                      }->{$token->{tag_name}}) {  
               if ($self->{insertion_mode} == IN_ROW_IM) {  
                 ## As if </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 'tr') {  
                     !!!cp ('t208');  
                     $i = $_;  
                     last INSCOPE;  
                   } elsif ({  
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
                     !!!cp ('t209');  
                     last INSCOPE;  
                   }  
                 } # INSCOPE  
                 unless (defined $i) {  
                  !!!cp ('t210');  
 ## TODO: This type is wrong.  
                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  
                   ## Ignore the token  
                   !!!next-token;  
                   redo B;  
                 }  
3168                                    
3169                  ## Clear back to table row context                  ## Clear back to table row context
3170                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
3171                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
3172                    !!!cp ('t211');                    !!!cp ('t211');
3173                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3174                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3175                  }                  }
3176                                    
# Line 4335  sub _tree_construction_main ($) { Line 3179  sub _tree_construction_main ($) {
3179                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3180                    !!!cp ('t212');                    !!!cp ('t212');
3181                    ## reprocess                    ## reprocess
3182                    redo B;                    !!!ack-later;
3183                      next B;
3184                  } else {                  } else {
3185                    !!!cp ('t213');                    !!!cp ('t213');
3186                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
3187                  }                  }
3188                }                }
3189    
3190                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_BODY_IM) {
3191                  ## have an element in table scope                  ## have an element in table scope
3192                  my $i;                  my $i;
3193                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3194                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3195                    if ({                    if ($node->[1] == TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
3196                      !!!cp ('t214');                      !!!cp ('t214');
3197                      $i = $_;                      $i = $_;
3198                      last INSCOPE;                      last INSCOPE;
3199                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
3200                      !!!cp ('t215');                      !!!cp ('t215');
3201                      last INSCOPE;                      last INSCOPE;
3202                    }                    }
3203                  } # INSCOPE                  } # INSCOPE
3204                  unless (defined $i) {                  unless (defined $i) {
3205                    !!!cp ('t216');                    !!!cp ('t216');
3206  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
3207                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
3208                                      text => $token->{tag_name}, token => $token);
3209                    ## Ignore the token                    ## Ignore the token
3210                      !!!nack ('t216.1');
3211                    !!!next-token;                    !!!next-token;
3212                    redo B;                    next B;
3213                  }                  }
3214    
3215                  ## Clear back to table body context                  ## Clear back to table body context
3216                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
3217                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
3218                    !!!cp ('t217');                    !!!cp ('t217');
3219                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3220                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3221                  }                  }
3222                                    
# Line 4393  sub _tree_construction_main ($) { Line 3234  sub _tree_construction_main ($) {
3234                  !!!cp ('t218');                  !!!cp ('t218');
3235                }                }
3236    
3237                if ($token->{tag_name} eq 'col') {            if ($token->{tag_name} eq 'col') {
3238                  ## Clear back to table context              ## Clear back to table context
3239                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
3240                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
3241                    !!!cp ('t219');                !!!cp ('t219');
3242                    ## ISSUE: Can this state be reached?                ## ISSUE: Can this state be reached?
3243                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                pop @{$self->{open_elements}};
3244                    pop @{$self->{open_elements}};              }
3245                  }              
3246                                !!!insert-element ('colgroup',, $token);
3247                  !!!insert-element ('colgroup');              $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3248                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;              ## reprocess
3249                  ## reprocess              $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3250                  redo B;              !!!ack-later;
3251                } elsif ({              next B;
3252                          caption => 1,            } elsif ({
3253                          colgroup => 1,                      caption => 1,
3254                          tbody => 1, tfoot => 1, thead => 1,                      colgroup => 1,
3255                         }->{$token->{tag_name}}) {                      tbody => 1, tfoot => 1, thead => 1,
3256                  ## Clear back to table context                     }->{$token->{tag_name}}) {
3257                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              ## Clear back to table context
3258                         $self->{open_elements}->[-1]->[1] ne 'html') {                  while (not ($self->{open_elements}->[-1]->[1]
3259                                    & TABLE_SCOPING_EL)) {
3260                    !!!cp ('t220');                    !!!cp ('t220');
3261                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3262                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3263                  }                  }
3264                                    
3265                  push @$active_formatting_elements, ['#marker', '']              push @$active_formatting_elements, ['#marker', '']
3266                      if $token->{tag_name} eq 'caption';                  if $token->{tag_name} eq 'caption';
3267                                    
3268                  !!!insert-element ($token->{tag_name}, $token->{attributes});              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3269                  $self->{insertion_mode} = {              $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3270                                             caption => IN_CAPTION_IM,              $self->{insertion_mode} = {
3271                                             colgroup => IN_COLUMN_GROUP_IM,                                         caption => IN_CAPTION_IM,
3272                                             tbody => IN_TABLE_BODY_IM,                                         colgroup => IN_COLUMN_GROUP_IM,
3273                                             tfoot => IN_TABLE_BODY_IM,                                         tbody => IN_TABLE_BODY_IM,
3274                                             thead => IN_TABLE_BODY_IM,                                         tfoot => IN_TABLE_BODY_IM,
3275                                            }->{$token->{tag_name}};                                         thead => IN_TABLE_BODY_IM,
3276                  !!!next-token;                                        }->{$token->{tag_name}};
3277                  redo B;              !!!next-token;
3278                } else {              !!!nack ('t220.1');
3279                  die "$0: in table: <>: $token->{tag_name}";              next B;
3280                }            } else {
3281                die "$0: in table: <>: $token->{tag_name}";
3282              }
3283              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3284                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
3285                                  text => $self->{open_elements}->[-1]->[0]
3286                                      ->manakai_local_name,
3287                                  token => $token);
3288    
3289                ## As if </table>                ## As if </table>
3290                ## have a table element in table scope                ## have a table element in table scope
3291                my $i;                my $i;
3292                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3293                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3294                  if ($node->[1] eq 'table') {                  if ($node->[1] == TABLE_EL) {
3295                    !!!cp ('t221');                    !!!cp ('t221');
3296                    $i = $_;                    $i = $_;
3297                    last INSCOPE;                    last INSCOPE;
3298                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
3299                    !!!cp ('t222');                    !!!cp ('t222');
3300                    last INSCOPE;                    last INSCOPE;
3301                  }                  }
# Line 4460  sub _tree_construction_main ($) { Line 3303  sub _tree_construction_main ($) {
3303                unless (defined $i) {                unless (defined $i) {
3304                  !!!cp ('t223');                  !!!cp ('t223');
3305  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
3306                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag', text => 'table',
3307                                    token => $token);
3308                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
3309                    !!!nack ('t223.1');
3310                  !!!next-token;                  !!!next-token;
3311                  redo B;                  next B;
3312                }                }
3313                                
3314    ## TODO: Followings are removed from the latest spec.
3315                ## generate implied end tags                ## generate implied end tags
3316                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
3317                  !!!cp ('t224');                  !!!cp ('t224');
3318                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3319                }                }
3320    
3321                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] == TABLE_EL) {
3322                  !!!cp ('t225');                  !!!cp ('t225');
3323  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
3324                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
3325                                    text => $self->{open_elements}->[-1]->[0]
3326                                        ->manakai_local_name,
3327                                    token => $token);
3328                } else {                } else {
3329                  !!!cp ('t226');                  !!!cp ('t226');
3330                }                }
3331    
3332                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3333                  pop @{$open_tables};
3334    
3335                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3336    
3337                ## reprocess            ## reprocess
3338                redo B;            !!!ack-later;
3339              next B;
3340            } elsif ($token->{tag_name} eq 'style') {
3341              if (not $open_tables->[-1]->[1]) { # tainted
3342                !!!cp ('t227.8');
3343                ## NOTE: This is a "as if in head" code clone.
3344                $parse_rcdata->(CDATA_CONTENT_MODEL);
3345                $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3346                next B;
3347              } else {
3348                !!!cp ('t227.7');
3349                #
3350              }
3351            } elsif ($token->{tag_name} eq 'script') {
3352              if (not $open_tables->[-1]->[1]) { # tainted
3353                !!!cp ('t227.6');
3354                ## NOTE: This is a "as if in head" code clone.
3355                $script_start_tag->();
3356                $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3357                next B;
3358              } else {
3359                !!!cp ('t227.5');
3360                #
3361              }
3362            } elsif ($token->{tag_name} eq 'input') {
3363              if (not $open_tables->[-1]->[1]) { # tainted
3364                if ($token->{attributes}->{type}) { ## TODO: case
3365                  my $type = lc $token->{attributes}->{type}->{value};
3366                  if ($type eq 'hidden') {
3367                    !!!cp ('t227.3');
3368                    !!!parse-error (type => 'in table',
3369                                    text => $token->{tag_name}, token => $token);
3370    
3371                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3372                    $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3373    
3374                    ## TODO: form element pointer
3375    
3376                    pop @{$self->{open_elements}};
3377    
3378                    !!!next-token;
3379                    !!!ack ('t227.2.1');
3380                    next B;
3381                  } else {
3382                    !!!cp ('t227.2');
3383                    #
3384                  }
3385                } else {
3386                  !!!cp ('t227.1');
3387                  #
3388                }
3389              } else {
3390                !!!cp ('t227.4');
3391                #
3392              }
3393          } else {          } else {
3394            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
3395            #            #
3396          }          }
3397    
3398            !!!parse-error (type => 'in table', text => $token->{tag_name},
3399                            token => $token);
3400    
3401            $insert = $insert_to_foster;
3402            #
3403        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
3404              if ($token->{tag_name} eq 'tr' and          if ($token->{tag_name} eq 'tr' and
3405                  $self->{insertion_mode} == IN_ROW_IM) {              ($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3406                ## have an element in table scope            ## have an element in table scope
3407                my $i;                my $i;
3408                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3409                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3410                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] == TABLE_ROW_EL) {
3411                    !!!cp ('t228');                    !!!cp ('t228');
3412                    $i = $_;                    $i = $_;
3413                    last INSCOPE;                    last INSCOPE;
3414                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
3415                    !!!cp ('t229');                    !!!cp ('t229');
3416                    last INSCOPE;                    last INSCOPE;
3417                  }                  }
3418                } # INSCOPE                } # INSCOPE
3419                unless (defined $i) {                unless (defined $i) {
3420                  !!!cp ('t230');                  !!!cp ('t230');
3421                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
3422                                    text => $token->{tag_name}, token => $token);
3423                  ## Ignore the token                  ## Ignore the token
3424                    !!!nack ('t230.1');
3425                  !!!next-token;                  !!!next-token;
3426                  redo B;                  next B;
3427                } else {                } else {
3428                  !!!cp ('t232');                  !!!cp ('t232');
3429                }                }
3430    
3431                ## Clear back to table row context                ## Clear back to table row context
3432                while (not {                while (not ($self->{open_elements}->[-1]->[1]
3433                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
3434                  !!!cp ('t231');                  !!!cp ('t231');
3435  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3436                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3437                }                }
3438    
3439                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
3440                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3441                !!!next-token;                !!!next-token;
3442                redo B;                !!!nack ('t231.1');
3443                  next B;
3444              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3445                if ($self->{insertion_mode} == IN_ROW_IM) {                if (($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3446                  ## As if </tr>                  ## As if </tr>
3447                  ## have an element in table scope                  ## have an element in table scope
3448                  my $i;                  my $i;
3449                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3450                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3451                    if ($node->[1] eq 'tr') {                    if ($node->[1] == TABLE_ROW_EL) {
3452                      !!!cp ('t233');                      !!!cp ('t233');
3453                      $i = $_;                      $i = $_;
3454                      last INSCOPE;                      last INSCOPE;
3455                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
3456                      !!!cp ('t234');                      !!!cp ('t234');
3457                      last INSCOPE;                      last INSCOPE;
3458                    }                    }
# Line 4558  sub _tree_construction_main ($) { Line 3460  sub _tree_construction_main ($) {
3460                  unless (defined $i) {                  unless (defined $i) {
3461                    !!!cp ('t235');                    !!!cp ('t235');
3462  ## TODO: The following is wrong.  ## TODO: The following is wrong.
3463                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag',
3464                                      text => $token->{type}, token => $token);
3465                    ## Ignore the token                    ## Ignore the token
3466                      !!!nack ('t236.1');
3467                    !!!next-token;                    !!!next-token;
3468                    redo B;                    next B;
3469                  }                  }
3470                                    
3471                  ## Clear back to table row context                  ## Clear back to table row context
3472                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
3473                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
3474                    !!!cp ('t236');                    !!!cp ('t236');
3475  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3476                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3477                  }                  }
3478                                    
# Line 4579  sub _tree_construction_main ($) { Line 3481  sub _tree_construction_main ($) {
3481                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3482                }                }
3483    
3484                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_BODY_IM) {
3485                  ## have an element in table scope                  ## have an element in table scope
3486                  my $i;                  my $i;
3487                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3488                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3489                    if ({                    if ($node->[1] == TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
3490                      !!!cp ('t237');                      !!!cp ('t237');
3491                      $i = $_;                      $i = $_;
3492                      last INSCOPE;                      last INSCOPE;
3493                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
3494                      !!!cp ('t238');                      !!!cp ('t238');
3495                      last INSCOPE;                      last INSCOPE;
3496                    }                    }
3497                  } # INSCOPE                  } # INSCOPE
3498                  unless (defined $i) {                  unless (defined $i) {
3499                    !!!cp ('t239');                    !!!cp ('t239');
3500                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
3501                                      text => $token->{tag_name}, token => $token);
3502                    ## Ignore the token                    ## Ignore the token
3503                      !!!nack ('t239.1');
3504                    !!!next-token;                    !!!next-token;
3505                    redo B;                    next B;
3506                  }                  }
3507                                    
3508                  ## Clear back to table body context                  ## Clear back to table body context
3509                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
3510                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
3511                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3512                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3513                  }                  }
3514                                    
# Line 4626  sub _tree_construction_main ($) { Line 3524  sub _tree_construction_main ($) {
3524                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
3525                }                }
3526    
3527                  ## NOTE: </table> in the "in table" insertion mode.
3528                  ## When you edit the code fragment below, please ensure that
3529                  ## the code for <table> in the "in table" insertion mode
3530                  ## is synced with it.
3531    
3532                ## have a table element in table scope                ## have a table element in table scope
3533                my $i;                my $i;
3534                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3535                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3536                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] == TABLE_EL) {
3537                    !!!cp ('t241');                    !!!cp ('t241');
3538                    $i = $_;                    $i = $_;
3539                    last INSCOPE;                    last INSCOPE;
3540                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
3541                    !!!cp ('t242');                    !!!cp ('t242');
3542                    last INSCOPE;                    last INSCOPE;
3543                  }                  }
3544                } # INSCOPE                } # INSCOPE
3545                unless (defined $i) {                unless (defined $i) {
3546                  !!!cp ('t243');                  !!!cp ('t243');
3547                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
3548                                    text => $token->{tag_name}, token => $token);
3549                  ## Ignore the token                  ## Ignore the token
3550                    !!!nack ('t243.1');
3551                  !!!next-token;                  !!!next-token;
3552                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               while ({  
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
 ## ISSUE: Can this case be reached?  
                 pop @{$self->{open_elements}};  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
3553                }                }
3554                                    
3555                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3556                  pop @{$open_tables};
3557                                
3558                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3559                                
3560                !!!next-token;                !!!next-token;
3561                redo B;                next B;
3562              } elsif ({              } elsif ({
3563                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3564                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3565                       $self->{insertion_mode} & ROW_IMS) {                       $self->{insertion_mode} & ROW_IMS) {
3566                if ($self->{insertion_mode} == IN_ROW_IM) {                if (($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3567                  ## have an element in table scope                  ## have an element in table scope
3568                  my $i;                  my $i;
3569                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3570                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3571                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
3572                      !!!cp ('t247');                      !!!cp ('t247');
3573                      $i = $_;                      $i = $_;
3574                      last INSCOPE;                      last INSCOPE;
3575                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
3576                      !!!cp ('t248');                      !!!cp ('t248');
3577                      last INSCOPE;                      last INSCOPE;
3578                    }                    }
3579                  } # INSCOPE                  } # INSCOPE
3580                    unless (defined $i) {                    unless (defined $i) {
3581                      !!!cp ('t249');                      !!!cp ('t249');
3582                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
3583                                        text => $token->{tag_name}, token => $token);
3584                      ## Ignore the token                      ## Ignore the token
3585                        !!!nack ('t249.1');
3586                      !!!next-token;                      !!!next-token;
3587                      redo B;                      next B;
3588                    }                    }
3589                                    
3590                  ## As if </tr>                  ## As if </tr>
# Line 4704  sub _tree_construction_main ($) { Line 3592  sub _tree_construction_main ($) {
3592                  my $i;                  my $i;
3593                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3594                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3595                    if ($node->[1] eq 'tr') {                    if ($node->[1] == TABLE_ROW_EL) {
3596                      !!!cp ('t250');                      !!!cp ('t250');
3597                      $i = $_;                      $i = $_;
3598                      last INSCOPE;                      last INSCOPE;
3599                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
3600                      !!!cp ('t251');                      !!!cp ('t251');
3601                      last INSCOPE;                      last INSCOPE;
3602                    }                    }
3603                  } # INSCOPE                  } # INSCOPE
3604                    unless (defined $i) {                    unless (defined $i) {
3605                      !!!cp ('t252');                      !!!cp ('t252');
3606                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag',
3607                                        text => 'tr', token => $token);
3608                      ## Ignore the token                      ## Ignore the token
3609                        !!!nack ('t252.1');
3610                      !!!next-token;                      !!!next-token;
3611                      redo B;                      next B;
3612                    }                    }
3613                                    
3614                  ## Clear back to table row context                  ## Clear back to table row context
3615                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
3616                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
3617                    !!!cp ('t253');                    !!!cp ('t253');
3618  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3619                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3620                  }                  }
3621                                    
# Line 4742  sub _tree_construction_main ($) { Line 3628  sub _tree_construction_main ($) {
3628                my $i;                my $i;
3629                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3630                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3631                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
3632                    !!!cp ('t254');                    !!!cp ('t254');
3633                    $i = $_;                    $i = $_;
3634                    last INSCOPE;                    last INSCOPE;
3635                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
3636                    !!!cp ('t255');                    !!!cp ('t255');
3637                    last INSCOPE;                    last INSCOPE;
3638                  }                  }
3639                } # INSCOPE                } # INSCOPE
3640                unless (defined $i) {                unless (defined $i) {
3641                  !!!cp ('t256');                  !!!cp ('t256');
3642                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
3643                                    text => $token->{tag_name}, token => $token);
3644                  ## Ignore the token                  ## Ignore the token
3645                    !!!nack ('t256.1');
3646                  !!!next-token;                  !!!next-token;
3647                  redo B;                  next B;
3648                }                }
3649    
3650                ## Clear back to table body context                ## Clear back to table body context
3651                while (not {                while (not ($self->{open_elements}->[-1]->[1]
3652                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
3653                  !!!cp ('t257');                  !!!cp ('t257');
3654  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3655                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3656                }                }
3657    
3658                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3659                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
3660                  !!!nack ('t257.1');
3661                !!!next-token;                !!!next-token;
3662                redo B;                next B;
3663              } elsif ({              } elsif ({
3664                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3665                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
3666                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3667                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3668                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3669                !!!cp ('t258');            !!!cp ('t258');
3670                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
3671                ## Ignore the token                            text => $token->{tag_name}, token => $token);
3672                !!!next-token;            ## Ignore the token
3673                redo B;            !!!nack ('t258.1');
3674               !!!next-token;
3675              next B;
3676          } else {          } else {
3677            !!!cp ('t259');            !!!cp ('t259');
3678            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/',
3679                              text => $token->{tag_name}, token => $token);
3680    
3681            $insert = $insert_to_foster;            $insert = $insert_to_foster;
3682            #            #
3683          }          }
3684          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3685            unless ($self->{open_elements}->[-1]->[1] == HTML_EL and
3686                    @{$self->{open_elements}} == 1) { # redundant, maybe
3687              !!!parse-error (type => 'in body:#eof', token => $token);
3688              !!!cp ('t259.1');
3689              #
3690            } else {
3691              !!!cp ('t259.2');
3692              #
3693            }
3694    
3695            ## Stop parsing
3696            last B;
3697        } else {        } else {
3698          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3699        }        }
3700      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {      } elsif (($self->{insertion_mode} & IM_MASK) == IN_COLUMN_GROUP_IM) {
3701            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
3702              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3703                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3704                unless (length $token->{data}) {                unless (length $token->{data}) {
3705                  !!!cp ('t260');                  !!!cp ('t260');
3706                  !!!next-token;                  !!!next-token;
3707                  redo B;                  next B;
3708                }                }
3709              }              }
3710                            
# Line 4812  sub _tree_construction_main ($) { Line 3713  sub _tree_construction_main ($) {
3713            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
3714              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
3715                !!!cp ('t262');                !!!cp ('t262');
3716                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3717                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3718                  !!!ack ('t262.1');
3719                !!!next-token;                !!!next-token;
3720                redo B;                next B;
3721              } else {              } else {
3722                !!!cp ('t263');                !!!cp ('t263');
3723                #                #
3724              }              }
3725            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
3726              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
3727                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] == HTML_EL) {
3728                  !!!cp ('t264');                  !!!cp ('t264');
3729                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag',
3730                                    text => 'colgroup', token => $token);
3731                  ## Ignore the token                  ## Ignore the token
3732                  !!!next-token;                  !!!next-token;
3733                  redo B;                  next B;
3734                } else {                } else {
3735                  !!!cp ('t265');                  !!!cp ('t265');
3736                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
3737                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
3738                  !!!next-token;                  !!!next-token;
3739                  redo B;                              next B;            
3740                }                }
3741              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
3742                !!!cp ('t266');                !!!cp ('t266');
3743                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag',
3744                                  text => 'col', token => $token);
3745                ## Ignore the token                ## Ignore the token
3746                !!!next-token;                !!!next-token;
3747                redo B;                next B;
3748              } else {              } else {
3749                !!!cp ('t267');                !!!cp ('t267');
3750                #                #
3751              }              }
3752            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3753              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] == HTML_EL and
3754            }              @{$self->{open_elements}} == 1) { # redundant, maybe
3755              !!!cp ('t270.2');
3756              ## Stop parsing.
3757              last B;
3758            } else {
3759              ## NOTE: As if </colgroup>.
3760              !!!cp ('t270.1');
3761              pop @{$self->{open_elements}}; # colgroup
3762              $self->{insertion_mode} = IN_TABLE_IM;
3763              ## Reprocess.
3764              next B;
3765            }
3766          } else {
3767            die "$0: $token->{type}: Unknown token type";
3768          }
3769    
3770            ## As if </colgroup>            ## As if </colgroup>
3771            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] == HTML_EL) {
3772              !!!cp ('t269');              !!!cp ('t269');
3773              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
3774                !!!parse-error (type => 'unmatched end tag',
3775                                text => 'colgroup', token => $token);
3776              ## Ignore the token              ## Ignore the token
3777                !!!nack ('t269.1');
3778              !!!next-token;              !!!next-token;
3779              redo B;              next B;
3780            } else {            } else {
3781              !!!cp ('t270');              !!!cp ('t270');
3782              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
3783              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
3784                !!!ack-later;
3785              ## reprocess              ## reprocess
3786              redo B;              next B;
3787            }            }
3788      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
3789        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3790          !!!cp ('t271');          !!!cp ('t271');
3791          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3792          !!!next-token;          !!!next-token;
3793          redo B;          next B;
3794        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3795              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
3796                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] == OPTION_EL) {
3797                  !!!cp ('t272');              !!!cp ('t272');
3798                  ## As if </option>              ## As if </option>
3799                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
3800                } else {            } else {
3801                  !!!cp ('t273');              !!!cp ('t273');
3802                }            }
3803    
3804                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3805                !!!next-token;            !!!nack ('t273.1');
3806                redo B;            !!!next-token;
3807              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
3808                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
3809                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] == OPTION_EL) {
3810                  ## As if </option>              !!!cp ('t274');
3811                  pop @{$self->{open_elements}};              ## As if </option>
3812                } else {              pop @{$self->{open_elements}};
3813                  !!!cp ('t275');            } else {
3814                }              !!!cp ('t275');
3815              }
3816    
3817                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] == OPTGROUP_EL) {
3818                  !!!cp ('t276');              !!!cp ('t276');
3819                  ## As if </optgroup>              ## As if </optgroup>
3820                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
3821                } else {            } else {
3822                  !!!cp ('t277');              !!!cp ('t277');
3823                }            }
3824    
3825                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3826                !!!next-token;            !!!nack ('t277.1');
3827                redo B;            !!!next-token;
3828              } elsif ($token->{tag_name} eq 'select') {            next B;
3829  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ({
3830                !!!parse-error (type => 'not closed:select');                     select => 1, input => 1, textarea => 1,
3831                ## As if </select> instead                   }->{$token->{tag_name}} or
3832                ## have an element in table scope                   (($self->{insertion_mode} & IM_MASK)
3833                my $i;                        == IN_SELECT_IN_TABLE_IM and
3834                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
3835                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
3836                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
3837                    !!!cp ('t278');                     tr => 1, td => 1, th => 1,
3838                    $i = $_;                    }->{$token->{tag_name}})) {
3839                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
3840                  } elsif ({            !!!parse-error (type => 'not closed', text => 'select',
3841                            table => 1, html => 1,                            token => $token);
3842                           }->{$node->[1]}) {            ## NOTE: As if the token were </select> (<select> case) or
3843                    !!!cp ('t279');            ## as if there were </select> (otherwise).
3844                    last INSCOPE;            ## have an element in table scope
3845                  }            my $i;
3846                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3847                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
3848                  !!!cp ('t280');              if ($node->[1] == SELECT_EL) {
3849                  !!!parse-error (type => 'unmatched end tag:select');                !!!cp ('t278');
3850                  ## Ignore the token                $i = $_;
3851                  !!!next-token;                last INSCOPE;
3852                  redo B;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
3853                }                !!!cp ('t279');
3854                  last INSCOPE;
3855                }
3856              } # INSCOPE
3857              unless (defined $i) {
3858                !!!cp ('t280');
3859                !!!parse-error (type => 'unmatched end tag',
3860                                text => 'select', token => $token);
3861                ## Ignore the token
3862                !!!nack ('t280.1');
3863                !!!next-token;
3864                next B;
3865              }
3866                                
3867                !!!cp ('t281');            !!!cp ('t281');
3868                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
3869    
3870                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
3871    
3872                !!!next-token;            if ($token->{tag_name} eq 'select') {
3873                redo B;              !!!nack ('t281.2');
3874                !!!next-token;
3875                next B;
3876              } else {
3877                !!!cp ('t281.1');
3878                !!!ack-later;
3879                ## Reprocess the token.
3880                next B;
3881              }
3882          } else {          } else {
3883            !!!cp ('t282');            !!!cp ('t282');
3884            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select',
3885                              text => $token->{tag_name}, token => $token);
3886            ## Ignore the token            ## Ignore the token
3887              !!!nack ('t282.1');
3888            !!!next-token;            !!!next-token;
3889            redo B;            next B;
3890          }          }
3891        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
3892              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
3893                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] == OPTION_EL and
3894                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] == OPTGROUP_EL) {
3895                  !!!cp ('t283');              !!!cp ('t283');
3896                  ## As if </option>              ## As if </option>
3897                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
3898                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] == OPTGROUP_EL) {
3899                  !!!cp ('t284');              !!!cp ('t284');
3900                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
3901                } else {            } else {
3902                  !!!cp ('t285');              !!!cp ('t285');
3903                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
3904                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
3905                }              ## Ignore the token
3906                !!!next-token;            }
3907                redo B;            !!!nack ('t285.1');
3908              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
3909                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
3910                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
3911                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] == OPTION_EL) {
3912                } else {              !!!cp ('t286');
3913                  !!!cp ('t287');              pop @{$self->{open_elements}};
3914                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            } else {
3915                  ## Ignore the token              !!!cp ('t287');
3916                }              !!!parse-error (type => 'unmatched end tag',
3917                !!!next-token;                              text => $token->{tag_name}, token => $token);
3918                redo B;              ## Ignore the token
3919              } elsif ($token->{tag_name} eq 'select') {            }
3920                ## have an element in table scope            !!!nack ('t287.1');
3921                my $i;            !!!next-token;
3922                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
3923                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
3924                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
3925                    !!!cp ('t288');            my $i;
3926                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3927                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
3928                  } elsif ({              if ($node->[1] == SELECT_EL) {
3929                            table => 1, html => 1,                !!!cp ('t288');
3930                           }->{$node->[1]}) {                $i = $_;
3931                    !!!cp ('t289');                last INSCOPE;
3932                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
3933                  }                !!!cp ('t289');
3934                } # INSCOPE                last INSCOPE;
3935                unless (defined $i) {              }
3936                  !!!cp ('t290');            } # INSCOPE
3937                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            unless (defined $i) {
3938                  ## Ignore the token              !!!cp ('t290');
3939                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
3940                  redo B;                              text => $token->{tag_name}, token => $token);
3941                }              ## Ignore the token
3942                !!!nack ('t290.1');
3943                !!!next-token;
3944                next B;
3945              }
3946                                
3947                !!!cp ('t291');            !!!cp ('t291');
3948                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
3949    
3950                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
3951    
3952                !!!next-token;            !!!nack ('t291.1');
3953                redo B;            !!!next-token;
3954              } elsif ({            next B;
3955                        caption => 1, table => 1, tbody => 1,          } elsif (($self->{insertion_mode} & IM_MASK)
3956                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                       == IN_SELECT_IN_TABLE_IM and
3957                       }->{$token->{tag_name}}) {                   {
3958                      caption => 1, table => 1, tbody => 1,
3959                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
3960                     }->{$token->{tag_name}}) {
3961  ## TODO: The following is wrong?  ## TODO: The following is wrong?
3962                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
3963                              text => $token->{tag_name}, token => $token);
3964                                
3965                ## have an element in table scope            ## have an element in table scope
3966                my $i;            my $i;
3967                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3968                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
3969                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
3970                    !!!cp ('t292');                !!!cp ('t292');
3971                    $i = $_;                $i = $_;
3972                    last INSCOPE;                last INSCOPE;
3973                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
3974                            table => 1, html => 1,                !!!cp ('t293');
3975                           }->{$node->[1]}) {                last INSCOPE;
3976                    !!!cp ('t293');              }
3977                    last INSCOPE;            } # INSCOPE
3978                  }            unless (defined $i) {
3979                } # INSCOPE              !!!cp ('t294');
3980                unless (defined $i) {              ## Ignore the token
3981                  !!!cp ('t294');              !!!nack ('t294.1');
3982                  ## Ignore the token              !!!next-token;
3983                  !!!next-token;              next B;
3984                  redo B;            }
               }  
3985                                
3986                ## As if </select>            ## As if </select>
3987                ## have an element in table scope            ## have an element in table scope
3988                undef $i;            undef $i;
3989                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3990                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
3991                  if ($node->[1] eq 'select') {              if ($node->[1] == SELECT_EL) {
3992                    !!!cp ('t295');                !!!cp ('t295');
3993                    $i = $_;                $i = $_;
3994                    last INSCOPE;                last INSCOPE;
3995                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
3996  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
3997                    !!!cp ('t296');                !!!cp ('t296');
3998                    last INSCOPE;                last INSCOPE;
3999                  }              }
4000                } # INSCOPE            } # INSCOPE
4001                unless (defined $i) {            unless (defined $i) {
4002                  !!!cp ('t297');              !!!cp ('t297');
4003  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
4004                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag',
4005                  ## Ignore the </select> token                              text => 'select', token => $token);
4006                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
4007                  redo B;              !!!nack ('t297.1');
4008                }              !!!next-token; ## TODO: ok?
4009                next B;
4010              }
4011                                
4012                !!!cp ('t298');            !!!cp ('t298');
4013                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
4014    
4015                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
4016    
4017                ## reprocess            !!!ack-later;
4018                redo B;            ## reprocess
4019              next B;
4020          } else {          } else {
4021            !!!cp ('t299');            !!!cp ('t299');
4022            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/',
4023                              text => $token->{tag_name}, token => $token);
4024            ## Ignore the token            ## Ignore the token
4025              !!!nack ('t299.3');
4026            !!!next-token;            !!!next-token;
4027            redo B;            next B;
4028            }
4029          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4030            unless ($self->{open_elements}->[-1]->[1] == HTML_EL and
4031                    @{$self->{open_elements}} == 1) { # redundant, maybe
4032              !!!cp ('t299.1');
4033              !!!parse-error (type => 'in body:#eof', token => $token);
4034            } else {
4035              !!!cp ('t299.2');
4036          }          }
4037    
4038            ## Stop parsing.
4039            last B;
4040        } else {        } else {
4041          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4042        }        }
4043      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4044        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4045          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
4046            my $data = $1;            my $data = $1;
4047            ## As if in body            ## As if in body
4048            $reconstruct_active_formatting_elements->($insert_to_current);            $reconstruct_active_formatting_elements->($insert_to_current);
# Line 5086  sub _tree_construction_main ($) { Line 4052  sub _tree_construction_main ($) {
4052            unless (length $token->{data}) {            unless (length $token->{data}) {
4053              !!!cp ('t300');              !!!cp ('t300');
4054              !!!next-token;              !!!next-token;
4055              redo B;              next B;
4056            }            }
4057          }          }
4058                    
4059          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4060            !!!cp ('t301');            !!!cp ('t301');
4061            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#text', token => $token);
4062              #
           ## Reprocess in the "after body" insertion mode.  
4063          } else {          } else {
4064            !!!cp ('t302');            !!!cp ('t302');
4065              ## "after body" insertion mode
4066              !!!parse-error (type => 'after body:#text', token => $token);
4067              #
4068          }          }
           
         ## "after body" insertion mode  
         !!!parse-error (type => 'after body:#character');  
4069    
4070          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4071          ## reprocess          ## reprocess
4072          redo B;          next B;
4073        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4074          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4075            !!!cp ('t303');            !!!cp ('t303');
4076            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html',
4077                                        text => $token->{tag_name}, token => $token);
4078            ## Reprocess in the "after body" insertion mode.            #
4079          } else {          } else {
4080            !!!cp ('t304');            !!!cp ('t304');
4081              ## "after body" insertion mode
4082              !!!parse-error (type => 'after body',
4083                              text => $token->{tag_name}, token => $token);
4084              #
4085          }          }
4086    
         ## "after body" insertion mode  
         !!!parse-error (type => 'after body:'.$token->{tag_name});  
   
4087          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4088            !!!ack-later;
4089          ## reprocess          ## reprocess
4090          redo B;          next B;
4091        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4092          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4093            !!!cp ('t305');            !!!cp ('t305');
4094            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/',
4095                              text => $token->{tag_name}, token => $token);
4096                        
4097            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
4098            ## Reprocess in the "after body" insertion mode.            ## Reprocess.
4099              next B;
4100          } else {          } else {
4101            !!!cp ('t306');            !!!cp ('t306');
4102          }          }
# Line 5136  sub _tree_construction_main ($) { Line 4105  sub _tree_construction_main ($) {
4105          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
4106            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
4107              !!!cp ('t307');              !!!cp ('t307');
4108              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag',
4109                                text => 'html', token => $token);
4110              ## Ignore the token              ## Ignore the token
4111              !!!next-token;              !!!next-token;
4112              redo B;              next B;
4113            } else {            } else {
4114              !!!cp ('t308');              !!!cp ('t308');
4115              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4116              !!!next-token;              !!!next-token;
4117              redo B;              next B;
4118            }            }
4119          } else {          } else {
4120            !!!cp ('t309');            !!!cp ('t309');
4121            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/',
4122                              text => $token->{tag_name}, token => $token);
4123    
4124            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
4125            ## reprocess            ## reprocess
4126            redo B;            next B;
4127          }          }
4128          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4129            !!!cp ('t309.2');
4130            ## Stop parsing
4131            last B;
4132        } else {        } else {
4133          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4134        }        }
4135      } elsif ($self->{insertion_mode} & FRAME_IMS) {      } elsif ($self->{insertion_mode} & FRAME_IMS) {
4136        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4137          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
4138            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4139                        
4140            unless (length $token->{data}) {            unless (length $token->{data}) {
4141              !!!cp ('t310');              !!!cp ('t310');
4142              !!!next-token;              !!!next-token;
4143              redo B;              next B;
4144            }            }
4145          }          }
4146                    
4147          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0C\x20]+//) {
4148            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4149              !!!cp ('t311');              !!!cp ('t311');
4150              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#text', token => $token);
4151            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4152              !!!cp ('t312');              !!!cp ('t312');
4153              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#text', token => $token);
4154            } else { # "after html frameset"            } else { # "after after frameset"
4155              !!!cp ('t313');              !!!cp ('t313');
4156              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#text', token => $token);
   
             $self->{insertion_mode} = AFTER_FRAMESET_IM;  
             ## Reprocess in the "after frameset" insertion mode.  
             !!!parse-error (type => 'after frameset:#character');  
4157            }            }
4158                        
4159            ## Ignore the token.            ## Ignore the token.
# Line 5193  sub _tree_construction_main ($) { Line 4164  sub _tree_construction_main ($) {
4164              !!!cp ('t315');              !!!cp ('t315');
4165              !!!next-token;              !!!next-token;
4166            }            }
4167            redo B;            next B;
4168          }          }
4169                    
4170          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
4171        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t316');  
           !!!parse-error (type => 'after html:'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t317');  
         }  
   
4172          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
4173              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4174            !!!cp ('t318');            !!!cp ('t318');
4175            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4176              !!!nack ('t318.1');
4177            !!!next-token;            !!!next-token;
4178            redo B;            next B;
4179          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
4180                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
4181            !!!cp ('t319');            !!!cp ('t319');
4182            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4183            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4184              !!!ack ('t319.1');
4185            !!!next-token;            !!!next-token;
4186            redo B;            next B;
4187          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
4188            !!!cp ('t320');            !!!cp ('t320');
4189            ## NOTE: As if in body.            ## NOTE: As if in head.
4190            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
4191            redo B;            next B;
4192    
4193              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
4194              ## has no parse error.
4195          } else {          } else {
4196            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4197              !!!cp ('t321');              !!!cp ('t321');
4198              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset',
4199            } else {                              text => $token->{tag_name}, token => $token);
4200              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4201              !!!cp ('t322');              !!!cp ('t322');
4202              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset',
4203                                text => $token->{tag_name}, token => $token);
4204              } else { # "after after frameset"
4205                !!!cp ('t322.2');
4206                !!!parse-error (type => 'after after frameset',
4207                                text => $token->{tag_name}, token => $token);
4208            }            }
4209            ## Ignore the token            ## Ignore the token
4210              !!!nack ('t322.1');
4211            !!!next-token;            !!!next-token;
4212            redo B;            next B;
4213          }          }
4214        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t323');  
           !!!parse-error (type => 'after html:/'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t324');  
         }  
   
4215          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
4216              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4217            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] == HTML_EL and
4218                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4219              !!!cp ('t325');              !!!cp ('t325');
4220              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
4221                                text => $token->{tag_name}, token => $token);
4222              ## Ignore the token              ## Ignore the token
4223              !!!next-token;              !!!next-token;
4224            } else {            } else {
# Line 5264  sub _tree_construction_main ($) { Line 4228  sub _tree_construction_main ($) {
4228            }            }
4229    
4230            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4231                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] == FRAMESET_EL)) {
4232              !!!cp ('t327');              !!!cp ('t327');
4233              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4234            } else {            } else {
4235              !!!cp ('t328');              !!!cp ('t328');
4236            }            }
4237            redo B;            next B;
4238          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
4239                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4240            !!!cp ('t329');            !!!cp ('t329');
4241            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4242            !!!next-token;            !!!next-token;
4243            redo B;            next B;
4244          } else {          } else {
4245            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4246              !!!cp ('t330');              !!!cp ('t330');
4247              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/',
4248            } else {                              text => $token->{tag_name}, token => $token);
4249              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4250                !!!cp ('t330.1');
4251                !!!parse-error (type => 'after frameset:/',
4252                                text => $token->{tag_name}, token => $token);
4253              } else { # "after after html"
4254              !!!cp ('t331');              !!!cp ('t331');
4255              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after after frameset:/',
4256                                text => $token->{tag_name}, token => $token);
4257            }            }
4258            ## Ignore the token            ## Ignore the token
4259            !!!next-token;            !!!next-token;
4260            redo B;            next B;
4261            }
4262          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4263            unless ($self->{open_elements}->[-1]->[1] == HTML_EL and
4264                    @{$self->{open_elements}} == 1) { # redundant, maybe
4265              !!!cp ('t331.1');
4266              !!!parse-error (type => 'in body:#eof', token => $token);
4267            } else {
4268              !!!cp ('t331.2');
4269          }          }
4270            
4271            ## Stop parsing
4272            last B;
4273        } else {        } else {
4274          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4275        }        }
   
       ## ISSUE: An issue in spec here  
4276      } else {      } else {
4277        die "$0: $self->{insertion_mode}: Unknown insertion mode";        die "$0: $self->{insertion_mode}: Unknown insertion mode";
4278      }      }
# Line 5303  sub _tree_construction_main ($) { Line 4282  sub _tree_construction_main ($) {
4282        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
4283          !!!cp ('t332');          !!!cp ('t332');
4284          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4285          $script_start_tag->($insert);          $script_start_tag->();
4286          redo B;          next B;
4287        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
4288          !!!cp ('t333');          !!!cp ('t333');
4289          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4290          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
4291          redo B;          next B;
4292        } elsif ({        } elsif ({
4293                  base => 1, link => 1,                  base => 1, command => 1, eventsource => 1, link => 1,
4294                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
4295          !!!cp ('t334');          !!!cp ('t334');
4296          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
4297          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4298          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}};
4299            !!!ack ('t334.1');
4300          !!!next-token;          !!!next-token;
4301          redo B;          next B;
4302        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
4303          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
4304          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4305          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}};
4306    
4307          unless ($self->{confident}) {          unless ($self->{confident}) {
4308            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
4309              !!!cp ('t335');              !!!cp ('t335');
4310                ## NOTE: Whether the encoding is supported or not is handled
4311                ## in the {change_encoding} callback.
4312              $self->{change_encoding}              $self->{change_encoding}
4313                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
4314                            
4315              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4316                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
4317                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
4318                                           ->{has_reference});                                           ->{has_reference});
4319            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4320              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
4321                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4322                      [\x09-\x0D\x20]*=                      [\x09\x0A\x0C\x0D\x20]*=
4323                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4324                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09\x0A\x0C\x0D\x20][^\x09\x0A\x0C\x0D\x20\x3B]*))
4325                       /x) {
4326                !!!cp ('t336');                !!!cp ('t336');
4327                  ## NOTE: Whether the encoding is supported or not is handled
4328                  ## in the {change_encoding} callback.
4329                $self->{change_encoding}                $self->{change_encoding}
4330                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
4331                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4332                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
4333                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5367  sub _tree_construction_main ($) { Line 4351  sub _tree_construction_main ($) {
4351            }            }
4352          }          }
4353    
4354            !!!ack ('t338.1');
4355          !!!next-token;          !!!next-token;
4356          redo B;          next B;
4357        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
4358          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
4359          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4360          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
4361            if (defined $self->{head_element}) {          next B;
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
4362        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
4363          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
4364                                
4365          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
4366              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] == BODY_EL)) {
4367            !!!cp ('t342');            !!!cp ('t342');
4368            ## Ignore the token            ## Ignore the token
4369          } else {          } else {
# Line 5401  sub _tree_construction_main ($) { Line 4377  sub _tree_construction_main ($) {
4377              }              }
4378            }            }
4379          }          }
4380            !!!nack ('t343.1');
4381          !!!next-token;          !!!next-token;
4382          redo B;          next B;
4383        } elsif ({        } elsif ({
4384                  address => 1, blockquote => 1, center => 1, dir => 1,                  ## NOTE: Start tags for non-phrasing flow content elements
4385                  div => 1, dl => 1, fieldset => 1,  
4386                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  ## NOTE: The normal one
4387                  listing => 1, menu => 1, ol => 1, p => 1, ul => 1,                  address => 1, article => 1, aside => 1, blockquote => 1,
4388                  pre => 1,                  center => 1, datagrid => 1, details => 1, dialog => 1,
4389                    dir => 1, div => 1, dl => 1, fieldset => 1, figure => 1,
4390                    footer => 1, h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1,
4391                    h6 => 1, header => 1, menu => 1, nav => 1, ol => 1, p => 1,
4392                    section => 1, ul => 1,
4393                    ## NOTE: As normal, but drops leading newline
4394                    pre => 1, listing => 1,
4395                    ## NOTE: As normal, but interacts with the form element pointer
4396                    form => 1,
4397                    
4398                    table => 1,
4399                    hr => 1,
4400                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
4401            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
4402              !!!cp ('t350');
4403              !!!parse-error (type => 'in form:form', token => $token);
4404              ## Ignore the token
4405              !!!nack ('t350.1');
4406              !!!next-token;
4407              next B;
4408            }
4409    
4410          ## has a p element in scope          ## has a p element in scope
4411          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4412            if ($_->[1] eq 'p') {            if ($_->[1] == P_EL) {
4413              !!!cp ('t344');              !!!cp ('t344');
4414              !!!back-token;              !!!back-token; # <form>
4415              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
4416              redo B;                        line => $token->{line}, column => $token->{column}};
4417            } elsif ({              next B;
4418                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
4419              !!!cp ('t345');              !!!cp ('t345');
4420              last INSCOPE;              last INSCOPE;
4421            }            }
4422          } # INSCOPE          } # INSCOPE
4423                        
4424          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4425          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
4426              !!!nack ('t346.1');
4427            !!!next-token;            !!!next-token;
4428            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4429              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5440  sub _tree_construction_main ($) { Line 4436  sub _tree_construction_main ($) {
4436            } else {            } else {
4437              !!!cp ('t348');              !!!cp ('t348');
4438            }            }
4439          } else {          } elsif ($token->{tag_name} eq 'form') {
4440            !!!cp ('t347');            !!!cp ('t347.1');
4441              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4442    
4443              !!!nack ('t347.2');
4444            !!!next-token;            !!!next-token;
4445          }          } elsif ($token->{tag_name} eq 'table') {
4446          redo B;            !!!cp ('t382');
4447        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
4448          if (defined $self->{form_element}) {            
4449            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
4450            !!!parse-error (type => 'in form:form');  
4451            ## Ignore the token            !!!nack ('t382.1');
4452              !!!next-token;
4453            } elsif ($token->{tag_name} eq 'hr') {
4454              !!!cp ('t386');
4455              pop @{$self->{open_elements}};
4456            
4457              !!!nack ('t386.1');
4458            !!!next-token;            !!!next-token;
           redo B;  
4459          } else {          } else {
4460            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!cp ('t351');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               !!!cp ('t352');  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
4461            !!!next-token;            !!!next-token;
           redo B;  
4462          }          }
4463            next B;
4464        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{tag_name} eq 'li') {
4465          ## has a p element in scope          ## NOTE: As normal, but imply </li> when there's another <li> ...
4466          INSCOPE: for (reverse @{$self->{open_elements}}) {  
4467            if ($_->[1] eq 'p') {          ## NOTE: Special, Scope (<li><foo><li> == <li><foo><li/></foo></li>)
4468              !!!cp ('t353');            ## Interpreted as <li><foo/></li><li/> (non-conforming)
4469              !!!back-token;            ## blockquote (O9.27), center (O), dd (Fx3, O, S3.1.2, IE7),
4470              $token = {type => END_TAG_TOKEN, tag_name => 'p'};            ## dt (Fx, O, S, IE), dl (O), fieldset (O, S, IE), form (Fx, O, S),
4471              redo B;            ## hn (O), pre (O), applet (O, S), button (O, S), marquee (Fx, O, S),
4472            } elsif ({            ## object (Fx)
4473                      table => 1, caption => 1, td => 1, th => 1,            ## Generate non-tree (non-conforming)
4474                      button => 1, marquee => 1, object => 1, html => 1,            ## basefont (IE7 (where basefont is non-void)), center (IE),
4475                     }->{$_->[1]}) {            ## form (IE), hn (IE)
4476              !!!cp ('t354');          ## address, div, p (<li><foo><li> == <li><foo/></li><li/>)
4477              last INSCOPE;            ## Interpreted as <li><foo><li/></foo></li> (non-conforming)
4478            }            ## div (Fx, S)
4479          } # INSCOPE  
4480                      my $non_optional;
         ## Step 1  
4481          my $i = -1;          my $i = -1;
4482          my $node = $self->{open_elements}->[$i];  
4483          LI: {          ## 1.
4484            ## Step 2          for my $node (reverse @{$self->{open_elements}}) {
4485            if ($node->[1] eq 'li') {            if ($node->[1] == LI_EL) {
4486              if ($i != -1) {              ## 2. (a) As if </li>
4487                !!!cp ('t355');              {
4488                !!!parse-error (type => 'end tag missing:'.                ## If no </li> - not applied
4489                                $self->{open_elements}->[-1]->[1]);                #
4490              } else {  
4491                !!!cp ('t356');                ## Otherwise
4492    
4493                  ## 1. generate implied end tags, except for </li>
4494                  #
4495    
4496                  ## 2. If current node != "li", parse error
4497                  if ($non_optional) {
4498                    !!!parse-error (type => 'not closed',
4499                                    text => $non_optional->[0]->manakai_local_name,
4500                                    token => $token);
4501                    !!!cp ('t355');
4502                  } else {
4503                    !!!cp ('t356');
4504                  }
4505    
4506                  ## 3. Pop
4507                  splice @{$self->{open_elements}}, $i;
4508              }              }
4509              splice @{$self->{open_elements}}, $i;  
4510              last LI;              last; ## 2. (b) goto 5.
4511            } else {            } elsif (
4512                       ## NOTE: not "formatting" and not "phrasing"
4513                       ($node->[1] & SPECIAL_EL or
4514                        $node->[1] & SCOPING_EL) and
4515                       ## NOTE: "li", "dt", and "dd" are in |SPECIAL_EL|.
4516                       (not $node->[1] & ADDRESS_DIV_P_EL)
4517                      ) {
4518                ## 3.
4519              !!!cp ('t357');              !!!cp ('t357');
4520            }              last; ## goto 5.
4521                        } elsif ($node->[1] & END_TAG_OPTIONAL_EL) {
           ## 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') {  
4522              !!!cp ('t358');              !!!cp ('t358');
4523              last LI;              #
4524              } else {
4525                !!!cp ('t359');
4526                $non_optional ||= $node;
4527                #
4528            }            }
4529                        ## 4.
4530            !!!cp ('t359');            ## goto 2.
           ## Step 4  
4531            $i--;            $i--;
4532            $node = $self->{open_elements}->[$i];          }
4533            redo LI;  
4534          } # LI          ## 5. (a) has a |p| element in scope
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
4535          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4536            if ($_->[1] eq 'p') {            if ($_->[1] == P_EL) {
4537              !!!cp ('t360');              !!!cp ('t353');
4538              !!!back-token;  
4539              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              ## NOTE: |<p><li>|, for example.
4540              redo B;  
4541            } elsif ({              !!!back-token; # <x>
4542                      table => 1, caption => 1, td => 1, th => 1,              $token = {type => END_TAG_TOKEN, tag_name => 'p',
4543                      button => 1, marquee => 1, object => 1, html => 1,                        line => $token->{line}, column => $token->{column}};
4544                     }->{$_->[1]}) {              next B;
4545              !!!cp ('t361');            } elsif ($_->[1] & SCOPING_EL) {
4546                !!!cp ('t354');
4547              last INSCOPE;              last INSCOPE;
4548            }            }
4549          } # INSCOPE          } # INSCOPE
4550              
4551          ## Step 1          ## 5. (b) insert
4552            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4553            !!!nack ('t359.1');
4554            !!!next-token;
4555            next B;
4556          } elsif ($token->{tag_name} eq 'dt' or
4557                   $token->{tag_name} eq 'dd') {
4558            ## NOTE: As normal, but imply </dt> or </dd> when ...
4559    
4560            my $non_optional;
4561          my $i = -1;          my $i = -1;
4562          my $node = $self->{open_elements}->[$i];  
4563          LI: {          ## 1.
4564            ## Step 2          for my $node (reverse @{$self->{open_elements}}) {
4565            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($node->[1] == DTDD_EL) {
4566              if ($i != -1) {              ## 2. (a) As if </li>
4567                !!!cp ('t362');              {
4568                !!!parse-error (type => 'end tag missing:'.                ## If no </li> - not applied
4569                                $self->{open_elements}->[-1]->[1]);                #
4570              } else {  
4571                !!!cp ('t363');                ## Otherwise
4572    
4573                  ## 1. generate implied end tags, except for </dt> or </dd>
4574                  #
4575    
4576                  ## 2. If current node != "dt"|"dd", parse error
4577                  if ($non_optional) {
4578                    !!!parse-error (type => 'not closed',
4579                                    text => $non_optional->[0]->manakai_local_name,
4580                                    token => $token);
4581                    !!!cp ('t355.1');
4582                  } else {
4583                    !!!cp ('t356.1');
4584                  }
4585    
4586                  ## 3. Pop
4587                  splice @{$self->{open_elements}}, $i;
4588              }              }
4589              splice @{$self->{open_elements}}, $i;  
4590              last LI;              last; ## 2. (b) goto 5.
4591              } elsif (
4592                       ## NOTE: not "formatting" and not "phrasing"
4593                       ($node->[1] & SPECIAL_EL or
4594                        $node->[1] & SCOPING_EL) and
4595                       ## NOTE: "li", "dt", and "dd" are in |SPECIAL_EL|.
4596    
4597                       (not $node->[1] & ADDRESS_DIV_P_EL)
4598                      ) {
4599                ## 3.
4600                !!!cp ('t357.1');
4601                last; ## goto 5.
4602              } elsif ($node->[1] & END_TAG_OPTIONAL_EL) {
4603                !!!cp ('t358.1');
4604                #
4605            } else {            } else {
4606              !!!cp ('t364');              !!!cp ('t359.1');
4607            }              $non_optional ||= $node;
4608                          #
           ## 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') {  
             !!!cp ('t365');  
             last LI;  
4609            }            }
4610                        ## 4.
4611            !!!cp ('t366');            ## goto 2.
           ## Step 4  
4612            $i--;            $i--;
4613            $node = $self->{open_elements}->[$i];          }
4614            redo LI;  
4615          } # LI          ## 5. (a) has a |p| element in scope
4616                      INSCOPE: for (reverse @{$self->{open_elements}}) {
4617          !!!insert-element-t ($token->{tag_name}, $token->{attributes});            if ($_->[1] == P_EL) {
4618                !!!cp ('t353.1');
4619                !!!back-token; # <x>
4620                $token = {type => END_TAG_TOKEN, tag_name => 'p',
4621                          line => $token->{line}, column => $token->{column}};
4622                next B;
4623              } elsif ($_->[1] & SCOPING_EL) {
4624                !!!cp ('t354.1');
4625                last INSCOPE;
4626              }
4627            } # INSCOPE
4628    
4629            ## 5. (b) insert
4630            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4631            !!!nack ('t359.2');
4632          !!!next-token;          !!!next-token;
4633          redo B;          next B;
4634        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
4635            ## NOTE: As normal, but effectively ends parsing
4636    
4637          ## has a p element in scope          ## has a p element in scope
4638          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4639            if ($_->[1] eq 'p') {            if ($_->[1] == P_EL) {
4640              !!!cp ('t367');              !!!cp ('t367');
4641              !!!back-token;              !!!back-token; # <plaintext>
4642              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
4643              redo B;                        line => $token->{line}, column => $token->{column}};
4644            } elsif ({              next B;
4645                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
4646              !!!cp ('t368');              !!!cp ('t368');
4647              last INSCOPE;              last INSCOPE;
4648            }            }
4649          } # INSCOPE          } # INSCOPE
4650                        
4651          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4652                        
4653          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4654                        
4655            !!!nack ('t368.1');
4656          !!!next-token;          !!!next-token;
4657          redo B;          next B;
4658        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
4659          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4660            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
4661            if ($node->[1] eq 'a') {            if ($node->[1] == A_EL) {
4662              !!!cp ('t371');              !!!cp ('t371');
4663              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
4664                            
4665              !!!back-token;              !!!back-token; # <a>
4666              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
4667              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
4668                $formatting_end_tag->($token);
4669                            
4670              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
4671                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5643  sub _tree_construction_main ($) { Line 4690  sub _tree_construction_main ($) {
4690                        
4691          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4692    
4693          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4694          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4695    
4696            !!!nack ('t374.1');
4697          !!!next-token;          !!!next-token;
4698          redo B;          next B;
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         !!!cp ('t375');  
         $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;  
         redo B;  
4699        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
4700          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4701    
4702          ## has a |nobr| element in scope          ## has a |nobr| element in scope
4703          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4704            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4705            if ($node->[1] eq 'nobr') {            if ($node->[1] == NOBR_EL) {
4706              !!!cp ('t376');              !!!cp ('t376');
4707              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
4708              !!!back-token;              !!!back-token; # <nobr>
4709              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
4710              redo B;                        line => $token->{line}, column => $token->{column}};
4711            } elsif ({              next B;
4712                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
4713              !!!cp ('t377');              !!!cp ('t377');
4714              last INSCOPE;              last INSCOPE;
4715            }            }
4716          } # INSCOPE          } # INSCOPE
4717                    
4718          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4719          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4720                    
4721            !!!nack ('t377.1');
4722          !!!next-token;          !!!next-token;
4723          redo B;          next B;
4724        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
4725          ## has a button element in scope          ## has a button element in scope
4726          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4727            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4728            if ($node->[1] eq 'button') {            if ($node->[1] == BUTTON_EL) {
4729              !!!cp ('t378');              !!!cp ('t378');
4730              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
4731              !!!back-token;              !!!back-token; # <button>
4732              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
4733              redo B;                        line => $token->{line}, column => $token->{column}};
4734            } elsif ({              next B;
4735                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
4736              !!!cp ('t379');              !!!cp ('t379');
4737              last INSCOPE;              last INSCOPE;
4738            }            }
# Line 5708  sub _tree_construction_main ($) { Line 4740  sub _tree_construction_main ($) {
4740                        
4741          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4742                        
4743          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4744    
4745          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
4746    
4747          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4748    
4749            !!!nack ('t379.1');
4750          !!!next-token;          !!!next-token;
4751          redo B;          next B;
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
         !!!next-token;  
         redo B;  
4752        } elsif ({        } elsif ({
4753                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
4754                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
4755                  image => 1,                  noembed => 1,
4756                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
4757                    noscript => 0, ## TODO: 1 if scripting is enabled
4758                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
4759          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
4760            !!!cp ('t384');            !!!cp ('t381');
4761            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
4762          } else {          } else {
4763            !!!cp ('t385');            !!!cp ('t399');
4764          }          }
4765            ## NOTE: There is an "as if in body" code clone.
4766          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
4767          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t386');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t387');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         !!!cp ('t388');  
         $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;  
         redo B;  
4768        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
4769          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
4770                    
4771          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4772            !!!cp ('t389');            !!!cp ('t389');
4773            ## Ignore the token            ## Ignore the token
4774              !!!nack ('t389'); ## NOTE: Not acknowledged.
4775            !!!next-token;            !!!next-token;
4776            redo B;            next B;
4777          } else {          } else {
4778              !!!ack ('t391.1');
4779    
4780            my $at = $token->{attributes};            my $at = $token->{attributes};
4781            my $form_attrs;            my $form_attrs;
4782            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5825  sub _tree_construction_main ($) { Line 4786  sub _tree_construction_main ($) {
4786            delete $at->{prompt};            delete $at->{prompt};
4787            my @tokens = (            my @tokens = (
4788                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
4789                           attributes => $form_attrs},                           attributes => $form_attrs,
4790                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
4791                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
4792                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
4793                            {type => START_TAG_TOKEN, tag_name => 'p',
4794                             line => $token->{line}, column => $token->{column}},
4795                            {type => START_TAG_TOKEN, tag_name => 'label',
4796                             line => $token->{line}, column => $token->{column}},
4797                         );                         );
4798            if ($prompt_attr) {            if ($prompt_attr) {
4799              !!!cp ('t390');              !!!cp ('t390');
4800              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
4801                               #line => $token->{line}, column => $token->{column},
4802                              };
4803            } else {            } else {
4804              !!!cp ('t391');              !!!cp ('t391');
4805              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
4806                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
4807                               #line => $token->{line}, column => $token->{column},
4808                              }; # SHOULD
4809              ## TODO: make this configurable              ## TODO: make this configurable
4810            }            }
4811            push @tokens,            push @tokens,
4812                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
4813                             line => $token->{line}, column => $token->{column}},
4814                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4815                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
4816                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
4817                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
4818                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
4819            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
4820                             line => $token->{line}, column => $token->{column}},
4821                            {type => END_TAG_TOKEN, tag_name => 'form',
4822                             line => $token->{line}, column => $token->{column}};
4823            !!!back-token (@tokens);            !!!back-token (@tokens);
4824            redo B;            !!!next-token;
4825              next B;
4826          }          }
4827        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
4828          my $tag_name = $token->{tag_name};          ## Step 1
4829          my $el;          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
4830                    
4831            ## Step 2
4832          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
4833    
4834            ## Step 3
4835            $self->{ignore_newline} = 1;
4836    
4837            ## Step 4
4838            ## ISSUE: This step is wrong. (r2302 enbugged)
4839    
4840            ## Step 5
4841          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
4842          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
4843            
4844          $insert->($el);          ## Step 6-7
4845                    $self->{insertion_mode} |= IN_CDATA_RCDATA_IM;
4846          my $text = '';  
4847            !!!nack ('t392.1');
4848          !!!next-token;          !!!next-token;
4849          if ($token->{type} == CHARACTER_TOKEN) {          next B;
4850            $token->{data} =~ s/^\x0A//;        } elsif ($token->{tag_name} eq 'optgroup' or
4851            unless (length $token->{data}) {                 $token->{tag_name} eq 'option') {
4852              !!!cp ('t392');          ## has an |option| element in scope
4853              !!!next-token;          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4854            } else {            my $node = $self->{open_elements}->[$_];
4855              !!!cp ('t393');            if ($node->[1] == OPTION_EL) {
4856                !!!cp ('t397.1');
4857                ## NOTE: As if </option>
4858                !!!back-token; # <option> or <optgroup>
4859                $token = {type => END_TAG_TOKEN, tag_name => 'option',
4860                          line => $token->{line}, column => $token->{column}};
4861                next B;
4862              } elsif ($node->[1] & SCOPING_EL) {
4863                !!!cp ('t397.2');
4864                last INSCOPE;
4865            }            }
4866          } else {          } # INSCOPE
4867            !!!cp ('t394');  
4868          }          $reconstruct_active_formatting_elements->($insert_to_current);
4869          while ($token->{type} == CHARACTER_TOKEN) {  
4870            !!!cp ('t395');          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4871            $text .= $token->{data};  
4872            !!!next-token;          !!!nack ('t397.3');
         }  
         if (length $text) {  
           !!!cp ('t396');  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model} = PCDATA_CONTENT_MODEL;  
           
         if ($token->{type} == END_TAG_TOKEN and  
             $token->{tag_name} eq $tag_name) {  
           !!!cp ('t397');  
           ## Ignore the token  
         } else {  
           !!!cp ('t398');  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
         }  
4873          !!!next-token;          !!!next-token;
4874          redo B;          redo B;
4875        } elsif ({        } elsif ($token->{tag_name} eq 'rt' or
4876                  iframe => 1,                 $token->{tag_name} eq 'rp') {
4877                  noembed => 1,          ## has a |ruby| element in scope
4878                  noframes => 1,          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4879                  noscript => 0, ## TODO: 1 if scripting is enabled            my $node = $self->{open_elements}->[$_];
4880                 }->{$token->{tag_name}}) {            if ($node->[1] == RUBY_EL) {
4881          !!!cp ('t399');              !!!cp ('t398.1');
4882          ## NOTE: There is an "as if in body" code clone.              ## generate implied end tags
4883          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4884                  !!!cp ('t398.2');
4885                  pop @{$self->{open_elements}};
4886                }
4887                unless ($self->{open_elements}->[-1]->[1] == RUBY_EL) {
4888                  !!!cp ('t398.3');
4889                  !!!parse-error (type => 'not closed',
4890                                  text => $self->{open_elements}->[-1]->[0]
4891                                      ->manakai_local_name,
4892                                  token => $token);
4893                  pop @{$self->{open_elements}}
4894                      while not $self->{open_elements}->[-1]->[1] == RUBY_EL;
4895                }
4896                last INSCOPE;
4897              } elsif ($node->[1] & SCOPING_EL) {
4898                !!!cp ('t398.4');
4899                last INSCOPE;
4900              }
4901            } # INSCOPE
4902    
4903            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4904    
4905            !!!nack ('t398.5');
4906            !!!next-token;
4907          redo B;          redo B;
4908        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'math' or
4909          !!!cp ('t400');                 $token->{tag_name} eq 'svg') {
4910          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
4911    
4912          ## TODO: associate with $self->{form_element} if defined          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
4913    
4914            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
4915    
4916            ## "adjust foreign attributes" - done in insert-element-f
4917            
4918            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
4919                    
4920          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
4921              pop @{$self->{open_elements}};
4922              !!!ack ('t398.6');
4923            } else {
4924              !!!cp ('t398.7');
4925              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
4926              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
4927              ## mode, "in body" (not "in foreign content") secondary insertion
4928              ## mode, maybe.
4929            }
4930    
4931          !!!next-token;          !!!next-token;
4932          redo B;          next B;
4933        } elsif ({        } elsif ({
4934                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
4935                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1,
4936                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
4937                  thead => 1, tr => 1,                  thead => 1, tr => 1,
4938                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
4939          !!!cp ('t401');          !!!cp ('t401');
4940          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body',
4941                            text => $token->{tag_name}, token => $token);
4942          ## Ignore the token          ## Ignore the token
4943            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
4944            !!!next-token;
4945            next B;
4946          } elsif ($token->{tag_name} eq 'param' or
4947                   $token->{tag_name} eq 'source') {
4948            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4949            pop @{$self->{open_elements}};
4950    
4951            !!!ack ('t398.5');
4952          !!!next-token;          !!!next-token;
4953          redo B;          redo B;
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
4954        } else {        } else {
4955          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
4956              !!!cp ('t384');
4957              !!!parse-error (type => 'image', token => $token);
4958              $token->{tag_name} = 'img';
4959            } else {
4960              !!!cp ('t385');
4961            }
4962    
4963            ## NOTE: There is an "as if <br>" code clone.
4964          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4965                    
4966          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4967    
4968            if ({
4969                 applet => 1, marquee => 1, object => 1,
4970                }->{$token->{tag_name}}) {
4971              !!!cp ('t380');
4972              push @$active_formatting_elements, ['#marker', ''];
4973              !!!nack ('t380.1');
4974            } elsif ({
4975                      b => 1, big => 1, em => 1, font => 1, i => 1,
4976                      s => 1, small => 1, strike => 1,
4977                      strong => 1, tt => 1, u => 1,
4978                     }->{$token->{tag_name}}) {
4979              !!!cp ('t375');
4980              push @$active_formatting_elements, $self->{open_elements}->[-1];
4981              !!!nack ('t375.1');
4982            } elsif ($token->{tag_name} eq 'input') {
4983              !!!cp ('t388');
4984              ## TODO: associate with $self->{form_element} if defined
4985              pop @{$self->{open_elements}};
4986              !!!ack ('t388.2');
4987            } elsif ({
4988                      area => 1, basefont => 1, bgsound => 1, br => 1,
4989                      embed => 1, img => 1, spacer => 1, wbr => 1,
4990                     }->{$token->{tag_name}}) {
4991              !!!cp ('t388.1');
4992              pop @{$self->{open_elements}};
4993              !!!ack ('t388.3');
4994            } elsif ($token->{tag_name} eq 'select') {
4995              ## TODO: associate with $self->{form_element} if defined
4996            
4997              if ($self->{insertion_mode} & TABLE_IMS or
4998                  $self->{insertion_mode} & BODY_TABLE_IMS or
4999                  ($self->{insertion_mode} & IM_MASK) == IN_COLUMN_GROUP_IM) {
5000                !!!cp ('t400.1');
5001                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
5002              } else {
5003                !!!cp ('t400.2');
5004                $self->{insertion_mode} = IN_SELECT_IM;
5005              }
5006              !!!nack ('t400.3');
5007            } else {
5008              !!!nack ('t402');
5009            }
5010                    
5011          !!!next-token;          !!!next-token;
5012          redo B;          next B;
5013        }        }
5014      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
5015        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
5016          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
5017              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
5018            for (@{$self->{open_elements}}) {          INSCOPE: {
5019              unless ({            for (reverse @{$self->{open_elements}}) {
5020                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] == BODY_EL) {
5021                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
5022                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
5023                      }->{$_->[1]}) {                last INSCOPE;
5024                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
5025                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
5026              } else {                last;
               !!!cp ('t404');  
5027              }              }
5028            }            }
5029    
5030            $self->{insertion_mode} = AFTER_BODY_IM;            ## NOTE: |<marquee></body>|, |<svg><foreignobject></body>|
5031            !!!next-token;  
5032            redo B;            !!!parse-error (type => 'unmatched end tag',
5033          } else {                            text => $token->{tag_name}, token => $token);
5034            !!!cp ('t405');            ## NOTE: Ignore the token.
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
5035            !!!next-token;            !!!next-token;
5036            redo B;            next B;
5037            } # INSCOPE
5038    
5039            for (@{$self->{open_elements}}) {
5040              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
5041                !!!cp ('t403');
5042                !!!parse-error (type => 'not closed',
5043                                text => $_->[0]->manakai_local_name,
5044                                token => $token);
5045                last;
5046              } else {
5047                !!!cp ('t404');
5048              }
5049          }          }
5050    
5051            $self->{insertion_mode} = AFTER_BODY_IM;
5052            !!!next-token;
5053            next B;
5054        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
5055          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          ## TODO: Update this code.  It seems that the code below is not
5056            ## ISSUE: There is an issue in the spec.          ## up-to-date, though it has same effect as speced.
5057            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if (@{$self->{open_elements}} > 1 and
5058                $self->{open_elements}->[1]->[1] == BODY_EL) {
5059              unless ($self->{open_elements}->[-1]->[1] == BODY_EL) {
5060              !!!cp ('t406');              !!!cp ('t406');
5061              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
5062                                text => $self->{open_elements}->[1]->[0]
5063                                    ->manakai_local_name,
5064                                token => $token);
5065            } else {            } else {
5066              !!!cp ('t407');              !!!cp ('t407');
5067            }            }
5068            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5069            ## reprocess            ## reprocess
5070            redo B;            next B;
5071          } else {          } else {
5072            !!!cp ('t408');            !!!cp ('t408');
5073            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
5074                              text => $token->{tag_name}, token => $token);
5075            ## Ignore the token            ## Ignore the token
5076            !!!next-token;            !!!next-token;
5077            redo B;            next B;
5078          }          }
5079        } elsif ({        } elsif ({
5080                  address => 1, blockquote => 1, center => 1, dir => 1,                  ## NOTE: End tags for non-phrasing flow content elements
5081                  div => 1, dl => 1, fieldset => 1, listing => 1,  
5082                  menu => 1, ol => 1, pre => 1, ul => 1,                  ## NOTE: The normal ones
5083                  p => 1,                  address => 1, article => 1, aside => 1, blockquote => 1,
5084                    center => 1, datagrid => 1, details => 1, dialog => 1,
5085                    dir => 1, div => 1, dl => 1, fieldset => 1, figure => 1,
5086                    footer => 1, header => 1, listing => 1, menu => 1, nav => 1,
5087                    ol => 1, pre => 1, section => 1, ul => 1,
5088    
5089                    ## NOTE: As normal, but ... optional tags
5090                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
5091                  button => 1, marquee => 1, object => 1,  
5092                    applet => 1, button => 1, marquee => 1, object => 1,
5093                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5094            ## NOTE: Code for <li> start tags includes "as if </li>" code.
5095            ## Code for <dt> or <dd> start tags includes "as if </dt> or
5096            ## </dd>" code.
5097    
5098          ## has an element in scope          ## has an element in scope
5099          my $i;          my $i;
5100          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5101            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5102            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
             ## generate implied end tags  
             while ({  
                     dd => ($token->{tag_name} ne 'dd'),  
                     dt => ($token->{tag_name} ne 'dt'),  
                     li => ($token->{tag_name} ne 'li'),  
                     p => ($token->{tag_name} ne 'p'),  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t409');  
               pop @{$self->{open_elements}};  
             }  
               
5103              !!!cp ('t410');              !!!cp ('t410');
5104              $i = $_;              $i = $_;
5105              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
5106            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
5107              !!!cp ('t411');              !!!cp ('t411');
5108              last INSCOPE;              last INSCOPE;
5109            }            }
5110          } # INSCOPE          } # INSCOPE
5111            
5112          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
5113            if (defined $i) {            !!!cp ('t413');
5114              !!!parse-error (type => 'unmatched end tag',
5115                              text => $token->{tag_name}, token => $token);
5116              ## NOTE: Ignore the token.
5117            } else {
5118              ## Step 1. generate implied end tags
5119              while ({
5120                      ## END_TAG_OPTIONAL_EL
5121                      dd => ($token->{tag_name} ne 'dd'),
5122                      dt => ($token->{tag_name} ne 'dt'),
5123                      li => ($token->{tag_name} ne 'li'),
5124                      option => 1,
5125                      optgroup => 1,
5126                      p => 1,
5127                      rt => 1,
5128                      rp => 1,
5129                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
5130                !!!cp ('t409');
5131                pop @{$self->{open_elements}};
5132              }
5133    
5134              ## Step 2.
5135              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5136                      ne $token->{tag_name}) {
5137              !!!cp ('t412');              !!!cp ('t412');
5138              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
5139                                text => $self->{open_elements}->[-1]->[0]
5140                                    ->manakai_local_name,
5141                                token => $token);
5142            } else {            } else {
5143              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5144            }            }
5145          }  
5146                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
5147            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5148          } elsif ($token->{tag_name} eq 'p') {  
5149            !!!cp ('t415');            ## Step 4.
5150            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
5151            my $el;                if {
5152            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
5153            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
5154          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
5155          !!!next-token;          !!!next-token;
5156          redo B;          next B;
5157        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
5158            ## NOTE: As normal, but interacts with the form element pointer
5159    
5160            undef $self->{form_element};
5161    
5162          ## has an element in scope          ## has an element in scope
5163            my $i;
5164          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5165            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5166            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] == FORM_EL) {
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               pop @{$self->{open_elements}};  
             }  
   
5167              !!!cp ('t418');              !!!cp ('t418');
5168                $i = $_;
5169              last INSCOPE;              last INSCOPE;
5170            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
5171              !!!cp ('t419');              !!!cp ('t419');
5172              last INSCOPE;              last INSCOPE;
5173            }            }
5174          } # INSCOPE          } # INSCOPE
5175            
5176          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
           !!!cp ('t420');  
           pop @{$self->{open_elements}};  
         } else {  
5177            !!!cp ('t421');            !!!cp ('t421');
5178            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
5179                              text => $token->{tag_name}, token => $token);
5180              ## NOTE: Ignore the token.
5181            } else {
5182              ## Step 1. generate implied end tags
5183              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5184                !!!cp ('t417');
5185                pop @{$self->{open_elements}};
5186              }
5187              
5188              ## Step 2.
5189              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5190                      ne $token->{tag_name}) {
5191                !!!cp ('t417.1');
5192                !!!parse-error (type => 'not closed',
5193                                text => $self->{open_elements}->[-1]->[0]
5194                                    ->manakai_local_name,
5195                                token => $token);
5196              } else {
5197                !!!cp ('t420');
5198              }  
5199              
5200              ## Step 3.
5201              splice @{$self->{open_elements}}, $i;
5202          }          }
5203    
         undef $self->{form_element};  
5204          !!!next-token;          !!!next-token;
5205          redo B;          next B;
5206        } elsif ({        } elsif ({
5207                    ## NOTE: As normal, except acts as a closer for any ...
5208                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5209                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5210          ## has an element in scope          ## has an element in scope
5211          my $i;          my $i;
5212          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5213            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5214            if ({            if ($node->[1] == HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               pop @{$self->{open_elements}};  
             }  
   
5215              !!!cp ('t423');              !!!cp ('t423');
5216              $i = $_;              $i = $_;
5217              last INSCOPE;              last INSCOPE;
5218            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
5219              !!!cp ('t424');              !!!cp ('t424');
5220              last INSCOPE;              last INSCOPE;
5221            }            }
5222          } # INSCOPE          } # INSCOPE
5223    
5224            unless (defined $i) { # has an element in scope
5225              !!!cp ('t425.1');
5226              !!!parse-error (type => 'unmatched end tag',
5227                              text => $token->{tag_name}, token => $token);
5228              ## NOTE: Ignore the token.
5229            } else {
5230              ## Step 1. generate implied end tags
5231              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5232                !!!cp ('t422');
5233                pop @{$self->{open_elements}};
5234              }
5235              
5236              ## Step 2.
5237              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5238                      ne $token->{tag_name}) {
5239                !!!cp ('t425');
5240                !!!parse-error (type => 'unmatched end tag',
5241                                text => $token->{tag_name}, token => $token);
5242              } else {
5243                !!!cp ('t426');
5244              }
5245    
5246              ## Step 3.
5247              splice @{$self->{open_elements}}, $i;
5248            }
5249                    
5250          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          !!!next-token;
5251            !!!cp ('t425');          next B;
5252            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});        } elsif ($token->{tag_name} eq 'p') {
5253            ## NOTE: As normal, except </p> implies <p> and ...
5254    
5255            ## has an element in scope
5256            my $non_optional;
5257            my $i;
5258            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5259              my $node = $self->{open_elements}->[$_];
5260              if ($node->[1] == P_EL) {
5261                !!!cp ('t410.1');
5262                $i = $_;
5263                last INSCOPE;
5264              } elsif ($node->[1] & SCOPING_EL) {
5265                !!!cp ('t411.1');
5266                last INSCOPE;
5267              } elsif ($node->[1] & END_TAG_OPTIONAL_EL) {
5268                ## NOTE: |END_TAG_OPTIONAL_EL| includes "p"
5269                !!!cp ('t411.2');
5270                #
5271              } else {
5272                !!!cp ('t411.3');
5273                $non_optional ||= $node;
5274                #
5275              }
5276            } # INSCOPE
5277    
5278            if (defined $i) {
5279              ## 1. Generate implied end tags
5280              #
5281    
5282              ## 2. If current node != "p", parse error
5283              if ($non_optional) {
5284                !!!cp ('t412.1');
5285                !!!parse-error (type => 'not closed',
5286                                text => $non_optional->[0]->manakai_local_name,
5287                                token => $token);
5288              } else {
5289                !!!cp ('t414.1');
5290              }
5291    
5292              ## 3. Pop
5293              splice @{$self->{open_elements}}, $i;
5294          } else {          } else {
5295            !!!cp ('t426');            !!!cp ('t413.1');
5296              !!!parse-error (type => 'unmatched end tag',
5297                              text => $token->{tag_name}, token => $token);
5298    
5299              !!!cp ('t415.1');
5300              ## As if <p>, then reprocess the current token
5301              my $el;
5302              !!!create-element ($el, $HTML_NS, 'p',, $token);
5303              $insert->($el);
5304              ## NOTE: Not inserted into |$self->{open_elements}|.
5305          }          }
5306            
         splice @{$self->{open_elements}}, $i if defined $i;  
5307          !!!next-token;          !!!next-token;
5308          redo B;          next B;
5309        } elsif ({        } elsif ({
5310                  a => 1,                  a => 1,
5311                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
5312                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strike => 1,
5313                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5314                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5315          !!!cp ('t427');          !!!cp ('t427');
5316          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
5317          redo B;          next B;
5318        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
5319          !!!cp ('t428');          !!!cp ('t428');
5320          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag',
5321                            text => 'br', token => $token);
5322    
5323          ## As if <br>          ## As if <br>
5324          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5325                    
5326          my $el;          my $el;
5327          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
5328          $insert->($el);          $insert->($el);
5329                    
5330          ## Ignore the token.          ## Ignore the token.
5331          !!!next-token;          !!!next-token;
5332          redo B;          next B;
       } 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,  
                 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}}) {  
         !!!cp ('t429');  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
5333        } else {        } else {
5334            if ($token->{tag_name} eq 'sarcasm') {
5335              sleep 0.001; # take a deep breath
5336            }
5337    
5338          ## Step 1          ## Step 1
5339          my $node_i = -1;          my $node_i = -1;
5340          my $node = $self->{open_elements}->[$node_i];          my $node = $self->{open_elements}->[$node_i];
5341    
5342          ## Step 2          ## Step 2
5343          S2: {          S2: {
5344            if ($node->[1] eq $token->{tag_name}) {            my $node_tag_name = $node->[0]->manakai_local_name;
5345              $node_tag_name =~ tr/A-Z/a-z/; # for SVG camelCase tag names
5346              if ($node_tag_name eq $token->{tag_name}) {
5347              ## Step 1              ## Step 1
5348              ## generate implied end tags              ## generate implied end tags
5349              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
5350                !!!cp ('t430');                !!!cp ('t430');
5351                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
5352                  ## ISSUE: <ruby><rt></rt> will also take this code path,
5353                  ## which seems wrong.
5354                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5355                  $node_i++;
5356              }              }
5357                    
5358              ## Step 2              ## Step 2
5359              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              my $current_tag_name
5360                    = $self->{open_elements}->[-1]->[0]->manakai_local_name;
5361                $current_tag_name =~ tr/A-Z/a-z/;
5362                if ($current_tag_name ne $token->{tag_name}) {
5363                !!!cp ('t431');                !!!cp ('t431');
5364                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
5365                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5366                                  text => $self->{open_elements}->[-1]->[0]
5367                                      ->manakai_local_name,
5368                                  token => $token);
5369              } else {              } else {
5370                !!!cp ('t432');                !!!cp ('t432');
5371              }              }
5372                            
5373              ## Step 3              ## Step 3
5374              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
5375    
5376              !!!next-token;              !!!next-token;
5377              last S2;              last S2;
5378            } else {            } else {
5379              ## Step 3              ## Step 3
5380              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
5381                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
5382                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
5383                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
5384                !!!cp ('t433');                !!!cp ('t433');
5385                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5386                                  text => $token->{tag_name}, token => $token);
5387                ## Ignore the token                ## Ignore the token
5388                !!!next-token;                !!!next-token;
5389                last S2;                last S2;
             }  
5390    
5391                  ## NOTE: |<span><dd></span>a|: In Safari 3.1.2 and Opera
5392                  ## 9.27, "a" is a child of <dd> (conforming).  In
5393                  ## Firefox 3.0.2, "a" is a child of <body>.  In WinIE 7,
5394                  ## "a" is a child of both <body> and <dd>.
5395                }
5396                
5397              !!!cp ('t434');              !!!cp ('t434');
5398            }            }
5399                        
# Line 6222  sub _tree_construction_main ($) { Line 5404  sub _tree_construction_main ($) {
5404            ## Step 5;            ## Step 5;
5405            redo S2;            redo S2;
5406          } # S2          } # S2
5407          redo B;          next B;
5408        }        }
5409      }      }
5410      redo B;      next B;
5411      } continue { # B
5412        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
5413          ## NOTE: The code below is executed in cases where it does not have
5414          ## to be, but it it is harmless even in those cases.
5415          ## has an element in scope
5416          INSCOPE: {
5417            for (reverse 0..$#{$self->{open_elements}}) {
5418              my $node = $self->{open_elements}->[$_];
5419              if ($node->[1] & FOREIGN_EL) {
5420                last INSCOPE;
5421              } elsif ($node->[1] & SCOPING_EL) {
5422                last;
5423              }
5424            }
5425            
5426            ## NOTE: No foreign element in scope.
5427            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
5428          } # INSCOPE
5429        }
5430    } # B    } # B
5431    
5432    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6233  sub _tree_construction_main ($) { Line 5434  sub _tree_construction_main ($) {
5434    ## TODO: script stuffs    ## TODO: script stuffs
5435  } # _tree_construct_main  } # _tree_construct_main
5436    
5437  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
5438    my $class = shift;    my $class = shift;
5439    my $node = shift;    my $node = shift;
5440    my $s = \$_[0];    #my $s = \$_[0];
5441    my $onerror = $_[1];    my $onerror = $_[1];
5442      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
5443    
5444    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
5445    
# Line 6256  sub set_inner_html ($$$) { Line 5458  sub set_inner_html ($$$) {
5458      }      }
5459    
5460      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
5461      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
5462    } elsif ($nt == 1) {    } elsif ($nt == 1) {
5463      ## TODO: If non-html element      ## TODO: If non-html element
5464    
5465      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
5466    
5467    ## TODO: Support for $get_wrapper
5468    
5469      ## Step 1 # MUST      ## Step 1 # MUST
5470      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
5471      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 6271  sub set_inner_html ($$$) { Line 5475  sub set_inner_html ($$$) {
5475    
5476      ## Step 8 # MUST      ## Step 8 # MUST
5477      my $i = 0;      my $i = 0;
5478      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
5479      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
5480      $p->{set_next_char} = sub {      require Whatpm::Charset::DecodeHandle;
5481        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
5482        $input = $get_wrapper->($input);
5483        $p->{set_nc} = sub {
5484        my $self = shift;        my $self = shift;
5485    
5486        pop @{$self->{prev_char}};        my $char = '';
5487        unshift @{$self->{prev_char}}, $self->{next_char};        if (defined $self->{next_nc}) {
5488            $char = $self->{next_nc};
5489            delete $self->{next_nc};
5490            $self->{nc} = ord $char;
5491          } else {
5492            $self->{char_buffer} = '';
5493            $self->{char_buffer_pos} = 0;
5494            
5495            my $count = $input->manakai_read_until
5496                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
5497                 $self->{char_buffer_pos});
5498            if ($count) {
5499              $self->{line_prev} = $self->{line};
5500              $self->{column_prev} = $self->{column};
5501              $self->{column}++;
5502              $self->{nc}
5503                  = ord substr ($self->{char_buffer},
5504                                $self->{char_buffer_pos}++, 1);
5505              return;
5506            }
5507            
5508            if ($input->read ($char, 1)) {
5509              $self->{nc} = ord $char;
5510            } else {
5511              $self->{nc} = -1;
5512              return;
5513            }
5514          }
5515    
5516        $self->{next_char} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
5517        $self->{next_char} = ord substr $$s, $i++, 1;        $p->{column}++;
5518        $column++;  
5519          if ($self->{nc} == 0x000A) { # LF
5520        if ($self->{next_char} == 0x000A) { # LF          $p->{line}++;
5521          $line++;          $p->{column} = 0;
         $column = 0;  
5522          !!!cp ('i1');          !!!cp ('i1');
5523        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{nc} == 0x000D) { # CR
5524          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
5525          $self->{next_char} = 0x000A; # LF # MUST          my $next = '';
5526          $line++;          if ($input->read ($next, 1) and $next ne "\x0A") {
5527          $column = 0;            $self->{next_nc} = $next;
5528            }
5529            $self->{nc} = 0x000A; # LF # MUST
5530            $p->{line}++;
5531            $p->{column} = 0;
5532          !!!cp ('i2');          !!!cp ('i2');
5533        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{nc} == 0x0000) { # NULL
         $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
         !!!cp ('i3');  
       } elsif ($self->{next_char} == 0x0000) { # NULL  
5534          !!!cp ('i4');          !!!cp ('i4');
5535          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
5536          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5537        }        }
5538      };      };
5539      $p->{prev_char} = [-1, -1, -1];  
5540      $p->{next_char} = -1;      $p->{read_until} = sub {
5541              #my ($scalar, $specials_range, $offset) = @_;
5542          return 0 if defined $p->{next_nc};
5543    
5544          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
5545          my $offset = $_[2] || 0;
5546          
5547          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
5548            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
5549            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
5550              substr ($_[0], $offset)
5551                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
5552              my $count = $+[0] - $-[0];
5553              if ($count) {
5554                $p->{column} += $count;
5555                $p->{char_buffer_pos} += $count;
5556                $p->{line_prev} = $p->{line};
5557                $p->{column_prev} = $p->{column} - 1;
5558                $p->{nc} = -1;
5559              }
5560              return $count;
5561            } else {
5562              return 0;
5563            }
5564          } else {
5565            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
5566            if ($count) {
5567              $p->{column} += $count;
5568              $p->{column_prev} += $count;
5569              $p->{nc} = -1;
5570            }
5571            return $count;
5572          }
5573        }; # $p->{read_until}
5574    
5575      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
5576        my (%opt) = @_;        my (%opt) = @_;
5577        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
5578          my $column = $opt{column};
5579          if (defined $opt{token} and defined $opt{token}->{line}) {
5580            $line = $opt{token}->{line};
5581            $column = $opt{token}->{column};
5582          }
5583          warn "Parse error ($opt{type}) at line $line column $column\n";
5584      };      };
5585      $p->{parse_error} = sub {      $p->{parse_error} = sub {
5586        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
5587      };      };
5588            
5589        my $char_onerror = sub {
5590          my (undef, $type, %opt) = @_;
5591          $ponerror->(layer => 'encode',
5592                      line => $p->{line}, column => $p->{column} + 1,
5593                      %opt, type => $type);
5594        }; # $char_onerror
5595        $input->onerror ($char_onerror);
5596    
5597      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
5598      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
5599    
# Line 6334  sub set_inner_html ($$$) { Line 5615  sub set_inner_html ($$$) {
5615          unless defined $p->{content_model};          unless defined $p->{content_model};
5616          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
5617    
5618      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
5619          ## TODO: Foreign element OK?
5620    
5621      ## Step 3      ## Step 3
5622      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6344  sub set_inner_html ($$$) { Line 5626  sub set_inner_html ($$$) {
5626      $doc->append_child ($root);      $doc->append_child ($root);
5627    
5628      ## Step 5 # MUST      ## Step 5 # MUST
5629      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
5630    
5631      undef $p->{head_element};      undef $p->{head_element};
5632        undef $p->{head_element_inserted};
5633    
5634      ## Step 6 # MUST      ## Step 6 # MUST
5635      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
# Line 6390  sub set_inner_html ($$$) { Line 5673  sub set_inner_html ($$$) {
5673      ## ISSUE: mutation events?      ## ISSUE: mutation events?
5674    
5675      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
5676    
5677        delete $p->{parse_error}; # delete loop
5678    } else {    } else {
5679      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";
5680    }    }

Legend:
Removed from v.1.86  
changed lines
  Added in v.1.210

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24