/[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.78 by wakaba, Mon Mar 3 11:56:18 2008 UTC revision 1.153 by wakaba, Fri Aug 15 08:32:41 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  require IO::Handle;
12  ## TODO: 1252 parse error (revision 1264)  
13  ## TODO: 8859-11 = 874 (revision 1271)  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    hr => 1,  
20    br => 1,  sub A_EL () { 0b1 }
21    img => 1,  sub ADDRESS_EL () { 0b10 }
22    embed => 1,  sub BODY_EL () { 0b100 }
23    param => 1,  sub BUTTON_EL () { 0b1000 }
24    area => 1,  sub CAPTION_EL () { 0b10000 }
25    col => 1,  sub DD_EL () { 0b100000 }
26    input => 1,  sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296  };  };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
350  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$) {
358    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
359    my $charset = shift;    my $charset_name = shift;
360    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
361    
362    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
363      my $self = shift;      my (%opt) = @_;
364      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
365      ## TODO: if $charset is supported    };
366      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
367    
368      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
369      require Message::Charset::Info;
370      ## Step 1        my $charset;
371      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?    my $buffer;
372        $charset = 'utf-8';    my ($char_stream, $e_status);
373    
374      SNIFFING: {
375    
376        ## Step 1
377        if (defined $charset_name) {
378          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
379    
380          ## ISSUE: Unsupported encoding is not ignored according to the spec.
381          ($char_stream, $e_status) = $charset->get_decode_handle
382              ($byte_stream, allow_error_reporting => 1,
383               allow_fallback => 1);
384          if ($char_stream) {
385            $self->{confident} = 1;
386            last SNIFFING;
387          } else {
388            ## TODO: unsupported error
389          }
390      }      }
391    
392      ## Step 2      ## Step 2
393      if (defined $self->{input_encoding} and      my $byte_buffer = '';
394          $self->{input_encoding} eq $charset) {      for (1..1024) {
395          my $char = $byte_stream->getc;
396          last unless defined $char;
397          $byte_buffer .= $char;
398        } ## TODO: timeout
399    
400        ## Step 3
401        if ($byte_buffer =~ /^\xFE\xFF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
403          ($char_stream, $e_status) = $charset->get_decode_handle
404              ($byte_stream, allow_error_reporting => 1,
405               allow_fallback => 1, byte_buffer => \$byte_buffer);
406        $self->{confident} = 1;        $self->{confident} = 1;
407        return;        last SNIFFING;
408        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
409          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
410          ($char_stream, $e_status) = $charset->get_decode_handle
411              ($byte_stream, allow_error_reporting => 1,
412               allow_fallback => 1, byte_buffer => \$byte_buffer);
413          $self->{confident} = 1;
414          last SNIFFING;
415        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
416          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
417          ($char_stream, $e_status) = $charset->get_decode_handle
418              ($byte_stream, allow_error_reporting => 1,
419               allow_fallback => 1, byte_buffer => \$byte_buffer);
420          $self->{confident} = 1;
421          last SNIFFING;
422      }      }
423    
424      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
425          ':'.$charset, level => 'w');      ## TODO: <meta charset>
426    
427      ## Step 3      ## Step 5
428      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
429    
430      ## Step 4      ## Step 6
431      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
432        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
433            ($byte_buffer);
434        if (defined $charset_name) {
435          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
436    
437          ## ISSUE: Unsupported encoding is not ignored according to the spec.
438          require Whatpm::Charset::DecodeHandle;
439          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
440              ($byte_stream);
441          ($char_stream, $e_status) = $charset->get_decode_handle
442              ($buffer, allow_error_reporting => 1,
443               allow_fallback => 1, byte_buffer => \$byte_buffer);
444          if ($char_stream) {
445            $buffer->{buffer} = $byte_buffer;
446            !!!parse-error (type => 'sniffing:chardet',
447                            text => $charset_name,
448                            level => $self->{level}->{info},
449                            layer => 'encode',
450                            line => 1, column => 1);
451            $self->{confident} = 0;
452            last SNIFFING;
453          }
454        }
455    
456        ## Step 7: default
457        ## TODO: Make this configurable.
458        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
459            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
460            ## detectable in the step 6.
461        require Whatpm::Charset::DecodeHandle;
462        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
463            ($byte_stream);
464        ($char_stream, $e_status)
465            = $charset->get_decode_handle ($buffer,
466                                           allow_error_reporting => 1,
467                                           allow_fallback => 1,
468                                           byte_buffer => \$byte_buffer);
469        $buffer->{buffer} = $byte_buffer;
470        !!!parse-error (type => 'sniffing:default',
471                        text => 'windows-1252',
472                        level => $self->{level}->{info},
473                        line => 1, column => 1,
474                        layer => 'encode');
475        $self->{confident} = 0;
476      } # SNIFFING
477    
478      $self->{input_encoding} = $charset->get_iana_name;
479      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
480        !!!parse-error (type => 'chardecode:fallback',
481                        text => $self->{input_encoding},
482                        level => $self->{level}->{uncertain},
483                        line => 1, column => 1,
484                        layer => 'encode');
485      } elsif (not ($e_status &
486                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
487        !!!parse-error (type => 'chardecode:no error',
488                        text => $self->{input_encoding},
489                        level => $self->{level}->{uncertain},
490                        line => 1, column => 1,
491                        layer => 'encode');
492      }
493    
494      $self->{change_encoding} = sub {
495        my $self = shift;
496        $charset_name = shift;
497        my $token = shift;
498    
499        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
500        ($char_stream, $e_status) = $charset->get_decode_handle
501            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
502             byte_buffer => \ $buffer->{buffer});
503        
504        if ($char_stream) { # if supported
505          ## "Change the encoding" algorithm:
506    
507          ## Step 1    
508          if ($charset->{category} &
509              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
510            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
511            ($char_stream, $e_status) = $charset->get_decode_handle
512                ($byte_stream,
513                 byte_buffer => \ $buffer->{buffer});
514          }
515          $charset_name = $charset->get_iana_name;
516          
517          ## Step 2
518          if (defined $self->{input_encoding} and
519              $self->{input_encoding} eq $charset_name) {
520            !!!parse-error (type => 'charset label:matching',
521                            text => $charset_name,
522                            level => $self->{level}->{info});
523            $self->{confident} = 1;
524            return;
525          }
526    
527          !!!parse-error (type => 'charset label detected',
528                          text => $self->{input_encoding},
529                          value => $charset_name,
530                          level => $self->{level}->{warn},
531                          token => $token);
532          
533          ## Step 3
534          # if (can) {
535            ## change the encoding on the fly.
536            #$self->{confident} = 1;
537            #return;
538          # }
539          
540          ## Step 4
541          throw Whatpm::HTML::RestartParser ();
542        }
543    }; # $self->{change_encoding}    }; # $self->{change_encoding}
544    
545      my $char_onerror = sub {
546        my (undef, $type, %opt) = @_;
547        !!!parse-error (layer => 'encode',
548                        %opt, type => $type,
549                        line => $self->{line}, column => $self->{column} + 1);
550        if ($opt{octets}) {
551          ${$opt{octets}} = "\x{FFFD}"; # relacement character
552        }
553      };
554      $char_stream->onerror ($char_onerror);
555    
556    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
557    my $return;    my $return;
558    try {    try {
559      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
560    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
561      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
562      $s = \ (Encode::decode ($charset, $$bytes_s));      
563      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
564        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
565          !!!parse-error (type => 'chardecode:fallback',
566                          text => $self->{input_encoding},
567                          level => $self->{level}->{uncertain},
568                          line => 1, column => 1,
569                          layer => 'encode');
570        } elsif (not ($e_status &
571                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
572          !!!parse-error (type => 'chardecode:no error',
573                          text => $self->{input_encoding},
574                          level => $self->{level}->{uncertain},
575                          line => 1, column => 1,
576                          layer => 'encode');
577        }
578      $self->{confident} = 1;      $self->{confident} = 1;
579      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
580        $return = $self->parse_char_stream ($char_stream, @args);
581    };    };
582    return $return;    return $return;
583  } # parse_byte_string  } # parse_byte_stream
584    
585  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
586  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 162  sub parse_byte_string ($$$$;$) { Line 591  sub parse_byte_string ($$$$;$) {
591  ## such as |parse_byte_string| in this module, must ensure that it does  ## such as |parse_byte_string| in this module, must ensure that it does
592  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
593    
594  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
595      my $self = shift;
596      require utf8;
597      my $s = ref $_[0] ? $_[0] : \($_[0]);
598      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
599      return $self->parse_char_stream ($input, @_[1..$#_]);
600    } # parse_char_string
601    *parse_string = \&parse_char_string;
602    
603  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
604    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
605    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
606    $self->{document} = $_[1];    $self->{document} = $_[1];
607    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
608    
# Line 177  sub parse_string ($$$;$) { Line 613  sub parse_string ($$$;$) {
613        if defined $self->{input_encoding};        if defined $self->{input_encoding};
614    
615    my $i = 0;    my $i = 0;
616    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
617    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
618    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
619      my $self = shift;      my $self = shift;
620    
621      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
622      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
623    
624      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
625      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
626      $column++;        $char = $self->{next_next_char};
627          delete $self->{next_next_char};
628        } else {
629          $char = $input->getc;
630        }
631        $self->{next_char} = -1 and return unless defined $char;
632        $self->{next_char} = ord $char;
633    
634        ($self->{line_prev}, $self->{column_prev})
635            = ($self->{line}, $self->{column});
636        $self->{column}++;
637            
638      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
639        $line++;        !!!cp ('j1');
640        $column = 0;        $self->{line}++;
641          $self->{column} = 0;
642      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
643        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
644          my $next = $input->getc;
645          if (defined $next and $next ne "\x0A") {
646            $self->{next_next_char} = $next;
647          }
648        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
649        $line++;        $self->{line}++;
650        $column = 0;        $self->{column} = 0;
651      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
652          !!!cp ('j3');
653        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
654      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
655          !!!cp ('j4');
656        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
657        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
658        } elsif ($self->{next_char} <= 0x0008 or
659                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
660                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
661                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
662                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
663                 {
664                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
665                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
666                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
667                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
668                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
669                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
670                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
671                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
672                  0x10FFFE => 1, 0x10FFFF => 1,
673                 }->{$self->{next_char}}) {
674          !!!cp ('j5');
675          if ($self->{next_char} < 0x10000) {
676            !!!parse-error (type => 'control char',
677                            text => (sprintf 'U+%04X', $self->{next_char}));
678          } else {
679            !!!parse-error (type => 'control char',
680                            text => (sprintf 'U-%08X', $self->{next_char}));
681          }
682      }      }
683    };    };
684    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 686  sub parse_string ($$$;$) {
686    
687    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
688      my (%opt) = @_;      my (%opt) = @_;
689      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
690        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
691        warn "Parse error ($opt{type}) at line $line column $column\n";
692    };    };
693    $self->{parse_error} = sub {    $self->{parse_error} = sub {
694      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
695    };    };
696    
697    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 699  sub parse_string ($$$;$) {
699    $self->_construct_tree;    $self->_construct_tree;
700    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
701    
702      delete $self->{parse_error}; # remove loop
703    
704    return $self->{document};    return $self->{document};
705  } # parse_string  } # parse_char_stream
706    
707  sub new ($) {  sub new ($) {
708    my $class = shift;    my $class = shift;
709    my $self = bless {}, $class;    my $self = bless {
710        level => {must => 'm',
711                  warn => 'w',
712                  info => 'i',
713                  uncertain => 'u'},
714      }, $class;
715    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
716      $self->{next_char} = -1;      $self->{next_char} = -1;
717    };    };
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 773  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
773  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
774  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
775  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
776    sub SELF_CLOSING_START_TAG_STATE () { 34 }
777    sub CDATA_BLOCK_STATE () { 35 }
778    
779  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
780  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 791  sub TABLE_IMS ()      { 0b1000000 }
791  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
792  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
793  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
794    sub SELECT_IMS ()     { 0b10000000000 }
795    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
796        ## NOTE: "in foreign content" insertion mode is special; it is combined
797        ## with the secondary insertion mode.  In this parser, they are stored
798        ## together in the bit-or'ed form.
799    
800    ## NOTE: "initial" and "before html" insertion modes have no constants.
801    
802    ## NOTE: "after after body" insertion mode.
803  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
804    
805    ## NOTE: "after after frameset" insertion mode.
806  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
807    
808  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
809  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
810  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 319  sub IN_TABLE_IM () { TABLE_IMS } Line 818  sub IN_TABLE_IM () { TABLE_IMS }
818  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
819  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
820  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
821  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
822    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
823  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
824    
825  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 332  sub _initialize_tokenizer ($) { Line 832  sub _initialize_tokenizer ($) {
832    undef $self->{current_attribute};    undef $self->{current_attribute};
833    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
834    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
835      delete $self->{self_closing};
836    $self->{char} = [];    $self->{char} = [];
837    # $self->{next_char}    # $self->{next_char}
838    !!!next-input-character;    !!!next-input-character;
# Line 352  sub _initialize_tokenizer ($) { Line 853  sub _initialize_tokenizer ($) {
853  ##        ->{value}  ##        ->{value}
854  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
855  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
856    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
857    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
858    ##     while the token is pushed back to the stack.
859    
860  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
861    
# Line 378  sub _initialize_tokenizer ($) { Line 882  sub _initialize_tokenizer ($) {
882    
883  sub _get_next_token ($) {  sub _get_next_token ($) {
884    my $self = shift;    my $self = shift;
885    
886      if ($self->{self_closing}) {
887        !!!parse-error (type => 'nestc', token => $self->{current_token});
888        ## NOTE: The |self_closing| flag is only set by start tag token.
889        ## In addition, when a start tag token is emitted, it is always set to
890        ## |current_token|.
891        delete $self->{self_closing};
892      }
893    
894    if (@{$self->{token}}) {    if (@{$self->{token}}) {
895        $self->{self_closing} = $self->{token}->[0]->{self_closing};
896      return shift @{$self->{token}};      return shift @{$self->{token}};
897    }    }
898    
# Line 441  sub _get_next_token ($) { Line 955  sub _get_next_token ($) {
955          #          #
956        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
957          !!!cp (11);          !!!cp (11);
958          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
959                      line => $self->{line}, column => $self->{column}});
960          last A; ## TODO: ok?          last A; ## TODO: ok?
961        } else {        } else {
962          !!!cp (12);          !!!cp (12);
963        }        }
964        # Anything else        # Anything else
965        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
966                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
967                       line => $self->{line}, column => $self->{column},
968                      };
969        ## Stay in the data state        ## Stay in the data state
970        !!!next-input-character;        !!!next-input-character;
971    
# Line 457  sub _get_next_token ($) { Line 974  sub _get_next_token ($) {
974        redo A;        redo A;
975      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
976        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
977    
978          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
979                
980        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
981    
# Line 465  sub _get_next_token ($) { Line 984  sub _get_next_token ($) {
984    
985        unless (defined $token) {        unless (defined $token) {
986          !!!cp (13);          !!!cp (13);
987          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
988                      line => $l, column => $c,
989                     });
990        } else {        } else {
991          !!!cp (14);          !!!cp (14);
992          !!!emit ($token);          !!!emit ($token);
# Line 484  sub _get_next_token ($) { Line 1005  sub _get_next_token ($) {
1005            ## reconsume            ## reconsume
1006            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1007    
1008            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1009                        line => $self->{line_prev},
1010                        column => $self->{column_prev},
1011                       });
1012    
1013            redo A;            redo A;
1014          }          }
# Line 504  sub _get_next_token ($) { Line 1028  sub _get_next_token ($) {
1028            !!!cp (19);            !!!cp (19);
1029            $self->{current_token}            $self->{current_token}
1030              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1031                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1032                   line => $self->{line_prev},
1033                   column => $self->{column_prev}};
1034            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1035            !!!next-input-character;            !!!next-input-character;
1036            redo A;            redo A;
# Line 512  sub _get_next_token ($) { Line 1038  sub _get_next_token ($) {
1038                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1039            !!!cp (20);            !!!cp (20);
1040            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1041                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1042                                        line => $self->{line_prev},
1043                                        column => $self->{column_prev}};
1044            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1045            !!!next-input-character;            !!!next-input-character;
1046            redo A;            redo A;
1047          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1048            !!!cp (21);            !!!cp (21);
1049            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1050                              line => $self->{line_prev},
1051                              column => $self->{column_prev});
1052            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1053            !!!next-input-character;            !!!next-input-character;
1054    
1055            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1056                        line => $self->{line_prev},
1057                        column => $self->{column_prev},
1058                       });
1059    
1060            redo A;            redo A;
1061          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1062            !!!cp (22);            !!!cp (22);
1063            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1064                              line => $self->{line_prev},
1065                              column => $self->{column_prev});
1066            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1067              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1068                                        line => $self->{line_prev},
1069                                        column => $self->{column_prev},
1070                                       };
1071            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1072            redo A;            redo A;
1073          } else {          } else {
1074            !!!cp (23);            !!!cp (23);
1075            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1076                              line => $self->{line_prev},
1077                              column => $self->{column_prev});
1078            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1079            ## reconsume            ## reconsume
1080    
1081            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1082                        line => $self->{line_prev},
1083                        column => $self->{column_prev},
1084                       });
1085    
1086            redo A;            redo A;
1087          }          }
# Line 545  sub _get_next_token ($) { Line 1089  sub _get_next_token ($) {
1089          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1090        }        }
1091      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1092          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1093        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1094          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1095    
1096            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1097            my @next_char;            my @next_char;
1098            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
# Line 563  sub _get_next_token ($) { Line 1109  sub _get_next_token ($) {
1109                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1110                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1111    
1112                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1113                            line => $l, column => $c,
1114                           });
1115        
1116                redo A;                redo A;
1117              }              }
# Line 582  sub _get_next_token ($) { Line 1130  sub _get_next_token ($) {
1130              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
1131              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1132              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1133              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1134                          line => $l, column => $c,
1135                         });
1136              redo A;              redo A;
1137            } else {            } else {
1138              !!!cp (27);              !!!cp (27);
# Line 595  sub _get_next_token ($) { Line 1145  sub _get_next_token ($) {
1145            !!!cp (28);            !!!cp (28);
1146            # next-input-character is already done            # next-input-character is already done
1147            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1148            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1149                        line => $l, column => $c,
1150                       });
1151            redo A;            redo A;
1152          }          }
1153        }        }
# Line 603  sub _get_next_token ($) { Line 1155  sub _get_next_token ($) {
1155        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1156            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1157          !!!cp (29);          !!!cp (29);
1158          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1159                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1160                   tag_name => chr ($self->{next_char} + 0x0020),
1161                   line => $l, column => $c};
1162          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1163          !!!next-input-character;          !!!next-input-character;
1164          redo A;          redo A;
# Line 612  sub _get_next_token ($) { Line 1166  sub _get_next_token ($) {
1166                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1167          !!!cp (30);          !!!cp (30);
1168          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1169                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1170                                      line => $l, column => $c};
1171          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1172          !!!next-input-character;          !!!next-input-character;
1173          redo A;          redo A;
1174        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1175          !!!cp (31);          !!!cp (31);
1176          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1177                            line => $self->{line_prev}, ## "<" in "</>"
1178                            column => $self->{column_prev} - 1);
1179          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1180          !!!next-input-character;          !!!next-input-character;
1181          redo A;          redo A;
# Line 628  sub _get_next_token ($) { Line 1185  sub _get_next_token ($) {
1185          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1186          # reconsume          # reconsume
1187    
1188          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1189                      line => $l, column => $c,
1190                     });
1191    
1192          redo A;          redo A;
1193        } else {        } else {
1194          !!!cp (33);          !!!cp (33);
1195          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1196          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1197            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1198                                      line => $self->{line_prev}, # "<" of "</"
1199                                      column => $self->{column_prev} - 1,
1200                                     };
1201          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1202          redo A;          redo A;
1203        }        }
# Line 651  sub _get_next_token ($) { Line 1214  sub _get_next_token ($) {
1214        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1215          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1216            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1217            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1218          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1219            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 684  sub _get_next_token ($) { Line 1245  sub _get_next_token ($) {
1245          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1246          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1247            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1248            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1249          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1250            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 706  sub _get_next_token ($) { Line 1265  sub _get_next_token ($) {
1265    
1266          redo A;          redo A;
1267        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1268            !!!cp (42);
1269            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1270          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (42);  
           #  
         } else {  
           !!!cp (43);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1271          redo A;          redo A;
1272        } else {        } else {
1273          !!!cp (44);          !!!cp (44);
# Line 741  sub _get_next_token ($) { Line 1290  sub _get_next_token ($) {
1290        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1291          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1292            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1293            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1294          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1295            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 764  sub _get_next_token ($) { Line 1311  sub _get_next_token ($) {
1311        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1312                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1313          !!!cp (49);          !!!cp (49);
1314          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1315                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1316                   value => '',
1317                   line => $self->{line}, column => $self->{column}};
1318          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1319          !!!next-input-character;          !!!next-input-character;
1320          redo A;          redo A;
1321        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1322            !!!cp (50);
1323            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1324          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (50);  
           #  
         } else {  
           !!!cp (51);  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
1325          redo A;          redo A;
1326        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1327          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1328          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1329            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1330            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1331          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1332            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 819  sub _get_next_token ($) { Line 1356  sub _get_next_token ($) {
1356          } else {          } else {
1357            !!!cp (56);            !!!cp (56);
1358          }          }
1359          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1360                                value => ''};              = {name => chr ($self->{next_char}),
1361                   value => '',
1362                   line => $self->{line}, column => $self->{column}};
1363          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1364          !!!next-input-character;          !!!next-input-character;
1365          redo A;          redo A;
# Line 830  sub _get_next_token ($) { Line 1369  sub _get_next_token ($) {
1369          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1370              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1371            !!!cp (57);            !!!cp (57);
1372            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1373            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1374          } else {          } else {
1375            !!!cp (58);            !!!cp (58);
# Line 859  sub _get_next_token ($) { Line 1398  sub _get_next_token ($) {
1398          $before_leave->();          $before_leave->();
1399          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1400            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1401            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1402          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1403            !!!cp (62);            !!!cp (62);
# Line 885  sub _get_next_token ($) { Line 1422  sub _get_next_token ($) {
1422          !!!next-input-character;          !!!next-input-character;
1423          redo A;          redo A;
1424        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1425            !!!cp (64);
1426          $before_leave->();          $before_leave->();
1427            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1428          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (64);  
           #  
         } else {  
           !!!cp (65);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1429          redo A;          redo A;
1430        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1431          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1432          $before_leave->();          $before_leave->();
1433          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1434            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1435            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1436          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1437            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 957  sub _get_next_token ($) { Line 1482  sub _get_next_token ($) {
1482        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1483          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1484            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1485            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1486          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1487            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 981  sub _get_next_token ($) { Line 1504  sub _get_next_token ($) {
1504        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1505                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1506          !!!cp (76);          !!!cp (76);
1507          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1508                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1509                   value => '',
1510                   line => $self->{line}, column => $self->{column}};
1511          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1512          !!!next-input-character;          !!!next-input-character;
1513          redo A;          redo A;
1514        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1515            !!!cp (77);
1516            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1517          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (77);  
           #  
         } else {  
           !!!cp (78);  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1518          redo A;          redo A;
1519        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1520          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1521          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1522            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1523            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1524          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1525            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1029  sub _get_next_token ($) { Line 1541  sub _get_next_token ($) {
1541          redo A;          redo A;
1542        } else {        } else {
1543          !!!cp (82);          !!!cp (82);
1544          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1545                                value => ''};              = {name => chr ($self->{next_char}),
1546                   value => '',
1547                   line => $self->{line}, column => $self->{column}};
1548          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1549          !!!next-input-character;          !!!next-input-character;
1550          redo A;                  redo A;        
# Line 1063  sub _get_next_token ($) { Line 1577  sub _get_next_token ($) {
1577        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1578          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1579            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1580            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1581          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1582            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1088  sub _get_next_token ($) { Line 1600  sub _get_next_token ($) {
1600          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1601          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1602            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1603            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1604          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1605            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1137  sub _get_next_token ($) { Line 1647  sub _get_next_token ($) {
1647          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1648          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1649            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1650            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1651          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1652            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1181  sub _get_next_token ($) { Line 1689  sub _get_next_token ($) {
1689          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1690          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1691            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1692            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1693          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1694            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1228  sub _get_next_token ($) { Line 1734  sub _get_next_token ($) {
1734        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1735          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1736            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1737            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1738          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1739            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1253  sub _get_next_token ($) { Line 1757  sub _get_next_token ($) {
1757          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1758          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1759            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1760            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1761          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1762            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1325  sub _get_next_token ($) { Line 1827  sub _get_next_token ($) {
1827        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1828          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1829            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1830            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1831          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1832            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1347  sub _get_next_token ($) { Line 1847  sub _get_next_token ($) {
1847    
1848          redo A;          redo A;
1849        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1850            !!!cp (122);
1851            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1852          !!!next-input-character;          !!!next-input-character;
1853          if ($self->{next_char} == 0x003E and # >          redo A;
1854              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1855              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1856            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1857            !!!cp (122);            !!!cp (122.3);
1858            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1859            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1860              if ($self->{current_token}->{attributes}) {
1861                !!!cp (122.1);
1862                !!!parse-error (type => 'end tag attribute');
1863              } else {
1864                ## NOTE: This state should never be reached.
1865                !!!cp (122.2);
1866              }
1867          } else {          } else {
1868            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1869          }          }
1870          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1871          # next-input-character is already done          ## Reconsume.
1872            !!!emit ($self->{current_token}); # start tag or end tag
1873          redo A;          redo A;
1874        } else {        } else {
1875          !!!cp (124);          !!!cp ('124.1');
1876          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1877          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1878          ## reconsume          ## reconsume
1879          redo A;          redo A;
1880        }        }
1881        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1882          if ($self->{next_char} == 0x003E) { # >
1883            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1884              !!!cp ('124.2');
1885              !!!parse-error (type => 'nestc', token => $self->{current_token});
1886              ## TODO: Different type than slash in start tag
1887              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1888              if ($self->{current_token}->{attributes}) {
1889                !!!cp ('124.4');
1890                !!!parse-error (type => 'end tag attribute');
1891              } else {
1892                !!!cp ('124.5');
1893              }
1894              ## TODO: Test |<title></title/>|
1895            } else {
1896              !!!cp ('124.3');
1897              $self->{self_closing} = 1;
1898            }
1899    
1900            $self->{state} = DATA_STATE;
1901            !!!next-input-character;
1902    
1903            !!!emit ($self->{current_token}); # start tag or end tag
1904    
1905            redo A;
1906          } elsif ($self->{next_char} == -1) {
1907            !!!parse-error (type => 'unclosed tag');
1908            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1909              !!!cp (124.7);
1910              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1911            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1912              if ($self->{current_token}->{attributes}) {
1913                !!!cp (124.5);
1914                !!!parse-error (type => 'end tag attribute');
1915              } else {
1916                ## NOTE: This state should never be reached.
1917                !!!cp (124.6);
1918              }
1919            } else {
1920              die "$0: $self->{current_token}->{type}: Unknown token type";
1921            }
1922            $self->{state} = DATA_STATE;
1923            ## Reconsume.
1924            !!!emit ($self->{current_token}); # start tag or end tag
1925            redo A;
1926          } else {
1927            !!!cp ('124.4');
1928            !!!parse-error (type => 'nestc');
1929            ## TODO: This error type is wrong.
1930            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1931            ## Reconsume.
1932            redo A;
1933          }
1934      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1935        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1936                
1937        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1938          #my $token = {type => COMMENT_TOKEN, data => ''};
1939    
1940        BC: {        BC: {
1941          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1379  sub _get_next_token ($) { Line 1943  sub _get_next_token ($) {
1943            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1944            !!!next-input-character;            !!!next-input-character;
1945    
1946            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1947    
1948            redo A;            redo A;
1949          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1387  sub _get_next_token ($) { Line 1951  sub _get_next_token ($) {
1951            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1952            ## reconsume            ## reconsume
1953    
1954            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1955    
1956            redo A;            redo A;
1957          } else {          } else {
1958            !!!cp (126);            !!!cp (126);
1959            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1960            !!!next-input-character;            !!!next-input-character;
1961            redo BC;            redo BC;
1962          }          }
# Line 1402  sub _get_next_token ($) { Line 1966  sub _get_next_token ($) {
1966      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1967        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1968    
1969          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1970    
1971        my @next_char;        my @next_char;
1972        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1973                
# Line 1410  sub _get_next_token ($) { Line 1976  sub _get_next_token ($) {
1976          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1977          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1978            !!!cp (127);            !!!cp (127);
1979            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1980                                        line => $l, column => $c,
1981                                       };
1982            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1983            !!!next-input-character;            !!!next-input-character;
1984            redo A;            redo A;
# Line 1446  sub _get_next_token ($) { Line 2014  sub _get_next_token ($) {
2014                      !!!cp (129);                      !!!cp (129);
2015                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
2016                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
2017                        $self->{current_token} = {type => DOCTYPE_TOKEN,
2018                                                  quirks => 1,
2019                                                  line => $l, column => $c,
2020                                                 };
2021                      !!!next-input-character;                      !!!next-input-character;
2022                      redo A;                      redo A;
2023                    } else {                    } else {
# Line 1466  sub _get_next_token ($) { Line 2038  sub _get_next_token ($) {
2038          } else {          } else {
2039            !!!cp (135);            !!!cp (135);
2040          }          }
2041          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2042                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2043                   $self->{next_char} == 0x005B) { # [
2044            !!!next-input-character;
2045            push @next_char, $self->{next_char};
2046            if ($self->{next_char} == 0x0043) { # C
2047              !!!next-input-character;
2048              push @next_char, $self->{next_char};
2049              if ($self->{next_char} == 0x0044) { # D
2050                !!!next-input-character;
2051                push @next_char, $self->{next_char};
2052                if ($self->{next_char} == 0x0041) { # A
2053                  !!!next-input-character;
2054                  push @next_char, $self->{next_char};
2055                  if ($self->{next_char} == 0x0054) { # T
2056                    !!!next-input-character;
2057                    push @next_char, $self->{next_char};
2058                    if ($self->{next_char} == 0x0041) { # A
2059                      !!!next-input-character;
2060                      push @next_char, $self->{next_char};
2061                      if ($self->{next_char} == 0x005B) { # [
2062                        !!!cp (135.1);
2063                        $self->{state} = CDATA_BLOCK_STATE;
2064                        !!!next-input-character;
2065                        redo A;
2066                      } else {
2067                        !!!cp (135.2);
2068                      }
2069                    } else {
2070                      !!!cp (135.3);
2071                    }
2072                  } else {
2073                    !!!cp (135.4);                
2074                  }
2075                } else {
2076                  !!!cp (135.5);
2077                }
2078              } else {
2079                !!!cp (135.6);
2080              }
2081            } else {
2082              !!!cp (135.7);
2083            }
2084        } else {        } else {
2085          !!!cp (136);          !!!cp (136);
2086        }        }
# Line 1474  sub _get_next_token ($) { Line 2089  sub _get_next_token ($) {
2089        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
2090        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2091        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2092          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2093                                    line => $l, column => $c,
2094                                   };
2095        redo A;        redo A;
2096                
2097        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 1597  sub _get_next_token ($) { Line 2215  sub _get_next_token ($) {
2215          redo A;          redo A;
2216        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2217          !!!cp (152);          !!!cp (152);
2218          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2219                            line => $self->{line_prev},
2220                            column => $self->{column_prev});
2221          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2222          ## Stay in the state          ## Stay in the state
2223          !!!next-input-character;          !!!next-input-character;
# Line 1613  sub _get_next_token ($) { Line 2233  sub _get_next_token ($) {
2233          redo A;          redo A;
2234        } else {        } else {
2235          !!!cp (154);          !!!cp (154);
2236          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2237                            line => $self->{line_prev},
2238                            column => $self->{column_prev});
2239          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2240          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2241          !!!next-input-character;          !!!next-input-character;
# Line 1652  sub _get_next_token ($) { Line 2274  sub _get_next_token ($) {
2274          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2275          !!!next-input-character;          !!!next-input-character;
2276    
2277          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2278    
2279          redo A;          redo A;
2280        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1661  sub _get_next_token ($) { Line 2283  sub _get_next_token ($) {
2283          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2284          ## reconsume          ## reconsume
2285    
2286          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2287    
2288          redo A;          redo A;
2289        } else {        } else {
2290          !!!cp (160);          !!!cp (160);
2291          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2292              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2293  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2294          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2295          !!!next-input-character;          !!!next-input-character;
# Line 2061  sub _get_next_token ($) { Line 2680  sub _get_next_token ($) {
2680          redo A;          redo A;
2681        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2682          !!!cp (208);          !!!cp (208);
2683          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2684    
2685          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2686          !!!next-input-character;          !!!next-input-character;
# Line 2097  sub _get_next_token ($) { Line 2716  sub _get_next_token ($) {
2716          redo A;          redo A;
2717        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2718          !!!cp (212);          !!!cp (212);
2719          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2720    
2721          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2722          !!!next-input-character;          !!!next-input-character;
# Line 2145  sub _get_next_token ($) { Line 2764  sub _get_next_token ($) {
2764        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2765          !!!cp (217);          !!!cp (217);
2766          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2767          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2768          ## reconsume          ## reconsume
2769    
# Line 2186  sub _get_next_token ($) { Line 2804  sub _get_next_token ($) {
2804          !!!next-input-character;          !!!next-input-character;
2805          redo A;          redo A;
2806        }        }
2807        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2808          my $s = '';
2809          
2810          my ($l, $c) = ($self->{line}, $self->{column});
2811    
2812          CS: while ($self->{next_char} != -1) {
2813            if ($self->{next_char} == 0x005D) { # ]
2814              !!!next-input-character;
2815              if ($self->{next_char} == 0x005D) { # ]
2816                !!!next-input-character;
2817                MDC: {
2818                  if ($self->{next_char} == 0x003E) { # >
2819                    !!!cp (221.1);
2820                    !!!next-input-character;
2821                    last CS;
2822                  } elsif ($self->{next_char} == 0x005D) { # ]
2823                    !!!cp (221.2);
2824                    $s .= ']';
2825                    !!!next-input-character;
2826                    redo MDC;
2827                  } else {
2828                    !!!cp (221.3);
2829                    $s .= ']]';
2830                    #
2831                  }
2832                } # MDC
2833              } else {
2834                !!!cp (221.4);
2835                $s .= ']';
2836                #
2837              }
2838            } else {
2839              !!!cp (221.5);
2840              #
2841            }
2842            $s .= chr $self->{next_char};
2843            !!!next-input-character;
2844          } # CS
2845    
2846          $self->{state} = DATA_STATE;
2847          ## next-input-character done or EOF, which is reconsumed.
2848    
2849          if (length $s) {
2850            !!!cp (221.6);
2851            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2852                      line => $l, column => $c});
2853          } else {
2854            !!!cp (221.7);
2855          }
2856    
2857          redo A;
2858    
2859          ## ISSUE: "text tokens" in spec.
2860          ## TODO: Streaming support
2861      } else {      } else {
2862        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2863      }      }
# Line 2197  sub _get_next_token ($) { Line 2869  sub _get_next_token ($) {
2869  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2870    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2871    
2872      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2873    
2874    if ({    if ({
2875         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2876         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
# Line 2237  sub _tokenize_attempt_to_consume_an_enti Line 2911  sub _tokenize_attempt_to_consume_an_enti
2911            redo X;            redo X;
2912          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2913            !!!cp (1005);            !!!cp (1005);
2914            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2915            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2916            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2917            return undef;            return undef;
# Line 2246  sub _tokenize_attempt_to_consume_an_enti Line 2920  sub _tokenize_attempt_to_consume_an_enti
2920            !!!next-input-character;            !!!next-input-character;
2921          } else {          } else {
2922            !!!cp (1007);            !!!cp (1007);
2923            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2924          }          }
2925    
2926          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2927            !!!cp (1008);            !!!cp (1008);
2928            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => 'invalid character reference',
2929                              text => (sprintf 'U+%04X', $code),
2930                              line => $l, column => $c);
2931            $code = 0xFFFD;            $code = 0xFFFD;
2932          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2933            !!!cp (1009);            !!!cp (1009);
2934            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => 'invalid character reference',
2935                              text => (sprintf 'U-%08X', $code),
2936                              line => $l, column => $c);
2937            $code = 0xFFFD;            $code = 0xFFFD;
2938          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2939            !!!cp (1010);            !!!cp (1010);
2940            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2941            $code = 0x000A;            $code = 0x000A;
2942          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2943            !!!cp (1011);            !!!cp (1011);
2944            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
2945            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2946          }          }
2947    
2948          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2949                  has_reference => 1};                  has_reference => 1,
2950                    line => $l, column => $c,
2951                   };
2952        } # X        } # X
2953      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2954               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2289  sub _tokenize_attempt_to_consume_an_enti Line 2969  sub _tokenize_attempt_to_consume_an_enti
2969          !!!next-input-character;          !!!next-input-character;
2970        } else {        } else {
2971          !!!cp (1014);          !!!cp (1014);
2972          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2973        }        }
2974    
2975        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2976          !!!cp (1015);          !!!cp (1015);
2977          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => 'invalid character reference',
2978                            text => (sprintf 'U+%04X', $code),
2979                            line => $l, column => $c);
2980          $code = 0xFFFD;          $code = 0xFFFD;
2981        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2982          !!!cp (1016);          !!!cp (1016);
2983          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => 'invalid character reference',
2984                            text => (sprintf 'U-%08X', $code),
2985                            line => $l, column => $c);
2986          $code = 0xFFFD;          $code = 0xFFFD;
2987        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2988          !!!cp (1017);          !!!cp (1017);
2989          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference',
2990                            line => $l, column => $c);
2991          $code = 0x000A;          $code = 0x000A;
2992        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2993          !!!cp (1018);          !!!cp (1018);
2994          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => 'C1 character reference',
2995                            text => (sprintf 'U+%04X', $code),
2996                            line => $l, column => $c);
2997          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2998        }        }
2999                
3000        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3001                  line => $l, column => $c,
3002                 };
3003      } else {      } else {
3004        !!!cp (1019);        !!!cp (1019);
3005        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
3006        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
3007        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
3008        return undef;        return undef;
# Line 2330  sub _tokenize_attempt_to_consume_an_enti Line 3019  sub _tokenize_attempt_to_consume_an_enti
3019      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3020      our $EntityChar;      our $EntityChar;
3021    
3022      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3023             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3024             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
3025               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2363  sub _tokenize_attempt_to_consume_an_enti Line 3052  sub _tokenize_attempt_to_consume_an_enti
3052            
3053      if ($match > 0) {      if ($match > 0) {
3054        !!!cp (1023);        !!!cp (1023);
3055        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3056                  line => $l, column => $c,
3057                 };
3058      } elsif ($match < 0) {      } elsif ($match < 0) {
3059        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3060        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3061          !!!cp (1024);          !!!cp (1024);
3062          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3063                    line => $l, column => $c,
3064                   };
3065        } else {        } else {
3066          !!!cp (1025);          !!!cp (1025);
3067          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3068                    line => $l, column => $c,
3069                   };
3070        }        }
3071      } else {      } else {
3072        !!!cp (1026);        !!!cp (1026);
3073        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3074        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3075        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
3076                  line => $l, column => $c,
3077                 };
3078      }      }
3079    } else {    } else {
3080      !!!cp (1027);      !!!cp (1027);
3081      ## no characters are consumed      ## no characters are consumed
3082      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3083      return undef;      return undef;
3084    }    }
3085  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2420  sub _construct_tree ($) { Line 3117  sub _construct_tree ($) {
3117        
3118    !!!next-token;    !!!next-token;
3119    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3120    undef $self->{form_element};    undef $self->{form_element};
3121    undef $self->{head_element};    undef $self->{head_element};
3122    $self->{open_elements} = [];    $self->{open_elements} = [];
3123    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3124    
3125      ## NOTE: The "initial" insertion mode.
3126    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3127    
3128      ## NOTE: The "before html" insertion mode.
3129    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3130      $self->{insertion_mode} = BEFORE_HEAD_IM;
3131    
3132      ## NOTE: The "before head" insertion mode and so on.
3133    $self->_tree_construction_main;    $self->_tree_construction_main;
3134  } # _construct_tree  } # _construct_tree
3135    
3136  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3137    my $self = shift;    my $self = shift;
3138    
3139      ## NOTE: "initial" insertion mode
3140    
3141    INITIAL: {    INITIAL: {
3142      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3143        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2444  sub _tree_construction_initial ($) { Line 3149  sub _tree_construction_initial ($) {
3149        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3150            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3151            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3152          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3153            !!!parse-error (type => 'not HTML5', token => $token);
3154        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3155            !!!cp ('t2');
3156          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3157          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3158          } else {
3159            !!!cp ('t3');
3160        }        }
3161                
3162        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3163          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3164          ## NOTE: Default value for both |public_id| and |system_id| attributes
3165          ## are empty strings, so that we don't set any value in missing cases.
3166        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3167            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3168        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2461  sub _tree_construction_initial ($) { Line 3172  sub _tree_construction_initial ($) {
3172        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3173                
3174        if ($token->{quirks} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3175            !!!cp ('t4');
3176          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3177        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3178          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3179          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3180          if ({          my $prefix = [
3181            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3182            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3183            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3184            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3185            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3186            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3187            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3188            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3189            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3190            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3191            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3192            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3193            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3194            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3195            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3196            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3197            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3198            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3199            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3200            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3201            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3202            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3203            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3204            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3205            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3206            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3207            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3208            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3209            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3210            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3211            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3212            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3213            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3214            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3215            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3216            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3217            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3218            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3219            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3220            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3221            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3222            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3223            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3224            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3225            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3226            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3227            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3228            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3229            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3230            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3231            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3232            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3233            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3234            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3235            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3236            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3237            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3238            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3239            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3240            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3241            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3242            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3243            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3244            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3245            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3246            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3247            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,              $pubid eq "HTML") {
3248            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,            !!!cp ('t5');
           "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,  
           "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,  
           "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,  
           "HTML" => 1,  
         }->{$pubid}) {  
3249            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3250          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3251                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3252            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3253                !!!cp ('t6');
3254              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3255            } else {            } else {
3256                !!!cp ('t7');
3257              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3258            }            }
3259          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3260                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3261              !!!cp ('t8');
3262            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3263            } else {
3264              !!!cp ('t9');
3265          }          }
3266          } else {
3267            !!!cp ('t10');
3268        }        }
3269        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3270          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3271          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3272          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3273              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3274              ## marked as quirks.
3275            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3276              !!!cp ('t11');
3277            } else {
3278              !!!cp ('t12');
3279          }          }
3280          } else {
3281            !!!cp ('t13');
3282        }        }
3283                
3284        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3285        !!!next-token;        !!!next-token;
3286        return;        return;
3287      } elsif ({      } elsif ({
# Line 2568  sub _tree_construction_initial ($) { Line 3289  sub _tree_construction_initial ($) {
3289                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3290                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3291               }->{$token->{type}}) {               }->{$token->{type}}) {
3292        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3293          !!!parse-error (type => 'no DOCTYPE', token => $token);
3294        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3295        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3296        ## reprocess        ## reprocess
3297          !!!ack-later;
3298        return;        return;
3299      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3300        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3301          ## Ignore the token          ## Ignore the token
3302    
3303          unless (length $token->{data}) {          unless (length $token->{data}) {
3304            ## Stay in the phase            !!!cp ('t15');
3305              ## Stay in the insertion mode.
3306            !!!next-token;            !!!next-token;
3307            redo INITIAL;            redo INITIAL;
3308            } else {
3309              !!!cp ('t16');
3310          }          }
3311          } else {
3312            !!!cp ('t17');
3313        }        }
3314    
3315        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3316        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3317        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3318        ## reprocess        ## reprocess
3319        return;        return;
3320      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3321          !!!cp ('t18');
3322        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3323        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3324                
3325        ## Stay in the phase.        ## Stay in the insertion mode.
3326        !!!next-token;        !!!next-token;
3327        redo INITIAL;        redo INITIAL;
3328      } else {      } else {
3329        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3330      }      }
3331    } # INITIAL    } # INITIAL
3332    
3333      die "$0: _tree_construction_initial: This should be never reached";
3334  } # _tree_construction_initial  } # _tree_construction_initial
3335    
3336  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3337    my $self = shift;    my $self = shift;
3338    
3339      ## NOTE: "before html" insertion mode.
3340        
3341    B: {    B: {
3342        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3343          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3344            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3345          ## Ignore the token          ## Ignore the token
3346          ## Stay in the phase          ## Stay in the insertion mode.
3347          !!!next-token;          !!!next-token;
3348          redo B;          redo B;
3349        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3350            !!!cp ('t20');
3351          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3352          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3353          ## Stay in the phase          ## Stay in the insertion mode.
3354          !!!next-token;          !!!next-token;
3355          redo B;          redo B;
3356        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2623  sub _tree_construction_root_element ($) Line 3358  sub _tree_construction_root_element ($)
3358            ## Ignore the token.            ## Ignore the token.
3359    
3360            unless (length $token->{data}) {            unless (length $token->{data}) {
3361              ## Stay in the phase              !!!cp ('t21');
3362                ## Stay in the insertion mode.
3363              !!!next-token;              !!!next-token;
3364              redo B;              redo B;
3365              } else {
3366                !!!cp ('t22');
3367            }            }
3368            } else {
3369              !!!cp ('t23');
3370          }          }
3371    
3372          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
3373    
3374          #          #
3375        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3376          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3377              $token->{attributes}->{manifest}) {            my $root_element;
3378            $self->{application_cache_selection}            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3379                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
3380            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
3381                  [$root_element, $el_category->{html}];
3382    
3383              if ($token->{attributes}->{manifest}) {
3384                !!!cp ('t24');
3385                $self->{application_cache_selection}
3386                    ->($token->{attributes}->{manifest}->{value});
3387                ## ISSUE: Spec is unclear on relative references.
3388                ## According to Hixie (#whatwg 2008-03-19), it should be
3389                ## resolved against the base URI of the document in HTML
3390                ## or xml:base of the element in XHTML.
3391              } else {
3392                !!!cp ('t25');
3393                $self->{application_cache_selection}->(undef);
3394              }
3395    
3396              !!!nack ('t25c');
3397    
3398              !!!next-token;
3399              return; ## Go to the "before head" insertion mode.
3400          } else {          } else {
3401            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
3402              #
3403          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3404        } elsif ({        } elsif ({
3405                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3406                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3407                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3408          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
3409          #          #
3410        } else {        } else {
3411          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3412        }        }
3413    
3414        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3415        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3416        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3417        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3418        #redo B;  
3419        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3420    
3421        ## NOTE: Reprocess the token.
3422        !!!ack-later;
3423        return; ## Go to the "before head" insertion mode.
3424    
3425        ## ISSUE: There is an issue in the spec
3426    } # B    } # B
3427    
3428      die "$0: _tree_construction_root_element: This should never be reached";
3429  } # _tree_construction_root_element  } # _tree_construction_root_element
3430    
3431  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2677  sub _reset_insertion_mode ($) { Line 3440  sub _reset_insertion_mode ($) {
3440            
3441      ## Step 3      ## Step 3
3442      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
3443        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3444          $last = 1;          $last = 1;
3445          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3446            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3447                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3448              #          } else {
3449            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3450          }          }
3451        }        }
3452              
3453        ## Step 4..13        ## Step 4..14
3454        my $new_mode = {        my $new_mode;
3455          if ($node->[1] & FOREIGN_EL) {
3456            !!!cp ('t28.1');
3457            ## NOTE: Strictly spaking, the line below only applies to MathML and
3458            ## SVG elements.  Currently the HTML syntax supports only MathML and
3459            ## SVG elements as foreigners.
3460            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3461          } elsif ($node->[1] & TABLE_CELL_EL) {
3462            if ($last) {
3463              !!!cp ('t28.2');
3464              #
3465            } else {
3466              !!!cp ('t28.3');
3467              $new_mode = IN_CELL_IM;
3468            }
3469          } else {
3470            !!!cp ('t28.4');
3471            $new_mode = {
3472                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3473                        td => IN_CELL_IM,                        ## NOTE: |option| and |optgroup| do not set
3474                        th => IN_CELL_IM,                        ## insertion mode to "in select" by themselves.
3475                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3476                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3477                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2709  sub _reset_insertion_mode ($) { Line 3482  sub _reset_insertion_mode ($) {
3482                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3483                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3484                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3485                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3486          }
3487        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3488                
3489        ## Step 14        ## Step 15
3490        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3491          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3492              !!!cp ('t29');
3493            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3494          } else {          } else {
3495              ## ISSUE: Can this state be reached?
3496              !!!cp ('t30');
3497            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3498          }          }
3499          return;          return;
3500          } else {
3501            !!!cp ('t31');
3502        }        }
3503                
3504        ## Step 15        ## Step 16
3505        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3506                
3507        ## Step 16        ## Step 17
3508        $i--;        $i--;
3509        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3510                
3511        ## Step 17        ## Step 18
3512        redo S3;        redo S3;
3513      } # S3      } # S3
3514    
3515      die "$0: _reset_insertion_mode: This line should never be reached";
3516  } # _reset_insertion_mode  } # _reset_insertion_mode
3517    
3518  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2753  sub _tree_construction_main ($) { Line 3534  sub _tree_construction_main ($) {
3534      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3535      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3536        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3537            !!!cp ('t32');
3538          return;          return;
3539        }        }
3540      }      }
# Line 2767  sub _tree_construction_main ($) { Line 3549  sub _tree_construction_main ($) {
3549    
3550        ## Step 6        ## Step 6
3551        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3552            !!!cp ('t33_1');
3553          #          #
3554        } else {        } else {
3555          my $in_open_elements;          my $in_open_elements;
3556          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3557            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3558                !!!cp ('t33');
3559              $in_open_elements = 1;              $in_open_elements = 1;
3560              last OE;              last OE;
3561            }            }
3562          }          }
3563          if ($in_open_elements) {          if ($in_open_elements) {
3564              !!!cp ('t34');
3565            #            #
3566          } else {          } else {
3567              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3568              !!!cp ('t35');
3569            redo S4;            redo S4;
3570          }          }
3571        }        }
# Line 2801  sub _tree_construction_main ($) { Line 3588  sub _tree_construction_main ($) {
3588    
3589        ## Step 11        ## Step 11
3590        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3591            !!!cp ('t36');
3592          ## Step 7'          ## Step 7'
3593          $i++;          $i++;
3594          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3595                    
3596          redo S7;          redo S7;
3597        }        }
3598    
3599          !!!cp ('t37');
3600      } # S7      } # S7
3601    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3602    
3603    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3604      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3605        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3606            !!!cp ('t38');
3607          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3608          return;          return;
3609        }        }
3610      }      }
3611    
3612        !!!cp ('t39');
3613    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3614    
3615    my $parse_rcdata = sub ($$) {    my $insert;
3616      my ($content_model_flag, $insert) = @_;  
3617      my $parse_rcdata = sub ($) {
3618        my ($content_model_flag) = @_;
3619    
3620      ## Step 1      ## Step 1
3621      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3622      my $el;      my $el;
3623      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3624    
3625      ## Step 2      ## Step 2
3626      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3627    
3628      ## Step 3      ## Step 3
3629      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2836  sub _tree_construction_main ($) { Line 3631  sub _tree_construction_main ($) {
3631    
3632      ## Step 4      ## Step 4
3633      my $text = '';      my $text = '';
3634        !!!nack ('t40.1');
3635      !!!next-token;      !!!next-token;
3636      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3637          !!!cp ('t40');
3638        $text .= $token->{data};        $text .= $token->{data};
3639        !!!next-token;        !!!next-token;
3640      }      }
3641    
3642      ## Step 5      ## Step 5
3643      if (length $text) {      if (length $text) {
3644          !!!cp ('t41');
3645        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3646        $el->append_child ($text);        $el->append_child ($text);
3647      }      }
# Line 2852  sub _tree_construction_main ($) { Line 3650  sub _tree_construction_main ($) {
3650      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3651    
3652      ## Step 7      ## Step 7
3653      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3654            $token->{tag_name} eq $start_tag_name) {
3655          !!!cp ('t42');
3656        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3657      } else {      } else {
3658        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3659          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3660            !!!cp ('t43');
3661            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3662          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3663            !!!cp ('t44');
3664            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3665          } else {
3666            die "$0: $content_model_flag in parse_rcdata";
3667          }
3668      }      }
3669      !!!next-token;      !!!next-token;
3670    }; # $parse_rcdata    }; # $parse_rcdata
3671    
3672    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3673      my $script_el;      my $script_el;
3674      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3675      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3676    
3677      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3678      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3679            
3680      my $text = '';      my $text = '';
3681        !!!nack ('t45.1');
3682      !!!next-token;      !!!next-token;
3683      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3684          !!!cp ('t45');
3685        $text .= $token->{data};        $text .= $token->{data};
3686        !!!next-token;        !!!next-token;
3687      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3688      if (length $text) {      if (length $text) {
3689          !!!cp ('t46');
3690        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3691      }      }
3692                                
# Line 2887  sub _tree_construction_main ($) { Line 3694  sub _tree_construction_main ($) {
3694    
3695      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3696          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3697          !!!cp ('t47');
3698        ## Ignore the token        ## Ignore the token
3699      } else {      } else {
3700        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3701          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3702        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3703        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3704      }      }
3705            
3706      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3707          !!!cp ('t49');
3708        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3709      } else {      } else {
3710          !!!cp ('t50');
3711        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3712        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3713    
# Line 2910  sub _tree_construction_main ($) { Line 3721  sub _tree_construction_main ($) {
3721      !!!next-token;      !!!next-token;
3722    }; # $script_start_tag    }; # $script_start_tag
3723    
3724      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3725      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3726      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3727    
3728    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3729      my $tag_name = shift;      my $end_tag_token = shift;
3730        my $tag_name = $end_tag_token->{tag_name};
3731    
3732        ## NOTE: The adoption agency algorithm (AAA).
3733    
3734      FET: {      FET: {
3735        ## Step 1        ## Step 1
3736        my $formatting_element;        my $formatting_element;
3737        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3738        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3739          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3740              !!!cp ('t52');
3741              last AFE;
3742            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3743                         eq $tag_name) {
3744              !!!cp ('t51');
3745            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3746            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3747            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3748          }          }
3749        } # AFE        } # AFE
3750        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3751          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3752            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3753          ## Ignore the token          ## Ignore the token
3754          !!!next-token;          !!!next-token;
3755          return;          return;
# Line 2939  sub _tree_construction_main ($) { Line 3761  sub _tree_construction_main ($) {
3761          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3762          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3763            if ($in_scope) {            if ($in_scope) {
3764                !!!cp ('t54');
3765              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3766              last INSCOPE;              last INSCOPE;
3767            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3768              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3769                !!!parse-error (type => 'unmatched end tag',
3770                                text => $token->{tag_name},
3771                                token => $end_tag_token);
3772              ## Ignore the token              ## Ignore the token
3773              !!!next-token;              !!!next-token;
3774              return;              return;
3775            }            }
3776          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3777                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3778            $in_scope = 0;            $in_scope = 0;
3779          }          }
3780        } # INSCOPE        } # INSCOPE
3781        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3782          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3783            !!!parse-error (type => 'unmatched end tag',
3784                            text => $token->{tag_name},
3785                            token => $end_tag_token);
3786          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3787          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3788          return;          return;
3789        }        }
3790        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3791          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3792            !!!parse-error (type => 'not closed',
3793                            text => $self->{open_elements}->[-1]->[0]
3794                                ->manakai_local_name,
3795                            token => $end_tag_token);
3796        }        }
3797                
3798        ## Step 2        ## Step 2
# Line 2969  sub _tree_construction_main ($) { Line 3800  sub _tree_construction_main ($) {
3800        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3801        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3802          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3803          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3804              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3805              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3806               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3807              !!!cp ('t59');
3808            $furthest_block = $node;            $furthest_block = $node;
3809            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3810          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3811              !!!cp ('t60');
3812            last OE;            last OE;
3813          }          }
3814        } # OE        } # OE
3815                
3816        ## Step 3        ## Step 3
3817        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3818            !!!cp ('t61');
3819          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3820          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3821          !!!next-token;          !!!next-token;
# Line 2994  sub _tree_construction_main ($) { Line 3828  sub _tree_construction_main ($) {
3828        ## Step 5        ## Step 5
3829        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3830        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3831            !!!cp ('t62');
3832          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3833        }        }
3834                
# Line 3016  sub _tree_construction_main ($) { Line 3851  sub _tree_construction_main ($) {
3851          S7S2: {          S7S2: {
3852            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3853              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3854                  !!!cp ('t63');
3855                $node_i_in_active = $_;                $node_i_in_active = $_;
3856                last S7S2;                last S7S2;
3857              }              }
# Line 3029  sub _tree_construction_main ($) { Line 3865  sub _tree_construction_main ($) {
3865                    
3866          ## Step 4          ## Step 4
3867          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3868              !!!cp ('t64');
3869            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3870          }          }
3871                    
3872          ## Step 5          ## Step 5
3873          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3874              !!!cp ('t65');
3875            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3876            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3877            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 3051  sub _tree_construction_main ($) { Line 3889  sub _tree_construction_main ($) {
3889        } # S7          } # S7  
3890                
3891        ## Step 8        ## Step 8
3892        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3893            my $foster_parent_element;
3894            my $next_sibling;
3895            OE: for (reverse 0..$#{$self->{open_elements}}) {
3896              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3897                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3898                                 if (defined $parent and $parent->node_type == 1) {
3899                                   !!!cp ('t65.1');
3900                                   $foster_parent_element = $parent;
3901                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3902                                 } else {
3903                                   !!!cp ('t65.2');
3904                                   $foster_parent_element
3905                                     = $self->{open_elements}->[$_ - 1]->[0];
3906                                 }
3907                                 last OE;
3908                               }
3909                             } # OE
3910                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3911                               unless defined $foster_parent_element;
3912            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3913            $open_tables->[-1]->[1] = 1; # tainted
3914          } else {
3915            !!!cp ('t65.3');
3916            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3917          }
3918                
3919        ## Step 9        ## Step 9
3920        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3068  sub _tree_construction_main ($) { Line 3931  sub _tree_construction_main ($) {
3931        my $i;        my $i;
3932        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3933          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3934              !!!cp ('t66');
3935            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3936            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3937          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3938              !!!cp ('t67');
3939            $i = $_;            $i = $_;
3940          }          }
3941        } # AFE        } # AFE
# Line 3080  sub _tree_construction_main ($) { Line 3945  sub _tree_construction_main ($) {
3945        undef $i;        undef $i;
3946        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3947          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3948              !!!cp ('t68');
3949            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3950            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3951          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3952              !!!cp ('t69');
3953            $i = $_;            $i = $_;
3954          }          }
3955        } # OE        } # OE
# Line 3093  sub _tree_construction_main ($) { Line 3960  sub _tree_construction_main ($) {
3960      } # FET      } # FET
3961    }; # $formatting_end_tag    }; # $formatting_end_tag
3962    
3963    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3964      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3965    }; # $insert_to_current    }; # $insert_to_current
3966    
3967    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3968                         my $child = shift;      my $child = shift;
3969                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3970                              table => 1, tbody => 1, tfoot => 1,        # MUST
3971                              thead => 1, tr => 1,        my $foster_parent_element;
3972                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3973                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3974                           my $foster_parent_element;          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
                          my $next_sibling;  
                          OE: for (reverse 0..$#{$self->{open_elements}}) {  
                            if ($self->{open_elements}->[$_]->[1] eq 'table') {  
3975                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3976                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3977                                   !!!cp ('t70');
3978                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3979                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3980                               } else {                               } else {
3981                                   !!!cp ('t71');
3982                                 $foster_parent_element                                 $foster_parent_element
3983                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3984                               }                               }
# Line 3123  sub _tree_construction_main ($) { Line 3989  sub _tree_construction_main ($) {
3989                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3990                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3991                             ($child, $next_sibling);                             ($child, $next_sibling);
3992                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3993                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3994                         }        !!!cp ('t72');
3995          $self->{open_elements}->[-1]->[0]->append_child ($child);
3996        }
3997    }; # $insert_to_foster    }; # $insert_to_foster
3998    
3999    my $insert;    B: while (1) {
   
   B: {  
4000      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4001        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
4002          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4003        ## Ignore the token        ## Ignore the token
4004        ## Stay in the phase        ## Stay in the phase
4005        !!!next-token;        !!!next-token;
4006        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
4007      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4008               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4009        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4010          ## Turn into the main phase          !!!cp ('t79');
4011          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4012          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4013        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4014          ## Turn into the main phase          !!!cp ('t80');
4015          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4016          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4017          } else {
4018            !!!cp ('t81');
4019        }        }
4020    
4021  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
4022  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag', token => $token);
       unless ($token->{first_start_tag}) {  
         !!!parse-error (type => 'not first start tag');  
       }  
4023        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4024        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4025          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4026              !!!cp ('t84');
4027            $top_el->set_attribute_ns            $top_el->set_attribute_ns
4028              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
4029               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4030          }          }
4031        }        }
4032          !!!nack ('t84.1');
4033        !!!next-token;        !!!next-token;
4034        redo B;        next B;
4035      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4036        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4037        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4038            !!!cp ('t85');
4039          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
4040        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4041            !!!cp ('t86');
4042          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
4043        } else {        } else {
4044            !!!cp ('t87');
4045          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4046        }        }
4047        !!!next-token;        !!!next-token;
4048        redo B;        next B;
4049      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4050          if ($token->{type} == CHARACTER_TOKEN) {
4051            !!!cp ('t87.1');
4052            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4053            !!!next-token;
4054            next B;
4055          } elsif ($token->{type} == START_TAG_TOKEN) {
4056            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4057                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4058                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4059                ($token->{tag_name} eq 'svg' and
4060                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4061              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4062              !!!cp ('t87.2');
4063              #
4064            } elsif ({
4065                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4066                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4067                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4068                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4069                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4070                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4071                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4072                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4073                     }->{$token->{tag_name}}) {
4074              !!!cp ('t87.2');
4075              !!!parse-error (type => 'not closed',
4076                              text => $self->{open_elements}->[-1]->[0]
4077                                  ->manakai_local_name,
4078                              token => $token);
4079    
4080              pop @{$self->{open_elements}}
4081                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4082    
4083              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4084              ## Reprocess.
4085              next B;
4086            } else {
4087              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4088              my $tag_name = $token->{tag_name};
4089              if ($nsuri eq $SVG_NS) {
4090                $tag_name = {
4091                   altglyph => 'altGlyph',
4092                   altglyphdef => 'altGlyphDef',
4093                   altglyphitem => 'altGlyphItem',
4094                   animatecolor => 'animateColor',
4095                   animatemotion => 'animateMotion',
4096                   animatetransform => 'animateTransform',
4097                   clippath => 'clipPath',
4098                   feblend => 'feBlend',
4099                   fecolormatrix => 'feColorMatrix',
4100                   fecomponenttransfer => 'feComponentTransfer',
4101                   fecomposite => 'feComposite',
4102                   feconvolvematrix => 'feConvolveMatrix',
4103                   fediffuselighting => 'feDiffuseLighting',
4104                   fedisplacementmap => 'feDisplacementMap',
4105                   fedistantlight => 'feDistantLight',
4106                   feflood => 'feFlood',
4107                   fefunca => 'feFuncA',
4108                   fefuncb => 'feFuncB',
4109                   fefuncg => 'feFuncG',
4110                   fefuncr => 'feFuncR',
4111                   fegaussianblur => 'feGaussianBlur',
4112                   feimage => 'feImage',
4113                   femerge => 'feMerge',
4114                   femergenode => 'feMergeNode',
4115                   femorphology => 'feMorphology',
4116                   feoffset => 'feOffset',
4117                   fepointlight => 'fePointLight',
4118                   fespecularlighting => 'feSpecularLighting',
4119                   fespotlight => 'feSpotLight',
4120                   fetile => 'feTile',
4121                   feturbulence => 'feTurbulence',
4122                   foreignobject => 'foreignObject',
4123                   glyphref => 'glyphRef',
4124                   lineargradient => 'linearGradient',
4125                   radialgradient => 'radialGradient',
4126                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4127                   textpath => 'textPath',  
4128                }->{$tag_name} || $tag_name;
4129              }
4130    
4131              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4132    
4133              ## "adjust foreign attributes" - done in insert-element-f
4134    
4135              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4136    
4137              if ($self->{self_closing}) {
4138                pop @{$self->{open_elements}};
4139                !!!ack ('t87.3');
4140              } else {
4141                !!!cp ('t87.4');
4142              }
4143    
4144              !!!next-token;
4145              next B;
4146            }
4147          } elsif ($token->{type} == END_TAG_TOKEN) {
4148            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4149            !!!cp ('t87.5');
4150            #
4151          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4152            !!!cp ('t87.6');
4153            !!!parse-error (type => 'not closed',
4154                            text => $self->{open_elements}->[-1]->[0]
4155                                ->manakai_local_name,
4156                            token => $token);
4157    
4158            pop @{$self->{open_elements}}
4159                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4160    
4161            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4162            ## Reprocess.
4163            next B;
4164          } else {
4165            die "$0: $token->{type}: Unknown token type";        
4166          }
4167        }
4168    
4169        if ($self->{insertion_mode} & HEAD_IMS) {
4170        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4171          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4172            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4173                !!!cp ('t88.2');
4174                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4175              } else {
4176                !!!cp ('t88.1');
4177                ## Ignore the token.
4178                !!!next-token;
4179                next B;
4180              }
4181            unless (length $token->{data}) {            unless (length $token->{data}) {
4182                !!!cp ('t88');
4183              !!!next-token;              !!!next-token;
4184              redo B;              next B;
4185            }            }
4186          }          }
4187    
4188          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4189              !!!cp ('t89');
4190            ## As if <head>            ## As if <head>
4191            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4192            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4193            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4194                  [$self->{head_element}, $el_category->{head}];
4195    
4196            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4197            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4198    
4199            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4200          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4201              !!!cp ('t90');
4202            ## As if </noscript>            ## As if </noscript>
4203            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4204            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4205                        
4206            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4207            ## As if </head>            ## As if </head>
# Line 3234  sub _tree_construction_main ($) { Line 4209  sub _tree_construction_main ($) {
4209    
4210            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4211          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4212              !!!cp ('t91');
4213            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4214    
4215            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4216            } else {
4217              !!!cp ('t92');
4218          }          }
4219    
4220              ## "after head" insertion mode          ## "after head" insertion mode
4221              ## As if <body>          ## As if <body>
4222              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4223              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4224              ## reprocess          ## reprocess
4225              redo B;          next B;
4226            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4227              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4228                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4229                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4230                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4231                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4232                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4233                  !!!next-token;              push @{$self->{open_elements}},
4234                  redo B;                  [$self->{head_element}, $el_category->{head}];
4235                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4236                  #              !!!nack ('t93.1');
4237                } else {              !!!next-token;
4238                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4239                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4240                  !!!next-token;              !!!cp ('t93.2');
4241                  redo B;              !!!parse-error (type => 'after head', text => 'head',
4242                }                              token => $token);
4243              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              ## Ignore the token
4244                ## As if <head>              !!!nack ('t93.3');
4245                !!!create-element ($self->{head_element}, 'head');              !!!next-token;
4246                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              next B;
4247                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            } else {
4248                !!!cp ('t95');
4249                !!!parse-error (type => 'in head:head',
4250                                token => $token); # or in head noscript
4251                ## Ignore the token
4252                !!!nack ('t95.1');
4253                !!!next-token;
4254                next B;
4255              }
4256            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4257              !!!cp ('t96');
4258              ## As if <head>
4259              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4260              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4261              push @{$self->{open_elements}},
4262                  [$self->{head_element}, $el_category->{head}];
4263    
4264                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4265                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4266              }          } else {
4267              !!!cp ('t97');
4268            }
4269    
4270              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4271                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4272                    !!!cp ('t98');
4273                  ## As if </noscript>                  ## As if </noscript>
4274                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4275                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4276                                    token => $token);
4277                                
4278                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4279                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4280                  } else {
4281                    !!!cp ('t99');
4282                }                }
4283    
4284                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4285                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4286                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4287                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4288                                    text => $token->{tag_name}, token => $token);
4289                    push @{$self->{open_elements}},
4290                        [$self->{head_element}, $el_category->{head}];
4291                  } else {
4292                    !!!cp ('t101');
4293                }                }
4294                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4295                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4296                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4297                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4298                  !!!nack ('t101.1');
4299                !!!next-token;                !!!next-token;
4300                redo B;                next B;
4301              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4302                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4303                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4304                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4305                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4306                                    text => $token->{tag_name}, token => $token);
4307                    push @{$self->{open_elements}},
4308                        [$self->{head_element}, $el_category->{head}];
4309                  } else {
4310                    !!!cp ('t103');
4311                }                }
4312                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4313                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4314                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4315                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4316                  !!!ack ('t103.1');
4317                !!!next-token;                !!!next-token;
4318                redo B;                next B;
4319              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4320                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4321                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4322                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4323                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4324                                    text => $token->{tag_name}, token => $token);
4325                    push @{$self->{open_elements}},
4326                        [$self->{head_element}, $el_category->{head}];
4327                  } else {
4328                    !!!cp ('t105');
4329                }                }
4330                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4331                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4332    
4333                unless ($self->{confident}) {                unless ($self->{confident}) {
4334                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4335                      !!!cp ('t106');
4336                      ## NOTE: Whether the encoding is supported or not is handled
4337                      ## in the {change_encoding} callback.
4338                    $self->{change_encoding}                    $self->{change_encoding}
4339                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4340                             $token);
4341                                        
4342                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4343                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4344                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4345                                                 ->{has_reference});                                                 ->{has_reference});
4346                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4347                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4348                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4349                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4350                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4351                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4352                        !!!cp ('t107');
4353                        ## NOTE: Whether the encoding is supported or not is handled
4354                        ## in the {change_encoding} callback.
4355                      $self->{change_encoding}                      $self->{change_encoding}
4356                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4357                               $token);
4358                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4359                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4360                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
4361                                                     ->{has_reference});                                                     ->{has_reference});
4362                      } else {
4363                        !!!cp ('t108');
4364                    }                    }
4365                  }                  }
4366                } else {                } else {
4367                  if ($token->{attributes}->{charset}) {                  if ($token->{attributes}->{charset}) {
4368                      !!!cp ('t109');
4369                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4370                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4371                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4372                                                 ->{has_reference});                                                 ->{has_reference});
4373                  }                  }
4374                  if ($token->{attributes}->{content}) {                  if ($token->{attributes}->{content}) {
4375                      !!!cp ('t110');
4376                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4377                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4378                                             $token->{attributes}->{content}                                             $token->{attributes}->{content}
# Line 3353  sub _tree_construction_main ($) { Line 4380  sub _tree_construction_main ($) {
4380                  }                  }
4381                }                }
4382    
4383                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4384                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4385                  !!!ack ('t110.1');
4386                !!!next-token;                !!!next-token;
4387                redo B;                next B;
4388              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4389                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4390                    !!!cp ('t111');
4391                  ## As if </noscript>                  ## As if </noscript>
4392                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4393                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4394                                    token => $token);
4395                                
4396                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4397                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4398                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4399                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4400                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4401                                    text => $token->{tag_name}, token => $token);
4402                    push @{$self->{open_elements}},
4403                        [$self->{head_element}, $el_category->{head}];
4404                  } else {
4405                    !!!cp ('t113');
4406                }                }
4407    
4408                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4409                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4410                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4411                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4412                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4413                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4414                redo B;                next B;
4415              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4416                         $token->{tag_name} eq 'noframes') {
4417                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4418                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4419                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4420                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4421                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4422                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4423                                    text => $token->{tag_name}, token => $token);
4424                    push @{$self->{open_elements}},
4425                        [$self->{head_element}, $el_category->{head}];
4426                  } else {
4427                    !!!cp ('t115');
4428                }                }
4429                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4430                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4431                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4432                redo B;                next B;
4433              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4434                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4435                    !!!cp ('t116');
4436                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4437                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4438                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4439                    !!!nack ('t116.1');
4440                  !!!next-token;                  !!!next-token;
4441                  redo B;                  next B;
4442                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4443                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4444                    !!!parse-error (type => 'in noscript', text => 'noscript',
4445                                    token => $token);
4446                  ## Ignore the token                  ## Ignore the token
4447                    !!!nack ('t117.1');
4448                  !!!next-token;                  !!!next-token;
4449                  redo B;                  next B;
4450                } else {                } else {
4451                    !!!cp ('t118');
4452                  #                  #
4453                }                }
4454              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4455                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4456                    !!!cp ('t119');
4457                  ## As if </noscript>                  ## As if </noscript>
4458                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4459                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4460                                    token => $token);
4461                                
4462                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4463                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4464                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4465                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4466                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4467                                    text => $token->{tag_name}, token => $token);
4468                    push @{$self->{open_elements}},
4469                        [$self->{head_element}, $el_category->{head}];
4470                  } else {
4471                    !!!cp ('t121');
4472                }                }
4473    
4474                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4475                $script_start_tag->($insert_to_current);                $script_start_tag->();
4476                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4477                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4478                redo B;                next B;
4479              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4480                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4481                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4482                    !!!cp ('t122');
4483                  ## As if </noscript>                  ## As if </noscript>
4484                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4485                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4486                                    text => $token->{tag_name}, token => $token);
4487                                    
4488                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4489                  ## As if </head>                  ## As if </head>
# Line 3436  sub _tree_construction_main ($) { Line 4491  sub _tree_construction_main ($) {
4491                                    
4492                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4493                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4494                    !!!cp ('t124');
4495                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4496                                    
4497                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4498                  } else {
4499                    !!!cp ('t125');
4500                }                }
4501    
4502                ## "after head" insertion mode                ## "after head" insertion mode
4503                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4504                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4505                    !!!cp ('t126');
4506                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4507                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4508                    !!!cp ('t127');
4509                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4510                } else {                } else {
4511                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4512                }                }
4513                  !!!nack ('t127.1');
4514                !!!next-token;                !!!next-token;
4515                redo B;                next B;
4516              } else {              } else {
4517                  !!!cp ('t128');
4518                #                #
4519              }              }
4520    
4521              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4522                  !!!cp ('t129');
4523                ## As if </noscript>                ## As if </noscript>
4524                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4525                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4526                                  text => $token->{tag_name}, token => $token);
4527                                
4528                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4529                ## As if </head>                ## As if </head>
# Line 3467  sub _tree_construction_main ($) { Line 4531  sub _tree_construction_main ($) {
4531    
4532                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4533              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4534                  !!!cp ('t130');
4535                ## As if </head>                ## As if </head>
4536                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4537    
4538                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4539                } else {
4540                  !!!cp ('t131');
4541              }              }
4542    
4543              ## "after head" insertion mode              ## "after head" insertion mode
4544              ## As if <body>              ## As if <body>
4545              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4546              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4547              ## reprocess              ## reprocess
4548              redo B;              !!!ack-later;
4549                next B;
4550            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4551              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4552                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4553                    !!!cp ('t132');
4554                  ## As if <head>                  ## As if <head>
4555                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4556                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4557                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4558                        [$self->{head_element}, $el_category->{head}];
4559    
4560                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4561                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4562                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4563                  !!!next-token;                  !!!next-token;
4564                  redo B;                  next B;
4565                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4566                    !!!cp ('t133');
4567                  ## As if </noscript>                  ## As if </noscript>
4568                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4569                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/',
4570                                    text => 'head', token => $token);
4571                                    
4572                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4573                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4574                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4575                  !!!next-token;                  !!!next-token;
4576                  redo B;                  next B;
4577                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4578                    !!!cp ('t134');
4579                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4580                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4581                  !!!next-token;                  !!!next-token;
4582                  redo B;                  next B;
4583                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4584                    !!!cp ('t134.1');
4585                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4586                                    token => $token);
4587                    ## Ignore the token
4588                    !!!next-token;
4589                    next B;
4590                } else {                } else {
4591                  #                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4592                }                }
4593              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4594                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4595                    !!!cp ('t136');
4596                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4597                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4598                  !!!next-token;                  !!!next-token;
4599                  redo B;                  next B;
4600                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4601                  !!!parse-error (type => 'unmatched end tag:noscript');                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4602                    !!!cp ('t137');
4603                    !!!parse-error (type => 'unmatched end tag',
4604                                    text => 'noscript', token => $token);
4605                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4606                  !!!next-token;                  !!!next-token;
4607                  redo B;                  next B;
4608                } else {                } else {
4609                    !!!cp ('t138');
4610                  #                  #
4611                }                }
4612              } elsif ({              } elsif ({
4613                        body => 1, html => 1,                        body => 1, html => 1,
4614                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4615                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4616                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_IM or
4617                  !!!create-element ($self->{head_element}, 'head');                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4618                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  !!!cp ('t140');
4619                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'unmatched end tag',
4620                                    text => $token->{tag_name}, token => $token);
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4621                  ## Ignore the token                  ## Ignore the token
4622                  !!!next-token;                  !!!next-token;
4623                  redo B;                  next B;
4624                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4625                    !!!cp ('t140.1');
4626                    !!!parse-error (type => 'unmatched end tag',
4627                                    text => $token->{tag_name}, token => $token);
4628                    ## Ignore the token
4629                    !!!next-token;
4630                    next B;
4631                  } else {
4632                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4633                }                }
4634                              } elsif ($token->{tag_name} eq 'p') {
4635                #                !!!cp ('t142');
4636              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4637                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4638                       }->{$token->{tag_name}}) {                ## Ignore the token
4639                  !!!next-token;
4640                  next B;
4641                } elsif ($token->{tag_name} eq 'br') {
4642                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4643                  ## As if <head>                  !!!cp ('t142.2');
4644                  !!!create-element ($self->{head_element}, 'head');                  ## (before head) as if <head>, (in head) as if </head>
4645                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4646                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4647                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4648      
4649                    ## Reprocess in the "after head" insertion mode...
4650                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4651                    !!!cp ('t143.2');
4652                    ## As if </head>
4653                    pop @{$self->{open_elements}};
4654                    $self->{insertion_mode} = AFTER_HEAD_IM;
4655      
4656                    ## Reprocess in the "after head" insertion mode...
4657                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4658                    !!!cp ('t143.3');
4659                    ## ISSUE: Two parse errors for <head><noscript></br>
4660                    !!!parse-error (type => 'unmatched end tag',
4661                                    text => 'br', token => $token);
4662                    ## As if </noscript>
4663                    pop @{$self->{open_elements}};
4664                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4665    
4666                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4667                }                  ## As if </head>
4668                    pop @{$self->{open_elements}};
4669                    $self->{insertion_mode} = AFTER_HEAD_IM;
4670    
4671                #                  ## Reprocess in the "after head" insertion mode...
4672              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4673                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
4674                  #                  #
4675                } else {                } else {
4676                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4677                }                }
4678    
4679                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4680                  !!!parse-error (type => 'unmatched end tag',
4681                                  text => 'br', token => $token);
4682                  ## Ignore the token
4683                  !!!next-token;
4684                  next B;
4685                } else {
4686                  !!!cp ('t145');
4687                  !!!parse-error (type => 'unmatched end tag',
4688                                  text => $token->{tag_name}, token => $token);
4689                  ## Ignore the token
4690                  !!!next-token;
4691                  next B;
4692              }              }
4693    
4694              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4695                  !!!cp ('t146');
4696                ## As if </noscript>                ## As if </noscript>
4697                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4698                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4699                                  text => $token->{tag_name}, token => $token);
4700                                
4701                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4702                ## As if </head>                ## As if </head>
# Line 3579  sub _tree_construction_main ($) { Line 4704  sub _tree_construction_main ($) {
4704    
4705                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4706              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4707                  !!!cp ('t147');
4708                ## As if </head>                ## As if </head>
4709                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4710    
4711                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4712              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4713                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4714                  !!!cp ('t148');
4715                  !!!parse-error (type => 'unmatched end tag',
4716                                  text => $token->{tag_name}, token => $token);
4717                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4718                !!!next-token;                !!!next-token;
4719                redo B;                next B;
4720                } else {
4721                  !!!cp ('t149');
4722              }              }
4723    
4724              ## "after head" insertion mode              ## "after head" insertion mode
4725              ## As if <body>              ## As if <body>
4726              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4727              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4728              ## reprocess              ## reprocess
4729              redo B;              next B;
4730            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4731              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4732            }            !!!cp ('t149.1');
4733    
4734              ## NOTE: As if <head>
4735              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4736              $self->{open_elements}->[-1]->[0]->append_child
4737                  ($self->{head_element});
4738              #push @{$self->{open_elements}},
4739              #    [$self->{head_element}, $el_category->{head}];
4740              #$self->{insertion_mode} = IN_HEAD_IM;
4741              ## NOTE: Reprocess.
4742    
4743              ## NOTE: As if </head>
4744              #pop @{$self->{open_elements}};
4745              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4746              ## NOTE: Reprocess.
4747              
4748              #
4749            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4750              !!!cp ('t149.2');
4751    
4752              ## NOTE: As if </head>
4753              pop @{$self->{open_elements}};
4754              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4755              ## NOTE: Reprocess.
4756    
4757              #
4758            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4759              !!!cp ('t149.3');
4760    
4761              !!!parse-error (type => 'in noscript:#eof', token => $token);
4762    
4763              ## As if </noscript>
4764              pop @{$self->{open_elements}};
4765              #$self->{insertion_mode} = IN_HEAD_IM;
4766              ## NOTE: Reprocess.
4767    
4768              ## NOTE: As if </head>
4769              pop @{$self->{open_elements}};
4770              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4771              ## NOTE: Reprocess.
4772    
4773              #
4774            } else {