/[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.65 by wakaba, Mon Nov 19 12:18:26 2007 UTC revision 1.153 by wakaba, Fri Aug 15 08:32:41 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  require IO::Handle;
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  
13  ## is not yet clear.  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  ## "{U+FEFF}..." in GB18030?  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17  my $permitted_slash_tag_name = {  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    base => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    link => 1,  
20    meta => 1,  sub A_EL () { 0b1 }
21    hr => 1,  sub ADDRESS_EL () { 0b10 }
22    br => 1,  sub BODY_EL () { 0b100 }
23    img=> 1,  sub BUTTON_EL () { 0b1000 }
24    embed => 1,  sub CAPTION_EL () { 0b10000 }
25    param => 1,  sub DD_EL () { 0b100000 }
26    area => 1,  sub DIV_EL () { 0b1000000 }
27    col => 1,  sub DT_EL () { 0b10000000 }
28    input => 1,  sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296  };  };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 63  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
 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,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
350  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$) {
358    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
359    my $charset = shift;    my $charset_name = shift;
360    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;  
   }  
361    
362    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
363      my $self = shift;      my (%opt) = @_;
364      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
365      ## TODO: if $charset is supported    };
366      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
367    
368      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
369      require Message::Charset::Info;
370      ## Step 1        my $charset;
371      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?    my $buffer;
372        $charset = 'utf-8';    my ($char_stream, $e_status);
373    
374      SNIFFING: {
375    
376        ## Step 1
377        if (defined $charset_name) {
378          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
379    
380          ## ISSUE: Unsupported encoding is not ignored according to the spec.
381          ($char_stream, $e_status) = $charset->get_decode_handle
382              ($byte_stream, allow_error_reporting => 1,
383               allow_fallback => 1);
384          if ($char_stream) {
385            $self->{confident} = 1;
386            last SNIFFING;
387          } else {
388            ## TODO: unsupported error
389          }
390      }      }
391    
392      ## Step 2      ## Step 2
393      if (defined $self->{input_encoding} and      my $byte_buffer = '';
394          $self->{input_encoding} eq $charset) {      for (1..1024) {
395          my $char = $byte_stream->getc;
396          last unless defined $char;
397          $byte_buffer .= $char;
398        } ## TODO: timeout
399    
400        ## Step 3
401        if ($byte_buffer =~ /^\xFE\xFF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
403          ($char_stream, $e_status) = $charset->get_decode_handle
404              ($byte_stream, allow_error_reporting => 1,
405               allow_fallback => 1, byte_buffer => \$byte_buffer);
406        $self->{confident} = 1;        $self->{confident} = 1;
407        return;        last SNIFFING;
408        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
409          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
410          ($char_stream, $e_status) = $charset->get_decode_handle
411              ($byte_stream, allow_error_reporting => 1,
412               allow_fallback => 1, byte_buffer => \$byte_buffer);
413          $self->{confident} = 1;
414          last SNIFFING;
415        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
416          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
417          ($char_stream, $e_status) = $charset->get_decode_handle
418              ($byte_stream, allow_error_reporting => 1,
419               allow_fallback => 1, byte_buffer => \$byte_buffer);
420          $self->{confident} = 1;
421          last SNIFFING;
422        }
423    
424        ## Step 4
425        ## TODO: <meta charset>
426    
427        ## Step 5
428        ## TODO: from history
429    
430        ## Step 6
431        require Whatpm::Charset::UniversalCharDet;
432        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
433            ($byte_buffer);
434        if (defined $charset_name) {
435          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
436    
437          ## ISSUE: Unsupported encoding is not ignored according to the spec.
438          require Whatpm::Charset::DecodeHandle;
439          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
440              ($byte_stream);
441          ($char_stream, $e_status) = $charset->get_decode_handle
442              ($buffer, allow_error_reporting => 1,
443               allow_fallback => 1, byte_buffer => \$byte_buffer);
444          if ($char_stream) {
445            $buffer->{buffer} = $byte_buffer;
446            !!!parse-error (type => 'sniffing:chardet',
447                            text => $charset_name,
448                            level => $self->{level}->{info},
449                            layer => 'encode',
450                            line => 1, column => 1);
451            $self->{confident} = 0;
452            last SNIFFING;
453          }
454      }      }
455    
456      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 7: default
457          ':'.$charset, level => 'w');      ## TODO: Make this configurable.
458        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
459            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
460            ## detectable in the step 6.
461        require Whatpm::Charset::DecodeHandle;
462        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
463            ($byte_stream);
464        ($char_stream, $e_status)
465            = $charset->get_decode_handle ($buffer,
466                                           allow_error_reporting => 1,
467                                           allow_fallback => 1,
468                                           byte_buffer => \$byte_buffer);
469        $buffer->{buffer} = $byte_buffer;
470        !!!parse-error (type => 'sniffing:default',
471                        text => 'windows-1252',
472                        level => $self->{level}->{info},
473                        line => 1, column => 1,
474                        layer => 'encode');
475        $self->{confident} = 0;
476      } # SNIFFING
477    
478      ## Step 3    $self->{input_encoding} = $charset->get_iana_name;
479      # if (can) {    if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
480        ## change the encoding on the fly.      !!!parse-error (type => 'chardecode:fallback',
481        #$self->{confident} = 1;                      text => $self->{input_encoding},
482        #return;                      level => $self->{level}->{uncertain},
483      # }                      line => 1, column => 1,
484                        layer => 'encode');
485      } elsif (not ($e_status &
486                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
487        !!!parse-error (type => 'chardecode:no error',
488                        text => $self->{input_encoding},
489                        level => $self->{level}->{uncertain},
490                        line => 1, column => 1,
491                        layer => 'encode');
492      }
493    
494      ## Step 4    $self->{change_encoding} = sub {
495      throw Whatpm::HTML::RestartParser (charset => $charset);      my $self = shift;
496        $charset_name = shift;
497        my $token = shift;
498    
499        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
500        ($char_stream, $e_status) = $charset->get_decode_handle
501            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
502             byte_buffer => \ $buffer->{buffer});
503        
504        if ($char_stream) { # if supported
505          ## "Change the encoding" algorithm:
506    
507          ## Step 1    
508          if ($charset->{category} &
509              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
510            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
511            ($char_stream, $e_status) = $charset->get_decode_handle
512                ($byte_stream,
513                 byte_buffer => \ $buffer->{buffer});
514          }
515          $charset_name = $charset->get_iana_name;
516          
517          ## Step 2
518          if (defined $self->{input_encoding} and
519              $self->{input_encoding} eq $charset_name) {
520            !!!parse-error (type => 'charset label:matching',
521                            text => $charset_name,
522                            level => $self->{level}->{info});
523            $self->{confident} = 1;
524            return;
525          }
526    
527          !!!parse-error (type => 'charset label detected',
528                          text => $self->{input_encoding},
529                          value => $charset_name,
530                          level => $self->{level}->{warn},
531                          token => $token);
532          
533          ## Step 3
534          # if (can) {
535            ## change the encoding on the fly.
536            #$self->{confident} = 1;
537            #return;
538          # }
539          
540          ## Step 4
541          throw Whatpm::HTML::RestartParser ();
542        }
543    }; # $self->{change_encoding}    }; # $self->{change_encoding}
544    
545      my $char_onerror = sub {
546        my (undef, $type, %opt) = @_;
547        !!!parse-error (layer => 'encode',
548                        %opt, type => $type,
549                        line => $self->{line}, column => $self->{column} + 1);
550        if ($opt{octets}) {
551          ${$opt{octets}} = "\x{FFFD}"; # relacement character
552        }
553      };
554      $char_stream->onerror ($char_onerror);
555    
556    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
557    my $return;    my $return;
558    try {    try {
559      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
560    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
561      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
562      $s = \ (Encode::decode ($charset, $$bytes_s));      
563      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
564        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
565          !!!parse-error (type => 'chardecode:fallback',
566                          text => $self->{input_encoding},
567                          level => $self->{level}->{uncertain},
568                          line => 1, column => 1,
569                          layer => 'encode');
570        } elsif (not ($e_status &
571                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
572          !!!parse-error (type => 'chardecode:no error',
573                          text => $self->{input_encoding},
574                          level => $self->{level}->{uncertain},
575                          line => 1, column => 1,
576                          layer => 'encode');
577        }
578      $self->{confident} = 1;      $self->{confident} = 1;
579      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
580        $return = $self->parse_char_stream ($char_stream, @args);
581    };    };
582    return $return;    return $return;
583  } # parse_byte_string  } # parse_byte_stream
584    
585  *parse_char_string = \&parse_string;  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
586    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
587    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
588    ## because the core part of our HTML parser expects a string of character,
589    ## not a string of bytes or code units or anything which might contain a BOM.
590    ## Therefore, any parser interface that accepts a string of bytes,
591    ## such as |parse_byte_string| in this module, must ensure that it does
592    ## strip the BOM and never strip any ZWNBSP.
593    
594  sub parse_string ($$$;$) {  sub parse_char_string ($$$;$) {
595    my $self = ref $_[0] ? shift : shift->new;    my $self = shift;
596      require utf8;
597    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $s = ref $_[0] ? $_[0] : \($_[0]);
598      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
599      return $self->parse_char_stream ($input, @_[1..$#_]);
600    } # parse_char_string
601    *parse_string = \&parse_char_string;
602    
603    sub parse_char_stream ($$$;$) {
604      my $self = ref $_[0] ? shift : shift->new;
605      my $input = $_[0];
606    $self->{document} = $_[1];    $self->{document} = $_[1];
607    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
608    
# Line 170  sub parse_string ($$$;$) { Line 613  sub parse_string ($$$;$) {
613        if defined $self->{input_encoding};        if defined $self->{input_encoding};
614    
615    my $i = 0;    my $i = 0;
616    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
617    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
618    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
619      my $self = shift;      my $self = shift;
620    
621      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
622      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
623    
624      $self->{next_input_character} = -1 and return if $i >= length $$s;      my $char;
625      $self->{next_input_character} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
626      $column++;        $char = $self->{next_next_char};
627          delete $self->{next_next_char};
628        } else {
629          $char = $input->getc;
630        }
631        $self->{next_char} = -1 and return unless defined $char;
632        $self->{next_char} = ord $char;
633    
634        ($self->{line_prev}, $self->{column_prev})
635            = ($self->{line}, $self->{column});
636        $self->{column}++;
637            
638      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
639        $line++;        !!!cp ('j1');
640        $column = 0;        $self->{line}++;
641      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
642        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
643        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
644        $line++;        my $next = $input->getc;
645        $column = 0;        if (defined $next and $next ne "\x0A") {
646      } elsif ($self->{next_input_character} > 0x10FFFF) {          $self->{next_next_char} = $next;
647        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        }
648      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0x000A; # LF # MUST
649          $self->{line}++;
650          $self->{column} = 0;
651        } elsif ($self->{next_char} > 0x10FFFF) {
652          !!!cp ('j3');
653          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
654        } elsif ($self->{next_char} == 0x0000) { # NULL
655          !!!cp ('j4');
656        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
657        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
658        } elsif ($self->{next_char} <= 0x0008 or
659                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
660                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
661                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
662                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
663                 {
664                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
665                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
666                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
667                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
668                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
669                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
670                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
671                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
672                  0x10FFFE => 1, 0x10FFFF => 1,
673                 }->{$self->{next_char}}) {
674          !!!cp ('j5');
675          if ($self->{next_char} < 0x10000) {
676            !!!parse-error (type => 'control char',
677                            text => (sprintf 'U+%04X', $self->{next_char}));
678          } else {
679            !!!parse-error (type => 'control char',
680                            text => (sprintf 'U-%08X', $self->{next_char}));
681          }
682      }      }
683    };    };
684    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
685    $self->{next_input_character} = -1;    $self->{next_char} = -1;
686    
687    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
688      my (%opt) = @_;      my (%opt) = @_;
689      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
690        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
691        warn "Parse error ($opt{type}) at line $line column $column\n";
692    };    };
693    $self->{parse_error} = sub {    $self->{parse_error} = sub {
694      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
695    };    };
696    
697    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 213  sub parse_string ($$$;$) { Line 699  sub parse_string ($$$;$) {
699    $self->_construct_tree;    $self->_construct_tree;
700    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
701    
702      delete $self->{parse_error}; # remove loop
703    
704    return $self->{document};    return $self->{document};
705  } # parse_string  } # parse_char_stream
706    
707  sub new ($) {  sub new ($) {
708    my $class = shift;    my $class = shift;
709    my $self = bless {}, $class;    my $self = bless {
710    $self->{set_next_input_character} = sub {      level => {must => 'm',
711      $self->{next_input_character} = -1;                warn => 'w',
712                  info => 'i',
713                  uncertain => 'u'},
714      }, $class;
715      $self->{set_next_char} = sub {
716        $self->{next_char} = -1;
717    };    };
718    $self->{parse_error} = sub {    $self->{parse_error} = sub {
719      #      #
# Line 279  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO Line 772  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO
772  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
773  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
774  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
775    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
776    sub SELF_CLOSING_START_TAG_STATE () { 34 }
777    sub CDATA_BLOCK_STATE () { 35 }
778    
779  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
780  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 295  sub TABLE_IMS ()      { 0b1000000 } Line 791  sub TABLE_IMS ()      { 0b1000000 }
791  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
792  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
793  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
794    sub SELECT_IMS ()     { 0b10000000000 }
795    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
796        ## NOTE: "in foreign content" insertion mode is special; it is combined
797        ## with the secondary insertion mode.  In this parser, they are stored
798        ## together in the bit-or'ed form.
799    
800    ## NOTE: "initial" and "before html" insertion modes have no constants.
801    
802    ## NOTE: "after after body" insertion mode.
803  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
804    
805    ## NOTE: "after after frameset" insertion mode.
806  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
807    
808  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
809  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
810  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 311  sub IN_TABLE_IM () { TABLE_IMS } Line 818  sub IN_TABLE_IM () { TABLE_IMS }
818  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
819  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
820  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
821  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
822    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
823  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
824    
825  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 324  sub _initialize_tokenizer ($) { Line 832  sub _initialize_tokenizer ($) {
832    undef $self->{current_attribute};    undef $self->{current_attribute};
833    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
834    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
835      delete $self->{self_closing};
836    $self->{char} = [];    $self->{char} = [];
837    # $self->{next_input_character}    # $self->{next_char}
838    !!!next-input-character;    !!!next-input-character;
839    $self->{token} = [];    $self->{token} = [];
840    # $self->{escape}    # $self->{escape}
# Line 338  sub _initialize_tokenizer ($) { Line 847  sub _initialize_tokenizer ($) {
847  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
848  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
849  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
850  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
851  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
852    ##        ->{name}
853    ##        ->{value}
854    ##        ->{has_reference} == 1 or 0
855  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
856    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
857    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
858    ##     while the token is pushed back to the stack.
859    
860  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
861    
# Line 367  sub _initialize_tokenizer ($) { Line 882  sub _initialize_tokenizer ($) {
882    
883  sub _get_next_token ($) {  sub _get_next_token ($) {
884    my $self = shift;    my $self = shift;
885    
886      if ($self->{self_closing}) {
887        !!!parse-error (type => 'nestc', token => $self->{current_token});
888        ## NOTE: The |self_closing| flag is only set by start tag token.
889        ## In addition, when a start tag token is emitted, it is always set to
890        ## |current_token|.
891        delete $self->{self_closing};
892      }
893    
894    if (@{$self->{token}}) {    if (@{$self->{token}}) {
895        $self->{self_closing} = $self->{token}->[0]->{self_closing};
896      return shift @{$self->{token}};      return shift @{$self->{token}};
897    }    }
898    
899    A: {    A: {
900      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
901        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
902          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
903                not $self->{escape}) {
904              !!!cp (1);
905            $self->{state} = ENTITY_DATA_STATE;            $self->{state} = ENTITY_DATA_STATE;
906            !!!next-input-character;            !!!next-input-character;
907            redo A;            redo A;
908          } else {          } else {
909              !!!cp (2);
910            #            #
911          }          }
912        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
913          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
914            unless ($self->{escape}) {            unless ($self->{escape}) {
915              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
916                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
917                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
918                  !!!cp (3);
919                $self->{escape} = 1;                $self->{escape} = 1;
920                } else {
921                  !!!cp (4);
922              }              }
923              } else {
924                !!!cp (5);
925            }            }
926          }          }
927                    
928          #          #
929        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
930          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
931              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
932               not $self->{escape})) {               not $self->{escape})) {
933              !!!cp (6);
934            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
935            !!!next-input-character;            !!!next-input-character;
936            redo A;            redo A;
937          } else {          } else {
938              !!!cp (7);
939            #            #
940          }          }
941        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
942          if ($self->{escape} and          if ($self->{escape} and
943              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
944            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
945                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
946                !!!cp (8);
947              delete $self->{escape};              delete $self->{escape};
948              } else {
949                !!!cp (9);
950            }            }
951            } else {
952              !!!cp (10);
953          }          }
954                    
955          #          #
956        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
957          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
958            !!!emit ({type => END_OF_FILE_TOKEN,
959                      line => $self->{line}, column => $self->{column}});
960          last A; ## TODO: ok?          last A; ## TODO: ok?
961          } else {
962            !!!cp (12);
963        }        }
964        # Anything else        # Anything else
965        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
966                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
967                       line => $self->{line}, column => $self->{column},
968                      };
969        ## Stay in the data state        ## Stay in the data state
970        !!!next-input-character;        !!!next-input-character;
971    
# Line 428  sub _get_next_token ($) { Line 974  sub _get_next_token ($) {
974        redo A;        redo A;
975      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
976        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
977    
978          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
979                
980        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
981    
982        $self->{state} = DATA_STATE;        $self->{state} = DATA_STATE;
983        # next-input-character is already done        # next-input-character is already done
984    
985        unless (defined $token) {        unless (defined $token) {
986          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!cp (13);
987            !!!emit ({type => CHARACTER_TOKEN, data => '&',
988                      line => $l, column => $c,
989                     });
990        } else {        } else {
991            !!!cp (14);
992          !!!emit ($token);          !!!emit ($token);
993        }        }
994    
995        redo A;        redo A;
996      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
997        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
998          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
999              !!!cp (15);
1000            !!!next-input-character;            !!!next-input-character;
1001            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1002            redo A;            redo A;
1003          } else {          } else {
1004              !!!cp (16);
1005            ## reconsume            ## reconsume
1006            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1007    
1008            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1009                        line => $self->{line_prev},
1010                        column => $self->{column_prev},
1011                       });
1012    
1013            redo A;            redo A;
1014          }          }
1015        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1016          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1017              !!!cp (17);
1018            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1019            !!!next-input-character;            !!!next-input-character;
1020            redo A;            redo A;
1021          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1022              !!!cp (18);
1023            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1024            !!!next-input-character;            !!!next-input-character;
1025            redo A;            redo A;
1026          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1027                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1028              !!!cp (19);
1029            $self->{current_token}            $self->{current_token}
1030              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1031                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1032                   line => $self->{line_prev},
1033                   column => $self->{column_prev}};
1034            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1035            !!!next-input-character;            !!!next-input-character;
1036            redo A;            redo A;
1037          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1038                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1039              !!!cp (20);
1040            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1041                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
1042                                        line => $self->{line_prev},
1043                                        column => $self->{column_prev}};
1044            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1045            !!!next-input-character;            !!!next-input-character;
1046            redo A;            redo A;
1047          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1048            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1049              !!!parse-error (type => 'empty start tag',
1050                              line => $self->{line_prev},
1051                              column => $self->{column_prev});
1052            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1053            !!!next-input-character;            !!!next-input-character;
1054    
1055            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1056                        line => $self->{line_prev},
1057                        column => $self->{column_prev},
1058                       });
1059    
1060            redo A;            redo A;
1061          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1062            !!!parse-error (type => 'pio');            !!!cp (22);
1063              !!!parse-error (type => 'pio',
1064                              line => $self->{line_prev},
1065                              column => $self->{column_prev});
1066            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1067            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1068                                        line => $self->{line_prev},
1069                                        column => $self->{column_prev},
1070                                       };
1071              ## $self->{next_char} is intentionally left as is
1072            redo A;            redo A;
1073          } else {          } else {
1074            !!!parse-error (type => 'bare stago');            !!!cp (23);
1075              !!!parse-error (type => 'bare stago',
1076                              line => $self->{line_prev},
1077                              column => $self->{column_prev});
1078            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1079            ## reconsume            ## reconsume
1080    
1081            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1082                        line => $self->{line_prev},
1083                        column => $self->{column_prev},
1084                       });
1085    
1086            redo A;            redo A;
1087          }          }
# Line 505  sub _get_next_token ($) { Line 1089  sub _get_next_token ($) {
1089          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1090        }        }
1091      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1092          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1093        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1094          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1095    
1096            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1097            my @next_char;            my @next_char;
1098            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
1099              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1100              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1101              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1102              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1103                  !!!cp (24);
1104                !!!next-input-character;                !!!next-input-character;
1105                next TAGNAME;                next TAGNAME;
1106              } else {              } else {
1107                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
1108                  $self->{next_char} = shift @next_char; # reconsume
1109                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1110                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1111    
1112                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1113                            line => $l, column => $c,
1114                           });
1115        
1116                redo A;                redo A;
1117              }              }
1118            }            }
1119            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1120                
1121            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
1122                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
1123                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
1124                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
1125                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
1126                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
1127                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
1128                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
1129              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1130                $self->{next_char} = shift @next_char; # reconsume
1131              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1132              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1133              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1134                          line => $l, column => $c,
1135                         });
1136              redo A;              redo A;
1137            } else {            } else {
1138              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1139                $self->{next_char} = shift @next_char;
1140              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1141              # and consume...              # and consume...
1142            }            }
1143          } else {          } else {
1144            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1145              !!!cp (28);
1146            # next-input-character is already done            # next-input-character is already done
1147            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1148            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1149                        line => $l, column => $c,
1150                       });
1151            redo A;            redo A;
1152          }          }
1153        }        }
1154                
1155        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1156            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1157          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
1158                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1159                = {type => END_TAG_TOKEN,
1160                   tag_name => chr ($self->{next_char} + 0x0020),
1161                   line => $l, column => $c};
1162          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1163          !!!next-input-character;          !!!next-input-character;
1164          redo A;          redo A;
1165        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
1166                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1167            !!!cp (30);
1168          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1169                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
1170                                      line => $l, column => $c};
1171          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1172          !!!next-input-character;          !!!next-input-character;
1173          redo A;          redo A;
1174        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1175          !!!parse-error (type => 'empty end tag');          !!!cp (31);
1176            !!!parse-error (type => 'empty end tag',
1177                            line => $self->{line_prev}, ## "<" in "</>"
1178                            column => $self->{column_prev} - 1);
1179          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1180          !!!next-input-character;          !!!next-input-character;
1181          redo A;          redo A;
1182        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1183            !!!cp (32);
1184          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1185          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1186          # reconsume          # reconsume
1187    
1188          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1189                      line => $l, column => $c,
1190                     });
1191    
1192          redo A;          redo A;
1193        } else {        } else {
1194            !!!cp (33);
1195          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1196          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1197          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1198                                      line => $self->{line_prev}, # "<" of "</"
1199                                      column => $self->{column_prev} - 1,
1200                                     };
1201            ## $self->{next_char} is intentionally left as is
1202          redo A;          redo A;
1203        }        }
1204      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1205        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1206            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1207            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1208            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1209            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1210            !!!cp (34);
1211          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1212          !!!next-input-character;          !!!next-input-character;
1213          redo A;          redo A;
1214        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1215          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1216            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
1217            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1218          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1219            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1220            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1221              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1222            }            #  !!! cp (36);
1223              #  !!! parse-error (type => 'end tag attribute');
1224              #} else {
1225                !!!cp (37);
1226              #}
1227          } else {          } else {
1228            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1229          }          }
# Line 616  sub _get_next_token ($) { Line 1233  sub _get_next_token ($) {
1233          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1234    
1235          redo A;          redo A;
1236        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1237                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1238          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1239            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1240            # start tag or end tag            # start tag or end tag
1241          ## Stay in this state          ## Stay in this state
1242          !!!next-input-character;          !!!next-input-character;
1243          redo A;          redo A;
1244        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1245          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1246          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1247            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1248            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1249          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1250            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1251            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1252              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1253            }            #  !!! cp (40);
1254              #  !!! parse-error (type => 'end tag attribute');
1255              #} else {
1256                !!!cp (41);
1257              #}
1258          } else {          } else {
1259            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1260          }          }
# Line 643  sub _get_next_token ($) { Line 1264  sub _get_next_token ($) {
1264          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1265    
1266          redo A;          redo A;
1267        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1268            !!!cp (42);
1269            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1270          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1271          redo A;          redo A;
1272        } else {        } else {
1273          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1274            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1275            # start tag or end tag            # start tag or end tag
1276          ## Stay in the state          ## Stay in the state
1277          !!!next-input-character;          !!!next-input-character;
1278          redo A;          redo A;
1279        }        }
1280      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1281        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1282            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1283            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1284            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1285            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1286            !!!cp (45);
1287          ## Stay in the state          ## Stay in the state
1288          !!!next-input-character;          !!!next-input-character;
1289          redo A;          redo A;
1290        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1291          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1292            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1293            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1294          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1295            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1296            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1297                !!!cp (47);
1298              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1299              } else {
1300                !!!cp (48);
1301            }            }
1302          } else {          } else {
1303            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 691  sub _get_next_token ($) { Line 1308  sub _get_next_token ($) {
1308          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1309    
1310          redo A;          redo A;
1311        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1312                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1313          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1314                                value => ''};          $self->{current_attribute}
1315                = {name => chr ($self->{next_char} + 0x0020),
1316                   value => '',
1317                   line => $self->{line}, column => $self->{column}};
1318          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1319          !!!next-input-character;          !!!next-input-character;
1320          redo A;          redo A;
1321        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1322            !!!cp (50);
1323            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1324          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
1325          redo A;          redo A;
1326        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1327          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1328          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1329            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1330            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1331          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1332            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1333            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1334                !!!cp (53);
1335              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1336              } else {
1337                !!!cp (54);
1338            }            }
1339          } else {          } else {
1340            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 732  sub _get_next_token ($) { Line 1346  sub _get_next_token ($) {
1346    
1347          redo A;          redo A;
1348        } else {        } else {
1349          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1350                                value => ''};               0x0022 => 1, # "
1351                 0x0027 => 1, # '
1352                 0x003D => 1, # =
1353                }->{$self->{next_char}}) {
1354              !!!cp (55);
1355              !!!parse-error (type => 'bad attribute name');
1356            } else {
1357              !!!cp (56);
1358            }
1359            $self->{current_attribute}
1360                = {name => chr ($self->{next_char}),
1361                   value => '',
1362                   line => $self->{line}, column => $self->{column}};
1363          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1364          !!!next-input-character;          !!!next-input-character;
1365          redo A;          redo A;
# Line 742  sub _get_next_token ($) { Line 1368  sub _get_next_token ($) {
1368        my $before_leave = sub {        my $before_leave = sub {
1369          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1370              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1371            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1372              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1373            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1374          } else {          } else {
1375              !!!cp (58);
1376            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1377              = $self->{current_attribute};              = $self->{current_attribute};
1378          }          }
1379        }; # $before_leave        }; # $before_leave
1380    
1381        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1382            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1383            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1384            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1385            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1386            !!!cp (59);
1387          $before_leave->();          $before_leave->();
1388          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1389          !!!next-input-character;          !!!next-input-character;
1390          redo A;          redo A;
1391        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1392            !!!cp (60);
1393          $before_leave->();          $before_leave->();
1394          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1395          !!!next-input-character;          !!!next-input-character;
1396          redo A;          redo A;
1397        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1398          $before_leave->();          $before_leave->();
1399          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1400            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1401            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1402          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1403              !!!cp (62);
1404            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1405            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1406              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 784  sub _get_next_token ($) { Line 1414  sub _get_next_token ($) {
1414          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1415    
1416          redo A;          redo A;
1417        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1418                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1419          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1420            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1421          ## Stay in the state          ## Stay in the state
1422          !!!next-input-character;          !!!next-input-character;
1423          redo A;          redo A;
1424        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1425            !!!cp (64);
1426          $before_leave->();          $before_leave->();
1427            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1428          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1429          redo A;          redo A;
1430        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1431          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1432          $before_leave->();          $before_leave->();
1433          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1434            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1435            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1436          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1437            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1438            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1439                !!!cp (67);
1440              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1441              } else {
1442                ## NOTE: This state should never be reached.
1443                !!!cp (68);
1444            }            }
1445          } else {          } else {
1446            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 826  sub _get_next_token ($) { Line 1452  sub _get_next_token ($) {
1452    
1453          redo A;          redo A;
1454        } else {        } else {
1455          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1456                $self->{next_char} == 0x0027) { # '
1457              !!!cp (69);
1458              !!!parse-error (type => 'bad attribute name');
1459            } else {
1460              !!!cp (70);
1461            }
1462            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1463          ## Stay in the state          ## Stay in the state
1464          !!!next-input-character;          !!!next-input-character;
1465          redo A;          redo A;
1466        }        }
1467      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1468        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1469            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1470            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1471            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1472            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1473            !!!cp (71);
1474          ## Stay in the state          ## Stay in the state
1475          !!!next-input-character;          !!!next-input-character;
1476          redo A;          redo A;
1477        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1478            !!!cp (72);
1479          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1480          !!!next-input-character;          !!!next-input-character;
1481          redo A;          redo A;
1482        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1483          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1484            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1485            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1486          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1487            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1488            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1489                !!!cp (74);
1490              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1491              } else {
1492                ## NOTE: This state should never be reached.
1493                !!!cp (75);
1494            }            }
1495          } else {          } else {
1496            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 863  sub _get_next_token ($) { Line 1501  sub _get_next_token ($) {
1501          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1502    
1503          redo A;          redo A;
1504        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1505                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1506          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1507                                value => ''};          $self->{current_attribute}
1508                = {name => chr ($self->{next_char} + 0x0020),
1509                   value => '',
1510                   line => $self->{line}, column => $self->{column}};
1511          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1512          !!!next-input-character;          !!!next-input-character;
1513          redo A;          redo A;
1514        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1515            !!!cp (77);
1516            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1517          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!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  
1518          redo A;          redo A;
1519        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1520          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1521          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1522            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1523            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1524          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1525            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1526            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1527                !!!cp (80);
1528              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1529              } else {
1530                ## NOTE: This state should never be reached.
1531                !!!cp (81);
1532            }            }
1533          } else {          } else {
1534            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 905  sub _get_next_token ($) { Line 1540  sub _get_next_token ($) {
1540    
1541          redo A;          redo A;
1542        } else {        } else {
1543          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1544                                value => ''};          $self->{current_attribute}
1545                = {name => chr ($self->{next_char}),
1546                   value => '',
1547                   line => $self->{line}, column => $self->{column}};
1548          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1549          !!!next-input-character;          !!!next-input-character;
1550          redo A;                  redo A;        
1551        }        }
1552      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1553        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1554            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1555            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1556            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1557            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1558            !!!cp (83);
1559          ## Stay in the state          ## Stay in the state
1560          !!!next-input-character;          !!!next-input-character;
1561          redo A;          redo A;
1562        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1563            !!!cp (84);
1564          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1565          !!!next-input-character;          !!!next-input-character;
1566          redo A;          redo A;
1567        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1568            !!!cp (85);
1569          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1570          ## reconsume          ## reconsume
1571          redo A;          redo A;
1572        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1573            !!!cp (86);
1574          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1575          !!!next-input-character;          !!!next-input-character;
1576          redo A;          redo A;
1577        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1578          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1579            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1580            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1581          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1582            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1583            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1584                !!!cp (88);
1585              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1586              } else {
1587                ## NOTE: This state should never be reached.
1588                !!!cp (89);
1589            }            }
1590          } else {          } else {
1591            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 951  sub _get_next_token ($) { Line 1596  sub _get_next_token ($) {
1596          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1597    
1598          redo A;          redo A;
1599        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1600          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1601          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1602            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1603            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1604          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1605            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1606            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1607                !!!cp (91);
1608              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1609              } else {
1610                ## NOTE: This state should never be reached.
1611                !!!cp (92);
1612            }            }
1613          } else {          } else {
1614            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 972  sub _get_next_token ($) { Line 1620  sub _get_next_token ($) {
1620    
1621          redo A;          redo A;
1622        } else {        } else {
1623          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1624              !!!cp (93);
1625              !!!parse-error (type => 'bad attribute value');
1626            } else {
1627              !!!cp (94);
1628            }
1629            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1630          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1631          !!!next-input-character;          !!!next-input-character;
1632          redo A;          redo A;
1633        }        }
1634      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1635        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1636          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (95);
1637            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1638          !!!next-input-character;          !!!next-input-character;
1639          redo A;          redo A;
1640        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1641            !!!cp (96);
1642          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1643          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1644          !!!next-input-character;          !!!next-input-character;
1645          redo A;          redo A;
1646        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1647          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1648          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1649            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1650            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1651          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1652            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1653            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1654                !!!cp (98);
1655              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1656              } else {
1657                ## NOTE: This state should never be reached.
1658                !!!cp (99);
1659            }            }
1660          } else {          } else {
1661            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1008  sub _get_next_token ($) { Line 1667  sub _get_next_token ($) {
1667    
1668          redo A;          redo A;
1669        } else {        } else {
1670          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1671            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1672          ## Stay in the state          ## Stay in the state
1673          !!!next-input-character;          !!!next-input-character;
1674          redo A;          redo A;
1675        }        }
1676      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1677        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1678          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (101);
1679            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1680          !!!next-input-character;          !!!next-input-character;
1681          redo A;          redo A;
1682        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1683            !!!cp (102);
1684          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1685          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1686          !!!next-input-character;          !!!next-input-character;
1687          redo A;          redo A;
1688        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1689          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1690          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1691            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1692            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1693          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1694            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1695            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1696                !!!cp (104);
1697              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1698              } else {
1699                ## NOTE: This state should never be reached.
1700                !!!cp (105);
1701            }            }
1702          } else {          } else {
1703            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1044  sub _get_next_token ($) { Line 1709  sub _get_next_token ($) {
1709    
1710          redo A;          redo A;
1711        } else {        } else {
1712          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1713            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1714          ## Stay in the state          ## Stay in the state
1715          !!!next-input-character;          !!!next-input-character;
1716          redo A;          redo A;
1717        }        }
1718      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1719        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1720            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1721            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1722            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1723            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1724            !!!cp (107);
1725          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1726          !!!next-input-character;          !!!next-input-character;
1727          redo A;          redo A;
1728        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1729            !!!cp (108);
1730          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1731          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1732          !!!next-input-character;          !!!next-input-character;
1733          redo A;          redo A;
1734        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1735          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1736            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1737            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1738          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1739            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1740            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1741                !!!cp (110);
1742              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1743              } else {
1744                ## NOTE: This state should never be reached.
1745                !!!cp (111);
1746            }            }
1747          } else {          } else {
1748            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1082  sub _get_next_token ($) { Line 1753  sub _get_next_token ($) {
1753          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1754    
1755          redo A;          redo A;
1756        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1757          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1758          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1759            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1760            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1761          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1762            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1763            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1764                !!!cp (113);
1765              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1766              } else {
1767                ## NOTE: This state should never be reached.
1768                !!!cp (114);
1769            }            }
1770          } else {          } else {
1771            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1103  sub _get_next_token ($) { Line 1777  sub _get_next_token ($) {
1777    
1778          redo A;          redo A;
1779        } else {        } else {
1780          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1781                 0x0022 => 1, # "
1782                 0x0027 => 1, # '
1783                 0x003D => 1, # =
1784                }->{$self->{next_char}}) {
1785              !!!cp (115);
1786              !!!parse-error (type => 'bad attribute value');
1787            } else {
1788              !!!cp (116);
1789            }
1790            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1791          ## Stay in the state          ## Stay in the state
1792          !!!next-input-character;          !!!next-input-character;
1793          redo A;          redo A;
1794        }        }
1795      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1796        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1797              (1,
1798               $self->{last_attribute_value_state}
1799                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1800               $self->{last_attribute_value_state}
1801                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1802               -1);
1803    
1804        unless (defined $token) {        unless (defined $token) {
1805            !!!cp (117);
1806          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1807        } else {        } else {
1808            !!!cp (118);
1809          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1810            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1811          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1812        }        }
1813    
1814        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1815        # next-input-character is already done        # next-input-character is already done
1816        redo A;        redo A;
1817        } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1818          if ($self->{next_char} == 0x0009 or # HT
1819              $self->{next_char} == 0x000A or # LF
1820              $self->{next_char} == 0x000B or # VT
1821              $self->{next_char} == 0x000C or # FF
1822              $self->{next_char} == 0x0020) { # SP
1823            !!!cp (118);
1824            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1825            !!!next-input-character;
1826            redo A;
1827          } elsif ($self->{next_char} == 0x003E) { # >
1828            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1829              !!!cp (119);
1830              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1831            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1832              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1833              if ($self->{current_token}->{attributes}) {
1834                !!!cp (120);
1835                !!!parse-error (type => 'end tag attribute');
1836              } else {
1837                ## NOTE: This state should never be reached.
1838                !!!cp (121);
1839              }
1840            } else {
1841              die "$0: $self->{current_token}->{type}: Unknown token type";
1842            }
1843            $self->{state} = DATA_STATE;
1844            !!!next-input-character;
1845    
1846            !!!emit ($self->{current_token}); # start tag or end tag
1847    
1848            redo A;
1849          } elsif ($self->{next_char} == 0x002F) { # /
1850            !!!cp (122);
1851            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1852            !!!next-input-character;
1853            redo A;
1854          } elsif ($self->{next_char} == -1) {
1855            !!!parse-error (type => 'unclosed tag');
1856            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1857              !!!cp (122.3);
1858              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1859            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1860              if ($self->{current_token}->{attributes}) {
1861                !!!cp (122.1);
1862                !!!parse-error (type => 'end tag attribute');
1863              } else {
1864                ## NOTE: This state should never be reached.
1865                !!!cp (122.2);
1866              }
1867            } else {
1868              die "$0: $self->{current_token}->{type}: Unknown token type";
1869            }
1870            $self->{state} = DATA_STATE;
1871            ## Reconsume.
1872            !!!emit ($self->{current_token}); # start tag or end tag
1873            redo A;
1874          } else {
1875            !!!cp ('124.1');
1876            !!!parse-error (type => 'no space between attributes');
1877            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1878            ## reconsume
1879            redo A;
1880          }
1881        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1882          if ($self->{next_char} == 0x003E) { # >
1883            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1884              !!!cp ('124.2');
1885              !!!parse-error (type => 'nestc', token => $self->{current_token});
1886              ## TODO: Different type than slash in start tag
1887              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1888              if ($self->{current_token}->{attributes}) {
1889                !!!cp ('124.4');
1890                !!!parse-error (type => 'end tag attribute');
1891              } else {
1892                !!!cp ('124.5');
1893              }
1894              ## TODO: Test |<title></title/>|
1895            } else {
1896              !!!cp ('124.3');
1897              $self->{self_closing} = 1;
1898            }
1899    
1900            $self->{state} = DATA_STATE;
1901            !!!next-input-character;
1902    
1903            !!!emit ($self->{current_token}); # start tag or end tag
1904    
1905            redo A;
1906          } elsif ($self->{next_char} == -1) {
1907            !!!parse-error (type => 'unclosed tag');
1908            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1909              !!!cp (124.7);
1910              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1911            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1912              if ($self->{current_token}->{attributes}) {
1913                !!!cp (124.5);
1914                !!!parse-error (type => 'end tag attribute');
1915              } else {
1916                ## NOTE: This state should never be reached.
1917                !!!cp (124.6);
1918              }
1919            } else {
1920              die "$0: $self->{current_token}->{type}: Unknown token type";
1921            }
1922            $self->{state} = DATA_STATE;
1923            ## Reconsume.
1924            !!!emit ($self->{current_token}); # start tag or end tag
1925            redo A;
1926          } else {
1927            !!!cp ('124.4');
1928            !!!parse-error (type => 'nestc');
1929            ## TODO: This error type is wrong.
1930            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1931            ## Reconsume.
1932            redo A;
1933          }
1934      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1935        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1936                
1937        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1938          #my $token = {type => COMMENT_TOKEN, data => ''};
1939    
1940        BC: {        BC: {
1941          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1942              !!!cp (124);
1943            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1944            !!!next-input-character;            !!!next-input-character;
1945    
1946            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1947    
1948            redo A;            redo A;
1949          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1950              !!!cp (125);
1951            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1952            ## reconsume            ## reconsume
1953    
1954            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1955    
1956            redo A;            redo A;
1957          } else {          } else {
1958            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1959              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1960            !!!next-input-character;            !!!next-input-character;
1961            redo BC;            redo BC;
1962          }          }
1963        } # BC        } # BC
1964    
1965          die "$0: _get_next_token: unexpected case [BC]";
1966      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1967        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1968    
1969          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1970    
1971        my @next_char;        my @next_char;
1972        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1973                
1974        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1975          !!!next-input-character;          !!!next-input-character;
1976          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1977          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1978            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            !!!cp (127);
1979              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1980                                        line => $l, column => $c,
1981                                       };
1982            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1983            !!!next-input-character;            !!!next-input-character;
1984            redo A;            redo A;
1985            } else {
1986              !!!cp (128);
1987          }          }
1988        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1989                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1990          !!!next-input-character;          !!!next-input-character;
1991          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1992          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1993              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1994            !!!next-input-character;            !!!next-input-character;
1995            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1996            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1997                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1998              !!!next-input-character;              !!!next-input-character;
1999              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
2000              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2001                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2002                !!!next-input-character;                !!!next-input-character;
2003                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
2004                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
2005                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
2006                  !!!next-input-character;                  !!!next-input-character;
2007                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
2008                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
2009                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
2010                    !!!next-input-character;                    !!!next-input-character;
2011                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
2012                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
2013                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
2014                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
2015                        ## TODO: What a stupid code this is!
2016                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
2017                        $self->{current_token} = {type => DOCTYPE_TOKEN,
2018                                                  quirks => 1,
2019                                                  line => $l, column => $c,
2020                                                 };
2021                      !!!next-input-character;                      !!!next-input-character;
2022                      redo A;                      redo A;
2023                      } else {
2024                        !!!cp (130);
2025                    }                    }
2026                    } else {
2027                      !!!cp (131);
2028                  }                  }
2029                  } else {
2030                    !!!cp (132);
2031                }                }
2032                } else {
2033                  !!!cp (133);
2034              }              }
2035              } else {
2036                !!!cp (134);
2037            }            }
2038            } else {
2039              !!!cp (135);
2040          }          }
2041          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2042                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2043                   $self->{next_char} == 0x005B) { # [
2044            !!!next-input-character;
2045            push @next_char, $self->{next_char};
2046            if ($self->{next_char} == 0x0043) { # C
2047              !!!next-input-character;
2048              push @next_char, $self->{next_char};
2049              if ($self->{next_char} == 0x0044) { # D
2050                !!!next-input-character;
2051                push @next_char, $self->{next_char};
2052                if ($self->{next_char} == 0x0041) { # A
2053                  !!!next-input-character;
2054                  push @next_char, $self->{next_char};
2055                  if ($self->{next_char} == 0x0054) { # T
2056                    !!!next-input-character;
2057                    push @next_char, $self->{next_char};
2058                    if ($self->{next_char} == 0x0041) { # A
2059                      !!!next-input-character;
2060                      push @next_char, $self->{next_char};
2061                      if ($self->{next_char} == 0x005B) { # [
2062                        !!!cp (135.1);
2063                        $self->{state} = CDATA_BLOCK_STATE;
2064                        !!!next-input-character;
2065                        redo A;
2066                      } else {
2067                        !!!cp (135.2);
2068                      }
2069                    } else {
2070                      !!!cp (135.3);
2071                    }
2072                  } else {
2073                    !!!cp (135.4);                
2074                  }
2075                } else {
2076                  !!!cp (135.5);
2077                }
2078              } else {
2079                !!!cp (135.6);
2080              }
2081            } else {
2082              !!!cp (135.7);
2083            }
2084          } else {
2085            !!!cp (136);
2086        }        }
2087    
2088        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
2089        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2090        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2091        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2092          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2093                                    line => $l, column => $c,
2094                                   };
2095        redo A;        redo A;
2096                
2097        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2098        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
2099      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2100        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2101            !!!cp (137);
2102          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2103          !!!next-input-character;          !!!next-input-character;
2104          redo A;          redo A;
2105        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2106            !!!cp (138);
2107          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2108          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2109          !!!next-input-character;          !!!next-input-character;
# Line 1221  sub _get_next_token ($) { Line 2111  sub _get_next_token ($) {
2111          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2112    
2113          redo A;          redo A;
2114        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2115            !!!cp (139);
2116          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2117          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2118          ## reconsume          ## reconsume
# Line 1230  sub _get_next_token ($) { Line 2121  sub _get_next_token ($) {
2121    
2122          redo A;          redo A;
2123        } else {        } else {
2124            !!!cp (140);
2125          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2126              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2127          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2128          !!!next-input-character;          !!!next-input-character;
2129          redo A;          redo A;
2130        }        }
2131      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2132        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2133            !!!cp (141);
2134          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2135          !!!next-input-character;          !!!next-input-character;
2136          redo A;          redo A;
2137        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2138            !!!cp (142);
2139          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2140          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2141          !!!next-input-character;          !!!next-input-character;
# Line 1249  sub _get_next_token ($) { Line 2143  sub _get_next_token ($) {
2143          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2144    
2145          redo A;          redo A;
2146        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2147            !!!cp (143);
2148          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2149          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2150          ## reconsume          ## reconsume
# Line 1258  sub _get_next_token ($) { Line 2153  sub _get_next_token ($) {
2153    
2154          redo A;          redo A;
2155        } else {        } else {
2156            !!!cp (144);
2157          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2158              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2159          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2160          !!!next-input-character;          !!!next-input-character;
2161          redo A;          redo A;
2162        }        }
2163      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2164        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2165            !!!cp (145);
2166          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2167          !!!next-input-character;          !!!next-input-character;
2168          redo A;          redo A;
2169        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2170            !!!cp (146);
2171          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2172          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2173          ## reconsume          ## reconsume
# Line 1278  sub _get_next_token ($) { Line 2176  sub _get_next_token ($) {
2176    
2177          redo A;          redo A;
2178        } else {        } else {
2179          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2180            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2181          ## Stay in the state          ## Stay in the state
2182          !!!next-input-character;          !!!next-input-character;
2183          redo A;          redo A;
2184        }        }
2185      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2186        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2187            !!!cp (148);
2188          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2189          !!!next-input-character;          !!!next-input-character;
2190          redo A;          redo A;
2191        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2192            !!!cp (149);
2193          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2194          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2195          ## reconsume          ## reconsume
# Line 1297  sub _get_next_token ($) { Line 2198  sub _get_next_token ($) {
2198    
2199          redo A;          redo A;
2200        } else {        } else {
2201          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2202            $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2203          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2204          !!!next-input-character;          !!!next-input-character;
2205          redo A;          redo A;
2206        }        }
2207      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2208        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2209            !!!cp (151);
2210          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2211          !!!next-input-character;          !!!next-input-character;
2212    
2213          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2214    
2215          redo A;          redo A;
2216        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2217          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2218            !!!parse-error (type => 'dash in comment',
2219                            line => $self->{line_prev},
2220                            column => $self->{column_prev});
2221          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2222          ## Stay in the state          ## Stay in the state
2223          !!!next-input-character;          !!!next-input-character;
2224          redo A;          redo A;
2225        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2226            !!!cp (153);
2227          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2228          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2229          ## reconsume          ## reconsume
# Line 1325  sub _get_next_token ($) { Line 2232  sub _get_next_token ($) {
2232    
2233          redo A;          redo A;
2234        } else {        } else {
2235          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2236          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2237                            line => $self->{line_prev},
2238                            column => $self->{column_prev});
2239            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2240          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2241          !!!next-input-character;          !!!next-input-character;
2242          redo A;          redo A;
2243        }        }
2244      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2245        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2246            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2247            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2248            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2249            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2250            !!!cp (155);
2251          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2252          !!!next-input-character;          !!!next-input-character;
2253          redo A;          redo A;
2254        } else {        } else {
2255            !!!cp (156);
2256          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2257          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2258          ## reconsume          ## reconsume
2259          redo A;          redo A;
2260        }        }
2261      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2262        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2263            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2264            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2265            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2266            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2267            !!!cp (157);
2268          ## Stay in the state          ## Stay in the state
2269          !!!next-input-character;          !!!next-input-character;
2270          redo A;          redo A;
2271        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2272            !!!cp (158);
2273          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2274          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2275          !!!next-input-character;          !!!next-input-character;
2276    
2277          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2278    
2279          redo A;          redo A;
2280        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2281            !!!cp (159);
2282          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2283          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2284          ## reconsume          ## reconsume
2285    
2286          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2287    
2288          redo A;          redo A;
2289        } else {        } else {
2290          $self->{current_token}          !!!cp (160);
2291              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
2292                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2293  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2294          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2295          !!!next-input-character;          !!!next-input-character;
# Line 1383  sub _get_next_token ($) { Line 2297  sub _get_next_token ($) {
2297        }        }
2298      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2299  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2300        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2301            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2302            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2303            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2304            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2305            !!!cp (161);
2306          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2307          !!!next-input-character;          !!!next-input-character;
2308          redo A;          redo A;
2309        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2310            !!!cp (162);
2311          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2312          !!!next-input-character;          !!!next-input-character;
2313    
2314          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2315    
2316          redo A;          redo A;
2317        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2318            !!!cp (163);
2319          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2320          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2321          ## reconsume          ## reconsume
2322    
2323          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2324          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2325    
2326          redo A;          redo A;
2327        } else {        } else {
2328            !!!cp (164);
2329          $self->{current_token}->{name}          $self->{current_token}->{name}
2330            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2331          ## Stay in the state          ## Stay in the state
2332          !!!next-input-character;          !!!next-input-character;
2333          redo A;          redo A;
2334        }        }
2335      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2336        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2337            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2338            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2339            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2340            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2341            !!!cp (165);
2342          ## Stay in the state          ## Stay in the state
2343          !!!next-input-character;          !!!next-input-character;
2344          redo A;          redo A;
2345        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2346            !!!cp (166);
2347          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2348          !!!next-input-character;          !!!next-input-character;
2349    
2350          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2351    
2352          redo A;          redo A;
2353        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2354            !!!cp (167);
2355          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2356          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2357          ## reconsume          ## reconsume
2358    
2359          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2360          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2361    
2362          redo A;          redo A;
2363        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2364                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2365          !!!next-input-character;          !!!next-input-character;
2366          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2367              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2368            !!!next-input-character;            !!!next-input-character;
2369            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2370                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2371              !!!next-input-character;              !!!next-input-character;
2372              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2373                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2374                !!!next-input-character;                !!!next-input-character;
2375                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2376                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2377                  !!!next-input-character;                  !!!next-input-character;
2378                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2379                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2380                      !!!cp (168);
2381                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2382                    !!!next-input-character;                    !!!next-input-character;
2383                    redo A;                    redo A;
2384                    } else {
2385                      !!!cp (169);
2386                  }                  }
2387                  } else {
2388                    !!!cp (170);
2389                }                }
2390                } else {
2391                  !!!cp (171);
2392              }              }
2393              } else {
2394                !!!cp (172);
2395            }            }
2396            } else {
2397              !!!cp (173);
2398          }          }
2399    
2400          #          #
2401        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2402                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2403          !!!next-input-character;          !!!next-input-character;
2404          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2405              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2406            !!!next-input-character;            !!!next-input-character;
2407            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2408                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2409              !!!next-input-character;              !!!next-input-character;
2410              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2411                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2412                !!!next-input-character;                !!!next-input-character;
2413                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2414                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2415                  !!!next-input-character;                  !!!next-input-character;
2416                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2417                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2418                      !!!cp (174);
2419                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2420                    !!!next-input-character;                    !!!next-input-character;
2421                    redo A;                    redo A;
2422                    } else {
2423                      !!!cp (175);
2424                  }                  }
2425                  } else {
2426                    !!!cp (176);
2427                }                }
2428                } else {
2429                  !!!cp (177);
2430              }              }
2431              } else {
2432                !!!cp (178);
2433            }            }
2434            } else {
2435              !!!cp (179);
2436          }          }
2437    
2438          #          #
2439        } else {        } else {
2440            !!!cp (180);
2441          !!!next-input-character;          !!!next-input-character;
2442          #          #
2443        }        }
2444    
2445        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2446          $self->{current_token}->{quirks} = 1;
2447    
2448        $self->{state} = BOGUS_DOCTYPE_STATE;        $self->{state} = BOGUS_DOCTYPE_STATE;
2449        # next-input-character is already done        # next-input-character is already done
2450        redo A;        redo A;
# Line 1506  sub _get_next_token ($) { Line 2452  sub _get_next_token ($) {
2452        if ({        if ({
2453              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2454              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2455            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2456            !!!cp (181);
2457          ## Stay in the state          ## Stay in the state
2458          !!!next-input-character;          !!!next-input-character;
2459          redo A;          redo A;
2460        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2461            !!!cp (182);
2462          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2463          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2464          !!!next-input-character;          !!!next-input-character;
2465          redo A;          redo A;
2466        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2467            !!!cp (183);
2468          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2469          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2470          !!!next-input-character;          !!!next-input-character;
2471          redo A;          redo A;
2472        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2473            !!!cp (184);
2474          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2475    
2476          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2477          !!!next-input-character;          !!!next-input-character;
2478    
2479          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2480          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2481    
2482          redo A;          redo A;
2483        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2484            !!!cp (185);
2485          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2486    
2487          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2488          ## reconsume          ## reconsume
2489    
2490          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2491          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2492    
2493          redo A;          redo A;
2494        } else {        } else {
2495            !!!cp (186);
2496          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2497            $self->{current_token}->{quirks} = 1;
2498    
2499          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2500          !!!next-input-character;          !!!next-input-character;
2501          redo A;          redo A;
2502        }        }
2503      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2504        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2505            !!!cp (187);
2506          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2507          !!!next-input-character;          !!!next-input-character;
2508          redo A;          redo A;
2509        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2510            !!!cp (188);
2511            !!!parse-error (type => 'unclosed PUBLIC literal');
2512    
2513            $self->{state} = DATA_STATE;
2514            !!!next-input-character;
2515    
2516            $self->{current_token}->{quirks} = 1;
2517            !!!emit ($self->{current_token}); # DOCTYPE
2518    
2519            redo A;
2520          } elsif ($self->{next_char} == -1) {
2521            !!!cp (189);
2522          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2523    
2524          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2525          ## reconsume          ## reconsume
2526    
2527          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2528          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2529    
2530          redo A;          redo A;
2531        } else {        } else {
2532            !!!cp (190);
2533          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2534              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2535          ## Stay in the state          ## Stay in the state
2536          !!!next-input-character;          !!!next-input-character;
2537          redo A;          redo A;
2538        }        }
2539      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2540        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2541            !!!cp (191);
2542          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2543          !!!next-input-character;          !!!next-input-character;
2544          redo A;          redo A;
2545        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2546            !!!cp (192);
2547            !!!parse-error (type => 'unclosed PUBLIC literal');
2548    
2549            $self->{state} = DATA_STATE;
2550            !!!next-input-character;
2551    
2552            $self->{current_token}->{quirks} = 1;
2553            !!!emit ($self->{current_token}); # DOCTYPE
2554    
2555            redo A;
2556          } elsif ($self->{next_char} == -1) {
2557            !!!cp (193);
2558          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2559    
2560          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2561          ## reconsume          ## reconsume
2562    
2563          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2564          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2565    
2566          redo A;          redo A;
2567        } else {        } else {
2568            !!!cp (194);
2569          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2570              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2571          ## Stay in the state          ## Stay in the state
2572          !!!next-input-character;          !!!next-input-character;
2573          redo A;          redo A;
# Line 1594  sub _get_next_token ($) { Line 2576  sub _get_next_token ($) {
2576        if ({        if ({
2577              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2578              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2579            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2580            !!!cp (195);
2581          ## Stay in the state          ## Stay in the state
2582          !!!next-input-character;          !!!next-input-character;
2583          redo A;          redo A;
2584        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2585            !!!cp (196);
2586          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2587          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2588          !!!next-input-character;          !!!next-input-character;
2589          redo A;          redo A;
2590        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2591            !!!cp (197);
2592          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2593          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2594          !!!next-input-character;          !!!next-input-character;
2595          redo A;          redo A;
2596        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2597            !!!cp (198);
2598          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2599          !!!next-input-character;          !!!next-input-character;
2600    
2601          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2602    
2603          redo A;          redo A;
2604        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2605            !!!cp (199);
2606          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2607    
2608          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2609          ## reconsume          ## reconsume
2610    
2611          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2612          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2613    
2614          redo A;          redo A;
2615        } else {        } else {
2616            !!!cp (200);
2617          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2618            $self->{current_token}->{quirks} = 1;
2619    
2620          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2621          !!!next-input-character;          !!!next-input-character;
2622          redo A;          redo A;
# Line 1635  sub _get_next_token ($) { Line 2625  sub _get_next_token ($) {
2625        if ({        if ({
2626              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2627              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2628            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2629            !!!cp (201);
2630          ## Stay in the state          ## Stay in the state
2631          !!!next-input-character;          !!!next-input-character;
2632          redo A;          redo A;
2633        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2634            !!!cp (202);
2635          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2636          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2637          !!!next-input-character;          !!!next-input-character;
2638          redo A;          redo A;
2639        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2640            !!!cp (203);
2641          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2642          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2643          !!!next-input-character;          !!!next-input-character;
2644          redo A;          redo A;
2645        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2646            !!!cp (204);
2647          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2648          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2649          !!!next-input-character;          !!!next-input-character;
2650    
2651          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2652          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2653    
2654          redo A;          redo A;
2655        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2656            !!!cp (205);
2657          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2658    
2659          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2660          ## reconsume          ## reconsume
2661    
2662          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2663          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2664    
2665          redo A;          redo A;
2666        } else {        } else {
2667            !!!cp (206);
2668          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2669            $self->{current_token}->{quirks} = 1;
2670    
2671          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2672          !!!next-input-character;          !!!next-input-character;
2673          redo A;          redo A;
2674        }        }
2675      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2676        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2677            !!!cp (207);
2678          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2679          !!!next-input-character;          !!!next-input-character;
2680          redo A;          redo A;
2681        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2682            !!!cp (208);
2683            !!!parse-error (type => 'unclosed SYSTEM literal');
2684    
2685            $self->{state} = DATA_STATE;
2686            !!!next-input-character;
2687    
2688            $self->{current_token}->{quirks} = 1;
2689            !!!emit ($self->{current_token}); # DOCTYPE
2690    
2691            redo A;
2692          } elsif ($self->{next_char} == -1) {
2693            !!!cp (209);
2694          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2695    
2696          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2697          ## reconsume          ## reconsume
2698    
2699          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2700          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2701    
2702          redo A;          redo A;
2703        } else {        } else {
2704            !!!cp (210);
2705          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2706              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2707          ## Stay in the state          ## Stay in the state
2708          !!!next-input-character;          !!!next-input-character;
2709          redo A;          redo A;
2710        }        }
2711      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2712        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2713            !!!cp (211);
2714          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2715          !!!next-input-character;          !!!next-input-character;
2716          redo A;          redo A;
2717        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2718            !!!cp (212);
2719            !!!parse-error (type => 'unclosed SYSTEM literal');
2720    
2721            $self->{state} = DATA_STATE;
2722            !!!next-input-character;
2723    
2724            $self->{current_token}->{quirks} = 1;
2725            !!!emit ($self->{current_token}); # DOCTYPE
2726    
2727            redo A;
2728          } elsif ($self->{next_char} == -1) {
2729            !!!cp (213);
2730          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2731    
2732          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2733          ## reconsume          ## reconsume
2734    
2735          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2736          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2737    
2738          redo A;          redo A;
2739        } else {        } else {
2740            !!!cp (214);
2741          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2742              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2743          ## Stay in the state          ## Stay in the state
2744          !!!next-input-character;          !!!next-input-character;
2745          redo A;          redo A;
# Line 1722  sub _get_next_token ($) { Line 2748  sub _get_next_token ($) {
2748        if ({        if ({
2749              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2750              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2751            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2752            !!!cp (215);
2753          ## Stay in the state          ## Stay in the state
2754          !!!next-input-character;          !!!next-input-character;
2755          redo A;          redo A;
2756        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2757            !!!cp (216);
2758          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2759          !!!next-input-character;          !!!next-input-character;
2760    
2761          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2762    
2763          redo A;          redo A;
2764        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2765            !!!cp (217);
2766          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2767          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2768          ## reconsume          ## reconsume
2769    
2770          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2771          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2772    
2773          redo A;          redo A;
2774        } else {        } else {
2775            !!!cp (218);
2776          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2777            #$self->{current_token}->{quirks} = 1;
2778    
2779          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2780          !!!next-input-character;          !!!next-input-character;
2781          redo A;          redo A;
2782        }        }
2783      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2784        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2785            !!!cp (219);
2786          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2787          !!!next-input-character;          !!!next-input-character;
2788    
         delete $self->{current_token}->{correct};  
2789          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2790    
2791          redo A;          redo A;
2792        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2793            !!!cp (220);
2794          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2795          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2796          ## reconsume          ## reconsume
2797    
         delete $self->{current_token}->{correct};  
2798          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2799    
2800          redo A;          redo A;
2801        } else {        } else {
2802            !!!cp (221);
2803          ## Stay in the state          ## Stay in the state
2804          !!!next-input-character;          !!!next-input-character;
2805          redo A;          redo A;
2806        }        }
2807        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2808          my $s = '';
2809          
2810          my ($l, $c) = ($self->{line}, $self->{column});
2811    
2812          CS: while ($self->{next_char} != -1) {
2813            if ($self->{next_char} == 0x005D) { # ]
2814              !!!next-input-character;
2815              if ($self->{next_char} == 0x005D) { # ]
2816                !!!next-input-character;
2817                MDC: {
2818                  if ($self->{next_char} == 0x003E) { # >
2819                    !!!cp (221.1);
2820                    !!!next-input-character;
2821                    last CS;
2822                  } elsif ($self->{next_char} == 0x005D) { # ]
2823                    !!!cp (221.2);
2824                    $s .= ']';
2825                    !!!next-input-character;
2826                    redo MDC;
2827                  } else {
2828                    !!!cp (221.3);
2829                    $s .= ']]';
2830                    #
2831                  }
2832                } # MDC
2833              } else {
2834                !!!cp (221.4);
2835                $s .= ']';
2836                #
2837              }
2838            } else {
2839              !!!cp (221.5);
2840              #
2841            }
2842            $s .= chr $self->{next_char};
2843            !!!next-input-character;
2844          } # CS
2845    
2846          $self->{state} = DATA_STATE;
2847          ## next-input-character done or EOF, which is reconsumed.
2848    
2849          if (length $s) {
2850            !!!cp (221.6);
2851            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2852                      line => $l, column => $c});
2853          } else {
2854            !!!cp (221.7);
2855          }
2856    
2857          redo A;
2858    
2859          ## ISSUE: "text tokens" in spec.
2860          ## TODO: Streaming support
2861      } else {      } else {
2862        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2863      }      }
# Line 1780  sub _get_next_token ($) { Line 2866  sub _get_next_token ($) {
2866    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2867  } # _get_next_token  } # _get_next_token
2868    
2869  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2870    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2871    
2872      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2873    
2874    if ({    if ({
2875         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2876         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2877        }->{$self->{next_input_character}}) {         $additional => 1,
2878          }->{$self->{next_char}}) {
2879        !!!cp (1001);
2880      ## Don't consume      ## Don't consume
2881      ## No error      ## No error
2882      return undef;      return undef;
2883    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2884      !!!next-input-character;      !!!next-input-character;
2885      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2886          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2887        my $code;        my $code;
2888        X: {        X: {
2889          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2890          !!!next-input-character;          !!!next-input-character;
2891          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2892              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2893              !!!cp (1002);
2894            $code ||= 0;            $code ||= 0;
2895            $code *= 0x10;            $code *= 0x10;
2896            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2897            redo X;            redo X;
2898          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2899                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2900              !!!cp (1003);
2901            $code ||= 0;            $code ||= 0;
2902            $code *= 0x10;            $code *= 0x10;
2903            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2904            redo X;            redo X;
2905          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2906                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2907              !!!cp (1004);
2908            $code ||= 0;            $code ||= 0;
2909            $code *= 0x10;            $code *= 0x10;
2910            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2911            redo X;            redo X;
2912          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2913            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2914            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2915            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2916              $self->{next_char} = 0x0023; # #
2917            return undef;            return undef;
2918          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2919              !!!cp (1006);
2920            !!!next-input-character;            !!!next-input-character;
2921          } else {          } else {
2922            !!!parse-error (type => 'no refc');            !!!cp (1007);
2923              !!!parse-error (type => 'no refc', line => $l, column => $c);
2924          }          }
2925    
2926          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2927            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2928              !!!parse-error (type => 'invalid character reference',
2929                              text => (sprintf 'U+%04X', $code),
2930                              line => $l, column => $c);
2931            $code = 0xFFFD;            $code = 0xFFFD;
2932          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2933            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2934              !!!parse-error (type => 'invalid character reference',
2935                              text => (sprintf 'U-%08X', $code),
2936                              line => $l, column => $c);
2937            $code = 0xFFFD;            $code = 0xFFFD;
2938          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2939            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2940              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2941            $code = 0x000A;            $code = 0x000A;
2942          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2943            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2944              !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
2945            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2946          }          }
2947    
2948          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2949                    has_reference => 1,
2950                    line => $l, column => $c,
2951                   };
2952        } # X        } # X
2953      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2954               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2955        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2956        !!!next-input-character;        !!!next-input-character;
2957                
2958        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2959                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2960            !!!cp (1012);
2961          $code *= 10;          $code *= 10;
2962          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2963                    
2964          !!!next-input-character;          !!!next-input-character;
2965        }        }
2966    
2967        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2968            !!!cp (1013);
2969          !!!next-input-character;          !!!next-input-character;
2970        } else {        } else {
2971          !!!parse-error (type => 'no refc');          !!!cp (1014);
2972            !!!parse-error (type => 'no refc', line => $l, column => $c);
2973        }        }
2974    
2975        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2976          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2977            !!!parse-error (type => 'invalid character reference',
2978                            text => (sprintf 'U+%04X', $code),
2979                            line => $l, column => $c);
2980          $code = 0xFFFD;          $code = 0xFFFD;
2981        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2982          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2983            !!!parse-error (type => 'invalid character reference',
2984                            text => (sprintf 'U-%08X', $code),
2985                            line => $l, column => $c);
2986          $code = 0xFFFD;          $code = 0xFFFD;
2987        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2988          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2989            !!!parse-error (type => 'CR character reference',
2990                            line => $l, column => $c);
2991          $code = 0x000A;          $code = 0x000A;
2992        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2993          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2994            !!!parse-error (type => 'C1 character reference',
2995                            text => (sprintf 'U+%04X', $code),
2996                            line => $l, column => $c);
2997          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2998        }        }
2999                
3000        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3001                  line => $l, column => $c,
3002                 };
3003      } else {      } else {
3004        !!!parse-error (type => 'bare nero');        !!!cp (1019);
3005        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
3006        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
3007          $self->{next_char} = 0x0023; # #
3008        return undef;        return undef;
3009      }      }
3010    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
3011              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
3012             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
3013              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
3014      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
3015      !!!next-input-character;      !!!next-input-character;
3016    
3017      my $value = $entity_name;      my $value = $entity_name;
# Line 1895  sub _tokenize_attempt_to_consume_an_enti Line 3019  sub _tokenize_attempt_to_consume_an_enti
3019      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3020      our $EntityChar;      our $EntityChar;
3021    
3022      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3023             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3024             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
3025               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
3026              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
3027               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
3028              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
3029               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
3030              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
3031        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
3032        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
3033          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
3034              !!!cp (1020);
3035            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3036            $match = 1;            $match = 1;
3037            !!!next-input-character;            !!!next-input-character;
3038            last;            last;
3039          } else {          } else {
3040              !!!cp (1021);
3041            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3042            $match = -1;            $match = -1;
3043            !!!next-input-character;            !!!next-input-character;
3044          }          }
3045        } else {        } else {
3046          $value .= chr $self->{next_input_character};          !!!cp (1022);
3047            $value .= chr $self->{next_char};
3048          $match *= 2;          $match *= 2;
3049          !!!next-input-character;          !!!next-input-character;
3050        }        }
3051      }      }
3052            
3053      if ($match > 0) {      if ($match > 0) {
3054        return {type => CHARACTER_TOKEN, data => $value};        !!!cp (1023);
3055          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3056                  line => $l, column => $c,
3057                 };
3058      } elsif ($match < 0) {      } elsif ($match < 0) {
3059        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3060        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3061          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          !!!cp (1024);
3062        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3063          return {type => CHARACTER_TOKEN, data => $value};                  line => $l, column => $c,
3064                   };
3065          } else {
3066            !!!cp (1025);
3067            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3068                    line => $l, column => $c,
3069                   };
3070        }        }
3071      } else {      } else {
3072        !!!parse-error (type => 'bare ero');        !!!cp (1026);
3073        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3074        return {type => CHARACTER_TOKEN, data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
3075          return {type => CHARACTER_TOKEN, data => '&'.$value,
3076                  line => $l, column => $c,
3077                 };
3078      }      }
3079    } else {    } else {
3080        !!!cp (1027);
3081      ## no characters are consumed      ## no characters are consumed
3082      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3083      return undef;      return undef;
3084    }    }
3085  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1977  sub _construct_tree ($) { Line 3117  sub _construct_tree ($) {
3117        
3118    !!!next-token;    !!!next-token;
3119    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3120    undef $self->{form_element};    undef $self->{form_element};
3121    undef $self->{head_element};    undef $self->{head_element};
3122    $self->{open_elements} = [];    $self->{open_elements} = [];
3123    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3124    
3125      ## NOTE: The "initial" insertion mode.
3126    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3127    
3128      ## NOTE: The "before html" insertion mode.
3129    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3130      $self->{insertion_mode} = BEFORE_HEAD_IM;
3131    
3132      ## NOTE: The "before head" insertion mode and so on.
3133    $self->_tree_construction_main;    $self->_tree_construction_main;
3134  } # _construct_tree  } # _construct_tree
3135    
3136  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3137    my $self = shift;    my $self = shift;
3138    
3139      ## NOTE: "initial" insertion mode
3140    
3141    INITIAL: {    INITIAL: {
3142      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3143        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2001  sub _tree_construction_initial ($) { Line 3149  sub _tree_construction_initial ($) {
3149        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3150            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3151            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3152          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3153            !!!parse-error (type => 'not HTML5', token => $token);
3154        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3155            !!!cp ('t2');
3156          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3157          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3158          } else {
3159            !!!cp ('t3');
3160        }        }
3161                
3162        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3163          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3164          ## NOTE: Default value for both |public_id| and |system_id| attributes
3165          ## are empty strings, so that we don't set any value in missing cases.
3166        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3167            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3168        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2017  sub _tree_construction_initial ($) { Line 3171  sub _tree_construction_initial ($) {
3171        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3172        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3173                
3174        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3175            !!!cp ('t4');
3176          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3177        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3178          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3179          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3180          if ({          my $prefix = [
3181            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3182            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3183            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3184            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3185            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3186            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3187            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3188            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3189            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3190            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3191            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3192            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3193            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3194            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3195            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3196            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3197            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3198            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3199            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3200            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3201            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3202            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3203            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3204            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3205            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3206            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3207            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3208            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3209            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3210            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3211            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3212            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3213            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3214            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3215            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3216            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3217            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3218            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3219            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3220            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3221            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3222            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3223            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3224            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3225            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3226            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3227            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3228            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3229            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3230            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3231            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3232            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3233            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3234            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3235            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3236            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3237            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3238            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3239            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3240            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3241            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3242            "-//W3C//DTD W3 HTML//EN" => 1,            }
3243            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3244            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3245            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3246            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3247            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3248            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3249            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3250          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3251                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3252            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3253                !!!cp ('t6');
3254              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3255            } else {            } else {
3256                !!!cp ('t7');
3257              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3258            }            }
3259          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3260                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3261              !!!cp ('t8');
3262            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3263            } else {
3264              !!!cp ('t9');
3265          }          }
3266          } else {
3267            !!!cp ('t10');
3268        }        }
3269        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3270          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3271          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3272          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") {
3273              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3274              ## marked as quirks.
3275            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3276              !!!cp ('t11');
3277            } else {
3278              !!!cp ('t12');
3279          }          }
3280          } else {
3281            !!!cp ('t13');
3282        }        }
3283                
3284        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3285        !!!next-token;        !!!next-token;
3286        return;        return;
3287      } elsif ({      } elsif ({
# Line 2122  sub _tree_construction_initial ($) { Line 3289  sub _tree_construction_initial ($) {
3289                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3290                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3291               }->{$token->{type}}) {               }->{$token->{type}}) {
3292        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3293          !!!parse-error (type => 'no DOCTYPE', token => $token);
3294        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3295        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3296        ## reprocess        ## reprocess
3297          !!!ack-later;
3298        return;        return;
3299      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3300        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3301          ## Ignore the token          ## Ignore the token
3302    
3303          unless (length $token->{data}) {          unless (length $token->{data}) {
3304            ## Stay in the phase            !!!cp ('t15');
3305              ## Stay in the insertion mode.
3306            !!!next-token;            !!!next-token;
3307            redo INITIAL;            redo INITIAL;
3308            } else {
3309              !!!cp ('t16');
3310          }          }
3311          } else {
3312            !!!cp ('t17');
3313        }        }
3314    
3315        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3316        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3317        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3318        ## reprocess        ## reprocess
3319        return;        return;
3320      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3321          !!!cp ('t18');
3322        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3323        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3324                
3325        ## Stay in the phase.        ## Stay in the insertion mode.
3326        !!!next-token;        !!!next-token;
3327        redo INITIAL;        redo INITIAL;
3328      } else {      } else {
3329        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3330      }      }
3331    } # INITIAL    } # INITIAL
3332    
3333      die "$0: _tree_construction_initial: This should be never reached";
3334  } # _tree_construction_initial  } # _tree_construction_initial
3335    
3336  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3337    my $self = shift;    my $self = shift;
3338    
3339      ## NOTE: "before html" insertion mode.
3340        
3341    B: {    B: {
3342        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3343          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3344            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3345          ## Ignore the token          ## Ignore the token
3346          ## Stay in the phase          ## Stay in the insertion mode.
3347          !!!next-token;          !!!next-token;
3348          redo B;          redo B;
3349        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3350            !!!cp ('t20');
3351          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3352          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3353          ## Stay in the phase          ## Stay in the insertion mode.
3354          !!!next-token;          !!!next-token;
3355          redo B;          redo B;
3356        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2177  sub _tree_construction_root_element ($) Line 3358  sub _tree_construction_root_element ($)
3358            ## Ignore the token.            ## Ignore the token.
3359    
3360            unless (length $token->{data}) {            unless (length $token->{data}) {
3361              ## Stay in the phase              !!!cp ('t21');
3362                ## Stay in the insertion mode.
3363              !!!next-token;              !!!next-token;
3364              redo B;              redo B;
3365              } else {
3366                !!!cp ('t22');
3367            }            }
3368            } else {
3369              !!!cp ('t23');
3370          }          }
3371    
3372          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
3373    
3374          #          #
3375        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3376          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3377              $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"            my $root_element;
3378            $self->{application_cache_selection}            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3379                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
3380            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
3381                  [$root_element, $el_category->{html}];
3382    
3383              if ($token->{attributes}->{manifest}) {
3384                !!!cp ('t24');
3385                $self->{application_cache_selection}
3386                    ->($token->{attributes}->{manifest}->{value});
3387                ## ISSUE: Spec is unclear on relative references.
3388                ## According to Hixie (#whatwg 2008-03-19), it should be
3389                ## resolved against the base URI of the document in HTML
3390                ## or xml:base of the element in XHTML.
3391              } else {
3392                !!!cp ('t25');
3393                $self->{application_cache_selection}->(undef);
3394              }
3395    
3396              !!!nack ('t25c');
3397    
3398              !!!next-token;
3399              return; ## Go to the "before head" insertion mode.
3400          } else {          } else {
3401            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
3402              #
3403          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3404        } elsif ({        } elsif ({
3405                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3406                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3407                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3408          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
3409          #          #
3410        } else {        } else {
3411          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3412        }        }
3413    
3414        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3415        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3416        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3417        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3418        #redo B;  
3419        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3420    
3421        ## NOTE: Reprocess the token.
3422        !!!ack-later;
3423        return; ## Go to the "before head" insertion mode.
3424    
3425        ## ISSUE: There is an issue in the spec
3426    } # B    } # B
3427    
3428      die "$0: _tree_construction_root_element: This should never be reached";
3429  } # _tree_construction_root_element  } # _tree_construction_root_element
3430    
3431  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2231  sub _reset_insertion_mode ($) { Line 3440  sub _reset_insertion_mode ($) {
3440            
3441      ## Step 3      ## Step 3
3442      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"!?  
3443        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3444          $last = 1;          $last = 1;
3445          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3446            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3447                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3448              #          } else {
3449            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3450          }          }
3451        }        }
3452              
3453        ## Step 4..13        ## Step 4..14
3454        my $new_mode = {        my $new_mode;
3455          if ($node->[1] & FOREIGN_EL) {
3456            !!!cp ('t28.1');
3457            ## NOTE: Strictly spaking, the line below only applies to MathML and
3458            ## SVG elements.  Currently the HTML syntax supports only MathML and
3459            ## SVG elements as foreigners.
3460            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3461          } elsif ($node->[1] & TABLE_CELL_EL) {
3462            if ($last) {
3463              !!!cp ('t28.2');
3464              #
3465            } else {
3466              !!!cp ('t28.3');
3467              $new_mode = IN_CELL_IM;
3468            }
3469          } else {
3470            !!!cp ('t28.4');
3471            $new_mode = {
3472                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3473                        td => IN_CELL_IM,                        ## NOTE: |option| and |optgroup| do not set
3474                        th => IN_CELL_IM,                        ## insertion mode to "in select" by themselves.
3475                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3476                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3477                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2263  sub _reset_insertion_mode ($) { Line 3482  sub _reset_insertion_mode ($) {
3482                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3483                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3484                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3485                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3486          }
3487        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3488                
3489        ## Step 14        ## Step 15
3490        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3491          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3492              !!!cp ('t29');
3493            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3494          } else {          } else {
3495              ## ISSUE: Can this state be reached?
3496              !!!cp ('t30');
3497            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3498          }          }
3499          return;          return;
3500          } else {
3501            !!!cp ('t31');
3502        }        }
3503                
3504        ## Step 15        ## Step 16
3505        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3506                
3507        ## Step 16        ## Step 17
3508        $i--;        $i--;
3509        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3510                
3511        ## Step 17        ## Step 18
3512        redo S3;        redo S3;
3513      } # S3      } # S3
3514    
3515      die "$0: _reset_insertion_mode: This line should never be reached";
3516  } # _reset_insertion_mode  } # _reset_insertion_mode
3517    
3518  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2307  sub _tree_construction_main ($) { Line 3534  sub _tree_construction_main ($) {
3534      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3535      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3536        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3537            !!!cp ('t32');
3538          return;          return;
3539        }        }
3540      }      }
# Line 2321  sub _tree_construction_main ($) { Line 3549  sub _tree_construction_main ($) {
3549    
3550        ## Step 6        ## Step 6
3551        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3552            !!!cp ('t33_1');
3553          #          #
3554        } else {        } else {
3555          my $in_open_elements;          my $in_open_elements;
3556          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3557            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3558                !!!cp ('t33');
3559              $in_open_elements = 1;              $in_open_elements = 1;
3560              last OE;              last OE;
3561            }            }
3562          }          }
3563          if ($in_open_elements) {          if ($in_open_elements) {
3564              !!!cp ('t34');
3565            #            #
3566          } else {          } else {
3567              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3568              !!!cp ('t35');
3569            redo S4;            redo S4;
3570          }          }
3571        }        }
# Line 2355  sub _tree_construction_main ($) { Line 3588  sub _tree_construction_main ($) {
3588    
3589        ## Step 11        ## Step 11
3590        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3591            !!!cp ('t36');
3592          ## Step 7'          ## Step 7'
3593          $i++;          $i++;
3594          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3595                    
3596          redo S7;          redo S7;
3597        }        }
3598    
3599          !!!cp ('t37');
3600      } # S7      } # S7
3601    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3602    
3603    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3604      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3605        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3606            !!!cp ('t38');
3607          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3608          return;          return;
3609        }        }
3610      }      }
3611    
3612        !!!cp ('t39');
3613    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3614    
3615    my $parse_rcdata = sub ($$) {    my $insert;
3616      my ($content_model_flag, $insert) = @_;  
3617      my $parse_rcdata = sub ($) {
3618        my ($content_model_flag) = @_;
3619    
3620      ## Step 1      ## Step 1
3621      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3622      my $el;      my $el;
3623      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3624    
3625      ## Step 2      ## Step 2
3626      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3627    
3628      ## Step 3      ## Step 3
3629      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2390  sub _tree_construction_main ($) { Line 3631  sub _tree_construction_main ($) {
3631    
3632      ## Step 4      ## Step 4
3633      my $text = '';      my $text = '';
3634        !!!nack ('t40.1');
3635      !!!next-token;      !!!next-token;
3636      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3637          !!!cp ('t40');
3638        $text .= $token->{data};        $text .= $token->{data};
3639        !!!next-token;        !!!next-token;
3640      }      }
3641    
3642      ## Step 5      ## Step 5
3643      if (length $text) {      if (length $text) {
3644          !!!cp ('t41');
3645        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3646        $el->append_child ($text);        $el->append_child ($text);
3647      }      }
# Line 2406  sub _tree_construction_main ($) { Line 3650  sub _tree_construction_main ($) {
3650      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3651    
3652      ## Step 7      ## Step 7
3653      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3654            $token->{tag_name} eq $start_tag_name) {
3655          !!!cp ('t42');
3656        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3657      } else {      } else {
3658        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3659          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3660            !!!cp ('t43');
3661            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3662          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3663            !!!cp ('t44');
3664            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3665          } else {
3666            die "$0: $content_model_flag in parse_rcdata";
3667          }
3668      }      }
3669      !!!next-token;      !!!next-token;
3670    }; # $parse_rcdata    }; # $parse_rcdata
3671    
3672    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3673      my $script_el;      my $script_el;
3674      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3675      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3676    
3677      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3678      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3679            
3680      my $text = '';      my $text = '';
3681        !!!nack ('t45.1');
3682      !!!next-token;      !!!next-token;
3683      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3684          !!!cp ('t45');
3685        $text .= $token->{data};        $text .= $token->{data};
3686        !!!next-token;        !!!next-token;
3687      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3688      if (length $text) {      if (length $text) {
3689          !!!cp ('t46');
3690        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3691      }      }
3692                                
# Line 2441  sub _tree_construction_main ($) { Line 3694  sub _tree_construction_main ($) {
3694    
3695      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3696          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3697          !!!cp ('t47');
3698        ## Ignore the token        ## Ignore the token
3699      } else {      } else {
3700        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3701          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3702        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3703        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3704      }      }
3705            
3706      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3707          !!!cp ('t49');
3708        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3709      } else {      } else {
3710          !!!cp ('t50');
3711        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3712        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3713    
# Line 2464  sub _tree_construction_main ($) { Line 3721  sub _tree_construction_main ($) {
3721      !!!next-token;      !!!next-token;
3722    }; # $script_start_tag    }; # $script_start_tag
3723    
3724      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3725      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3726      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3727    
3728    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3729      my $tag_name = shift;      my $end_tag_token = shift;
3730        my $tag_name = $end_tag_token->{tag_name};
3731    
3732        ## NOTE: The adoption agency algorithm (AAA).
3733    
3734      FET: {      FET: {
3735        ## Step 1        ## Step 1
3736        my $formatting_element;        my $formatting_element;
3737        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3738        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3739          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3740              !!!cp ('t52');
3741              last AFE;
3742            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3743                         eq $tag_name) {
3744              !!!cp ('t51');
3745            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3746            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3747            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3748          }          }
3749        } # AFE        } # AFE
3750        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3751          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3752            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3753          ## Ignore the token          ## Ignore the token
3754          !!!next-token;          !!!next-token;
3755          return;          return;
# Line 2493  sub _tree_construction_main ($) { Line 3761  sub _tree_construction_main ($) {
3761          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3762          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3763            if ($in_scope) {            if ($in_scope) {
3764                !!!cp ('t54');
3765              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3766              last INSCOPE;              last INSCOPE;
3767            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3768              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3769                !!!parse-error (type => 'unmatched end tag',
3770                                text => $token->{tag_name},
3771                                token => $end_tag_token);
3772              ## Ignore the token              ## Ignore the token
3773              !!!next-token;              !!!next-token;
3774              return;              return;
3775            }            }
3776          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3777                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3778            $in_scope = 0;            $in_scope = 0;
3779          }          }
3780        } # INSCOPE        } # INSCOPE
3781        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3782          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3783            !!!parse-error (type => 'unmatched end tag',
3784                            text => $token->{tag_name},
3785                            token => $end_tag_token);
3786          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3787          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3788          return;          return;
3789        }        }
3790        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3791          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3792            !!!parse-error (type => 'not closed',
3793                            text => $self->{open_elements}->[-1]->[0]
3794                                ->manakai_local_name,
3795                            token => $end_tag_token);
3796        }        }
3797                
3798        ## Step 2        ## Step 2
# Line 2523  sub _tree_construction_main ($) { Line 3800  sub _tree_construction_main ($) {
3800        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3801        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3802          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3803          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3804              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3805              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3806               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3807              !!!cp ('t59');
3808            $furthest_block = $node;            $furthest_block = $node;
3809            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3810          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3811              !!!cp ('t60');
3812            last OE;            last OE;
3813          }          }
3814        } # OE        } # OE
3815                
3816        ## Step 3        ## Step 3
3817        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3818            !!!cp ('t61');
3819          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3820          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3821          !!!next-token;          !!!next-token;
# Line 2548  sub _tree_construction_main ($) { Line 3828  sub _tree_construction_main ($) {
3828        ## Step 5        ## Step 5
3829        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3830        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3831            !!!cp ('t62');
3832          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3833        }        }
3834                
# Line 2570  sub _tree_construction_main ($) { Line 3851  sub _tree_construction_main ($) {
3851          S7S2: {          S7S2: {
3852            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3853              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3854                  !!!cp ('t63');
3855                $node_i_in_active = $_;                $node_i_in_active = $_;
3856                last S7S2;                last S7S2;
3857              }              }
# Line 2583  sub _tree_construction_main ($) { Line 3865  sub _tree_construction_main ($) {
3865                    
3866          ## Step 4          ## Step 4
3867          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3868              !!!cp ('t64');
3869            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3870          }          }
3871                    
3872          ## Step 5          ## Step 5
3873          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3874              !!!cp ('t65');
3875            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3876            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3877            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2605  sub _tree_construction_main ($) { Line 3889  sub _tree_construction_main ($) {
3889        } # S7          } # S7  
3890                
3891        ## Step 8        ## Step 8
3892        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3893            my $foster_parent_element;
3894            my $next_sibling;
3895            OE: for (reverse 0..$#{$self->{open_elements}}) {
3896              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3897                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3898                                 if (defined $parent and $parent->node_type == 1) {
3899                                   !!!cp ('t65.1');
3900                                   $foster_parent_element = $parent;
3901                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3902                                 } else {
3903                                   !!!cp ('t65.2');
3904                                   $foster_parent_element
3905                                     = $self->{open_elements}->[$_ - 1]->[0];
3906                                 }
3907                                 last OE;
3908                               }
3909                             } # OE
3910                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3911                               unless defined $foster_parent_element;
3912            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3913            $open_tables->[-1]->[1] = 1; # tainted
3914          } else {
3915            !!!cp ('t65.3');
3916            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3917          }
3918                
3919        ## Step 9        ## Step 9
3920        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2622  sub _tree_construction_main ($) { Line 3931  sub _tree_construction_main ($) {
3931        my $i;        my $i;
3932        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3933          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3934              !!!cp ('t66');
3935            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3936            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3937          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3938              !!!cp ('t67');
3939            $i = $_;            $i = $_;
3940          }          }
3941        } # AFE        } # AFE
# Line 2634  sub _tree_construction_main ($) { Line 3945  sub _tree_construction_main ($) {
3945        undef $i;        undef $i;
3946        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3947          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3948              !!!cp ('t68');
3949            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3950            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3951          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3952              !!!cp ('t69');
3953            $i = $_;            $i = $_;
3954          }          }
3955        } # OE        } # OE
# Line 2647  sub _tree_construction_main ($) { Line 3960  sub _tree_construction_main ($) {
3960      } # FET      } # FET
3961    }; # $formatting_end_tag    }; # $formatting_end_tag
3962    
3963    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3964      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3965    }; # $insert_to_current    }; # $insert_to_current
3966    
3967    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3968                         my $child = shift;      my $child = shift;
3969                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3970                              table => 1, tbody => 1, tfoot => 1,        # MUST
3971                              thead => 1, tr => 1,        my $foster_parent_element;
3972                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3973                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3974                           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') {  
3975                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3976                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3977                                   !!!cp ('t70');
3978                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3979                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3980                               } else {                               } else {
3981                                   !!!cp ('t71');
3982                                 $foster_parent_element                                 $foster_parent_element
3983                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3984                               }                               }
# Line 2677  sub _tree_construction_main ($) { Line 3989  sub _tree_construction_main ($) {
3989                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3990                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3991                             ($child, $next_sibling);                             ($child, $next_sibling);
3992                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3993                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3994                         }        !!!cp ('t72');
3995          $self->{open_elements}->[-1]->[0]->append_child ($child);
3996        }
3997    }; # $insert_to_foster    }; # $insert_to_foster
3998    
3999    my $insert;    B: while (1) {
   
   B: {  
4000      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4001        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
4002          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4003        ## Ignore the token        ## Ignore the token
4004        ## Stay in the phase        ## Stay in the phase
4005        !!!next-token;        !!!next-token;
4006        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
4007      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4008               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4009        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4010          ## Turn into the main phase          !!!cp ('t79');
4011          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4012          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4013        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4014          ## Turn into the main phase          !!!cp ('t80');
4015          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4016          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4017          } else {
4018            !!!cp ('t81');
4019        }        }
4020    
4021  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
4022  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag', token => $token);
       unless ($token->{first_start_tag}) {  
         !!!parse-error (type => 'not first start tag');  
       }  
4023        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4024        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4025          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4026              !!!cp ('t84');
4027            $top_el->set_attribute_ns            $top_el->set_attribute_ns
4028              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
4029               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4030          }          }
4031        }        }
4032          !!!nack ('t84.1');
4033        !!!next-token;        !!!next-token;
4034        redo B;        next B;
4035      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4036        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4037        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4038            !!!cp ('t85');
4039          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
4040        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4041            !!!cp ('t86');
4042          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
4043        } else {        } else {
4044            !!!cp ('t87');
4045          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4046        }        }
4047        !!!next-token;        !!!next-token;
4048        redo B;        next B;
4049      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4050          if ($token->{type} == CHARACTER_TOKEN) {
4051            !!!cp ('t87.1');
4052            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4053            !!!next-token;
4054            next B;
4055          } elsif ($token->{type} == START_TAG_TOKEN) {
4056            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4057                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4058                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4059                ($token->{tag_name} eq 'svg' and
4060                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4061              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4062              !!!cp ('t87.2');
4063              #
4064            } elsif ({
4065                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4066                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4067                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4068                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4069                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4070                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4071                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4072                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4073                     }->{$token->{tag_name}}) {
4074              !!!cp ('t87.2');
4075              !!!parse-error (type => 'not closed',
4076                              text => $self->{open_elements}->[-1]->[0]
4077                                  ->manakai_local_name,
4078                              token => $token);
4079    
4080              pop @{$self->{open_elements}}
4081                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4082    
4083              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4084              ## Reprocess.
4085              next B;
4086            } else {
4087              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4088              my $tag_name = $token->{tag_name};
4089              if ($nsuri eq $SVG_NS) {
4090                $tag_name = {
4091                   altglyph => 'altGlyph',
4092                   altglyphdef => 'altGlyphDef',
4093                   altglyphitem => 'altGlyphItem',
4094                   animatecolor => 'animateColor',
4095                   animatemotion => 'animateMotion',
4096                   animatetransform => 'animateTransform',
4097                   clippath => 'clipPath',
4098                   feblend => 'feBlend',
4099                   fecolormatrix => 'feColorMatrix',
4100                   fecomponenttransfer => 'feComponentTransfer',
4101                   fecomposite => 'feComposite',
4102                   feconvolvematrix => 'feConvolveMatrix',
4103                   fediffuselighting => 'feDiffuseLighting',
4104                   fedisplacementmap => 'feDisplacementMap',
4105                   fedistantlight => 'feDistantLight',
4106                   feflood => 'feFlood',
4107                   fefunca => 'feFuncA',
4108                   fefuncb => 'feFuncB',
4109                   fefuncg => 'feFuncG',
4110                   fefuncr => 'feFuncR',
4111                   fegaussianblur => 'feGaussianBlur',
4112                   feimage => 'feImage',
4113                   femerge => 'feMerge',
4114                   femergenode => 'feMergeNode',
4115                   femorphology => 'feMorphology',
4116                   feoffset => 'feOffset',
4117                   fepointlight => 'fePointLight',
4118                   fespecularlighting => 'feSpecularLighting',
4119                   fespotlight => 'feSpotLight',
4120                   fetile => 'feTile',
4121                   feturbulence => 'feTurbulence',
4122                   foreignobject => 'foreignObject',
4123                   glyphref => 'glyphRef',
4124                   lineargradient => 'linearGradient',
4125                   radialgradient => 'radialGradient',
4126                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4127                   textpath => 'textPath',  
4128                }->{$tag_name} || $tag_name;
4129              }
4130    
4131              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4132    
4133              ## "adjust foreign attributes" - done in insert-element-f
4134    
4135              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4136    
4137              if ($self->{self_closing}) {
4138                pop @{$self->{open_elements}};
4139                !!!ack ('t87.3');
4140              } else {
4141                !!!cp ('t87.4');
4142              }
4143    
4144              !!!next-token;
4145              next B;
4146            }
4147          } elsif ($token->{type} == END_TAG_TOKEN) {
4148            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4149            !!!cp ('t87.5');
4150            #
4151          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4152            !!!cp ('t87.6');
4153            !!!parse-error (type => 'not closed',
4154                            text => $self->{open_elements}->[-1]->[0]
4155                                ->manakai_local_name,
4156                            token => $token);
4157    
4158            pop @{$self->{open_elements}}
4159                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4160    
4161            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4162            ## Reprocess.
4163            next B;
4164          } else {
4165            die "$0: $token->{type}: Unknown token type";        
4166          }
4167        }
4168    
4169        if ($self->{insertion_mode} & HEAD_IMS) {
4170        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4171          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4172            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4173                !!!cp ('t88.2');
4174                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4175              } else {
4176                !!!cp ('t88.1');
4177                ## Ignore the token.
4178                !!!next-token;
4179                next B;
4180              }
4181            unless (length $token->{data}) {            unless (length $token->{data}) {
4182                !!!cp ('t88');
4183              !!!next-token;              !!!next-token;
4184              redo B;              next B;
4185            }            }
4186          }          }
4187    
4188          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4189              !!!cp ('t89');
4190            ## As if <head>            ## As if <head>
4191            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4192            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4193            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4194                  [$self->{head_element}, $el_category->{head}];
4195    
4196            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4197            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4198    
4199            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4200          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4201              !!!cp ('t90');
4202            ## As if </noscript>            ## As if </noscript>
4203            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4204            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4205                        
4206            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4207            ## As if </head>            ## As if </head>
# Line 2788  sub _tree_construction_main ($) { Line 4209  sub _tree_construction_main ($) {
4209    
4210            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4211          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4212              !!!cp ('t91');
4213            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4214    
4215            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4216            } else {
4217              !!!cp ('t92');
4218          }          }
4219    
4220              ## "after head" insertion mode          ## "after head" insertion mode
4221              ## As if <body>          ## As if <body>
4222              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4223              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4224              ## reprocess          ## reprocess
4225              redo B;          next B;
4226            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4227              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4228                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4229                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4230                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4231                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4232                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4233                  !!!next-token;              push @{$self->{open_elements}},
4234                  redo B;                  [$self->{head_element}, $el_category->{head}];
4235                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4236                  #              !!!nack ('t93.1');
4237                } else {              !!!next-token;
4238                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4239                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4240                  !!!next-token;              !!!cp ('t93.2');
4241                  redo B;              !!!parse-error (type => 'after head', text => 'head',
4242                }                              token => $token);
4243              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              ## Ignore the token
4244                ## As if <head>              !!!nack ('t93.3');
4245                !!!create-element ($self->{head_element}, 'head');              !!!next-token;
4246                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              next B;
4247                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            } else {
4248                !!!cp ('t95');
4249                !!!parse-error (type => 'in head:head',
4250                                token => $token); # or in head noscript
4251                ## Ignore the token
4252                !!!nack ('t95.1');
4253                !!!next-token;
4254                next B;
4255              }
4256            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4257              !!!cp ('t96');
4258              ## As if <head>
4259              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4260              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4261              push @{$self->{open_elements}},
4262                  [$self->{head_element}, $el_category->{head}];
4263    
4264                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4265                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4266              }          } else {
4267              !!!cp ('t97');
4268            }
4269    
4270              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4271                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4272                    !!!cp ('t98');
4273                  ## As if </noscript>                  ## As if </noscript>
4274                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4275                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4276                                    token => $token);
4277                                
4278                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4279                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4280                  } else {
4281                    !!!cp ('t99');
4282                }                }
4283    
4284                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4285                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4286                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4287                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4288                                    text => $token->{tag_name}, token => $token);
4289                    push @{$self->{open_elements}},
4290                        [$self->{head_element}, $el_category->{head}];
4291                  } else {
4292                    !!!cp ('t101');
4293                }                }
4294                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4295                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4296                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4297                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4298                  !!!nack ('t101.1');
4299                !!!next-token;                !!!next-token;
4300                redo B;                next B;
4301              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4302                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4303                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4304                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4305                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4306                                    text => $token->{tag_name}, token => $token);
4307                    push @{$self->{open_elements}},
4308                        [$self->{head_element}, $el_category->{head}];
4309                  } else {
4310                    !!!cp ('t103');
4311                }                }
4312                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4313                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4314                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4315                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4316                  !!!ack ('t103.1');
4317                !!!next-token;                !!!next-token;
4318                redo B;                next B;
4319              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4320                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4321                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4322                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4323                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4324                                    text => $token->{tag_name}, token => $token);
4325                    push @{$self->{open_elements}},
4326                        [$self->{head_element}, $el_category->{head}];
4327                  } else {
4328                    !!!cp ('t105');
4329                }                }
4330                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4331                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4332    
4333                unless ($self->{confident}) {                unless ($self->{confident}) {
4334                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4335                      !!!cp ('t106');
4336                      ## NOTE: Whether the encoding is supported or not is handled
4337                      ## in the {change_encoding} callback.
4338                    $self->{change_encoding}                    $self->{change_encoding}
4339                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4340                             $token);
4341                      
4342                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4343                          ->set_user_data (manakai_has_reference =>
4344                                               $token->{attributes}->{charset}
4345                                                   ->{has_reference});
4346                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4347                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4348                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4349                              [\x09-\x0D\x20]*=
4350                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4351                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4352                        !!!cp ('t107');
4353                        ## NOTE: Whether the encoding is supported or not is handled
4354                        ## in the {change_encoding} callback.
4355                      $self->{change_encoding}                      $self->{change_encoding}
4356                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4357                               $token);
4358                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4359                            ->set_user_data (manakai_has_reference =>
4360                                                 $token->{attributes}->{content}
4361                                                       ->{has_reference});
4362                      } else {
4363                        !!!cp ('t108');
4364                    }                    }
4365                  }                  }
4366                  } else {
4367                    if ($token->{attributes}->{charset}) {
4368                      !!!cp ('t109');
4369                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4370                          ->set_user_data (manakai_has_reference =>
4371                                               $token->{attributes}->{charset}
4372                                                   ->{has_reference});
4373                    }
4374                    if ($token->{attributes}->{content}) {
4375                      !!!cp ('t110');
4376                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4377                          ->set_user_data (manakai_has_reference =>
4378                                               $token->{attributes}->{content}
4379                                                   ->{has_reference});
4380                    }
4381                }                }
4382    
4383                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4384                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4385                  !!!ack ('t110.1');
4386                !!!next-token;                !!!next-token;
4387                redo B;                next B;
4388              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4389                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4390                    !!!cp ('t111');
4391                  ## As if </noscript>                  ## As if </noscript>
4392                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4393                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4394                                    token => $token);
4395                                
4396                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4397                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4398                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4399                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4400                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4401                                    text => $token->{tag_name}, token => $token);
4402                    push @{$self->{open_elements}},
4403                        [$self->{head_element}, $el_category->{head}];
4404                  } else {
4405                    !!!cp ('t113');
4406                }                }
4407    
4408                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4409                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4410                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4411                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4412                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4413                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4414                redo B;                next B;
4415              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4416                         $token->{tag_name} eq 'noframes') {
4417                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4418                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4419                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4420                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4421                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4422                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4423                                    text => $token->{tag_name}, token => $token);
4424                    push @{$self->{open_elements}},
4425                        [$self->{head_element}, $el_category->{head}];
4426                  } else {
4427                    !!!cp ('t115');
4428                }                }
4429                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4430                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4431                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4432                redo B;                next B;
4433              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4434                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4435                    !!!cp ('t116');
4436                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4437                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4438                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4439                    !!!nack ('t116.1');
4440                  !!!next-token;                  !!!next-token;
4441                  redo B;                  next B;
4442                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4443                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4444                    !!!parse-error (type => 'in noscript', text => 'noscript',
4445                                    token => $token);
4446                  ## Ignore the token                  ## Ignore the token
4447                    !!!nack ('t117.1');
4448                  !!!next-token;                  !!!next-token;
4449                  redo B;                  next B;
4450                } else {                } else {
4451                    !!!cp ('t118');
4452                  #                  #
4453                }                }
4454              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4455                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4456                    !!!cp ('t119');
4457                  ## As if </noscript>                  ## As if </noscript>
4458                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4459                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4460                                    token => $token);
4461                                
4462                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4463                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4464                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4465                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4466                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4467                                    text => $token->{tag_name}, token => $token);
4468                    push @{$self->{open_elements}},
4469                        [$self->{head_element}, $el_category->{head}];
4470                  } else {
4471                    !!!cp ('t121');
4472                }                }
4473    
4474                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4475                $script_start_tag->($insert_to_current);                $script_start_tag->();
4476                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4477                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4478                redo B;                next B;
4479              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4480                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4481                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4482                    !!!cp ('t122');
4483                  ## As if </noscript>                  ## As if </noscript>
4484                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4485                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4486                                    text => $token->{tag_name}, token => $token);
4487                                    
4488                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4489                  ## As if </head>                  ## As if </head>
# Line 2967  sub _tree_construction_main ($) { Line 4491  sub _tree_construction_main ($) {
4491                                    
4492                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4493                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4494                    !!!cp ('t124');
4495                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4496                                    
4497                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4498                  } else {
4499                    !!!cp ('t125');
4500                }                }
4501    
4502                ## "after head" insertion mode                ## "after head" insertion mode
4503                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4504                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4505                    !!!cp ('t126');
4506                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4507                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4508                    !!!cp ('t127');
4509                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4510                } else {                } else {
4511                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4512                }                }
4513                  !!!nack ('t127.1');
4514                !!!next-token;                !!!next-token;
4515                redo B;                next B;
4516              } else {              } else {
4517                  !!!cp ('t128');
4518                #                #
4519              }              }
4520    
4521              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4522                  !!!cp ('t129');
4523                ## As if </noscript>                ## As if </noscript>
4524                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4525                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4526                                  text => $token->{tag_name}, token => $token);
4527                                
4528                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4529                ## As if </head>                ## As if </head>
# Line 2998  sub _tree_construction_main ($) { Line 4531  sub _tree_construction_main ($) {
4531    
4532                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4533              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4534                  !!!cp ('t130');
4535                ## As if </head>                ## As if </head>
4536                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4537    
4538                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4539                } else {
4540                  !!!cp ('t131');
4541              }              }
4542    
4543              ## "after head" insertion mode              ## "after head" insertion mode
4544              ## As if <body>              ## As if <body>
4545              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4546              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4547              ## reprocess              ## reprocess
4548              redo B;              !!!ack-later;
4549                next B;
4550            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4551              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4552                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4553                    !!!cp ('t132');
4554                  ## As if <head>                  ## As if <head>
4555                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4556                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4557                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4558                        [$self->{head_element}, $el_category->{head}];
4559    
4560                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4561                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4562                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4563                  !!!next-token;                  !!!next-token;
4564                  redo B;                  next B;
4565                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4566                    !!!cp ('t133');
4567                  ## As if </noscript>                  ## As if </noscript>
4568                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4569                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/',
4570                                    text => 'head', token => $token);
4571                                    
4572                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4573                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4574                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4575                  !!!next-token;                  !!!next-token;
4576                  redo B;                  next B;
4577                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4578                    !!!cp ('t134');
4579                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4580                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4581                  !!!next-token;                  !!!next-token;
4582                  redo B;                  next B;
4583                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4584                    !!!cp ('t134.1');
4585                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4586                                    token => $token);
4587                    ## Ignore the token
4588                    !!!next-token;
4589                    next B;
4590                } else {                } else {
4591                  #                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4592                }                }
4593              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4594                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4595                    !!!cp ('t136');
4596                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4597                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4598                  !!!next-token;                  !!!next-token;
4599                  redo B;                  next B;
4600                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4601                  !!!parse-error (type => 'unmatched end tag:noscript');                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4602                    !!!cp ('t137');
4603                    !!!parse-error (type => 'unmatched end tag',
4604                                    text => 'noscript', token => $token);
4605                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4606                  !!!next-token;                  !!!next-token;
4607                  redo B;                  next B;
4608                } else {                } else {
4609                    !!!cp ('t138');
4610                  #                  #
4611                }                }
4612              } elsif ({              } elsif ({
4613                        body => 1, html => 1,                        body => 1, html => 1,
4614                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4615                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4616                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_IM or
4617                  !!!create-element ($self->{head_element}, 'head');                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4618                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  !!!cp ('t140');
4619                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'unmatched end tag',
4620                                    text => $token->{tag_name}, token => $token);
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4621                  ## Ignore the token                  ## Ignore the token
4622                  !!!next-token;                  !!!next-token;
4623                  redo B;                  next B;
4624                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4625                    !!!cp ('t140.1');
4626                    !!!parse-error (type => 'unmatched end tag',
4627                                    text => $token->{tag_name}, token => $token);
4628                    ## Ignore the token
4629                    !!!next-token;
4630                    next B;
4631                  } else {
4632                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4633                }                }
4634                              } elsif ($token->{tag_name} eq 'p') {
4635                #                !!!cp ('t142');
4636              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4637                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4638                       }->{$token->{tag_name}}) {                ## Ignore the token
4639                  !!!next-token;
4640                  next B;
4641                } elsif ($token->{tag_name} eq 'br') {
4642                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4643                  ## As if <head>                  !!!cp ('t142.2');
4644                  !!!create-element ($self->{head_element}, 'head');                  ## (before head) as if <head>, (in head) as if </head>
4645                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4646                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4647                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4648      
4649                    ## Reprocess in the "after head" insertion mode...
4650                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4651                    !!!cp ('t143.2');
4652                    ## As if </head>
4653                    pop @{$self->{open_elements}};
4654                    $self->{insertion_mode} = AFTER_HEAD_IM;
4655      
4656                    ## Reprocess in the "after head" insertion mode...
4657                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4658                    !!!cp ('t143.3');
4659                    ## ISSUE: Two parse errors for <head><noscript></br>
4660                    !!!parse-error (type => 'unmatched end tag',
4661                                    text => 'br', token => $token);
4662                    ## As if </noscript>
4663                    pop @{$self->{open_elements}};
4664                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4665    
4666                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4667                }                  ## As if </head>
4668                    pop @{$self->{open_elements}};
4669                    $self->{insertion_mode} = AFTER_HEAD_IM;
4670    
4671                #                  ## Reprocess in the "after head" insertion mode...
4672              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4673                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
4674                  #                  #
4675                } else {                } else {
4676                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4677                }                }
4678    
4679                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4680                  !!!parse-error (type => 'unmatched end tag',
4681                                  text => 'br', token => $token);
4682                  ## Ignore the token
4683                  !!!next-token;
4684                  next B;
4685                } else {
4686                  !!!cp ('t145');
4687                  !!!parse-error (type => 'unmatched end tag',
4688                                  text => $token->{tag_name}, token => $token);
4689                  ## Ignore the token
4690                  !!!next-token;
4691                  next B;
4692              }              }
4693    
4694              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4695                  !!!cp ('t146');
4696                ## As if </noscript>                ## As if </noscript>
4697                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4698                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4699                                  text => $token->{tag_name}, token => $token);
4700                                
4701                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4702                ## As if </head>                ## As if </head>
# Line 3110  sub _tree_construction_main ($) { Line 4704  sub _tree_construction_main ($) {
4704    
4705                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4706              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4707                  !!!cp ('t147');
4708                ## As if </head>                ## As if </head>
4709                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4710    
4711                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4712              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4713                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4714                  !!!cp ('t148');
4715                  !!!parse-error (type => 'unmatched end tag',
4716                                  text => $token->{tag_name}, token => $token);
4717                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4718                !!!next-token;                !!!next-token;
4719                redo B;                next B;
4720                } else {
4721                  !!!cp ('t149');
4722              }              }
4723    
4724              ## "after head" insertion mode              ## "after head" insertion mode
4725              ## As if <body>              ## As if <body>
4726              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4727              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4728              ## reprocess              ## reprocess
4729              redo B;              next B;
4730            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4731              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4732            }            !!!cp ('t149.1');
4733    
4734              ## NOTE: As if <head>
4735              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4736              $self->{open_elements}->[-1]->[0]->append_child
4737                  ($self->{head_element});
4738              #push @{$self->{open_elements}},
4739              #    [$self->{head_element}, $el_category->{head}];
4740              #$self->{insertion_mode} = IN_HEAD_IM;
4741              ## NOTE: Reprocess.
4742    
4743              ## NOTE: As if </head>
4744              #pop @{$self->{open_elements}};
4745              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4746              ## NOTE: Reprocess.
4747              
4748              #
4749            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4750              !!!cp ('t149.2');
4751    
4752              ## NOTE: As if </head>
4753              pop @{$self->{open_elements}};
4754              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4755              ## NOTE: Reprocess.
4756    
4757              #
4758            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4759              !!!cp ('t149.3');
4760    
4761              !!!parse-error (type => 'in noscript:#eof', token => $token);
4762    
4763              ## As if </noscript>
4764              pop @{$self->{open_elements}};
4765              #$self->{insertion_mode} = IN_HEAD_IM;
4766              ## NOTE: Reprocess.
4767    
4768              ## NOTE: As if </head>
4769              pop @{$self->{open_elements}};
4770              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4771              ## NOTE: Reprocess.
4772    
4773              #
4774            } else {
4775              !!!cp ('t149.4');
4776              #
4777            }
4778    
4779            ## NOTE: As if <body>
4780            !!!insert-element ('body',, $token);
4781            $self->{insertion_mode} = IN_BODY_IM;
4782            ## NOTE: Reprocess.
4783            next B;
4784          } else {
4785            die "$0: $token->{type}: Unknown token type";
4786          }
4787    
4788            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4789      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
4790            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4791                !!!cp ('t150');
4792              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4793              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4794                            
4795              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4796    
4797              !!!next-token;              !!!next-token;
4798              redo B;              next B;
4799            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4800              if ({              if ({
4801                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3148  sub _tree_construction_main ($) { Line 4803  sub _tree_construction_main ($) {
4803                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4804                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4805                  ## have an element in table scope                  ## have an element in table scope
4806                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4807                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4808                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4809                      $tn = $node->[1];                      !!!cp ('t151');
4810                      last INSCOPE;  
4811                    } elsif ({                      ## Close the cell
4812                              table => 1, html => 1,                      !!!back-token; # <x>
4813                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4814                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4815                    }                                line => $token->{line},
4816                  } # INSCOPE                                column => $token->{column}};
4817                    unless (defined $tn) {                      next B;
4818                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4819                      ## Ignore the token                      !!!cp ('t152');
4820                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4821                      redo B;                      last;
4822                    }                    }
4823                                    }
4824                  ## Close the cell  
4825                  !!!back-token; # <?>                  !!!cp ('t153');
4826                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
4827                  redo B;                      text => $token->{tag_name}, token => $token);
4828                    ## Ignore the token
4829                    !!!nack ('t153.1');
4830                    !!!next-token;
4831                    next B;
4832                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4833                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
4834                                    token => $token);
4835                                    
4836                  ## As if </caption>                  ## NOTE: As if </caption>.
4837                  ## have a table element in table scope                  ## have a table element in table scope
4838                  my $i;                  my $i;
4839                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4840                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4841                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4842                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4843                      last INSCOPE;                        !!!cp ('t155');
4844                    } elsif ({                        $i = $_;
4845                              table => 1, html => 1,                        last INSCOPE;
4846                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4847                      last INSCOPE;                        !!!cp ('t156');
4848                          last;
4849                        }
4850                    }                    }
4851    
4852                      !!!cp ('t157');
4853                      !!!parse-error (type => 'start tag not allowed',
4854                                      text => $token->{tag_name}, token => $token);
4855                      ## Ignore the token
4856                      !!!nack ('t157.1');
4857                      !!!next-token;
4858                      next B;
4859                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4860                                    
4861                  ## generate implied end tags                  ## generate implied end tags
4862                  if ({                  while ($self->{open_elements}->[-1]->[1]
4863                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4864                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4865                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token; # <?>  
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4866                  }                  }
4867    
4868                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4869                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4870                      !!!parse-error (type => 'not closed',
4871                                      text => $self->{open_elements}->[-1]->[0]
4872                                          ->manakai_local_name,
4873                                      token => $token);
4874                    } else {
4875                      !!!cp ('t160');
4876                  }                  }
4877                                    
4878                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3220  sub _tree_construction_main ($) { Line 4882  sub _tree_construction_main ($) {
4882                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4883                                    
4884                  ## reprocess                  ## reprocess
4885                  redo B;                  !!!ack-later;
4886                    next B;
4887                } else {                } else {
4888                    !!!cp ('t161');
4889                  #                  #
4890                }                }
4891              } else {              } else {
4892                  !!!cp ('t162');
4893                #                #
4894              }              }
4895            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3234  sub _tree_construction_main ($) { Line 4899  sub _tree_construction_main ($) {
4899                  my $i;                  my $i;
4900                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4901                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4902                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4903                        !!!cp ('t163');
4904                      $i = $_;                      $i = $_;
4905                      last INSCOPE;                      last INSCOPE;
4906                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4907                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4908                      last INSCOPE;                      last INSCOPE;
4909                    }                    }
4910                  } # INSCOPE                  } # INSCOPE
4911                    unless (defined $i) {                    unless (defined $i) {
4912                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4913                        !!!parse-error (type => 'unmatched end tag',
4914                                        text => $token->{tag_name},
4915                                        token => $token);
4916                      ## Ignore the token                      ## Ignore the token
4917                      !!!next-token;                      !!!next-token;
4918                      redo B;                      next B;
4919                    }                    }
4920                                    
4921                  ## generate implied end tags                  ## generate implied end tags
4922                  if ({                  while ($self->{open_elements}->[-1]->[1]
4923                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4924                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4925                       th => ($token->{tag_name} eq 'td'),                    pop @{$self->{open_elements}};
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4926                  }                  }
4927                    
4928                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4929                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4930                      !!!cp ('t167');
4931                      !!!parse-error (type => 'not closed',
4932                                      text => $self->{open_elements}->[-1]->[0]
4933                                          ->manakai_local_name,
4934                                      token => $token);
4935                    } else {
4936                      !!!cp ('t168');
4937                  }                  }
4938                                    
4939                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3275  sub _tree_construction_main ($) { Line 4943  sub _tree_construction_main ($) {
4943                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4944                                    
4945                  !!!next-token;                  !!!next-token;
4946                  redo B;                  next B;
4947                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4948                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4949                    !!!parse-error (type => 'unmatched end tag',
4950                                    text => $token->{tag_name}, token => $token);
4951                  ## Ignore the token                  ## Ignore the token
4952                  !!!next-token;                  !!!next-token;
4953                  redo B;                  next B;
4954                } else {                } else {
4955                    !!!cp ('t170');
4956                  #                  #
4957                }                }
4958              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4959                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4960                  ## have a table element in table scope                  ## have a table element in table scope
4961                  my $i;                  my $i;
4962                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4963                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4964                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4965                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4966                      last INSCOPE;                        !!!cp ('t171');
4967                    } elsif ({                        $i = $_;
4968                              table => 1, html => 1,                        last INSCOPE;
4969                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4970                      last INSCOPE;                        !!!cp ('t172');
4971                          last;
4972                        }
4973                    }                    }
4974    
4975                      !!!cp ('t173');
4976                      !!!parse-error (type => 'unmatched end tag',
4977                                      text => $token->{tag_name}, token => $token);
4978                      ## Ignore the token
4979                      !!!next-token;
4980                      next B;
4981                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4982                                    
4983                  ## generate implied end tags                  ## generate implied end tags
4984                  if ({                  while ($self->{open_elements}->[-1]->[1]
4985                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4986                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
4987                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4988                  }                  }
4989                                    
4990                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4991                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4992                      !!!parse-error (type => 'not closed',
4993                                      text => $self->{open_elements}->[-1]->[0]
4994                                          ->manakai_local_name,
4995                                      token => $token);
4996                    } else {
4997                      !!!cp ('t176');
4998                  }                  }
4999                                    
5000                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3329  sub _tree_construction_main ($) { Line 5004  sub _tree_construction_main ($) {
5004                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5005                                    
5006                  !!!next-token;                  !!!next-token;
5007                  redo B;                  next B;
5008                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5009                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
5010                    !!!parse-error (type => 'unmatched end tag',
5011                                    text => $token->{tag_name}, token => $token);
5012                  ## Ignore the token                  ## Ignore the token
5013                  !!!next-token;                  !!!next-token;
5014                  redo B;                  next B;
5015                } else {                } else {
5016                    !!!cp ('t178');
5017                  #                  #
5018                }                }
5019              } elsif ({              } elsif ({
# Line 3346  sub _tree_construction_main ($) { Line 5024  sub _tree_construction_main ($) {
5024                ## have an element in table scope                ## have an element in table scope
5025                my $i;                my $i;
5026                my $tn;                my $tn;
5027                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5028                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5029                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5030                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5031                    last INSCOPE;                      !!!cp ('t179');
5032                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
5033                    $tn = $node->[1];  
5034                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
5035                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
5036                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5037                            table => 1, html => 1,                                line => $token->{line},
5038                           }->{$node->[1]}) {                                column => $token->{column}};
5039                    last INSCOPE;                      next B;
5040                      } elsif ($node->[1] & TABLE_CELL_EL) {
5041                        !!!cp ('t180');
5042                        $tn = $node->[0]->manakai_local_name;
5043                        ## NOTE: There is exactly one |td| or |th| element
5044                        ## in scope in the stack of open elements by definition.
5045                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5046                        ## ISSUE: Can this be reached?
5047                        !!!cp ('t181');
5048                        last;
5049                      }
5050                  }                  }
5051                } # INSCOPE  
5052                unless (defined $i) {                  !!!cp ('t182');
5053                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5054                        text => $token->{tag_name}, token => $token);
5055                  ## Ignore the token                  ## Ignore the token
5056                  !!!next-token;                  !!!next-token;
5057                  redo B;                  next B;
5058                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5059              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5060                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5061                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5062                                  token => $token);
5063    
5064                ## As if </caption>                ## As if </caption>
5065                ## have a table element in table scope                ## have a table element in table scope
5066                my $i;                my $i;
5067                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5068                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5069                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5070                      !!!cp ('t184');
5071                    $i = $_;                    $i = $_;
5072                    last INSCOPE;                    last INSCOPE;
5073                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5074                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5075                    last INSCOPE;                    last INSCOPE;
5076                  }                  }
5077                } # INSCOPE                } # INSCOPE
5078                unless (defined $i) {                unless (defined $i) {
5079                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5080                    !!!parse-error (type => 'unmatched end tag',
5081                                    text => 'caption', token => $token);
5082                  ## Ignore the token                  ## Ignore the token
5083                  !!!next-token;                  !!!next-token;
5084                  redo B;                  next B;
5085                }                }
5086                                
5087                ## generate implied end tags                ## generate implied end tags
5088                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5089                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5090                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5091                }                }
5092    
5093                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5094                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5095                    !!!parse-error (type => 'not closed',
5096                                    text => $self->{open_elements}->[-1]->[0]
5097                                        ->manakai_local_name,
5098                                    token => $token);
5099                  } else {
5100                    !!!cp ('t189');
5101                }                }
5102    
5103                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3422  sub _tree_construction_main ($) { Line 5107  sub _tree_construction_main ($) {
5107                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5108    
5109                ## reprocess                ## reprocess
5110                redo B;                next B;
5111              } elsif ({              } elsif ({
5112                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5113                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5114                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5115                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
5116                    !!!parse-error (type => 'unmatched end tag',
5117                                    text => $token->{tag_name}, token => $token);
5118                  ## Ignore the token                  ## Ignore the token
5119                  !!!next-token;                  !!!next-token;
5120                  redo B;                  next B;
5121                } else {                } else {
5122                    !!!cp ('t191');
5123                  #                  #
5124                }                }
5125              } elsif ({              } elsif ({
# Line 3439  sub _tree_construction_main ($) { Line 5127  sub _tree_construction_main ($) {
5127                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5128                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5129                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5130                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5131                  !!!parse-error (type => 'unmatched end tag',
5132                                  text => $token->{tag_name}, token => $token);
5133                ## Ignore the token                ## Ignore the token
5134                !!!next-token;                !!!next-token;
5135                redo B;                next B;
5136              } else {              } else {
5137                  !!!cp ('t193');
5138                #                #
5139              }              }
5140          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5141            for my $entry (@{$self->{open_elements}}) {
5142              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5143                !!!cp ('t75');
5144                !!!parse-error (type => 'in body:#eof', token => $token);
5145                last;
5146              }
5147            }
5148    
5149            ## Stop parsing.
5150            last B;
5151        } else {        } else {
5152          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5153        }        }
# Line 3454  sub _tree_construction_main ($) { Line 5156  sub _tree_construction_main ($) {
5156        #        #
5157      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5158        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5159              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5160                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5161              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5162                                
5163                unless (length $token->{data}) {            unless (length $token->{data}) {
5164                  !!!next-token;              !!!cp ('t194');
5165                  redo B;              !!!next-token;
5166                }              next B;
5167              }            } else {
5168                !!!cp ('t195');
5169              }
5170            }
5171    
5172              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5173    
5174              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5175              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3471  sub _tree_construction_main ($) { Line 5177  sub _tree_construction_main ($) {
5177              ## result in a new Text node.              ## result in a new Text node.
5178              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5179                            
5180              if ({              if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5181                # MUST                # MUST
5182                my $foster_parent_element;                my $foster_parent_element;
5183                my $next_sibling;                my $next_sibling;
5184                my $prev_sibling;                my $prev_sibling;
5185                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5186                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5187                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5188                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5189                        !!!cp ('t196');
5190                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5191                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5192                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5193                    } else {                    } else {
5194                        !!!cp ('t197');
5195                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5196                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5197                    }                    }
# Line 3498  sub _tree_construction_main ($) { Line 5203  sub _tree_construction_main ($) {
5203                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5204                if (defined $prev_sibling and                if (defined $prev_sibling and
5205                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5206                    !!!cp ('t198');
5207                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5208                } else {                } else {
5209                    !!!cp ('t199');
5210                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5211                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5212                     $next_sibling);                     $next_sibling);
5213                }                }
5214              } else {            $open_tables->[-1]->[1] = 1; # tainted
5215                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5216              }            !!!cp ('t200');
5217              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5218            }
5219                            
5220              !!!next-token;          !!!next-token;
5221              redo B;          next B;
5222        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5223              if ({          if ({
5224                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5225                   th => 1, td => 1,               th => 1, td => 1,
5226                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5227                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5228                  ## Clear back to table context              ## Clear back to table context
5229                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5230                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5231                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!cp ('t201');
5232                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5233                  }              }
5234                                
5235                  !!!insert-element ('tbody');              !!!insert-element ('tbody',, $token);
5236                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5237                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5238                }            }
5239              
5240                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5241                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5242                    !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t202');
5243                  }                !!!parse-error (type => 'missing start tag:tr', token => $token);
5244                }
5245                                    
5246                  ## Clear back to table body context              ## Clear back to table body context
5247                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5248                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5249                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5250                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                ## ISSUE: Can this case be reached?
5251                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5252                  }              }
5253                                    
5254                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5255                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5256                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5257                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5258                      !!!nack ('t204');
5259                    !!!next-token;                    !!!next-token;
5260                    redo B;                    next B;
5261                  } else {                  } else {
5262                    !!!insert-element ('tr');                    !!!cp ('t205');
5263                      !!!insert-element ('tr',, $token);
5264                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5265                  }                  }
5266                  } else {
5267                    !!!cp ('t206');
5268                }                }
5269    
5270                ## Clear back to table row context                ## Clear back to table row context
5271                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5272                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5273                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5274                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5275                }                }
5276                                
5277                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5278                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5279    
5280                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5281                                
5282                  !!!nack ('t207.1');
5283                !!!next-token;                !!!next-token;
5284                redo B;                next B;
5285              } elsif ({              } elsif ({
5286                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5287                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 3578  sub _tree_construction_main ($) { Line 5293  sub _tree_construction_main ($) {
5293                  my $i;                  my $i;
5294                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5295                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5296                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5297                        !!!cp ('t208');
5298                      $i = $_;                      $i = $_;
5299                      last INSCOPE;                      last INSCOPE;
5300                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5301                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5302                      last INSCOPE;                      last INSCOPE;
5303                    }                    }
5304                  } # INSCOPE                  } # INSCOPE
5305                  unless (defined $i) {                  unless (defined $i) {
5306                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5307    ## TODO: This type is wrong.
5308                      !!!parse-error (type => 'unmacthed end tag',
5309                                      text => $token->{tag_name}, token => $token);
5310                    ## Ignore the token                    ## Ignore the token
5311                      !!!nack ('t210.1');
5312                    !!!next-token;                    !!!next-token;
5313                    redo B;                    next B;
5314                  }                  }
5315                                    
5316                  ## Clear back to table row context                  ## Clear back to table row context
5317                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5318                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5319                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5320                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5321                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5322                  }                  }
5323                                    
5324                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5325                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5326                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5327                      !!!cp ('t212');
5328                    ## reprocess                    ## reprocess
5329                    redo B;                    !!!ack-later;
5330                      next B;
5331                  } else {                  } else {
5332                      !!!cp ('t213');
5333                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5334                  }                  }
5335                }                }
# Line 3617  sub _tree_construction_main ($) { Line 5339  sub _tree_construction_main ($) {
5339                  my $i;                  my $i;
5340                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5341                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5342                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5343                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5344                      $i = $_;                      $i = $_;
5345                      last INSCOPE;                      last INSCOPE;
5346                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5347                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5348                      last INSCOPE;                      last INSCOPE;
5349                    }                    }
5350                  } # INSCOPE                  } # INSCOPE
5351                  unless (defined $i) {                  unless (defined $i) {
5352                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5353    ## TODO: This erorr type is wrong.
5354                      !!!parse-error (type => 'unmatched end tag',
5355                                      text => $token->{tag_name}, token => $token);
5356                    ## Ignore the token                    ## Ignore the token
5357                      !!!nack ('t216.1');
5358                    !!!next-token;                    !!!next-token;
5359                    redo B;                    next B;
5360                  }                  }
5361    
5362                  ## Clear back to table body context                  ## Clear back to table body context
5363                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5364                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5365                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5366                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5367                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5368                  }                  }
5369                                    
# Line 3653  sub _tree_construction_main ($) { Line 5377  sub _tree_construction_main ($) {
5377                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5378                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5379                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5380                  } else {
5381                    !!!cp ('t218');
5382                }                }
5383    
5384                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5385                  ## Clear back to table context                  ## Clear back to table context
5386                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5387                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5388                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5389                      ## ISSUE: Can this state be reached?
5390                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5391                  }                  }
5392                                    
5393                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5394                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5395                  ## reprocess                  ## reprocess
5396                  redo B;                  !!!ack-later;
5397                    next B;
5398                } elsif ({                } elsif ({
5399                          caption => 1,                          caption => 1,
5400                          colgroup => 1,                          colgroup => 1,
5401                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5402                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5403                  ## Clear back to table context                  ## Clear back to table context
5404                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5405                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5406                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5407                      ## ISSUE: Can this state be reached?
5408                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5409                  }                  }
5410                                    
5411                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5412                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5413                                    
5414                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5415                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5416                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5417                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3691  sub _tree_construction_main ($) { Line 5420  sub _tree_construction_main ($) {
5420                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5421                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5422                  !!!next-token;                  !!!next-token;
5423                  redo B;                  !!!nack ('t220.1');
5424                    next B;
5425                } else {                } else {
5426                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5427                }                }
5428              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5429                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5430                                  text => $self->{open_elements}->[-1]->[0]
5431                                      ->manakai_local_name,
5432                                  token => $token);
5433    
5434                ## As if </table>                ## As if </table>
5435                ## have a table element in table scope                ## have a table element in table scope
5436                my $i;                my $i;
5437                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5438                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5439                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5440                      !!!cp ('t221');
5441                    $i = $_;                    $i = $_;
5442                    last INSCOPE;                    last INSCOPE;
5443                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5444                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5445                    last INSCOPE;                    last INSCOPE;
5446                  }                  }
5447                } # INSCOPE                } # INSCOPE
5448                unless (defined $i) {                unless (defined $i) {
5449                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5450    ## TODO: The following is wrong, maybe.
5451                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5452                                    token => $token);
5453                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5454                    !!!nack ('t223.1');
5455                  !!!next-token;                  !!!next-token;
5456                  redo B;                  next B;
5457                }                }
5458                                
5459    ## TODO: Followings are removed from the latest spec.
5460                ## generate implied end tags                ## generate implied end tags
5461                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5462                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5463                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5464                }                }
5465    
5466                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5467                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5468                    ## NOTE: |<table><tr><table>|
5469                    !!!parse-error (type => 'not closed',
5470                                    text => $self->{open_elements}->[-1]->[0]
5471                                        ->manakai_local_name,
5472                                    token => $token);
5473                  } else {
5474                    !!!cp ('t226');
5475                }                }
5476    
5477                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5478                  pop @{$open_tables};
5479    
5480                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5481    
5482                ## reprocess            ## reprocess
5483                redo B;            !!!ack-later;
5484          } else {            next B;
5485            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
5486              if (not $open_tables->[-1]->[1]) { # tainted
5487                !!!cp ('t227.8');
5488                ## NOTE: This is a "as if in head" code clone.
5489                $parse_rcdata->(CDATA_CONTENT_MODEL);
5490                next B;
5491              } else {
5492                !!!cp ('t227.7');
5493                #
5494              }
5495            } elsif ($token->{tag_name} eq 'script') {
5496              if (not $open_tables->[-1]->[1]) { # tainted
5497                !!!cp ('t227.6');
5498                ## NOTE: This is a "as if in head" code clone.
5499                $script_start_tag->();
5500                next B;
5501              } else {
5502                !!!cp ('t227.5');
5503                #
5504              }
5505            } elsif ($token->{tag_name} eq 'input') {
5506              if (not $open_tables->[-1]->[1]) { # tainted
5507                if ($token->{attributes}->{type}) { ## TODO: case
5508                  my $type = lc $token->{attributes}->{type}->{value};
5509                  if ($type eq 'hidden') {
5510                    !!!cp ('t227.3');
5511                    !!!parse-error (type => 'in table',
5512                                    text => $token->{tag_name}, token => $token);
5513    
5514            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5515    
5516                    ## TODO: form element pointer
5517    
5518                    pop @{$self->{open_elements}};
5519    
5520                    !!!next-token;
5521                    !!!ack ('t227.2.1');
5522                    next B;
5523                  } else {
5524                    !!!cp ('t227.2');
5525                    #
5526                  }
5527                } else {
5528                  !!!cp ('t227.1');
5529                  #
5530                }
5531              } else {
5532                !!!cp ('t227.4');
5533                #
5534              }
5535            } else {
5536              !!!cp ('t227');
5537            #            #
5538          }          }
5539    
5540            !!!parse-error (type => 'in table', text => $token->{tag_name},
5541                            token => $token);
5542    
5543            $insert = $insert_to_foster;
5544            #
5545        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5546              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5547                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3756  sub _tree_construction_main ($) { Line 5549  sub _tree_construction_main ($) {
5549                my $i;                my $i;
5550                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5551                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5552                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5553                      !!!cp ('t228');
5554                    $i = $_;                    $i = $_;
5555                    last INSCOPE;                    last INSCOPE;
5556                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5557                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5558                    last INSCOPE;                    last INSCOPE;
5559                  }                  }
5560                } # INSCOPE                } # INSCOPE
5561                unless (defined $i) {                unless (defined $i) {
5562                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5563                    !!!parse-error (type => 'unmatched end tag',
5564                                    text => $token->{tag_name}, token => $token);
5565                  ## Ignore the token                  ## Ignore the token
5566                    !!!nack ('t230.1');
5567                  !!!next-token;                  !!!next-token;
5568                  redo B;                  next B;
5569                  } else {
5570                    !!!cp ('t232');
5571                }                }
5572    
5573                ## Clear back to table row context                ## Clear back to table row context
5574                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5575                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5576                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5577                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5578                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5579                }                }
5580    
5581                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5582                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5583                !!!next-token;                !!!next-token;
5584                redo B;                !!!nack ('t231.1');
5585                  next B;
5586              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5587                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5588                  ## As if </tr>                  ## As if </tr>
# Line 3791  sub _tree_construction_main ($) { Line 5590  sub _tree_construction_main ($) {
5590                  my $i;                  my $i;
5591                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5592                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5593                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5594                        !!!cp ('t233');
5595                      $i = $_;                      $i = $_;
5596                      last INSCOPE;                      last INSCOPE;
5597                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5598                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5599                      last INSCOPE;                      last INSCOPE;
5600                    }                    }
5601                  } # INSCOPE                  } # INSCOPE
5602                  unless (defined $i) {                  unless (defined $i) {
5603                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5604    ## TODO: The following is wrong.
5605                      !!!parse-error (type => 'unmatched end tag',
5606                                      text => $token->{type}, token => $token);
5607                    ## Ignore the token                    ## Ignore the token
5608                      !!!nack ('t236.1');
5609                    !!!next-token;                    !!!next-token;
5610                    redo B;                    next B;
5611                  }                  }
5612                                    
5613                  ## Clear back to table row context                  ## Clear back to table row context
5614                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5615                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5616                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5617                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5618                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5619                  }                  }
5620                                    
# Line 3825  sub _tree_construction_main ($) { Line 5628  sub _tree_construction_main ($) {
5628                  my $i;                  my $i;
5629                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5630                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5631                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5632                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5633                      $i = $_;                      $i = $_;
5634                      last INSCOPE;                      last INSCOPE;
5635                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5636                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5637                      last INSCOPE;                      last INSCOPE;
5638                    }                    }
5639                  } # INSCOPE                  } # INSCOPE
5640                  unless (defined $i) {                  unless (defined $i) {
5641                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5642                      !!!parse-error (type => 'unmatched end tag',
5643                                      text => $token->{tag_name}, token => $token);
5644                    ## Ignore the token                    ## Ignore the token
5645                      !!!nack ('t239.1');
5646                    !!!next-token;                    !!!next-token;
5647                    redo B;                    next B;
5648                  }                  }
5649                                    
5650                  ## Clear back to table body context                  ## Clear back to table body context
5651                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5652                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5653                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5654                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5655                  }                  }
5656                                    
# Line 3863  sub _tree_construction_main ($) { Line 5666  sub _tree_construction_main ($) {
5666                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5667                }                }
5668    
5669                  ## NOTE: </table> in the "in table" insertion mode.
5670                  ## When you edit the code fragment below, please ensure that
5671                  ## the code for <table> in the "in table" insertion mode
5672                  ## is synced with it.
5673    
5674                ## have a table element in table scope                ## have a table element in table scope
5675                my $i;                my $i;
5676                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5677                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5678                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5679                      !!!cp ('t241');
5680                    $i = $_;                    $i = $_;
5681                    last INSCOPE;                    last INSCOPE;
5682                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5683                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5684                    last INSCOPE;                    last INSCOPE;
5685                  }                  }
5686                } # INSCOPE                } # INSCOPE
5687                unless (defined $i) {                unless (defined $i) {
5688                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5689                    !!!parse-error (type => 'unmatched end tag',
5690                                    text => $token->{tag_name}, token => $token);
5691                  ## Ignore the token                  ## Ignore the token
5692                    !!!nack ('t243.1');
5693                  !!!next-token;                  !!!next-token;
5694                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5695                }                }
5696                                    
5697                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5698                  pop @{$open_tables};
5699                                
5700                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5701                                
5702                !!!next-token;                !!!next-token;
5703                redo B;                next B;
5704              } elsif ({              } elsif ({
5705                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5706                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 3914  sub _tree_construction_main ($) { Line 5710  sub _tree_construction_main ($) {
5710                  my $i;                  my $i;
5711                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5712                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5713                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5714                        !!!cp ('t247');
5715                      $i = $_;                      $i = $_;
5716                      last INSCOPE;                      last INSCOPE;
5717                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5718                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
5719                      last INSCOPE;                      last INSCOPE;
5720                    }                    }
5721                  } # INSCOPE                  } # INSCOPE
5722                    unless (defined $i) {                    unless (defined $i) {
5723                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
5724                        !!!parse-error (type => 'unmatched end tag',
5725                                        text => $token->{tag_name}, token => $token);
5726                      ## Ignore the token                      ## Ignore the token
5727                        !!!nack ('t249.1');
5728                      !!!next-token;                      !!!next-token;
5729                      redo B;                      next B;
5730                    }                    }
5731                                    
5732                  ## As if </tr>                  ## As if </tr>
# Line 3935  sub _tree_construction_main ($) { Line 5734  sub _tree_construction_main ($) {
5734                  my $i;                  my $i;
5735                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5736                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5737                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5738                        !!!cp ('t250');
5739                      $i = $_;                      $i = $_;
5740                      last INSCOPE;                      last INSCOPE;
5741                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5742                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
5743                      last INSCOPE;                      last INSCOPE;
5744                    }                    }
5745                  } # INSCOPE                  } # INSCOPE
5746                    unless (defined $i) {                    unless (defined $i) {
5747                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
5748                        !!!parse-error (type => 'unmatched end tag',
5749                                        text => 'tr', token => $token);
5750                      ## Ignore the token                      ## Ignore the token
5751                        !!!nack ('t252.1');
5752                      !!!next-token;                      !!!next-token;
5753                      redo B;                      next B;
5754                    }                    }
5755                                    
5756                  ## Clear back to table row context                  ## Clear back to table row context
5757                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5758                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5759                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
5760                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5761                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5762                  }                  }
5763                                    
# Line 3968  sub _tree_construction_main ($) { Line 5770  sub _tree_construction_main ($) {
5770                my $i;                my $i;
5771                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5772                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5773                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5774                      !!!cp ('t254');
5775                    $i = $_;                    $i = $_;
5776                    last INSCOPE;                    last INSCOPE;
5777                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5778                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5779                    last INSCOPE;                    last INSCOPE;
5780                  }                  }
5781                } # INSCOPE                } # INSCOPE
5782                unless (defined $i) {                unless (defined $i) {
5783                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5784                    !!!parse-error (type => 'unmatched end tag',
5785                                    text => $token->{tag_name}, token => $token);
5786                  ## Ignore the token                  ## Ignore the token
5787                    !!!nack ('t256.1');
5788                  !!!next-token;                  !!!next-token;
5789                  redo B;                  next B;
5790                }                }
5791    
5792                ## Clear back to table body context                ## Clear back to table body context
5793                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5794                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5795                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5796                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5797                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5798                }                }
5799    
5800                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5801                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5802                  !!!nack ('t257.1');
5803                !!!next-token;                !!!next-token;
5804                redo B;                next B;
5805              } elsif ({              } elsif ({
5806                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5807                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5808                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5809                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5810                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5811                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5812                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
5813                !!!next-token;                            text => $token->{tag_name}, token => $token);
5814                redo B;            ## Ignore the token
5815          } else {            !!!nack ('t258.1');
5816            !!!parse-error (type => 'in table:/'.$token->{tag_name});             !!!next-token;
5817              next B;
5818            } else {
5819              !!!cp ('t259');
5820              !!!parse-error (type => 'in table:/',
5821                              text => $token->{tag_name}, token => $token);
5822    
5823            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5824            #            #
5825          }          }
5826          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5827            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5828                    @{$self->{open_elements}} == 1) { # redundant, maybe
5829              !!!parse-error (type => 'in body:#eof', token => $token);
5830              !!!cp ('t259.1');
5831              #
5832            } else {
5833              !!!cp ('t259.2');
5834              #
5835            }
5836    
5837            ## Stop parsing
5838            last B;
5839        } else {        } else {
5840          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5841        }        }
# Line 4020  sub _tree_construction_main ($) { Line 5844  sub _tree_construction_main ($) {
5844              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5845                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5846                unless (length $token->{data}) {                unless (length $token->{data}) {
5847                    !!!cp ('t260');
5848                  !!!next-token;                  !!!next-token;
5849                  redo B;                  next B;
5850                }                }
5851              }              }
5852                            
5853                !!!cp ('t261');
5854              #              #
5855            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5856              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5857                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
5858                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5859                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5860                  !!!ack ('t262.1');
5861                !!!next-token;                !!!next-token;
5862                redo B;                next B;
5863              } else {              } else {
5864                  !!!cp ('t263');
5865                #                #
5866              }              }
5867            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5868              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5869                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5870                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5871                    !!!parse-error (type => 'unmatched end tag',
5872                                    text => 'colgroup', token => $token);
5873                  ## Ignore the token                  ## Ignore the token
5874                  !!!next-token;                  !!!next-token;
5875                  redo B;                  next B;
5876                } else {                } else {
5877                    !!!cp ('t265');
5878                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5879                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5880                  !!!next-token;                  !!!next-token;
5881                  redo B;                              next B;            
5882                }                }
5883              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5884                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5885                  !!!parse-error (type => 'unmatched end tag',
5886                                  text => 'col', token => $token);
5887                ## Ignore the token                ## Ignore the token
5888                !!!next-token;                !!!next-token;
5889                redo B;                next B;
5890              } else {              } else {
5891                  !!!cp ('t267');
5892                #                #
5893              }              }
5894            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5895              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5896            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5897              !!!cp ('t270.2');
5898              ## Stop parsing.
5899              last B;
5900            } else {
5901              ## NOTE: As if </colgroup>.
5902              !!!cp ('t270.1');
5903              pop @{$self->{open_elements}}; # colgroup
5904              $self->{insertion_mode} = IN_TABLE_IM;
5905              ## Reprocess.
5906              next B;
5907            }
5908          } else {
5909            die "$0: $token->{type}: Unknown token type";
5910          }
5911    
5912            ## As if </colgroup>            ## As if </colgroup>
5913            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5914              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5915    ## TODO: Wrong error type?
5916                !!!parse-error (type => 'unmatched end tag',
5917                                text => 'colgroup', token => $token);
5918              ## Ignore the token              ## Ignore the token
5919                !!!nack ('t269.1');
5920              !!!next-token;              !!!next-token;
5921              redo B;              next B;
5922            } else {            } else {
5923                !!!cp ('t270');
5924              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5925              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5926                !!!ack-later;
5927              ## reprocess              ## reprocess
5928              redo B;              next B;
5929            }            }
5930      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5931        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5932            !!!cp ('t271');
5933          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5934          !!!next-token;          !!!next-token;
5935          redo B;          next B;
5936        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5937              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5938                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5939                  ## As if </option>              !!!cp ('t272');
5940                  pop @{$self->{open_elements}};              ## As if </option>
5941                }              pop @{$self->{open_elements}};
5942              } else {
5943                !!!cp ('t273');
5944              }
5945    
5946                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5947                !!!next-token;            !!!nack ('t273.1');
5948                redo B;            !!!next-token;
5949              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5950                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5951                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5952                  pop @{$self->{open_elements}};              !!!cp ('t274');
5953                }              ## As if </option>
5954                pop @{$self->{open_elements}};
5955              } else {
5956                !!!cp ('t275');
5957              }
5958    
5959                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5960                  ## As if </optgroup>              !!!cp ('t276');
5961                  pop @{$self->{open_elements}};              ## As if </optgroup>
5962                }              pop @{$self->{open_elements}};
5963              } else {
5964                !!!cp ('t277');
5965              }
5966    
5967                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5968                !!!next-token;            !!!nack ('t277.1');
5969                redo B;            !!!next-token;
5970              } elsif ($token->{tag_name} eq 'select') {            next B;
5971                !!!parse-error (type => 'not closed:select');          } elsif ({
5972                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
5973                ## have an element in table scope                   }->{$token->{tag_name}} or
5974                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5975                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
5976                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
5977                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
5978                    $i = $_;                     tr => 1, td => 1, th => 1,
5979                    last INSCOPE;                    }->{$token->{tag_name}})) {
5980                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
5981                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
5982                           }->{$node->[1]}) {                            token => $token);
5983                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
5984                  }            ## as if there were </select> (otherwise).
5985                } # INSCOPE            ## have an element in table scope
5986                unless (defined $i) {            my $i;
5987                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5988                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
5989                  !!!next-token;              if ($node->[1] & SELECT_EL) {
5990                  redo B;                !!!cp ('t278');
5991                }                $i = $_;
5992                  last INSCOPE;
5993                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5994                  !!!cp ('t279');
5995                  last INSCOPE;
5996                }
5997              } # INSCOPE
5998              unless (defined $i) {
5999                !!!cp ('t280');
6000                !!!parse-error (type => 'unmatched end tag',
6001                                text => 'select', token => $token);
6002                ## Ignore the token
6003                !!!nack ('t280.1');
6004                !!!next-token;
6005                next B;
6006              }
6007                                
6008                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6009              splice @{$self->{open_elements}}, $i;
6010    
6011                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6012    
6013                !!!next-token;            if ($token->{tag_name} eq 'select') {
6014                redo B;              !!!nack ('t281.2');
6015                !!!next-token;
6016                next B;
6017              } else {
6018                !!!cp ('t281.1');
6019                !!!ack-later;
6020                ## Reprocess the token.
6021                next B;
6022              }
6023          } else {          } else {
6024            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
6025              !!!parse-error (type => 'in select',
6026                              text => $token->{tag_name}, token => $token);
6027            ## Ignore the token            ## Ignore the token
6028              !!!nack ('t282.1');
6029            !!!next-token;            !!!next-token;
6030            redo B;            next B;
6031          }          }
6032        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6033              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6034                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6035                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6036                  ## As if </option>              !!!cp ('t283');
6037                  splice @{$self->{open_elements}}, -2;              ## As if </option>
6038                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
6039                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6040                } else {              !!!cp ('t284');
6041                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
6042                  ## Ignore the token            } else {
6043                }              !!!cp ('t285');
6044                !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6045                redo B;                              text => $token->{tag_name}, token => $token);
6046              } elsif ($token->{tag_name} eq 'option') {              ## Ignore the token
6047                if ($self->{open_elements}->[-1]->[1] eq 'option') {            }
6048                  pop @{$self->{open_elements}};            !!!nack ('t285.1');
6049                } else {            !!!next-token;
6050                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            next B;
6051                  ## Ignore the token          } elsif ($token->{tag_name} eq 'option') {
6052                }            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6053                !!!next-token;              !!!cp ('t286');
6054                redo B;              pop @{$self->{open_elements}};
6055              } elsif ($token->{tag_name} eq 'select') {            } else {
6056                ## have an element in table scope              !!!cp ('t287');
6057                my $i;              !!!parse-error (type => 'unmatched end tag',
6058                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                              text => $token->{tag_name}, token => $token);
6059                  my $node = $self->{open_elements}->[$_];              ## Ignore the token
6060                  if ($node->[1] eq $token->{tag_name}) {            }
6061                    $i = $_;            !!!nack ('t287.1');
6062                    last INSCOPE;            !!!next-token;
6063                  } elsif ({            next B;
6064                            table => 1, html => 1,          } elsif ($token->{tag_name} eq 'select') {
6065                           }->{$node->[1]}) {            ## have an element in table scope
6066                    last INSCOPE;            my $i;
6067                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6068                } # INSCOPE              my $node = $self->{open_elements}->[$_];
6069                unless (defined $i) {              if ($node->[1] & SELECT_EL) {
6070                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t288');
6071                  ## Ignore the token                $i = $_;
6072                  !!!next-token;                last INSCOPE;
6073                  redo B;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6074                }                !!!cp ('t289');
6075                  last INSCOPE;
6076                }
6077              } # INSCOPE
6078              unless (defined $i) {
6079                !!!cp ('t290');
6080                !!!parse-error (type => 'unmatched end tag',
6081                                text => $token->{tag_name}, token => $token);
6082                ## Ignore the token
6083                !!!nack ('t290.1');
6084                !!!next-token;
6085                next B;
6086              }
6087                                
6088                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6089              splice @{$self->{open_elements}}, $i;
6090    
6091                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6092    
6093                !!!next-token;            !!!nack ('t291.1');
6094                redo B;            !!!next-token;
6095              } elsif ({            next B;
6096                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6097                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6098                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6099                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6100                     }->{$token->{tag_name}}) {
6101    ## TODO: The following is wrong?
6102              !!!parse-error (type => 'unmatched end tag',
6103                              text => $token->{tag_name}, token => $token);
6104                                
6105                ## have an element in table scope            ## have an element in table scope
6106                my $i;            my $i;
6107                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6108                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6109                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6110                    $i = $_;                !!!cp ('t292');
6111                    last INSCOPE;                $i = $_;
6112                  } elsif ({                last INSCOPE;
6113                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6114                           }->{$node->[1]}) {                !!!cp ('t293');
6115                    last INSCOPE;                last INSCOPE;
6116                  }              }
6117                } # INSCOPE            } # INSCOPE
6118                unless (defined $i) {            unless (defined $i) {
6119                  ## Ignore the token              !!!cp ('t294');
6120                  !!!next-token;              ## Ignore the token
6121                  redo B;              !!!nack ('t294.1');
6122                }              !!!next-token;
6123                next B;
6124              }
6125                                
6126                ## As if </select>            ## As if </select>
6127                ## have an element in table scope            ## have an element in table scope
6128                undef $i;            undef $i;
6129                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6130                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6131                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6132                    $i = $_;                !!!cp ('t295');
6133                    last INSCOPE;                $i = $_;
6134                  } elsif ({                last INSCOPE;
6135                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6136                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
6137                    last INSCOPE;                !!!cp ('t296');
6138                  }                last INSCOPE;
6139                } # INSCOPE              }
6140                unless (defined $i) {            } # INSCOPE
6141                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
6142                  ## Ignore the </select> token              !!!cp ('t297');
6143                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
6144                  redo B;              !!!parse-error (type => 'unmatched end tag',
6145                }                              text => 'select', token => $token);
6146                ## Ignore the </select> token
6147                !!!nack ('t297.1');
6148                !!!next-token; ## TODO: ok?
6149                next B;
6150              }
6151                                
6152                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
6153              splice @{$self->{open_elements}}, $i;
6154    
6155                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6156    
6157                ## reprocess            !!!ack-later;
6158                redo B;            ## reprocess
6159              next B;
6160          } else {          } else {
6161            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
6162              !!!parse-error (type => 'in select:/',
6163                              text => $token->{tag_name}, token => $token);
6164            ## Ignore the token            ## Ignore the token
6165              !!!nack ('t299.3');
6166            !!!next-token;            !!!next-token;
6167            redo B;            next B;
6168          }          }
6169          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6170            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6171                    @{$self->{open_elements}} == 1) { # redundant, maybe
6172              !!!cp ('t299.1');
6173              !!!parse-error (type => 'in body:#eof', token => $token);
6174            } else {
6175              !!!cp ('t299.2');
6176            }
6177    
6178            ## Stop parsing.
6179            last B;
6180        } else {        } else {
6181          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6182        }        }
# Line 4257  sub _tree_construction_main ($) { Line 6190  sub _tree_construction_main ($) {
6190            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6191                        
6192            unless (length $token->{data}) {            unless (length $token->{data}) {
6193                !!!cp ('t300');
6194              !!!next-token;              !!!next-token;
6195              redo B;              next B;
6196            }            }
6197          }          }
6198                    
6199          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6200            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
6201              !!!parse-error (type => 'after html:#text', token => $token);
6202    
6203            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6204            } else {
6205              !!!cp ('t302');
6206          }          }
6207                    
6208          ## "after body" insertion mode          ## "after body" insertion mode
6209          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6210    
6211          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6212          ## reprocess          ## reprocess
6213          redo B;          next B;
6214        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6215          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6216            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
6217              !!!parse-error (type => 'after html',
6218                              text => $token->{tag_name}, token => $token);
6219                        
6220            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6221            } else {
6222              !!!cp ('t304');
6223          }          }
6224    
6225          ## "after body" insertion mode          ## "after body" insertion mode
6226          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6227                            text => $token->{tag_name}, token => $token);
6228    
6229          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6230            !!!ack-later;
6231          ## reprocess          ## reprocess
6232          redo B;          next B;
6233        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6234          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6235            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
6236              !!!parse-error (type => 'after html:/',
6237                              text => $token->{tag_name}, token => $token);
6238                        
6239            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6240            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6241            } else {
6242              !!!cp ('t306');
6243          }          }
6244    
6245          ## "after body" insertion mode          ## "after body" insertion mode
6246          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6247            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6248              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
6249                !!!parse-error (type => 'unmatched end tag',
6250                                text => 'html', token => $token);
6251              ## Ignore the token              ## Ignore the token
6252              !!!next-token;              !!!next-token;
6253              redo B;              next B;
6254            } else {            } else {
6255                !!!cp ('t308');
6256              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6257              !!!next-token;              !!!next-token;
6258              redo B;              next B;
6259            }            }
6260          } else {          } else {
6261            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
6262              !!!parse-error (type => 'after body:/',
6263                              text => $token->{tag_name}, token => $token);
6264    
6265            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6266            ## reprocess            ## reprocess
6267            redo B;            next B;
6268          }          }
6269          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6270            !!!cp ('t309.2');
6271            ## Stop parsing
6272            last B;
6273        } else {        } else {
6274          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6275        }        }
# Line 4323  sub _tree_construction_main ($) { Line 6279  sub _tree_construction_main ($) {
6279            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6280                        
6281            unless (length $token->{data}) {            unless (length $token->{data}) {
6282                !!!cp ('t310');
6283              !!!next-token;              !!!next-token;
6284              redo B;              next B;
6285            }            }
6286          }          }
6287                    
6288          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6289            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6290              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6291                !!!parse-error (type => 'in frameset:#text', token => $token);
6292            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6293              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
6294                !!!parse-error (type => 'after frameset:#text', token => $token);
6295            } else { # "after html frameset"            } else { # "after html frameset"
6296              !!!parse-error (type => 'after html:#character');              !!!cp ('t313');
6297                !!!parse-error (type => 'after html:#text', token => $token);
6298    
6299              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6300              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
6301              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#text', token => $token);
6302            }            }
6303                        
6304            ## Ignore the token.            ## Ignore the token.
6305            if (length $token->{data}) {            if (length $token->{data}) {
6306                !!!cp ('t314');
6307              ## reprocess the rest of characters              ## reprocess the rest of characters
6308            } else {            } else {
6309                !!!cp ('t315');
6310              !!!next-token;              !!!next-token;
6311            }            }
6312            redo B;            next B;
6313          }          }
6314                    
6315          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6316        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6317          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6318            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t316');
6319              !!!parse-error (type => 'after html',
6320                              text => $token->{tag_name}, token => $token);
6321    
6322            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6323            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6324          }          } else {
6325              !!!cp ('t317');
6326            }
6327    
6328          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6329              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6330            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6331              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6332              !!!nack ('t318.1');
6333            !!!next-token;            !!!next-token;
6334            redo B;            next B;
6335          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6336                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6337            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6338              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6339            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6340              !!!ack ('t319.1');
6341            !!!next-token;            !!!next-token;
6342            redo B;            next B;
6343          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6344            ## NOTE: As if in body.            !!!cp ('t320');
6345            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            ## NOTE: As if in head.
6346            redo B;            $parse_rcdata->(CDATA_CONTENT_MODEL);
6347              next B;
6348          } else {          } else {
6349            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6350              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6351                !!!parse-error (type => 'in frameset',
6352                                text => $token->{tag_name}, token => $token);
6353            } else {            } else {
6354              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!cp ('t322');
6355                !!!parse-error (type => 'after frameset',
6356                                text => $token->{tag_name}, token => $token);
6357            }            }
6358            ## Ignore the token            ## Ignore the token
6359              !!!nack ('t322.1');
6360            !!!next-token;            !!!next-token;
6361            redo B;            next B;
6362          }          }
6363        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6364          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6365            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t323');
6366              !!!parse-error (type => 'after html:/',
6367                              text => $token->{tag_name}, token => $token);
6368    
6369            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6370            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6371            } else {
6372              !!!cp ('t324');
6373          }          }
6374    
6375          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6376              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6377            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6378                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6379              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6380                !!!parse-error (type => 'unmatched end tag',
6381                                text => $token->{tag_name}, token => $token);
6382              ## Ignore the token              ## Ignore the token
6383              !!!next-token;              !!!next-token;
6384            } else {            } else {
6385                !!!cp ('t326');
6386              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6387              !!!next-token;              !!!next-token;
6388            }            }
6389    
6390            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6391                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6392                !!!cp ('t327');
6393              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6394              } else {
6395                !!!cp ('t328');
6396            }            }
6397            redo B;            next B;
6398          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6399                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6400              !!!cp ('t329');
6401            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6402            !!!next-token;            !!!next-token;
6403            redo B;            next B;
6404          } else {          } else {
6405            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6406              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6407                !!!parse-error (type => 'in frameset:/',
6408                                text => $token->{tag_name}, token => $token);
6409            } else {            } else {
6410              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!cp ('t331');
6411                !!!parse-error (type => 'after frameset:/',
6412                                text => $token->{tag_name}, token => $token);
6413            }            }
6414            ## Ignore the token            ## Ignore the token
6415            !!!next-token;            !!!next-token;
6416            redo B;            next B;
6417          }          }
6418          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6419            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6420                    @{$self->{open_elements}} == 1) { # redundant, maybe
6421              !!!cp ('t331.1');
6422              !!!parse-error (type => 'in body:#eof', token => $token);
6423            } else {
6424              !!!cp ('t331.2');
6425            }
6426            
6427            ## Stop parsing
6428            last B;
6429        } else {        } else {
6430          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6431        }        }
# Line 4436  sub _tree_construction_main ($) { Line 6438  sub _tree_construction_main ($) {
6438      ## "in body" insertion mode      ## "in body" insertion mode
6439      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
6440        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6441            !!!cp ('t332');
6442          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6443          $script_start_tag->($insert);          $script_start_tag->();
6444          redo B;          next B;
6445        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6446            !!!cp ('t333');
6447          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6448          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6449          redo B;          next B;
6450        } elsif ({        } elsif ({
6451                  base => 1, link => 1,                  base => 1, link => 1,
6452                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6453            !!!cp ('t334');
6454          ## 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
6455          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6456          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6457            !!!ack ('t334.1');
6458          !!!next-token;          !!!next-token;
6459          redo B;          next B;
6460        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6461          ## 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
6462          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6463          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6464    
6465          unless ($self->{confident}) {          unless ($self->{confident}) {
6466            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6467                !!!cp ('t335');
6468                ## NOTE: Whether the encoding is supported or not is handled
6469                ## in the {change_encoding} callback.
6470              $self->{change_encoding}              $self->{change_encoding}
6471                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6472                
6473                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6474                    ->set_user_data (manakai_has_reference =>
6475                                         $token->{attributes}->{charset}
6476                                             ->{has_reference});
6477            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6478              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6479                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6480                        [\x09-\x0D\x20]*=
6481                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6482                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6483                  !!!cp ('t336');
6484                  ## NOTE: Whether the encoding is supported or not is handled
6485                  ## in the {change_encoding} callback.
6486                $self->{change_encoding}                $self->{change_encoding}
6487                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6488                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6489                      ->set_user_data (manakai_has_reference =>
6490                                           $token->{attributes}->{content}
6491                                                 ->{has_reference});
6492              }              }
6493            }            }
6494            } else {
6495              if ($token->{attributes}->{charset}) {
6496                !!!cp ('t337');
6497                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6498                    ->set_user_data (manakai_has_reference =>
6499                                         $token->{attributes}->{charset}
6500                                             ->{has_reference});
6501              }
6502              if ($token->{attributes}->{content}) {
6503                !!!cp ('t338');
6504                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6505                    ->set_user_data (manakai_has_reference =>
6506                                         $token->{attributes}->{content}
6507                                             ->{has_reference});
6508              }
6509          }          }
6510    
6511            !!!ack ('t338.1');
6512          !!!next-token;          !!!next-token;
6513          redo B;          next B;
6514        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6515          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6516          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6517          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6518            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6519        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6520          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6521                                
6522          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6523              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6524              !!!cp ('t342');
6525            ## Ignore the token            ## Ignore the token
6526          } else {          } else {
6527            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6528            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6529              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6530                  !!!cp ('t343');
6531                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6532                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6533                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6534              }              }
6535            }            }
6536          }          }
6537            !!!nack ('t343.1');
6538          !!!next-token;          !!!next-token;
6539          redo B;          next B;
6540        } elsif ({        } elsif ({
6541                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6542                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6543                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6544                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6545                  pre => 1,                  pre => 1, listing => 1,
6546                    form => 1,
6547                    table => 1,
6548                    hr => 1,
6549                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6550            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6551              !!!cp ('t350');
6552              !!!parse-error (type => 'in form:form', token => $token);
6553              ## Ignore the token
6554              !!!nack ('t350.1');
6555              !!!next-token;
6556              next B;
6557            }
6558    
6559          ## has a p element in scope          ## has a p element in scope
6560          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6561            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6562              !!!back-token;              !!!cp ('t344');
6563              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
6564              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6565            } elsif ({                        line => $token->{line}, column => $token->{column}};
6566                      table => 1, caption => 1, td => 1, th => 1,              next B;
6567                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6568                     }->{$_->[1]}) {              !!!cp ('t345');
6569              last INSCOPE;              last INSCOPE;
6570            }            }
6571          } # INSCOPE          } # INSCOPE
6572                        
6573          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6574          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6575              !!!nack ('t346.1');
6576            !!!next-token;            !!!next-token;
6577            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6578              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6579              unless (length $token->{data}) {              unless (length $token->{data}) {
6580                  !!!cp ('t346');
6581                !!!next-token;                !!!next-token;
6582                } else {
6583                  !!!cp ('t349');
6584              }              }
6585              } else {
6586                !!!cp ('t348');
6587            }            }
6588          } else {          } elsif ($token->{tag_name} eq 'form') {
6589              !!!cp ('t347.1');
6590              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6591    
6592              !!!nack ('t347.2');
6593            !!!next-token;            !!!next-token;
6594          }          } elsif ($token->{tag_name} eq 'table') {
6595          redo B;            !!!cp ('t382');
6596        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6597          if (defined $self->{form_element}) {            
6598            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6599            ## Ignore the token  
6600              !!!nack ('t382.1');
6601              !!!next-token;
6602            } elsif ($token->{tag_name} eq 'hr') {
6603              !!!cp ('t386');
6604              pop @{$self->{open_elements}};
6605            
6606              !!!nack ('t386.1');
6607            !!!next-token;            !!!next-token;
           redo B;  
6608          } else {          } else {
6609            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!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]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6610            !!!next-token;            !!!next-token;
           redo B;  
6611          }          }
6612        } elsif ($token->{tag_name} eq 'li') {          next B;
6613          ## has a p element in scope        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!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]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
6614          ## has a p element in scope          ## has a p element in scope
6615          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6616            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6617              !!!back-token;              !!!cp ('t353');
6618              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
6619              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6620            } elsif ({                        line => $token->{line}, column => $token->{column}};
6621                      table => 1, caption => 1, td => 1, th => 1,              next B;
6622                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6623                     }->{$_->[1]}) {              !!!cp ('t354');
6624              last INSCOPE;              last INSCOPE;
6625            }            }
6626          } # INSCOPE          } # INSCOPE
# Line 4627  sub _tree_construction_main ($) { Line 6628  sub _tree_construction_main ($) {
6628          ## Step 1          ## Step 1
6629          my $i = -1;          my $i = -1;
6630          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6631            my $li_or_dtdd = {li => {li => 1},
6632                              dt => {dt => 1, dd => 1},
6633                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6634          LI: {          LI: {
6635            ## Step 2            ## Step 2
6636            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6637              if ($i != -1) {              if ($i != -1) {
6638                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6639                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6640                                  text => $self->{open_elements}->[-1]->[0]
6641                                      ->manakai_local_name,
6642                                  token => $token);
6643                } else {
6644                  !!!cp ('t356');
6645              }              }
6646              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6647              last LI;              last LI;
6648              } else {
6649                !!!cp ('t357');
6650            }            }
6651                        
6652            ## Step 3            ## Step 3
6653            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6654                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6655                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6656                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6657                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6658                  not ($node->[1] & DIV_EL)) {
6659                !!!cp ('t358');
6660              last LI;              last LI;
6661            }            }
6662                        
6663              !!!cp ('t359');
6664            ## Step 4            ## Step 4
6665            $i--;            $i--;
6666            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
6667            redo LI;            redo LI;
6668          } # LI          } # LI
6669                        
6670          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6671            !!!nack ('t359.1');
6672          !!!next-token;          !!!next-token;
6673          redo B;          next B;
6674        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6675          ## has a p element in scope          ## has a p element in scope
6676          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6677            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6678              !!!back-token;              !!!cp ('t367');
6679              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
6680              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6681            } elsif ({                        line => $token->{line}, column => $token->{column}};
6682                      table => 1, caption => 1, td => 1, th => 1,              next B;
6683                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6684                     }->{$_->[1]}) {              !!!cp ('t368');
6685              last INSCOPE;              last INSCOPE;
6686            }            }
6687          } # INSCOPE          } # INSCOPE
6688                        
6689          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6690                        
6691          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6692                        
6693            !!!nack ('t368.1');
6694          !!!next-token;          !!!next-token;
6695          redo B;          next B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
6696        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6697          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6698            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6699            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6700              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
6701                !!!parse-error (type => 'in a:a', token => $token);
6702                            
6703              !!!back-token;              !!!back-token; # <a>
6704              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6705              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6706                $formatting_end_tag->($token);
6707                            
6708              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6709                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6710                    !!!cp ('t372');
6711                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
6712                  last AFE2;                  last AFE2;
6713                }                }
6714              } # AFE2              } # AFE2
6715              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
6716                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6717                    !!!cp ('t373');
6718                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
6719                  last OE;                  last OE;
6720                }                }
6721              } # OE              } # OE
6722              last AFE;              last AFE;
6723            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
6724                !!!cp ('t374');
6725              last AFE;              last AFE;
6726            }            }
6727          } # AFE          } # AFE
6728                        
6729          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6730    
6731          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6732          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6733    
6734            !!!nack ('t374.1');
6735          !!!next-token;          !!!next-token;
6736          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}}) {  
         $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;  
6737        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6738          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6739    
6740          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6741          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6742            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6743            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6744              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
6745              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
6746              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
6747              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6748            } elsif ({                        line => $token->{line}, column => $token->{column}};
6749                      table => 1, caption => 1, td => 1, th => 1,              next B;
6750                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6751                     }->{$node->[1]}) {              !!!cp ('t377');
6752              last INSCOPE;              last INSCOPE;
6753            }            }
6754          } # INSCOPE          } # INSCOPE
6755                    
6756          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6757          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6758                    
6759            !!!nack ('t377.1');
6760          !!!next-token;          !!!next-token;
6761          redo B;          next B;
6762        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6763          ## has a button element in scope          ## has a button element in scope
6764          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6765            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6766            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6767              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
6768              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
6769              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
6770              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6771            } elsif ({                        line => $token->{line}, column => $token->{column}};
6772                      table => 1, caption => 1, td => 1, th => 1,              next B;
6773                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6774                     }->{$node->[1]}) {              !!!cp ('t379');
6775              last INSCOPE;              last INSCOPE;
6776            }            }
6777          } # INSCOPE          } # INSCOPE
6778                        
6779          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6780                        
6781          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6782          push @$active_formatting_elements, ['#marker', ''];  
6783            ## TODO: associate with $self->{form_element} if defined
6784    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6785          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6786            
6787          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $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') {  
             !!!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]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
6788          !!!next-token;          !!!next-token;
6789          redo B;          next B;
6790        } elsif ({        } elsif ({
6791                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6792                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6793                  image => 1,                  noembed => 1,
6794                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6795                    noscript => 0, ## TODO: 1 if scripting is enabled
6796                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6797          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6798            !!!parse-error (type => 'image');            !!!cp ('t381');
6799            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
6800            } else {
6801              !!!cp ('t399');
6802          }          }
6803            ## NOTE: There is an "as if in body" code clone.
6804          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6805          $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') {  
             !!!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]}) {  
             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') {  
         $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;  
6806        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6807          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6808                    
6809          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6810              !!!cp ('t389');
6811            ## Ignore the token            ## Ignore the token
6812              !!!nack ('t389'); ## NOTE: Not acknowledged.
6813            !!!next-token;            !!!next-token;
6814            redo B;            next B;
6815          } else {          } else {
6816              !!!ack ('t391.1');
6817    
6818            my $at = $token->{attributes};            my $at = $token->{attributes};
6819            my $form_attrs;            my $form_attrs;
6820            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 4915  sub _tree_construction_main ($) { Line 6824  sub _tree_construction_main ($) {
6824            delete $at->{prompt};            delete $at->{prompt};
6825            my @tokens = (            my @tokens = (
6826                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6827                           attributes => $form_attrs},                           attributes => $form_attrs,
6828                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6829                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6830                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6831                            {type => START_TAG_TOKEN, tag_name => 'p',
6832                             line => $token->{line}, column => $token->{column}},
6833                            {type => START_TAG_TOKEN, tag_name => 'label',
6834                             line => $token->{line}, column => $token->{column}},
6835                         );                         );
6836            if ($prompt_attr) {            if ($prompt_attr) {
6837              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
6838                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6839                               #line => $token->{line}, column => $token->{column},
6840                              };
6841            } else {            } else {
6842                !!!cp ('t391');
6843              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6844                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6845                               #line => $token->{line}, column => $token->{column},
6846                              }; # SHOULD
6847              ## TODO: make this configurable              ## TODO: make this configurable
6848            }            }
6849            push @tokens,            push @tokens,
6850                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6851                             line => $token->{line}, column => $token->{column}},
6852                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6853                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6854                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6855                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6856                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6857            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6858                             line => $token->{line}, column => $token->{column}},
6859                            {type => END_TAG_TOKEN, tag_name => 'form',
6860                             line => $token->{line}, column => $token->{column}};
6861            !!!back-token (@tokens);            !!!back-token (@tokens);
6862            redo B;            !!!next-token;
6863              next B;
6864          }          }
6865        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6866          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6867          my $el;          my $el;
6868          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6869                    
6870          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6871          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4950  sub _tree_construction_main ($) { Line 6874  sub _tree_construction_main ($) {
6874          $insert->($el);          $insert->($el);
6875                    
6876          my $text = '';          my $text = '';
6877            !!!nack ('t392.1');
6878          !!!next-token;          !!!next-token;
6879          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6880            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
6881            unless (length $token->{data}) {            unless (length $token->{data}) {
6882                !!!cp ('t392');
6883              !!!next-token;              !!!next-token;
6884              } else {
6885                !!!cp ('t393');
6886            }            }
6887            } else {
6888              !!!cp ('t394');
6889          }          }
6890          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
6891              !!!cp ('t395');
6892            $text .= $token->{data};            $text .= $token->{data};
6893            !!!next-token;            !!!next-token;
6894          }          }
6895          if (length $text) {          if (length $text) {
6896              !!!cp ('t396');
6897            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6898          }          }
6899                    
# Line 4969  sub _tree_construction_main ($) { Line 6901  sub _tree_construction_main ($) {
6901                    
6902          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
6903              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6904              !!!cp ('t397');
6905            ## Ignore the token            ## Ignore the token
6906          } else {          } else {
6907            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6908              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6909          }          }
6910          !!!next-token;          !!!next-token;
6911            next B;
6912          } elsif ($token->{tag_name} eq 'rt' or
6913                   $token->{tag_name} eq 'rp') {
6914            ## has a |ruby| element in scope
6915            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6916              my $node = $self->{open_elements}->[$_];
6917              if ($node->[1] & RUBY_EL) {
6918                !!!cp ('t398.1');
6919                ## generate implied end tags
6920                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6921                  !!!cp ('t398.2');
6922                  pop @{$self->{open_elements}};
6923                }
6924                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6925                  !!!cp ('t398.3');
6926                  !!!parse-error (type => 'not closed',
6927                                  text => $self->{open_elements}->[-1]->[0]
6928                                      ->manakai_local_name,
6929                                  token => $token);
6930                  pop @{$self->{open_elements}}
6931                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6932                }
6933                last INSCOPE;
6934              } elsif ($node->[1] & SCOPING_EL) {
6935                !!!cp ('t398.4');
6936                last INSCOPE;
6937              }
6938            } # INSCOPE
6939    
6940            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6941    
6942            !!!nack ('t398.5');
6943            !!!next-token;
6944          redo B;          redo B;
6945        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6946                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
6947          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6948    
6949            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6950    
6951            ## "adjust foreign attributes" - done in insert-element-f
6952                    
6953          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6954                    
6955          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6956              pop @{$self->{open_elements}};
6957              !!!ack ('t398.1');
6958            } else {
6959              !!!cp ('t398.2');
6960              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6961              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6962              ## mode, "in body" (not "in foreign content") secondary insertion
6963              ## mode, maybe.
6964            }
6965    
6966          !!!next-token;          !!!next-token;
6967          redo B;          next B;
6968        } elsif ({        } elsif ({
6969                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6970                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6971                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6972                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6973                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6974          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
6975            !!!parse-error (type => 'in body',
6976                            text => $token->{tag_name}, token => $token);
6977          ## Ignore the token          ## Ignore the token
6978            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6979          !!!next-token;          !!!next-token;
6980          redo B;          next B;
6981                    
6982          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6983        } else {        } else {
6984            if ($token->{tag_name} eq 'image') {
6985              !!!cp ('t384');
6986              !!!parse-error (type => 'image', token => $token);
6987              $token->{tag_name} = 'img';
6988            } else {
6989              !!!cp ('t385');
6990            }
6991    
6992            ## NOTE: There is an "as if <br>" code clone.
6993          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6994                    
6995          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6996    
6997            if ({
6998                 applet => 1, marquee => 1, object => 1,
6999                }->{$token->{tag_name}}) {
7000              !!!cp ('t380');
7001              push @$active_formatting_elements, ['#marker', ''];
7002              !!!nack ('t380.1');
7003            } elsif ({
7004                      b => 1, big => 1, em => 1, font => 1, i => 1,
7005                      s => 1, small => 1, strile => 1,
7006                      strong => 1, tt => 1, u => 1,
7007                     }->{$token->{tag_name}}) {
7008              !!!cp ('t375');
7009              push @$active_formatting_elements, $self->{open_elements}->[-1];
7010              !!!nack ('t375.1');
7011            } elsif ($token->{tag_name} eq 'input') {
7012              !!!cp ('t388');
7013              ## TODO: associate with $self->{form_element} if defined
7014              pop @{$self->{open_elements}};
7015              !!!ack ('t388.2');
7016            } elsif ({
7017                      area => 1, basefont => 1, bgsound => 1, br => 1,
7018                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7019                      #image => 1,
7020                     }->{$token->{tag_name}}) {
7021              !!!cp ('t388.1');
7022              pop @{$self->{open_elements}};
7023              !!!ack ('t388.3');
7024            } elsif ($token->{tag_name} eq 'select') {
7025              ## TODO: associate with $self->{form_element} if defined
7026            
7027              if ($self->{insertion_mode} & TABLE_IMS or
7028                  $self->{insertion_mode} & BODY_TABLE_IMS or
7029                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7030                !!!cp ('t400.1');
7031                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7032              } else {
7033                !!!cp ('t400.2');
7034                $self->{insertion_mode} = IN_SELECT_IM;
7035              }
7036              !!!nack ('t400.3');
7037            } else {
7038              !!!nack ('t402');
7039            }
7040                    
7041          !!!next-token;          !!!next-token;
7042          redo B;          next B;
7043        }        }
7044      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7045        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7046          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7047              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7048            for (@{$self->{open_elements}}) {          INSCOPE: {
7049              unless ({            for (reverse @{$self->{open_elements}}) {
7050                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7051                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7052                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7053                      }->{$_->[1]}) {                last INSCOPE;
7054                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
7055                  !!!cp ('t405.1');
7056                  last;
7057              }              }
7058            }            }
7059    
7060            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7061            !!!next-token;                            text => $token->{tag_name}, token => $token);
7062            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
7063            !!!next-token;            !!!next-token;
7064            redo B;            next B;
7065            } # INSCOPE
7066    
7067            for (@{$self->{open_elements}}) {
7068              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7069                !!!cp ('t403');
7070                !!!parse-error (type => 'not closed',
7071                                text => $_->[0]->manakai_local_name,
7072                                token => $token);
7073                last;
7074              } else {
7075                !!!cp ('t404');
7076              }
7077          }          }
7078    
7079            $self->{insertion_mode} = AFTER_BODY_IM;
7080            !!!next-token;
7081            next B;
7082        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7083          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
7084            ## up-to-date, though it has same effect as speced.
7085            if (@{$self->{open_elements}} > 1 and
7086                $self->{open_elements}->[1]->[1] & BODY_EL) {
7087            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7088            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7089              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
7090                !!!parse-error (type => 'not closed',
7091                                text => $self->{open_elements}->[1]->[0]
7092                                    ->manakai_local_name,
7093                                token => $token);
7094              } else {
7095                !!!cp ('t407');
7096            }            }
7097            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7098            ## reprocess            ## reprocess
7099            redo B;            next B;
7100          } else {          } else {
7101            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
7102              !!!parse-error (type => 'unmatched end tag',
7103                              text => $token->{tag_name}, token => $token);
7104            ## Ignore the token            ## Ignore the token
7105            !!!next-token;            !!!next-token;
7106            redo B;            next B;
7107          }          }
7108        } elsif ({        } elsif ({
7109                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7110                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7111                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7112                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7113                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7114                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7115          ## has an element in scope          ## has an element in scope
7116          my $i;          my $i;
7117          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7118            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7119            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7120              ## generate implied end tags              !!!cp ('t410');
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
7121              $i = $_;              $i = $_;
7122              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7123            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7124                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7125              last INSCOPE;              last INSCOPE;
7126            }            }
7127          } # INSCOPE          } # INSCOPE
7128            
7129          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7130            if (defined $i) {            !!!cp ('t413');
7131              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag',
7132                              text => $token->{tag_name}, token => $token);
7133            } else {
7134              ## Step 1. generate implied end tags
7135              while ({
7136                      ## END_TAG_OPTIONAL_EL
7137                      dd => ($token->{tag_name} ne 'dd'),
7138                      dt => ($token->{tag_name} ne 'dt'),
7139                      li => ($token->{tag_name} ne 'li'),
7140                      p => 1,
7141                      rt => 1,
7142                      rp => 1,
7143                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7144                !!!cp ('t409');
7145                pop @{$self->{open_elements}};
7146              }
7147    
7148              ## Step 2.
7149              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7150                      ne $token->{tag_name}) {
7151                !!!cp ('t412');
7152                !!!parse-error (type => 'not closed',
7153                                text => $self->{open_elements}->[-1]->[0]
7154                                    ->manakai_local_name,
7155                                token => $token);
7156            } else {            } else {
7157              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
7158            }            }
7159          }  
7160                      ## Step 3.
         if (defined $i) {  
7161            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7162          } elsif ($token->{tag_name} eq 'p') {  
7163            ## As if <p>, then reprocess the current token            ## Step 4.
7164            my $el;            $clear_up_to_marker->()
7165            !!!create-element ($el, 'p');                if {
7166            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
7167                  }->{$token->{tag_name}};
7168          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7169          !!!next-token;          !!!next-token;
7170          redo B;          next B;
7171        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7172            undef $self->{form_element};
7173    
7174          ## has an element in scope          ## has an element in scope
7175            my $i;
7176          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7177            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7178            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7179              ## generate implied end tags              !!!cp ('t418');
7180              if ({              $i = $_;
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
7181              last INSCOPE;              last INSCOPE;
7182            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7183                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7184              last INSCOPE;              last INSCOPE;
7185            }            }
7186          } # INSCOPE          } # INSCOPE
7187            
7188          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7189            pop @{$self->{open_elements}};            !!!cp ('t421');
7190          } else {            !!!parse-error (type => 'unmatched end tag',
7191            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                            text => $token->{tag_name}, token => $token);
7192            } else {
7193              ## Step 1. generate implied end tags
7194              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7195                !!!cp ('t417');
7196                pop @{$self->{open_elements}};
7197              }
7198              
7199              ## Step 2.
7200              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7201                      ne $token->{tag_name}) {
7202                !!!cp ('t417.1');
7203                !!!parse-error (type => 'not closed',
7204                                text => $self->{open_elements}->[-1]->[0]
7205                                    ->manakai_local_name,
7206                                token => $token);
7207              } else {
7208                !!!cp ('t420');
7209              }  
7210              
7211              ## Step 3.
7212              splice @{$self->{open_elements}}, $i;
7213          }          }
7214    
         undef $self->{form_element};  
7215          !!!next-token;          !!!next-token;
7216          redo B;          next B;
7217        } elsif ({        } elsif ({
7218                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7219                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5150  sub _tree_construction_main ($) { Line 7221  sub _tree_construction_main ($) {
7221          my $i;          my $i;
7222          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7223            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7224            if ({            if ($node->[1] & HEADING_EL) {
7225                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,              !!!cp ('t423');
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
7226              $i = $_;              $i = $_;
7227              last INSCOPE;              last INSCOPE;
7228            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7229                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7230              last INSCOPE;              last INSCOPE;
7231            }            }
7232          } # INSCOPE          } # INSCOPE
7233            
7234          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7235            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
7236              !!!parse-error (type => 'unmatched end tag',
7237                              text => $token->{tag_name}, token => $token);
7238            } else {
7239              ## Step 1. generate implied end tags
7240              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7241                !!!cp ('t422');
7242                pop @{$self->{open_elements}};
7243              }
7244              
7245              ## Step 2.
7246              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7247                      ne $token->{tag_name}) {
7248                !!!cp ('t425');
7249                !!!parse-error (type => 'unmatched end tag',
7250                                text => $token->{tag_name}, token => $token);
7251              } else {
7252                !!!cp ('t426');
7253              }
7254    
7255              ## Step 3.
7256              splice @{$self->{open_elements}}, $i;
7257          }          }
7258                    
         splice @{$self->{open_elements}}, $i if defined $i;  
7259          !!!next-token;          !!!next-token;
7260          redo B;          next B;
7261          } elsif ($token->{tag_name} eq 'p') {
7262            ## has an element in scope
7263            my $i;
7264            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7265              my $node = $self->{open_elements}->[$_];
7266              if ($node->[1] & P_EL) {
7267                !!!cp ('t410.1');
7268                $i = $_;
7269                last INSCOPE;
7270              } elsif ($node->[1] & SCOPING_EL) {
7271                !!!cp ('t411.1');
7272                last INSCOPE;
7273              }
7274            } # INSCOPE
7275    
7276            if (defined $i) {
7277              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7278                      ne $token->{tag_name}) {
7279                !!!cp ('t412.1');
7280                !!!parse-error (type => 'not closed',
7281                                text => $self->{open_elements}->[-1]->[0]
7282                                    ->manakai_local_name,
7283                                token => $token);
7284              } else {
7285                !!!cp ('t414.1');
7286              }
7287    
7288              splice @{$self->{open_elements}}, $i;
7289            } else {
7290              !!!cp ('t413.1');
7291              !!!parse-error (type => 'unmatched end tag',
7292                              text => $token->{tag_name}, token => $token);
7293    
7294              !!!cp ('t415.1');
7295              ## As if <p>, then reprocess the current token
7296              my $el;
7297              !!!create-element ($el, $HTML_NS, 'p',, $token);
7298              $insert->($el);
7299              ## NOTE: Not inserted into |$self->{open_elements}|.
7300            }
7301    
7302            !!!next-token;
7303            next B;
7304        } elsif ({        } elsif ({
7305                  a => 1,                  a => 1,
7306                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
7307                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
7308                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7309                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7310          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
7311          redo B;          $formatting_end_tag->($token);
7312            next B;
7313        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7314          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
7315            !!!parse-error (type => 'unmatched end tag',
7316                            text => 'br', token => $token);
7317    
7318          ## As if <br>          ## As if <br>
7319          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7320                    
7321          my $el;          my $el;
7322          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7323          $insert->($el);          $insert->($el);
7324                    
7325          ## Ignore the token.          ## Ignore the token.
7326          !!!next-token;          !!!next-token;
7327          redo B;          next B;
7328        } elsif ({        } elsif ({
7329                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7330                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5214  sub _tree_construction_main ($) { Line 7337  sub _tree_construction_main ($) {
7337                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
7338                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7339                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7340          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
7341            !!!parse-error (type => 'unmatched end tag',
7342                            text => $token->{tag_name}, token => $token);
7343          ## Ignore the token          ## Ignore the token
7344          !!!next-token;          !!!next-token;
7345          redo B;          next B;
7346                    
7347          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7348                    
# Line 5228  sub _tree_construction_main ($) { Line 7353  sub _tree_construction_main ($) {
7353    
7354          ## Step 2          ## Step 2
7355          S2: {          S2: {
7356            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7357              ## Step 1              ## Step 1
7358              ## generate implied end tags              ## generate implied end tags
7359              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7360                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7361                   td => 1, th => 1, tr => 1,                ## NOTE: |<ruby><rt></ruby>|.
7362                   tbody => 1, tfoot => 1, thead => 1,                ## ISSUE: <ruby><rt></rt> will also take this code path,
7363                  }->{$self->{open_elements}->[-1]->[1]}) {                ## which seems wrong.
7364                !!!back-token;                pop @{$self->{open_elements}};
7365                $token = {type => END_TAG_TOKEN,                $node_i++;
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7366              }              }
7367                    
7368              ## Step 2              ## Step 2
7369              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7370                        ne $token->{tag_name}) {
7371                  !!!cp ('t431');
7372                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7373                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7374                                  text => $self->{open_elements}->[-1]->[0]
7375                                      ->manakai_local_name,
7376                                  token => $token);
7377                } else {
7378                  !!!cp ('t432');
7379              }              }
7380                            
7381              ## Step 3              ## Step 3
7382              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7383    
7384              !!!next-token;              !!!next-token;
7385              last S2;              last S2;
7386            } else {            } else {
7387              ## Step 3              ## Step 3
7388              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7389                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7390                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7391                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7392                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7393                  !!!parse-error (type => 'unmatched end tag',
7394                                  text => $token->{tag_name}, token => $token);
7395                ## Ignore the token                ## Ignore the token
7396                !!!next-token;                !!!next-token;
7397                last S2;                last S2;
7398              }              }
7399    
7400                !!!cp ('t434');
7401            }            }
7402                        
7403            ## Step 4            ## Step 4
# Line 5273  sub _tree_construction_main ($) { Line 7407  sub _tree_construction_main ($) {
7407            ## Step 5;            ## Step 5;
7408            redo S2;            redo S2;
7409          } # S2          } # S2
7410          redo B;          next B;
7411        }        }
7412      }      }
7413      redo B;      next B;
7414      } continue { # B
7415        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7416          ## NOTE: The code below is executed in cases where it does not have
7417          ## to be, but it it is harmless even in those cases.
7418          ## has an element in scope
7419          INSCOPE: {
7420            for (reverse 0..$#{$self->{open_elements}}) {
7421              my $node = $self->{open_elements}->[$_];
7422              if ($node->[1] & FOREIGN_EL) {
7423                last INSCOPE;
7424              } elsif ($node->[1] & SCOPING_EL) {
7425                last;
7426              }
7427            }
7428            
7429            ## NOTE: No foreign element in scope.
7430            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7431          } # INSCOPE
7432        }
7433    } # B    } # B
7434    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
7435    ## Stop parsing # MUST    ## Stop parsing # MUST
7436        
7437    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5325  sub set_inner_html ($$$) { Line 7473  sub set_inner_html ($$$) {
7473      my $p = $class->new;      my $p = $class->new;
7474      $p->{document} = $doc;      $p->{document} = $doc;
7475    
7476      ## Step 9 # MUST      ## Step 8 # MUST
7477      my $i = 0;      my $i = 0;
7478      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7479      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7480      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7481        my $self = shift;        my $self = shift;
7482    
7483        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7484        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7485    
7486          $self->{next_char} = -1 and return if $i >= length $$s;
7487          $self->{next_char} = ord substr $$s, $i++, 1;
7488    
7489        $self->{next_input_character} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7490        $self->{next_input_character} = ord substr $$s, $i++, 1;        $p->{column}++;
7491        $column++;  
7492          if ($self->{next_char} == 0x000A) { # LF
7493        if ($self->{next_input_character} == 0x000A) { # LF          $p->{line}++;
7494          $line++;          $p->{column} = 0;
7495          $column = 0;          !!!cp ('i1');
7496        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7497          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7498          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7499          $line++;          $p->{line}++;
7500          $column = 0;          $p->{column} = 0;
7501        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7502          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7503        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7504            !!!cp ('i3');
7505          } elsif ($self->{next_char} == 0x0000) { # NULL
7506            !!!cp ('i4');
7507          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7508          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7509          } elsif ($self->{next_char} <= 0x0008 or
7510                   (0x000E <= $self->{next_char} and
7511                    $self->{next_char} <= 0x001F) or
7512                   (0x007F <= $self->{next_char} and
7513                    $self->{next_char} <= 0x009F) or
7514                   (0xD800 <= $self->{next_char} and
7515                    $self->{next_char} <= 0xDFFF) or
7516                   (0xFDD0 <= $self->{next_char} and
7517                    $self->{next_char} <= 0xFDDF) or
7518                   {
7519                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7520                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7521                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7522                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7523                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7524                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7525                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7526                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7527                    0x10FFFE => 1, 0x10FFFF => 1,
7528                   }->{$self->{next_char}}) {
7529            !!!cp ('i4.1');
7530            if ($self->{next_char} < 0x10000) {
7531              !!!parse-error (type => 'control char',
7532                              text => (sprintf 'U+%04X', $self->{next_char}));
7533            } else {
7534              !!!parse-error (type => 'control char',
7535                              text => (sprintf 'U-%08X', $self->{next_char}));
7536            }
7537        }        }
7538      };      };
7539      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7540      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7541            
7542      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7543        my (%opt) = @_;        my (%opt) = @_;
7544        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7545          my $column = $opt{column};
7546          if (defined $opt{token} and defined $opt{token}->{line}) {
7547            $line = $opt{token}->{line};
7548            $column = $opt{token}->{column};
7549          }
7550          warn "Parse error ($opt{type}) at line $line column $column\n";
7551      };      };
7552      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7553        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7554      };      };
7555            
7556      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7557      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7558    
7559      ## Step 2      ## Step 2
7560      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7561      $p->{content_model} = {      $p->{content_model} = {
7562        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7563        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5386  sub set_inner_html ($$$) { Line 7574  sub set_inner_html ($$$) {
7574          unless defined $p->{content_model};          unless defined $p->{content_model};
7575          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7576    
7577      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7578          ## TODO: Foreign element OK?
7579    
7580      ## Step 4      ## Step 3
7581      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7582        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7583    
7584      ## Step 5 # MUST      ## Step 4 # MUST
7585      $doc->append_child ($root);      $doc->append_child ($root);
7586    
7587      ## Step 6 # MUST      ## Step 5 # MUST
7588      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7589    
7590      undef $p->{head_element};      undef $p->{head_element};
7591    
7592      ## Step 7 # MUST      ## Step 6 # MUST
7593      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7594    
7595      ## Step 8 # MUST      ## Step 7 # MUST
7596      my $anode = $node;      my $anode = $node;
7597      AN: while (defined $anode) {      AN: while (defined $anode) {
7598        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7599          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7600          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7601            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7602                !!!cp ('i5');
7603              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7604              last AN;              last AN;
7605            }            }
# Line 5418  sub set_inner_html ($$$) { Line 7608  sub set_inner_html ($$$) {
7608        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7609      } # AN      } # AN
7610            
7611      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7612      {      {
7613        my $self = $p;        my $self = $p;
7614        !!!next-token;        !!!next-token;
7615      }      }
7616      $p->_tree_construction_main;      $p->_tree_construction_main;
7617    
7618      ## Step 11 # MUST      ## Step 10 # MUST
7619      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7620      for (@cn) {      for (@cn) {
7621        $node->remove_child ($_);        $node->remove_child ($_);
7622      }      }
7623      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7624    
7625      ## Step 12 # MUST      ## Step 11 # MUST
7626      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7627      for (@cn) {      for (@cn) {
7628        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5442  sub set_inner_html ($$$) { Line 7631  sub set_inner_html ($$$) {
7631      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7632    
7633      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7634    
7635        delete $p->{parse_error}; # delete loop
7636    } else {    } else {
7637      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";
7638    }    }

Legend:
Removed from v.1.65  
changed lines
  Added in v.1.153

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24