/[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.137 by wakaba, Sun May 18 03:46:27 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  ## TODO: 1252 parse error (revision 1264)
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  ## TODO: 8859-11 = 874 (revision 1271)
13  ## is not yet clear.  
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
15  ## "{U+FEFF}..." in GB18030?  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
16    my $SVG_NS = q<http://www.w3.org/2000/svg>;
17  my $permitted_slash_tag_name = {  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
18    base => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
19    link => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
20    meta => 1,  
21    hr => 1,  sub A_EL () { 0b1 }
22    br => 1,  sub ADDRESS_EL () { 0b10 }
23    img=> 1,  sub BODY_EL () { 0b100 }
24    embed => 1,  sub BUTTON_EL () { 0b1000 }
25    param => 1,  sub CAPTION_EL () { 0b10000 }
26    area => 1,  sub DD_EL () { 0b100000 }
27    col => 1,  sub DIV_EL () { 0b1000000 }
28    input => 1,  sub DT_EL () { 0b10000000 }
29    sub FORM_EL () { 0b100000000 }
30    sub FORMATTING_EL () { 0b1000000000 }
31    sub FRAMESET_EL () { 0b10000000000 }
32    sub HEADING_EL () { 0b100000000000 }
33    sub HTML_EL () { 0b1000000000000 }
34    sub LI_EL () { 0b10000000000000 }
35    sub NOBR_EL () { 0b100000000000000 }
36    sub OPTION_EL () { 0b1000000000000000 }
37    sub OPTGROUP_EL () { 0b10000000000000000 }
38    sub P_EL () { 0b100000000000000000 }
39    sub SELECT_EL () { 0b1000000000000000000 }
40    sub TABLE_EL () { 0b10000000000000000000 }
41    sub TABLE_CELL_EL () { 0b100000000000000000000 }
42    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
43    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
44    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
45    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
46    sub FOREIGN_EL () { 0b10000000000000000000000000 }
47    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
48    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
49    
50    sub TABLE_ROWS_EL () {
51      TABLE_EL |
52      TABLE_ROW_EL |
53      TABLE_ROW_GROUP_EL
54    }
55    
56    sub END_TAG_OPTIONAL_EL () {
57      DD_EL |
58      DT_EL |
59      LI_EL |
60      P_EL
61    }
62    
63    sub ALL_END_TAG_OPTIONAL_EL () {
64      END_TAG_OPTIONAL_EL |
65      BODY_EL |
66      HTML_EL |
67      TABLE_CELL_EL |
68      TABLE_ROW_EL |
69      TABLE_ROW_GROUP_EL
70    }
71    
72    sub SCOPING_EL () {
73      BUTTON_EL |
74      CAPTION_EL |
75      HTML_EL |
76      TABLE_EL |
77      TABLE_CELL_EL |
78      MISC_SCOPING_EL
79    }
80    
81    sub TABLE_SCOPING_EL () {
82      HTML_EL |
83      TABLE_EL
84    }
85    
86    sub TABLE_ROWS_SCOPING_EL () {
87      HTML_EL |
88      TABLE_ROW_GROUP_EL
89    }
90    
91    sub TABLE_ROW_SCOPING_EL () {
92      HTML_EL |
93      TABLE_ROW_EL
94    }
95    
96    sub SPECIAL_EL () {
97      ADDRESS_EL |
98      BODY_EL |
99      DIV_EL |
100      END_TAG_OPTIONAL_EL |
101      FORM_EL |
102      FRAMESET_EL |
103      HEADING_EL |
104      OPTION_EL |
105      OPTGROUP_EL |
106      SELECT_EL |
107      TABLE_ROW_EL |
108      TABLE_ROW_GROUP_EL |
109      MISC_SPECIAL_EL
110    }
111    
112    my $el_category = {
113      a => A_EL | FORMATTING_EL,
114      address => ADDRESS_EL,
115      applet => MISC_SCOPING_EL,
116      area => MISC_SPECIAL_EL,
117      b => FORMATTING_EL,
118      base => MISC_SPECIAL_EL,
119      basefont => MISC_SPECIAL_EL,
120      bgsound => MISC_SPECIAL_EL,
121      big => FORMATTING_EL,
122      blockquote => MISC_SPECIAL_EL,
123      body => BODY_EL,
124      br => MISC_SPECIAL_EL,
125      button => BUTTON_EL,
126      caption => CAPTION_EL,
127      center => MISC_SPECIAL_EL,
128      col => MISC_SPECIAL_EL,
129      colgroup => MISC_SPECIAL_EL,
130      dd => DD_EL,
131      dir => MISC_SPECIAL_EL,
132      div => DIV_EL,
133      dl => MISC_SPECIAL_EL,
134      dt => DT_EL,
135      em => FORMATTING_EL,
136      embed => MISC_SPECIAL_EL,
137      fieldset => MISC_SPECIAL_EL,
138      font => FORMATTING_EL,
139      form => FORM_EL,
140      frame => MISC_SPECIAL_EL,
141      frameset => FRAMESET_EL,
142      h1 => HEADING_EL,
143      h2 => HEADING_EL,
144      h3 => HEADING_EL,
145      h4 => HEADING_EL,
146      h5 => HEADING_EL,
147      h6 => HEADING_EL,
148      head => MISC_SPECIAL_EL,
149      hr => MISC_SPECIAL_EL,
150      html => HTML_EL,
151      i => FORMATTING_EL,
152      iframe => MISC_SPECIAL_EL,
153      img => MISC_SPECIAL_EL,
154      input => MISC_SPECIAL_EL,
155      isindex => MISC_SPECIAL_EL,
156      li => LI_EL,
157      link => MISC_SPECIAL_EL,
158      listing => MISC_SPECIAL_EL,
159      marquee => MISC_SCOPING_EL,
160      menu => MISC_SPECIAL_EL,
161      meta => MISC_SPECIAL_EL,
162      nobr => NOBR_EL | FORMATTING_EL,
163      noembed => MISC_SPECIAL_EL,
164      noframes => MISC_SPECIAL_EL,
165      noscript => MISC_SPECIAL_EL,
166      object => MISC_SCOPING_EL,
167      ol => MISC_SPECIAL_EL,
168      optgroup => OPTGROUP_EL,
169      option => OPTION_EL,
170      p => P_EL,
171      param => MISC_SPECIAL_EL,
172      plaintext => MISC_SPECIAL_EL,
173      pre => MISC_SPECIAL_EL,
174      s => FORMATTING_EL,
175      script => MISC_SPECIAL_EL,
176      select => SELECT_EL,
177      small => FORMATTING_EL,
178      spacer => MISC_SPECIAL_EL,
179      strike => FORMATTING_EL,
180      strong => FORMATTING_EL,
181      style => MISC_SPECIAL_EL,
182      table => TABLE_EL,
183      tbody => TABLE_ROW_GROUP_EL,
184      td => TABLE_CELL_EL,
185      textarea => MISC_SPECIAL_EL,
186      tfoot => TABLE_ROW_GROUP_EL,
187      th => TABLE_CELL_EL,
188      thead => TABLE_ROW_GROUP_EL,
189      title => MISC_SPECIAL_EL,
190      tr => TABLE_ROW_EL,
191      tt => FORMATTING_EL,
192      u => FORMATTING_EL,
193      ul => MISC_SPECIAL_EL,
194      wbr => MISC_SPECIAL_EL,
195    };
196    
197    my $el_category_f = {
198      $MML_NS => {
199        'annotation-xml' => MML_AXML_EL,
200        mi => FOREIGN_FLOW_CONTENT_EL,
201        mo => FOREIGN_FLOW_CONTENT_EL,
202        mn => FOREIGN_FLOW_CONTENT_EL,
203        ms => FOREIGN_FLOW_CONTENT_EL,
204        mtext => FOREIGN_FLOW_CONTENT_EL,
205      },
206      $SVG_NS => {
207        foreignObject => FOREIGN_FLOW_CONTENT_EL,
208        desc => FOREIGN_FLOW_CONTENT_EL,
209        title => FOREIGN_FLOW_CONTENT_EL,
210      },
211      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
212    };
213    
214    my $svg_attr_name = {
215      attributetype => 'attributeType',
216      basefrequency => 'baseFrequency',
217      baseprofile => 'baseProfile',
218      calcmode => 'calcMode',
219      clippathunits => 'clipPathUnits',
220      contentscripttype => 'contentScriptType',
221      contentstyletype => 'contentStyleType',
222      diffuseconstant => 'diffuseConstant',
223      edgemode => 'edgeMode',
224      externalresourcesrequired => 'externalResourcesRequired',
225      fecolormatrix => 'feColorMatrix',
226      fecomposite => 'feComposite',
227      fegaussianblur => 'feGaussianBlur',
228      femorphology => 'feMorphology',
229      fetile => 'feTile',
230      filterres => 'filterRes',
231      filterunits => 'filterUnits',
232      glyphref => 'glyphRef',
233      gradienttransform => 'gradientTransform',
234      gradientunits => 'gradientUnits',
235      kernelmatrix => 'kernelMatrix',
236      kernelunitlength => 'kernelUnitLength',
237      keypoints => 'keyPoints',
238      keysplines => 'keySplines',
239      keytimes => 'keyTimes',
240      lengthadjust => 'lengthAdjust',
241      limitingconeangle => 'limitingConeAngle',
242      markerheight => 'markerHeight',
243      markerunits => 'markerUnits',
244      markerwidth => 'markerWidth',
245      maskcontentunits => 'maskContentUnits',
246      maskunits => 'maskUnits',
247      numoctaves => 'numOctaves',
248      pathlength => 'pathLength',
249      patterncontentunits => 'patternContentUnits',
250      patterntransform => 'patternTransform',
251      patternunits => 'patternUnits',
252      pointsatx => 'pointsAtX',
253      pointsaty => 'pointsAtY',
254      pointsatz => 'pointsAtZ',
255      preservealpha => 'preserveAlpha',
256      preserveaspectratio => 'preserveAspectRatio',
257      primitiveunits => 'primitiveUnits',
258      refx => 'refX',
259      refy => 'refY',
260      repeatcount => 'repeatCount',
261      repeatdur => 'repeatDur',
262      requiredextensions => 'requiredExtensions',
263      specularconstant => 'specularConstant',
264      specularexponent => 'specularExponent',
265      spreadmethod => 'spreadMethod',
266      startoffset => 'startOffset',
267      stddeviation => 'stdDeviation',
268      stitchtiles => 'stitchTiles',
269      surfacescale => 'surfaceScale',
270      systemlanguage => 'systemLanguage',
271      tablevalues => 'tableValues',
272      targetx => 'targetX',
273      targety => 'targetY',
274      textlength => 'textLength',
275      viewbox => 'viewBox',
276      viewtarget => 'viewTarget',
277      xchannelselector => 'xChannelSelector',
278      ychannelselector => 'yChannelSelector',
279      zoomandpan => 'zoomAndPan',
280  };  };
281    
282    my $foreign_attr_xname = {
283      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
284      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
285      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
286      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
287      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
288      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
289      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
290      'xml:base' => [$XML_NS, ['xml', 'base']],
291      'xml:lang' => [$XML_NS, ['xml', 'lang']],
292      'xml:space' => [$XML_NS, ['xml', 'space']],
293      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
294      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
295    };
296    
297    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
298    
299  my $c1_entity_char = {  my $c1_entity_char = {
300    0x80 => 0x20AC,    0x80 => 0x20AC,
301    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 63  my $c1_entity_char = { Line 331  my $c1_entity_char = {
331    0x9F => 0x0178,    0x9F => 0x0178,
332  }; # $c1_entity_char  }; # $c1_entity_char
333    
 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  
   
334  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
335    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
336    my $charset = shift;    my $charset_name = shift;
337    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    open my $byte_stream, '<', ref $_[0] ? $_[0] : \($_[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;  
   }  
338    
339    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
340      my $self = shift;      my (%opt) = @_;
341      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
342      ## TODO: if $charset is supported    };
343      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
344    
345      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
346      require Message::Charset::Info;
347      ## Step 1        my $charset;
348      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?    my $buffer;
349        $charset = 'utf-8';    my ($char_stream, $e_status);
350    
351      SNIFFING: {
352    
353        ## Step 1
354        if (defined $charset_name) {
355          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
356    
357          ## ISSUE: Unsupported encoding is not ignored according to the spec.
358          ($char_stream, $e_status) = $charset->get_decode_handle
359              ($byte_stream, allow_error_reporting => 1,
360               allow_fallback => 1);
361          if ($char_stream) {
362            $self->{confident} = 1;
363            last SNIFFING;
364          } else {
365            ## TODO: unsupported error
366          }
367      }      }
368    
369      ## Step 2      ## Step 2
370      if (defined $self->{input_encoding} and      my $byte_buffer = '';
371          $self->{input_encoding} eq $charset) {      for (1..1024) {
372          my $char = $byte_stream->getc;
373          last unless defined $char;
374          $byte_buffer .= $char;
375        } ## TODO: timeout
376    
377        ## Step 3
378        if ($byte_buffer =~ /^\xFE\xFF/) {
379          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
380          ($char_stream, $e_status) = $charset->get_decode_handle
381              ($byte_stream, allow_error_reporting => 1,
382               allow_fallback => 1, byte_buffer => \$byte_buffer);
383        $self->{confident} = 1;        $self->{confident} = 1;
384        return;        last SNIFFING;
385        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
386          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
387          ($char_stream, $e_status) = $charset->get_decode_handle
388              ($byte_stream, allow_error_reporting => 1,
389               allow_fallback => 1, byte_buffer => \$byte_buffer);
390          $self->{confident} = 1;
391          last SNIFFING;
392        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
393          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1, byte_buffer => \$byte_buffer);
397          $self->{confident} = 1;
398          last SNIFFING;
399      }      }
400    
401      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
402          ':'.$charset, level => 'w');      ## TODO: <meta charset>
403    
404      ## Step 3      ## Step 5
405      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
406    
407      ## Step 4      ## Step 6
408      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
409        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
410            ($byte_buffer);
411        if (defined $charset_name) {
412          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
413    
414          ## ISSUE: Unsupported encoding is not ignored according to the spec.
415          require Whatpm::Charset::DecodeHandle;
416          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
417              ($byte_stream);
418          ($char_stream, $e_status) = $charset->get_decode_handle
419              ($buffer, allow_error_reporting => 1,
420               allow_fallback => 1, byte_buffer => \$byte_buffer);
421          if ($char_stream) {
422            $buffer->{buffer} = $byte_buffer;
423            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
424                            value => $charset_name,
425                            level => $self->{info_level},
426                            line => 1, column => 1);
427            $self->{confident} = 0;
428            last SNIFFING;
429          }
430        }
431    
432        ## Step 7: default
433        ## TODO: Make this configurable.
434        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
435            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
436            ## detectable in the step 6.
437        require Whatpm::Charset::DecodeHandle;
438        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
439            ($byte_stream);
440        ($char_stream, $e_status)
441            = $charset->get_decode_handle ($buffer,
442                                           allow_error_reporting => 1,
443                                           allow_fallback => 1,
444                                           byte_buffer => \$byte_buffer);
445        $buffer->{buffer} = $byte_buffer;
446        !!!parse-error (type => 'sniffing:default', ## TODO: type name
447                        value => 'windows-1252',
448                        level => $self->{info_level},
449                        line => 1, column => 1);
450        $self->{confident} = 0;
451      } # SNIFFING
452    
453      $self->{input_encoding} = $charset->get_iana_name;
454      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
455        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
456                        value => $self->{input_encoding},
457                        level => $self->{unsupported_level},
458                        line => 1, column => 1);
459      } elsif (not ($e_status &
460                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
461        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
462                        value => $self->{input_encoding},
463                        level => $self->{unsupported_level},
464                        line => 1, column => 1);
465      }
466    
467      $self->{change_encoding} = sub {
468        my $self = shift;
469        $charset_name = shift;
470        my $token = shift;
471    
472        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
473        ($char_stream, $e_status) = $charset->get_decode_handle
474            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
475             byte_buffer => \ $buffer->{buffer});
476        
477        if ($char_stream) { # if supported
478          ## "Change the encoding" algorithm:
479    
480          ## Step 1    
481          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
482            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
483            ($char_stream, $e_status) = $charset->get_decode_handle
484                ($byte_stream,
485                 byte_buffer => \ $buffer->{buffer});
486          }
487          $charset_name = $charset->get_iana_name;
488          
489          ## Step 2
490          if (defined $self->{input_encoding} and
491              $self->{input_encoding} eq $charset_name) {
492            !!!parse-error (type => 'charset label:matching', ## TODO: type
493                            value => $charset_name,
494                            level => $self->{info_level});
495            $self->{confident} = 1;
496            return;
497          }
498    
499          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
500              ':'.$charset_name, level => 'w', token => $token);
501          
502          ## Step 3
503          # if (can) {
504            ## change the encoding on the fly.
505            #$self->{confident} = 1;
506            #return;
507          # }
508          
509          ## Step 4
510          throw Whatpm::HTML::RestartParser ();
511        }
512    }; # $self->{change_encoding}    }; # $self->{change_encoding}
513    
514      my $char_onerror = sub {
515        my (undef, $type, %opt) = @_;
516        !!!parse-error (%opt, type => $type,
517                        line => $self->{line}, column => $self->{column} + 1);
518        if ($opt{octets}) {
519          ${$opt{octets}} = "\x{FFFD}"; # relacement character
520        }
521      };
522      $char_stream->onerror ($char_onerror);
523    
524    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
525    my $return;    my $return;
526    try {    try {
527      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
528    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
529      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
530      $s = \ (Encode::decode ($charset, $$bytes_s));      
531      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
532        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
533          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
534                          value => $self->{input_encoding},
535                          level => $self->{unsupported_level},
536                          line => 1, column => 1);
537        } elsif (not ($e_status &
538                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
539          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
540                          value => $self->{input_encoding},
541                          level => $self->{unsupported_level},
542                          line => 1, column => 1);
543        }
544      $self->{confident} = 1;      $self->{confident} = 1;
545      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
546        $return = $self->parse_char_stream ($char_stream, @args);
547    };    };
548    return $return;    return $return;
549  } # parse_byte_string  } # parse_byte_string
550    
551  *parse_char_string = \&parse_string;  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
552    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
553    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
554    ## because the core part of our HTML parser expects a string of character,
555    ## not a string of bytes or code units or anything which might contain a BOM.
556    ## Therefore, any parser interface that accepts a string of bytes,
557    ## such as |parse_byte_string| in this module, must ensure that it does
558    ## strip the BOM and never strip any ZWNBSP.
559    
560    sub parse_char_string ($$$;$) {
561      my $self = shift;
562      open my $input, '<:utf8', ref $_[0] ? $_[0] : \($_[0]);
563      return $self->parse_char_stream ($input, @_[1..$#_]);
564    } # parse_char_string
565    *parse_string = \&parse_char_string;
566    
567  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
568    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
569    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
570    $self->{document} = $_[1];    $self->{document} = $_[1];
571    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
572    
# Line 170  sub parse_string ($$$;$) { Line 577  sub parse_string ($$$;$) {
577        if defined $self->{input_encoding};        if defined $self->{input_encoding};
578    
579    my $i = 0;    my $i = 0;
580    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
581    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
582    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
583      my $self = shift;      my $self = shift;
584    
585      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
586      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
587    
588      $self->{next_input_character} = -1 and return if $i >= length $$s;      my $char = $input->getc;
589      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_char} = -1 and return unless defined $char;
590      $column++;      $self->{next_char} = ord $char;
591    
592        ($self->{line_prev}, $self->{column_prev})
593            = ($self->{line}, $self->{column});
594        $self->{column}++;
595            
596      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
597        $line++;        !!!cp ('j1');
598        $column = 0;        $self->{line}++;
599      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
600        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
601        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
602        $line++;        my $next = $input->getc;
603        $column = 0;        if ($next ne "\x0A") {
604      } elsif ($self->{next_input_character} > 0x10FFFF) {          $input->ungetc ($next);
605        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        }
606      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0x000A; # LF # MUST
607          $self->{line}++;
608          $self->{column} = 0;
609        } elsif ($self->{next_char} > 0x10FFFF) {
610          !!!cp ('j3');
611          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
612        } elsif ($self->{next_char} == 0x0000) { # NULL
613          !!!cp ('j4');
614        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
615        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
616        } elsif ($self->{next_char} <= 0x0008 or
617                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
618                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
619                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
620                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
621                 {
622                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
623                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
624                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
625                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
626                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
627                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
628                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
629                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
630                  0x10FFFE => 1, 0x10FFFF => 1,
631                 }->{$self->{next_char}}) {
632          !!!cp ('j5');
633          !!!parse-error (type => 'control char', level => $self->{must_level});
634    ## TODO: error type documentation
635      }      }
636    };    };
637    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
638    $self->{next_input_character} = -1;    $self->{next_char} = -1;
639    
640    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
641      my (%opt) = @_;      my (%opt) = @_;
642      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
643        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
644        warn "Parse error ($opt{type}) at line $line column $column\n";
645    };    };
646    $self->{parse_error} = sub {    $self->{parse_error} = sub {
647      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
648    };    };
649    
650    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 213  sub parse_string ($$$;$) { Line 652  sub parse_string ($$$;$) {
652    $self->_construct_tree;    $self->_construct_tree;
653    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
654    
655      delete $self->{parse_error}; # remove loop
656    
657    return $self->{document};    return $self->{document};
658  } # parse_string  } # parse_char_stream
659    
660  sub new ($) {  sub new ($) {
661    my $class = shift;    my $class = shift;
662    my $self = bless {}, $class;    my $self = bless {
663    $self->{set_next_input_character} = sub {      must_level => 'm',
664      $self->{next_input_character} = -1;      should_level => 's',
665        good_level => 'w',
666        warn_level => 'w',
667        info_level => 'i',
668        unsupported_level => 'u',
669      }, $class;
670      $self->{set_next_char} = sub {
671        $self->{next_char} = -1;
672    };    };
673    $self->{parse_error} = sub {    $self->{parse_error} = sub {
674      #      #
# Line 279  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO Line 727  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO
727  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
728  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
729  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
730    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
731    sub SELF_CLOSING_START_TAG_STATE () { 34 }
732    sub CDATA_BLOCK_STATE () { 35 }
733    
734  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
735  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 295  sub TABLE_IMS ()      { 0b1000000 } Line 746  sub TABLE_IMS ()      { 0b1000000 }
746  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
747  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
748  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
749    sub SELECT_IMS ()     { 0b10000000000 }
750    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
751        ## NOTE: "in foreign content" insertion mode is special; it is combined
752        ## with the secondary insertion mode.  In this parser, they are stored
753        ## together in the bit-or'ed form.
754    
755    ## NOTE: "initial" and "before html" insertion modes have no constants.
756    
757    ## NOTE: "after after body" insertion mode.
758  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
759    
760    ## NOTE: "after after frameset" insertion mode.
761  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
762    
763  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
764  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
765  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 311  sub IN_TABLE_IM () { TABLE_IMS } Line 773  sub IN_TABLE_IM () { TABLE_IMS }
773  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
774  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
775  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
776  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
777    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
778  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
779    
780  ## 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 787  sub _initialize_tokenizer ($) {
787    undef $self->{current_attribute};    undef $self->{current_attribute};
788    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
789    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
790      delete $self->{self_closing};
791    $self->{char} = [];    $self->{char} = [];
792    # $self->{next_input_character}    # $self->{next_char}
793    !!!next-input-character;    !!!next-input-character;
794    $self->{token} = [];    $self->{token} = [];
795    # $self->{escape}    # $self->{escape}
# Line 338  sub _initialize_tokenizer ($) { Line 802  sub _initialize_tokenizer ($) {
802  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
803  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
804  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
805  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
806  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
807    ##        ->{name}
808    ##        ->{value}
809    ##        ->{has_reference} == 1 or 0
810  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
811    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
812    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
813    ##     while the token is pushed back to the stack.
814    
815    ## ISSUE: "When a DOCTYPE token is created, its
816    ## <i>self-closing flag</i> must be unset (its other state is that it
817    ## be set), and its attributes list must be empty.": Wrong subject?
818    
819  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
820    
# Line 367  sub _initialize_tokenizer ($) { Line 841  sub _initialize_tokenizer ($) {
841    
842  sub _get_next_token ($) {  sub _get_next_token ($) {
843    my $self = shift;    my $self = shift;
844    
845      if ($self->{self_closing}) {
846        !!!parse-error (type => 'nestc', token => $self->{current_token});
847        ## NOTE: The |self_closing| flag is only set by start tag token.
848        ## In addition, when a start tag token is emitted, it is always set to
849        ## |current_token|.
850        delete $self->{self_closing};
851      }
852    
853    if (@{$self->{token}}) {    if (@{$self->{token}}) {
854        $self->{self_closing} = $self->{token}->[0]->{self_closing};
855      return shift @{$self->{token}};      return shift @{$self->{token}};
856    }    }
857    
858    A: {    A: {
859      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
860        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
861          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
862                not $self->{escape}) {
863              !!!cp (1);
864            $self->{state} = ENTITY_DATA_STATE;            $self->{state} = ENTITY_DATA_STATE;
865            !!!next-input-character;            !!!next-input-character;
866            redo A;            redo A;
867          } else {          } else {
868              !!!cp (2);
869            #            #
870          }          }
871        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
872          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
873            unless ($self->{escape}) {            unless ($self->{escape}) {
874              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
875                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
876                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
877                  !!!cp (3);
878                $self->{escape} = 1;                $self->{escape} = 1;
879                } else {
880                  !!!cp (4);
881              }              }
882              } else {
883                !!!cp (5);
884            }            }
885          }          }
886                    
887          #          #
888        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
889          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
890              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
891               not $self->{escape})) {               not $self->{escape})) {
892              !!!cp (6);
893            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
894            !!!next-input-character;            !!!next-input-character;
895            redo A;            redo A;
896          } else {          } else {
897              !!!cp (7);
898            #            #
899          }          }
900        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
901          if ($self->{escape} and          if ($self->{escape} and
902              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
903            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
904                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
905                !!!cp (8);
906              delete $self->{escape};              delete $self->{escape};
907              } else {
908                !!!cp (9);
909            }            }
910            } else {
911              !!!cp (10);
912          }          }
913                    
914          #          #
915        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
916          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
917            !!!emit ({type => END_OF_FILE_TOKEN,
918                      line => $self->{line}, column => $self->{column}});
919          last A; ## TODO: ok?          last A; ## TODO: ok?
920          } else {
921            !!!cp (12);
922        }        }
923        # Anything else        # Anything else
924        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
925                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
926                       line => $self->{line}, column => $self->{column},
927                      };
928        ## Stay in the data state        ## Stay in the data state
929        !!!next-input-character;        !!!next-input-character;
930    
# Line 428  sub _get_next_token ($) { Line 933  sub _get_next_token ($) {
933        redo A;        redo A;
934      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
935        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
936    
937          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
938                
939        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
940    
941        $self->{state} = DATA_STATE;        $self->{state} = DATA_STATE;
942        # next-input-character is already done        # next-input-character is already done
943    
944        unless (defined $token) {        unless (defined $token) {
945          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!cp (13);
946            !!!emit ({type => CHARACTER_TOKEN, data => '&',
947                      line => $l, column => $c,
948                     });
949        } else {        } else {
950            !!!cp (14);
951          !!!emit ($token);          !!!emit ($token);
952        }        }
953    
954        redo A;        redo A;
955      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
956        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
957          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
958              !!!cp (15);
959            !!!next-input-character;            !!!next-input-character;
960            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
961            redo A;            redo A;
962          } else {          } else {
963              !!!cp (16);
964            ## reconsume            ## reconsume
965            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
966    
967            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
968                        line => $self->{line_prev},
969                        column => $self->{column_prev},
970                       });
971    
972            redo A;            redo A;
973          }          }
974        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
975          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
976              !!!cp (17);
977            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
978            !!!next-input-character;            !!!next-input-character;
979            redo A;            redo A;
980          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
981              !!!cp (18);
982            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
983            !!!next-input-character;            !!!next-input-character;
984            redo A;            redo A;
985          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
986                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
987              !!!cp (19);
988            $self->{current_token}            $self->{current_token}
989              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
990                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
991                   line => $self->{line_prev},
992                   column => $self->{column_prev}};
993            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
994            !!!next-input-character;            !!!next-input-character;
995            redo A;            redo A;
996          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
997                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
998              !!!cp (20);
999            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1000                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
1001                                        line => $self->{line_prev},
1002                                        column => $self->{column_prev}};
1003            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1004            !!!next-input-character;            !!!next-input-character;
1005            redo A;            redo A;
1006          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1007            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1008              !!!parse-error (type => 'empty start tag',
1009                              line => $self->{line_prev},
1010                              column => $self->{column_prev});
1011            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1012            !!!next-input-character;            !!!next-input-character;
1013    
1014            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1015                        line => $self->{line_prev},
1016                        column => $self->{column_prev},
1017                       });
1018    
1019            redo A;            redo A;
1020          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1021            !!!parse-error (type => 'pio');            !!!cp (22);
1022              !!!parse-error (type => 'pio',
1023                              line => $self->{line_prev},
1024                              column => $self->{column_prev});
1025            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1026            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1027                                        line => $self->{line_prev},
1028                                        column => $self->{column_prev},
1029                                       };
1030              ## $self->{next_char} is intentionally left as is
1031            redo A;            redo A;
1032          } else {          } else {
1033            !!!parse-error (type => 'bare stago');            !!!cp (23);
1034              !!!parse-error (type => 'bare stago',
1035                              line => $self->{line_prev},
1036                              column => $self->{column_prev});
1037            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1038            ## reconsume            ## reconsume
1039    
1040            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1041                        line => $self->{line_prev},
1042                        column => $self->{column_prev},
1043                       });
1044    
1045            redo A;            redo A;
1046          }          }
# Line 505  sub _get_next_token ($) { Line 1048  sub _get_next_token ($) {
1048          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1049        }        }
1050      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1051          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1052        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1053          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1054    
1055            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1056            my @next_char;            my @next_char;
1057            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++) {
1058              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1059              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1060              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1061              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1062                  !!!cp (24);
1063                !!!next-input-character;                !!!next-input-character;
1064                next TAGNAME;                next TAGNAME;
1065              } else {              } else {
1066                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
1067                  $self->{next_char} = shift @next_char; # reconsume
1068                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1069                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1070    
1071                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1072                            line => $l, column => $c,
1073                           });
1074        
1075                redo A;                redo A;
1076              }              }
1077            }            }
1078            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1079                
1080            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
1081                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
1082                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
1083                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
1084                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
1085                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
1086                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
1087                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
1088              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1089                $self->{next_char} = shift @next_char; # reconsume
1090              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1091              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1092              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1093                          line => $l, column => $c,
1094                         });
1095              redo A;              redo A;
1096            } else {            } else {
1097              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1098                $self->{next_char} = shift @next_char;
1099              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1100              # and consume...              # and consume...
1101            }            }
1102          } else {          } else {
1103            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1104              !!!cp (28);
1105            # next-input-character is already done            # next-input-character is already done
1106            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1107            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1108                        line => $l, column => $c,
1109                       });
1110            redo A;            redo A;
1111          }          }
1112        }        }
1113                
1114        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1115            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1116          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
1117                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1118                = {type => END_TAG_TOKEN,
1119                   tag_name => chr ($self->{next_char} + 0x0020),
1120                   line => $l, column => $c};
1121          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1122          !!!next-input-character;          !!!next-input-character;
1123          redo A;          redo A;
1124        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
1125                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1126            !!!cp (30);
1127          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1128                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
1129                                      line => $l, column => $c};
1130          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1131          !!!next-input-character;          !!!next-input-character;
1132          redo A;          redo A;
1133        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1134          !!!parse-error (type => 'empty end tag');          !!!cp (31);
1135            !!!parse-error (type => 'empty end tag',
1136                            line => $self->{line_prev}, ## "<" in "</>"
1137                            column => $self->{column_prev} - 1);
1138          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1139          !!!next-input-character;          !!!next-input-character;
1140          redo A;          redo A;
1141        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1142            !!!cp (32);
1143          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1144          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1145          # reconsume          # reconsume
1146    
1147          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1148                      line => $l, column => $c,
1149                     });
1150    
1151          redo A;          redo A;
1152        } else {        } else {
1153            !!!cp (33);
1154          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1155          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1156          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1157                                      line => $self->{line_prev}, # "<" of "</"
1158                                      column => $self->{column_prev} - 1,
1159                                     };
1160            ## $self->{next_char} is intentionally left as is
1161          redo A;          redo A;
1162        }        }
1163      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1164        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1165            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1166            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1167            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1168            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1169            !!!cp (34);
1170          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1171          !!!next-input-character;          !!!next-input-character;
1172          redo A;          redo A;
1173        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1174          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1175            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
1176            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1177          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1178            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1179            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1180              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1181            }            #  !!! cp (36);
1182              #  !!! parse-error (type => 'end tag attribute');
1183              #} else {
1184                !!!cp (37);
1185              #}
1186          } else {          } else {
1187            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1188          }          }
# Line 616  sub _get_next_token ($) { Line 1192  sub _get_next_token ($) {
1192          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1193    
1194          redo A;          redo A;
1195        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1196                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1197          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1198            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1199            # start tag or end tag            # start tag or end tag
1200          ## Stay in this state          ## Stay in this state
1201          !!!next-input-character;          !!!next-input-character;
1202          redo A;          redo A;
1203        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1204          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1205          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1206            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1207            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1208          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1209            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1210            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1211              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1212            }            #  !!! cp (40);
1213              #  !!! parse-error (type => 'end tag attribute');
1214              #} else {
1215                !!!cp (41);
1216              #}
1217          } else {          } else {
1218            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1219          }          }
# Line 643  sub _get_next_token ($) { Line 1223  sub _get_next_token ($) {
1223          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1224    
1225          redo A;          redo A;
1226        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1227            !!!cp (42);
1228            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1229          !!!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  
1230          redo A;          redo A;
1231        } else {        } else {
1232          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1233            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1234            # start tag or end tag            # start tag or end tag
1235          ## Stay in the state          ## Stay in the state
1236          !!!next-input-character;          !!!next-input-character;
1237          redo A;          redo A;
1238        }        }
1239      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1240        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1241            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1242            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1243            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1244            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1245            !!!cp (45);
1246          ## Stay in the state          ## Stay in the state
1247          !!!next-input-character;          !!!next-input-character;
1248          redo A;          redo A;
1249        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1250          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1251            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1252            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1253          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1254            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1255            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1256                !!!cp (47);
1257              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1258              } else {
1259                !!!cp (48);
1260            }            }
1261          } else {          } else {
1262            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 1267  sub _get_next_token ($) {
1267          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1268    
1269          redo A;          redo A;
1270        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1271                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1272          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1273                                value => ''};          $self->{current_attribute}
1274                = {name => chr ($self->{next_char} + 0x0020),
1275                   value => '',
1276                   line => $self->{line}, column => $self->{column}};
1277          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1278          !!!next-input-character;          !!!next-input-character;
1279          redo A;          redo A;
1280        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1281            !!!cp (50);
1282            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1283          !!!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  
1284          redo A;          redo A;
1285        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1286          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1287          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1288            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1289            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1290          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1291            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1292            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1293                !!!cp (53);
1294              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1295              } else {
1296                !!!cp (54);
1297            }            }
1298          } else {          } else {
1299            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 1305  sub _get_next_token ($) {
1305    
1306          redo A;          redo A;
1307        } else {        } else {
1308          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1309                                value => ''};               0x0022 => 1, # "
1310                 0x0027 => 1, # '
1311                 0x003D => 1, # =
1312                }->{$self->{next_char}}) {
1313              !!!cp (55);
1314              !!!parse-error (type => 'bad attribute name');
1315            } else {
1316              !!!cp (56);
1317            }
1318            $self->{current_attribute}
1319                = {name => chr ($self->{next_char}),
1320                   value => '',
1321                   line => $self->{line}, column => $self->{column}};
1322          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1323          !!!next-input-character;          !!!next-input-character;
1324          redo A;          redo A;
# Line 742  sub _get_next_token ($) { Line 1327  sub _get_next_token ($) {
1327        my $before_leave = sub {        my $before_leave = sub {
1328          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1329              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1330            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1331              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1332            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1333          } else {          } else {
1334              !!!cp (58);
1335            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1336              = $self->{current_attribute};              = $self->{current_attribute};
1337          }          }
1338        }; # $before_leave        }; # $before_leave
1339    
1340        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1341            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1342            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1343            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1344            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1345            !!!cp (59);
1346          $before_leave->();          $before_leave->();
1347          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1348          !!!next-input-character;          !!!next-input-character;
1349          redo A;          redo A;
1350        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1351            !!!cp (60);
1352          $before_leave->();          $before_leave->();
1353          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1354          !!!next-input-character;          !!!next-input-character;
1355          redo A;          redo A;
1356        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1357          $before_leave->();          $before_leave->();
1358          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1359            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1360            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1361          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1362              !!!cp (62);
1363            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1364            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1365              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 784  sub _get_next_token ($) { Line 1373  sub _get_next_token ($) {
1373          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1374    
1375          redo A;          redo A;
1376        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1377                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1378          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1379            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1380          ## Stay in the state          ## Stay in the state
1381          !!!next-input-character;          !!!next-input-character;
1382          redo A;          redo A;
1383        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1384            !!!cp (64);
1385          $before_leave->();          $before_leave->();
1386            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1387          !!!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  
1388          redo A;          redo A;
1389        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1390          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1391          $before_leave->();          $before_leave->();
1392          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1393            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1394            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1395          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1396            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1397            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1398                !!!cp (67);
1399              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1400              } else {
1401                ## NOTE: This state should never be reached.
1402                !!!cp (68);
1403            }            }
1404          } else {          } else {
1405            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 1411  sub _get_next_token ($) {
1411    
1412          redo A;          redo A;
1413        } else {        } else {
1414          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1415                $self->{next_char} == 0x0027) { # '
1416              !!!cp (69);
1417              !!!parse-error (type => 'bad attribute name');
1418            } else {
1419              !!!cp (70);
1420            }
1421            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1422          ## Stay in the state          ## Stay in the state
1423          !!!next-input-character;          !!!next-input-character;
1424          redo A;          redo A;
1425        }        }
1426      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1427        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1428            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1429            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1430            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1431            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1432            !!!cp (71);
1433          ## Stay in the state          ## Stay in the state
1434          !!!next-input-character;          !!!next-input-character;
1435          redo A;          redo A;
1436        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1437            !!!cp (72);
1438          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1439          !!!next-input-character;          !!!next-input-character;
1440          redo A;          redo A;
1441        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1442          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1443            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1444            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1445          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1446            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1447            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1448                !!!cp (74);
1449              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1450              } else {
1451                ## NOTE: This state should never be reached.
1452                !!!cp (75);
1453            }            }
1454          } else {          } else {
1455            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 1460  sub _get_next_token ($) {
1460          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1461    
1462          redo A;          redo A;
1463        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1464                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1465          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1466                                value => ''};          $self->{current_attribute}
1467                = {name => chr ($self->{next_char} + 0x0020),
1468                   value => '',
1469                   line => $self->{line}, column => $self->{column}};
1470          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1471          !!!next-input-character;          !!!next-input-character;
1472          redo A;          redo A;
1473        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1474            !!!cp (77);
1475            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1476          !!!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  
1477          redo A;          redo A;
1478        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1479          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1480          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1481            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1482            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1483          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1484            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1485            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1486                !!!cp (80);
1487              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1488              } else {
1489                ## NOTE: This state should never be reached.
1490                !!!cp (81);
1491            }            }
1492          } else {          } else {
1493            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 1499  sub _get_next_token ($) {
1499    
1500          redo A;          redo A;
1501        } else {        } else {
1502          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1503                                value => ''};          $self->{current_attribute}
1504                = {name => chr ($self->{next_char}),
1505                   value => '',
1506                   line => $self->{line}, column => $self->{column}};
1507          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1508          !!!next-input-character;          !!!next-input-character;
1509          redo A;                  redo A;        
1510        }        }
1511      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1512        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1513            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1514            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1515            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1516            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1517            !!!cp (83);
1518          ## Stay in the state          ## Stay in the state
1519          !!!next-input-character;          !!!next-input-character;
1520          redo A;          redo A;
1521        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1522            !!!cp (84);
1523          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1524          !!!next-input-character;          !!!next-input-character;
1525          redo A;          redo A;
1526        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1527            !!!cp (85);
1528          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1529          ## reconsume          ## reconsume
1530          redo A;          redo A;
1531        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1532            !!!cp (86);
1533          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1534          !!!next-input-character;          !!!next-input-character;
1535          redo A;          redo A;
1536        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1537          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1538            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1539            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1540          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1541            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1542            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1543                !!!cp (88);
1544              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1545              } else {
1546                ## NOTE: This state should never be reached.
1547                !!!cp (89);
1548            }            }
1549          } else {          } else {
1550            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 1555  sub _get_next_token ($) {
1555          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1556    
1557          redo A;          redo A;
1558        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1559          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1560          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1561            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1562            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1563          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1564            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1565            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1566                !!!cp (91);
1567              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1568              } else {
1569                ## NOTE: This state should never be reached.
1570                !!!cp (92);
1571            }            }
1572          } else {          } else {
1573            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 1579  sub _get_next_token ($) {
1579    
1580          redo A;          redo A;
1581        } else {        } else {
1582          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1583              !!!cp (93);
1584              !!!parse-error (type => 'bad attribute value');
1585            } else {
1586              !!!cp (94);
1587            }
1588            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1589          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1590          !!!next-input-character;          !!!next-input-character;
1591          redo A;          redo A;
1592        }        }
1593      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1594        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1595          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (95);
1596            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1597          !!!next-input-character;          !!!next-input-character;
1598          redo A;          redo A;
1599        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1600            !!!cp (96);
1601          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1602          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1603          !!!next-input-character;          !!!next-input-character;
1604          redo A;          redo A;
1605        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1606          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1607          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1608            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1609            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1610          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1611            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1612            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1613                !!!cp (98);
1614              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1615              } else {
1616                ## NOTE: This state should never be reached.
1617                !!!cp (99);
1618            }            }
1619          } else {          } else {
1620            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 1626  sub _get_next_token ($) {
1626    
1627          redo A;          redo A;
1628        } else {        } else {
1629          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1630            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1631          ## Stay in the state          ## Stay in the state
1632          !!!next-input-character;          !!!next-input-character;
1633          redo A;          redo A;
1634        }        }
1635      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1636        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1637          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (101);
1638            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1639          !!!next-input-character;          !!!next-input-character;
1640          redo A;          redo A;
1641        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1642            !!!cp (102);
1643          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1644          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1645          !!!next-input-character;          !!!next-input-character;
1646          redo A;          redo A;
1647        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1648          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1649          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1650            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1651            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1652          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1653            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1654            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1655                !!!cp (104);
1656              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1657              } else {
1658                ## NOTE: This state should never be reached.
1659                !!!cp (105);
1660            }            }
1661          } else {          } else {
1662            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 1668  sub _get_next_token ($) {
1668    
1669          redo A;          redo A;
1670        } else {        } else {
1671          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1672            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1673          ## Stay in the state          ## Stay in the state
1674          !!!next-input-character;          !!!next-input-character;
1675          redo A;          redo A;
1676        }        }
1677      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1678        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1679            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1680            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1681            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1682            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1683            !!!cp (107);
1684          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1685          !!!next-input-character;          !!!next-input-character;
1686          redo A;          redo A;
1687        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1688            !!!cp (108);
1689          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1690          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1691          !!!next-input-character;          !!!next-input-character;
1692          redo A;          redo A;
1693        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1694          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1695            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1696            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1697          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1698            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1699            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1700                !!!cp (110);
1701              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1702              } else {
1703                ## NOTE: This state should never be reached.
1704                !!!cp (111);
1705            }            }
1706          } else {          } else {
1707            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 1712  sub _get_next_token ($) {
1712          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1713    
1714          redo A;          redo A;
1715        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1716          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1717          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1718            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1719            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1720          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1721            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1722            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1723                !!!cp (113);
1724              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1725              } else {
1726                ## NOTE: This state should never be reached.
1727                !!!cp (114);
1728            }            }
1729          } else {          } else {
1730            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 1736  sub _get_next_token ($) {
1736    
1737          redo A;          redo A;
1738        } else {        } else {
1739          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1740                 0x0022 => 1, # "
1741                 0x0027 => 1, # '
1742                 0x003D => 1, # =
1743                }->{$self->{next_char}}) {
1744              !!!cp (115);
1745              !!!parse-error (type => 'bad attribute value');
1746            } else {
1747              !!!cp (116);
1748            }
1749            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1750          ## Stay in the state          ## Stay in the state
1751          !!!next-input-character;          !!!next-input-character;
1752          redo A;          redo A;
1753        }        }
1754      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1755        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1756              (1,
1757               $self->{last_attribute_value_state}
1758                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1759               $self->{last_attribute_value_state}
1760                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1761               -1);
1762    
1763        unless (defined $token) {        unless (defined $token) {
1764            !!!cp (117);
1765          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1766        } else {        } else {
1767            !!!cp (118);
1768          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1769            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1770          ## 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"
1771        }        }
1772    
1773        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1774        # next-input-character is already done        # next-input-character is already done
1775        redo A;        redo A;
1776        } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1777          if ($self->{next_char} == 0x0009 or # HT
1778              $self->{next_char} == 0x000A or # LF
1779              $self->{next_char} == 0x000B or # VT
1780              $self->{next_char} == 0x000C or # FF
1781              $self->{next_char} == 0x0020) { # SP
1782            !!!cp (118);
1783            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1784            !!!next-input-character;
1785            redo A;
1786          } elsif ($self->{next_char} == 0x003E) { # >
1787            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1788              !!!cp (119);
1789              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1790            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1791              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1792              if ($self->{current_token}->{attributes}) {
1793                !!!cp (120);
1794                !!!parse-error (type => 'end tag attribute');
1795              } else {
1796                ## NOTE: This state should never be reached.
1797                !!!cp (121);
1798              }
1799            } else {
1800              die "$0: $self->{current_token}->{type}: Unknown token type";
1801            }
1802            $self->{state} = DATA_STATE;
1803            !!!next-input-character;
1804    
1805            !!!emit ($self->{current_token}); # start tag or end tag
1806    
1807            redo A;
1808          } elsif ($self->{next_char} == 0x002F) { # /
1809            !!!cp (122);
1810            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1811            !!!next-input-character;
1812            redo A;
1813          } else {
1814            !!!cp ('124.1');
1815            !!!parse-error (type => 'no space between attributes');
1816            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1817            ## reconsume
1818            redo A;
1819          }
1820        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1821          if ($self->{next_char} == 0x003E) { # >
1822            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1823              !!!cp ('124.2');
1824              !!!parse-error (type => 'nestc', token => $self->{current_token});
1825              ## TODO: Different type than slash in start tag
1826              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1827              if ($self->{current_token}->{attributes}) {
1828                !!!cp ('124.4');
1829                !!!parse-error (type => 'end tag attribute');
1830              } else {
1831                !!!cp ('124.5');
1832              }
1833              ## TODO: Test |<title></title/>|
1834            } else {
1835              !!!cp ('124.3');
1836              $self->{self_closing} = 1;
1837            }
1838    
1839            $self->{state} = DATA_STATE;
1840            !!!next-input-character;
1841    
1842            !!!emit ($self->{current_token}); # start tag or end tag
1843    
1844            redo A;
1845          } else {
1846            !!!cp ('124.4');
1847            !!!parse-error (type => 'nestc');
1848            ## TODO: This error type is wrong.
1849            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1850            ## Reconsume.
1851            redo A;
1852          }
1853      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1854        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1855                
1856        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1857          #my $token = {type => COMMENT_TOKEN, data => ''};
1858    
1859        BC: {        BC: {
1860          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1861              !!!cp (124);
1862            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1863            !!!next-input-character;            !!!next-input-character;
1864    
1865            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1866    
1867            redo A;            redo A;
1868          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1869              !!!cp (125);
1870            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1871            ## reconsume            ## reconsume
1872    
1873            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1874    
1875            redo A;            redo A;
1876          } else {          } else {
1877            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1878              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1879            !!!next-input-character;            !!!next-input-character;
1880            redo BC;            redo BC;
1881          }          }
1882        } # BC        } # BC
1883    
1884          die "$0: _get_next_token: unexpected case [BC]";
1885      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1886        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1887    
1888          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1889    
1890        my @next_char;        my @next_char;
1891        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1892                
1893        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1894          !!!next-input-character;          !!!next-input-character;
1895          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1896          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1897            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            !!!cp (127);
1898              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1899                                        line => $l, column => $c,
1900                                       };
1901            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1902            !!!next-input-character;            !!!next-input-character;
1903            redo A;            redo A;
1904            } else {
1905              !!!cp (128);
1906          }          }
1907        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1908                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1909          !!!next-input-character;          !!!next-input-character;
1910          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1911          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1912              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1913            !!!next-input-character;            !!!next-input-character;
1914            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1915            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1916                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1917              !!!next-input-character;              !!!next-input-character;
1918              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1919              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1920                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1921                !!!next-input-character;                !!!next-input-character;
1922                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1923                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1924                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1925                  !!!next-input-character;                  !!!next-input-character;
1926                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1927                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1928                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1929                    !!!next-input-character;                    !!!next-input-character;
1930                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1931                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1932                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1933                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1934                        ## TODO: What a stupid code this is!
1935                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1936                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1937                                                  quirks => 1,
1938                                                  line => $l, column => $c,
1939                                                 };
1940                        !!!next-input-character;
1941                        redo A;
1942                      } else {
1943                        !!!cp (130);
1944                      }
1945                    } else {
1946                      !!!cp (131);
1947                    }
1948                  } else {
1949                    !!!cp (132);
1950                  }
1951                } else {
1952                  !!!cp (133);
1953                }
1954              } else {
1955                !!!cp (134);
1956              }
1957            } else {
1958              !!!cp (135);
1959            }
1960          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1961                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1962                   $self->{next_char} == 0x005B) { # [
1963            !!!next-input-character;
1964            push @next_char, $self->{next_char};
1965            if ($self->{next_char} == 0x0043) { # C
1966              !!!next-input-character;
1967              push @next_char, $self->{next_char};
1968              if ($self->{next_char} == 0x0044) { # D
1969                !!!next-input-character;
1970                push @next_char, $self->{next_char};
1971                if ($self->{next_char} == 0x0041) { # A
1972                  !!!next-input-character;
1973                  push @next_char, $self->{next_char};
1974                  if ($self->{next_char} == 0x0054) { # T
1975                    !!!next-input-character;
1976                    push @next_char, $self->{next_char};
1977                    if ($self->{next_char} == 0x0041) { # A
1978                      !!!next-input-character;
1979                      push @next_char, $self->{next_char};
1980                      if ($self->{next_char} == 0x005B) { # [
1981                        !!!cp (135.1);
1982                        $self->{state} = CDATA_BLOCK_STATE;
1983                      !!!next-input-character;                      !!!next-input-character;
1984                      redo A;                      redo A;
1985                      } else {
1986                        !!!cp (135.2);
1987                    }                    }
1988                    } else {
1989                      !!!cp (135.3);
1990                  }                  }
1991                  } else {
1992                    !!!cp (135.4);                
1993                }                }
1994                } else {
1995                  !!!cp (135.5);
1996              }              }
1997              } else {
1998                !!!cp (135.6);
1999            }            }
2000            } else {
2001              !!!cp (135.7);
2002          }          }
2003          } else {
2004            !!!cp (136);
2005        }        }
2006    
2007        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
2008        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2009        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2010        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2011          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2012                                    line => $l, column => $c,
2013                                   };
2014        redo A;        redo A;
2015                
2016        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2017        ## 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?
2018      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2019        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2020            !!!cp (137);
2021          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2022          !!!next-input-character;          !!!next-input-character;
2023          redo A;          redo A;
2024        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2025            !!!cp (138);
2026          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2027          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2028          !!!next-input-character;          !!!next-input-character;
# Line 1221  sub _get_next_token ($) { Line 2030  sub _get_next_token ($) {
2030          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2031    
2032          redo A;          redo A;
2033        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2034            !!!cp (139);
2035          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2036          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2037          ## reconsume          ## reconsume
# Line 1230  sub _get_next_token ($) { Line 2040  sub _get_next_token ($) {
2040    
2041          redo A;          redo A;
2042        } else {        } else {
2043            !!!cp (140);
2044          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2045              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2046          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2047          !!!next-input-character;          !!!next-input-character;
2048          redo A;          redo A;
2049        }        }
2050      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2051        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2052            !!!cp (141);
2053          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2054          !!!next-input-character;          !!!next-input-character;
2055          redo A;          redo A;
2056        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2057            !!!cp (142);
2058          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2059          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2060          !!!next-input-character;          !!!next-input-character;
# Line 1249  sub _get_next_token ($) { Line 2062  sub _get_next_token ($) {
2062          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2063    
2064          redo A;          redo A;
2065        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2066            !!!cp (143);
2067          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2068          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2069          ## reconsume          ## reconsume
# Line 1258  sub _get_next_token ($) { Line 2072  sub _get_next_token ($) {
2072    
2073          redo A;          redo A;
2074        } else {        } else {
2075            !!!cp (144);
2076          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2077              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2078          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2079          !!!next-input-character;          !!!next-input-character;
2080          redo A;          redo A;
2081        }        }
2082      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2083        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2084            !!!cp (145);
2085          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2086          !!!next-input-character;          !!!next-input-character;
2087          redo A;          redo A;
2088        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2089            !!!cp (146);
2090          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2091          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2092          ## reconsume          ## reconsume
# Line 1278  sub _get_next_token ($) { Line 2095  sub _get_next_token ($) {
2095    
2096          redo A;          redo A;
2097        } else {        } else {
2098          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2099            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2100          ## Stay in the state          ## Stay in the state
2101          !!!next-input-character;          !!!next-input-character;
2102          redo A;          redo A;
2103        }        }
2104      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2105        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2106            !!!cp (148);
2107          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2108          !!!next-input-character;          !!!next-input-character;
2109          redo A;          redo A;
2110        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2111            !!!cp (149);
2112          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2113          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2114          ## reconsume          ## reconsume
# Line 1297  sub _get_next_token ($) { Line 2117  sub _get_next_token ($) {
2117    
2118          redo A;          redo A;
2119        } else {        } else {
2120          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2121            $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2122          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2123          !!!next-input-character;          !!!next-input-character;
2124          redo A;          redo A;
2125        }        }
2126      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2127        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2128            !!!cp (151);
2129          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2130          !!!next-input-character;          !!!next-input-character;
2131    
2132          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2133    
2134          redo A;          redo A;
2135        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2136          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2137            !!!parse-error (type => 'dash in comment',
2138                            line => $self->{line_prev},
2139                            column => $self->{column_prev});
2140          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2141          ## Stay in the state          ## Stay in the state
2142          !!!next-input-character;          !!!next-input-character;
2143          redo A;          redo A;
2144        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2145            !!!cp (153);
2146          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2147          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2148          ## reconsume          ## reconsume
# Line 1325  sub _get_next_token ($) { Line 2151  sub _get_next_token ($) {
2151    
2152          redo A;          redo A;
2153        } else {        } else {
2154          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2155          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2156                            line => $self->{line_prev},
2157                            column => $self->{column_prev});
2158            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
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} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2164        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2165            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2166            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2167            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2168            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2169            !!!cp (155);
2170          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2171          !!!next-input-character;          !!!next-input-character;
2172          redo A;          redo A;
2173        } else {        } else {
2174            !!!cp (156);
2175          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2176          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2177          ## reconsume          ## reconsume
2178          redo A;          redo A;
2179        }        }
2180      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2181        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2182            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2183            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2184            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2185            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2186            !!!cp (157);
2187          ## Stay in the state          ## Stay in the state
2188          !!!next-input-character;          !!!next-input-character;
2189          redo A;          redo A;
2190        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2191            !!!cp (158);
2192          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2193          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2194          !!!next-input-character;          !!!next-input-character;
2195    
2196          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2197    
2198          redo A;          redo A;
2199        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2200            !!!cp (159);
2201          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2202          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2203          ## reconsume          ## reconsume
2204    
2205          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2206    
2207          redo A;          redo A;
2208        } else {        } else {
2209          $self->{current_token}          !!!cp (160);
2210              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
2211                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2212  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2213          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2214          !!!next-input-character;          !!!next-input-character;
# Line 1383  sub _get_next_token ($) { Line 2216  sub _get_next_token ($) {
2216        }        }
2217      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2218  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2219        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2220            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2221            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2222            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2223            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2224            !!!cp (161);
2225          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2226          !!!next-input-character;          !!!next-input-character;
2227          redo A;          redo A;
2228        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2229            !!!cp (162);
2230          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2231          !!!next-input-character;          !!!next-input-character;
2232    
2233          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2234    
2235          redo A;          redo A;
2236        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2237            !!!cp (163);
2238          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2239          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2240          ## reconsume          ## reconsume
2241    
2242          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2243          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2244    
2245          redo A;          redo A;
2246        } else {        } else {
2247            !!!cp (164);
2248          $self->{current_token}->{name}          $self->{current_token}->{name}
2249            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2250          ## Stay in the state          ## Stay in the state
2251          !!!next-input-character;          !!!next-input-character;
2252          redo A;          redo A;
2253        }        }
2254      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2255        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2256            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2257            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2258            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2259            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2260            !!!cp (165);
2261          ## Stay in the state          ## Stay in the state
2262          !!!next-input-character;          !!!next-input-character;
2263          redo A;          redo A;
2264        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2265            !!!cp (166);
2266          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2267          !!!next-input-character;          !!!next-input-character;
2268    
2269          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2270    
2271          redo A;          redo A;
2272        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2273            !!!cp (167);
2274          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2275          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2276          ## reconsume          ## reconsume
2277    
2278          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2279          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2280    
2281          redo A;          redo A;
2282        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2283                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2284          !!!next-input-character;          !!!next-input-character;
2285          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2286              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2287            !!!next-input-character;            !!!next-input-character;
2288            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2289                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2290              !!!next-input-character;              !!!next-input-character;
2291              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2292                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2293                !!!next-input-character;                !!!next-input-character;
2294                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2295                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2296                  !!!next-input-character;                  !!!next-input-character;
2297                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2298                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2299                      !!!cp (168);
2300                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2301                    !!!next-input-character;                    !!!next-input-character;
2302                    redo A;                    redo A;
2303                    } else {
2304                      !!!cp (169);
2305                  }                  }
2306                  } else {
2307                    !!!cp (170);
2308                }                }
2309                } else {
2310                  !!!cp (171);
2311              }              }
2312              } else {
2313                !!!cp (172);
2314            }            }
2315            } else {
2316              !!!cp (173);
2317          }          }
2318    
2319          #          #
2320        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2321                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2322          !!!next-input-character;          !!!next-input-character;
2323          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2324              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2325            !!!next-input-character;            !!!next-input-character;
2326            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2327                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2328              !!!next-input-character;              !!!next-input-character;
2329              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2330                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2331                !!!next-input-character;                !!!next-input-character;
2332                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2333                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2334                  !!!next-input-character;                  !!!next-input-character;
2335                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2336                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2337                      !!!cp (174);
2338                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2339                    !!!next-input-character;                    !!!next-input-character;
2340                    redo A;                    redo A;
2341                    } else {
2342                      !!!cp (175);
2343                  }                  }
2344                  } else {
2345                    !!!cp (176);
2346                }                }
2347                } else {
2348                  !!!cp (177);
2349              }              }
2350              } else {
2351                !!!cp (178);
2352            }            }
2353            } else {
2354              !!!cp (179);
2355          }          }
2356    
2357          #          #
2358        } else {        } else {
2359            !!!cp (180);
2360          !!!next-input-character;          !!!next-input-character;
2361          #          #
2362        }        }
2363    
2364        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2365          $self->{current_token}->{quirks} = 1;
2366    
2367        $self->{state} = BOGUS_DOCTYPE_STATE;        $self->{state} = BOGUS_DOCTYPE_STATE;
2368        # next-input-character is already done        # next-input-character is already done
2369        redo A;        redo A;
# Line 1506  sub _get_next_token ($) { Line 2371  sub _get_next_token ($) {
2371        if ({        if ({
2372              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2373              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2374            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2375            !!!cp (181);
2376          ## Stay in the state          ## Stay in the state
2377          !!!next-input-character;          !!!next-input-character;
2378          redo A;          redo A;
2379        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2380            !!!cp (182);
2381          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2382          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2383          !!!next-input-character;          !!!next-input-character;
2384          redo A;          redo A;
2385        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2386            !!!cp (183);
2387          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2388          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2389          !!!next-input-character;          !!!next-input-character;
2390          redo A;          redo A;
2391        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2392            !!!cp (184);
2393          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2394    
2395          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2396          !!!next-input-character;          !!!next-input-character;
2397    
2398          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2399          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2400    
2401          redo A;          redo A;
2402        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2403            !!!cp (185);
2404          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2405    
2406          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2407          ## reconsume          ## reconsume
2408    
2409          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2410          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2411    
2412          redo A;          redo A;
2413        } else {        } else {
2414            !!!cp (186);
2415          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2416            $self->{current_token}->{quirks} = 1;
2417    
2418          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2419          !!!next-input-character;          !!!next-input-character;
2420          redo A;          redo A;
2421        }        }
2422      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2423        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2424            !!!cp (187);
2425          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2426          !!!next-input-character;          !!!next-input-character;
2427          redo A;          redo A;
2428        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2429            !!!cp (188);
2430            !!!parse-error (type => 'unclosed PUBLIC literal');
2431    
2432            $self->{state} = DATA_STATE;
2433            !!!next-input-character;
2434    
2435            $self->{current_token}->{quirks} = 1;
2436            !!!emit ($self->{current_token}); # DOCTYPE
2437    
2438            redo A;
2439          } elsif ($self->{next_char} == -1) {
2440            !!!cp (189);
2441          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2442    
2443          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2444          ## reconsume          ## reconsume
2445    
2446          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2447          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2448    
2449          redo A;          redo A;
2450        } else {        } else {
2451            !!!cp (190);
2452          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2453              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2454          ## Stay in the state          ## Stay in the state
2455          !!!next-input-character;          !!!next-input-character;
2456          redo A;          redo A;
2457        }        }
2458      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2459        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2460            !!!cp (191);
2461          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2462          !!!next-input-character;          !!!next-input-character;
2463          redo A;          redo A;
2464        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2465            !!!cp (192);
2466            !!!parse-error (type => 'unclosed PUBLIC literal');
2467    
2468            $self->{state} = DATA_STATE;
2469            !!!next-input-character;
2470    
2471            $self->{current_token}->{quirks} = 1;
2472            !!!emit ($self->{current_token}); # DOCTYPE
2473    
2474            redo A;
2475          } elsif ($self->{next_char} == -1) {
2476            !!!cp (193);
2477          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2478    
2479          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2480          ## reconsume          ## reconsume
2481    
2482          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2483          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2484    
2485          redo A;          redo A;
2486        } else {        } else {
2487            !!!cp (194);
2488          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2489              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2490          ## Stay in the state          ## Stay in the state
2491          !!!next-input-character;          !!!next-input-character;
2492          redo A;          redo A;
# Line 1594  sub _get_next_token ($) { Line 2495  sub _get_next_token ($) {
2495        if ({        if ({
2496              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2497              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2498            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2499            !!!cp (195);
2500          ## Stay in the state          ## Stay in the state
2501          !!!next-input-character;          !!!next-input-character;
2502          redo A;          redo A;
2503        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2504            !!!cp (196);
2505          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2506          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2507          !!!next-input-character;          !!!next-input-character;
2508          redo A;          redo A;
2509        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2510            !!!cp (197);
2511          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2512          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2513          !!!next-input-character;          !!!next-input-character;
2514          redo A;          redo A;
2515        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2516            !!!cp (198);
2517          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2518          !!!next-input-character;          !!!next-input-character;
2519    
2520          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2521    
2522          redo A;          redo A;
2523        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2524            !!!cp (199);
2525          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2526    
2527          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2528          ## reconsume          ## reconsume
2529    
2530          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2531          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2532    
2533          redo A;          redo A;
2534        } else {        } else {
2535            !!!cp (200);
2536          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2537            $self->{current_token}->{quirks} = 1;
2538    
2539          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2540          !!!next-input-character;          !!!next-input-character;
2541          redo A;          redo A;
# Line 1635  sub _get_next_token ($) { Line 2544  sub _get_next_token ($) {
2544        if ({        if ({
2545              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2546              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2547            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2548            !!!cp (201);
2549          ## Stay in the state          ## Stay in the state
2550          !!!next-input-character;          !!!next-input-character;
2551          redo A;          redo A;
2552        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2553            !!!cp (202);
2554          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2555          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2556          !!!next-input-character;          !!!next-input-character;
2557          redo A;          redo A;
2558        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2559            !!!cp (203);
2560          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2561          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2562          !!!next-input-character;          !!!next-input-character;
2563          redo A;          redo A;
2564        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2565            !!!cp (204);
2566          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2567          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2568          !!!next-input-character;          !!!next-input-character;
2569    
2570          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2571          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2572    
2573          redo A;          redo A;
2574        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2575            !!!cp (205);
2576          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2577    
2578          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2579          ## reconsume          ## reconsume
2580    
2581          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2582          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2583    
2584          redo A;          redo A;
2585        } else {        } else {
2586            !!!cp (206);
2587          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2588            $self->{current_token}->{quirks} = 1;
2589    
2590          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2591          !!!next-input-character;          !!!next-input-character;
2592          redo A;          redo A;
2593        }        }
2594      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2595        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2596            !!!cp (207);
2597          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2598          !!!next-input-character;          !!!next-input-character;
2599          redo A;          redo A;
2600        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2601            !!!cp (208);
2602            !!!parse-error (type => 'unclosed PUBLIC literal');
2603    
2604            $self->{state} = DATA_STATE;
2605            !!!next-input-character;
2606    
2607            $self->{current_token}->{quirks} = 1;
2608            !!!emit ($self->{current_token}); # DOCTYPE
2609    
2610            redo A;
2611          } elsif ($self->{next_char} == -1) {
2612            !!!cp (209);
2613          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2614    
2615          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2616          ## reconsume          ## reconsume
2617    
2618          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2619          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2620    
2621          redo A;          redo A;
2622        } else {        } else {
2623            !!!cp (210);
2624          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2625              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2626          ## Stay in the state          ## Stay in the state
2627          !!!next-input-character;          !!!next-input-character;
2628          redo A;          redo A;
2629        }        }
2630      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2631        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2632            !!!cp (211);
2633          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2634          !!!next-input-character;          !!!next-input-character;
2635          redo A;          redo A;
2636        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2637            !!!cp (212);
2638            !!!parse-error (type => 'unclosed PUBLIC literal');
2639    
2640            $self->{state} = DATA_STATE;
2641            !!!next-input-character;
2642    
2643            $self->{current_token}->{quirks} = 1;
2644            !!!emit ($self->{current_token}); # DOCTYPE
2645    
2646            redo A;
2647          } elsif ($self->{next_char} == -1) {
2648            !!!cp (213);
2649          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2650    
2651          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2652          ## reconsume          ## reconsume
2653    
2654          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2655          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2656    
2657          redo A;          redo A;
2658        } else {        } else {
2659            !!!cp (214);
2660          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2661              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2662          ## Stay in the state          ## Stay in the state
2663          !!!next-input-character;          !!!next-input-character;
2664          redo A;          redo A;
# Line 1722  sub _get_next_token ($) { Line 2667  sub _get_next_token ($) {
2667        if ({        if ({
2668              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2669              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2670            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2671            !!!cp (215);
2672          ## Stay in the state          ## Stay in the state
2673          !!!next-input-character;          !!!next-input-character;
2674          redo A;          redo A;
2675        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2676            !!!cp (216);
2677          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2678          !!!next-input-character;          !!!next-input-character;
2679    
2680          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2681    
2682          redo A;          redo A;
2683        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2684            !!!cp (217);
2685          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2686    
2687          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2688          ## reconsume          ## reconsume
2689    
2690          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2691          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2692    
2693          redo A;          redo A;
2694        } else {        } else {
2695            !!!cp (218);
2696          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2697            #$self->{current_token}->{quirks} = 1;
2698    
2699          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2700          !!!next-input-character;          !!!next-input-character;
2701          redo A;          redo A;
2702        }        }
2703      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2704        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2705            !!!cp (219);
2706          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2707          !!!next-input-character;          !!!next-input-character;
2708    
         delete $self->{current_token}->{correct};  
2709          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2710    
2711          redo A;          redo A;
2712        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2713            !!!cp (220);
2714          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2715          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2716          ## reconsume          ## reconsume
2717    
         delete $self->{current_token}->{correct};  
2718          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2719    
2720          redo A;          redo A;
2721        } else {        } else {
2722            !!!cp (221);
2723          ## Stay in the state          ## Stay in the state
2724          !!!next-input-character;          !!!next-input-character;
2725          redo A;          redo A;
2726        }        }
2727        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2728          my $s = '';
2729          
2730          my ($l, $c) = ($self->{line}, $self->{column});
2731    
2732          CS: while ($self->{next_char} != -1) {
2733            if ($self->{next_char} == 0x005D) { # ]
2734              !!!next-input-character;
2735              if ($self->{next_char} == 0x005D) { # ]
2736                !!!next-input-character;
2737                MDC: {
2738                  if ($self->{next_char} == 0x003E) { # >
2739                    !!!cp (221.1);
2740                    !!!next-input-character;
2741                    last CS;
2742                  } elsif ($self->{next_char} == 0x005D) { # ]
2743                    !!!cp (221.2);
2744                    $s .= ']';
2745                    !!!next-input-character;
2746                    redo MDC;
2747                  } else {
2748                    !!!cp (221.3);
2749                    $s .= ']]';
2750                    #
2751                  }
2752                } # MDC
2753              } else {
2754                !!!cp (221.4);
2755                $s .= ']';
2756                #
2757              }
2758            } else {
2759              !!!cp (221.5);
2760              #
2761            }
2762            $s .= chr $self->{next_char};
2763            !!!next-input-character;
2764          } # CS
2765    
2766          $self->{state} = DATA_STATE;
2767          ## next-input-character done or EOF, which is reconsumed.
2768    
2769          if (length $s) {
2770            !!!cp (221.6);
2771            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2772                      line => $l, column => $c});
2773          } else {
2774            !!!cp (221.7);
2775          }
2776    
2777          redo A;
2778    
2779          ## ISSUE: "text tokens" in spec.
2780          ## TODO: Streaming support
2781      } else {      } else {
2782        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2783      }      }
# Line 1780  sub _get_next_token ($) { Line 2786  sub _get_next_token ($) {
2786    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2787  } # _get_next_token  } # _get_next_token
2788    
2789  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2790    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2791    
2792      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2793    
2794    if ({    if ({
2795         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2796         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2797        }->{$self->{next_input_character}}) {         $additional => 1,
2798          }->{$self->{next_char}}) {
2799        !!!cp (1001);
2800      ## Don't consume      ## Don't consume
2801      ## No error      ## No error
2802      return undef;      return undef;
2803    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2804      !!!next-input-character;      !!!next-input-character;
2805      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2806          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2807        my $code;        my $code;
2808        X: {        X: {
2809          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2810          !!!next-input-character;          !!!next-input-character;
2811          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2812              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2813              !!!cp (1002);
2814            $code ||= 0;            $code ||= 0;
2815            $code *= 0x10;            $code *= 0x10;
2816            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2817            redo X;            redo X;
2818          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2819                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2820              !!!cp (1003);
2821            $code ||= 0;            $code ||= 0;
2822            $code *= 0x10;            $code *= 0x10;
2823            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2824            redo X;            redo X;
2825          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2826                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2827              !!!cp (1004);
2828            $code ||= 0;            $code ||= 0;
2829            $code *= 0x10;            $code *= 0x10;
2830            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2831            redo X;            redo X;
2832          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2833            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2834            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2835            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2836              $self->{next_char} = 0x0023; # #
2837            return undef;            return undef;
2838          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2839              !!!cp (1006);
2840            !!!next-input-character;            !!!next-input-character;
2841          } else {          } else {
2842            !!!parse-error (type => 'no refc');            !!!cp (1007);
2843              !!!parse-error (type => 'no refc', line => $l, column => $c);
2844          }          }
2845    
2846          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2847            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2848              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2849            $code = 0xFFFD;            $code = 0xFFFD;
2850          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2851            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2852              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2853            $code = 0xFFFD;            $code = 0xFFFD;
2854          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2855            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2856              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2857            $code = 0x000A;            $code = 0x000A;
2858          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2859            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2860              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2861            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2862          }          }
2863    
2864          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2865                    has_reference => 1,
2866                    line => $l, column => $c,
2867                   };
2868        } # X        } # X
2869      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2870               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2871        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2872        !!!next-input-character;        !!!next-input-character;
2873                
2874        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2875                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2876            !!!cp (1012);
2877          $code *= 10;          $code *= 10;
2878          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2879                    
2880          !!!next-input-character;          !!!next-input-character;
2881        }        }
2882    
2883        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2884            !!!cp (1013);
2885          !!!next-input-character;          !!!next-input-character;
2886        } else {        } else {
2887          !!!parse-error (type => 'no refc');          !!!cp (1014);
2888            !!!parse-error (type => 'no refc', line => $l, column => $c);
2889        }        }
2890    
2891        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2892          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2893            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2894          $code = 0xFFFD;          $code = 0xFFFD;
2895        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2896          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2897            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2898          $code = 0xFFFD;          $code = 0xFFFD;
2899        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2900          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2901            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2902          $code = 0x000A;          $code = 0x000A;
2903        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2904          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2905            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2906          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2907        }        }
2908                
2909        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2910                  line => $l, column => $c,
2911                 };
2912      } else {      } else {
2913        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2914        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2915        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2916          $self->{next_char} = 0x0023; # #
2917        return undef;        return undef;
2918      }      }
2919    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2920              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2921             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2922              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2923      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2924      !!!next-input-character;      !!!next-input-character;
2925    
2926      my $value = $entity_name;      my $value = $entity_name;
# Line 1895  sub _tokenize_attempt_to_consume_an_enti Line 2928  sub _tokenize_attempt_to_consume_an_enti
2928      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2929      our $EntityChar;      our $EntityChar;
2930    
2931      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2932             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2933             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2934               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2935              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2936               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2937              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2938               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2939              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2940        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2941        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2942          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2943              !!!cp (1020);
2944            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2945            $match = 1;            $match = 1;
2946            !!!next-input-character;            !!!next-input-character;
2947            last;            last;
2948          } else {          } else {
2949              !!!cp (1021);
2950            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2951            $match = -1;            $match = -1;
2952            !!!next-input-character;            !!!next-input-character;
2953          }          }
2954        } else {        } else {
2955          $value .= chr $self->{next_input_character};          !!!cp (1022);
2956            $value .= chr $self->{next_char};
2957          $match *= 2;          $match *= 2;
2958          !!!next-input-character;          !!!next-input-character;
2959        }        }
2960      }      }
2961            
2962      if ($match > 0) {      if ($match > 0) {
2963        return {type => CHARACTER_TOKEN, data => $value};        !!!cp (1023);
2964          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2965                  line => $l, column => $c,
2966                 };
2967      } elsif ($match < 0) {      } elsif ($match < 0) {
2968        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2969        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2970          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          !!!cp (1024);
2971        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2972          return {type => CHARACTER_TOKEN, data => $value};                  line => $l, column => $c,
2973                   };
2974          } else {
2975            !!!cp (1025);
2976            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2977                    line => $l, column => $c,
2978                   };
2979        }        }
2980      } else {      } else {
2981        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2982        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2983        return {type => CHARACTER_TOKEN, data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
2984          return {type => CHARACTER_TOKEN, data => '&'.$value,
2985                  line => $l, column => $c,
2986                 };
2987      }      }
2988    } else {    } else {
2989        !!!cp (1027);
2990      ## no characters are consumed      ## no characters are consumed
2991      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2992      return undef;      return undef;
2993    }    }
2994  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1977  sub _construct_tree ($) { Line 3026  sub _construct_tree ($) {
3026        
3027    !!!next-token;    !!!next-token;
3028    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3029    undef $self->{form_element};    undef $self->{form_element};
3030    undef $self->{head_element};    undef $self->{head_element};
3031    $self->{open_elements} = [];    $self->{open_elements} = [];
3032    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3033    
3034      ## NOTE: The "initial" insertion mode.
3035    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3036    
3037      ## NOTE: The "before html" insertion mode.
3038    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3039      $self->{insertion_mode} = BEFORE_HEAD_IM;
3040    
3041      ## NOTE: The "before head" insertion mode and so on.
3042    $self->_tree_construction_main;    $self->_tree_construction_main;
3043  } # _construct_tree  } # _construct_tree
3044    
3045  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3046    my $self = shift;    my $self = shift;
3047    
3048      ## NOTE: "initial" insertion mode
3049    
3050    INITIAL: {    INITIAL: {
3051      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3052        ## 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 3058  sub _tree_construction_initial ($) {
3058        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3059            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3060            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3061          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3062            !!!parse-error (type => 'not HTML5', token => $token);
3063        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3064            !!!cp ('t2');
3065          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3066          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3067          } else {
3068            !!!cp ('t3');
3069        }        }
3070                
3071        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3072          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3073          ## NOTE: Default value for both |public_id| and |system_id| attributes
3074          ## are empty strings, so that we don't set any value in missing cases.
3075        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3076            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3077        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2017  sub _tree_construction_initial ($) { Line 3080  sub _tree_construction_initial ($) {
3080        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3081        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3082                
3083        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3084            !!!cp ('t4');
3085          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3086        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3087          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 2071  sub _tree_construction_initial ($) { Line 3135  sub _tree_construction_initial ($) {
3135            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
3136            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
3137            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
3138              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
3139              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
3140              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
3141            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
3142            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
3143            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 2093  sub _tree_construction_initial ($) { Line 3160  sub _tree_construction_initial ($) {
3160            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
3161            "HTML" => 1,            "HTML" => 1,
3162          }->{$pubid}) {          }->{$pubid}) {
3163              !!!cp ('t5');
3164            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3165          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
3166                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
3167            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3168                !!!cp ('t6');
3169              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3170            } else {            } else {
3171                !!!cp ('t7');
3172              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3173            }            }
3174          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
3175                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
3176              !!!cp ('t8');
3177            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3178            } else {
3179              !!!cp ('t9');
3180          }          }
3181          } else {
3182            !!!cp ('t10');
3183        }        }
3184        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3185          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3186          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3187          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") {
3188              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
3189            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3190              !!!cp ('t11');
3191            } else {
3192              !!!cp ('t12');
3193          }          }
3194          } else {
3195            !!!cp ('t13');
3196        }        }
3197                
3198        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3199        !!!next-token;        !!!next-token;
3200        return;        return;
3201      } elsif ({      } elsif ({
# Line 2122  sub _tree_construction_initial ($) { Line 3203  sub _tree_construction_initial ($) {
3203                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3204                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3205               }->{$token->{type}}) {               }->{$token->{type}}) {
3206        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3207          !!!parse-error (type => 'no DOCTYPE', token => $token);
3208        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3209        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3210        ## reprocess        ## reprocess
3211          !!!ack-later;
3212        return;        return;
3213      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3214        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3215          ## Ignore the token          ## Ignore the token
3216    
3217          unless (length $token->{data}) {          unless (length $token->{data}) {
3218            ## Stay in the phase            !!!cp ('t15');
3219              ## Stay in the insertion mode.
3220            !!!next-token;            !!!next-token;
3221            redo INITIAL;            redo INITIAL;
3222            } else {
3223              !!!cp ('t16');
3224          }          }
3225          } else {
3226            !!!cp ('t17');
3227        }        }
3228    
3229        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3230        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3231        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3232        ## reprocess        ## reprocess
3233        return;        return;
3234      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3235          !!!cp ('t18');
3236        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3237        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3238                
3239        ## Stay in the phase.        ## Stay in the insertion mode.
3240        !!!next-token;        !!!next-token;
3241        redo INITIAL;        redo INITIAL;
3242      } else {      } else {
3243        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3244      }      }
3245    } # INITIAL    } # INITIAL
3246    
3247      die "$0: _tree_construction_initial: This should be never reached";
3248  } # _tree_construction_initial  } # _tree_construction_initial
3249    
3250  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3251    my $self = shift;    my $self = shift;
3252    
3253      ## NOTE: "before html" insertion mode.
3254        
3255    B: {    B: {
3256        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3257          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3258            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3259          ## Ignore the token          ## Ignore the token
3260          ## Stay in the phase          ## Stay in the insertion mode.
3261          !!!next-token;          !!!next-token;
3262          redo B;          redo B;
3263        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3264            !!!cp ('t20');
3265          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3266          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3267          ## Stay in the phase          ## Stay in the insertion mode.
3268          !!!next-token;          !!!next-token;
3269          redo B;          redo B;
3270        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2177  sub _tree_construction_root_element ($) Line 3272  sub _tree_construction_root_element ($)
3272            ## Ignore the token.            ## Ignore the token.
3273    
3274            unless (length $token->{data}) {            unless (length $token->{data}) {
3275              ## Stay in the phase              !!!cp ('t21');
3276                ## Stay in the insertion mode.
3277              !!!next-token;              !!!next-token;
3278              redo B;              redo B;
3279              } else {
3280                !!!cp ('t22');
3281            }            }
3282            } else {
3283              !!!cp ('t23');
3284          }          }
3285    
3286          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
3287    
3288          #          #
3289        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3290          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3291              $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"            my $root_element;
3292            $self->{application_cache_selection}            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3293                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
3294            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
3295                  [$root_element, $el_category->{html}];
3296    
3297              if ($token->{attributes}->{manifest}) {
3298                !!!cp ('t24');
3299                $self->{application_cache_selection}
3300                    ->($token->{attributes}->{manifest}->{value});
3301                ## ISSUE: Spec is unclear on relative references.
3302                ## According to Hixie (#whatwg 2008-03-19), it should be
3303                ## resolved against the base URI of the document in HTML
3304                ## or xml:base of the element in XHTML.
3305              } else {
3306                !!!cp ('t25');
3307                $self->{application_cache_selection}->(undef);
3308              }
3309    
3310              !!!nack ('t25c');
3311    
3312              !!!next-token;
3313              return; ## Go to the "before head" insertion mode.
3314          } else {          } else {
3315            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
3316              #
3317          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3318        } elsif ({        } elsif ({
3319                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3320                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3321                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3322          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
3323          #          #
3324        } else {        } else {
3325          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3326        }        }
3327    
3328        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3329        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3330        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3331        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3332        #redo B;  
3333        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3334    
3335        ## NOTE: Reprocess the token.
3336        !!!ack-later;
3337        return; ## Go to the "before head" insertion mode.
3338    
3339        ## ISSUE: There is an issue in the spec
3340    } # B    } # B
3341    
3342      die "$0: _tree_construction_root_element: This should never be reached";
3343  } # _tree_construction_root_element  } # _tree_construction_root_element
3344    
3345  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2231  sub _reset_insertion_mode ($) { Line 3354  sub _reset_insertion_mode ($) {
3354            
3355      ## Step 3      ## Step 3
3356      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"!?  
3357        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3358          $last = 1;          $last = 1;
3359          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3360            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
3361                $self->{inner_html_node}->[1] eq 'th') {              !!!cp ('t27');
3362              #              #
3363            } else {            } else {
3364                !!!cp ('t28');
3365              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
3366            }            }
3367          }          }
3368        }        }
3369            
3370        ## Step 4..13      ## Step 4..14
3371        my $new_mode = {      my $new_mode;
3372        if ($node->[1] & FOREIGN_EL) {
3373          ## NOTE: Strictly spaking, the line below only applies to MathML and
3374          ## SVG elements.  Currently the HTML syntax supports only MathML and
3375          ## SVG elements as foreigners.
3376          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3377          ## ISSUE: What is set as the secondary insertion mode?
3378        } else {
3379          $new_mode = {
3380                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3381                          ## NOTE: |option| and |optgroup| do not set
3382                          ## insertion mode to "in select" by themselves.
3383                        td => IN_CELL_IM,                        td => IN_CELL_IM,
3384                        th => IN_CELL_IM,                        th => IN_CELL_IM,
3385                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2263  sub _reset_insertion_mode ($) { Line 3392  sub _reset_insertion_mode ($) {
3392                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3393                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3394                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3395                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3396        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3397        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3398                
3399        ## Step 14        ## Step 15
3400        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3401          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3402              !!!cp ('t29');
3403            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3404          } else {          } else {
3405              ## ISSUE: Can this state be reached?
3406              !!!cp ('t30');
3407            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3408          }          }
3409          return;          return;
3410          } else {
3411            !!!cp ('t31');
3412        }        }
3413                
3414        ## Step 15        ## Step 16
3415        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3416                
3417        ## Step 16        ## Step 17
3418        $i--;        $i--;
3419        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3420                
3421        ## Step 17        ## Step 18
3422        redo S3;        redo S3;
3423      } # S3      } # S3
3424    
3425      die "$0: _reset_insertion_mode: This line should never be reached";
3426  } # _reset_insertion_mode  } # _reset_insertion_mode
3427    
3428  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2307  sub _tree_construction_main ($) { Line 3444  sub _tree_construction_main ($) {
3444      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3445      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3446        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3447            !!!cp ('t32');
3448          return;          return;
3449        }        }
3450      }      }
# Line 2321  sub _tree_construction_main ($) { Line 3459  sub _tree_construction_main ($) {
3459    
3460        ## Step 6        ## Step 6
3461        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3462            !!!cp ('t33_1');
3463          #          #
3464        } else {        } else {
3465          my $in_open_elements;          my $in_open_elements;
3466          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3467            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3468                !!!cp ('t33');
3469              $in_open_elements = 1;              $in_open_elements = 1;
3470              last OE;              last OE;
3471            }            }
3472          }          }
3473          if ($in_open_elements) {          if ($in_open_elements) {
3474              !!!cp ('t34');
3475            #            #
3476          } else {          } else {
3477              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3478              !!!cp ('t35');
3479            redo S4;            redo S4;
3480          }          }
3481        }        }
# Line 2355  sub _tree_construction_main ($) { Line 3498  sub _tree_construction_main ($) {
3498    
3499        ## Step 11        ## Step 11
3500        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3501            !!!cp ('t36');
3502          ## Step 7'          ## Step 7'
3503          $i++;          $i++;
3504          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3505                    
3506          redo S7;          redo S7;
3507        }        }
3508    
3509          !!!cp ('t37');
3510      } # S7      } # S7
3511    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3512    
3513    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3514      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3515        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3516            !!!cp ('t38');
3517          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3518          return;          return;
3519        }        }
3520      }      }
3521    
3522        !!!cp ('t39');
3523    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3524    
3525    my $parse_rcdata = sub ($$) {    my $insert;
3526      my ($content_model_flag, $insert) = @_;  
3527      my $parse_rcdata = sub ($) {
3528        my ($content_model_flag) = @_;
3529    
3530      ## Step 1      ## Step 1
3531      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3532      my $el;      my $el;
3533      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3534    
3535      ## Step 2      ## Step 2
3536      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3537    
3538      ## Step 3      ## Step 3
3539      $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 3541  sub _tree_construction_main ($) {
3541    
3542      ## Step 4      ## Step 4
3543      my $text = '';      my $text = '';
3544        !!!nack ('t40.1');
3545      !!!next-token;      !!!next-token;
3546      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3547          !!!cp ('t40');
3548        $text .= $token->{data};        $text .= $token->{data};
3549        !!!next-token;        !!!next-token;
3550      }      }
3551    
3552      ## Step 5      ## Step 5
3553      if (length $text) {      if (length $text) {
3554          !!!cp ('t41');
3555        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3556        $el->append_child ($text);        $el->append_child ($text);
3557      }      }
# Line 2406  sub _tree_construction_main ($) { Line 3560  sub _tree_construction_main ($) {
3560      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3561    
3562      ## Step 7      ## Step 7
3563      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3564            $token->{tag_name} eq $start_tag_name) {
3565          !!!cp ('t42');
3566        ## 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});  
3567      } else {      } else {
3568        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3569          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3570            !!!cp ('t43');
3571            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3572          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3573            !!!cp ('t44');
3574            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3575          } else {
3576            die "$0: $content_model_flag in parse_rcdata";
3577          }
3578      }      }
3579      !!!next-token;      !!!next-token;
3580    }; # $parse_rcdata    }; # $parse_rcdata
3581    
3582    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3583      my $script_el;      my $script_el;
3584      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3585      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3586    
3587      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3588      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3589            
3590      my $text = '';      my $text = '';
3591        !!!nack ('t45.1');
3592      !!!next-token;      !!!next-token;
3593      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3594          !!!cp ('t45');
3595        $text .= $token->{data};        $text .= $token->{data};
3596        !!!next-token;        !!!next-token;
3597      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3598      if (length $text) {      if (length $text) {
3599          !!!cp ('t46');
3600        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3601      }      }
3602                                
# Line 2441  sub _tree_construction_main ($) { Line 3604  sub _tree_construction_main ($) {
3604    
3605      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3606          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3607          !!!cp ('t47');
3608        ## Ignore the token        ## Ignore the token
3609      } else {      } else {
3610        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3611          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3612        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3613        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3614      }      }
3615            
3616      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3617          !!!cp ('t49');
3618        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3619      } else {      } else {
3620          !!!cp ('t50');
3621        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3622        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3623    
# Line 2464  sub _tree_construction_main ($) { Line 3631  sub _tree_construction_main ($) {
3631      !!!next-token;      !!!next-token;
3632    }; # $script_start_tag    }; # $script_start_tag
3633    
3634      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3635      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3636      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3637    
3638    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3639      my $tag_name = shift;      my $end_tag_token = shift;
3640        my $tag_name = $end_tag_token->{tag_name};
3641    
3642        ## NOTE: The adoption agency algorithm (AAA).
3643    
3644      FET: {      FET: {
3645        ## Step 1        ## Step 1
3646        my $formatting_element;        my $formatting_element;
3647        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3648        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3649          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3650              !!!cp ('t52');
3651              last AFE;
3652            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3653                         eq $tag_name) {
3654              !!!cp ('t51');
3655            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3656            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3657            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3658          }          }
3659        } # AFE        } # AFE
3660        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3661          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3662            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3663          ## Ignore the token          ## Ignore the token
3664          !!!next-token;          !!!next-token;
3665          return;          return;
# Line 2493  sub _tree_construction_main ($) { Line 3671  sub _tree_construction_main ($) {
3671          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3672          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3673            if ($in_scope) {            if ($in_scope) {
3674                !!!cp ('t54');
3675              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3676              last INSCOPE;              last INSCOPE;
3677            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3678              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3679                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3680                                token => $end_tag_token);
3681              ## Ignore the token              ## Ignore the token
3682              !!!next-token;              !!!next-token;
3683              return;              return;
3684            }            }
3685          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3686                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3687            $in_scope = 0;            $in_scope = 0;
3688          }          }
3689        } # INSCOPE        } # INSCOPE
3690        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3691          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3692            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3693                            token => $end_tag_token);
3694          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3695          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3696          return;          return;
3697        }        }
3698        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3699          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3700            !!!parse-error (type => 'not closed',
3701                            value => $self->{open_elements}->[-1]->[0]
3702                                ->manakai_local_name,
3703                            token => $end_tag_token);
3704        }        }
3705                
3706        ## Step 2        ## Step 2
# Line 2523  sub _tree_construction_main ($) { Line 3708  sub _tree_construction_main ($) {
3708        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3709        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3710          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3711          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3712              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3713              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3714               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3715              !!!cp ('t59');
3716            $furthest_block = $node;            $furthest_block = $node;
3717            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3718          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3719              !!!cp ('t60');
3720            last OE;            last OE;
3721          }          }
3722        } # OE        } # OE
3723                
3724        ## Step 3        ## Step 3
3725        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3726            !!!cp ('t61');
3727          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3728          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3729          !!!next-token;          !!!next-token;
# Line 2548  sub _tree_construction_main ($) { Line 3736  sub _tree_construction_main ($) {
3736        ## Step 5        ## Step 5
3737        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3738        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3739            !!!cp ('t62');
3740          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3741        }        }
3742                
# Line 2570  sub _tree_construction_main ($) { Line 3759  sub _tree_construction_main ($) {
3759          S7S2: {          S7S2: {
3760            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3761              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3762                  !!!cp ('t63');
3763                $node_i_in_active = $_;                $node_i_in_active = $_;
3764                last S7S2;                last S7S2;
3765              }              }
# Line 2583  sub _tree_construction_main ($) { Line 3773  sub _tree_construction_main ($) {
3773                    
3774          ## Step 4          ## Step 4
3775          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3776              !!!cp ('t64');
3777            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3778          }          }
3779                    
3780          ## Step 5          ## Step 5
3781          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3782              !!!cp ('t65');
3783            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3784            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3785            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2605  sub _tree_construction_main ($) { Line 3797  sub _tree_construction_main ($) {
3797        } # S7          } # S7  
3798                
3799        ## Step 8        ## Step 8
3800        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3801            my $foster_parent_element;
3802            my $next_sibling;
3803            OE: for (reverse 0..$#{$self->{open_elements}}) {
3804              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3805                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3806                                 if (defined $parent and $parent->node_type == 1) {
3807                                   !!!cp ('t65.1');
3808                                   $foster_parent_element = $parent;
3809                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3810                                 } else {
3811                                   !!!cp ('t65.2');
3812                                   $foster_parent_element
3813                                     = $self->{open_elements}->[$_ - 1]->[0];
3814                                 }
3815                                 last OE;
3816                               }
3817                             } # OE
3818                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3819                               unless defined $foster_parent_element;
3820            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3821            $open_tables->[-1]->[1] = 1; # tainted
3822          } else {
3823            !!!cp ('t65.3');
3824            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3825          }
3826                
3827        ## Step 9        ## Step 9
3828        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2622  sub _tree_construction_main ($) { Line 3839  sub _tree_construction_main ($) {
3839        my $i;        my $i;
3840        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3841          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3842              !!!cp ('t66');
3843            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3844            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3845          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3846              !!!cp ('t67');
3847            $i = $_;            $i = $_;
3848          }          }
3849        } # AFE        } # AFE
# Line 2634  sub _tree_construction_main ($) { Line 3853  sub _tree_construction_main ($) {
3853        undef $i;        undef $i;
3854        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3855          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3856              !!!cp ('t68');
3857            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3858            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3859          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3860              !!!cp ('t69');
3861            $i = $_;            $i = $_;
3862          }          }
3863        } # OE        } # OE
# Line 2647  sub _tree_construction_main ($) { Line 3868  sub _tree_construction_main ($) {
3868      } # FET      } # FET
3869    }; # $formatting_end_tag    }; # $formatting_end_tag
3870    
3871    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3872      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3873    }; # $insert_to_current    }; # $insert_to_current
3874    
3875    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3876                         my $child = shift;      my $child = shift;
3877                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3878                              table => 1, tbody => 1, tfoot => 1,        # MUST
3879                              thead => 1, tr => 1,        my $foster_parent_element;
3880                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3881                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3882                           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') {  
3883                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3884                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3885                                   !!!cp ('t70');
3886                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3887                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3888                               } else {                               } else {
3889                                   !!!cp ('t71');
3890                                 $foster_parent_element                                 $foster_parent_element
3891                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3892                               }                               }
# Line 2677  sub _tree_construction_main ($) { Line 3897  sub _tree_construction_main ($) {
3897                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3898                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3899                             ($child, $next_sibling);                             ($child, $next_sibling);
3900                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3901                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3902                         }        !!!cp ('t72');
3903          $self->{open_elements}->[-1]->[0]->append_child ($child);
3904        }
3905    }; # $insert_to_foster    }; # $insert_to_foster
3906    
3907    my $insert;    B: while (1) {
   
   B: {  
3908      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3909        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
3910          !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3911        ## Ignore the token        ## Ignore the token
3912        ## Stay in the phase        ## Stay in the phase
3913        !!!next-token;        !!!next-token;
3914        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;  
3915      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3916               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3917        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3918          ## Turn into the main phase          !!!cp ('t79');
3919          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3920          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3921        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3922          ## Turn into the main phase          !!!cp ('t80');
3923          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3924          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3925          } else {
3926            !!!cp ('t81');
3927        }        }
3928    
3929  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3930  ## 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');  
       }  
3931        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3932        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3933          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3934              !!!cp ('t84');
3935            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3936              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3937               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3938          }          }
3939        }        }
3940          !!!nack ('t84.1');
3941        !!!next-token;        !!!next-token;
3942        redo B;        next B;
3943      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3944        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3945        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3946            !!!cp ('t85');
3947          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3948        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3949            !!!cp ('t86');
3950          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3951        } else {        } else {
3952            !!!cp ('t87');
3953          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3954        }        }
3955        !!!next-token;        !!!next-token;
3956        redo B;        next B;
3957      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3958          if ($token->{type} == CHARACTER_TOKEN) {
3959            !!!cp ('t87.1');
3960            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3961            !!!next-token;
3962            next B;
3963          } elsif ($token->{type} == START_TAG_TOKEN) {
3964            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3965                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3966                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3967                ($token->{tag_name} eq 'svg' and
3968                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3969              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3970              !!!cp ('t87.2');
3971              #
3972            } elsif ({
3973                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3974                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3975                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3976                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3977                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3978                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3979                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3980                      var => 1,
3981                     }->{$token->{tag_name}}) {
3982              !!!cp ('t87.2');
3983              !!!parse-error (type => 'not closed',
3984                              value => $self->{open_elements}->[-1]->[0]
3985                                  ->manakai_local_name,
3986                              token => $token);
3987    
3988              pop @{$self->{open_elements}}
3989                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3990    
3991              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3992              ## Reprocess.
3993              next B;
3994            } else {
3995              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3996              my $tag_name = $token->{tag_name};
3997              if ($nsuri eq $SVG_NS) {
3998                $tag_name = {
3999                   altglyph => 'altGlyph',
4000                   altglyphdef => 'altGlyphDef',
4001                   altglyphitem => 'altGlyphItem',
4002                   animatecolor => 'animateColor',
4003                   animatemotion => 'animateMotion',
4004                   animatetransform => 'animateTransform',
4005                   clippath => 'clipPath',
4006                   feblend => 'feBlend',
4007                   fecolormatrix => 'feColorMatrix',
4008                   fecomponenttransfer => 'feComponentTransfer',
4009                   fecomposite => 'feComposite',
4010                   feconvolvematrix => 'feConvolveMatrix',
4011                   fediffuselighting => 'feDiffuseLighting',
4012                   fedisplacementmap => 'feDisplacementMap',
4013                   fedistantlight => 'feDistantLight',
4014                   feflood => 'feFlood',
4015                   fefunca => 'feFuncA',
4016                   fefuncb => 'feFuncB',
4017                   fefuncg => 'feFuncG',
4018                   fefuncr => 'feFuncR',
4019                   fegaussianblur => 'feGaussianBlur',
4020                   feimage => 'feImage',
4021                   femerge => 'feMerge',
4022                   femergenode => 'feMergeNode',
4023                   femorphology => 'feMorphology',
4024                   feoffset => 'feOffset',
4025                   fepointlight => 'fePointLight',
4026                   fespecularlighting => 'feSpecularLighting',
4027                   fespotlight => 'feSpotLight',
4028                   fetile => 'feTile',
4029                   feturbulence => 'feTurbulence',
4030                   foreignobject => 'foreignObject',
4031                   glyphref => 'glyphRef',
4032                   lineargradient => 'linearGradient',
4033                   radialgradient => 'radialGradient',
4034                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4035                   textpath => 'textPath',  
4036                }->{$tag_name} || $tag_name;
4037              }
4038    
4039              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4040    
4041              ## "adjust foreign attributes" - done in insert-element-f
4042    
4043              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4044    
4045              if ($self->{self_closing}) {
4046                pop @{$self->{open_elements}};
4047                !!!ack ('t87.3');
4048              } else {
4049                !!!cp ('t87.4');
4050              }
4051    
4052              !!!next-token;
4053              next B;
4054            }
4055          } elsif ($token->{type} == END_TAG_TOKEN) {
4056            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4057            !!!cp ('t87.5');
4058            #
4059          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4060            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4061            !!!cp ('t87.6');
4062            #
4063            ## TODO: ...
4064          } else {
4065            die "$0: $token->{type}: Unknown token type";        
4066          }
4067        }
4068    
4069        if ($self->{insertion_mode} & HEAD_IMS) {
4070        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4071          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4072            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4073                !!!cp ('t88.2');
4074                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4075              } else {
4076                !!!cp ('t88.1');
4077                ## Ignore the token.
4078                !!!next-token;
4079                next B;
4080              }
4081            unless (length $token->{data}) {            unless (length $token->{data}) {
4082                !!!cp ('t88');
4083              !!!next-token;              !!!next-token;
4084              redo B;              next B;
4085            }            }
4086          }          }
4087    
4088          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4089              !!!cp ('t89');
4090            ## As if <head>            ## As if <head>
4091            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4092            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4093            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4094                  [$self->{head_element}, $el_category->{head}];
4095    
4096            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4097            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4098    
4099            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4100          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4101              !!!cp ('t90');
4102            ## As if </noscript>            ## As if </noscript>
4103            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4104            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4105                        
4106            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4107            ## As if </head>            ## As if </head>
# Line 2788  sub _tree_construction_main ($) { Line 4109  sub _tree_construction_main ($) {
4109    
4110            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4111          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4112              !!!cp ('t91');
4113            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4114    
4115            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4116            } else {
4117              !!!cp ('t92');
4118          }          }
4119    
4120              ## "after head" insertion mode          ## "after head" insertion mode
4121              ## As if <body>          ## As if <body>
4122              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4123              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4124              ## reprocess          ## reprocess
4125              redo B;          next B;
4126            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4127              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4128                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4129                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4130                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4131                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4132                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4133                  !!!next-token;              push @{$self->{open_elements}},
4134                  redo B;                  [$self->{head_element}, $el_category->{head}];
4135                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4136                  #              !!!nack ('t93.1');
4137                } else {              !!!next-token;
4138                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4139                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4140                  !!!next-token;              !!!cp ('t94');
4141                  redo B;              #
4142                }            } else {
4143              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!cp ('t95');
4144                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4145                !!!create-element ($self->{head_element}, 'head');              ## Ignore the token
4146                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
4147                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4148                next B;
4149              }
4150            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4151              !!!cp ('t96');
4152              ## As if <head>
4153              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4154              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4155              push @{$self->{open_elements}},
4156                  [$self->{head_element}, $el_category->{head}];
4157    
4158                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4159                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4160              }          } else {
4161              !!!cp ('t97');
4162            }
4163    
4164              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4165                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4166                    !!!cp ('t98');
4167                  ## As if </noscript>                  ## As if </noscript>
4168                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4169                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4170                                
4171                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4172                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4173                  } else {
4174                    !!!cp ('t99');
4175                }                }
4176    
4177                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4178                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4179                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4180                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4181                    push @{$self->{open_elements}},
4182                        [$self->{head_element}, $el_category->{head}];
4183                  } else {
4184                    !!!cp ('t101');
4185                }                }
4186                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4187                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4188                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4189                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4190                  !!!nack ('t101.1');
4191                !!!next-token;                !!!next-token;
4192                redo B;                next B;
4193              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4194                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4195                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4196                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4197                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4198                    push @{$self->{open_elements}},
4199                        [$self->{head_element}, $el_category->{head}];
4200                  } else {
4201                    !!!cp ('t103');
4202                }                }
4203                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4204                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4205                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4206                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4207                  !!!ack ('t103.1');
4208                !!!next-token;                !!!next-token;
4209                redo B;                next B;
4210              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4211                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4212                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4213                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4214                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4215                    push @{$self->{open_elements}},
4216                        [$self->{head_element}, $el_category->{head}];
4217                  } else {
4218                    !!!cp ('t105');
4219                }                }
4220                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4221                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.
4222    
4223                unless ($self->{confident}) {                unless ($self->{confident}) {
4224                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4225                      !!!cp ('t106');
4226                      ## NOTE: Whether the encoding is supported or not is handled
4227                      ## in the {change_encoding} callback.
4228                    $self->{change_encoding}                    $self->{change_encoding}
4229                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4230                             $token);
4231                      
4232                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4233                          ->set_user_data (manakai_has_reference =>
4234                                               $token->{attributes}->{charset}
4235                                                   ->{has_reference});
4236                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4237                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4238                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4239                              [\x09-\x0D\x20]*=
4240                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4241                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4242                        !!!cp ('t107');
4243                        ## NOTE: Whether the encoding is supported or not is handled
4244                        ## in the {change_encoding} callback.
4245                      $self->{change_encoding}                      $self->{change_encoding}
4246                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4247                               $token);
4248                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4249                            ->set_user_data (manakai_has_reference =>
4250                                                 $token->{attributes}->{content}
4251                                                       ->{has_reference});
4252                      } else {
4253                        !!!cp ('t108');
4254                    }                    }
4255                  }                  }
4256                  } else {
4257                    if ($token->{attributes}->{charset}) {
4258                      !!!cp ('t109');
4259                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4260                          ->set_user_data (manakai_has_reference =>
4261                                               $token->{attributes}->{charset}
4262                                                   ->{has_reference});
4263                    }
4264                    if ($token->{attributes}->{content}) {
4265                      !!!cp ('t110');
4266                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4267                          ->set_user_data (manakai_has_reference =>
4268                                               $token->{attributes}->{content}
4269                                                   ->{has_reference});
4270                    }
4271                }                }
4272    
4273                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4274                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4275                  !!!ack ('t110.1');
4276                !!!next-token;                !!!next-token;
4277                redo B;                next B;
4278              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4279                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4280                    !!!cp ('t111');
4281                  ## As if </noscript>                  ## As if </noscript>
4282                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4283                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4284                                
4285                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4286                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4287                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4288                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4289                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4290                    push @{$self->{open_elements}},
4291                        [$self->{head_element}, $el_category->{head}];
4292                  } else {
4293                    !!!cp ('t113');
4294                }                }
4295    
4296                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4297                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4298                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4299                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4300                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4301                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4302                redo B;                next B;
4303              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4304                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4305                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4306                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4307                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4308                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4309                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4310                    push @{$self->{open_elements}},
4311                        [$self->{head_element}, $el_category->{head}];
4312                  } else {
4313                    !!!cp ('t115');
4314                }                }
4315                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4316                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4317                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4318                redo B;                next B;
4319              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4320                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4321                    !!!cp ('t116');
4322                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4323                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4324                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4325                    !!!nack ('t116.1');
4326                  !!!next-token;                  !!!next-token;
4327                  redo B;                  next B;
4328                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4329                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4330                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4331                  ## Ignore the token                  ## Ignore the token
4332                    !!!nack ('t117.1');
4333                  !!!next-token;                  !!!next-token;
4334                  redo B;                  next B;
4335                } else {                } else {
4336                    !!!cp ('t118');
4337                  #                  #
4338                }                }
4339              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4340                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4341                    !!!cp ('t119');
4342                  ## As if </noscript>                  ## As if </noscript>
4343                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4344                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4345                                
4346                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4347                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4348                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4349                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4350                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4351                    push @{$self->{open_elements}},
4352                        [$self->{head_element}, $el_category->{head}];
4353                  } else {
4354                    !!!cp ('t121');
4355                }                }
4356    
4357                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4358                $script_start_tag->($insert_to_current);                $script_start_tag->();
4359                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4360                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4361                redo B;                next B;
4362              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4363                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4364                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4365                    !!!cp ('t122');
4366                  ## As if </noscript>                  ## As if </noscript>
4367                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4368                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4369                                    
4370                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4371                  ## As if </head>                  ## As if </head>
# Line 2967  sub _tree_construction_main ($) { Line 4373  sub _tree_construction_main ($) {
4373                                    
4374                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4375                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4376                    !!!cp ('t124');
4377                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4378                                    
4379                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4380                  } else {
4381                    !!!cp ('t125');
4382                }                }
4383    
4384                ## "after head" insertion mode                ## "after head" insertion mode
4385                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4386                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4387                    !!!cp ('t126');
4388                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4389                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4390                    !!!cp ('t127');
4391                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4392                } else {                } else {
4393                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4394                }                }
4395                  !!!nack ('t127.1');
4396                !!!next-token;                !!!next-token;
4397                redo B;                next B;
4398              } else {              } else {
4399                  !!!cp ('t128');
4400                #                #
4401              }              }
4402    
4403              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4404                  !!!cp ('t129');
4405                ## As if </noscript>                ## As if </noscript>
4406                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4407                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4408                                
4409                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4410                ## As if </head>                ## As if </head>
# Line 2998  sub _tree_construction_main ($) { Line 4412  sub _tree_construction_main ($) {
4412    
4413                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4414              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4415                  !!!cp ('t130');
4416                ## As if </head>                ## As if </head>
4417                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4418    
4419                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4420                } else {
4421                  !!!cp ('t131');
4422              }              }
4423    
4424              ## "after head" insertion mode              ## "after head" insertion mode
4425              ## As if <body>              ## As if <body>
4426              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4427              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4428              ## reprocess              ## reprocess
4429              redo B;              !!!ack-later;
4430                next B;
4431            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4432              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4433                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4434                    !!!cp ('t132');
4435                  ## As if <head>                  ## As if <head>
4436                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4437                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4438                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4439                        [$self->{head_element}, $el_category->{head}];
4440    
4441                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4442                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4443                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4444                  !!!next-token;                  !!!next-token;
4445                  redo B;                  next B;
4446                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4447                    !!!cp ('t133');
4448                  ## As if </noscript>                  ## As if </noscript>
4449                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4450                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4451                                    
4452                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4453                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4454                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4455                  !!!next-token;                  !!!next-token;
4456                  redo B;                  next B;
4457                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4458                    !!!cp ('t134');
4459                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4460                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4461                  !!!next-token;                  !!!next-token;
4462                  redo B;                  next B;
4463                } else {                } else {
4464                    !!!cp ('t135');
4465                  #                  #
4466                }                }
4467              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4468                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4469                    !!!cp ('t136');
4470                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4471                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4472                  !!!next-token;                  !!!next-token;
4473                  redo B;                  next B;
4474                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4475                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!cp ('t137');
4476                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4477                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4478                  !!!next-token;                  !!!next-token;
4479                  redo B;                  next B;
4480                } else {                } else {
4481                    !!!cp ('t138');
4482                  #                  #
4483                }                }
4484              } elsif ({              } elsif ({
4485                        body => 1, html => 1,                        body => 1, html => 1,
4486                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4487                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4488                    !!!cp ('t139');
4489                  ## As if <head>                  ## As if <head>
4490                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4491                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4492                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4493                        [$self->{head_element}, $el_category->{head}];
4494    
4495                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4496                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4497                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4498                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t140');
4499                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4500                  ## Ignore the token                  ## Ignore the token
4501                  !!!next-token;                  !!!next-token;
4502                  redo B;                  next B;
4503                  } else {
4504                    !!!cp ('t141');
4505                }                }
4506                                
4507                #                #
# Line 3078  sub _tree_construction_main ($) { Line 4509  sub _tree_construction_main ($) {
4509                        p => 1, br => 1,                        p => 1, br => 1,
4510                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4511                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4512                    !!!cp ('t142');
4513                  ## As if <head>                  ## As if <head>
4514                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4515                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4516                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4517                        [$self->{head_element}, $el_category->{head}];
4518    
4519                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4520                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4521                  } else {
4522                    !!!cp ('t143');
4523                }                }
4524    
4525                #                #
4526              } else {              } else {
4527                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4528                    !!!cp ('t144');
4529                  #                  #
4530                } else {                } else {
4531                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t145');
4532                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4533                  ## Ignore the token                  ## Ignore the token
4534                  !!!next-token;                  !!!next-token;
4535                  redo B;                  next B;
4536                }                }
4537              }              }
4538    
4539              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4540                  !!!cp ('t146');
4541                ## As if </noscript>                ## As if </noscript>
4542                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4543                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4544                                
4545                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4546                ## As if </head>                ## As if </head>
# Line 3110  sub _tree_construction_main ($) { Line 4548  sub _tree_construction_main ($) {
4548    
4549                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4550              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4551                  !!!cp ('t147');
4552                ## As if </head>                ## As if </head>
4553                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4554    
4555                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4556              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4557                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4558                  !!!cp ('t148');
4559                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4560                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4561                !!!next-token;                !!!next-token;
4562                redo B;                next B;
4563                } else {
4564                  !!!cp ('t149');
4565              }              }
4566    
4567              ## "after head" insertion mode              ## "after head" insertion mode
4568              ## As if <body>              ## As if <body>
4569              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4570              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4571              ## reprocess              ## reprocess
4572              redo B;              next B;
4573            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4574              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4575            }            !!!cp ('t149.1');
4576    
4577              ## NOTE: As if <head>
4578              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4579              $self->{open_elements}->[-1]->[0]->append_child
4580                  ($self->{head_element});
4581              #push @{$self->{open_elements}},
4582              #    [$self->{head_element}, $el_category->{head}];
4583              #$self->{insertion_mode} = IN_HEAD_IM;
4584              ## NOTE: Reprocess.
4585    
4586              ## NOTE: As if </head>
4587              #pop @{$self->{open_elements}};
4588              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4589              ## NOTE: Reprocess.
4590              
4591              #
4592            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4593              !!!cp ('t149.2');
4594    
4595              ## NOTE: As if </head>
4596              pop @{$self->{open_elements}};
4597              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4598              ## NOTE: Reprocess.
4599    
4600              #
4601            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4602              !!!cp ('t149.3');
4603    
4604              !!!parse-error (type => 'in noscript:#eof', token => $token);
4605    
4606              ## As if </noscript>
4607              pop @{$self->{open_elements}};
4608              #$self->{insertion_mode} = IN_HEAD_IM;
4609              ## NOTE: Reprocess.
4610    
4611              ## NOTE: As if </head>
4612              pop @{$self->{open_elements}};
4613              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4614              ## NOTE: Reprocess.
4615    
4616              #
4617            } else {
4618              !!!cp ('t149.4');
4619              #
4620            }
4621    
4622            ## NOTE: As if <body>
4623            !!!insert-element ('body',, $token);
4624            $self->{insertion_mode} = IN_BODY_IM;
4625            ## NOTE: Reprocess.
4626            next B;
4627          } else {
4628            die "$0: $token->{type}: Unknown token type";
4629          }
4630    
4631            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4632      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
4633            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4634                !!!cp ('t150');
4635              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4636              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4637                            
4638              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4639    
4640              !!!next-token;              !!!next-token;
4641              redo B;              next B;
4642            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4643              if ({              if ({
4644                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3148  sub _tree_construction_main ($) { Line 4646  sub _tree_construction_main ($) {
4646                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4647                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4648                  ## have an element in table scope                  ## have an element in table scope
4649                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4650                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4651                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4652                      $tn = $node->[1];                      !!!cp ('t151');
4653                      last INSCOPE;  
4654                    } elsif ({                      ## Close the cell
4655                              table => 1, html => 1,                      !!!back-token; # <x>
4656                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4657                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4658                    }                                line => $token->{line},
4659                  } # INSCOPE                                column => $token->{column}};
4660                    unless (defined $tn) {                      next B;
4661                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4662                      ## Ignore the token                      !!!cp ('t152');
4663                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4664                      redo B;                      last;
4665                    }                    }
4666                                    }
4667                  ## Close the cell  
4668                  !!!back-token; # <?>                  !!!cp ('t153');
4669                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
4670                  redo B;                      value => $token->{tag_name}, token => $token);
4671                    ## Ignore the token
4672                    !!!nack ('t153.1');
4673                    !!!next-token;
4674                    next B;
4675                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4676                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4677                                    
4678                  ## As if </caption>                  ## NOTE: As if </caption>.
4679                  ## have a table element in table scope                  ## have a table element in table scope
4680                  my $i;                  my $i;
4681                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4682                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4683                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4684                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4685                      last INSCOPE;                        !!!cp ('t155');
4686                    } elsif ({                        $i = $_;
4687                              table => 1, html => 1,                        last INSCOPE;
4688                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4689                      last INSCOPE;                        !!!cp ('t156');
4690                          last;
4691                        }
4692                    }                    }
4693    
4694                      !!!cp ('t157');
4695                      !!!parse-error (type => 'start tag not allowed',
4696                                      value => $token->{tag_name}, token => $token);
4697                      ## Ignore the token
4698                      !!!nack ('t157.1');
4699                      !!!next-token;
4700                      next B;
4701                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4702                                    
4703                  ## generate implied end tags                  ## generate implied end tags
4704                  if ({                  while ($self->{open_elements}->[-1]->[1]
4705                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4706                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4707                       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;  
4708                  }                  }
4709    
4710                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4711                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4712                      !!!parse-error (type => 'not closed',
4713                                      value => $self->{open_elements}->[-1]->[0]
4714                                          ->manakai_local_name,
4715                                      token => $token);
4716                    } else {
4717                      !!!cp ('t160');
4718                  }                  }
4719                                    
4720                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3220  sub _tree_construction_main ($) { Line 4724  sub _tree_construction_main ($) {
4724                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4725                                    
4726                  ## reprocess                  ## reprocess
4727                  redo B;                  !!!ack-later;
4728                    next B;
4729                } else {                } else {
4730                    !!!cp ('t161');
4731                  #                  #
4732                }                }
4733              } else {              } else {
4734                  !!!cp ('t162');
4735                #                #
4736              }              }
4737            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3234  sub _tree_construction_main ($) { Line 4741  sub _tree_construction_main ($) {
4741                  my $i;                  my $i;
4742                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4743                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4744                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4745                        !!!cp ('t163');
4746                      $i = $_;                      $i = $_;
4747                      last INSCOPE;                      last INSCOPE;
4748                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4749                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4750                      last INSCOPE;                      last INSCOPE;
4751                    }                    }
4752                  } # INSCOPE                  } # INSCOPE
4753                    unless (defined $i) {                    unless (defined $i) {
4754                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4755                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4756                      ## Ignore the token                      ## Ignore the token
4757                      !!!next-token;                      !!!next-token;
4758                      redo B;                      next B;
4759                    }                    }
4760                                    
4761                  ## generate implied end tags                  ## generate implied end tags
4762                  if ({                  while ($self->{open_elements}->[-1]->[1]
4763                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4764                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4765                       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;  
4766                  }                  }
4767                    
4768                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4769                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4770                      !!!cp ('t167');
4771                      !!!parse-error (type => 'not closed',
4772                                      value => $self->{open_elements}->[-1]->[0]
4773                                          ->manakai_local_name,
4774                                      token => $token);
4775                    } else {
4776                      !!!cp ('t168');
4777                  }                  }
4778                                    
4779                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3275  sub _tree_construction_main ($) { Line 4783  sub _tree_construction_main ($) {
4783                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4784                                    
4785                  !!!next-token;                  !!!next-token;
4786                  redo B;                  next B;
4787                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4788                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4789                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4790                  ## Ignore the token                  ## Ignore the token
4791                  !!!next-token;                  !!!next-token;
4792                  redo B;                  next B;
4793                } else {                } else {
4794                    !!!cp ('t170');
4795                  #                  #
4796                }                }
4797              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4798                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4799                  ## have a table element in table scope                  ## have a table element in table scope
4800                  my $i;                  my $i;
4801                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4802                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4803                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4804                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4805                      last INSCOPE;                        !!!cp ('t171');
4806                    } elsif ({                        $i = $_;
4807                              table => 1, html => 1,                        last INSCOPE;
4808                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4809                      last INSCOPE;                        !!!cp ('t172');
4810                          last;
4811                        }
4812                    }                    }
4813    
4814                      !!!cp ('t173');
4815                      !!!parse-error (type => 'unmatched end tag',
4816                                      value => $token->{tag_name}, token => $token);
4817                      ## Ignore the token
4818                      !!!next-token;
4819                      next B;
4820                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4821                                    
4822                  ## generate implied end tags                  ## generate implied end tags
4823                  if ({                  while ($self->{open_elements}->[-1]->[1]
4824                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4825                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
4826                       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;  
4827                  }                  }
4828                                    
4829                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4830                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4831                      !!!parse-error (type => 'not closed',
4832                                      value => $self->{open_elements}->[-1]->[0]
4833                                          ->manakai_local_name,
4834                                      token => $token);
4835                    } else {
4836                      !!!cp ('t176');
4837                  }                  }
4838                                    
4839                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3329  sub _tree_construction_main ($) { Line 4843  sub _tree_construction_main ($) {
4843                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4844                                    
4845                  !!!next-token;                  !!!next-token;
4846                  redo B;                  next B;
4847                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4848                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4849                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4850                  ## Ignore the token                  ## Ignore the token
4851                  !!!next-token;                  !!!next-token;
4852                  redo B;                  next B;
4853                } else {                } else {
4854                    !!!cp ('t178');
4855                  #                  #
4856                }                }
4857              } elsif ({              } elsif ({
# Line 3346  sub _tree_construction_main ($) { Line 4862  sub _tree_construction_main ($) {
4862                ## have an element in table scope                ## have an element in table scope
4863                my $i;                my $i;
4864                my $tn;                my $tn;
4865                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4866                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4867                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4868                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4869                    last INSCOPE;                      !!!cp ('t179');
4870                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4871                    $tn = $node->[1];  
4872                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4873                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
4874                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4875                            table => 1, html => 1,                                line => $token->{line},
4876                           }->{$node->[1]}) {                                column => $token->{column}};
4877                    last INSCOPE;                      next B;
4878                      } elsif ($node->[1] & TABLE_CELL_EL) {
4879                        !!!cp ('t180');
4880                        $tn = $node->[0]->manakai_local_name;
4881                        ## NOTE: There is exactly one |td| or |th| element
4882                        ## in scope in the stack of open elements by definition.
4883                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4884                        ## ISSUE: Can this be reached?
4885                        !!!cp ('t181');
4886                        last;
4887                      }
4888                  }                  }
4889                } # INSCOPE  
4890                unless (defined $i) {                  !!!cp ('t182');
4891                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4892                        value => $token->{tag_name}, token => $token);
4893                  ## Ignore the token                  ## Ignore the token
4894                  !!!next-token;                  !!!next-token;
4895                  redo B;                  next B;
4896                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4897              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4898                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4899                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4900    
4901                ## As if </caption>                ## As if </caption>
4902                ## have a table element in table scope                ## have a table element in table scope
4903                my $i;                my $i;
4904                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4905                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4906                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4907                      !!!cp ('t184');
4908                    $i = $_;                    $i = $_;
4909                    last INSCOPE;                    last INSCOPE;
4910                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4911                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4912                    last INSCOPE;                    last INSCOPE;
4913                  }                  }
4914                } # INSCOPE                } # INSCOPE
4915                unless (defined $i) {                unless (defined $i) {
4916                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4917                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4918                  ## Ignore the token                  ## Ignore the token
4919                  !!!next-token;                  !!!next-token;
4920                  redo B;                  next B;
4921                }                }
4922                                
4923                ## generate implied end tags                ## generate implied end tags
4924                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4925                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4926                     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;  
4927                }                }
4928    
4929                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4930                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4931                    !!!parse-error (type => 'not closed',
4932                                    value => $self->{open_elements}->[-1]->[0]
4933                                        ->manakai_local_name,
4934                                    token => $token);
4935                  } else {
4936                    !!!cp ('t189');
4937                }                }
4938    
4939                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3422  sub _tree_construction_main ($) { Line 4943  sub _tree_construction_main ($) {
4943                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4944    
4945                ## reprocess                ## reprocess
4946                redo B;                next B;
4947              } elsif ({              } elsif ({
4948                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4949                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4950                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4951                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
4952                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4953                  ## Ignore the token                  ## Ignore the token
4954                  !!!next-token;                  !!!next-token;
4955                  redo B;                  next B;
4956                } else {                } else {
4957                    !!!cp ('t191');
4958                  #                  #
4959                }                }
4960              } elsif ({              } elsif ({
# Line 3439  sub _tree_construction_main ($) { Line 4962  sub _tree_construction_main ($) {
4962                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4963                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4964                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4965                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
4966                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4967                ## Ignore the token                ## Ignore the token
4968                !!!next-token;                !!!next-token;
4969                redo B;                next B;
4970              } else {              } else {
4971                  !!!cp ('t193');
4972                #                #
4973              }              }
4974          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4975            for my $entry (@{$self->{open_elements}}) {
4976              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4977                !!!cp ('t75');
4978                !!!parse-error (type => 'in body:#eof', token => $token);
4979                last;
4980              }
4981            }
4982    
4983            ## Stop parsing.
4984            last B;
4985        } else {        } else {
4986          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4987        }        }
# Line 3454  sub _tree_construction_main ($) { Line 4990  sub _tree_construction_main ($) {
4990        #        #
4991      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4992        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4993              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4994                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4995              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4996                                
4997                unless (length $token->{data}) {            unless (length $token->{data}) {
4998                  !!!next-token;              !!!cp ('t194');
4999                  redo B;              !!!next-token;
5000                }              next B;
5001              }            } else {
5002                !!!cp ('t195');
5003              }
5004            }
5005    
5006              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5007    
5008              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5009              ## 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 5011  sub _tree_construction_main ($) {
5011              ## result in a new Text node.              ## result in a new Text node.
5012              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5013                            
5014              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]}) {  
5015                # MUST                # MUST
5016                my $foster_parent_element;                my $foster_parent_element;
5017                my $next_sibling;                my $next_sibling;
5018                my $prev_sibling;                my $prev_sibling;
5019                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5020                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5021                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5022                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5023                        !!!cp ('t196');
5024                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5025                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5026                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5027                    } else {                    } else {
5028                        !!!cp ('t197');
5029                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5030                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5031                    }                    }
# Line 3498  sub _tree_construction_main ($) { Line 5037  sub _tree_construction_main ($) {
5037                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5038                if (defined $prev_sibling and                if (defined $prev_sibling and
5039                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5040                    !!!cp ('t198');
5041                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5042                } else {                } else {
5043                    !!!cp ('t199');
5044                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5045                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5046                     $next_sibling);                     $next_sibling);
5047                }                }
5048              } else {            $open_tables->[-1]->[1] = 1; # tainted
5049                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5050              }            !!!cp ('t200');
5051              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5052            }
5053                            
5054              !!!next-token;          !!!next-token;
5055              redo B;          next B;
5056        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5057              if ({              if ({
5058                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 3517  sub _tree_construction_main ($) { Line 5060  sub _tree_construction_main ($) {
5060                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5061                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5062                  ## Clear back to table context                  ## Clear back to table context
5063                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5064                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5065                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
5066                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5067                  }                  }
5068                                    
5069                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5070                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5071                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5072                }                }
5073    
5074                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5075                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
5076                    !!!parse-error (type => 'missing start tag:tr');                    !!!cp ('t202');
5077                      !!!parse-error (type => 'missing start tag:tr', token => $token);
5078                  }                  }
5079                                    
5080                  ## Clear back to table body context                  ## Clear back to table body context
5081                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5082                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5083                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t203');
5084                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5085                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5086                  }                  }
5087                                    
5088                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5089                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5090                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5091                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5092                      !!!nack ('t204');
5093                    !!!next-token;                    !!!next-token;
5094                    redo B;                    next B;
5095                  } else {                  } else {
5096                    !!!insert-element ('tr');                    !!!cp ('t205');
5097                      !!!insert-element ('tr',, $token);
5098                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5099                  }                  }
5100                  } else {
5101                    !!!cp ('t206');
5102                }                }
5103    
5104                ## Clear back to table row context                ## Clear back to table row context
5105                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5106                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5107                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5108                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5109                }                }
5110                                
5111                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5112                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5113    
5114                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5115                                
5116                  !!!nack ('t207.1');
5117                !!!next-token;                !!!next-token;
5118                redo B;                next B;
5119              } elsif ({              } elsif ({
5120                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5121                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 3578  sub _tree_construction_main ($) { Line 5127  sub _tree_construction_main ($) {
5127                  my $i;                  my $i;
5128                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5129                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5130                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5131                        !!!cp ('t208');
5132                      $i = $_;                      $i = $_;
5133                      last INSCOPE;                      last INSCOPE;
5134                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5135                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5136                      last INSCOPE;                      last INSCOPE;
5137                    }                    }
5138                  } # INSCOPE                  } # INSCOPE
5139                  unless (defined $i) {                  unless (defined $i) {
5140                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5141    ## TODO: This type is wrong.
5142                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5143                    ## Ignore the token                    ## Ignore the token
5144                      !!!nack ('t210.1');
5145                    !!!next-token;                    !!!next-token;
5146                    redo B;                    next B;
5147                  }                  }
5148                                    
5149                  ## Clear back to table row context                  ## Clear back to table row context
5150                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5151                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5152                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5153                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5154                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5155                  }                  }
5156                                    
5157                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5158                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5159                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5160                      !!!cp ('t212');
5161                    ## reprocess                    ## reprocess
5162                    redo B;                    !!!ack-later;
5163                      next B;
5164                  } else {                  } else {
5165                      !!!cp ('t213');
5166                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5167                  }                  }
5168                }                }
# Line 3617  sub _tree_construction_main ($) { Line 5172  sub _tree_construction_main ($) {
5172                  my $i;                  my $i;
5173                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5174                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5175                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5176                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5177                      $i = $_;                      $i = $_;
5178                      last INSCOPE;                      last INSCOPE;
5179                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5180                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5181                      last INSCOPE;                      last INSCOPE;
5182                    }                    }
5183                  } # INSCOPE                  } # INSCOPE
5184                  unless (defined $i) {                  unless (defined $i) {
5185                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5186    ## TODO: This erorr type ios wrong.
5187                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5188                    ## Ignore the token                    ## Ignore the token
5189                      !!!nack ('t216.1');
5190                    !!!next-token;                    !!!next-token;
5191                    redo B;                    next B;
5192                  }                  }
5193    
5194                  ## Clear back to table body context                  ## Clear back to table body context
5195                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5196                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5197                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5198                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5199                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5200                  }                  }
5201                                    
# Line 3653  sub _tree_construction_main ($) { Line 5209  sub _tree_construction_main ($) {
5209                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5210                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5211                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5212                  } else {
5213                    !!!cp ('t218');
5214                }                }
5215    
5216                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5217                  ## Clear back to table context                  ## Clear back to table context
5218                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5219                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5220                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5221                      ## ISSUE: Can this state be reached?
5222                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5223                  }                  }
5224                                    
5225                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5226                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5227                  ## reprocess                  ## reprocess
5228                  redo B;                  !!!ack-later;
5229                    next B;
5230                } elsif ({                } elsif ({
5231                          caption => 1,                          caption => 1,
5232                          colgroup => 1,                          colgroup => 1,
5233                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5234                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5235                  ## Clear back to table context                  ## Clear back to table context
5236                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5237                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5238                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5239                      ## ISSUE: Can this state be reached?
5240                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5241                  }                  }
5242                                    
5243                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5244                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5245                                    
5246                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5247                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5248                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5249                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3691  sub _tree_construction_main ($) { Line 5252  sub _tree_construction_main ($) {
5252                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5253                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5254                  !!!next-token;                  !!!next-token;
5255                  redo B;                  !!!nack ('t220.1');
5256                    next B;
5257                } else {                } else {
5258                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5259                }                }
5260              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5261                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5262                                  value => $self->{open_elements}->[-1]->[0]
5263                                      ->manakai_local_name,
5264                                  token => $token);
5265    
5266                ## As if </table>                ## As if </table>
5267                ## have a table element in table scope                ## have a table element in table scope
5268                my $i;                my $i;
5269                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5270                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5271                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5272                      !!!cp ('t221');
5273                    $i = $_;                    $i = $_;
5274                    last INSCOPE;                    last INSCOPE;
5275                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5276                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5277                    last INSCOPE;                    last INSCOPE;
5278                  }                  }
5279                } # INSCOPE                } # INSCOPE
5280                unless (defined $i) {                unless (defined $i) {
5281                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5282    ## TODO: The following is wrong, maybe.
5283                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5284                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5285                    !!!nack ('t223.1');
5286                  !!!next-token;                  !!!next-token;
5287                  redo B;                  next B;
5288                }                }
5289                                
5290    ## TODO: Followings are removed from the latest spec.
5291                ## generate implied end tags                ## generate implied end tags
5292                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5293                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5294                     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;  
5295                }                }
5296    
5297                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5298                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5299                    ## NOTE: |<table><tr><table>|
5300                    !!!parse-error (type => 'not closed',
5301                                    value => $self->{open_elements}->[-1]->[0]
5302                                        ->manakai_local_name,
5303                                    token => $token);
5304                  } else {
5305                    !!!cp ('t226');
5306                }                }
5307    
5308                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5309                  pop @{$open_tables};
5310    
5311                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5312    
5313                ## reprocess            ## reprocess
5314                redo B;            !!!ack-later;
5315          } else {            next B;
5316            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
5317              if (not $open_tables->[-1]->[1]) { # tainted
5318                !!!cp ('t227.8');
5319                ## NOTE: This is a "as if in head" code clone.
5320                $parse_rcdata->(CDATA_CONTENT_MODEL);
5321                next B;
5322              } else {
5323                !!!cp ('t227.7');
5324                #
5325              }
5326            } elsif ($token->{tag_name} eq 'script') {
5327              if (not $open_tables->[-1]->[1]) { # tainted
5328                !!!cp ('t227.6');
5329                ## NOTE: This is a "as if in head" code clone.
5330                $script_start_tag->();
5331                next B;
5332              } else {
5333                !!!cp ('t227.5');
5334                #
5335              }
5336            } elsif ($token->{tag_name} eq 'input') {
5337              if (not $open_tables->[-1]->[1]) { # tainted
5338                if ($token->{attributes}->{type}) { ## TODO: case
5339                  my $type = lc $token->{attributes}->{type}->{value};
5340                  if ($type eq 'hidden') {
5341                    !!!cp ('t227.3');
5342                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5343    
5344            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5345    
5346                    ## TODO: form element pointer
5347    
5348                    pop @{$self->{open_elements}};
5349    
5350                    !!!next-token;
5351                    !!!ack ('t227.2.1');
5352                    next B;
5353                  } else {
5354                    !!!cp ('t227.2');
5355                    #
5356                  }
5357                } else {
5358                  !!!cp ('t227.1');
5359                  #
5360                }
5361              } else {
5362                !!!cp ('t227.4');
5363                #
5364              }
5365            } else {
5366              !!!cp ('t227');
5367            #            #
5368          }          }
5369    
5370            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5371    
5372            $insert = $insert_to_foster;
5373            #
5374        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5375              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5376                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3756  sub _tree_construction_main ($) { Line 5378  sub _tree_construction_main ($) {
5378                my $i;                my $i;
5379                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5380                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5381                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5382                      !!!cp ('t228');
5383                    $i = $_;                    $i = $_;
5384                    last INSCOPE;                    last INSCOPE;
5385                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5386                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5387                    last INSCOPE;                    last INSCOPE;
5388                  }                  }
5389                } # INSCOPE                } # INSCOPE
5390                unless (defined $i) {                unless (defined $i) {
5391                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5392                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5393                  ## Ignore the token                  ## Ignore the token
5394                    !!!nack ('t230.1');
5395                  !!!next-token;                  !!!next-token;
5396                  redo B;                  next B;
5397                  } else {
5398                    !!!cp ('t232');
5399                }                }
5400    
5401                ## Clear back to table row context                ## Clear back to table row context
5402                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5403                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5404                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5405                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5406                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5407                }                }
5408    
5409                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5410                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5411                !!!next-token;                !!!next-token;
5412                redo B;                !!!nack ('t231.1');
5413                  next B;
5414              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5415                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5416                  ## As if </tr>                  ## As if </tr>
# Line 3791  sub _tree_construction_main ($) { Line 5418  sub _tree_construction_main ($) {
5418                  my $i;                  my $i;
5419                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5420                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5421                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5422                        !!!cp ('t233');
5423                      $i = $_;                      $i = $_;
5424                      last INSCOPE;                      last INSCOPE;
5425                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5426                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5427                      last INSCOPE;                      last INSCOPE;
5428                    }                    }
5429                  } # INSCOPE                  } # INSCOPE
5430                  unless (defined $i) {                  unless (defined $i) {
5431                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5432    ## TODO: The following is wrong.
5433                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5434                    ## Ignore the token                    ## Ignore the token
5435                      !!!nack ('t236.1');
5436                    !!!next-token;                    !!!next-token;
5437                    redo B;                    next B;
5438                  }                  }
5439                                    
5440                  ## Clear back to table row context                  ## Clear back to table row context
5441                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5442                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5443                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5444                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5445                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5446                  }                  }
5447                                    
# Line 3825  sub _tree_construction_main ($) { Line 5455  sub _tree_construction_main ($) {
5455                  my $i;                  my $i;
5456                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5457                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5458                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5459                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5460                      $i = $_;                      $i = $_;
5461                      last INSCOPE;                      last INSCOPE;
5462                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5463                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5464                      last INSCOPE;                      last INSCOPE;
5465                    }                    }
5466                  } # INSCOPE                  } # INSCOPE
5467                  unless (defined $i) {                  unless (defined $i) {
5468                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5469                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5470                    ## Ignore the token                    ## Ignore the token
5471                      !!!nack ('t239.1');
5472                    !!!next-token;                    !!!next-token;
5473                    redo B;                    next B;
5474                  }                  }
5475                                    
5476                  ## Clear back to table body context                  ## Clear back to table body context
5477                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5478                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5479                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5480                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5481                  }                  }
5482                                    
# Line 3863  sub _tree_construction_main ($) { Line 5492  sub _tree_construction_main ($) {
5492                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5493                }                }
5494    
5495                  ## NOTE: </table> in the "in table" insertion mode.
5496                  ## When you edit the code fragment below, please ensure that
5497                  ## the code for <table> in the "in table" insertion mode
5498                  ## is synced with it.
5499    
5500                ## have a table element in table scope                ## have a table element in table scope
5501                my $i;                my $i;
5502                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5503                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5504                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5505                      !!!cp ('t241');
5506                    $i = $_;                    $i = $_;
5507                    last INSCOPE;                    last INSCOPE;
5508                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5509                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5510                    last INSCOPE;                    last INSCOPE;
5511                  }                  }
5512                } # INSCOPE                } # INSCOPE
5513                unless (defined $i) {                unless (defined $i) {
5514                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5515                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5516                  ## Ignore the token                  ## Ignore the token
5517                    !!!nack ('t243.1');
5518                  !!!next-token;                  !!!next-token;
5519                  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]);  
5520                }                }
5521                                    
5522                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5523                  pop @{$open_tables};
5524                                
5525                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5526                                
5527                !!!next-token;                !!!next-token;
5528                redo B;                next B;
5529              } elsif ({              } elsif ({
5530                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5531                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 3914  sub _tree_construction_main ($) { Line 5535  sub _tree_construction_main ($) {
5535                  my $i;                  my $i;
5536                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5537                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5538                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5539                        !!!cp ('t247');
5540                      $i = $_;                      $i = $_;
5541                      last INSCOPE;                      last INSCOPE;
5542                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5543                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
5544                      last INSCOPE;                      last INSCOPE;
5545                    }                    }
5546                  } # INSCOPE                  } # INSCOPE
5547                    unless (defined $i) {                    unless (defined $i) {
5548                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
5549                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5550                      ## Ignore the token                      ## Ignore the token
5551                        !!!nack ('t249.1');
5552                      !!!next-token;                      !!!next-token;
5553                      redo B;                      next B;
5554                    }                    }
5555                                    
5556                  ## As if </tr>                  ## As if </tr>
# Line 3935  sub _tree_construction_main ($) { Line 5558  sub _tree_construction_main ($) {
5558                  my $i;                  my $i;
5559                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5560                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5561                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5562                        !!!cp ('t250');
5563                      $i = $_;                      $i = $_;
5564                      last INSCOPE;                      last INSCOPE;
5565                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5566                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
5567                      last INSCOPE;                      last INSCOPE;
5568                    }                    }
5569                  } # INSCOPE                  } # INSCOPE
5570                    unless (defined $i) {                    unless (defined $i) {
5571                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
5572                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5573                      ## Ignore the token                      ## Ignore the token
5574                        !!!nack ('t252.1');
5575                      !!!next-token;                      !!!next-token;
5576                      redo B;                      next B;
5577                    }                    }
5578                                    
5579                  ## Clear back to table row context                  ## Clear back to table row context
5580                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5581                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5582                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
5583                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5584                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5585                  }                  }
5586                                    
# Line 3968  sub _tree_construction_main ($) { Line 5593  sub _tree_construction_main ($) {
5593                my $i;                my $i;
5594                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5595                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5596                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5597                      !!!cp ('t254');
5598                    $i = $_;                    $i = $_;
5599                    last INSCOPE;                    last INSCOPE;
5600                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5601                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5602                    last INSCOPE;                    last INSCOPE;
5603                  }                  }
5604                } # INSCOPE                } # INSCOPE
5605                unless (defined $i) {                unless (defined $i) {
5606                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5607                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5608                  ## Ignore the token                  ## Ignore the token
5609                    !!!nack ('t256.1');
5610                  !!!next-token;                  !!!next-token;
5611                  redo B;                  next B;
5612                }                }
5613    
5614                ## Clear back to table body context                ## Clear back to table body context
5615                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5616                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5617                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5618                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5619                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5620                }                }
5621    
5622                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5623                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5624                  !!!nack ('t257.1');
5625                !!!next-token;                !!!next-token;
5626                redo B;                next B;
5627              } elsif ({              } elsif ({
5628                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5629                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5630                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5631                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5632                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5633                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5634                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5635                !!!next-token;            ## Ignore the token
5636                redo B;            !!!nack ('t258.1');
5637               !!!next-token;
5638              next B;
5639          } else {          } else {
5640            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!cp ('t259');
5641              !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5642    
5643            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5644            #            #
5645          }          }
5646          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5647            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5648                    @{$self->{open_elements}} == 1) { # redundant, maybe
5649              !!!parse-error (type => 'in body:#eof', token => $token);
5650              !!!cp ('t259.1');
5651              #
5652            } else {
5653              !!!cp ('t259.2');
5654              #
5655            }
5656    
5657            ## Stop parsing
5658            last B;
5659        } else {        } else {
5660          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5661        }        }
# Line 4020  sub _tree_construction_main ($) { Line 5664  sub _tree_construction_main ($) {
5664              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5665                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5666                unless (length $token->{data}) {                unless (length $token->{data}) {
5667                    !!!cp ('t260');
5668                  !!!next-token;                  !!!next-token;
5669                  redo B;                  next B;
5670                }                }
5671              }              }
5672                            
5673                !!!cp ('t261');
5674              #              #
5675            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5676              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5677                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
5678                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5679                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5680                  !!!ack ('t262.1');
5681                !!!next-token;                !!!next-token;
5682                redo B;                next B;
5683              } else {              } else {
5684                  !!!cp ('t263');
5685                #                #
5686              }              }
5687            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5688              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5689                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5690                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5691                    !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5692                  ## Ignore the token                  ## Ignore the token
5693                  !!!next-token;                  !!!next-token;
5694                  redo B;                  next B;
5695                } else {                } else {
5696                    !!!cp ('t265');
5697                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5698                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5699                  !!!next-token;                  !!!next-token;
5700                  redo B;                              next B;            
5701                }                }
5702              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5703                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5704                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
5705                ## Ignore the token                ## Ignore the token
5706                !!!next-token;                !!!next-token;
5707                redo B;                next B;
5708              } else {              } else {
5709                  !!!cp ('t267');
5710                #                #
5711              }              }
5712            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5713              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5714            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5715              !!!cp ('t270.2');
5716              ## Stop parsing.
5717              last B;
5718            } else {
5719              ## NOTE: As if </colgroup>.
5720              !!!cp ('t270.1');
5721              pop @{$self->{open_elements}}; # colgroup
5722              $self->{insertion_mode} = IN_TABLE_IM;
5723              ## Reprocess.
5724              next B;
5725            }
5726          } else {
5727            die "$0: $token->{type}: Unknown token type";
5728          }
5729    
5730            ## As if </colgroup>            ## As if </colgroup>
5731            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5732              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5733    ## TODO: Wrong error type?
5734                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5735              ## Ignore the token              ## Ignore the token
5736                !!!nack ('t269.1');
5737              !!!next-token;              !!!next-token;
5738              redo B;              next B;
5739            } else {            } else {
5740                !!!cp ('t270');
5741              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5742              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5743                !!!ack-later;
5744              ## reprocess              ## reprocess
5745              redo B;              next B;
5746            }            }
5747      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5748        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5749            !!!cp ('t271');
5750          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5751          !!!next-token;          !!!next-token;
5752          redo B;          next B;
5753        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5754              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5755                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5756                  ## As if </option>              !!!cp ('t272');
5757                  pop @{$self->{open_elements}};              ## As if </option>
5758                }              pop @{$self->{open_elements}};
5759              } else {
5760                !!!cp ('t273');
5761              }
5762    
5763                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5764                !!!next-token;            !!!nack ('t273.1');
5765                redo B;            !!!next-token;
5766              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5767                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5768                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5769                  pop @{$self->{open_elements}};              !!!cp ('t274');
5770                }              ## As if </option>
5771                pop @{$self->{open_elements}};
5772              } else {
5773                !!!cp ('t275');
5774              }
5775    
5776                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5777                  ## As if </optgroup>              !!!cp ('t276');
5778                  pop @{$self->{open_elements}};              ## As if </optgroup>
5779                }              pop @{$self->{open_elements}};
5780              } else {
5781                !!!cp ('t277');
5782              }
5783    
5784                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5785                !!!next-token;            !!!nack ('t277.1');
5786                redo B;            !!!next-token;
5787              } elsif ($token->{tag_name} eq 'select') {            next B;
5788                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5789                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5790                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5791                my $i;                    {
5792                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5793                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5794                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5795                    $i = $_;                    }->{$token->{tag_name}})) {
5796                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5797                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5798                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5799                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5800                    last INSCOPE;            ## have an element in table scope
5801                  }            my $i;
5802                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5803                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5804                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5805                  ## Ignore the token                !!!cp ('t278');
5806                  !!!next-token;                $i = $_;
5807                  redo B;                last INSCOPE;
5808                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5809                  !!!cp ('t279');
5810                  last INSCOPE;
5811                }
5812              } # INSCOPE
5813              unless (defined $i) {
5814                !!!cp ('t280');
5815                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5816                ## Ignore the token
5817                !!!nack ('t280.1');
5818                !!!next-token;
5819                next B;
5820              }
5821                                
5822                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5823              splice @{$self->{open_elements}}, $i;
5824    
5825                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5826    
5827                !!!next-token;            if ($token->{tag_name} eq 'select') {
5828                redo B;              !!!nack ('t281.2');
5829                !!!next-token;
5830                next B;
5831              } else {
5832                !!!cp ('t281.1');
5833                !!!ack-later;
5834                ## Reprocess the token.
5835                next B;
5836              }
5837          } else {          } else {
5838            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
5839              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5840            ## Ignore the token            ## Ignore the token
5841              !!!nack ('t282.1');
5842            !!!next-token;            !!!next-token;
5843            redo B;            next B;
5844          }          }
5845        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5846              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5847                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5848                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5849                  ## As if </option>              !!!cp ('t283');
5850                  splice @{$self->{open_elements}}, -2;              ## As if </option>
5851                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
5852                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5853                } else {              !!!cp ('t284');
5854                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
5855                  ## Ignore the token            } else {
5856                }              !!!cp ('t285');
5857                !!!next-token;              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5858                redo B;              ## Ignore the token
5859              } elsif ($token->{tag_name} eq 'option') {            }
5860                if ($self->{open_elements}->[-1]->[1] eq 'option') {            !!!nack ('t285.1');
5861                  pop @{$self->{open_elements}};            !!!next-token;
5862                } else {            next B;
5863                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'option') {
5864                  ## Ignore the token            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5865                }              !!!cp ('t286');
5866                !!!next-token;              pop @{$self->{open_elements}};
5867                redo B;            } else {
5868              } elsif ($token->{tag_name} eq 'select') {              !!!cp ('t287');
5869                ## have an element in table scope              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5870                my $i;              ## Ignore the token
5871                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            }
5872                  my $node = $self->{open_elements}->[$_];            !!!nack ('t287.1');
5873                  if ($node->[1] eq $token->{tag_name}) {            !!!next-token;
5874                    $i = $_;            next B;
5875                    last INSCOPE;          } elsif ($token->{tag_name} eq 'select') {
5876                  } elsif ({            ## have an element in table scope
5877                            table => 1, html => 1,            my $i;
5878                           }->{$node->[1]}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5879                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5880                  }              if ($node->[1] & SELECT_EL) {
5881                } # INSCOPE                !!!cp ('t288');
5882                unless (defined $i) {                $i = $_;
5883                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                last INSCOPE;
5884                  ## Ignore the token              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5885                  !!!next-token;                !!!cp ('t289');
5886                  redo B;                last INSCOPE;
5887                }              }
5888              } # INSCOPE
5889              unless (defined $i) {
5890                !!!cp ('t290');
5891                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5892                ## Ignore the token
5893                !!!nack ('t290.1');
5894                !!!next-token;
5895                next B;
5896              }
5897                                
5898                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5899              splice @{$self->{open_elements}}, $i;
5900    
5901                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5902    
5903                !!!next-token;            !!!nack ('t291.1');
5904                redo B;            !!!next-token;
5905              } elsif ({            next B;
5906                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5907                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5908                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5909                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5910                     }->{$token->{tag_name}}) {
5911    ## TODO: The following is wrong?
5912              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5913                                
5914                ## have an element in table scope            ## have an element in table scope
5915                my $i;            my $i;
5916                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5917                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5918                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5919                    $i = $_;                !!!cp ('t292');
5920                    last INSCOPE;                $i = $_;
5921                  } elsif ({                last INSCOPE;
5922                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5923                           }->{$node->[1]}) {                !!!cp ('t293');
5924                    last INSCOPE;                last INSCOPE;
5925                  }              }
5926                } # INSCOPE            } # INSCOPE
5927                unless (defined $i) {            unless (defined $i) {
5928                  ## Ignore the token              !!!cp ('t294');
5929                  !!!next-token;              ## Ignore the token
5930                  redo B;              !!!nack ('t294.1');
5931                }              !!!next-token;
5932                next B;
5933              }
5934                                
5935                ## As if </select>            ## As if </select>
5936                ## have an element in table scope            ## have an element in table scope
5937                undef $i;            undef $i;
5938                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5939                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5940                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5941                    $i = $_;                !!!cp ('t295');
5942                    last INSCOPE;                $i = $_;
5943                  } elsif ({                last INSCOPE;
5944                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5945                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
5946                    last INSCOPE;                !!!cp ('t296');
5947                  }                last INSCOPE;
5948                } # INSCOPE              }
5949                unless (defined $i) {            } # INSCOPE
5950                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
5951                  ## Ignore the </select> token              !!!cp ('t297');
5952                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
5953                  redo B;              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5954                }              ## Ignore the </select> token
5955                !!!nack ('t297.1');
5956                !!!next-token; ## TODO: ok?
5957                next B;
5958              }
5959                                
5960                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
5961              splice @{$self->{open_elements}}, $i;
5962    
5963                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5964    
5965                ## reprocess            !!!ack-later;
5966                redo B;            ## reprocess
5967              next B;
5968          } else {          } else {
5969            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
5970              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5971            ## Ignore the token            ## Ignore the token
5972              !!!nack ('t299.3');
5973            !!!next-token;            !!!next-token;
5974            redo B;            next B;
5975          }          }
5976          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5977            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5978                    @{$self->{open_elements}} == 1) { # redundant, maybe
5979              !!!cp ('t299.1');
5980              !!!parse-error (type => 'in body:#eof', token => $token);
5981            } else {
5982              !!!cp ('t299.2');
5983            }
5984    
5985            ## Stop parsing.
5986            last B;
5987        } else {        } else {
5988          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5989        }        }
# Line 4257  sub _tree_construction_main ($) { Line 5997  sub _tree_construction_main ($) {
5997            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5998                        
5999            unless (length $token->{data}) {            unless (length $token->{data}) {
6000                !!!cp ('t300');
6001              !!!next-token;              !!!next-token;
6002              redo B;              next B;
6003            }            }
6004          }          }
6005                    
6006          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6007            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
6008              !!!parse-error (type => 'after html:#character', token => $token);
6009    
6010            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6011            } else {
6012              !!!cp ('t302');
6013          }          }
6014                    
6015          ## "after body" insertion mode          ## "after body" insertion mode
6016          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
6017    
6018          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6019          ## reprocess          ## reprocess
6020          redo B;          next B;
6021        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6022          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6023            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
6024              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6025                        
6026            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6027            } else {
6028              !!!cp ('t304');
6029          }          }
6030    
6031          ## "after body" insertion mode          ## "after body" insertion mode
6032          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6033    
6034          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6035            !!!ack-later;
6036          ## reprocess          ## reprocess
6037          redo B;          next B;
6038        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6039          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6040            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
6041              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6042                        
6043            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6044            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6045            } else {
6046              !!!cp ('t306');
6047          }          }
6048    
6049          ## "after body" insertion mode          ## "after body" insertion mode
6050          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6051            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6052              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
6053                !!!parse-error (type => 'unmatched end tag:html', token => $token);
6054              ## Ignore the token              ## Ignore the token
6055              !!!next-token;              !!!next-token;
6056              redo B;              next B;
6057            } else {            } else {
6058                !!!cp ('t308');
6059              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6060              !!!next-token;              !!!next-token;
6061              redo B;              next B;
6062            }            }
6063          } else {          } else {
6064            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
6065              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6066    
6067            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6068            ## reprocess            ## reprocess
6069            redo B;            next B;
6070          }          }
6071          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6072            !!!cp ('t309.2');
6073            ## Stop parsing
6074            last B;
6075        } else {        } else {
6076          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6077        }        }
# Line 4323  sub _tree_construction_main ($) { Line 6081  sub _tree_construction_main ($) {
6081            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6082                        
6083            unless (length $token->{data}) {            unless (length $token->{data}) {
6084                !!!cp ('t310');
6085              !!!next-token;              !!!next-token;
6086              redo B;              next B;
6087            }            }
6088          }          }
6089                    
6090          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6091            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6092              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6093                !!!parse-error (type => 'in frameset:#character', token => $token);
6094            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6095              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
6096                !!!parse-error (type => 'after frameset:#character', token => $token);
6097            } else { # "after html frameset"            } else { # "after html frameset"
6098              !!!parse-error (type => 'after html:#character');              !!!cp ('t313');
6099                !!!parse-error (type => 'after html:#character', token => $token);
6100    
6101              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6102              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
6103              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6104            }            }
6105                        
6106            ## Ignore the token.            ## Ignore the token.
6107            if (length $token->{data}) {            if (length $token->{data}) {
6108                !!!cp ('t314');
6109              ## reprocess the rest of characters              ## reprocess the rest of characters
6110            } else {            } else {
6111                !!!cp ('t315');
6112              !!!next-token;              !!!next-token;
6113            }            }
6114            redo B;            next B;
6115          }          }
6116                    
6117          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6118        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6119          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6120            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t316');
6121              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6122    
6123            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6124            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6125          }          } else {
6126              !!!cp ('t317');
6127            }
6128    
6129          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6130              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6131            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6132              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6133              !!!nack ('t318.1');
6134            !!!next-token;            !!!next-token;
6135            redo B;            next B;
6136          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6137                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6138            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6139              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6140            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6141              !!!ack ('t319.1');
6142            !!!next-token;            !!!next-token;
6143            redo B;            next B;
6144          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6145              !!!cp ('t320');
6146            ## NOTE: As if in body.            ## NOTE: As if in body.
6147            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6148            redo B;            next B;
6149          } else {          } else {
6150            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6151              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6152                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6153            } else {            } else {
6154              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!cp ('t322');
6155                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6156            }            }
6157            ## Ignore the token            ## Ignore the token
6158              !!!nack ('t322.1');
6159            !!!next-token;            !!!next-token;
6160            redo B;            next B;
6161          }          }
6162        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6163          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6164            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t323');
6165              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6166    
6167            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6168            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6169            } else {
6170              !!!cp ('t324');
6171          }          }
6172    
6173          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6174              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6175            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6176                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6177              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6178                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6179              ## Ignore the token              ## Ignore the token
6180              !!!next-token;              !!!next-token;
6181            } else {            } else {
6182                !!!cp ('t326');
6183              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6184              !!!next-token;              !!!next-token;
6185            }            }
6186    
6187            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6188                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6189                !!!cp ('t327');
6190              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6191              } else {
6192                !!!cp ('t328');
6193            }            }
6194            redo B;            next B;
6195          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6196                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6197              !!!cp ('t329');
6198            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6199            !!!next-token;            !!!next-token;
6200            redo B;            next B;
6201          } else {          } else {
6202            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6203              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6204                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6205            } else {            } else {
6206              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!cp ('t331');
6207                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6208            }            }
6209            ## Ignore the token            ## Ignore the token
6210            !!!next-token;            !!!next-token;
6211            redo B;            next B;
6212            }
6213          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6214            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6215                    @{$self->{open_elements}} == 1) { # redundant, maybe
6216              !!!cp ('t331.1');
6217              !!!parse-error (type => 'in body:#eof', token => $token);
6218            } else {
6219              !!!cp ('t331.2');
6220          }          }
6221            
6222            ## Stop parsing
6223            last B;
6224        } else {        } else {
6225          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6226        }        }
# Line 4436  sub _tree_construction_main ($) { Line 6233  sub _tree_construction_main ($) {
6233      ## "in body" insertion mode      ## "in body" insertion mode
6234      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
6235        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6236            !!!cp ('t332');
6237          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6238          $script_start_tag->($insert);          $script_start_tag->();
6239          redo B;          next B;
6240        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6241            !!!cp ('t333');
6242          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6243          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6244          redo B;          next B;
6245        } elsif ({        } elsif ({
6246                  base => 1, link => 1,                  base => 1, link => 1,
6247                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6248            !!!cp ('t334');
6249          ## 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
6250          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6251          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6252            !!!ack ('t334.1');
6253          !!!next-token;          !!!next-token;
6254          redo B;          next B;
6255        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6256          ## 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
6257          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6258          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.
6259    
6260          unless ($self->{confident}) {          unless ($self->{confident}) {
6261            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6262                !!!cp ('t335');
6263                ## NOTE: Whether the encoding is supported or not is handled
6264                ## in the {change_encoding} callback.
6265              $self->{change_encoding}              $self->{change_encoding}
6266                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6267                
6268                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6269                    ->set_user_data (manakai_has_reference =>
6270                                         $token->{attributes}->{charset}
6271                                             ->{has_reference});
6272            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6273              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6274                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6275                        [\x09-\x0D\x20]*=
6276                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6277                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6278                  !!!cp ('t336');
6279                  ## NOTE: Whether the encoding is supported or not is handled
6280                  ## in the {change_encoding} callback.
6281                $self->{change_encoding}                $self->{change_encoding}
6282                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6283                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6284                      ->set_user_data (manakai_has_reference =>
6285                                           $token->{attributes}->{content}
6286                                                 ->{has_reference});
6287              }              }
6288            }            }
6289            } else {
6290              if ($token->{attributes}->{charset}) {
6291                !!!cp ('t337');
6292                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6293                    ->set_user_data (manakai_has_reference =>
6294                                         $token->{attributes}->{charset}
6295                                             ->{has_reference});
6296              }
6297              if ($token->{attributes}->{content}) {
6298                !!!cp ('t338');
6299                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6300                    ->set_user_data (manakai_has_reference =>
6301                                         $token->{attributes}->{content}
6302                                             ->{has_reference});
6303              }
6304          }          }
6305    
6306            !!!ack ('t338.1');
6307          !!!next-token;          !!!next-token;
6308          redo B;          next B;
6309        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6310          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6311          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6312          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6313            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6314        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6315          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6316                                
6317          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6318              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6319              !!!cp ('t342');
6320            ## Ignore the token            ## Ignore the token
6321          } else {          } else {
6322            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6323            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6324              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6325                  !!!cp ('t343');
6326                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6327                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6328                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6329              }              }
6330            }            }
6331          }          }
6332            !!!nack ('t343.1');
6333          !!!next-token;          !!!next-token;
6334          redo B;          next B;
6335        } elsif ({        } elsif ({
6336                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6337                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6338                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6339                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6340                  pre => 1,                  pre => 1, listing => 1,
6341                    form => 1,
6342                    table => 1,
6343                    hr => 1,
6344                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6345            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6346              !!!cp ('t350');
6347              !!!parse-error (type => 'in form:form', token => $token);
6348              ## Ignore the token
6349              !!!nack ('t350.1');
6350              !!!next-token;
6351              next B;
6352            }
6353    
6354          ## has a p element in scope          ## has a p element in scope
6355          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6356            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6357              !!!back-token;              !!!cp ('t344');
6358              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
6359              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6360            } elsif ({                        line => $token->{line}, column => $token->{column}};
6361                      table => 1, caption => 1, td => 1, th => 1,              next B;
6362                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6363                     }->{$_->[1]}) {              !!!cp ('t345');
6364              last INSCOPE;              last INSCOPE;
6365            }            }
6366          } # INSCOPE          } # INSCOPE
6367                        
6368          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6369          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6370              !!!nack ('t346.1');
6371            !!!next-token;            !!!next-token;
6372            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6373              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6374              unless (length $token->{data}) {              unless (length $token->{data}) {
6375                  !!!cp ('t346');
6376                !!!next-token;                !!!next-token;
6377                } else {
6378                  !!!cp ('t349');
6379              }              }
6380              } else {
6381                !!!cp ('t348');
6382            }            }
6383          } else {          } elsif ($token->{tag_name} eq 'form') {
6384              !!!cp ('t347.1');
6385              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6386    
6387              !!!nack ('t347.2');
6388            !!!next-token;            !!!next-token;
6389          }          } elsif ($token->{tag_name} eq 'table') {
6390          redo B;            !!!cp ('t382');
6391        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6392          if (defined $self->{form_element}) {            
6393            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6394            ## Ignore the token  
6395              !!!nack ('t382.1');
6396              !!!next-token;
6397            } elsif ($token->{tag_name} eq 'hr') {
6398              !!!cp ('t386');
6399              pop @{$self->{open_elements}};
6400            
6401              !!!nack ('t386.1');
6402            !!!next-token;            !!!next-token;
           redo B;  
6403          } else {          } else {
6404            ## 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];  
6405            !!!next-token;            !!!next-token;
           redo B;  
6406          }          }
6407        } elsif ($token->{tag_name} eq 'li') {          next B;
6408          ## 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') {  
6409          ## has a p element in scope          ## has a p element in scope
6410          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6411            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6412              !!!back-token;              !!!cp ('t353');
6413              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
6414              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6415            } elsif ({                        line => $token->{line}, column => $token->{column}};
6416                      table => 1, caption => 1, td => 1, th => 1,              next B;
6417                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6418                     }->{$_->[1]}) {              !!!cp ('t354');
6419              last INSCOPE;              last INSCOPE;
6420            }            }
6421          } # INSCOPE          } # INSCOPE
# Line 4627  sub _tree_construction_main ($) { Line 6423  sub _tree_construction_main ($) {
6423          ## Step 1          ## Step 1
6424          my $i = -1;          my $i = -1;
6425          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6426            my $li_or_dtdd = {li => {li => 1},
6427                              dt => {dt => 1, dd => 1},
6428                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6429          LI: {          LI: {
6430            ## Step 2            ## Step 2
6431            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6432              if ($i != -1) {              if ($i != -1) {
6433                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6434                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6435                                  value => $self->{open_elements}->[-1]->[0]
6436                                      ->manakai_local_name,
6437                                  token => $token);
6438                } else {
6439                  !!!cp ('t356');
6440              }              }
6441              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6442              last LI;              last LI;
6443              } else {
6444                !!!cp ('t357');
6445            }            }
6446                        
6447            ## Step 3            ## Step 3
6448            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6449                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6450                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6451                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6452                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6453                  not ($node->[1] & DIV_EL)) {
6454                !!!cp ('t358');
6455              last LI;              last LI;
6456            }            }
6457                        
6458              !!!cp ('t359');
6459            ## Step 4            ## Step 4
6460            $i--;            $i--;
6461            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
6462            redo LI;            redo LI;
6463          } # LI          } # LI
6464                        
6465          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6466            !!!nack ('t359.1');
6467          !!!next-token;          !!!next-token;
6468          redo B;          next B;
6469        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6470          ## has a p element in scope          ## has a p element in scope
6471          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6472            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6473              !!!back-token;              !!!cp ('t367');
6474              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
6475              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6476            } elsif ({                        line => $token->{line}, column => $token->{column}};
6477                      table => 1, caption => 1, td => 1, th => 1,              next B;
6478                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6479                     }->{$_->[1]}) {              !!!cp ('t368');
6480              last INSCOPE;              last INSCOPE;
6481            }            }
6482          } # INSCOPE          } # INSCOPE
6483                        
6484          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6485                        
6486          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6487                        
6488            !!!nack ('t368.1');
6489          !!!next-token;          !!!next-token;
6490          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;  
6491        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6492          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6493            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6494            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6495              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
6496                !!!parse-error (type => 'in a:a', token => $token);
6497                            
6498              !!!back-token;              !!!back-token; # <a>
6499              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6500              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6501                $formatting_end_tag->($token);
6502                            
6503              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6504                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6505                    !!!cp ('t372');
6506                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
6507                  last AFE2;                  last AFE2;
6508                }                }
6509              } # AFE2              } # AFE2
6510              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
6511                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6512                    !!!cp ('t373');
6513                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
6514                  last OE;                  last OE;
6515                }                }
6516              } # OE              } # OE
6517              last AFE;              last AFE;
6518            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
6519                !!!cp ('t374');
6520              last AFE;              last AFE;
6521            }            }
6522          } # AFE          } # AFE
6523                        
6524          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6525    
6526          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6527          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6528    
6529            !!!nack ('t374.1');
6530          !!!next-token;          !!!next-token;
6531          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;  
6532        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6533          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6534    
6535          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6536          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6537            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6538            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6539              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
6540              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
6541              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
6542              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6543            } elsif ({                        line => $token->{line}, column => $token->{column}};
6544                      table => 1, caption => 1, td => 1, th => 1,              next B;
6545                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6546                     }->{$node->[1]}) {              !!!cp ('t377');
6547              last INSCOPE;              last INSCOPE;
6548            }            }
6549          } # INSCOPE          } # INSCOPE
6550                    
6551          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6552          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6553                    
6554            !!!nack ('t377.1');
6555          !!!next-token;          !!!next-token;
6556          redo B;          next B;
6557        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6558          ## has a button element in scope          ## has a button element in scope
6559          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6560            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6561            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6562              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
6563              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
6564              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
6565              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6566            } elsif ({                        line => $token->{line}, column => $token->{column}};
6567                      table => 1, caption => 1, td => 1, th => 1,              next B;
6568                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6569                     }->{$node->[1]}) {              !!!cp ('t379');
6570              last INSCOPE;              last INSCOPE;
6571            }            }
6572          } # INSCOPE          } # INSCOPE
6573                        
6574          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6575                        
6576          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6577          push @$active_formatting_elements, ['#marker', ''];  
6578            ## TODO: associate with $self->{form_element} if defined
6579    
         !!!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});  
6580          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6581            
6582          !!!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;  
             
6583          !!!next-token;          !!!next-token;
6584          redo B;          next B;
6585        } elsif ({        } elsif ({
6586                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6587                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6588                  image => 1,                  noembed => 1,
6589                    noframes => 1,
6590                    noscript => 0, ## TODO: 1 if scripting is enabled
6591                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6592          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6593            !!!parse-error (type => 'image');            !!!cp ('t381');
6594            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
6595            } else {
6596              !!!cp ('t399');
6597          }          }
6598            ## NOTE: There is an "as if in body" code clone.
6599          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6600          $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;  
6601        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6602          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6603                    
6604          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6605              !!!cp ('t389');
6606            ## Ignore the token            ## Ignore the token
6607              !!!nack ('t389'); ## NOTE: Not acknowledged.
6608            !!!next-token;            !!!next-token;
6609            redo B;            next B;
6610          } else {          } else {
6611            my $at = $token->{attributes};            my $at = $token->{attributes};
6612            my $form_attrs;            my $form_attrs;
# Line 4915  sub _tree_construction_main ($) { Line 6617  sub _tree_construction_main ($) {
6617            delete $at->{prompt};            delete $at->{prompt};
6618            my @tokens = (            my @tokens = (
6619                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6620                           attributes => $form_attrs},                           attributes => $form_attrs,
6621                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6622                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6623                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6624                            {type => START_TAG_TOKEN, tag_name => 'p',
6625                             line => $token->{line}, column => $token->{column}},
6626                            {type => START_TAG_TOKEN, tag_name => 'label',
6627                             line => $token->{line}, column => $token->{column}},
6628                         );                         );
6629            if ($prompt_attr) {            if ($prompt_attr) {
6630              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
6631                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6632                               #line => $token->{line}, column => $token->{column},
6633                              };
6634            } else {            } else {
6635                !!!cp ('t391');
6636              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6637                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6638                               #line => $token->{line}, column => $token->{column},
6639                              }; # SHOULD
6640              ## TODO: make this configurable              ## TODO: make this configurable
6641            }            }
6642            push @tokens,            push @tokens,
6643                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6644                             line => $token->{line}, column => $token->{column}},
6645                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6646                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6647                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6648                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6649                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6650            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6651                             line => $token->{line}, column => $token->{column}},
6652                            {type => END_TAG_TOKEN, tag_name => 'form',
6653                             line => $token->{line}, column => $token->{column}};
6654              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6655            !!!back-token (@tokens);            !!!back-token (@tokens);
6656            redo B;            !!!next-token;
6657              next B;
6658          }          }
6659        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6660          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6661          my $el;          my $el;
6662          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6663                    
6664          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6665          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4950  sub _tree_construction_main ($) { Line 6668  sub _tree_construction_main ($) {
6668          $insert->($el);          $insert->($el);
6669                    
6670          my $text = '';          my $text = '';
6671            !!!nack ('t392.1');
6672          !!!next-token;          !!!next-token;
6673          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6674            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
6675            unless (length $token->{data}) {            unless (length $token->{data}) {
6676                !!!cp ('t392');
6677              !!!next-token;              !!!next-token;
6678              } else {
6679                !!!cp ('t393');
6680            }            }
6681            } else {
6682              !!!cp ('t394');
6683          }          }
6684          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
6685              !!!cp ('t395');
6686            $text .= $token->{data};            $text .= $token->{data};
6687            !!!next-token;            !!!next-token;
6688          }          }
6689          if (length $text) {          if (length $text) {
6690              !!!cp ('t396');
6691            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6692          }          }
6693                    
# Line 4969  sub _tree_construction_main ($) { Line 6695  sub _tree_construction_main ($) {
6695                    
6696          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
6697              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6698              !!!cp ('t397');
6699            ## Ignore the token            ## Ignore the token
6700          } else {          } else {
6701            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6702              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6703          }          }
6704          !!!next-token;          !!!next-token;
6705          redo B;          next B;
6706        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6707                  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') {  
6708          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6709    
6710            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6711    
6712            ## "adjust foreign attributes" - done in insert-element-f
6713                    
6714          !!!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);
6715                    
6716          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6717              pop @{$self->{open_elements}};
6718              !!!ack ('t398.1');
6719            } else {
6720              !!!cp ('t398.2');
6721              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6722              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6723              ## mode, "in body" (not "in foreign content") secondary insertion
6724              ## mode, maybe.
6725            }
6726    
6727          !!!next-token;          !!!next-token;
6728          redo B;          next B;
6729        } elsif ({        } elsif ({
6730                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6731                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6732                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6733                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6734                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6735          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
6736            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6737          ## Ignore the token          ## Ignore the token
6738            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6739          !!!next-token;          !!!next-token;
6740          redo B;          next B;
6741                    
6742          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6743        } else {        } else {
6744            if ($token->{tag_name} eq 'image') {
6745              !!!cp ('t384');
6746              !!!parse-error (type => 'image', token => $token);
6747              $token->{tag_name} = 'img';
6748            } else {
6749              !!!cp ('t385');
6750            }
6751    
6752            ## NOTE: There is an "as if <br>" code clone.
6753          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6754                    
6755          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6756    
6757            if ({
6758                 applet => 1, marquee => 1, object => 1,
6759                }->{$token->{tag_name}}) {
6760              !!!cp ('t380');
6761              push @$active_formatting_elements, ['#marker', ''];
6762              !!!nack ('t380.1');
6763            } elsif ({
6764                      b => 1, big => 1, em => 1, font => 1, i => 1,
6765                      s => 1, small => 1, strile => 1,
6766                      strong => 1, tt => 1, u => 1,
6767                     }->{$token->{tag_name}}) {
6768              !!!cp ('t375');
6769              push @$active_formatting_elements, $self->{open_elements}->[-1];
6770              !!!nack ('t375.1');
6771            } elsif ($token->{tag_name} eq 'input') {
6772              !!!cp ('t388');
6773              ## TODO: associate with $self->{form_element} if defined
6774              pop @{$self->{open_elements}};
6775              !!!ack ('t388.2');
6776            } elsif ({
6777                      area => 1, basefont => 1, bgsound => 1, br => 1,
6778                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6779                      #image => 1,
6780                     }->{$token->{tag_name}}) {
6781              !!!cp ('t388.1');
6782              pop @{$self->{open_elements}};
6783              !!!ack ('t388.3');
6784            } elsif ($token->{tag_name} eq 'select') {
6785              ## TODO: associate with $self->{form_element} if defined
6786            
6787              if ($self->{insertion_mode} & TABLE_IMS or
6788                  $self->{insertion_mode} & BODY_TABLE_IMS or
6789                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6790                !!!cp ('t400.1');
6791                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6792              } else {
6793                !!!cp ('t400.2');
6794                $self->{insertion_mode} = IN_SELECT_IM;
6795              }
6796              !!!nack ('t400.3');
6797            } else {
6798              !!!nack ('t402');
6799            }
6800                    
6801          !!!next-token;          !!!next-token;
6802          redo B;          next B;
6803        }        }
6804      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6805        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6806          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6807              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6808            for (@{$self->{open_elements}}) {          INSCOPE: {
6809              unless ({            for (reverse @{$self->{open_elements}}) {
6810                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6811                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6812                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6813                      }->{$_->[1]}) {                last INSCOPE;
6814                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
6815                  !!!cp ('t405.1');
6816                  last;
6817              }              }
6818            }            }
6819    
6820            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6821                              value => $token->{tag_name}, token => $token);
6822              ## NOTE: Ignore the token.
6823            !!!next-token;            !!!next-token;
6824            redo B;            next B;
6825          } else {          } # INSCOPE
6826            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
6827            ## Ignore the token          for (@{$self->{open_elements}}) {
6828            !!!next-token;            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6829            redo B;              !!!cp ('t403');
6830                !!!parse-error (type => 'not closed',
6831                                value => $_->[0]->manakai_local_name,
6832                                token => $token);
6833                last;
6834              } else {
6835                !!!cp ('t404');
6836              }
6837          }          }
6838    
6839            $self->{insertion_mode} = AFTER_BODY_IM;
6840            !!!next-token;
6841            next B;
6842        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6843          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
6844            ## up-to-date, though it has same effect as speced.
6845            if (@{$self->{open_elements}} > 1 and
6846                $self->{open_elements}->[1]->[1] & BODY_EL) {
6847            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6848            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6849              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
6850                !!!parse-error (type => 'not closed',
6851                                value => $self->{open_elements}->[1]->[0]
6852                                    ->manakai_local_name,
6853                                token => $token);
6854              } else {
6855                !!!cp ('t407');
6856            }            }
6857            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6858            ## reprocess            ## reprocess
6859            redo B;            next B;
6860          } else {          } else {
6861            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
6862              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6863            ## Ignore the token            ## Ignore the token
6864            !!!next-token;            !!!next-token;
6865            redo B;            next B;
6866          }          }
6867        } elsif ({        } elsif ({
6868                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6869                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6870                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6871                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6872                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6873                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6874          ## has an element in scope          ## has an element in scope
6875          my $i;          my $i;
6876          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6877            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6878            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6879              ## 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;  
             }  
6880              $i = $_;              $i = $_;
6881              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6882            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6883                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6884              last INSCOPE;              last INSCOPE;
6885            }            }
6886          } # INSCOPE          } # INSCOPE
6887            
6888          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6889            if (defined $i) {            !!!cp ('t413');
6890              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6891            } else {
6892              ## Step 1. generate implied end tags
6893              while ({
6894                      dd => ($token->{tag_name} ne 'dd'),
6895                      dt => ($token->{tag_name} ne 'dt'),
6896                      li => ($token->{tag_name} ne 'li'),
6897                      p => 1,
6898                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6899                !!!cp ('t409');
6900                pop @{$self->{open_elements}};
6901              }
6902    
6903              ## Step 2.
6904              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6905                      ne $token->{tag_name}) {
6906                !!!cp ('t412');
6907                !!!parse-error (type => 'not closed',
6908                                value => $self->{open_elements}->[-1]->[0]
6909                                    ->manakai_local_name,
6910                                token => $token);
6911            } else {            } else {
6912              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6913            }            }
6914          }  
6915                      ## Step 3.
         if (defined $i) {  
6916            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6917          } elsif ($token->{tag_name} eq 'p') {  
6918            ## As if <p>, then reprocess the current token            ## Step 4.
6919            my $el;            $clear_up_to_marker->()
6920            !!!create-element ($el, 'p');                if {
6921            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
6922                  }->{$token->{tag_name}};
6923          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6924          !!!next-token;          !!!next-token;
6925          redo B;          next B;
6926        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6927            undef $self->{form_element};
6928    
6929          ## has an element in scope          ## has an element in scope
6930            my $i;
6931          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6932            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6933            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6934              ## generate implied end tags              !!!cp ('t418');
6935              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;  
             }  
6936              last INSCOPE;              last INSCOPE;
6937            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6938                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6939              last INSCOPE;              last INSCOPE;
6940            }            }
6941          } # INSCOPE          } # INSCOPE
6942            
6943          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6944            pop @{$self->{open_elements}};            !!!cp ('t421');
6945          } else {            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6946            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } else {
6947              ## Step 1. generate implied end tags
6948              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6949                !!!cp ('t417');
6950                pop @{$self->{open_elements}};
6951              }
6952              
6953              ## Step 2.
6954              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6955                      ne $token->{tag_name}) {
6956                !!!cp ('t417.1');
6957                !!!parse-error (type => 'not closed',
6958                                value => $self->{open_elements}->[-1]->[0]
6959                                    ->manakai_local_name,
6960                                token => $token);
6961              } else {
6962                !!!cp ('t420');
6963              }  
6964              
6965              ## Step 3.
6966              splice @{$self->{open_elements}}, $i;
6967          }          }
6968    
         undef $self->{form_element};  
6969          !!!next-token;          !!!next-token;
6970          redo B;          next B;
6971        } elsif ({        } elsif ({
6972                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6973                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5150  sub _tree_construction_main ($) { Line 6975  sub _tree_construction_main ($) {
6975          my $i;          my $i;
6976          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6977            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6978            if ({            if ($node->[1] & HEADING_EL) {
6979                 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;  
             }  
6980              $i = $_;              $i = $_;
6981              last INSCOPE;              last INSCOPE;
6982            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6983                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6984              last INSCOPE;              last INSCOPE;
6985            }            }
6986          } # INSCOPE          } # INSCOPE
6987            
6988          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6989            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
6990              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6991            } else {
6992              ## Step 1. generate implied end tags
6993              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6994                !!!cp ('t422');
6995                pop @{$self->{open_elements}};
6996              }
6997              
6998              ## Step 2.
6999              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7000                      ne $token->{tag_name}) {
7001                !!!cp ('t425');
7002                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7003              } else {
7004                !!!cp ('t426');
7005              }
7006    
7007              ## Step 3.
7008              splice @{$self->{open_elements}}, $i;
7009          }          }
7010                    
         splice @{$self->{open_elements}}, $i if defined $i;  
7011          !!!next-token;          !!!next-token;
7012          redo B;          next B;
7013          } elsif ($token->{tag_name} eq 'p') {
7014            ## has an element in scope
7015            my $i;
7016            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7017              my $node = $self->{open_elements}->[$_];
7018              if ($node->[1] & P_EL) {
7019                !!!cp ('t410.1');
7020                $i = $_;
7021                last INSCOPE;
7022              } elsif ($node->[1] & SCOPING_EL) {
7023                !!!cp ('t411.1');
7024                last INSCOPE;
7025              }
7026            } # INSCOPE
7027    
7028            if (defined $i) {
7029              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7030                      ne $token->{tag_name}) {
7031                !!!cp ('t412.1');
7032                !!!parse-error (type => 'not closed',
7033                                value => $self->{open_elements}->[-1]->[0]
7034                                    ->manakai_local_name,
7035                                token => $token);
7036              } else {
7037                !!!cp ('t414.1');
7038              }
7039    
7040              splice @{$self->{open_elements}}, $i;
7041            } else {
7042              !!!cp ('t413.1');
7043              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7044    
7045              !!!cp ('t415.1');
7046              ## As if <p>, then reprocess the current token
7047              my $el;
7048              !!!create-element ($el, $HTML_NS, 'p',, $token);
7049              $insert->($el);
7050              ## NOTE: Not inserted into |$self->{open_elements}|.
7051            }
7052    
7053            !!!next-token;
7054            next B;
7055        } elsif ({        } elsif ({
7056                  a => 1,                  a => 1,
7057                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
7058                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
7059                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7060                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7061          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
7062          redo B;          $formatting_end_tag->($token);
7063            next B;
7064        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7065          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
7066            !!!parse-error (type => 'unmatched end tag:br', token => $token);
7067    
7068          ## As if <br>          ## As if <br>
7069          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7070                    
7071          my $el;          my $el;
7072          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7073          $insert->($el);          $insert->($el);
7074                    
7075          ## Ignore the token.          ## Ignore the token.
7076          !!!next-token;          !!!next-token;
7077          redo B;          next B;
7078        } elsif ({        } elsif ({
7079                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7080                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5214  sub _tree_construction_main ($) { Line 7087  sub _tree_construction_main ($) {
7087                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
7088                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7089                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7090          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
7091            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7092          ## Ignore the token          ## Ignore the token
7093          !!!next-token;          !!!next-token;
7094          redo B;          next B;
7095                    
7096          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7097                    
# Line 5228  sub _tree_construction_main ($) { Line 7102  sub _tree_construction_main ($) {
7102    
7103          ## Step 2          ## Step 2
7104          S2: {          S2: {
7105            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7106              ## Step 1              ## Step 1
7107              ## generate implied end tags              ## generate implied end tags
7108              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7109                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7110                   td => 1, th => 1, tr => 1,                ## ISSUE: Can this case be reached?
7111                   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;  
7112              }              }
7113                    
7114              ## Step 2              ## Step 2
7115              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7116                        ne $token->{tag_name}) {
7117                  !!!cp ('t431');
7118                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7119                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7120                                  value => $self->{open_elements}->[-1]->[0]
7121                                      ->manakai_local_name,
7122                                  token => $token);
7123                } else {
7124                  !!!cp ('t432');
7125              }              }
7126                            
7127              ## Step 3              ## Step 3
# Line 5255  sub _tree_construction_main ($) { Line 7131  sub _tree_construction_main ($) {
7131              last S2;              last S2;
7132            } else {            } else {
7133              ## Step 3              ## Step 3
7134              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7135                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7136                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7137                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7138                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7139                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7140                ## Ignore the token                ## Ignore the token
7141                !!!next-token;                !!!next-token;
7142                last S2;                last S2;
7143              }              }
7144    
7145                !!!cp ('t434');
7146            }            }
7147                        
7148            ## Step 4            ## Step 4
# Line 5273  sub _tree_construction_main ($) { Line 7152  sub _tree_construction_main ($) {
7152            ## Step 5;            ## Step 5;
7153            redo S2;            redo S2;
7154          } # S2          } # S2
7155          redo B;          next B;
7156        }        }
7157      }      }
7158      redo B;      next B;
7159      } continue { # B
7160        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7161          ## NOTE: The code below is executed in cases where it does not have
7162          ## to be, but it it is harmless even in those cases.
7163          ## has an element in scope
7164          INSCOPE: {
7165            for (reverse 0..$#{$self->{open_elements}}) {
7166              my $node = $self->{open_elements}->[$_];
7167              if ($node->[1] & FOREIGN_EL) {
7168                last INSCOPE;
7169              } elsif ($node->[1] & SCOPING_EL) {
7170                last;
7171              }
7172            }
7173            
7174            ## NOTE: No foreign element in scope.
7175            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7176          } # INSCOPE
7177        }
7178    } # B    } # B
7179    
   ## 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  
   
7180    ## Stop parsing # MUST    ## Stop parsing # MUST
7181        
7182    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5325  sub set_inner_html ($$$) { Line 7218  sub set_inner_html ($$$) {
7218      my $p = $class->new;      my $p = $class->new;
7219      $p->{document} = $doc;      $p->{document} = $doc;
7220    
7221      ## Step 9 # MUST      ## Step 8 # MUST
7222      my $i = 0;      my $i = 0;
7223      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7224      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7225      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7226        my $self = shift;        my $self = shift;
7227    
7228        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7229        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7230    
7231          $self->{next_char} = -1 and return if $i >= length $$s;
7232          $self->{next_char} = ord substr $$s, $i++, 1;
7233    
7234        $self->{next_input_character} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7235        $self->{next_input_character} = ord substr $$s, $i++, 1;        $p->{column}++;
7236        $column++;  
7237          if ($self->{next_char} == 0x000A) { # LF
7238        if ($self->{next_input_character} == 0x000A) { # LF          $p->{line}++;
7239          $line++;          $p->{column} = 0;
7240          $column = 0;          !!!cp ('i1');
7241        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7242          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7243          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7244          $line++;          $p->{line}++;
7245          $column = 0;          $p->{column} = 0;
7246        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7247          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7248        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7249            !!!cp ('i3');
7250          } elsif ($self->{next_char} == 0x0000) { # NULL
7251            !!!cp ('i4');
7252          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7253          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7254          } elsif ($self->{next_char} <= 0x0008 or
7255                   (0x000E <= $self->{next_char} and
7256                    $self->{next_char} <= 0x001F) or
7257                   (0x007F <= $self->{next_char} and
7258                    $self->{next_char} <= 0x009F) or
7259                   (0xD800 <= $self->{next_char} and
7260                    $self->{next_char} <= 0xDFFF) or
7261                   (0xFDD0 <= $self->{next_char} and
7262                    $self->{next_char} <= 0xFDDF) or
7263                   {
7264                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7265                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7266                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7267                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7268                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7269                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7270                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7271                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7272                    0x10FFFE => 1, 0x10FFFF => 1,
7273                   }->{$self->{next_char}}) {
7274            !!!cp ('i4.1');
7275            !!!parse-error (type => 'control char', level => $self->{must_level});
7276    ## TODO: error type documentation
7277        }        }
7278      };      };
7279      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7280      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7281            
7282      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7283        my (%opt) = @_;        my (%opt) = @_;
7284        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7285          my $column = $opt{column};
7286          if (defined $opt{token} and defined $opt{token}->{line}) {
7287            $line = $opt{token}->{line};
7288            $column = $opt{token}->{column};
7289          }
7290          warn "Parse error ($opt{type}) at line $line column $column\n";
7291      };      };
7292      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7293        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7294      };      };
7295            
7296      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7297      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7298    
7299      ## Step 2      ## Step 2
7300      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7301      $p->{content_model} = {      $p->{content_model} = {
7302        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7303        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5386  sub set_inner_html ($$$) { Line 7314  sub set_inner_html ($$$) {
7314          unless defined $p->{content_model};          unless defined $p->{content_model};
7315          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7316    
7317      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7318          ## TODO: Foreign element OK?
7319    
7320      ## Step 4      ## Step 3
7321      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7322        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7323    
7324      ## Step 5 # MUST      ## Step 4 # MUST
7325      $doc->append_child ($root);      $doc->append_child ($root);
7326    
7327      ## Step 6 # MUST      ## Step 5 # MUST
7328      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7329    
7330      undef $p->{head_element};      undef $p->{head_element};
7331    
7332      ## Step 7 # MUST      ## Step 6 # MUST
7333      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7334    
7335      ## Step 8 # MUST      ## Step 7 # MUST
7336      my $anode = $node;      my $anode = $node;
7337      AN: while (defined $anode) {      AN: while (defined $anode) {
7338        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7339          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7340          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7341            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7342                !!!cp ('i5');
7343              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7344              last AN;              last AN;
7345            }            }
# Line 5418  sub set_inner_html ($$$) { Line 7348  sub set_inner_html ($$$) {
7348        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7349      } # AN      } # AN
7350            
7351      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7352      {      {
7353        my $self = $p;        my $self = $p;
7354        !!!next-token;        !!!next-token;
7355      }      }
7356      $p->_tree_construction_main;      $p->_tree_construction_main;
7357    
7358      ## Step 11 # MUST      ## Step 10 # MUST
7359      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7360      for (@cn) {      for (@cn) {
7361        $node->remove_child ($_);        $node->remove_child ($_);
7362      }      }
7363      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7364    
7365      ## Step 12 # MUST      ## Step 11 # MUST
7366      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7367      for (@cn) {      for (@cn) {
7368        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5442  sub set_inner_html ($$$) { Line 7371  sub set_inner_html ($$$) {
7371      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7372    
7373      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7374    
7375        delete $p->{parse_error}; # delete loop
7376    } else {    } else {
7377      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";
7378    }    }

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24