/[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.53 by wakaba, Sat Jul 21 12:37:57 2007 UTC revision 1.180 by wakaba, Sun Sep 14 14:35:43 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  require IO::Handle;
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  
13  ## is not yet clear.  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  ## "{U+FEFF}..." in GB18030?  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17  my $permitted_slash_tag_name = {  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    base => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    link => 1,  
20    meta => 1,  sub A_EL () { 0b1 }
21    hr => 1,  sub ADDRESS_EL () { 0b10 }
22    br => 1,  sub BODY_EL () { 0b100 }
23    img=> 1,  sub BUTTON_EL () { 0b1000 }
24    embed => 1,  sub CAPTION_EL () { 0b10000 }
25    param => 1,  sub DD_EL () { 0b100000 }
26    area => 1,  sub DIV_EL () { 0b1000000 }
27    col => 1,  sub DT_EL () { 0b10000000 }
28    input => 1,  sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296    };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311  };  };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 62  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
350  my $special_category = {  sub parse_byte_string ($$$$;$) {
351    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = shift;
352    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
353    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  } # parse_byte_string
356    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
357    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  sub parse_byte_stream ($$$$;$$) {
358    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $self = ref $_[0] ? shift : shift->new;
360    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    my $charset_name = shift;
361  };    my $byte_stream = $_[0];
362  my $scoping_category = {  
363    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    my $onerror = $_[2] || sub {
364    table => 1, td => 1, th => 1,      my (%opt) = @_;
365  };      warn "Parse error ($opt{type})\n";
366  my $formatting_category = {    };
367    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,    $self->{parse_error} = $onerror; # updated later by parse_char_string
368    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
369  };    my $get_wrapper = $_[3] || sub ($) {
370  # $phrasing_category: all other elements      return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      };
372    
373      ## HTML5 encoding sniffing algorithm
374      require Message::Charset::Info;
375      my $charset;
376      my $buffer;
377      my ($char_stream, $e_status);
378    
379      SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387        ## Step 1
388        if (defined $charset_name) {
389          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393          ## ISSUE: Unsupported encoding is not ignored according to the spec.
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1);
397          if ($char_stream) {
398            $self->{confident} = 1;
399            last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402          }
403        }
404    
405        ## Step 2
406        my $byte_buffer = '';
407        for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413        ## Step 3
414        if ($byte_buffer =~ /^\xFE\xFF/) {
415          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416          ($char_stream, $e_status) = $charset->get_decode_handle
417              ($byte_stream, allow_error_reporting => 1,
418               allow_fallback => 1, byte_buffer => \$byte_buffer);
419          $self->{confident} = 1;
420          last SNIFFING;
421        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435        }
436    
437        ## Step 4
438        ## TODO: <meta charset>
439    
440        ## Step 5
441        ## TODO: from history
442    
443        ## Step 6
444        require Whatpm::Charset::UniversalCharDet;
445        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446            ($byte_buffer);
447        if (defined $charset_name) {
448          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450          ## ISSUE: Unsupported encoding is not ignored according to the spec.
451          require Whatpm::Charset::DecodeHandle;
452          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453              ($byte_stream);
454          ($char_stream, $e_status) = $charset->get_decode_handle
455              ($buffer, allow_error_reporting => 1,
456               allow_fallback => 1, byte_buffer => \$byte_buffer);
457          if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            level => $self->{level}->{info},
462                            layer => 'encode',
463                            line => 1, column => 1);
464            $self->{confident} = 0;
465            last SNIFFING;
466          }
467        }
468    
469        ## Step 7: default
470        ## TODO: Make this configurable.
471        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473            ## detectable in the step 6.
474        require Whatpm::Charset::DecodeHandle;
475        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476            ($byte_stream);
477        ($char_stream, $e_status)
478            = $charset->get_decode_handle ($buffer,
479                                           allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        line => 1, column => 1,
487                        layer => 'encode');
488        $self->{confident} = 0;
489      } # SNIFFING
490    
491      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493        !!!parse-error (type => 'chardecode:fallback',
494                        #text => $self->{input_encoding},
495                        level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498      } elsif (not ($e_status &
499                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
500        $self->{input_encoding} = $charset->get_iana_name;
501        !!!parse-error (type => 'chardecode:no error',
502                        text => $self->{input_encoding},
503                        level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508      }
509    
510      $self->{change_encoding} = sub {
511        my $self = shift;
512        $charset_name = shift;
513        my $token = shift;
514    
515        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516        ($char_stream, $e_status) = $charset->get_decode_handle
517            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519        
520        if ($char_stream) { # if supported
521          ## "Change the encoding" algorithm:
522    
523          ## Step 1    
524          if ($charset->{category} &
525              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530          }
531          $charset_name = $charset->get_iana_name;
532          
533          ## Step 2
534          if (defined $self->{input_encoding} and
535              $self->{input_encoding} eq $charset_name) {
536            !!!parse-error (type => 'charset label:matching',
537                            text => $charset_name,
538                            level => $self->{level}->{info});
539            $self->{confident} = 1;
540            return;
541          }
542    
543          !!!parse-error (type => 'charset label detected',
544                          text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          token => $token);
548          
549          ## Step 3
550          # if (can) {
551            ## change the encoding on the fly.
552            #$self->{confident} = 1;
553            #return;
554          # }
555          
556          ## Step 4
557          throw Whatpm::HTML::RestartParser ();
558        }
559      }; # $self->{change_encoding}
560    
561  sub parse_string ($$$;$) {    my $char_onerror = sub {
562    my $self = shift->new;      my (undef, $type, %opt) = @_;
563    my $s = \$_[0];      !!!parse-error (layer => 'encode',
564                        line => $self->{line}, column => $self->{column} + 1,
565                        %opt, type => $type);
566        if ($opt{octets}) {
567          ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        }
569      };
570    
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574      my @args = @_; shift @args; # $s
575      my $return;
576      try {
577        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578      } catch Whatpm::HTML::RestartParser with {
579        ## NOTE: Invoked after {change_encoding}.
580    
581        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583          !!!parse-error (type => 'chardecode:fallback',
584                          level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588        } elsif (not ($e_status &
589                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
590          $self->{input_encoding} = $charset->get_iana_name;
591          !!!parse-error (type => 'chardecode:no error',
592                          text => $self->{input_encoding},
593                          level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598        }
599        $self->{confident} = 1;
600    
601        $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605      };
606      return $return;
607    } # parse_byte_stream
608    
609    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
611    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
612    ## because the core part of our HTML parser expects a string of character,
613    ## not a string of bytes or code units or anything which might contain a BOM.
614    ## Therefore, any parser interface that accepts a string of bytes,
615    ## such as |parse_byte_string| in this module, must ensure that it does
616    ## strip the BOM and never strip any ZWNBSP.
617    
618    sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      my $s = ref $_[0] ? $_[0] : \($_[0]);
622      require Whatpm::Charset::DecodeHandle;
623      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631    sub parse_char_stream ($$$;$) {
632      my $self = ref $_[0] ? shift : shift->new;
633      my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635      @{$self->{document}->child_nodes} = ();
636    
637    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
638    
639      $self->{confident} = 1 unless exists $self->{confident};
640      $self->{document}->input_encoding ($self->{input_encoding})
641          if defined $self->{input_encoding};
642    ## TODO: |{input_encoding}| is needless?
643    
644    my $i = 0;    my $i = 0;
645    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
646    my $column = 0;    $self->{column_prev} = -1;
647    $self->{set_next_input_character} = sub {    $self->{column} = 0;
648      $self->{set_next_char} = sub {
649      my $self = shift;      my $self = shift;
650    
651      pop @{$self->{prev_input_character}};      my $char = '';
652      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      if (defined $self->{next_next_char}) {
653          $char = $self->{next_next_char};
654          delete $self->{next_next_char};
655          $self->{next_char} = ord $char;
656        } else {
657          $self->{char_buffer} = '';
658          $self->{char_buffer_pos} = 0;
659    
660          my $count = $input->manakai_read_until
661             ($self->{char_buffer},
662              qr/(?![\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
663              $self->{char_buffer_pos});
664          if ($count) {
665            $self->{line_prev} = $self->{line};
666            $self->{column_prev} = $self->{column};
667            $self->{column}++;
668            $self->{next_char}
669                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
670            return;
671          }
672    
673          if ($input->read ($char, 1)) {
674            $self->{next_char} = ord $char;
675          } else {
676            $self->{next_char} = -1;
677            return;
678          }
679        }
680    
681      $self->{next_input_character} = -1 and return if $i >= length $$s;      ($self->{line_prev}, $self->{column_prev})
682      $self->{next_input_character} = ord substr $$s, $i++, 1;          = ($self->{line}, $self->{column});
683      $column++;      $self->{column}++;
684            
685      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
686        $line++;        !!!cp ('j1');
687        $column = 0;        $self->{line}++;
688      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
689        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
690        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
691        $line++;  ## TODO: support for abort/streaming
692        $column = 0;        my $next = '';
693      } elsif ($self->{next_input_character} > 0x10FFFF) {        if ($input->read ($next, 1) and $next ne "\x0A") {
694        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_next_char} = $next;
695      } elsif ($self->{next_input_character} == 0x0000) { # NULL        }
696          $self->{next_char} = 0x000A; # LF # MUST
697          $self->{line}++;
698          $self->{column} = 0;
699        } elsif ($self->{next_char} > 0x10FFFF) {
700          !!!cp ('j3');
701          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
702        } elsif ($self->{next_char} == 0x0000) { # NULL
703          !!!cp ('j4');
704        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
705        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
706        } elsif ($self->{next_char} <= 0x0008 or
707                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
708                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
709                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
710                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
711    ## ISSUE: U+FDE0-U+FDEF are not excluded
712                 {
713                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
714                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
715                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
716                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
717                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
718                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
719                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
720                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
721                  0x10FFFE => 1, 0x10FFFF => 1,
722                 }->{$self->{next_char}}) {
723          !!!cp ('j5');
724          if ($self->{next_char} < 0x10000) {
725            !!!parse-error (type => 'control char',
726                            text => (sprintf 'U+%04X', $self->{next_char}));
727          } else {
728            !!!parse-error (type => 'control char',
729                            text => (sprintf 'U-%08X', $self->{next_char}));
730          }
731      }      }
732    };    };
733    $self->{prev_input_character} = [-1, -1, -1];  
734    $self->{next_input_character} = -1;    $self->{read_until} = sub {
735        #my ($scalar, $specials_range, $offset) = @_;
736        return 0 if defined $self->{next_next_char};
737    
738        my $pattern = qr/(?![$_[1]\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/;
739        my $offset = $_[2] || 0;
740    
741        if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
742          pos ($self->{char_buffer}) = $self->{char_buffer_pos};
743          if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
744            substr ($_[0], $offset)
745                = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
746            my $count = $+[0] - $-[0];
747            if ($count) {
748              $self->{column} += $count;
749              $self->{char_buffer_pos} += $count;
750              $self->{line_prev} = $self->{line};
751              $self->{column_prev} = $self->{column} - 1;
752              $self->{prev_char} = [-1, -1, -1];
753              $self->{next_char} = -1;
754            }
755            return $count;
756          } else {
757            return 0;
758          }
759        } else {
760          my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
761          if ($count) {
762            $self->{column} += $count;
763            $self->{line_prev} = $self->{line};
764            $self->{column_prev} = $self->{column} - 1;
765            $self->{prev_char} = [-1, -1, -1];
766            $self->{next_char} = -1;
767          }
768          return $count;
769        }
770      }; # $self->{read_until}
771    
772    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
773      my (%opt) = @_;      my (%opt) = @_;
774      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
775        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
776        warn "Parse error ($opt{type}) at line $line column $column\n";
777    };    };
778    $self->{parse_error} = sub {    $self->{parse_error} = sub {
779      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
780    };    };
781    
782    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 784  sub parse_string ($$$;$) {
784    $self->_construct_tree;    $self->_construct_tree;
785    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
786    
787      delete $self->{parse_error}; # remove loop
788    
789    return $self->{document};    return $self->{document};
790  } # parse_string  } # parse_char_stream
791    
792  sub new ($) {  sub new ($) {
793    my $class = shift;    my $class = shift;
794    my $self = bless {}, $class;    my $self = bless {
795    $self->{set_next_input_character} = sub {      level => {must => 'm',
796      $self->{next_input_character} = -1;                should => 's',
797                  warn => 'w',
798                  info => 'i',
799                  uncertain => 'u'},
800      }, $class;
801      $self->{set_next_char} = sub {
802        $self->{next_char} = -1;
803    };    };
804    $self->{parse_error} = sub {    $self->{parse_error} = sub {
805      #      #
806    };    };
807      $self->{change_encoding} = sub {
808        # if ($_[0] is a supported encoding) {
809        #   run "change the encoding" algorithm;
810        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
811        # }
812      };
813      $self->{application_cache_selection} = sub {
814        #
815      };
816    return $self;    return $self;
817  } # new  } # new
818    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 825  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
825  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
826  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
827    
828    sub DATA_STATE () { 0 }
829    #sub ENTITY_DATA_STATE () { 1 }
830    sub TAG_OPEN_STATE () { 2 }
831    sub CLOSE_TAG_OPEN_STATE () { 3 }
832    sub TAG_NAME_STATE () { 4 }
833    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
834    sub ATTRIBUTE_NAME_STATE () { 6 }
835    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
836    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
837    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
838    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
839    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
840    #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
841    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
842    sub COMMENT_START_STATE () { 14 }
843    sub COMMENT_START_DASH_STATE () { 15 }
844    sub COMMENT_STATE () { 16 }
845    sub COMMENT_END_STATE () { 17 }
846    sub COMMENT_END_DASH_STATE () { 18 }
847    sub BOGUS_COMMENT_STATE () { 19 }
848    sub DOCTYPE_STATE () { 20 }
849    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
850    sub DOCTYPE_NAME_STATE () { 22 }
851    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
852    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
853    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
854    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
855    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
856    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
857    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
858    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
859    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
860    sub BOGUS_DOCTYPE_STATE () { 32 }
861    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
862    sub SELF_CLOSING_START_TAG_STATE () { 34 }
863    sub CDATA_SECTION_STATE () { 35 }
864    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
865    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
866    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
867    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
868    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
869    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
870    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
871    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
872    ## NOTE: "Entity data state", "entity in attribute value state", and
873    ## "consume a character reference" algorithm are jointly implemented
874    ## using the following six states:
875    sub ENTITY_STATE () { 44 }
876    sub ENTITY_HASH_STATE () { 45 }
877    sub NCR_NUM_STATE () { 46 }
878    sub HEXREF_X_STATE () { 47 }
879    sub HEXREF_HEX_STATE () { 48 }
880    sub ENTITY_NAME_STATE () { 49 }
881    
882    sub DOCTYPE_TOKEN () { 1 }
883    sub COMMENT_TOKEN () { 2 }
884    sub START_TAG_TOKEN () { 3 }
885    sub END_TAG_TOKEN () { 4 }
886    sub END_OF_FILE_TOKEN () { 5 }
887    sub CHARACTER_TOKEN () { 6 }
888    
889    sub AFTER_HTML_IMS () { 0b100 }
890    sub HEAD_IMS ()       { 0b1000 }
891    sub BODY_IMS ()       { 0b10000 }
892    sub BODY_TABLE_IMS () { 0b100000 }
893    sub TABLE_IMS ()      { 0b1000000 }
894    sub ROW_IMS ()        { 0b10000000 }
895    sub BODY_AFTER_IMS () { 0b100000000 }
896    sub FRAME_IMS ()      { 0b1000000000 }
897    sub SELECT_IMS ()     { 0b10000000000 }
898    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
899        ## NOTE: "in foreign content" insertion mode is special; it is combined
900        ## with the secondary insertion mode.  In this parser, they are stored
901        ## together in the bit-or'ed form.
902    
903    ## NOTE: "initial" and "before html" insertion modes have no constants.
904    
905    ## NOTE: "after after body" insertion mode.
906    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
907    
908    ## NOTE: "after after frameset" insertion mode.
909    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
910    
911    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
912    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
913    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
914    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
915    sub IN_BODY_IM () { BODY_IMS }
916    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
917    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
918    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
919    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
920    sub IN_TABLE_IM () { TABLE_IMS }
921    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
922    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
923    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
924    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
925    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
926    sub IN_COLUMN_GROUP_IM () { 0b10 }
927    
928  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
929    
930  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
931    my $self = shift;    my $self = shift;
932    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
933      #$self->{state_keyword}; # initialized when used
934      #$self->{entity__value}; # initialized when used
935      #$self->{entity__match}; # initialized when used
936    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
937    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
938    undef $self->{current_attribute};    undef $self->{current_attribute};
939    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
940    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
941    $self->{char} = [];    delete $self->{self_closing};
942    # $self->{next_input_character}    $self->{char_buffer} = '';
943      $self->{char_buffer_pos} = 0;
944      $self->{prev_char} = [-1, -1, -1];
945      $self->{next_char} = -1;
946    !!!next-input-character;    !!!next-input-character;
947    $self->{token} = [];    $self->{token} = [];
948    # $self->{escape}    # $self->{escape}
949  } # _initialize_tokenizer  } # _initialize_tokenizer
950    
951  ## A token has:  ## A token has:
952  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
953  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
954  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
955  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
956  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
957  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
958  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
959  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
960    ##        ->{name}
961    ##        ->{value}
962    ##        ->{has_reference} == 1 or 0
963    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
964    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
965    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
966    ##     while the token is pushed back to the stack.
967    
968  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
969    
# Line 194  sub _initialize_tokenizer ($) { Line 973  sub _initialize_tokenizer ($) {
973  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
974  ## and removed from the list.  ## and removed from the list.
975    
976    ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
977    ## (This requirement was dropped from HTML5 spec, unfortunately.)
978    
979  sub _get_next_token ($) {  sub _get_next_token ($) {
980    my $self = shift;    my $self = shift;
981    
982      if ($self->{self_closing}) {
983        !!!parse-error (type => 'nestc', token => $self->{current_token});
984        ## NOTE: The |self_closing| flag is only set by start tag token.
985        ## In addition, when a start tag token is emitted, it is always set to
986        ## |current_token|.
987        delete $self->{self_closing};
988      }
989    
990    if (@{$self->{token}}) {    if (@{$self->{token}}) {
991        $self->{self_closing} = $self->{token}->[0]->{self_closing};
992      return shift @{$self->{token}};      return shift @{$self->{token}};
993    }    }
994    
995    A: {    A: {
996      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
997        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
998          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
999            $self->{state} = 'entity data';              not $self->{escape}) {
1000              !!!cp (1);
1001              ## NOTE: In the spec, the tokenizer is switched to the
1002              ## "entity data state".  In this implementation, the tokenizer
1003              ## is switched to the |ENTITY_STATE|, which is an implementation
1004              ## of the "consume a character reference" algorithm.
1005              $self->{entity_additional} = -1;
1006              $self->{prev_state} = DATA_STATE;
1007              $self->{state} = ENTITY_STATE;
1008            !!!next-input-character;            !!!next-input-character;
1009            redo A;            redo A;
1010          } else {          } else {
1011              !!!cp (2);
1012            #            #
1013          }          }
1014        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1015          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1016            unless ($self->{escape}) {            unless ($self->{escape}) {
1017              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
1018                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
1019                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
1020                  !!!cp (3);
1021                $self->{escape} = 1;                $self->{escape} = 1;
1022                } else {
1023                  !!!cp (4);
1024              }              }
1025              } else {
1026                !!!cp (5);
1027            }            }
1028          }          }
1029                    
1030          #          #
1031        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
1032          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1033              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
1034               not $self->{escape})) {               not $self->{escape})) {
1035            $self->{state} = 'tag open';            !!!cp (6);
1036              $self->{state} = TAG_OPEN_STATE;
1037            !!!next-input-character;            !!!next-input-character;
1038            redo A;            redo A;
1039          } else {          } else {
1040              !!!cp (7);
1041            #            #
1042          }          }
1043        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1044          if ($self->{escape} and          if ($self->{escape} and
1045              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1046            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
1047                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
1048                !!!cp (8);
1049              delete $self->{escape};              delete $self->{escape};
1050              } else {
1051                !!!cp (9);
1052            }            }
1053            } else {
1054              !!!cp (10);
1055          }          }
1056                    
1057          #          #
1058        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1059          !!!emit ({type => 'end-of-file'});          !!!cp (11);
1060            !!!emit ({type => END_OF_FILE_TOKEN,
1061                      line => $self->{line}, column => $self->{column}});
1062          last A; ## TODO: ok?          last A; ## TODO: ok?
1063          } else {
1064            !!!cp (12);
1065        }        }
1066        # Anything else        # Anything else
1067        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
1068                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
1069                       line => $self->{line}, column => $self->{column},
1070                      };
1071          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1072    
1073        ## Stay in the data state        ## Stay in the data state
1074        !!!next-input-character;        !!!next-input-character;
1075    
1076        !!!emit ($token);        !!!emit ($token);
1077    
1078        redo A;        redo A;
1079      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
       ## (cannot happen in CDATA state)  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity (0);  
   
       $self->{state} = 'data';  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!emit ({type => 'character', data => '&'});  
       } else {  
         !!!emit ($token);  
       }  
   
       redo A;  
     } elsif ($self->{state} eq 'tag open') {  
1080        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1081          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
1082              !!!cp (15);
1083            !!!next-input-character;            !!!next-input-character;
1084            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1085            redo A;            redo A;
1086          } else {          } else {
1087              !!!cp (16);
1088            ## reconsume            ## reconsume
1089            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1090    
1091            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1092                        line => $self->{line_prev},
1093                        column => $self->{column_prev},
1094                       });
1095    
1096            redo A;            redo A;
1097          }          }
1098        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1099          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1100            $self->{state} = 'markup declaration open';            !!!cp (17);
1101              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1102            !!!next-input-character;            !!!next-input-character;
1103            redo A;            redo A;
1104          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1105            $self->{state} = 'close tag open';            !!!cp (18);
1106              $self->{state} = CLOSE_TAG_OPEN_STATE;
1107            !!!next-input-character;            !!!next-input-character;
1108            redo A;            redo A;
1109          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1110                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1111              !!!cp (19);
1112            $self->{current_token}            $self->{current_token}
1113              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1114                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1115            $self->{state} = 'tag name';                 line => $self->{line_prev},
1116                   column => $self->{column_prev}};
1117              $self->{state} = TAG_NAME_STATE;
1118            !!!next-input-character;            !!!next-input-character;
1119            redo A;            redo A;
1120          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1121                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1122            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1123                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1124            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1125                                        line => $self->{line_prev},
1126                                        column => $self->{column_prev}};
1127              $self->{state} = TAG_NAME_STATE;
1128            !!!next-input-character;            !!!next-input-character;
1129            redo A;            redo A;
1130          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1131            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1132            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1133                              line => $self->{line_prev},
1134                              column => $self->{column_prev});
1135              $self->{state} = DATA_STATE;
1136            !!!next-input-character;            !!!next-input-character;
1137    
1138            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1139                        line => $self->{line_prev},
1140                        column => $self->{column_prev},
1141                       });
1142    
1143            redo A;            redo A;
1144          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1145            !!!parse-error (type => 'pio');            !!!cp (22);
1146            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1147            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1148                              column => $self->{column_prev});
1149              $self->{state} = BOGUS_COMMENT_STATE;
1150              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1151                                        line => $self->{line_prev},
1152                                        column => $self->{column_prev},
1153                                       };
1154              ## $self->{next_char} is intentionally left as is
1155            redo A;            redo A;
1156          } else {          } else {
1157            !!!parse-error (type => 'bare stago');            !!!cp (23);
1158            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1159                              line => $self->{line_prev},
1160                              column => $self->{column_prev});
1161              $self->{state} = DATA_STATE;
1162            ## reconsume            ## reconsume
1163    
1164            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1165                        line => $self->{line_prev},
1166                        column => $self->{column_prev},
1167                       });
1168    
1169            redo A;            redo A;
1170          }          }
1171        } else {        } else {
1172          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1173        }        }
1174      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1175          ## NOTE: The "close tag open state" in the spec is implemented as
1176          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1177    
1178          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1179        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1180          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1181            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1182            my @next_char;            $self->{state_keyword} = '';
1183            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            ## Reconsume.
1184              push @next_char, $self->{next_input_character};            redo A;
             my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
             my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
             if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {  
               !!!next-input-character;  
               next TAGNAME;  
             } else {  
               $self->{next_input_character} = shift @next_char; # reconsume  
               !!!back-next-input-character (@next_char);  
               $self->{state} = 'data';  
   
               !!!emit ({type => 'character', data => '</'});  
     
               redo A;  
             }  
           }  
           push @next_char, $self->{next_input_character};  
         
           unless ($self->{next_input_character} == 0x0009 or # HT  
                   $self->{next_input_character} == 0x000A or # LF  
                   $self->{next_input_character} == 0x000B or # VT  
                   $self->{next_input_character} == 0x000C or # FF  
                   $self->{next_input_character} == 0x0020 or # SP  
                   $self->{next_input_character} == 0x003E or # >  
                   $self->{next_input_character} == 0x002F or # /  
                   $self->{next_input_character} == -1) {  
             $self->{next_input_character} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = 'data';  
             !!!emit ({type => 'character', data => '</'});  
             redo A;  
           } else {  
             $self->{next_input_character} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
1185          } else {          } else {
1186            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1187            # next-input-character is already done            ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1188            $self->{state} = 'data';            !!!cp (28);
1189            !!!emit ({type => 'character', data => '</'});            $self->{state} = DATA_STATE;
1190              ## Reconsume.
1191              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1192                        line => $l, column => $c,
1193                       });
1194            redo A;            redo A;
1195          }          }
1196        }        }
1197          
1198        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1199            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1200          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1201                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1202          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1203          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1204          redo A;                 line => $l, column => $c};
1205        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1206                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1207          $self->{current_token} = {type => 'end tag',          redo A;
1208                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1209          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1210          !!!next-input-character;          !!!cp (30);
1211          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1212        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1213          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1214          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1215            !!!next-input-character;
1216            redo A;
1217          } elsif ($self->{next_char} == 0x003E) { # >
1218            !!!cp (31);
1219            !!!parse-error (type => 'empty end tag',
1220                            line => $self->{line_prev}, ## "<" in "</>"
1221                            column => $self->{column_prev} - 1);
1222            $self->{state} = DATA_STATE;
1223          !!!next-input-character;          !!!next-input-character;
1224          redo A;          redo A;
1225        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1226            !!!cp (32);
1227          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1228          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1229          # reconsume          # reconsume
1230    
1231          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1232                      line => $l, column => $c,
1233                     });
1234    
1235          redo A;          redo A;
1236        } else {        } else {
1237            !!!cp (33);
1238          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1239          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1240          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1241          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1242                                      column => $self->{column_prev} - 1,
1243                                     };
1244            ## NOTE: $self->{next_char} is intentionally left as is.
1245            ## Although the "anything else" case of the spec not explicitly
1246            ## states that the next input character is to be reconsumed,
1247            ## it will be included to the |data| of the comment token
1248            ## generated from the bogus end tag, as defined in the
1249            ## "bogus comment state" entry.
1250            redo A;
1251          }
1252        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1253          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1254          if (length $ch) {
1255            my $CH = $ch;
1256            $ch =~ tr/a-z/A-Z/;
1257            my $nch = chr $self->{next_char};
1258            if ($nch eq $ch or $nch eq $CH) {
1259              !!!cp (24);
1260              ## Stay in the state.
1261              $self->{state_keyword} .= $nch;
1262              !!!next-input-character;
1263              redo A;
1264            } else {
1265              !!!cp (25);
1266              $self->{state} = DATA_STATE;
1267              ## Reconsume.
1268              !!!emit ({type => CHARACTER_TOKEN,
1269                        data => '</' . $self->{state_keyword},
1270                        line => $self->{line_prev},
1271                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1272                       });
1273              redo A;
1274            }
1275          } else { # after "<{tag-name}"
1276            unless ({
1277                     0x0009 => 1, # HT
1278                     0x000A => 1, # LF
1279                     0x000B => 1, # VT
1280                     0x000C => 1, # FF
1281                     0x0020 => 1, # SP
1282                     0x003E => 1, # >
1283                     0x002F => 1, # /
1284                     -1 => 1, # EOF
1285                    }->{$self->{next_char}}) {
1286              !!!cp (26);
1287              ## Reconsume.
1288              $self->{state} = DATA_STATE;
1289              !!!emit ({type => CHARACTER_TOKEN,
1290                        data => '</' . $self->{state_keyword},
1291                        line => $self->{line_prev},
1292                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1293                       });
1294              redo A;
1295            } else {
1296              !!!cp (27);
1297              $self->{current_token}
1298                  = {type => END_TAG_TOKEN,
1299                     tag_name => $self->{last_emitted_start_tag_name},
1300                     line => $self->{line_prev},
1301                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1302              $self->{state} = TAG_NAME_STATE;
1303              ## Reconsume.
1304              redo A;
1305            }
1306        }        }
1307      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
1308        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1309            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1310            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1311            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1312            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1313          $self->{state} = 'before attribute name';          !!!cp (34);
1314          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1315          redo A;          !!!next-input-character;
1316        } elsif ($self->{next_input_character} == 0x003E) { # >          redo A;
1317          if ($self->{current_token}->{type} eq 'start tag') {        } elsif ($self->{next_char} == 0x003E) { # >
1318            $self->{current_token}->{first_start_tag}          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1319                = not defined $self->{last_emitted_start_tag_name};            !!!cp (35);
1320            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1321          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1322            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1323            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1324              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1325            }            #  !!! cp (36);
1326              #  !!! parse-error (type => 'end tag attribute');
1327              #} else {
1328                !!!cp (37);
1329              #}
1330          } else {          } else {
1331            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1332          }          }
1333          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1334          !!!next-input-character;          !!!next-input-character;
1335    
1336          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1337    
1338          redo A;          redo A;
1339        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1340                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1341          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1342            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1343            # start tag or end tag            # start tag or end tag
1344          ## Stay in this state          ## Stay in this state
1345          !!!next-input-character;          !!!next-input-character;
1346          redo A;          redo A;
1347        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1348          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1349          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1350            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1351            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1352          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1353            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1354            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1355              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1356            }            #  !!! cp (40);
1357              #  !!! parse-error (type => 'end tag attribute');
1358              #} else {
1359                !!!cp (41);
1360              #}
1361          } else {          } else {
1362            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1363          }          }
1364          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1365          # reconsume          # reconsume
1366    
1367          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1368    
1369          redo A;          redo A;
1370        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1371            !!!cp (42);
1372            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1373          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
1374          redo A;          redo A;
1375        } else {        } else {
1376          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1377            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1378            # start tag or end tag            # start tag or end tag
1379          ## Stay in the state          ## Stay in the state
1380          !!!next-input-character;          !!!next-input-character;
1381          redo A;          redo A;
1382        }        }
1383      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1384        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1385            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1386            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1387            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1388            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1389            !!!cp (45);
1390          ## Stay in the state          ## Stay in the state
1391          !!!next-input-character;          !!!next-input-character;
1392          redo A;          redo A;
1393        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1394          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1395            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1396            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1397          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1398            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1399            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1400                !!!cp (47);
1401              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1402              } else {
1403                !!!cp (48);
1404            }            }
1405          } else {          } else {
1406            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1407          }          }
1408          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1409          !!!next-input-character;          !!!next-input-character;
1410    
1411          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1412    
1413          redo A;          redo A;
1414        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1415                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1416          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1417                                value => ''};          $self->{current_attribute}
1418          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1419                   value => '',
1420                   line => $self->{line}, column => $self->{column}};
1421            $self->{state} = ATTRIBUTE_NAME_STATE;
1422            !!!next-input-character;
1423            redo A;
1424          } elsif ($self->{next_char} == 0x002F) { # /
1425            !!!cp (50);
1426            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1427          !!!next-input-character;          !!!next-input-character;
1428          redo A;          redo A;
1429        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1430          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1431          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1432            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1433            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1434          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1435            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1436            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1437                !!!cp (53);
1438              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1439              } else {
1440                !!!cp (54);
1441            }            }
1442          } else {          } else {
1443            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1444          }          }
1445          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1446          # reconsume          # reconsume
1447    
1448          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1449    
1450          redo A;          redo A;
1451        } else {        } else {
1452          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1453                                value => ''};               0x0022 => 1, # "
1454          $self->{state} = 'attribute name';               0x0027 => 1, # '
1455                 0x003D => 1, # =
1456                }->{$self->{next_char}}) {
1457              !!!cp (55);
1458              !!!parse-error (type => 'bad attribute name');
1459            } else {
1460              !!!cp (56);
1461            }
1462            $self->{current_attribute}
1463                = {name => chr ($self->{next_char}),
1464                   value => '',
1465                   line => $self->{line}, column => $self->{column}};
1466            $self->{state} = ATTRIBUTE_NAME_STATE;
1467          !!!next-input-character;          !!!next-input-character;
1468          redo A;          redo A;
1469        }        }
1470      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1471        my $before_leave = sub {        my $before_leave = sub {
1472          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1473              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1474            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1475              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1476            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1477          } else {          } else {
1478              !!!cp (58);
1479            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1480              = $self->{current_attribute};              = $self->{current_attribute};
1481          }          }
1482        }; # $before_leave        }; # $before_leave
1483    
1484        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1485            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1486            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1487            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1488            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1489            !!!cp (59);
1490          $before_leave->();          $before_leave->();
1491          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1492          !!!next-input-character;          !!!next-input-character;
1493          redo A;          redo A;
1494        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1495            !!!cp (60);
1496          $before_leave->();          $before_leave->();
1497          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1498          !!!next-input-character;          !!!next-input-character;
1499          redo A;          redo A;
1500        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1501          $before_leave->();          $before_leave->();
1502          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1503            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1504            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1505          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1506              !!!cp (62);
1507            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1508            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1509              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 1511  sub _get_next_token ($) {
1511          } else {          } else {
1512            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1513          }          }
1514          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1515          !!!next-input-character;          !!!next-input-character;
1516    
1517          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1518    
1519          redo A;          redo A;
1520        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1521                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1522          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1523            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1524          ## Stay in the state          ## Stay in the state
1525          !!!next-input-character;          !!!next-input-character;
1526          redo A;          redo A;
1527        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1528            !!!cp (64);
1529          $before_leave->();          $before_leave->();
1530            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1531          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
1532          redo A;          redo A;
1533        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1534          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1535          $before_leave->();          $before_leave->();
1536          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1537            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1538            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1539          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1540            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1541            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1542                !!!cp (67);
1543              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1544              } else {
1545                ## NOTE: This state should never be reached.
1546                !!!cp (68);
1547            }            }
1548          } else {          } else {
1549            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1550          }          }
1551          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1552          # reconsume          # reconsume
1553    
1554          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1555    
1556          redo A;          redo A;
1557        } else {        } else {
1558          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1559                $self->{next_char} == 0x0027) { # '
1560              !!!cp (69);
1561              !!!parse-error (type => 'bad attribute name');
1562            } else {
1563              !!!cp (70);
1564            }
1565            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1566          ## Stay in the state          ## Stay in the state
1567          !!!next-input-character;          !!!next-input-character;
1568          redo A;          redo A;
1569        }        }
1570      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1571        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1572            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1573            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1574            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1575            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1576            !!!cp (71);
1577          ## Stay in the state          ## Stay in the state
1578          !!!next-input-character;          !!!next-input-character;
1579          redo A;          redo A;
1580        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1581          $self->{state} = 'before attribute value';          !!!cp (72);
1582            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1583          !!!next-input-character;          !!!next-input-character;
1584          redo A;          redo A;
1585        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1586          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1587            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1588            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1589          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1590            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1591            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1592                !!!cp (74);
1593              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1594              } else {
1595                ## NOTE: This state should never be reached.
1596                !!!cp (75);
1597            }            }
1598          } else {          } else {
1599            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1600          }          }
1601          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1602          !!!next-input-character;          !!!next-input-character;
1603    
1604          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1605    
1606          redo A;          redo A;
1607        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1608                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1609          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1610                                value => ''};          $self->{current_attribute}
1611          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1612                   value => '',
1613                   line => $self->{line}, column => $self->{column}};
1614            $self->{state} = ATTRIBUTE_NAME_STATE;
1615            !!!next-input-character;
1616            redo A;
1617          } elsif ($self->{next_char} == 0x002F) { # /
1618            !!!cp (77);
1619            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1620          !!!next-input-character;          !!!next-input-character;
1621          redo A;          redo A;
1622        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1623          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1624          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1625            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1626            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1627          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1628            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1629            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1630                !!!cp (80);
1631              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1632              } else {
1633                ## NOTE: This state should never be reached.
1634                !!!cp (81);
1635            }            }
1636          } else {          } else {
1637            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1638          }          }
1639          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1640          # reconsume          # reconsume
1641    
1642          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1643    
1644          redo A;          redo A;
1645        } else {        } else {
1646          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{next_char} == 0x0022 or # "
1647                                value => ''};              $self->{next_char} == 0x0027) { # '
1648          $self->{state} = 'attribute name';            !!!cp (78);
1649              !!!parse-error (type => 'bad attribute name');
1650            } else {
1651              !!!cp (82);
1652            }
1653            $self->{current_attribute}
1654                = {name => chr ($self->{next_char}),
1655                   value => '',
1656                   line => $self->{line}, column => $self->{column}};
1657            $self->{state} = ATTRIBUTE_NAME_STATE;
1658          !!!next-input-character;          !!!next-input-character;
1659          redo A;                  redo A;        
1660        }        }
1661      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1662        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1663            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1664            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1665            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1666            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1667            !!!cp (83);
1668          ## Stay in the state          ## Stay in the state
1669          !!!next-input-character;          !!!next-input-character;
1670          redo A;          redo A;
1671        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1672          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1673            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1674          !!!next-input-character;          !!!next-input-character;
1675          redo A;          redo A;
1676        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1677          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1678            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1679          ## reconsume          ## reconsume
1680          redo A;          redo A;
1681        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1682          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1683            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1684          !!!next-input-character;          !!!next-input-character;
1685          redo A;          redo A;
1686        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1687          if ($self->{current_token}->{type} eq 'start tag') {          !!!parse-error (type => 'empty unquoted attribute value');
1688            $self->{current_token}->{first_start_tag}          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1689                = not defined $self->{last_emitted_start_tag_name};            !!!cp (87);
1690            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1691          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1692            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1693            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1694                !!!cp (88);
1695              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1696              } else {
1697                ## NOTE: This state should never be reached.
1698                !!!cp (89);
1699            }            }
1700          } else {          } else {
1701            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1702          }          }
1703          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1704          !!!next-input-character;          !!!next-input-character;
1705    
1706          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1707    
1708          redo A;          redo A;
1709        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1710          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1711          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1712            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1713            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1714          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1715            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1716            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1717                !!!cp (91);
1718              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1719              } else {
1720                ## NOTE: This state should never be reached.
1721                !!!cp (92);
1722            }            }
1723          } else {          } else {
1724            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1725          }          }
1726          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1727          ## reconsume          ## reconsume
1728    
1729          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1730    
1731          redo A;          redo A;
1732        } else {        } else {
1733          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1734          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1735              !!!parse-error (type => 'bad attribute value');
1736            } else {
1737              !!!cp (94);
1738            }
1739            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1740            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1741          !!!next-input-character;          !!!next-input-character;
1742          redo A;          redo A;
1743        }        }
1744      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1745        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1746          $self->{state} = 'before attribute name';          !!!cp (95);
1747            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1748          !!!next-input-character;          !!!next-input-character;
1749          redo A;          redo A;
1750        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1751          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1752          $self->{state} = 'entity in attribute value';          ## NOTE: In the spec, the tokenizer is switched to the
1753            ## "entity in attribute value state".  In this implementation, the
1754            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1755            ## implementation of the "consume a character reference" algorithm.
1756            $self->{prev_state} = $self->{state};
1757            $self->{entity_additional} = 0x0022; # "
1758            $self->{state} = ENTITY_STATE;
1759          !!!next-input-character;          !!!next-input-character;
1760          redo A;          redo A;
1761        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1762          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1763          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1764            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1765            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1766          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1767            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1768            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1769                !!!cp (98);
1770              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1771              } else {
1772                ## NOTE: This state should never be reached.
1773                !!!cp (99);
1774            }            }
1775          } else {          } else {
1776            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1777          }          }
1778          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1779          ## reconsume          ## reconsume
1780    
1781          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1782    
1783          redo A;          redo A;
1784        } else {        } else {
1785          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1786            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1787            $self->{read_until}->($self->{current_attribute}->{value},
1788                                  q["&],
1789                                  length $self->{current_attribute}->{value});
1790    
1791          ## Stay in the state          ## Stay in the state
1792          !!!next-input-character;          !!!next-input-character;
1793          redo A;          redo A;
1794        }        }
1795      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1796        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1797          $self->{state} = 'before attribute name';          !!!cp (101);
1798            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1799            !!!next-input-character;
1800            redo A;
1801          } elsif ($self->{next_char} == 0x0026) { # &
1802            !!!cp (102);
1803            ## NOTE: In the spec, the tokenizer is switched to the
1804            ## "entity in attribute value state".  In this implementation, the
1805            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1806            ## implementation of the "consume a character reference" algorithm.
1807            $self->{entity_additional} = 0x0027; # '
1808            $self->{prev_state} = $self->{state};
1809            $self->{state} = ENTITY_STATE;
1810          !!!next-input-character;          !!!next-input-character;
1811          redo A;          redo A;
1812        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == -1) {
         $self->{last_attribute_value_state} = 'attribute value (single-quoted)';  
         $self->{state} = 'entity in attribute value';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1813          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1814          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1815            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1816            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1817          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1818            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1819            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1820                !!!cp (104);
1821              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1822              } else {
1823                ## NOTE: This state should never be reached.
1824                !!!cp (105);
1825            }            }
1826          } else {          } else {
1827            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1828          }          }
1829          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1830          ## reconsume          ## reconsume
1831    
1832          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1833    
1834          redo A;          redo A;
1835        } else {        } else {
1836          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1837            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1838            $self->{read_until}->($self->{current_attribute}->{value},
1839                                  q['&],
1840                                  length $self->{current_attribute}->{value});
1841    
1842          ## Stay in the state          ## Stay in the state
1843          !!!next-input-character;          !!!next-input-character;
1844          redo A;          redo A;
1845        }        }
1846      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1847        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1848            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1849            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1850            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1851            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1852          $self->{state} = 'before attribute name';          !!!cp (107);
1853          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1854          redo A;          !!!next-input-character;
1855        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1856          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1857          $self->{state} = 'entity in attribute value';          !!!cp (108);
1858          !!!next-input-character;          ## NOTE: In the spec, the tokenizer is switched to the
1859          redo A;          ## "entity in attribute value state".  In this implementation, the
1860        } elsif ($self->{next_input_character} == 0x003E) { # >          ## tokenizer is switched to the |ENTITY_STATE|, which is an
1861          if ($self->{current_token}->{type} eq 'start tag') {          ## implementation of the "consume a character reference" algorithm.
1862            $self->{current_token}->{first_start_tag}          $self->{entity_additional} = -1;
1863                = not defined $self->{last_emitted_start_tag_name};          $self->{prev_state} = $self->{state};
1864            $self->{state} = ENTITY_STATE;
1865            !!!next-input-character;
1866            redo A;
1867          } elsif ($self->{next_char} == 0x003E) { # >
1868            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1869              !!!cp (109);
1870            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1871          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1872            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1873            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1874                !!!cp (110);
1875              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1876              } else {
1877                ## NOTE: This state should never be reached.
1878                !!!cp (111);
1879            }            }
1880          } else {          } else {
1881            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1882          }          }
1883          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1884          !!!next-input-character;          !!!next-input-character;
1885    
1886          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1887    
1888          redo A;          redo A;
1889        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1890          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1891          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1892            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1893            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1894          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1895            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1896            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1897                !!!cp (113);
1898              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1899              } else {
1900                ## NOTE: This state should never be reached.
1901                !!!cp (114);
1902            }            }
1903          } else {          } else {
1904            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1905          }          }
1906          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1907          ## reconsume          ## reconsume
1908    
1909          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1910    
1911          redo A;          redo A;
1912        } else {        } else {
1913          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1914                 0x0022 => 1, # "
1915                 0x0027 => 1, # '
1916                 0x003D => 1, # =
1917                }->{$self->{next_char}}) {
1918              !!!cp (115);
1919              !!!parse-error (type => 'bad attribute value');
1920            } else {
1921              !!!cp (116);
1922            }
1923            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1924            $self->{read_until}->($self->{current_attribute}->{value},
1925                                  q["'=& >],
1926                                  length $self->{current_attribute}->{value});
1927    
1928          ## Stay in the state          ## Stay in the state
1929          !!!next-input-character;          !!!next-input-character;
1930          redo A;          redo A;
1931        }        }
1932      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1933        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        if ($self->{next_char} == 0x0009 or # HT
1934              $self->{next_char} == 0x000A or # LF
1935              $self->{next_char} == 0x000B or # VT
1936              $self->{next_char} == 0x000C or # FF
1937              $self->{next_char} == 0x0020) { # SP
1938            !!!cp (118);
1939            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1940            !!!next-input-character;
1941            redo A;
1942          } elsif ($self->{next_char} == 0x003E) { # >
1943            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1944              !!!cp (119);
1945              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1946            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1947              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1948              if ($self->{current_token}->{attributes}) {
1949                !!!cp (120);
1950                !!!parse-error (type => 'end tag attribute');
1951              } else {
1952                ## NOTE: This state should never be reached.
1953                !!!cp (121);
1954              }
1955            } else {
1956              die "$0: $self->{current_token}->{type}: Unknown token type";
1957            }
1958            $self->{state} = DATA_STATE;
1959            !!!next-input-character;
1960    
1961        unless (defined $token) {          !!!emit ($self->{current_token}); # start tag or end tag
1962          $self->{current_attribute}->{value} .= '&';  
1963            redo A;
1964          } elsif ($self->{next_char} == 0x002F) { # /
1965            !!!cp (122);
1966            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1967            !!!next-input-character;
1968            redo A;
1969          } elsif ($self->{next_char} == -1) {
1970            !!!parse-error (type => 'unclosed tag');
1971            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1972              !!!cp (122.3);
1973              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1974            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1975              if ($self->{current_token}->{attributes}) {
1976                !!!cp (122.1);
1977                !!!parse-error (type => 'end tag attribute');
1978              } else {
1979                ## NOTE: This state should never be reached.
1980                !!!cp (122.2);
1981              }
1982            } else {
1983              die "$0: $self->{current_token}->{type}: Unknown token type";
1984            }
1985            $self->{state} = DATA_STATE;
1986            ## Reconsume.
1987            !!!emit ($self->{current_token}); # start tag or end tag
1988            redo A;
1989        } else {        } else {
1990          $self->{current_attribute}->{value} .= $token->{data};          !!!cp ('124.1');
1991          ## ISSUE: spec says "append the returned character token to the current attribute's value"          !!!parse-error (type => 'no space between attributes');
1992            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1993            ## reconsume
1994            redo A;
1995        }        }
1996        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1997          if ($self->{next_char} == 0x003E) { # >
1998            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1999              !!!cp ('124.2');
2000              !!!parse-error (type => 'nestc', token => $self->{current_token});
2001              ## TODO: Different type than slash in start tag
2002              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
2003              if ($self->{current_token}->{attributes}) {
2004                !!!cp ('124.4');
2005                !!!parse-error (type => 'end tag attribute');
2006              } else {
2007                !!!cp ('124.5');
2008              }
2009              ## TODO: Test |<title></title/>|
2010            } else {
2011              !!!cp ('124.3');
2012              $self->{self_closing} = 1;
2013            }
2014    
2015        $self->{state} = $self->{last_attribute_value_state};          $self->{state} = DATA_STATE;
2016        # next-input-character is already done          !!!next-input-character;
       redo A;  
     } elsif ($self->{state} eq 'bogus comment') {  
       ## (only happen if PCDATA state)  
         
       my $token = {type => 'comment', data => ''};  
   
       BC: {  
         if ($self->{next_input_character} == 0x003E) { # >  
           $self->{state} = 'data';  
           !!!next-input-character;  
   
           !!!emit ($token);  
   
           redo A;  
         } elsif ($self->{next_input_character} == -1) {  
           $self->{state} = 'data';  
           ## reconsume  
2017    
2018            !!!emit ($token);          !!!emit ($self->{current_token}); # start tag or end tag
2019    
2020            redo A;          redo A;
2021          } elsif ($self->{next_char} == -1) {
2022            !!!parse-error (type => 'unclosed tag');
2023            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
2024              !!!cp (124.7);
2025              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
2026            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
2027              if ($self->{current_token}->{attributes}) {
2028                !!!cp (124.5);
2029                !!!parse-error (type => 'end tag attribute');
2030              } else {
2031                ## NOTE: This state should never be reached.
2032                !!!cp (124.6);
2033              }
2034          } else {          } else {
2035            $token->{data} .= chr ($self->{next_input_character});            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!next-input-character;  
           redo BC;  
2036          }          }
2037        } # BC          $self->{state} = DATA_STATE;
2038      } elsif ($self->{state} eq 'markup declaration open') {          ## Reconsume.
2039            !!!emit ($self->{current_token}); # start tag or end tag
2040            redo A;
2041          } else {
2042            !!!cp ('124.4');
2043            !!!parse-error (type => 'nestc');
2044            ## TODO: This error type is wrong.
2045            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2046            ## Reconsume.
2047            redo A;
2048          }
2049        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2050        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2051    
2052        my @next_char;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2053        push @next_char, $self->{next_input_character};        ## consumes characters one-by-one basis.
2054                
2055        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x003E) { # >
2056            !!!cp (124);
2057            $self->{state} = DATA_STATE;
2058          !!!next-input-character;          !!!next-input-character;
2059          push @next_char, $self->{next_input_character};  
2060          if ($self->{next_input_character} == 0x002D) { # -          !!!emit ($self->{current_token}); # comment
2061            $self->{current_token} = {type => 'comment', data => ''};          redo A;
2062            $self->{state} = 'comment start';        } elsif ($self->{next_char} == -1) {
2063            !!!next-input-character;          !!!cp (125);
2064            redo A;          $self->{state} = DATA_STATE;
2065          }          ## reconsume
2066        } elsif ($self->{next_input_character} == 0x0044 or # D  
2067                 $self->{next_input_character} == 0x0064) { # d          !!!emit ($self->{current_token}); # comment
2068            redo A;
2069          } else {
2070            !!!cp (126);
2071            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2072            $self->{read_until}->($self->{current_token}->{data},
2073                                  q[>],
2074                                  length $self->{current_token}->{data});
2075    
2076            ## Stay in the state.
2077          !!!next-input-character;          !!!next-input-character;
2078          push @next_char, $self->{next_input_character};          redo A;
2079          if ($self->{next_input_character} == 0x004F or # O        }
2080              $self->{next_input_character} == 0x006F) { # o      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2081            !!!next-input-character;        ## (only happen if PCDATA state)
2082            push @next_char, $self->{next_input_character};        
2083            if ($self->{next_input_character} == 0x0043 or # C        if ($self->{next_char} == 0x002D) { # -
2084                $self->{next_input_character} == 0x0063) { # c          !!!cp (133);
2085              !!!next-input-character;          $self->{state} = MD_HYPHEN_STATE;
2086              push @next_char, $self->{next_input_character};          !!!next-input-character;
2087              if ($self->{next_input_character} == 0x0054 or # T          redo A;
2088                  $self->{next_input_character} == 0x0074) { # t        } elsif ($self->{next_char} == 0x0044 or # D
2089                !!!next-input-character;                 $self->{next_char} == 0x0064) { # d
2090                push @next_char, $self->{next_input_character};          ## ASCII case-insensitive.
2091                if ($self->{next_input_character} == 0x0059 or # Y          !!!cp (130);
2092                    $self->{next_input_character} == 0x0079) { # y          $self->{state} = MD_DOCTYPE_STATE;
2093                  !!!next-input-character;          $self->{state_keyword} = chr $self->{next_char};
2094                  push @next_char, $self->{next_input_character};          !!!next-input-character;
2095                  if ($self->{next_input_character} == 0x0050 or # P          redo A;
2096                      $self->{next_input_character} == 0x0070) { # p        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2097                    !!!next-input-character;                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2098                    push @next_char, $self->{next_input_character};                 $self->{next_char} == 0x005B) { # [
2099                    if ($self->{next_input_character} == 0x0045 or # E          !!!cp (135.4);                
2100                        $self->{next_input_character} == 0x0065) { # e          $self->{state} = MD_CDATA_STATE;
2101                      ## ISSUE: What a stupid code this is!          $self->{state_keyword} = '[';
2102                      $self->{state} = 'DOCTYPE';          !!!next-input-character;
2103                      !!!next-input-character;          redo A;
2104                      redo A;        } else {
2105                    }          !!!cp (136);
                 }  
               }  
             }  
           }  
         }  
2106        }        }
2107    
2108        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2109        $self->{next_input_character} = shift @next_char;                        line => $self->{line_prev},
2110        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2111        $self->{state} = 'bogus comment';        ## Reconsume.
2112          $self->{state} = BOGUS_COMMENT_STATE;
2113          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2114                                    line => $self->{line_prev},
2115                                    column => $self->{column_prev} - 1,
2116                                   };
2117        redo A;        redo A;
2118              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2119        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2120        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?          !!!cp (127);
2121      } elsif ($self->{state} eq 'comment start') {          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2122        if ($self->{next_input_character} == 0x002D) { # -                                    line => $self->{line_prev},
2123          $self->{state} = 'comment start dash';                                    column => $self->{column_prev} - 2,
2124                                     };
2125            $self->{state} = COMMENT_START_STATE;
2126            !!!next-input-character;
2127            redo A;
2128          } else {
2129            !!!cp (128);
2130            !!!parse-error (type => 'bogus comment',
2131                            line => $self->{line_prev},
2132                            column => $self->{column_prev} - 2);
2133            $self->{state} = BOGUS_COMMENT_STATE;
2134            ## Reconsume.
2135            $self->{current_token} = {type => COMMENT_TOKEN,
2136                                      data => '-',
2137                                      line => $self->{line_prev},
2138                                      column => $self->{column_prev} - 2,
2139                                     };
2140            redo A;
2141          }
2142        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2143          ## ASCII case-insensitive.
2144          if ($self->{next_char} == [
2145                undef,
2146                0x004F, # O
2147                0x0043, # C
2148                0x0054, # T
2149                0x0059, # Y
2150                0x0050, # P
2151              ]->[length $self->{state_keyword}] or
2152              $self->{next_char} == [
2153                undef,
2154                0x006F, # o
2155                0x0063, # c
2156                0x0074, # t
2157                0x0079, # y
2158                0x0070, # p
2159              ]->[length $self->{state_keyword}]) {
2160            !!!cp (131);
2161            ## Stay in the state.
2162            $self->{state_keyword} .= chr $self->{next_char};
2163            !!!next-input-character;
2164            redo A;
2165          } elsif ((length $self->{state_keyword}) == 6 and
2166                   ($self->{next_char} == 0x0045 or # E
2167                    $self->{next_char} == 0x0065)) { # e
2168            !!!cp (129);
2169            $self->{state} = DOCTYPE_STATE;
2170            $self->{current_token} = {type => DOCTYPE_TOKEN,
2171                                      quirks => 1,
2172                                      line => $self->{line_prev},
2173                                      column => $self->{column_prev} - 7,
2174                                     };
2175            !!!next-input-character;
2176            redo A;
2177          } else {
2178            !!!cp (132);        
2179            !!!parse-error (type => 'bogus comment',
2180                            line => $self->{line_prev},
2181                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2182            $self->{state} = BOGUS_COMMENT_STATE;
2183            ## Reconsume.
2184            $self->{current_token} = {type => COMMENT_TOKEN,
2185                                      data => $self->{state_keyword},
2186                                      line => $self->{line_prev},
2187                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2188                                     };
2189            redo A;
2190          }
2191        } elsif ($self->{state} == MD_CDATA_STATE) {
2192          if ($self->{next_char} == {
2193                '[' => 0x0043, # C
2194                '[C' => 0x0044, # D
2195                '[CD' => 0x0041, # A
2196                '[CDA' => 0x0054, # T
2197                '[CDAT' => 0x0041, # A
2198              }->{$self->{state_keyword}}) {
2199            !!!cp (135.1);
2200            ## Stay in the state.
2201            $self->{state_keyword} .= chr $self->{next_char};
2202            !!!next-input-character;
2203            redo A;
2204          } elsif ($self->{state_keyword} eq '[CDATA' and
2205                   $self->{next_char} == 0x005B) { # [
2206            !!!cp (135.2);
2207            $self->{current_token} = {type => CHARACTER_TOKEN,
2208                                      data => '',
2209                                      line => $self->{line_prev},
2210                                      column => $self->{column_prev} - 7};
2211            $self->{state} = CDATA_SECTION_STATE;
2212            !!!next-input-character;
2213            redo A;
2214          } else {
2215            !!!cp (135.3);
2216            !!!parse-error (type => 'bogus comment',
2217                            line => $self->{line_prev},
2218                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2219            $self->{state} = BOGUS_COMMENT_STATE;
2220            ## Reconsume.
2221            $self->{current_token} = {type => COMMENT_TOKEN,
2222                                      data => $self->{state_keyword},
2223                                      line => $self->{line_prev},
2224                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2225                                     };
2226            redo A;
2227          }
2228        } elsif ($self->{state} == COMMENT_START_STATE) {
2229          if ($self->{next_char} == 0x002D) { # -
2230            !!!cp (137);
2231            $self->{state} = COMMENT_START_DASH_STATE;
2232          !!!next-input-character;          !!!next-input-character;
2233          redo A;          redo A;
2234        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2235            !!!cp (138);
2236          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2237          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2238          !!!next-input-character;          !!!next-input-character;
2239    
2240          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2241    
2242          redo A;          redo A;
2243        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2244            !!!cp (139);
2245          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2246          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2247          ## reconsume          ## reconsume
2248    
2249          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2250    
2251          redo A;          redo A;
2252        } else {        } else {
2253            !!!cp (140);
2254          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2255              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2256          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2257          !!!next-input-character;          !!!next-input-character;
2258          redo A;          redo A;
2259        }        }
2260      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2261        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2262          $self->{state} = 'comment end';          !!!cp (141);
2263            $self->{state} = COMMENT_END_STATE;
2264          !!!next-input-character;          !!!next-input-character;
2265          redo A;          redo A;
2266        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2267            !!!cp (142);
2268          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2269          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2270          !!!next-input-character;          !!!next-input-character;
2271    
2272          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2273    
2274          redo A;          redo A;
2275        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2276            !!!cp (143);
2277          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2278          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2279          ## reconsume          ## reconsume
2280    
2281          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2282    
2283          redo A;          redo A;
2284        } else {        } else {
2285            !!!cp (144);
2286          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2287              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2288          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2289          !!!next-input-character;          !!!next-input-character;
2290          redo A;          redo A;
2291        }        }
2292      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
2293        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2294          $self->{state} = 'comment end dash';          !!!cp (145);
2295            $self->{state} = COMMENT_END_DASH_STATE;
2296          !!!next-input-character;          !!!next-input-character;
2297          redo A;          redo A;
2298        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2299            !!!cp (146);
2300          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2301          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2302          ## reconsume          ## reconsume
2303    
2304          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2305    
2306          redo A;          redo A;
2307        } else {        } else {
2308          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2309            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2310            $self->{read_until}->($self->{current_token}->{data},
2311                                  q[-],
2312                                  length $self->{current_token}->{data});
2313    
2314          ## Stay in the state          ## Stay in the state
2315          !!!next-input-character;          !!!next-input-character;
2316          redo A;          redo A;
2317        }        }
2318      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2319        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2320          $self->{state} = 'comment end';          !!!cp (148);
2321            $self->{state} = COMMENT_END_STATE;
2322          !!!next-input-character;          !!!next-input-character;
2323          redo A;          redo A;
2324        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2325            !!!cp (149);
2326          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2327          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2328          ## reconsume          ## reconsume
2329    
2330          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2331    
2332          redo A;          redo A;
2333        } else {        } else {
2334          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2335          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2336            $self->{state} = COMMENT_STATE;
2337          !!!next-input-character;          !!!next-input-character;
2338          redo A;          redo A;
2339        }        }
2340      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2341        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2342          $self->{state} = 'data';          !!!cp (151);
2343            $self->{state} = DATA_STATE;
2344          !!!next-input-character;          !!!next-input-character;
2345    
2346          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2347    
2348          redo A;          redo A;
2349        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2350          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2351            !!!parse-error (type => 'dash in comment',
2352                            line => $self->{line_prev},
2353                            column => $self->{column_prev});
2354          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2355          ## Stay in the state          ## Stay in the state
2356          !!!next-input-character;          !!!next-input-character;
2357          redo A;          redo A;
2358        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2359            !!!cp (153);
2360          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2361          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2362          ## reconsume          ## reconsume
2363    
2364          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2365    
2366          redo A;          redo A;
2367        } else {        } else {
2368          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2369          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2370          $self->{state} = 'comment';                          line => $self->{line_prev},
2371                            column => $self->{column_prev});
2372            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2373            $self->{state} = COMMENT_STATE;
2374          !!!next-input-character;          !!!next-input-character;
2375          redo A;          redo A;
2376        }        }
2377      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2378        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2379            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2380            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2381            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2382            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2383          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2384            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2385          !!!next-input-character;          !!!next-input-character;
2386          redo A;          redo A;
2387        } else {        } else {
2388            !!!cp (156);
2389          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2390          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2391          ## reconsume          ## reconsume
2392          redo A;          redo A;
2393        }        }
2394      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2395        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2396            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2397            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2398            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2399            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2400            !!!cp (157);
2401          ## Stay in the state          ## Stay in the state
2402          !!!next-input-character;          !!!next-input-character;
2403          redo A;          redo A;
2404        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2405            !!!cp (158);
2406          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2407          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2408          !!!next-input-character;          !!!next-input-character;
2409    
2410          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2411    
2412          redo A;          redo A;
2413        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2414            !!!cp (159);
2415          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2416          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2417          ## reconsume          ## reconsume
2418    
2419          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2420    
2421          redo A;          redo A;
2422        } else {        } else {
2423          $self->{current_token}          !!!cp (160);
2424              = {type => 'DOCTYPE',          $self->{current_token}->{name} = chr $self->{next_char};
2425                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2426  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2427          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2428          !!!next-input-character;          !!!next-input-character;
2429          redo A;          redo A;
2430        }        }
2431      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2432  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2433        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2434            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2435            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2436            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2437            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2438          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2439            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2440          !!!next-input-character;          !!!next-input-character;
2441          redo A;          redo A;
2442        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2443          $self->{state} = 'data';          !!!cp (162);
2444            $self->{state} = DATA_STATE;
2445          !!!next-input-character;          !!!next-input-character;
2446    
2447          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2448    
2449          redo A;          redo A;
2450        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2451            !!!cp (163);
2452          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2453          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2454          ## reconsume          ## reconsume
2455    
2456          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2457          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2458    
2459          redo A;          redo A;
2460        } else {        } else {
2461            !!!cp (164);
2462          $self->{current_token}->{name}          $self->{current_token}->{name}
2463            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2464          ## Stay in the state          ## Stay in the state
2465          !!!next-input-character;          !!!next-input-character;
2466          redo A;          redo A;
2467        }        }
2468      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2469        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2470            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2471            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2472            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2473            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2474            !!!cp (165);
2475          ## Stay in the state          ## Stay in the state
2476          !!!next-input-character;          !!!next-input-character;
2477          redo A;          redo A;
2478        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2479          $self->{state} = 'data';          !!!cp (166);
2480            $self->{state} = DATA_STATE;
2481          !!!next-input-character;          !!!next-input-character;
2482    
2483          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2484    
2485          redo A;          redo A;
2486        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2487            !!!cp (167);
2488          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2489          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2490          ## reconsume          ## reconsume
2491    
2492          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2493          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2494    
2495          redo A;          redo A;
2496        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2497                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2498          !!!next-input-character;          $self->{state} = PUBLIC_STATE;
2499          if ($self->{next_input_character} == 0x0055 or # U          $self->{state_keyword} = chr $self->{next_char};
2500              $self->{next_input_character} == 0x0075) { # u          !!!next-input-character;
2501            !!!next-input-character;          redo A;
2502            if ($self->{next_input_character} == 0x0042 or # B        } elsif ($self->{next_char} == 0x0053 or # S
2503                $self->{next_input_character} == 0x0062) { # b                 $self->{next_char} == 0x0073) { # s
2504              !!!next-input-character;          $self->{state} = SYSTEM_STATE;
2505              if ($self->{next_input_character} == 0x004C or # L          $self->{state_keyword} = chr $self->{next_char};
2506                  $self->{next_input_character} == 0x006C) { # l          !!!next-input-character;
2507                !!!next-input-character;          redo A;
2508                if ($self->{next_input_character} == 0x0049 or # I        } else {
2509                    $self->{next_input_character} == 0x0069) { # i          !!!cp (180);
2510                  !!!next-input-character;          !!!parse-error (type => 'string after DOCTYPE name');
2511                  if ($self->{next_input_character} == 0x0043 or # C          $self->{current_token}->{quirks} = 1;
2512                      $self->{next_input_character} == 0x0063) { # c  
2513                    $self->{state} = 'before DOCTYPE public identifier';          $self->{state} = BOGUS_DOCTYPE_STATE;
2514                    !!!next-input-character;          !!!next-input-character;
2515                    redo A;          redo A;
2516                  }        }
2517                }      } elsif ($self->{state} == PUBLIC_STATE) {
2518              }        ## ASCII case-insensitive
2519            }        if ($self->{next_char} == [
2520          }              undef,
2521                0x0055, # U
2522          #              0x0042, # B
2523        } elsif ($self->{next_input_character} == 0x0053 or # S              0x004C, # L
2524                 $self->{next_input_character} == 0x0073) { # s              0x0049, # I
2525          !!!next-input-character;            ]->[length $self->{state_keyword}] or
2526          if ($self->{next_input_character} == 0x0059 or # Y            $self->{next_char} == [
2527              $self->{next_input_character} == 0x0079) { # y              undef,
2528            !!!next-input-character;              0x0075, # u
2529            if ($self->{next_input_character} == 0x0053 or # S              0x0062, # b
2530                $self->{next_input_character} == 0x0073) { # s              0x006C, # l
2531              !!!next-input-character;              0x0069, # i
2532              if ($self->{next_input_character} == 0x0054 or # T            ]->[length $self->{state_keyword}]) {
2533                  $self->{next_input_character} == 0x0074) { # t          !!!cp (175);
2534                !!!next-input-character;          ## Stay in the state.
2535                if ($self->{next_input_character} == 0x0045 or # E          $self->{state_keyword} .= chr $self->{next_char};
2536                    $self->{next_input_character} == 0x0065) { # e          !!!next-input-character;
2537                  !!!next-input-character;          redo A;
2538                  if ($self->{next_input_character} == 0x004D or # M        } elsif ((length $self->{state_keyword}) == 5 and
2539                      $self->{next_input_character} == 0x006D) { # m                 ($self->{next_char} == 0x0043 or # C
2540                    $self->{state} = 'before DOCTYPE system identifier';                  $self->{next_char} == 0x0063)) { # c
2541                    !!!next-input-character;          !!!cp (168);
2542                    redo A;          $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2543                  }          !!!next-input-character;
2544                }          redo A;
2545              }        } else {
2546            }          !!!cp (169);
2547          }          !!!parse-error (type => 'string after DOCTYPE name',
2548                            line => $self->{line_prev},
2549                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2550            $self->{current_token}->{quirks} = 1;
2551    
2552            $self->{state} = BOGUS_DOCTYPE_STATE;
2553            ## Reconsume.
2554            redo A;
2555          }
2556        } elsif ($self->{state} == SYSTEM_STATE) {
2557          ## ASCII case-insensitive
2558          if ($self->{next_char} == [
2559                undef,
2560                0x0059, # Y
2561                0x0053, # S
2562                0x0054, # T
2563                0x0045, # E
2564              ]->[length $self->{state_keyword}] or
2565              $self->{next_char} == [
2566                undef,
2567                0x0079, # y
2568                0x0073, # s
2569                0x0074, # t
2570                0x0065, # e
2571              ]->[length $self->{state_keyword}]) {
2572            !!!cp (170);
2573            ## Stay in the state.
2574            $self->{state_keyword} .= chr $self->{next_char};
2575            !!!next-input-character;
2576            redo A;
2577          } elsif ((length $self->{state_keyword}) == 5 and
2578                   ($self->{next_char} == 0x004D or # M
2579                    $self->{next_char} == 0x006D)) { # m
2580            !!!cp (171);
2581            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2582            !!!next-input-character;
2583            redo A;
2584          } else {
2585            !!!cp (172);
2586            !!!parse-error (type => 'string after DOCTYPE name',
2587                            line => $self->{line_prev},
2588                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2589            $self->{current_token}->{quirks} = 1;
2590    
2591          #          $self->{state} = BOGUS_DOCTYPE_STATE;
2592        } else {          ## Reconsume.
2593          !!!next-input-character;          redo A;
         #  
2594        }        }
2595        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
       !!!parse-error (type => 'string after DOCTYPE name');  
       $self->{state} = 'bogus DOCTYPE';  
       # next-input-character is already done  
       redo A;  
     } elsif ($self->{state} eq 'before DOCTYPE public identifier') {  
2596        if ({        if ({
2597              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2598              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2599            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2600            !!!cp (181);
2601          ## Stay in the state          ## Stay in the state
2602          !!!next-input-character;          !!!next-input-character;
2603          redo A;          redo A;
2604        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2605            !!!cp (182);
2606          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2607          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2608          !!!next-input-character;          !!!next-input-character;
2609          redo A;          redo A;
2610        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2611            !!!cp (183);
2612          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2613          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2614          !!!next-input-character;          !!!next-input-character;
2615          redo A;          redo A;
2616        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2617            !!!cp (184);
2618          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2619    
2620          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2621          !!!next-input-character;          !!!next-input-character;
2622    
2623          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2624          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2625    
2626          redo A;          redo A;
2627        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2628            !!!cp (185);
2629          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2630    
2631          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2632          ## reconsume          ## reconsume
2633    
2634          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2635          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2636    
2637          redo A;          redo A;
2638        } else {        } else {
2639            !!!cp (186);
2640          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2641          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2642    
2643            $self->{state} = BOGUS_DOCTYPE_STATE;
2644          !!!next-input-character;          !!!next-input-character;
2645          redo A;          redo A;
2646        }        }
2647      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2648        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2649          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2650            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2651          !!!next-input-character;          !!!next-input-character;
2652          redo A;          redo A;
2653        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2654            !!!cp (188);
2655          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2656    
2657          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2658            !!!next-input-character;
2659    
2660            $self->{current_token}->{quirks} = 1;
2661            !!!emit ($self->{current_token}); # DOCTYPE
2662    
2663            redo A;
2664          } elsif ($self->{next_char} == -1) {
2665            !!!cp (189);
2666            !!!parse-error (type => 'unclosed PUBLIC literal');
2667    
2668            $self->{state} = DATA_STATE;
2669          ## reconsume          ## reconsume
2670    
2671          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2672          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2673    
2674          redo A;          redo A;
2675        } else {        } else {
2676            !!!cp (190);
2677          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2678              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2679            $self->{read_until}->($self->{current_token}->{public_identifier},
2680                                  q[">],
2681                                  length $self->{current_token}->{public_identifier});
2682    
2683          ## Stay in the state          ## Stay in the state
2684          !!!next-input-character;          !!!next-input-character;
2685          redo A;          redo A;
2686        }        }
2687      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2688        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2689          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2690            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2691            !!!next-input-character;
2692            redo A;
2693          } elsif ($self->{next_char} == 0x003E) { # >
2694            !!!cp (192);
2695            !!!parse-error (type => 'unclosed PUBLIC literal');
2696    
2697            $self->{state} = DATA_STATE;
2698          !!!next-input-character;          !!!next-input-character;
2699    
2700            $self->{current_token}->{quirks} = 1;
2701            !!!emit ($self->{current_token}); # DOCTYPE
2702    
2703          redo A;          redo A;
2704        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2705            !!!cp (193);
2706          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2707    
2708          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2709          ## reconsume          ## reconsume
2710    
2711          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2712          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2713    
2714          redo A;          redo A;
2715        } else {        } else {
2716            !!!cp (194);
2717          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2718              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2719            $self->{read_until}->($self->{current_token}->{public_identifier},
2720                                  q['>],
2721                                  length $self->{current_token}->{public_identifier});
2722    
2723          ## Stay in the state          ## Stay in the state
2724          !!!next-input-character;          !!!next-input-character;
2725          redo A;          redo A;
2726        }        }
2727      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2728        if ({        if ({
2729              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2730              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2731            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2732            !!!cp (195);
2733          ## Stay in the state          ## Stay in the state
2734          !!!next-input-character;          !!!next-input-character;
2735          redo A;          redo A;
2736        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2737            !!!cp (196);
2738          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2739          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2740          !!!next-input-character;          !!!next-input-character;
2741          redo A;          redo A;
2742        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2743            !!!cp (197);
2744          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2745          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2746          !!!next-input-character;          !!!next-input-character;
2747          redo A;          redo A;
2748        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2749          $self->{state} = 'data';          !!!cp (198);
2750            $self->{state} = DATA_STATE;
2751          !!!next-input-character;          !!!next-input-character;
2752    
2753          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2754    
2755          redo A;          redo A;
2756        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2757            !!!cp (199);
2758          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2759    
2760          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2761          ## reconsume          ## reconsume
2762    
2763          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2764          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2765    
2766          redo A;          redo A;
2767        } else {        } else {
2768            !!!cp (200);
2769          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2770          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2771    
2772            $self->{state} = BOGUS_DOCTYPE_STATE;
2773          !!!next-input-character;          !!!next-input-character;
2774          redo A;          redo A;
2775        }        }
2776      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2777        if ({        if ({
2778              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2779              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2780            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2781            !!!cp (201);
2782          ## Stay in the state          ## Stay in the state
2783          !!!next-input-character;          !!!next-input-character;
2784          redo A;          redo A;
2785        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2786            !!!cp (202);
2787          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2788          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2789          !!!next-input-character;          !!!next-input-character;
2790          redo A;          redo A;
2791        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2792            !!!cp (203);
2793          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2794          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2795          !!!next-input-character;          !!!next-input-character;
2796          redo A;          redo A;
2797        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2798            !!!cp (204);
2799          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2800          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2801          !!!next-input-character;          !!!next-input-character;
2802    
2803          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2804          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2805    
2806          redo A;          redo A;
2807        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2808            !!!cp (205);
2809          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2810    
2811          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2812          ## reconsume          ## reconsume
2813    
2814          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2815          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2816    
2817          redo A;          redo A;
2818        } else {        } else {
2819            !!!cp (206);
2820          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2821          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2822    
2823            $self->{state} = BOGUS_DOCTYPE_STATE;
2824          !!!next-input-character;          !!!next-input-character;
2825          redo A;          redo A;
2826        }        }
2827      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2828        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2829          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2830            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2831          !!!next-input-character;          !!!next-input-character;
2832          redo A;          redo A;
2833        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2834            !!!cp (208);
2835            !!!parse-error (type => 'unclosed SYSTEM literal');
2836    
2837            $self->{state} = DATA_STATE;
2838            !!!next-input-character;
2839    
2840            $self->{current_token}->{quirks} = 1;
2841            !!!emit ($self->{current_token}); # DOCTYPE
2842    
2843            redo A;
2844          } elsif ($self->{next_char} == -1) {
2845            !!!cp (209);
2846          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2847    
2848          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2849          ## reconsume          ## reconsume
2850    
2851          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2852          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2853    
2854          redo A;          redo A;
2855        } else {        } else {
2856            !!!cp (210);
2857          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2858              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2859            $self->{read_until}->($self->{current_token}->{system_identifier},
2860                                  q[">],
2861                                  length $self->{current_token}->{system_identifier});
2862    
2863          ## Stay in the state          ## Stay in the state
2864          !!!next-input-character;          !!!next-input-character;
2865          redo A;          redo A;
2866        }        }
2867      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2868        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2869          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2870            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2871          !!!next-input-character;          !!!next-input-character;
2872          redo A;          redo A;
2873        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2874            !!!cp (212);
2875          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2876    
2877          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2878            !!!next-input-character;
2879    
2880            $self->{current_token}->{quirks} = 1;
2881            !!!emit ($self->{current_token}); # DOCTYPE
2882    
2883            redo A;
2884          } elsif ($self->{next_char} == -1) {
2885            !!!cp (213);
2886            !!!parse-error (type => 'unclosed SYSTEM literal');
2887    
2888            $self->{state} = DATA_STATE;
2889          ## reconsume          ## reconsume
2890    
2891          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2892          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2893    
2894          redo A;          redo A;
2895        } else {        } else {
2896            !!!cp (214);
2897          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2898              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2899            $self->{read_until}->($self->{current_token}->{system_identifier},
2900                                  q['>],
2901                                  length $self->{current_token}->{system_identifier});
2902    
2903          ## Stay in the state          ## Stay in the state
2904          !!!next-input-character;          !!!next-input-character;
2905          redo A;          redo A;
2906        }        }
2907      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2908        if ({        if ({
2909              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2910              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2911            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2912            !!!cp (215);
2913          ## Stay in the state          ## Stay in the state
2914          !!!next-input-character;          !!!next-input-character;
2915          redo A;          redo A;
2916        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2917          $self->{state} = 'data';          !!!cp (216);
2918            $self->{state} = DATA_STATE;
2919          !!!next-input-character;          !!!next-input-character;
2920    
2921          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2922    
2923          redo A;          redo A;
2924        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2925            !!!cp (217);
2926          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2927            $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
2928          ## reconsume          ## reconsume
2929    
2930          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2931          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2932    
2933          redo A;          redo A;
2934        } else {        } else {
2935            !!!cp (218);
2936          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2937          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2938    
2939            $self->{state} = BOGUS_DOCTYPE_STATE;
2940          !!!next-input-character;          !!!next-input-character;
2941          redo A;          redo A;
2942        }        }
2943      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2944        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2945          $self->{state} = 'data';          !!!cp (219);
2946            $self->{state} = DATA_STATE;
2947          !!!next-input-character;          !!!next-input-character;
2948    
         delete $self->{current_token}->{correct};  
2949          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2950    
2951          redo A;          redo A;
2952        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2953            !!!cp (220);
2954          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2955          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2956          ## reconsume          ## reconsume
2957    
         delete $self->{current_token}->{correct};  
2958          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2959    
2960          redo A;          redo A;
2961        } else {        } else {
2962            !!!cp (221);
2963            my $s = '';
2964            $self->{read_until}->($s, q[>], 0);
2965    
2966          ## Stay in the state          ## Stay in the state
2967          !!!next-input-character;          !!!next-input-character;
2968          redo A;          redo A;
2969        }        }
2970      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2971        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2972      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2973    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2974          
2975    die "$0: _get_next_token: unexpected case";        if ($self->{next_char} == 0x005D) { # ]
2976  } # _get_next_token          !!!cp (221.1);
2977            $self->{state} = CDATA_SECTION_MSE1_STATE;
2978            !!!next-input-character;
2979            redo A;
2980          } elsif ($self->{next_char} == -1) {
2981            $self->{state} = DATA_STATE;
2982            !!!next-input-character;
2983            if (length $self->{current_token}->{data}) { # character
2984              !!!cp (221.2);
2985              !!!emit ($self->{current_token}); # character
2986            } else {
2987              !!!cp (221.3);
2988              ## No token to emit. $self->{current_token} is discarded.
2989            }        
2990            redo A;
2991          } else {
2992            !!!cp (221.4);
2993            $self->{current_token}->{data} .= chr $self->{next_char};
2994            $self->{read_until}->($self->{current_token}->{data},
2995                                  q<]>,
2996                                  length $self->{current_token}->{data});
2997    
2998  sub _tokenize_attempt_to_consume_an_entity ($$) {          ## Stay in the state.
2999    my ($self, $in_attr) = @_;          !!!next-input-character;
3000            redo A;
3001          }
3002    
3003    if ({        ## ISSUE: "text tokens" in spec.
3004         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
3005         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{next_char} == 0x005D) { # ]
3006        }->{$self->{next_input_character}}) {          !!!cp (221.5);
3007      ## Don't consume          $self->{state} = CDATA_SECTION_MSE2_STATE;
3008      ## No error          !!!next-input-character;
3009      return undef;          redo A;
3010    } elsif ($self->{next_input_character} == 0x0023) { # #        } else {
3011      !!!next-input-character;          !!!cp (221.6);
3012      if ($self->{next_input_character} == 0x0078 or # x          $self->{current_token}->{data} .= ']';
3013          $self->{next_input_character} == 0x0058) { # X          $self->{state} = CDATA_SECTION_STATE;
3014        my $code;          ## Reconsume.
3015        X: {          redo A;
3016          my $x_char = $self->{next_input_character};        }
3017          !!!next-input-character;      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3018          if (0x0030 <= $self->{next_input_character} and        if ($self->{next_char} == 0x003E) { # >
3019              $self->{next_input_character} <= 0x0039) { # 0..9          $self->{state} = DATA_STATE;
3020            $code ||= 0;          !!!next-input-character;
3021            $code *= 0x10;          if (length $self->{current_token}->{data}) { # character
3022            $code += $self->{next_input_character} - 0x0030;            !!!cp (221.7);
3023            redo X;            !!!emit ($self->{current_token}); # character
         } elsif (0x0061 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0066) { # a..f  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_input_character} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0046) { # A..F  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_input_character} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $code) { # no hexadecimal digit  
           !!!parse-error (type => 'bare hcro');  
           !!!back-next-input-character ($x_char, $self->{next_input_character});  
           $self->{next_input_character} = 0x0023; # #  
           return undef;  
         } elsif ($self->{next_input_character} == 0x003B) { # ;  
           !!!next-input-character;  
3024          } else {          } else {
3025            !!!parse-error (type => 'no refc');            !!!cp (221.8);
3026              ## No token to emit. $self->{current_token} is discarded.
3027          }          }
3028            redo A;
3029          } elsif ($self->{next_char} == 0x005D) { # ]
3030            !!!cp (221.9); # character
3031            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
3032            ## Stay in the state.
3033            !!!next-input-character;
3034            redo A;
3035          } else {
3036            !!!cp (221.11);
3037            $self->{current_token}->{data} .= ']]'; # character
3038            $self->{state} = CDATA_SECTION_STATE;
3039            ## Reconsume.
3040            redo A;
3041          }
3042        } elsif ($self->{state} == ENTITY_STATE) {
3043          if ({
3044            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3045            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3046            $self->{entity_additional} => 1,
3047          }->{$self->{next_char}}) {
3048            !!!cp (1001);
3049            ## Don't consume
3050            ## No error
3051            ## Return nothing.
3052            #
3053          } elsif ($self->{next_char} == 0x0023) { # #
3054            !!!cp (999);
3055            $self->{state} = ENTITY_HASH_STATE;
3056            $self->{state_keyword} = '#';
3057            !!!next-input-character;
3058            redo A;
3059          } elsif ((0x0041 <= $self->{next_char} and
3060                    $self->{next_char} <= 0x005A) or # A..Z
3061                   (0x0061 <= $self->{next_char} and
3062                    $self->{next_char} <= 0x007A)) { # a..z
3063            !!!cp (998);
3064            require Whatpm::_NamedEntityList;
3065            $self->{state} = ENTITY_NAME_STATE;
3066            $self->{state_keyword} = chr $self->{next_char};
3067            $self->{entity__value} = $self->{state_keyword};
3068            $self->{entity__match} = 0;
3069            !!!next-input-character;
3070            redo A;
3071          } else {
3072            !!!cp (1027);
3073            !!!parse-error (type => 'bare ero');
3074            ## Return nothing.
3075            #
3076          }
3077    
3078          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        ## NOTE: No character is consumed by the "consume a character
3079            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);        ## reference" algorithm.  In other word, there is an "&" character
3080            $code = 0xFFFD;        ## that does not introduce a character reference, which would be
3081          } elsif ($code > 0x10FFFF) {        ## appended to the parent element or the attribute value in later
3082            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);        ## process of the tokenizer.
3083            $code = 0xFFFD;  
3084          } elsif ($code == 0x000D) {        if ($self->{prev_state} == DATA_STATE) {
3085            !!!parse-error (type => 'CR character reference');          !!!cp (997);
3086            $code = 0x000A;          $self->{state} = $self->{prev_state};
3087          } elsif (0x80 <= $code and $code <= 0x9F) {          ## Reconsume.
3088            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3089            $code = $c1_entity_char->{$code};                    line => $self->{line_prev},
3090          }                    column => $self->{column_prev},
3091                     });
3092          return {type => 'character', data => chr $code};          redo A;
3093        } # X        } else {
3094      } elsif (0x0030 <= $self->{next_input_character} and          !!!cp (996);
3095               $self->{next_input_character} <= 0x0039) { # 0..9          $self->{current_attribute}->{value} .= '&';
3096        my $code = $self->{next_input_character} - 0x0030;          $self->{state} = $self->{prev_state};
3097        !!!next-input-character;          ## Reconsume.
3098                  redo A;
3099        while (0x0030 <= $self->{next_input_character} and        }
3100                  $self->{next_input_character} <= 0x0039) { # 0..9      } elsif ($self->{state} == ENTITY_HASH_STATE) {
3101          $code *= 10;        if ($self->{next_char} == 0x0078 or # x
3102          $code += $self->{next_input_character} - 0x0030;            $self->{next_char} == 0x0058) { # X
3103            !!!cp (995);
3104            $self->{state} = HEXREF_X_STATE;
3105            $self->{state_keyword} .= chr $self->{next_char};
3106            !!!next-input-character;
3107            redo A;
3108          } elsif (0x0030 <= $self->{next_char} and
3109                   $self->{next_char} <= 0x0039) { # 0..9
3110            !!!cp (994);
3111            $self->{state} = NCR_NUM_STATE;
3112            $self->{state_keyword} = $self->{next_char} - 0x0030;
3113            !!!next-input-character;
3114            redo A;
3115          } else {
3116            !!!parse-error (type => 'bare nero',
3117                            line => $self->{line_prev},
3118                            column => $self->{column_prev} - 1);
3119    
3120            ## NOTE: According to the spec algorithm, nothing is returned,
3121            ## and then "&#" is appended to the parent element or the attribute
3122            ## value in the later processing.
3123    
3124            if ($self->{prev_state} == DATA_STATE) {
3125              !!!cp (1019);
3126              $self->{state} = $self->{prev_state};
3127              ## Reconsume.
3128              !!!emit ({type => CHARACTER_TOKEN,
3129                        data => '&#',
3130                        line => $self->{line_prev},
3131                        column => $self->{column_prev} - 1,
3132                       });
3133              redo A;
3134            } else {
3135              !!!cp (993);
3136              $self->{current_attribute}->{value} .= '&#';
3137              $self->{state} = $self->{prev_state};
3138              ## Reconsume.
3139              redo A;
3140            }
3141          }
3142        } elsif ($self->{state} == NCR_NUM_STATE) {
3143          if (0x0030 <= $self->{next_char} and
3144              $self->{next_char} <= 0x0039) { # 0..9
3145            !!!cp (1012);
3146            $self->{state_keyword} *= 10;
3147            $self->{state_keyword} += $self->{next_char} - 0x0030;
3148                    
3149            ## Stay in the state.
3150          !!!next-input-character;          !!!next-input-character;
3151            redo A;
3152          } elsif ($self->{next_char} == 0x003B) { # ;
3153            !!!cp (1013);
3154            !!!next-input-character;
3155            #
3156          } else {
3157            !!!cp (1014);
3158            !!!parse-error (type => 'no refc');
3159            ## Reconsume.
3160            #
3161          }
3162    
3163          my $code = $self->{state_keyword};
3164          my $l = $self->{line_prev};
3165          my $c = $self->{column_prev};
3166          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3167            !!!cp (1015);
3168            !!!parse-error (type => 'invalid character reference',
3169                            text => (sprintf 'U+%04X', $code),
3170                            line => $l, column => $c);
3171            $code = 0xFFFD;
3172          } elsif ($code > 0x10FFFF) {
3173            !!!cp (1016);
3174            !!!parse-error (type => 'invalid character reference',
3175                            text => (sprintf 'U-%08X', $code),
3176                            line => $l, column => $c);
3177            $code = 0xFFFD;
3178          } elsif ($code == 0x000D) {
3179            !!!cp (1017);
3180            !!!parse-error (type => 'CR character reference',
3181                            line => $l, column => $c);
3182            $code = 0x000A;
3183          } elsif (0x80 <= $code and $code <= 0x9F) {
3184            !!!cp (1018);
3185            !!!parse-error (type => 'C1 character reference',
3186                            text => (sprintf 'U+%04X', $code),
3187                            line => $l, column => $c);
3188            $code = $c1_entity_char->{$code};
3189        }        }
3190    
3191        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{prev_state} == DATA_STATE) {
3192            !!!cp (992);
3193            $self->{state} = $self->{prev_state};
3194            ## Reconsume.
3195            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3196                      line => $l, column => $c,
3197                     });
3198            redo A;
3199          } else {
3200            !!!cp (991);
3201            $self->{current_attribute}->{value} .= chr $code;
3202            $self->{current_attribute}->{has_reference} = 1;
3203            $self->{state} = $self->{prev_state};
3204            ## Reconsume.
3205            redo A;
3206          }
3207        } elsif ($self->{state} == HEXREF_X_STATE) {
3208          if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3209              (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3210              (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3211            # 0..9, A..F, a..f
3212            !!!cp (990);
3213            $self->{state} = HEXREF_HEX_STATE;
3214            $self->{state_keyword} = 0;
3215            ## Reconsume.
3216            redo A;
3217          } else {
3218            !!!parse-error (type => 'bare hcro',
3219                            line => $self->{line_prev},
3220                            column => $self->{column_prev} - 2);
3221    
3222            ## NOTE: According to the spec algorithm, nothing is returned,
3223            ## and then "&#" followed by "X" or "x" is appended to the parent
3224            ## element or the attribute value in the later processing.
3225    
3226            if ($self->{prev_state} == DATA_STATE) {
3227              !!!cp (1005);
3228              $self->{state} = $self->{prev_state};
3229              ## Reconsume.
3230              !!!emit ({type => CHARACTER_TOKEN,
3231                        data => '&' . $self->{state_keyword},
3232                        line => $self->{line_prev},
3233                        column => $self->{column_prev} - length $self->{state_keyword},
3234                       });
3235              redo A;
3236            } else {
3237              !!!cp (989);
3238              $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3239              $self->{state} = $self->{prev_state};
3240              ## Reconsume.
3241              redo A;
3242            }
3243          }
3244        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3245          if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3246            # 0..9
3247            !!!cp (1002);
3248            $self->{state_keyword} *= 0x10;
3249            $self->{state_keyword} += $self->{next_char} - 0x0030;
3250            ## Stay in the state.
3251            !!!next-input-character;
3252            redo A;
3253          } elsif (0x0061 <= $self->{next_char} and
3254                   $self->{next_char} <= 0x0066) { # a..f
3255            !!!cp (1003);
3256            $self->{state_keyword} *= 0x10;
3257            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3258            ## Stay in the state.
3259            !!!next-input-character;
3260            redo A;
3261          } elsif (0x0041 <= $self->{next_char} and
3262                   $self->{next_char} <= 0x0046) { # A..F
3263            !!!cp (1004);
3264            $self->{state_keyword} *= 0x10;
3265            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3266            ## Stay in the state.
3267            !!!next-input-character;
3268            redo A;
3269          } elsif ($self->{next_char} == 0x003B) { # ;
3270            !!!cp (1006);
3271          !!!next-input-character;          !!!next-input-character;
3272            #
3273        } else {        } else {
3274          !!!parse-error (type => 'no refc');          !!!cp (1007);
3275            !!!parse-error (type => 'no refc',
3276                            line => $self->{line},
3277                            column => $self->{column});
3278            ## Reconsume.
3279            #
3280        }        }
3281    
3282          my $code = $self->{state_keyword};
3283          my $l = $self->{line_prev};
3284          my $c = $self->{column_prev};
3285        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3286          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1008);
3287            !!!parse-error (type => 'invalid character reference',
3288                            text => (sprintf 'U+%04X', $code),
3289                            line => $l, column => $c);
3290          $code = 0xFFFD;          $code = 0xFFFD;
3291        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3292          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1009);
3293            !!!parse-error (type => 'invalid character reference',
3294                            text => (sprintf 'U-%08X', $code),
3295                            line => $l, column => $c);
3296          $code = 0xFFFD;          $code = 0xFFFD;
3297        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3298          !!!parse-error (type => 'CR character reference');          !!!cp (1010);
3299            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3300          $code = 0x000A;          $code = 0x000A;
3301        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3302          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1011);
3303            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3304          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3305        }        }
3306          
3307        return {type => 'character', data => chr $code};        if ($self->{prev_state} == DATA_STATE) {
3308      } else {          !!!cp (988);
3309        !!!parse-error (type => 'bare nero');          $self->{state} = $self->{prev_state};
3310        !!!back-next-input-character ($self->{next_input_character});          ## Reconsume.
3311        $self->{next_input_character} = 0x0023; # #          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3312        return undef;                    line => $l, column => $c,
3313      }                   });
3314    } elsif ((0x0041 <= $self->{next_input_character} and          redo A;
3315              $self->{next_input_character} <= 0x005A) or        } else {
3316             (0x0061 <= $self->{next_input_character} and          !!!cp (987);
3317              $self->{next_input_character} <= 0x007A)) {          $self->{current_attribute}->{value} .= chr $code;
3318      my $entity_name = chr $self->{next_input_character};          $self->{current_attribute}->{has_reference} = 1;
3319      !!!next-input-character;          $self->{state} = $self->{prev_state};
3320            ## Reconsume.
3321      my $value = $entity_name;          redo A;
3322      my $match = 0;        }
3323      require Whatpm::_NamedEntityList;      } elsif ($self->{state} == ENTITY_NAME_STATE) {
3324      our $EntityChar;        if (length $self->{state_keyword} < 30 and
3325              ## NOTE: Some number greater than the maximum length of entity name
3326      while (length $entity_name < 10 and            ((0x0041 <= $self->{next_char} and # a
3327             ## NOTE: Some number greater than the maximum length of entity name              $self->{next_char} <= 0x005A) or # x
3328             ((0x0041 <= $self->{next_input_character} and # a             (0x0061 <= $self->{next_char} and # a
3329               $self->{next_input_character} <= 0x005A) or # x              $self->{next_char} <= 0x007A) or # z
3330              (0x0061 <= $self->{next_input_character} and # a             (0x0030 <= $self->{next_char} and # 0
3331               $self->{next_input_character} <= 0x007A) or # z              $self->{next_char} <= 0x0039) or # 9
3332              (0x0030 <= $self->{next_input_character} and # 0             $self->{next_char} == 0x003B)) { # ;
3333               $self->{next_input_character} <= 0x0039) or # 9          our $EntityChar;
3334              $self->{next_input_character} == 0x003B)) { # ;          $self->{state_keyword} .= chr $self->{next_char};
3335        $entity_name .= chr $self->{next_input_character};          if (defined $EntityChar->{$self->{state_keyword}}) {
3336        if (defined $EntityChar->{$entity_name}) {            if ($self->{next_char} == 0x003B) { # ;
3337          if ($self->{next_input_character} == 0x003B) { # ;              !!!cp (1020);
3338            $value = $EntityChar->{$entity_name};              $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3339            $match = 1;              $self->{entity__match} = 1;
3340            !!!next-input-character;              !!!next-input-character;
3341            last;              #
3342              } else {
3343                !!!cp (1021);
3344                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3345                $self->{entity__match} = -1;
3346                ## Stay in the state.
3347                !!!next-input-character;
3348                redo A;
3349              }
3350          } else {          } else {
3351            $value = $EntityChar->{$entity_name};            !!!cp (1022);
3352            $match = -1;            $self->{entity__value} .= chr $self->{next_char};
3353              $self->{entity__match} *= 2;
3354              ## Stay in the state.
3355            !!!next-input-character;            !!!next-input-character;
3356              redo A;
3357          }          }
       } else {  
         $value .= chr $self->{next_input_character};  
         $match *= 2;  
         !!!next-input-character;  
3358        }        }
3359      }  
3360              my $data;
3361      if ($match > 0) {        my $has_ref;
3362        return {type => 'character', data => $value};        if ($self->{entity__match} > 0) {
3363      } elsif ($match < 0) {          !!!cp (1023);
3364        !!!parse-error (type => 'no refc');          $data = $self->{entity__value};
3365        if ($in_attr and $match < -1) {          $has_ref = 1;
3366          return {type => 'character', data => '&'.$entity_name};          #
3367          } elsif ($self->{entity__match} < 0) {
3368            !!!parse-error (type => 'no refc');
3369            if ($self->{prev_state} != DATA_STATE and # in attribute
3370                $self->{entity__match} < -1) {
3371              !!!cp (1024);
3372              $data = '&' . $self->{state_keyword};
3373              #
3374            } else {
3375              !!!cp (1025);
3376              $data = $self->{entity__value};
3377              $has_ref = 1;
3378              #
3379            }
3380        } else {        } else {
3381          return {type => 'character', data => $value};          !!!cp (1026);
3382            !!!parse-error (type => 'bare ero',
3383                            line => $self->{line_prev},
3384                            column => $self->{column_prev} - length $self->{state_keyword});
3385            $data = '&' . $self->{state_keyword};
3386            #
3387          }
3388      
3389          ## NOTE: In these cases, when a character reference is found,
3390          ## it is consumed and a character token is returned, or, otherwise,
3391          ## nothing is consumed and returned, according to the spec algorithm.
3392          ## In this implementation, anything that has been examined by the
3393          ## tokenizer is appended to the parent element or the attribute value
3394          ## as string, either literal string when no character reference or
3395          ## entity-replaced string otherwise, in this stage, since any characters
3396          ## that would not be consumed are appended in the data state or in an
3397          ## appropriate attribute value state anyway.
3398    
3399          if ($self->{prev_state} == DATA_STATE) {
3400            !!!cp (986);
3401            $self->{state} = $self->{prev_state};
3402            ## Reconsume.
3403            !!!emit ({type => CHARACTER_TOKEN,
3404                      data => $data,
3405                      line => $self->{line_prev},
3406                      column => $self->{column_prev} + 1 - length $self->{state_keyword},
3407                     });
3408            redo A;
3409          } else {
3410            !!!cp (985);
3411            $self->{current_attribute}->{value} .= $data;
3412            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3413            $self->{state} = $self->{prev_state};
3414            ## Reconsume.
3415            redo A;
3416        }        }
3417      } else {      } else {
3418        !!!parse-error (type => 'bare ero');        die "$0: $self->{state}: Unknown state";
       ## NOTE: No characters are consumed in the spec.  
       return {type => 'character', data => '&'.$value};  
3419      }      }
3420    } else {    } # A  
3421      ## no characters are consumed  
3422      !!!parse-error (type => 'bare ero');    die "$0: _get_next_token: unexpected case";
3423      return undef;  } # _get_next_token
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3424    
3425  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3426    my $self = shift;    my $self = shift;
# Line 1780  sub _initialize_tree_constructor ($) { Line 3429  sub _initialize_tree_constructor ($) {
3429    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3430    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3431    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3432      $self->{document}->set_user_data (manakai_source_line => 1);
3433      $self->{document}->set_user_data (manakai_source_column => 1);
3434  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3435    
3436  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1806  sub _construct_tree ($) { Line 3457  sub _construct_tree ($) {
3457        
3458    !!!next-token;    !!!next-token;
3459    
   $self->{insertion_mode} = 'before head';  
3460    undef $self->{form_element};    undef $self->{form_element};
3461    undef $self->{head_element};    undef $self->{head_element};
3462    $self->{open_elements} = [];    $self->{open_elements} = [];
3463    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3464    
3465      ## NOTE: The "initial" insertion mode.
3466    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3467    
3468      ## NOTE: The "before html" insertion mode.
3469    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3470      $self->{insertion_mode} = BEFORE_HEAD_IM;
3471    
3472      ## NOTE: The "before head" insertion mode and so on.
3473    $self->_tree_construction_main;    $self->_tree_construction_main;
3474  } # _construct_tree  } # _construct_tree
3475    
3476  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3477    my $self = shift;    my $self = shift;
3478    
3479      ## NOTE: "initial" insertion mode
3480    
3481    INITIAL: {    INITIAL: {
3482      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
3483        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3484        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
3485        ## language.        ## language.
3486        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3487        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3488        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3489        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3490            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3491          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3492            !!!parse-error (type => 'not HTML5', token => $token);
3493        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3494          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!cp ('t2');
3495          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3496          } elsif (defined $token->{public_identifier}) {
3497            if ($token->{public_identifier} eq 'XSLT-compat') {
3498              !!!cp ('t1.2');
3499              !!!parse-error (type => 'XSLT-compat', token => $token,
3500                              level => $self->{level}->{should});
3501            } else {
3502              !!!parse-error (type => 'not HTML5', token => $token);
3503            }
3504          } else {
3505            !!!cp ('t3');
3506            #
3507        }        }
3508                
3509        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3510          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3511          ## NOTE: Default value for both |public_id| and |system_id| attributes
3512          ## are empty strings, so that we don't set any value in missing cases.
3513        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3514            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3515        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1846  sub _tree_construction_initial ($) { Line 3518  sub _tree_construction_initial ($) {
3518        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3519        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3520                
3521        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3522            !!!cp ('t4');
3523          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3524        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3525          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3526          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3527          if ({          my $prefix = [
3528            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3529            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3530            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3531            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3532            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3533            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3534            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3535            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3536            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3537            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3538            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3539            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3540            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3541            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3542            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3543            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3544            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3545            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3546            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3547            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3548            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3549            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3550            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3551            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3552            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3553            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3554            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3555            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3556            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3557            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3558            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3559            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3560            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3561            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3562            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3563            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3564            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3565            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3566            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3567            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3568            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3569            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3570            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3571            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3572            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3573            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3574            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3575            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3576            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3577            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3578            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3579            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3580            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3581            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3582            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3583            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3584            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3585            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3586            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3587            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3588            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3589            "-//W3C//DTD W3 HTML//EN" => 1,            }
3590            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3591            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3592            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3593            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3594            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3595            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3596            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3597          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3598                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3599            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3600                !!!cp ('t6');
3601              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3602            } else {            } else {
3603                !!!cp ('t7');
3604              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3605            }            }
3606          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3607                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3608              !!!cp ('t8');
3609            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3610            } else {
3611              !!!cp ('t9');
3612          }          }
3613          } else {
3614            !!!cp ('t10');
3615        }        }
3616        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3617          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3618          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3619          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") {
3620              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3621              ## marked as quirks.
3622            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3623              !!!cp ('t11');
3624            } else {
3625              !!!cp ('t12');
3626          }          }
3627          } else {
3628            !!!cp ('t13');
3629        }        }
3630                
3631        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3632        !!!next-token;        !!!next-token;
3633        return;        return;
3634      } elsif ({      } elsif ({
3635                'start tag' => 1,                START_TAG_TOKEN, 1,
3636                'end tag' => 1,                END_TAG_TOKEN, 1,
3637                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
3638               }->{$token->{type}}) {               }->{$token->{type}}) {
3639        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3640          !!!parse-error (type => 'no DOCTYPE', token => $token);
3641        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3642        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3643        ## reprocess        ## reprocess
3644          !!!ack-later;
3645        return;        return;
3646      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3647        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3648          ## Ignore the token          ## Ignore the token
3649    
3650          unless (length $token->{data}) {          unless (length $token->{data}) {
3651            ## Stay in the phase            !!!cp ('t15');
3652              ## Stay in the insertion mode.
3653            !!!next-token;            !!!next-token;
3654            redo INITIAL;            redo INITIAL;
3655            } else {
3656              !!!cp ('t16');
3657          }          }
3658          } else {
3659            !!!cp ('t17');
3660        }        }
3661    
3662        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3663        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3664        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3665        ## reprocess        ## reprocess
3666        return;        return;
3667      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3668          !!!cp ('t18');
3669        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3670        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3671                
3672        ## Stay in the phase.        ## Stay in the insertion mode.
3673        !!!next-token;        !!!next-token;
3674        redo INITIAL;        redo INITIAL;
3675      } else {      } else {
3676        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
3677      }      }
3678    } # INITIAL    } # INITIAL
3679    
3680      die "$0: _tree_construction_initial: This should be never reached";
3681  } # _tree_construction_initial  } # _tree_construction_initial
3682    
3683  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3684    my $self = shift;    my $self = shift;
3685    
3686      ## NOTE: "before html" insertion mode.
3687        
3688    B: {    B: {
3689        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3690          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3691            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3692          ## Ignore the token          ## Ignore the token
3693          ## Stay in the phase          ## Stay in the insertion mode.
3694          !!!next-token;          !!!next-token;
3695          redo B;          redo B;
3696        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3697            !!!cp ('t20');
3698          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3699          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3700          ## Stay in the phase          ## Stay in the insertion mode.
3701          !!!next-token;          !!!next-token;
3702          redo B;          redo B;
3703        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3704          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3705            ## Ignore the token.            ## Ignore the token.
3706    
3707            unless (length $token->{data}) {            unless (length $token->{data}) {
3708              ## Stay in the phase              !!!cp ('t21');
3709                ## Stay in the insertion mode.
3710              !!!next-token;              !!!next-token;
3711              redo B;              redo B;
3712              } else {
3713                !!!cp ('t22');
3714            }            }
3715            } else {
3716              !!!cp ('t23');
3717          }          }
3718    
3719            $self->{application_cache_selection}->(undef);
3720    
3721          #          #
3722          } elsif ($token->{type} == START_TAG_TOKEN) {
3723            if ($token->{tag_name} eq 'html') {
3724              my $root_element;
3725              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3726              $self->{document}->append_child ($root_element);
3727              push @{$self->{open_elements}},
3728                  [$root_element, $el_category->{html}];
3729    
3730              if ($token->{attributes}->{manifest}) {
3731                !!!cp ('t24');
3732                $self->{application_cache_selection}
3733                    ->($token->{attributes}->{manifest}->{value});
3734                ## ISSUE: Spec is unclear on relative references.
3735                ## According to Hixie (#whatwg 2008-03-19), it should be
3736                ## resolved against the base URI of the document in HTML
3737                ## or xml:base of the element in XHTML.
3738              } else {
3739                !!!cp ('t25');
3740                $self->{application_cache_selection}->(undef);
3741              }
3742    
3743              !!!nack ('t25c');
3744    
3745              !!!next-token;
3746              return; ## Go to the "before head" insertion mode.
3747            } else {
3748              !!!cp ('t25.1');
3749              #
3750            }
3751        } elsif ({        } elsif ({
3752                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3753                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3754                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3755          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3756          #          #
3757        } else {        } else {
3758          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3759        }        }
3760        my $root_element; !!!create-element ($root_element, 'html');  
3761        $self->{document}->append_child ($root_element);      my $root_element;
3762        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3763        ## reprocess      $self->{document}->append_child ($root_element);
3764        #redo B;      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3765        return; ## Go to the main phase.  
3766        $self->{application_cache_selection}->(undef);
3767    
3768        ## NOTE: Reprocess the token.
3769        !!!ack-later;
3770        return; ## Go to the "before head" insertion mode.
3771    
3772        ## ISSUE: There is an issue in the spec
3773    } # B    } # B
3774    
3775      die "$0: _tree_construction_root_element: This should never be reached";
3776  } # _tree_construction_root_element  } # _tree_construction_root_element
3777    
3778  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2043  sub _reset_insertion_mode ($) { Line 3787  sub _reset_insertion_mode ($) {
3787            
3788      ## Step 3      ## Step 3
3789      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"!?  
3790        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3791          $last = 1;          $last = 1;
3792          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3793            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3794                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3795              #          } else {
3796            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3797          }          }
3798        }        }
3799              
3800        ## Step 4..13        ## Step 4..14
3801        my $new_mode = {        my $new_mode;
3802                        select => 'in select',        if ($node->[1] & FOREIGN_EL) {
3803                        td => 'in cell',          !!!cp ('t28.1');
3804                        th => 'in cell',          ## NOTE: Strictly spaking, the line below only applies to MathML and
3805                        tr => 'in row',          ## SVG elements.  Currently the HTML syntax supports only MathML and
3806                        tbody => 'in table body',          ## SVG elements as foreigners.
3807                        thead => 'in table body',          $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3808                        tfoot => 'in table body',        } elsif ($node->[1] & TABLE_CELL_EL) {
3809                        caption => 'in caption',          if ($last) {
3810                        colgroup => 'in column group',            !!!cp ('t28.2');
3811                        table => 'in table',            #
3812                        head => 'in body', # not in head!          } else {
3813                        body => 'in body',            !!!cp ('t28.3');
3814                        frameset => 'in frameset',            $new_mode = IN_CELL_IM;
3815                       }->{$node->[1]};          }
3816          } else {
3817            !!!cp ('t28.4');
3818            $new_mode = {
3819                          select => IN_SELECT_IM,
3820                          ## NOTE: |option| and |optgroup| do not set
3821                          ## insertion mode to "in select" by themselves.
3822                          tr => IN_ROW_IM,
3823                          tbody => IN_TABLE_BODY_IM,
3824                          thead => IN_TABLE_BODY_IM,
3825                          tfoot => IN_TABLE_BODY_IM,
3826                          caption => IN_CAPTION_IM,
3827                          colgroup => IN_COLUMN_GROUP_IM,
3828                          table => IN_TABLE_IM,
3829                          head => IN_BODY_IM, # not in head!
3830                          body => IN_BODY_IM,
3831                          frameset => IN_FRAMESET_IM,
3832                         }->{$node->[0]->manakai_local_name};
3833          }
3834        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3835                
3836        ## Step 14        ## Step 15
3837        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3838          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3839            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3840              $self->{insertion_mode} = BEFORE_HEAD_IM;
3841          } else {          } else {
3842            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3843              !!!cp ('t30');
3844              $self->{insertion_mode} = AFTER_HEAD_IM;
3845          }          }
3846          return;          return;
3847          } else {
3848            !!!cp ('t31');
3849        }        }
3850                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3851        ## Step 16        ## Step 16
3852          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3853          
3854          ## Step 17
3855        $i--;        $i--;
3856        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3857                
3858        ## Step 17        ## Step 18
3859        redo S3;        redo S3;
3860      } # S3      } # S3
3861    
3862      die "$0: _reset_insertion_mode: This line should never be reached";
3863  } # _reset_insertion_mode  } # _reset_insertion_mode
3864    
3865  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2119  sub _tree_construction_main ($) { Line 3881  sub _tree_construction_main ($) {
3881      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3882      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3883        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3884            !!!cp ('t32');
3885          return;          return;
3886        }        }
3887      }      }
# Line 2133  sub _tree_construction_main ($) { Line 3896  sub _tree_construction_main ($) {
3896    
3897        ## Step 6        ## Step 6
3898        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3899            !!!cp ('t33_1');
3900          #          #
3901        } else {        } else {
3902          my $in_open_elements;          my $in_open_elements;
3903          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3904            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3905                !!!cp ('t33');
3906              $in_open_elements = 1;              $in_open_elements = 1;
3907              last OE;              last OE;
3908            }            }
3909          }          }
3910          if ($in_open_elements) {          if ($in_open_elements) {
3911              !!!cp ('t34');
3912            #            #
3913          } else {          } else {
3914              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3915              !!!cp ('t35');
3916            redo S4;            redo S4;
3917          }          }
3918        }        }
# Line 2167  sub _tree_construction_main ($) { Line 3935  sub _tree_construction_main ($) {
3935    
3936        ## Step 11        ## Step 11
3937        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3938            !!!cp ('t36');
3939          ## Step 7'          ## Step 7'
3940          $i++;          $i++;
3941          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3942                    
3943          redo S7;          redo S7;
3944        }        }
3945    
3946          !!!cp ('t37');
3947      } # S7      } # S7
3948    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3949    
3950    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3951      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3952        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3953            !!!cp ('t38');
3954          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3955          return;          return;
3956        }        }
3957      }      }
3958    
3959        !!!cp ('t39');
3960    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3961    
3962    my $parse_rcdata = sub ($$) {    my $insert;
3963      my ($content_model_flag, $insert) = @_;  
3964      my $parse_rcdata = sub ($) {
3965        my ($content_model_flag) = @_;
3966    
3967      ## Step 1      ## Step 1
3968      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3969      my $el;      my $el;
3970      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3971    
3972      ## Step 2      ## Step 2
3973      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3974    
3975      ## Step 3      ## Step 3
3976      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2202  sub _tree_construction_main ($) { Line 3978  sub _tree_construction_main ($) {
3978    
3979      ## Step 4      ## Step 4
3980      my $text = '';      my $text = '';
3981        !!!nack ('t40.1');
3982      !!!next-token;      !!!next-token;
3983      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3984          !!!cp ('t40');
3985        $text .= $token->{data};        $text .= $token->{data};
3986        !!!next-token;        !!!next-token;
3987      }      }
3988    
3989      ## Step 5      ## Step 5
3990      if (length $text) {      if (length $text) {
3991          !!!cp ('t41');
3992        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3993        $el->append_child ($text);        $el->append_child ($text);
3994      }      }
# Line 2218  sub _tree_construction_main ($) { Line 3997  sub _tree_construction_main ($) {
3997      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3998    
3999      ## Step 7      ## Step 7
4000      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
4001            $token->{tag_name} eq $start_tag_name) {
4002          !!!cp ('t42');
4003        ## 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});  
4004      } else {      } else {
4005        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
4006          if ($content_model_flag == CDATA_CONTENT_MODEL) {
4007            !!!cp ('t43');
4008            !!!parse-error (type => 'in CDATA:#eof', token => $token);
4009          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
4010            !!!cp ('t44');
4011            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
4012          } else {
4013            die "$0: $content_model_flag in parse_rcdata";
4014          }
4015      }      }
4016      !!!next-token;      !!!next-token;
4017    }; # $parse_rcdata    }; # $parse_rcdata
4018    
4019    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
4020      my $script_el;      my $script_el;
4021      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
4022      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
4023    
4024      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
4025      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
4026            
4027      my $text = '';      my $text = '';
4028        !!!nack ('t45.1');
4029      !!!next-token;      !!!next-token;
4030      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
4031          !!!cp ('t45');
4032        $text .= $token->{data};        $text .= $token->{data};
4033        !!!next-token;        !!!next-token;
4034      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
4035      if (length $text) {      if (length $text) {
4036          !!!cp ('t46');
4037        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
4038      }      }
4039                                
4040      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
4041    
4042      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
4043          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
4044          !!!cp ('t47');
4045        ## Ignore the token        ## Ignore the token
4046      } else {      } else {
4047        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
4048          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4049        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4050        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4051      }      }
4052            
4053      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
4054          !!!cp ('t49');
4055        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4056      } else {      } else {
4057          !!!cp ('t50');
4058        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
4059        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
4060    
# Line 2276  sub _tree_construction_main ($) { Line 4068  sub _tree_construction_main ($) {
4068      !!!next-token;      !!!next-token;
4069    }; # $script_start_tag    }; # $script_start_tag
4070    
4071      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
4072      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
4073      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4074    
4075    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4076      my $tag_name = shift;      my $end_tag_token = shift;
4077        my $tag_name = $end_tag_token->{tag_name};
4078    
4079        ## NOTE: The adoption agency algorithm (AAA).
4080    
4081      FET: {      FET: {
4082        ## Step 1        ## Step 1
4083        my $formatting_element;        my $formatting_element;
4084        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4085        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4086          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4087              !!!cp ('t52');
4088              last AFE;
4089            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4090                         eq $tag_name) {
4091              !!!cp ('t51');
4092            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4093            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4094            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
4095          }          }
4096        } # AFE        } # AFE
4097        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4098          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
4099            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4100          ## Ignore the token          ## Ignore the token
4101          !!!next-token;          !!!next-token;
4102          return;          return;
# Line 2305  sub _tree_construction_main ($) { Line 4108  sub _tree_construction_main ($) {
4108          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4109          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
4110            if ($in_scope) {            if ($in_scope) {
4111                !!!cp ('t54');
4112              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
4113              last INSCOPE;              last INSCOPE;
4114            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4115              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
4116                !!!parse-error (type => 'unmatched end tag',
4117                                text => $token->{tag_name},
4118                                token => $end_tag_token);
4119              ## Ignore the token              ## Ignore the token
4120              !!!next-token;              !!!next-token;
4121              return;              return;
4122            }            }
4123          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
4124                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4125            $in_scope = 0;            $in_scope = 0;
4126          }          }
4127        } # INSCOPE        } # INSCOPE
4128        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4129          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
4130            !!!parse-error (type => 'unmatched end tag',
4131                            text => $token->{tag_name},
4132                            token => $end_tag_token);
4133          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4134          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4135          return;          return;
4136        }        }
4137        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4138          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
4139            !!!parse-error (type => 'not closed',
4140                            text => $self->{open_elements}->[-1]->[0]
4141                                ->manakai_local_name,
4142                            token => $end_tag_token);
4143        }        }
4144                
4145        ## Step 2        ## Step 2
# Line 2335  sub _tree_construction_main ($) { Line 4147  sub _tree_construction_main ($) {
4147        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4148        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4149          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4150          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4151              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4152              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4153               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4154              !!!cp ('t59');
4155            $furthest_block = $node;            $furthest_block = $node;
4156            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
4157          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
4158              !!!cp ('t60');
4159            last OE;            last OE;
4160          }          }
4161        } # OE        } # OE
4162                
4163        ## Step 3        ## Step 3
4164        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
4165            !!!cp ('t61');
4166          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
4167          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
4168          !!!next-token;          !!!next-token;
# Line 2360  sub _tree_construction_main ($) { Line 4175  sub _tree_construction_main ($) {
4175        ## Step 5        ## Step 5
4176        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
4177        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
4178            !!!cp ('t62');
4179          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
4180        }        }
4181                
# Line 2382  sub _tree_construction_main ($) { Line 4198  sub _tree_construction_main ($) {
4198          S7S2: {          S7S2: {
4199            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
4200              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4201                  !!!cp ('t63');
4202                $node_i_in_active = $_;                $node_i_in_active = $_;
4203                last S7S2;                last S7S2;
4204              }              }
# Line 2395  sub _tree_construction_main ($) { Line 4212  sub _tree_construction_main ($) {
4212                    
4213          ## Step 4          ## Step 4
4214          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
4215              !!!cp ('t64');
4216            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
4217          }          }
4218                    
4219          ## Step 5          ## Step 5
4220          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
4221              !!!cp ('t65');
4222            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
4223            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
4224            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2417  sub _tree_construction_main ($) { Line 4236  sub _tree_construction_main ($) {
4236        } # S7          } # S7  
4237                
4238        ## Step 8        ## Step 8
4239        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4240            my $foster_parent_element;
4241            my $next_sibling;
4242            OE: for (reverse 0..$#{$self->{open_elements}}) {
4243              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4244                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4245                                 if (defined $parent and $parent->node_type == 1) {
4246                                   !!!cp ('t65.1');
4247                                   $foster_parent_element = $parent;
4248                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4249                                 } else {
4250                                   !!!cp ('t65.2');
4251                                   $foster_parent_element
4252                                     = $self->{open_elements}->[$_ - 1]->[0];
4253                                 }
4254                                 last OE;
4255                               }
4256                             } # OE
4257                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4258                               unless defined $foster_parent_element;
4259            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4260            $open_tables->[-1]->[1] = 1; # tainted
4261          } else {
4262            !!!cp ('t65.3');
4263            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4264          }
4265                
4266        ## Step 9        ## Step 9
4267        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2434  sub _tree_construction_main ($) { Line 4278  sub _tree_construction_main ($) {
4278        my $i;        my $i;
4279        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4280          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4281              !!!cp ('t66');
4282            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
4283            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
4284          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4285              !!!cp ('t67');
4286            $i = $_;            $i = $_;
4287          }          }
4288        } # AFE        } # AFE
# Line 2446  sub _tree_construction_main ($) { Line 4292  sub _tree_construction_main ($) {
4292        undef $i;        undef $i;
4293        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4294          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4295              !!!cp ('t68');
4296            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4297            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4298          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4299              !!!cp ('t69');
4300            $i = $_;            $i = $_;
4301          }          }
4302        } # OE        } # OE
# Line 2459  sub _tree_construction_main ($) { Line 4307  sub _tree_construction_main ($) {
4307      } # FET      } # FET
4308    }; # $formatting_end_tag    }; # $formatting_end_tag
4309    
4310    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4311      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4312    }; # $insert_to_current    }; # $insert_to_current
4313    
4314    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4315                         my $child = shift;      my $child = shift;
4316                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4317                              table => 1, tbody => 1, tfoot => 1,        # MUST
4318                              thead => 1, tr => 1,        my $foster_parent_element;
4319                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4320                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4321                           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') {  
4322                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4323                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4324                                   !!!cp ('t70');
4325                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4326                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4327                               } else {                               } else {
4328                                   !!!cp ('t71');
4329                                 $foster_parent_element                                 $foster_parent_element
4330                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4331                               }                               }
# Line 2489  sub _tree_construction_main ($) { Line 4336  sub _tree_construction_main ($) {
4336                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4337                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4338                             ($child, $next_sibling);                             ($child, $next_sibling);
4339                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4340                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4341                         }        !!!cp ('t72');
4342          $self->{open_elements}->[-1]->[0]->append_child ($child);
4343        }
4344    }; # $insert_to_foster    }; # $insert_to_foster
4345    
4346    my $insert;    B: while (1) {
4347        if ($token->{type} == DOCTYPE_TOKEN) {
4348    B: {        !!!cp ('t73');
4349      if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
       !!!parse-error (type => 'DOCTYPE in the middle');  
4350        ## Ignore the token        ## Ignore the token
4351        ## Stay in the phase        ## Stay in the phase
4352        !!!next-token;        !!!next-token;
4353        redo B;        next B;
4354      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == START_TAG_TOKEN and
       if ($self->{insertion_mode} eq 'after html body' or  
           $self->{insertion_mode} eq 'after html frameset') {  
         #  
       } 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', 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;  
     } elsif ($token->{type} eq 'start tag' and  
4355               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4356        if ($self->{insertion_mode} eq 'after html body') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4357          ## Turn into the main phase          !!!cp ('t79');
4358          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4359          $self->{insertion_mode} = 'after body';          $self->{insertion_mode} = AFTER_BODY_IM;
4360        } elsif ($self->{insertion_mode} eq 'after html frameset') {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4361          ## Turn into the main phase          !!!cp ('t80');
4362          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4363          $self->{insertion_mode} = 'after frameset';          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4364          } else {
4365            !!!cp ('t81');
4366        }        }
4367    
4368  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
4369  ## 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');  
       }  
4370        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4371        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4372          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4373              !!!cp ('t84');
4374            $top_el->set_attribute_ns            $top_el->set_attribute_ns
4375              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
4376               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4377          }          }
4378        }        }
4379          !!!nack ('t84.1');
4380        !!!next-token;        !!!next-token;
4381        redo B;        next B;
4382      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
4383        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4384        if ($self->{insertion_mode} eq 'after html body' or        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4385            $self->{insertion_mode} eq 'after html frameset') {          !!!cp ('t85');
4386          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
4387        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4388            !!!cp ('t86');
4389          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
4390        } else {        } else {
4391            !!!cp ('t87');
4392          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4393        }        }
4394        !!!next-token;        !!!next-token;
4395        redo B;        next B;
4396      } elsif ($self->{insertion_mode} eq 'in head' or      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4397               $self->{insertion_mode} eq 'in head noscript' or        if ($token->{type} == CHARACTER_TOKEN) {
4398               $self->{insertion_mode} eq 'after head' or          !!!cp ('t87.1');
4399               $self->{insertion_mode} eq 'before head') {          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4400        if ($token->{type} eq 'character') {          !!!next-token;
4401            next B;
4402          } elsif ($token->{type} == START_TAG_TOKEN) {
4403            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4404                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4405                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4406                ($token->{tag_name} eq 'svg' and
4407                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4408              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4409              !!!cp ('t87.2');
4410              #
4411            } elsif ({
4412                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4413                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4414                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4415                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4416                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4417                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4418                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4419                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4420                     }->{$token->{tag_name}}) {
4421              !!!cp ('t87.2');
4422              !!!parse-error (type => 'not closed',
4423                              text => $self->{open_elements}->[-1]->[0]
4424                                  ->manakai_local_name,
4425                              token => $token);
4426    
4427              pop @{$self->{open_elements}}
4428                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4429    
4430              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4431              ## Reprocess.
4432              next B;
4433            } else {
4434              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4435              my $tag_name = $token->{tag_name};
4436              if ($nsuri eq $SVG_NS) {
4437                $tag_name = {
4438                   altglyph => 'altGlyph',
4439                   altglyphdef => 'altGlyphDef',
4440                   altglyphitem => 'altGlyphItem',
4441                   animatecolor => 'animateColor',
4442                   animatemotion => 'animateMotion',
4443                   animatetransform => 'animateTransform',
4444                   clippath => 'clipPath',
4445                   feblend => 'feBlend',
4446                   fecolormatrix => 'feColorMatrix',
4447                   fecomponenttransfer => 'feComponentTransfer',
4448                   fecomposite => 'feComposite',
4449                   feconvolvematrix => 'feConvolveMatrix',
4450                   fediffuselighting => 'feDiffuseLighting',
4451                   fedisplacementmap => 'feDisplacementMap',
4452                   fedistantlight => 'feDistantLight',
4453                   feflood => 'feFlood',
4454                   fefunca => 'feFuncA',
4455                   fefuncb => 'feFuncB',
4456                   fefuncg => 'feFuncG',
4457                   fefuncr => 'feFuncR',
4458                   fegaussianblur => 'feGaussianBlur',
4459                   feimage => 'feImage',
4460                   femerge => 'feMerge',
4461                   femergenode => 'feMergeNode',
4462                   femorphology => 'feMorphology',
4463                   feoffset => 'feOffset',
4464                   fepointlight => 'fePointLight',
4465                   fespecularlighting => 'feSpecularLighting',
4466                   fespotlight => 'feSpotLight',
4467                   fetile => 'feTile',
4468                   feturbulence => 'feTurbulence',
4469                   foreignobject => 'foreignObject',
4470                   glyphref => 'glyphRef',
4471                   lineargradient => 'linearGradient',
4472                   radialgradient => 'radialGradient',
4473                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4474                   textpath => 'textPath',  
4475                }->{$tag_name} || $tag_name;
4476              }
4477    
4478              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4479    
4480              ## "adjust foreign attributes" - done in insert-element-f
4481    
4482              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4483    
4484              if ($self->{self_closing}) {
4485                pop @{$self->{open_elements}};
4486                !!!ack ('t87.3');
4487              } else {
4488                !!!cp ('t87.4');
4489              }
4490    
4491              !!!next-token;
4492              next B;
4493            }
4494          } elsif ($token->{type} == END_TAG_TOKEN) {
4495            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4496            !!!cp ('t87.5');
4497            #
4498          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4499            !!!cp ('t87.6');
4500            !!!parse-error (type => 'not closed',
4501                            text => $self->{open_elements}->[-1]->[0]
4502                                ->manakai_local_name,
4503                            token => $token);
4504    
4505            pop @{$self->{open_elements}}
4506                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4507    
4508            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4509            ## Reprocess.
4510            next B;
4511          } else {
4512            die "$0: $token->{type}: Unknown token type";        
4513          }
4514        }
4515    
4516        if ($self->{insertion_mode} & HEAD_IMS) {
4517          if ($token->{type} == CHARACTER_TOKEN) {
4518          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4519            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4520                !!!cp ('t88.2');
4521                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4522                #
4523              } else {
4524                !!!cp ('t88.1');
4525                ## Ignore the token.
4526                #
4527              }
4528            unless (length $token->{data}) {            unless (length $token->{data}) {
4529                !!!cp ('t88');
4530              !!!next-token;              !!!next-token;
4531              redo B;              next B;
4532            }            }
4533    ## TODO: set $token->{column} appropriately
4534          }          }
4535    
4536          if ($self->{insertion_mode} eq 'before head') {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4537              !!!cp ('t89');
4538            ## As if <head>            ## As if <head>
4539            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4540            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4541            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4542                  [$self->{head_element}, $el_category->{head}];
4543    
4544            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4545            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4546    
4547            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4548          } elsif ($self->{insertion_mode} eq 'in head noscript') {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4549              !!!cp ('t90');
4550            ## As if </noscript>            ## As if </noscript>
4551            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4552            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4553                        
4554            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4555            ## As if </head>            ## As if </head>
4556            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4557    
4558            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4559          } elsif ($self->{insertion_mode} eq 'in head') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4560              !!!cp ('t91');
4561            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4562    
4563            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4564            } else {
4565              !!!cp ('t92');
4566          }          }
4567    
4568              ## "after head" insertion mode          ## "after head" insertion mode
4569              ## As if <body>          ## As if <body>
4570              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4571              $self->{insertion_mode} = 'in body';          $self->{insertion_mode} = IN_BODY_IM;
4572              ## reprocess          ## reprocess
4573              redo B;          next B;
4574            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4575              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4576                if ($self->{insertion_mode} eq 'before head') {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4577                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4578                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4579                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4580                  $self->{insertion_mode} = 'in head';                  ($self->{head_element});
4581                  !!!next-token;              push @{$self->{open_elements}},
4582                  redo B;                  [$self->{head_element}, $el_category->{head}];
4583                } elsif ($self->{insertion_mode} ne 'after head') {              $self->{insertion_mode} = IN_HEAD_IM;
4584                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!nack ('t93.1');
4585                  ## Ignore the token              !!!next-token;
4586                  !!!next-token;              next B;
4587                  redo B;            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4588                } else {              !!!cp ('t93.2');
4589                  #              !!!parse-error (type => 'after head', text => 'head',
4590                }                              token => $token);
4591              } elsif ($self->{insertion_mode} eq 'before head') {              ## Ignore the token
4592                ## As if <head>              !!!nack ('t93.3');
4593                !!!create-element ($self->{head_element}, 'head');              !!!next-token;
4594                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              next B;
4595                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            } else {
4596                !!!cp ('t95');
4597                !!!parse-error (type => 'in head:head',
4598                                token => $token); # or in head noscript
4599                ## Ignore the token
4600                !!!nack ('t95.1');
4601                !!!next-token;
4602                next B;
4603              }
4604            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4605              !!!cp ('t96');
4606              ## As if <head>
4607              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4608              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4609              push @{$self->{open_elements}},
4610                  [$self->{head_element}, $el_category->{head}];
4611    
4612                $self->{insertion_mode} = 'in head';            $self->{insertion_mode} = IN_HEAD_IM;
4613                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4614              }          } else {
4615              !!!cp ('t97');
4616            }
4617    
4618              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4619                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4620                    !!!cp ('t98');
4621                  ## As if </noscript>                  ## As if </noscript>
4622                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4623                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4624                                    token => $token);
4625                                
4626                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
4627                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4628                  } else {
4629                    !!!cp ('t99');
4630                }                }
4631    
4632                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4633                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4634                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4635                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4636                                    text => $token->{tag_name}, token => $token);
4637                    push @{$self->{open_elements}},
4638                        [$self->{head_element}, $el_category->{head}];
4639                  } else {
4640                    !!!cp ('t101');
4641                }                }
4642                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4643                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4644                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4645                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4646                  !!!nack ('t101.1');
4647                !!!next-token;                !!!next-token;
4648                redo B;                next B;
4649              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4650                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4651                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4652                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4653                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4654                                    text => $token->{tag_name}, token => $token);
4655                    push @{$self->{open_elements}},
4656                        [$self->{head_element}, $el_category->{head}];
4657                  } else {
4658                    !!!cp ('t103');
4659                }                }
4660                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4661                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4662                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4663                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4664                  !!!ack ('t103.1');
4665                !!!next-token;                !!!next-token;
4666                redo B;                next B;
4667              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4668                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4669                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4670                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4671                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4672                                    text => $token->{tag_name}, token => $token);
4673                    push @{$self->{open_elements}},
4674                        [$self->{head_element}, $el_category->{head}];
4675                  } else {
4676                    !!!cp ('t105');
4677                }                }
4678                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4679                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.
4680    
4681                unless ($self->{confident}) {                unless ($self->{confident}) {
4682                  my $charset;                  if ($token->{attributes}->{charset}) {
4683                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                    !!!cp ('t106');
4684                    $charset = $token->{attributes}->{charset}->{value};                    ## NOTE: Whether the encoding is supported or not is handled
4685                  }                    ## in the {change_encoding} callback.
4686                  if ($token->{attributes}->{'http-equiv'}) {                    $self->{change_encoding}
4687                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                        ->($self, $token->{attributes}->{charset}->{value},
4688                    if ($token->{attributes}->{'http-equiv'}->{value}                           $token);
4689                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                    
4690                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4691                          ->set_user_data (manakai_has_reference =>
4692                                               $token->{attributes}->{charset}
4693                                                   ->{has_reference});
4694                    } elsif ($token->{attributes}->{content}) {
4695                      if ($token->{attributes}->{content}->{value}
4696                          =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4697                              [\x09-\x0D\x20]*=
4698                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4699                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4700                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
4701                    } ## TODO: And if supported                      ## NOTE: Whether the encoding is supported or not is handled
4702                        ## in the {change_encoding} callback.
4703                        $self->{change_encoding}
4704                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4705                               $token);
4706                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4707                            ->set_user_data (manakai_has_reference =>
4708                                                 $token->{attributes}->{content}
4709                                                       ->{has_reference});
4710                      } else {
4711                        !!!cp ('t108');
4712                      }
4713                    }
4714                  } else {
4715                    if ($token->{attributes}->{charset}) {
4716                      !!!cp ('t109');
4717                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4718                          ->set_user_data (manakai_has_reference =>
4719                                               $token->{attributes}->{charset}
4720                                                   ->{has_reference});
4721                    }
4722                    if ($token->{attributes}->{content}) {
4723                      !!!cp ('t110');
4724                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4725                          ->set_user_data (manakai_has_reference =>
4726                                               $token->{attributes}->{content}
4727                                                   ->{has_reference});
4728                  }                  }
                 ## TODO: Change the encoding  
4729                }                }
4730    
4731                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
4732                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4733                    if $self->{insertion_mode} eq 'after head';                !!!ack ('t110.1');
4734                !!!next-token;                !!!next-token;
4735                redo B;                next B;
4736              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4737                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4738                    !!!cp ('t111');
4739                  ## As if </noscript>                  ## As if </noscript>
4740                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4741                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4742                                    token => $token);
4743                                
4744                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
4745                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4746                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4747                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4748                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4749                                    text => $token->{tag_name}, token => $token);
4750                    push @{$self->{open_elements}},
4751                        [$self->{head_element}, $el_category->{head}];
4752                  } else {
4753                    !!!cp ('t113');
4754                }                }
4755    
4756                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4757                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4758                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4759                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4760                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
4761                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4762                    if $self->{insertion_mode} eq 'after head';                next B;
4763                redo B;              } elsif ($token->{tag_name} eq 'style' or
4764              } elsif ($token->{tag_name} eq 'style') {                       $token->{tag_name} eq 'noframes') {
4765                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4766                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
4767                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4768                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4769                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4770                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4771                                    text => $token->{tag_name}, token => $token);
4772                    push @{$self->{open_elements}},
4773                        [$self->{head_element}, $el_category->{head}];
4774                  } else {
4775                    !!!cp ('t115');
4776                }                }
4777                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4778                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4779                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4780                redo B;                next B;
4781              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4782                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4783                    !!!cp ('t116');
4784                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4785                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4786                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4787                    !!!nack ('t116.1');
4788                  !!!next-token;                  !!!next-token;
4789                  redo B;                  next B;
4790                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4791                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4792                    !!!parse-error (type => 'in noscript', text => 'noscript',
4793                                    token => $token);
4794                  ## Ignore the token                  ## Ignore the token
4795                    !!!nack ('t117.1');
4796                  !!!next-token;                  !!!next-token;
4797                  redo B;                  next B;
4798                } else {                } else {
4799                    !!!cp ('t118');
4800                  #                  #
4801                }                }
4802              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4803                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4804                    !!!cp ('t119');
4805                  ## As if </noscript>                  ## As if </noscript>
4806                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4807                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4808                                    token => $token);
4809                                
4810                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
4811                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4812                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4813                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4814                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4815                                    text => $token->{tag_name}, token => $token);
4816                    push @{$self->{open_elements}},
4817                        [$self->{head_element}, $el_category->{head}];
4818                  } else {
4819                    !!!cp ('t121');
4820                }                }
4821    
4822                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4823                $script_start_tag->($insert_to_current);                $script_start_tag->();
4824                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4825                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4826                redo B;                next B;
4827              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4828                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4829                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4830                    !!!cp ('t122');
4831                  ## As if </noscript>                  ## As if </noscript>
4832                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4833                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4834                                    text => $token->{tag_name}, token => $token);
4835                                    
4836                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4837                  ## As if </head>                  ## As if </head>
4838                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4839                                    
4840                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4841                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4842                    !!!cp ('t124');
4843                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4844                                    
4845                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4846                  } else {
4847                    !!!cp ('t125');
4848                }                }
4849    
4850                ## "after head" insertion mode                ## "after head" insertion mode
4851                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4852                $self->{insertion_mode} = 'in '.$token->{tag_name};                if ($token->{tag_name} eq 'body') {
4853                    !!!cp ('t126');
4854                    $self->{insertion_mode} = IN_BODY_IM;
4855                  } elsif ($token->{tag_name} eq 'frameset') {
4856                    !!!cp ('t127');
4857                    $self->{insertion_mode} = IN_FRAMESET_IM;
4858                  } else {
4859                    die "$0: tag name: $self->{tag_name}";
4860                  }
4861                  !!!nack ('t127.1');
4862                !!!next-token;                !!!next-token;
4863                redo B;                next B;
4864              } else {              } else {
4865                  !!!cp ('t128');
4866                #                #
4867              }              }
4868    
4869              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4870                  !!!cp ('t129');
4871                ## As if </noscript>                ## As if </noscript>
4872                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4873                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4874                                  text => $token->{tag_name}, token => $token);
4875                                
4876                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4877                ## As if </head>                ## As if </head>
4878                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4879    
4880                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4881              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4882                  !!!cp ('t130');
4883                ## As if </head>                ## As if </head>
4884                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4885    
4886                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4887                } else {
4888                  !!!cp ('t131');
4889              }              }
4890    
4891              ## "after head" insertion mode              ## "after head" insertion mode
4892              ## As if <body>              ## As if <body>
4893              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4894              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
4895              ## reprocess              ## reprocess
4896              redo B;              !!!ack-later;
4897            } elsif ($token->{type} eq 'end tag') {              next B;
4898              } elsif ($token->{type} == END_TAG_TOKEN) {
4899              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4900                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4901                    !!!cp ('t132');
4902                  ## As if <head>                  ## As if <head>
4903                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4904                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4905                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4906                        [$self->{head_element}, $el_category->{head}];
4907    
4908                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4909                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4910                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
4911                  !!!next-token;                  !!!next-token;
4912                  redo B;                  next B;
4913                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4914                    !!!cp ('t133');
4915                  ## As if </noscript>                  ## As if </noscript>
4916                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4917                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/',
4918                                    text => 'head', token => $token);
4919                                    
4920                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4921                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4922                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
4923                  !!!next-token;                  !!!next-token;
4924                  redo B;                  next B;
4925                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4926                    !!!cp ('t134');
4927                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4928                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
4929                  !!!next-token;                  !!!next-token;
4930                  redo B;                  next B;
4931                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4932                    !!!cp ('t134.1');
4933                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4934                                    token => $token);
4935                    ## Ignore the token
4936                    !!!next-token;
4937                    next B;
4938                } else {                } else {
4939                  #                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4940                }                }
4941              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4942                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4943                    !!!cp ('t136');
4944                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4945                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
4946                  !!!next-token;                  !!!next-token;
4947                  redo B;                  next B;
4948                } elsif ($self->{insertion_mode} eq 'before head') {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4949                  !!!parse-error (type => 'unmatched end tag:noscript');                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4950                    !!!cp ('t137');
4951                    !!!parse-error (type => 'unmatched end tag',
4952                                    text => 'noscript', token => $token);
4953                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4954                  !!!next-token;                  !!!next-token;
4955                  redo B;                  next B;
4956                } else {                } else {
4957                    !!!cp ('t138');
4958                  #                  #
4959                }                }
4960              } elsif ({              } elsif ({
4961                        body => 1, html => 1,                        body => 1, html => 1,
4962                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4963                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4964                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_IM or
4965                  !!!create-element ($self->{head_element}, 'head');                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4966                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  !!!cp ('t140');
4967                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'unmatched end tag',
4968                                    text => $token->{tag_name}, token => $token);
                 $self->{insertion_mode} = 'in head';  
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} eq 'in head noscript') {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4969                  ## Ignore the token                  ## Ignore the token
4970                  !!!next-token;                  !!!next-token;
4971                  redo B;                  next B;
4972                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4973                    !!!cp ('t140.1');
4974                    !!!parse-error (type => 'unmatched end tag',
4975                                    text => $token->{tag_name}, token => $token);
4976                    ## Ignore the token
4977                    !!!next-token;
4978                    next B;
4979                  } else {
4980                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4981                }                }
4982                              } elsif ($token->{tag_name} eq 'p') {
4983                #                !!!cp ('t142');
4984              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4985                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4986                       }->{$token->{tag_name}}) {                ## Ignore the token
4987                if ($self->{insertion_mode} eq 'before head') {                !!!next-token;
4988                  ## As if <head>                next B;
4989                  !!!create-element ($self->{head_element}, 'head');              } elsif ($token->{tag_name} eq 'br') {
4990                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4991                    !!!cp ('t142.2');
4992                    ## (before head) as if <head>, (in head) as if </head>
4993                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4994                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4995                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4996      
4997                    ## Reprocess in the "after head" insertion mode...
4998                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4999                    !!!cp ('t143.2');
5000                    ## As if </head>
5001                    pop @{$self->{open_elements}};
5002                    $self->{insertion_mode} = AFTER_HEAD_IM;
5003      
5004                    ## Reprocess in the "after head" insertion mode...
5005                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5006                    !!!cp ('t143.3');
5007                    ## ISSUE: Two parse errors for <head><noscript></br>
5008                    !!!parse-error (type => 'unmatched end tag',
5009                                    text => 'br', token => $token);
5010                    ## As if </noscript>
5011                    pop @{$self->{open_elements}};
5012                    $self->{insertion_mode} = IN_HEAD_IM;
5013    
                 $self->{insertion_mode} = 'in head';  
5014                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
5015                }                  ## As if </head>
5016                    pop @{$self->{open_elements}};
5017                    $self->{insertion_mode} = AFTER_HEAD_IM;
5018    
5019                #                  ## Reprocess in the "after head" insertion mode...
5020              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5021                if ($self->{insertion_mode} ne 'after head') {                  !!!cp ('t143.4');
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
5022                  #                  #
5023                  } else {
5024                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
5025                }                }
5026    
5027                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5028                  !!!parse-error (type => 'unmatched end tag',
5029                                  text => 'br', token => $token);
5030                  ## Ignore the token
5031                  !!!next-token;
5032                  next B;
5033                } else {
5034                  !!!cp ('t145');
5035                  !!!parse-error (type => 'unmatched end tag',
5036                                  text => $token->{tag_name}, token => $token);
5037                  ## Ignore the token
5038                  !!!next-token;
5039                  next B;
5040              }              }
5041    
5042              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5043                  !!!cp ('t146');
5044                ## As if </noscript>                ## As if </noscript>
5045                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5046                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
5047                                  text => $token->{tag_name}, token => $token);
5048                                
5049                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5050                ## As if </head>                ## As if </head>
5051                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5052    
5053                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
5054              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5055                  !!!cp ('t147');
5056                ## As if </head>                ## As if </head>
5057                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5058    
5059                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
5060              } elsif ($self->{insertion_mode} eq 'before head') {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5061                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
5062                  !!!cp ('t148');
5063                  !!!parse-error (type => 'unmatched end tag',
5064                                  text => $token->{tag_name}, token => $token);
5065                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5066                !!!next-token;                !!!next-token;
5067                redo B;                next B;
5068                } else {
5069                  !!!cp ('t149');
5070              }              }
5071    
5072              ## "after head" insertion mode              ## "after head" insertion mode
5073              ## As if <body>              ## As if <body>
5074              !!!insert-element ('body');              !!!insert-element ('body',, $token);
5075              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
5076              ## reprocess              ## reprocess
5077              redo B;              next B;
5078            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5079              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5080            }            !!!cp ('t149.1');
5081    
5082              ## NOTE: As if <head>
5083              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5084              $self->{open_elements}->[-1]->[0]->append_child
5085                  ($self->{head_element});
5086              #push @{$self->{open_elements}},
5087              #    [$self->{head_element}, $el_category->{head}];
5088              #$self->{insertion_mode} = IN_HEAD_IM;
5089              ## NOTE: Reprocess.
5090    
5091              ## NOTE: As if </head>
5092              #pop @{$self->{open_elements}};
5093              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5094              ## NOTE: Reprocess.
5095              
5096              #
5097            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5098              !!!cp ('t149.2');
5099    
5100              ## NOTE: As if </head>
5101              pop @{$self->{open_elements}};
5102              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5103              ## NOTE: Reprocess.
5104    
5105              #
5106            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5107              !!!cp ('t149.3');
5108    
5109              !!!parse-error (type => 'in noscript:#eof', token => $token);
5110    
5111              ## As if </noscript>
5112              pop @{$self->{open_elements}};
5113              #$self->{insertion_mode} = IN_HEAD_IM;
5114              ## NOTE: Reprocess.
5115    
5116              ## NOTE: As if </head>
5117              pop @{$self->{open_elements}};
5118              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5119              ## NOTE: Reprocess.
5120    
5121              #
5122            } else {
5123              !!!cp ('t149.4');
5124              #
5125            }
5126    
5127            ## NOTE: As if <body>
5128            !!!insert-element ('body',, $token);
5129            $self->{insertion_mode} = IN_BODY_IM;
5130            ## NOTE: Reprocess.
5131            next B;
5132          } else {
5133            die "$0: $token->{type}: Unknown token type";
5134          }
5135    
5136            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
5137      } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
5138               $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
5139               $self->{insertion_mode} eq 'in caption') {              !!!cp ('t150');
           if ($token->{type} eq 'character') {  
5140              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
5141              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
5142                            
5143              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5144    
5145              !!!next-token;              !!!next-token;
5146              redo B;              next B;
5147            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
5148              if ({              if ({
5149                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
5150                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
5151                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5152                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
5153                  ## have an element in table scope                  ## have an element in table scope
5154                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
5155                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5156                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
5157                      $tn = $node->[1];                      !!!cp ('t151');
5158                      last INSCOPE;  
5159                    } elsif ({                      ## Close the cell
5160                              table => 1, html => 1,                      !!!back-token; # <x>
5161                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
5162                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
5163                    }                                line => $token->{line},
5164                  } # INSCOPE                                column => $token->{column}};
5165                    unless (defined $tn) {                      next B;
5166                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5167                      ## Ignore the token                      !!!cp ('t152');
5168                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
5169                      redo B;                      last;
5170                    }                    }
5171                    }
5172    
5173                    !!!cp ('t153');
5174                    !!!parse-error (type => 'start tag not allowed',
5175                        text => $token->{tag_name}, token => $token);
5176                    ## Ignore the token
5177                    !!!nack ('t153.1');
5178                    !!!next-token;
5179                    next B;
5180                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5181                    !!!parse-error (type => 'not closed', text => 'caption',
5182                                    token => $token);
5183                                    
5184                  ## Close the cell                  ## NOTE: As if </caption>.
                 !!!back-token; # <?>  
                 $token = {type => 'end tag', tag_name => $tn};  
                 redo B;  
               } elsif ($self->{insertion_mode} eq 'in caption') {  
                 !!!parse-error (type => 'not closed:caption');  
                   
                 ## As if </caption>  
5185                  ## have a table element in table scope                  ## have a table element in table scope
5186                  my $i;                  my $i;
5187                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5188                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5189                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
5190                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5191                      last INSCOPE;                        !!!cp ('t155');
5192                    } elsif ({                        $i = $_;
5193                              table => 1, html => 1,                        last INSCOPE;
5194                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5195                      last INSCOPE;                        !!!cp ('t156');
5196                          last;
5197                        }
5198                    }                    }
5199    
5200                      !!!cp ('t157');
5201                      !!!parse-error (type => 'start tag not allowed',
5202                                      text => $token->{tag_name}, token => $token);
5203                      ## Ignore the token
5204                      !!!nack ('t157.1');
5205                      !!!next-token;
5206                      next B;
5207                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5208                                    
5209                  ## generate implied end tags                  ## generate implied end tags
5210                  if ({                  while ($self->{open_elements}->[-1]->[1]
5211                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5212                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
5213                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token; # <?>  
                   $token = {type => 'end tag', tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
5214                  }                  }
5215    
5216                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5217                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
5218                      !!!parse-error (type => 'not closed',
5219                                      text => $self->{open_elements}->[-1]->[0]
5220                                          ->manakai_local_name,
5221                                      token => $token);
5222                    } else {
5223                      !!!cp ('t160');
5224                  }                  }
5225                                    
5226                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
5227                                    
5228                  $clear_up_to_marker->();                  $clear_up_to_marker->();
5229                                    
5230                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
5231                                    
5232                  ## reprocess                  ## reprocess
5233                  redo B;                  !!!ack-later;
5234                    next B;
5235                } else {                } else {
5236                    !!!cp ('t161');
5237                  #                  #
5238                }                }
5239              } else {              } else {
5240                  !!!cp ('t162');
5241                #                #
5242              }              }
5243            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5244              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
5245                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
5246                  ## have an element in table scope                  ## have an element in table scope
5247                  my $i;                  my $i;
5248                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5249                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5250                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5251                        !!!cp ('t163');
5252                      $i = $_;                      $i = $_;
5253                      last INSCOPE;                      last INSCOPE;
5254                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5255                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
5256                      last INSCOPE;                      last INSCOPE;
5257                    }                    }
5258                  } # INSCOPE                  } # INSCOPE
5259                    unless (defined $i) {                    unless (defined $i) {
5260                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
5261                        !!!parse-error (type => 'unmatched end tag',
5262                                        text => $token->{tag_name},
5263                                        token => $token);
5264                      ## Ignore the token                      ## Ignore the token
5265                      !!!next-token;                      !!!next-token;
5266                      redo B;                      next B;
5267                    }                    }
5268                                    
5269                  ## generate implied end tags                  ## generate implied end tags
5270                  if ({                  while ($self->{open_elements}->[-1]->[1]
5271                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5272                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
5273                       th => ($token->{tag_name} eq 'td'),                    pop @{$self->{open_elements}};
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
5274                  }                  }
5275                    
5276                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5277                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
5278                      !!!cp ('t167');
5279                      !!!parse-error (type => 'not closed',
5280                                      text => $self->{open_elements}->[-1]->[0]
5281                                          ->manakai_local_name,
5282                                      token => $token);
5283                    } else {
5284                      !!!cp ('t168');
5285                  }                  }
5286                                    
5287                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
5288                                    
5289                  $clear_up_to_marker->();                  $clear_up_to_marker->();
5290                                    
5291                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
5292                                    
5293                  !!!next-token;                  !!!next-token;
5294                  redo B;                  next B;
5295                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5296                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
5297                    !!!parse-error (type => 'unmatched end tag',
5298                                    text => $token->{tag_name}, token => $token);
5299                  ## Ignore the token                  ## Ignore the token
5300                  !!!next-token;                  !!!next-token;
5301                  redo B;                  next B;
5302                } else {                } else {
5303                    !!!cp ('t170');
5304                  #                  #
5305                }                }
5306              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
5307                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5308                  ## have a table element in table scope                  ## have a table element in table scope
5309                  my $i;                  my $i;
5310                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5311                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5312                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5313                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5314                      last INSCOPE;                        !!!cp ('t171');
5315                    } elsif ({                        $i = $_;
5316                              table => 1, html => 1,                        last INSCOPE;
5317                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5318                      last INSCOPE;                        !!!cp ('t172');
5319                          last;
5320                        }
5321                    }                    }
5322    
5323                      !!!cp ('t173');
5324                      !!!parse-error (type => 'unmatched end tag',
5325                                      text => $token->{tag_name}, token => $token);
5326                      ## Ignore the token
5327                      !!!next-token;
5328                      next B;
5329                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5330                                    
5331                  ## generate implied end tags                  ## generate implied end tags
5332                  if ({                  while ($self->{open_elements}->[-1]->[1]
5333                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5334                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
5335                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
5336                  }                  }
5337                                    
5338                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5339                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
5340                      !!!parse-error (type => 'not closed',
5341                                      text => $self->{open_elements}->[-1]->[0]
5342                                          ->manakai_local_name,
5343                                      token => $token);
5344                    } else {
5345                      !!!cp ('t176');
5346                  }                  }
5347                                    
5348                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
5349                                    
5350                  $clear_up_to_marker->();                  $clear_up_to_marker->();
5351                                    
5352                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
5353                                    
5354                  !!!next-token;                  !!!next-token;
5355                  redo B;                  next B;
5356                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5357                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
5358                    !!!parse-error (type => 'unmatched end tag',
5359                                    text => $token->{tag_name}, token => $token);
5360                  ## Ignore the token                  ## Ignore the token
5361                  !!!next-token;                  !!!next-token;
5362                  redo B;                  next B;
5363                } else {                } else {
5364                    !!!cp ('t178');
5365                  #                  #
5366                }                }
5367              } elsif ({              } elsif ({
5368                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
5369                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5370                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5371                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
5372                ## have an element in table scope                ## have an element in table scope
5373                my $i;                my $i;
5374                my $tn;                my $tn;
5375                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5376                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5377                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5378                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5379                    last INSCOPE;                      !!!cp ('t179');
5380                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
5381                    $tn = $node->[1];  
5382                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
5383                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
5384                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5385                            table => 1, html => 1,                                line => $token->{line},
5386                           }->{$node->[1]}) {                                column => $token->{column}};
5387                    last INSCOPE;                      next B;
5388                      } elsif ($node->[1] & TABLE_CELL_EL) {
5389                        !!!cp ('t180');
5390                        $tn = $node->[0]->manakai_local_name;
5391                        ## NOTE: There is exactly one |td| or |th| element
5392                        ## in scope in the stack of open elements by definition.
5393                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5394                        ## ISSUE: Can this be reached?
5395                        !!!cp ('t181');
5396                        last;
5397                      }
5398                  }                  }
5399                } # INSCOPE  
5400                unless (defined $i) {                  !!!cp ('t182');
5401                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5402                        text => $token->{tag_name}, token => $token);
5403                  ## Ignore the token                  ## Ignore the token
5404                  !!!next-token;                  !!!next-token;
5405                  redo B;                  next B;
5406                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
5407              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5408                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5409                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5410                                  token => $token);
5411    
5412                ## As if </caption>                ## As if </caption>
5413                ## have a table element in table scope                ## have a table element in table scope
5414                my $i;                my $i;
5415                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5416                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5417                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5418                      !!!cp ('t184');
5419                    $i = $_;                    $i = $_;
5420                    last INSCOPE;                    last INSCOPE;
5421                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5422                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5423                    last INSCOPE;                    last INSCOPE;
5424                  }                  }
5425                } # INSCOPE                } # INSCOPE
5426                unless (defined $i) {                unless (defined $i) {
5427                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5428                    !!!parse-error (type => 'unmatched end tag',
5429                                    text => 'caption', token => $token);
5430                  ## Ignore the token                  ## Ignore the token
5431                  !!!next-token;                  !!!next-token;
5432                  redo B;                  next B;
5433                }                }
5434                                
5435                ## generate implied end tags                ## generate implied end tags
5436                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5437                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5438                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5439                }                }
5440    
5441                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5442                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5443                    !!!parse-error (type => 'not closed',
5444                                    text => $self->{open_elements}->[-1]->[0]
5445                                        ->manakai_local_name,
5446                                    token => $token);
5447                  } else {
5448                    !!!cp ('t189');
5449                }                }
5450    
5451                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5452    
5453                $clear_up_to_marker->();                $clear_up_to_marker->();
5454    
5455                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5456    
5457                ## reprocess                ## reprocess
5458                redo B;                next B;
5459              } elsif ({              } elsif ({
5460                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5461                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5462                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5463                    $self->{insertion_mode} eq 'in caption') {                  !!!cp ('t190');
5464                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5465                                    text => $token->{tag_name}, token => $token);
5466                  ## Ignore the token                  ## Ignore the token
5467                  !!!next-token;                  !!!next-token;
5468                  redo B;                  next B;
5469                } else {                } else {
5470                    !!!cp ('t191');
5471                  #                  #
5472                }                }
5473              } elsif ({              } elsif ({
5474                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
5475                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5476                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5477                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5478                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5479                  !!!parse-error (type => 'unmatched end tag',
5480                                  text => $token->{tag_name}, token => $token);
5481                ## Ignore the token                ## Ignore the token
5482                !!!next-token;                !!!next-token;
5483                redo B;                next B;
5484              } else {              } else {
5485                  !!!cp ('t193');
5486                #                #
5487              }              }
5488          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5489            for my $entry (@{$self->{open_elements}}) {
5490              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5491                !!!cp ('t75');
5492                !!!parse-error (type => 'in body:#eof', token => $token);
5493                last;
5494              }
5495            }
5496    
5497            ## Stop parsing.
5498            last B;
5499        } else {        } else {
5500          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5501        }        }
5502    
5503        $insert = $insert_to_current;        $insert = $insert_to_current;
5504        #        #
5505      } elsif ($self->{insertion_mode} eq 'in row' or      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5506               $self->{insertion_mode} eq 'in table body' or        if ($token->{type} == CHARACTER_TOKEN) {
5507               $self->{insertion_mode} eq 'in table') {          if (not $open_tables->[-1]->[1] and # tainted
5508            if ($token->{type} eq 'character') {              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5509              ## NOTE: There are "character in table" code clones.            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
5510                                
5511                unless (length $token->{data}) {            unless (length $token->{data}) {
5512                  !!!next-token;              !!!cp ('t194');
5513                  redo B;              !!!next-token;
5514                }              next B;
5515              }            } else {
5516                !!!cp ('t195');
5517              }
5518            }
5519    
5520              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5521    
5522              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5523              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3290  sub _tree_construction_main ($) { Line 5525  sub _tree_construction_main ($) {
5525              ## result in a new Text node.              ## result in a new Text node.
5526              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5527                            
5528              if ({              if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5529                # MUST                # MUST
5530                my $foster_parent_element;                my $foster_parent_element;
5531                my $next_sibling;                my $next_sibling;
5532                my $prev_sibling;                my $prev_sibling;
5533                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5534                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5535                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5536                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5537                        !!!cp ('t196');
5538                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5539                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5540                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5541                    } else {                    } else {
5542                        !!!cp ('t197');
5543                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5544                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5545                    }                    }
# Line 3317  sub _tree_construction_main ($) { Line 5551  sub _tree_construction_main ($) {
5551                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5552                if (defined $prev_sibling and                if (defined $prev_sibling and
5553                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5554                    !!!cp ('t198');
5555                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5556                } else {                } else {
5557                    !!!cp ('t199');
5558                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5559                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5560                     $next_sibling);                     $next_sibling);
5561                }                }
5562              } else {            $open_tables->[-1]->[1] = 1; # tainted
5563                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5564              !!!cp ('t200');
5565              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5566            }
5567                
5568            !!!next-token;
5569            next B;
5570          } elsif ($token->{type} == START_TAG_TOKEN) {
5571            if ({
5572                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5573                 th => 1, td => 1,
5574                }->{$token->{tag_name}}) {
5575              if ($self->{insertion_mode} == IN_TABLE_IM) {
5576                ## Clear back to table context
5577                while (not ($self->{open_elements}->[-1]->[1]
5578                                & TABLE_SCOPING_EL)) {
5579                  !!!cp ('t201');
5580                  pop @{$self->{open_elements}};
5581              }              }
5582                            
5583              !!!next-token;              !!!insert-element ('tbody',, $token);
5584              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5585            } elsif ($token->{type} eq 'start tag') {              ## reprocess in the "in table body" insertion mode...
5586              if ({            }
5587                   tr => ($self->{insertion_mode} ne 'in row'),            
5588                   th => 1, td => 1,            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5589                  }->{$token->{tag_name}}) {              unless ($token->{tag_name} eq 'tr') {
5590                if ($self->{insertion_mode} eq 'in table') {                !!!cp ('t202');
5591                  ## Clear back to table context                !!!parse-error (type => 'missing start tag:tr', token => $token);
5592                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              }
                        $self->{open_elements}->[-1]->[1] ne 'html') {  
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                   pop @{$self->{open_elements}};  
                 }  
                   
                 !!!insert-element ('tbody');  
                 $self->{insertion_mode} = 'in table body';  
                 ## reprocess in the "in table body" insertion mode...  
               }  
   
               if ($self->{insertion_mode} eq 'in table body') {  
                 unless ($token->{tag_name} eq 'tr') {  
                   !!!parse-error (type => 'missing start tag:tr');  
                 }  
5593                                    
5594                  ## Clear back to table body context              ## Clear back to table body context
5595                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5596                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5597                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5598                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                ## ISSUE: Can this case be reached?
5599                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5600                  }              }
5601                                    
5602                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
5603                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5604                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5605                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5606                      !!!nack ('t204');
5607                    !!!next-token;                    !!!next-token;
5608                    redo B;                    next B;
5609                  } else {                  } else {
5610                    !!!insert-element ('tr');                    !!!cp ('t205');
5611                      !!!insert-element ('tr',, $token);
5612                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5613                  }                  }
5614                  } else {
5615                    !!!cp ('t206');
5616                }                }
5617    
5618                ## Clear back to table row context                ## Clear back to table row context
5619                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5620                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5621                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5622                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5623                }                }
5624                                
5625                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5626                $self->{insertion_mode} = 'in cell';                $self->{insertion_mode} = IN_CELL_IM;
5627    
5628                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5629                                
5630                  !!!nack ('t207.1');
5631                !!!next-token;                !!!next-token;
5632                redo B;                next B;
5633              } elsif ({              } elsif ({
5634                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5635                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5636                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5637                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5638                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
5639                  ## As if </tr>                  ## As if </tr>
5640                  ## have an element in table scope                  ## have an element in table scope
5641                  my $i;                  my $i;
5642                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5643                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5644                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5645                        !!!cp ('t208');
5646                      $i = $_;                      $i = $_;
5647                      last INSCOPE;                      last INSCOPE;
5648                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5649                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5650                      last INSCOPE;                      last INSCOPE;
5651                    }                    }
5652                  } # INSCOPE                  } # INSCOPE
5653                  unless (defined $i) {                  unless (defined $i) {
5654                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5655    ## TODO: This type is wrong.
5656                      !!!parse-error (type => 'unmacthed end tag',
5657                                      text => $token->{tag_name}, token => $token);
5658                    ## Ignore the token                    ## Ignore the token
5659                      !!!nack ('t210.1');
5660                    !!!next-token;                    !!!next-token;
5661                    redo B;                    next B;
5662                  }                  }
5663                                    
5664                  ## Clear back to table row context                  ## Clear back to table row context
5665                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5666                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5667                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5668                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5669                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5670                  }                  }
5671                                    
5672                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5673                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5674                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5675                      !!!cp ('t212');
5676                    ## reprocess                    ## reprocess
5677                    redo B;                    !!!ack-later;
5678                      next B;
5679                  } else {                  } else {
5680                      !!!cp ('t213');
5681                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5682                  }                  }
5683                }                }
5684    
5685                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5686                  ## have an element in table scope                  ## have an element in table scope
5687                  my $i;                  my $i;
5688                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5689                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5690                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5691                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5692                      $i = $_;                      $i = $_;
5693                      last INSCOPE;                      last INSCOPE;
5694                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5695                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5696                      last INSCOPE;                      last INSCOPE;
5697                    }                    }
5698                  } # INSCOPE                  } # INSCOPE
5699                  unless (defined $i) {                  unless (defined $i) {
5700                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5701    ## TODO: This erorr type is wrong.
5702                      !!!parse-error (type => 'unmatched end tag',
5703                                      text => $token->{tag_name}, token => $token);
5704                    ## Ignore the token                    ## Ignore the token
5705                      !!!nack ('t216.1');
5706                    !!!next-token;                    !!!next-token;
5707                    redo B;                    next B;
5708                  }                  }
5709    
5710                  ## Clear back to table body context                  ## Clear back to table body context
5711                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5712                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5713                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5714                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5715                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5716                  }                  }
5717                                    
# Line 3470  sub _tree_construction_main ($) { Line 5723  sub _tree_construction_main ($) {
5723                  ## nop by definition                  ## nop by definition
5724                                    
5725                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5726                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
5727                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5728                  } else {
5729                    !!!cp ('t218');
5730                }                }
5731    
5732                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5733                  ## Clear back to table context                  ## Clear back to table context
5734                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5735                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5736                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5737                      ## ISSUE: Can this state be reached?
5738                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5739                  }                  }
5740                                    
5741                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5742                  $self->{insertion_mode} = 'in column group';                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5743                  ## reprocess                  ## reprocess
5744                  redo B;                  !!!ack-later;
5745                    next B;
5746                } elsif ({                } elsif ({
5747                          caption => 1,                          caption => 1,
5748                          colgroup => 1,                          colgroup => 1,
5749                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5750                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5751                  ## Clear back to table context                  ## Clear back to table context
5752                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5753                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5754                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5755                      ## ISSUE: Can this state be reached?
5756                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5757                  }                  }
5758                                    
5759                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5760                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5761                                    
5762                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5763                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5764                                             caption => 'in caption',                                             caption => IN_CAPTION_IM,
5765                                             colgroup => 'in column group',                                             colgroup => IN_COLUMN_GROUP_IM,
5766                                             tbody => 'in table body',                                             tbody => IN_TABLE_BODY_IM,
5767                                             tfoot => 'in table body',                                             tfoot => IN_TABLE_BODY_IM,
5768                                             thead => 'in table body',                                             thead => IN_TABLE_BODY_IM,
5769                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5770                  !!!next-token;                  !!!next-token;
5771                  redo B;                  !!!nack ('t220.1');
5772                    next B;
5773                } else {                } else {
5774                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5775                }                }
5776              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5777                ## NOTE: There are code clones for this "table in table"                !!!parse-error (type => 'not closed',
5778                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                                text => $self->{open_elements}->[-1]->[0]
5779                                      ->manakai_local_name,
5780                                  token => $token);
5781    
5782                ## As if </table>                ## As if </table>
5783                ## have a table element in table scope                ## have a table element in table scope
5784                my $i;                my $i;
5785                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5786                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5787                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5788                      !!!cp ('t221');
5789                    $i = $_;                    $i = $_;
5790                    last INSCOPE;                    last INSCOPE;
5791                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5792                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5793                    last INSCOPE;                    last INSCOPE;
5794                  }                  }
5795                } # INSCOPE                } # INSCOPE
5796                unless (defined $i) {                unless (defined $i) {
5797                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5798    ## TODO: The following is wrong, maybe.
5799                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5800                                    token => $token);
5801                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5802                    !!!nack ('t223.1');
5803                  !!!next-token;                  !!!next-token;
5804                  redo B;                  next B;
5805                }                }
5806                                
5807    ## TODO: Followings are removed from the latest spec.
5808                ## generate implied end tags                ## generate implied end tags
5809                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5810                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5811                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5812                }                }
5813    
5814                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5815                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5816                    ## NOTE: |<table><tr><table>|
5817                    !!!parse-error (type => 'not closed',
5818                                    text => $self->{open_elements}->[-1]->[0]
5819                                        ->manakai_local_name,
5820                                    token => $token);
5821                  } else {
5822                    !!!cp ('t226');
5823                }                }
5824    
5825                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5826                  pop @{$open_tables};
5827    
5828                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5829    
5830                ## reprocess            ## reprocess
5831                redo B;            !!!ack-later;
5832              next B;
5833            } elsif ($token->{tag_name} eq 'style') {
5834              if (not $open_tables->[-1]->[1]) { # tainted
5835                !!!cp ('t227.8');
5836                ## NOTE: This is a "as if in head" code clone.
5837                $parse_rcdata->(CDATA_CONTENT_MODEL);
5838                next B;
5839              } else {
5840                !!!cp ('t227.7');
5841                #
5842              }
5843            } elsif ($token->{tag_name} eq 'script') {
5844              if (not $open_tables->[-1]->[1]) { # tainted
5845                !!!cp ('t227.6');
5846                ## NOTE: This is a "as if in head" code clone.
5847                $script_start_tag->();
5848                next B;
5849              } else {
5850                !!!cp ('t227.5');
5851                #
5852              }
5853            } elsif ($token->{tag_name} eq 'input') {
5854              if (not $open_tables->[-1]->[1]) { # tainted
5855                if ($token->{attributes}->{type}) { ## TODO: case
5856                  my $type = lc $token->{attributes}->{type}->{value};
5857                  if ($type eq 'hidden') {
5858                    !!!cp ('t227.3');
5859                    !!!parse-error (type => 'in table',
5860                                    text => $token->{tag_name}, token => $token);
5861    
5862                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5863    
5864                    ## TODO: form element pointer
5865    
5866                    pop @{$self->{open_elements}};
5867    
5868                    !!!next-token;
5869                    !!!ack ('t227.2.1');
5870                    next B;
5871                  } else {
5872                    !!!cp ('t227.2');
5873                    #
5874                  }
5875              } else {              } else {
5876                  !!!cp ('t227.1');
5877                #                #
5878              }              }
5879            } elsif ($token->{type} eq 'end tag') {            } else {
5880                !!!cp ('t227.4');
5881                #
5882              }
5883            } else {
5884              !!!cp ('t227');
5885              #
5886            }
5887    
5888            !!!parse-error (type => 'in table', text => $token->{tag_name},
5889                            token => $token);
5890    
5891            $insert = $insert_to_foster;
5892            #
5893          } elsif ($token->{type} == END_TAG_TOKEN) {
5894              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5895                  $self->{insertion_mode} eq 'in row') {                  $self->{insertion_mode} == IN_ROW_IM) {
5896                ## have an element in table scope                ## have an element in table scope
5897                my $i;                my $i;
5898                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5899                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5900                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5901                      !!!cp ('t228');
5902                    $i = $_;                    $i = $_;
5903                    last INSCOPE;                    last INSCOPE;
5904                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5905                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5906                    last INSCOPE;                    last INSCOPE;
5907                  }                  }
5908                } # INSCOPE                } # INSCOPE
5909                unless (defined $i) {                unless (defined $i) {
5910                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5911                    !!!parse-error (type => 'unmatched end tag',
5912                                    text => $token->{tag_name}, token => $token);
5913                  ## Ignore the token                  ## Ignore the token
5914                    !!!nack ('t230.1');
5915                  !!!next-token;                  !!!next-token;
5916                  redo B;                  next B;
5917                  } else {
5918                    !!!cp ('t232');
5919                }                }
5920    
5921                ## Clear back to table row context                ## Clear back to table row context
5922                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5923                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5924                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5925                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5926                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5927                }                }
5928    
5929                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5930                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5931                !!!next-token;                !!!next-token;
5932                redo B;                !!!nack ('t231.1');
5933                  next B;
5934              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5935                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
5936                  ## As if </tr>                  ## As if </tr>
5937                  ## have an element in table scope                  ## have an element in table scope
5938                  my $i;                  my $i;
5939                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5940                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5941                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5942                        !!!cp ('t233');
5943                      $i = $_;                      $i = $_;
5944                      last INSCOPE;                      last INSCOPE;
5945                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5946                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5947                      last INSCOPE;                      last INSCOPE;
5948                    }                    }
5949                  } # INSCOPE                  } # INSCOPE
5950                  unless (defined $i) {                  unless (defined $i) {
5951                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5952    ## TODO: The following is wrong.
5953                      !!!parse-error (type => 'unmatched end tag',
5954                                      text => $token->{type}, token => $token);
5955                    ## Ignore the token                    ## Ignore the token
5956                      !!!nack ('t236.1');
5957                    !!!next-token;                    !!!next-token;
5958                    redo B;                    next B;
5959                  }                  }
5960                                    
5961                  ## Clear back to table row context                  ## Clear back to table row context
5962                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5963                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5964                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5965                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5966                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5967                  }                  }
5968                                    
5969                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5970                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5971                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5972                }                }
5973    
5974                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5975                  ## have an element in table scope                  ## have an element in table scope
5976                  my $i;                  my $i;
5977                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5978                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5979                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5980                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5981                      $i = $_;                      $i = $_;
5982                      last INSCOPE;                      last INSCOPE;
5983                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5984                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5985                      last INSCOPE;                      last INSCOPE;
5986                    }                    }
5987                  } # INSCOPE                  } # INSCOPE
5988                  unless (defined $i) {                  unless (defined $i) {
5989                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5990                      !!!parse-error (type => 'unmatched end tag',
5991                                      text => $token->{tag_name}, token => $token);
5992                    ## Ignore the token                    ## Ignore the token
5993                      !!!nack ('t239.1');
5994                    !!!next-token;                    !!!next-token;
5995                    redo B;                    next B;
5996                  }                  }
5997                                    
5998                  ## Clear back to table body context                  ## Clear back to table body context
5999                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6000                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
6001                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
6002                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
6003                  }                  }
6004                                    
# Line 3676  sub _tree_construction_main ($) { Line 6010  sub _tree_construction_main ($) {
6010                  ## nop by definition                  ## nop by definition
6011                                    
6012                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6013                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
6014                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
6015                }                }
6016    
6017                  ## NOTE: </table> in the "in table" insertion mode.
6018                  ## When you edit the code fragment below, please ensure that
6019                  ## the code for <table> in the "in table" insertion mode
6020                  ## is synced with it.
6021    
6022                ## have a table element in table scope                ## have a table element in table scope
6023                my $i;                my $i;
6024                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6025                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6026                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
6027                      !!!cp ('t241');
6028                    $i = $_;                    $i = $_;
6029                    last INSCOPE;                    last INSCOPE;
6030                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6031                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
6032                    last INSCOPE;                    last INSCOPE;
6033                  }                  }
6034                } # INSCOPE                } # INSCOPE
6035                unless (defined $i) {                unless (defined $i) {
6036                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
6037                    !!!parse-error (type => 'unmatched end tag',
6038                                    text => $token->{tag_name}, token => $token);
6039                  ## Ignore the token                  ## Ignore the token
6040                    !!!nack ('t243.1');
6041                  !!!next-token;                  !!!next-token;
6042                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
6043                }                }
6044                                    
6045                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
6046                  pop @{$open_tables};
6047                                
6048                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
6049                                
6050                !!!next-token;                !!!next-token;
6051                redo B;                next B;
6052              } elsif ({              } elsif ({
6053                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6054                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
6055                       ($self->{insertion_mode} eq 'in row' or                       $self->{insertion_mode} & ROW_IMS) {
6056                        $self->{insertion_mode} eq 'in table body')) {                if ($self->{insertion_mode} == IN_ROW_IM) {
               if ($self->{insertion_mode} eq 'in row') {  
6057                  ## have an element in table scope                  ## have an element in table scope
6058                  my $i;                  my $i;
6059                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6060                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6061                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6062                        !!!cp ('t247');
6063                      $i = $_;                      $i = $_;
6064                      last INSCOPE;                      last INSCOPE;
6065                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6066                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
6067                      last INSCOPE;                      last INSCOPE;
6068                    }                    }
6069                  } # INSCOPE                  } # INSCOPE
6070                    unless (defined $i) {                    unless (defined $i) {
6071                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
6072                        !!!parse-error (type => 'unmatched end tag',
6073                                        text => $token->{tag_name}, token => $token);
6074                      ## Ignore the token                      ## Ignore the token
6075                        !!!nack ('t249.1');
6076                      !!!next-token;                      !!!next-token;
6077                      redo B;                      next B;
6078                    }                    }
6079                                    
6080                  ## As if </tr>                  ## As if </tr>
# Line 3753  sub _tree_construction_main ($) { Line 6082  sub _tree_construction_main ($) {
6082                  my $i;                  my $i;
6083                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6084                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6085                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
6086                        !!!cp ('t250');
6087                      $i = $_;                      $i = $_;
6088                      last INSCOPE;                      last INSCOPE;
6089                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6090                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
6091                      last INSCOPE;                      last INSCOPE;
6092                    }                    }
6093                  } # INSCOPE                  } # INSCOPE
6094                    unless (defined $i) {                    unless (defined $i) {
6095                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
6096                        !!!parse-error (type => 'unmatched end tag',
6097                                        text => 'tr', token => $token);
6098                      ## Ignore the token                      ## Ignore the token
6099                        !!!nack ('t252.1');
6100                      !!!next-token;                      !!!next-token;
6101                      redo B;                      next B;
6102                    }                    }
6103                                    
6104                  ## Clear back to table row context                  ## Clear back to table row context
6105                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6106                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
6107                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
6108                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6109                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
6110                  }                  }
6111                                    
6112                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
6113                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
6114                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
6115                }                }
6116    
# Line 3786  sub _tree_construction_main ($) { Line 6118  sub _tree_construction_main ($) {
6118                my $i;                my $i;
6119                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6120                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6121                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6122                      !!!cp ('t254');
6123                    $i = $_;                    $i = $_;
6124                    last INSCOPE;                    last INSCOPE;
6125                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6126                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
6127                    last INSCOPE;                    last INSCOPE;
6128                  }                  }
6129                } # INSCOPE                } # INSCOPE
6130                unless (defined $i) {                unless (defined $i) {
6131                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
6132                    !!!parse-error (type => 'unmatched end tag',
6133                                    text => $token->{tag_name}, token => $token);
6134                  ## Ignore the token                  ## Ignore the token
6135                    !!!nack ('t256.1');
6136                  !!!next-token;                  !!!next-token;
6137                  redo B;                  next B;
6138                }                }
6139    
6140                ## Clear back to table body context                ## Clear back to table body context
6141                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6142                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
6143                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
6144                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6145                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6146                }                }
6147    
6148                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6149                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
6150                  !!!nack ('t257.1');
6151                !!!next-token;                !!!next-token;
6152                redo B;                next B;
6153              } elsif ({              } elsif ({
6154                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6155                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6156                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6157                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6158                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6159                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
6160                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
6161                !!!next-token;                            text => $token->{tag_name}, token => $token);
6162                redo B;            ## Ignore the token
6163              } else {            !!!nack ('t258.1');
6164                #             !!!next-token;
6165              }            next B;
6166            } else {          } else {
6167              die "$0: $token->{type}: Unknown token type";            !!!cp ('t259');
6168            }            !!!parse-error (type => 'in table:/',
6169                              text => $token->{tag_name}, token => $token);
6170    
6171        !!!parse-error (type => 'in table:'.$token->{tag_name});            $insert = $insert_to_foster;
6172              #
6173            }
6174          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6175            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6176                    @{$self->{open_elements}} == 1) { # redundant, maybe
6177              !!!parse-error (type => 'in body:#eof', token => $token);
6178              !!!cp ('t259.1');
6179              #
6180            } else {
6181              !!!cp ('t259.2');
6182              #
6183            }
6184    
6185        $insert = $insert_to_foster;          ## Stop parsing
6186        #          last B;
6187      } elsif ($self->{insertion_mode} eq 'in column group') {        } else {
6188            if ($token->{type} eq 'character') {          die "$0: $token->{type}: Unknown token type";
6189          }
6190        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6191              if ($token->{type} == CHARACTER_TOKEN) {
6192              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6193                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6194                unless (length $token->{data}) {                unless (length $token->{data}) {
6195                    !!!cp ('t260');
6196                  !!!next-token;                  !!!next-token;
6197                  redo B;                  next B;
6198                }                }
6199              }              }
6200                            
6201                !!!cp ('t261');
6202              #              #
6203            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
6204              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
6205                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
6206                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6207                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6208                  !!!ack ('t262.1');
6209                !!!next-token;                !!!next-token;
6210                redo B;                next B;
6211              } else {              } else {
6212                  !!!cp ('t263');
6213                #                #
6214              }              }
6215            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
6216              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6217                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6218                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
6219                    !!!parse-error (type => 'unmatched end tag',
6220                                    text => 'colgroup', token => $token);
6221                  ## Ignore the token                  ## Ignore the token
6222                  !!!next-token;                  !!!next-token;
6223                  redo B;                  next B;
6224                } else {                } else {
6225                    !!!cp ('t265');
6226                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6227                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
6228                  !!!next-token;                  !!!next-token;
6229                  redo B;                              next B;            
6230                }                }
6231              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6232                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
6233                  !!!parse-error (type => 'unmatched end tag',
6234                                  text => 'col', token => $token);
6235                ## Ignore the token                ## Ignore the token
6236                !!!next-token;                !!!next-token;
6237                redo B;                next B;
6238              } else {              } else {
6239                  !!!cp ('t267');
6240                #                #
6241              }              }
6242            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6243              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6244            }              @{$self->{open_elements}} == 1) { # redundant, maybe
6245              !!!cp ('t270.2');
6246              ## Stop parsing.
6247              last B;
6248            } else {
6249              ## NOTE: As if </colgroup>.
6250              !!!cp ('t270.1');
6251              pop @{$self->{open_elements}}; # colgroup
6252              $self->{insertion_mode} = IN_TABLE_IM;
6253              ## Reprocess.
6254              next B;
6255            }
6256          } else {
6257            die "$0: $token->{type}: Unknown token type";
6258          }
6259    
6260            ## As if </colgroup>            ## As if </colgroup>
6261            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6262              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
6263    ## TODO: Wrong error type?
6264                !!!parse-error (type => 'unmatched end tag',
6265                                text => 'colgroup', token => $token);
6266              ## Ignore the token              ## Ignore the token
6267                !!!nack ('t269.1');
6268              !!!next-token;              !!!next-token;
6269              redo B;              next B;
6270            } else {            } else {
6271                !!!cp ('t270');
6272              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6273              $self->{insertion_mode} = 'in table';              $self->{insertion_mode} = IN_TABLE_IM;
6274                !!!ack-later;
6275              ## reprocess              ## reprocess
6276              redo B;              next B;
6277              }
6278        } elsif ($self->{insertion_mode} & SELECT_IMS) {
6279          if ($token->{type} == CHARACTER_TOKEN) {
6280            !!!cp ('t271');
6281            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6282            !!!next-token;
6283            next B;
6284          } elsif ($token->{type} == START_TAG_TOKEN) {
6285            if ($token->{tag_name} eq 'option') {
6286              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6287                !!!cp ('t272');
6288                ## As if </option>
6289                pop @{$self->{open_elements}};
6290              } else {
6291                !!!cp ('t273');
6292            }            }
     } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
6293    
6294                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6295                !!!next-token;            !!!nack ('t273.1');
6296                redo B;            !!!next-token;
6297              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6298                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6299                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6300                  pop @{$self->{open_elements}};              !!!cp ('t274');
6301                }              ## As if </option>
6302                pop @{$self->{open_elements}};
6303              } else {
6304                !!!cp ('t275');
6305              }
6306    
6307                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6308                  ## As if </optgroup>              !!!cp ('t276');
6309                  pop @{$self->{open_elements}};              ## As if </optgroup>
6310                }              pop @{$self->{open_elements}};
6311              } else {
6312                !!!cp ('t277');
6313              }
6314    
6315                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6316                !!!next-token;            !!!nack ('t277.1');
6317                redo B;            !!!next-token;
6318              } elsif ($token->{tag_name} eq 'select') {            next B;
6319                !!!parse-error (type => 'not closed:select');          } elsif ({
6320                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6321                ## have an element in table scope                   }->{$token->{tag_name}} or
6322                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6323                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6324                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6325                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6326                    $i = $_;                     tr => 1, td => 1, th => 1,
6327                    last INSCOPE;                    }->{$token->{tag_name}})) {
6328                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6329                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6330                           }->{$node->[1]}) {                            token => $token);
6331                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6332                  }            ## as if there were </select> (otherwise).
6333                } # INSCOPE            ## have an element in table scope
6334                unless (defined $i) {            my $i;
6335                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6336                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6337                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6338                  redo B;                !!!cp ('t278');
6339                }                $i = $_;
6340                  last INSCOPE;
6341                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6342                  !!!cp ('t279');
6343                  last INSCOPE;
6344                }
6345              } # INSCOPE
6346              unless (defined $i) {
6347                !!!cp ('t280');
6348                !!!parse-error (type => 'unmatched end tag',
6349                                text => 'select', token => $token);
6350                ## Ignore the token
6351                !!!nack ('t280.1');
6352                !!!next-token;
6353                next B;
6354              }
6355                                
6356                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6357              splice @{$self->{open_elements}}, $i;
6358    
6359                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6360    
6361                !!!next-token;            if ($token->{tag_name} eq 'select') {
6362                redo B;              !!!nack ('t281.2');
6363              } else {              !!!next-token;
6364                #              next B;
6365              } else {
6366                !!!cp ('t281.1');
6367                !!!ack-later;
6368                ## Reprocess the token.
6369                next B;
6370              }
6371            } else {
6372              !!!cp ('t282');
6373              !!!parse-error (type => 'in select',
6374                              text => $token->{tag_name}, token => $token);
6375              ## Ignore the token
6376              !!!nack ('t282.1');
6377              !!!next-token;
6378              next B;
6379            }
6380          } elsif ($token->{type} == END_TAG_TOKEN) {
6381            if ($token->{tag_name} eq 'optgroup') {
6382              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6383                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6384                !!!cp ('t283');
6385                ## As if </option>
6386                splice @{$self->{open_elements}}, -2;
6387              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6388                !!!cp ('t284');
6389                pop @{$self->{open_elements}};
6390              } else {
6391                !!!cp ('t285');
6392                !!!parse-error (type => 'unmatched end tag',
6393                                text => $token->{tag_name}, token => $token);
6394                ## Ignore the token
6395              }
6396              !!!nack ('t285.1');
6397              !!!next-token;
6398              next B;
6399            } elsif ($token->{tag_name} eq 'option') {
6400              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6401                !!!cp ('t286');
6402                pop @{$self->{open_elements}};
6403              } else {
6404                !!!cp ('t287');
6405                !!!parse-error (type => 'unmatched end tag',
6406                                text => $token->{tag_name}, token => $token);
6407                ## Ignore the token
6408              }
6409              !!!nack ('t287.1');
6410              !!!next-token;
6411              next B;
6412            } elsif ($token->{tag_name} eq 'select') {
6413              ## have an element in table scope
6414              my $i;
6415              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6416                my $node = $self->{open_elements}->[$_];
6417                if ($node->[1] & SELECT_EL) {
6418                  !!!cp ('t288');
6419                  $i = $_;
6420                  last INSCOPE;
6421                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6422                  !!!cp ('t289');
6423                  last INSCOPE;
6424              }              }
6425            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6426              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6427                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6428                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6429                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6430                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6431                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6432                  pop @{$self->{open_elements}};              !!!next-token;
6433                } else {              next B;
6434                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            }
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
6435                                
6436                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6437              splice @{$self->{open_elements}}, $i;
6438    
6439                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6440    
6441                !!!next-token;            !!!nack ('t291.1');
6442                redo B;            !!!next-token;
6443              } elsif ({            next B;
6444                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6445                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6446                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6447                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6448                                   }->{$token->{tag_name}}) {
6449                ## have an element in table scope  ## TODO: The following is wrong?
6450                my $i;            !!!parse-error (type => 'unmatched end tag',
6451                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                            text => $token->{tag_name}, token => $token);
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
6452                                
6453                ## As if </select>            ## have an element in table scope
6454                ## have an element in table scope            my $i;
6455                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6456                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6457                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6458                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6459                    $i = $_;                $i = $_;
6460                    last INSCOPE;                last INSCOPE;
6461                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6462                            table => 1, html => 1,                !!!cp ('t293');
6463                           }->{$node->[1]}) {                last INSCOPE;
6464                    last INSCOPE;              }
6465                  }            } # INSCOPE
6466                } # INSCOPE            unless (defined $i) {
6467                unless (defined $i) {              !!!cp ('t294');
6468                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6469                  ## Ignore the </select> token              !!!nack ('t294.1');
6470                  !!!next-token; ## TODO: ok?              !!!next-token;
6471                  redo B;              next B;
6472                }            }
6473                                
6474                splice @{$self->{open_elements}}, $i;            ## As if </select>
6475              ## have an element in table scope
6476                $self->_reset_insertion_mode;            undef $i;
6477              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6478                ## reprocess              my $node = $self->{open_elements}->[$_];
6479                redo B;              if ($node->[1] & SELECT_EL) {
6480              } else {                !!!cp ('t295');
6481                #                $i = $_;
6482                  last INSCOPE;
6483                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6484    ## ISSUE: Can this state be reached?
6485                  !!!cp ('t296');
6486                  last INSCOPE;
6487              }              }
6488            } else {            } # INSCOPE
6489              #            unless (defined $i) {
6490                !!!cp ('t297');
6491    ## TODO: The following error type is correct?
6492                !!!parse-error (type => 'unmatched end tag',
6493                                text => 'select', token => $token);
6494                ## Ignore the </select> token
6495                !!!nack ('t297.1');
6496                !!!next-token; ## TODO: ok?
6497                next B;
6498            }            }
6499                  
6500              !!!cp ('t298');
6501              splice @{$self->{open_elements}}, $i;
6502    
6503              $self->_reset_insertion_mode;
6504    
6505            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
6506              ## reprocess
6507              next B;
6508            } else {
6509              !!!cp ('t299');
6510              !!!parse-error (type => 'in select:/',
6511                              text => $token->{tag_name}, token => $token);
6512            ## Ignore the token            ## Ignore the token
6513              !!!nack ('t299.3');
6514            !!!next-token;            !!!next-token;
6515            redo B;            next B;
6516      } elsif ($self->{insertion_mode} eq 'after body' or          }
6517               $self->{insertion_mode} eq 'after html body') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6518        if ($token->{type} eq 'character') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6519                    @{$self->{open_elements}} == 1) { # redundant, maybe
6520              !!!cp ('t299.1');
6521              !!!parse-error (type => 'in body:#eof', token => $token);
6522            } else {
6523              !!!cp ('t299.2');
6524            }
6525    
6526            ## Stop parsing.
6527            last B;
6528          } else {
6529            die "$0: $token->{type}: Unknown token type";
6530          }
6531        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6532          if ($token->{type} == CHARACTER_TOKEN) {
6533          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6534            my $data = $1;            my $data = $1;
6535            ## As if in body            ## As if in body
# Line 4077  sub _tree_construction_main ($) { Line 6538  sub _tree_construction_main ($) {
6538            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6539                        
6540            unless (length $token->{data}) {            unless (length $token->{data}) {
6541                !!!cp ('t300');
6542              !!!next-token;              !!!next-token;
6543              redo B;              next B;
6544            }            }
6545          }          }
6546                    
6547          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6548            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
6549              !!!parse-error (type => 'after html:#text', token => $token);
6550    
6551            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6552            } else {
6553              !!!cp ('t302');
6554          }          }
6555                    
6556          ## "after body" insertion mode          ## "after body" insertion mode
6557          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6558    
6559          $self->{insertion_mode} = 'in body';          $self->{insertion_mode} = IN_BODY_IM;
6560          ## reprocess          ## reprocess
6561          redo B;          next B;
6562        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
6563          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6564            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
6565              !!!parse-error (type => 'after html',
6566                              text => $token->{tag_name}, token => $token);
6567                        
6568            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6569            } else {
6570              !!!cp ('t304');
6571          }          }
6572    
6573          ## "after body" insertion mode          ## "after body" insertion mode
6574          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6575                            text => $token->{tag_name}, token => $token);
6576    
6577          $self->{insertion_mode} = 'in body';          $self->{insertion_mode} = IN_BODY_IM;
6578            !!!ack-later;
6579          ## reprocess          ## reprocess
6580          redo B;          next B;
6581        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6582          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6583            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
6584              !!!parse-error (type => 'after html:/',
6585                              text => $token->{tag_name}, token => $token);
6586                        
6587            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
6588            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6589            } else {
6590              !!!cp ('t306');
6591          }          }
6592    
6593          ## "after body" insertion mode          ## "after body" insertion mode
6594          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6595            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6596              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
6597                !!!parse-error (type => 'unmatched end tag',
6598                                text => 'html', token => $token);
6599              ## Ignore the token              ## Ignore the token
6600              !!!next-token;              !!!next-token;
6601              redo B;              next B;
6602            } else {            } else {
6603              $self->{insertion_mode} = 'after html body';              !!!cp ('t308');
6604                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6605              !!!next-token;              !!!next-token;
6606              redo B;              next B;
6607            }            }
6608          } else {          } else {
6609            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
6610              !!!parse-error (type => 'after body:/',
6611                              text => $token->{tag_name}, token => $token);
6612    
6613            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
6614            ## reprocess            ## reprocess
6615            redo B;            next B;
6616          }          }
6617          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6618            !!!cp ('t309.2');
6619            ## Stop parsing
6620            last B;
6621        } else {        } else {
6622          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6623        }        }
6624      } elsif ($self->{insertion_mode} eq 'in frameset' or      } elsif ($self->{insertion_mode} & FRAME_IMS) {
6625               $self->{insertion_mode} eq 'after frameset' or        if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} eq 'after html frameset') {  
       if ($token->{type} eq 'character') {  
6626          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6627            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6628                        
6629            unless (length $token->{data}) {            unless (length $token->{data}) {
6630                !!!cp ('t310');
6631              !!!next-token;              !!!next-token;
6632              redo B;              next B;
6633            }            }
6634          }          }
6635                    
6636          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6637            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6638              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6639            } elsif ($self->{insertion_mode} eq 'after frameset') {              !!!parse-error (type => 'in frameset:#text', token => $token);
6640              !!!parse-error (type => 'after frameset:#character');            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6641            } else { # "after html frameset"              !!!cp ('t312');
6642              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after frameset:#text', token => $token);
6643              } else { # "after after frameset"
6644              $self->{insertion_mode} = 'after frameset';              !!!cp ('t313');
6645              ## Reprocess in the "main" phase, "after frameset"...              !!!parse-error (type => 'after html:#text', token => $token);
             !!!parse-error (type => 'after frameset:#character');  
6646            }            }
6647                        
6648            ## Ignore the token.            ## Ignore the token.
6649            if (length $token->{data}) {            if (length $token->{data}) {
6650                !!!cp ('t314');
6651              ## reprocess the rest of characters              ## reprocess the rest of characters
6652            } else {            } else {
6653                !!!cp ('t315');
6654              !!!next-token;              !!!next-token;
6655            }            }
6656            redo B;            next B;
6657          }          }
6658                    
6659          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6660        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
         if ($self->{insertion_mode} eq 'after html frameset') {  
           !!!parse-error (type => 'after html:'.$token->{tag_name});  
   
           $self->{insertion_mode} = 'after frameset';  
           ## Process in the "main" phase, "after frameset" insertion mode...  
         }  
   
6661          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6662              $self->{insertion_mode} eq 'in frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6663            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6664              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6665              !!!nack ('t318.1');
6666            !!!next-token;            !!!next-token;
6667            redo B;            next B;
6668          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6669                   $self->{insertion_mode} eq 'in frameset') {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6670            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6671              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6672            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6673              !!!ack ('t319.1');
6674            !!!next-token;            !!!next-token;
6675            redo B;            next B;
6676          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6677            ## NOTE: As if in body.            !!!cp ('t320');
6678            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            ## NOTE: As if in head.
6679            redo B;            $parse_rcdata->(CDATA_CONTENT_MODEL);
6680              next B;
6681    
6682              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6683              ## has no parse error.
6684          } else {          } else {
6685            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6686              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6687            } else {              !!!parse-error (type => 'in frameset',
6688              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6689              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6690                !!!cp ('t322');
6691                !!!parse-error (type => 'after frameset',
6692                                text => $token->{tag_name}, token => $token);
6693              } else { # "after after frameset"
6694                !!!cp ('t322.2');
6695                !!!parse-error (type => 'after after frameset',
6696                                text => $token->{tag_name}, token => $token);
6697            }            }
6698            ## Ignore the token            ## Ignore the token
6699              !!!nack ('t322.1');
6700            !!!next-token;            !!!next-token;
6701            redo B;            next B;
         }  
       } elsif ($token->{type} eq 'end tag') {  
         if ($self->{insertion_mode} eq 'after html frameset') {  
           !!!parse-error (type => 'after html:/'.$token->{tag_name});  
   
           $self->{insertion_mode} = 'after frameset';  
           ## Process in the "main" phase, "after frameset" insertion mode...  
6702          }          }
6703          } elsif ($token->{type} == END_TAG_TOKEN) {
6704          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6705              $self->{insertion_mode} eq 'in frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6706            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6707                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6708              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6709                !!!parse-error (type => 'unmatched end tag',
6710                                text => $token->{tag_name}, token => $token);
6711              ## Ignore the token              ## Ignore the token
6712              !!!next-token;              !!!next-token;
6713            } else {            } else {
6714                !!!cp ('t326');
6715              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6716              !!!next-token;              !!!next-token;
6717            }            }
6718    
6719            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6720                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6721              $self->{insertion_mode} = 'after frameset';              !!!cp ('t327');
6722                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6723              } else {
6724                !!!cp ('t328');
6725            }            }
6726            redo B;            next B;
6727          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6728                   $self->{insertion_mode} eq 'after frameset') {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6729            $self->{insertion_mode} = 'after html frameset';            !!!cp ('t329');
6730              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6731            !!!next-token;            !!!next-token;
6732            redo B;            next B;
6733          } else {          } else {
6734            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6735              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6736            } else {              !!!parse-error (type => 'in frameset:/',
6737              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6738              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6739                !!!cp ('t330.1');
6740                !!!parse-error (type => 'after frameset:/',
6741                                text => $token->{tag_name}, token => $token);
6742              } else { # "after after html"
6743                !!!cp ('t331');
6744                !!!parse-error (type => 'after after frameset:/',
6745                                text => $token->{tag_name}, token => $token);
6746            }            }
6747            ## Ignore the token            ## Ignore the token
6748            !!!next-token;            !!!next-token;
6749            redo B;            next B;
6750          }          }
6751          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6752            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6753                    @{$self->{open_elements}} == 1) { # redundant, maybe
6754              !!!cp ('t331.1');
6755              !!!parse-error (type => 'in body:#eof', token => $token);
6756            } else {
6757              !!!cp ('t331.2');
6758            }
6759            
6760            ## Stop parsing
6761            last B;
6762        } else {        } else {
6763          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6764        }        }
# Line 4256  sub _tree_construction_main ($) { Line 6769  sub _tree_construction_main ($) {
6769      }      }
6770    
6771      ## "in body" insertion mode      ## "in body" insertion mode
6772      if ($token->{type} eq 'start tag') {      if ($token->{type} == START_TAG_TOKEN) {
6773        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6774            !!!cp ('t332');
6775          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6776          $script_start_tag->($insert);          $script_start_tag->();
6777          redo B;          next B;
6778        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6779            !!!cp ('t333');
6780          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6781          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6782          redo B;          next B;
6783        } elsif ({        } elsif ({
6784                  base => 1, link => 1,                  base => 1, link => 1,
6785                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6786            !!!cp ('t334');
6787          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
6788          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6789          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6790            !!!ack ('t334.1');
6791          !!!next-token;          !!!next-token;
6792          redo B;          next B;
6793        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6794          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
6795          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6796          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.
6797    
6798          unless ($self->{confident}) {          unless ($self->{confident}) {
6799            my $charset;            if ($token->{attributes}->{charset}) {
6800            if ($token->{attributes}->{charset}) { ## TODO: And if supported              !!!cp ('t335');
6801              $charset = $token->{attributes}->{charset}->{value};              ## NOTE: Whether the encoding is supported or not is handled
6802            }              ## in the {change_encoding} callback.
6803            if ($token->{attributes}->{'http-equiv'}) {              $self->{change_encoding}
6804              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6805              if ($token->{attributes}->{'http-equiv'}->{value}              
6806                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6807                    ->set_user_data (manakai_has_reference =>
6808                                         $token->{attributes}->{charset}
6809                                             ->{has_reference});
6810              } elsif ($token->{attributes}->{content}) {
6811                if ($token->{attributes}->{content}->{value}
6812                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6813                        [\x09-\x0D\x20]*=
6814                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6815                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6816                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                !!!cp ('t336');
6817              } ## TODO: And if supported                ## NOTE: Whether the encoding is supported or not is handled
6818                  ## in the {change_encoding} callback.
6819                  $self->{change_encoding}
6820                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6821                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6822                      ->set_user_data (manakai_has_reference =>
6823                                           $token->{attributes}->{content}
6824                                                 ->{has_reference});
6825                }
6826              }
6827            } else {
6828              if ($token->{attributes}->{charset}) {
6829                !!!cp ('t337');
6830                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6831                    ->set_user_data (manakai_has_reference =>
6832                                         $token->{attributes}->{charset}
6833                                             ->{has_reference});
6834              }
6835              if ($token->{attributes}->{content}) {
6836                !!!cp ('t338');
6837                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6838                    ->set_user_data (manakai_has_reference =>
6839                                         $token->{attributes}->{content}
6840                                             ->{has_reference});
6841            }            }
           ## TODO: Change the encoding  
6842          }          }
6843    
6844            !!!ack ('t338.1');
6845          !!!next-token;          !!!next-token;
6846          redo B;          next B;
6847        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6848          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6849          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6850          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6851            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6852        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6853          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6854                                
6855          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6856              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6857              !!!cp ('t342');
6858            ## Ignore the token            ## Ignore the token
6859          } else {          } else {
6860            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6861            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6862              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6863                  !!!cp ('t343');
6864                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6865                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6866                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6867              }              }
6868            }            }
6869          }          }
6870            !!!nack ('t343.1');
6871          !!!next-token;          !!!next-token;
6872          redo B;          next B;
6873        } elsif ({        } elsif ({
6874                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6875                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6876                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6877                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6878                  pre => 1,                  pre => 1, listing => 1,
6879                    form => 1,
6880                    table => 1,
6881                    hr => 1,
6882                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6883            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6884              !!!cp ('t350');
6885              !!!parse-error (type => 'in form:form', token => $token);
6886              ## Ignore the token
6887              !!!nack ('t350.1');
6888              !!!next-token;
6889              next B;
6890            }
6891    
6892          ## has a p element in scope          ## has a p element in scope
6893          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6894            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6895              !!!back-token;              !!!cp ('t344');
6896              $token = {type => 'end tag', tag_name => 'p'};              !!!back-token; # <form>
6897              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6898            } elsif ({                        line => $token->{line}, column => $token->{column}};
6899                      table => 1, caption => 1, td => 1, th => 1,              next B;
6900                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6901                     }->{$_->[1]}) {              !!!cp ('t345');
6902              last INSCOPE;              last INSCOPE;
6903            }            }
6904          } # INSCOPE          } # INSCOPE
6905                        
6906          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6907          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6908              !!!nack ('t346.1');
6909            !!!next-token;            !!!next-token;
6910            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
6911              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6912              unless (length $token->{data}) {              unless (length $token->{data}) {
6913                  !!!cp ('t346');
6914                !!!next-token;                !!!next-token;
6915                } else {
6916                  !!!cp ('t349');
6917              }              }
6918              } else {
6919                !!!cp ('t348');
6920            }            }
6921          } else {          } elsif ($token->{tag_name} eq 'form') {
6922              !!!cp ('t347.1');
6923              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6924    
6925              !!!nack ('t347.2');
6926            !!!next-token;            !!!next-token;
6927          }          } elsif ($token->{tag_name} eq 'table') {
6928          redo B;            !!!cp ('t382');
6929        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6930          if (defined $self->{form_element}) {            
6931            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6932            ## Ignore the token  
6933              !!!nack ('t382.1');
6934              !!!next-token;
6935            } elsif ($token->{tag_name} eq 'hr') {
6936              !!!cp ('t386');
6937              pop @{$self->{open_elements}};
6938            
6939              !!!nack ('t386.1');
6940            !!!next-token;            !!!next-token;
           redo B;  
6941          } else {          } else {
6942            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6943            !!!next-token;            !!!next-token;
           redo B;  
6944          }          }
6945        } elsif ($token->{tag_name} eq 'li') {          next B;
6946          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6947          ## has a p element in scope          ## has a p element in scope
6948          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6949            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6950              !!!back-token;              !!!cp ('t353');
6951              $token = {type => 'end tag', tag_name => 'p'};              !!!back-token; # <x>
6952              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6953            } elsif ({                        line => $token->{line}, column => $token->{column}};
6954                      table => 1, caption => 1, td => 1, th => 1,              next B;
6955                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6956                     }->{$_->[1]}) {              !!!cp ('t354');
6957              last INSCOPE;              last INSCOPE;
6958            }            }
6959          } # INSCOPE          } # INSCOPE
# Line 4403  sub _tree_construction_main ($) { Line 6961  sub _tree_construction_main ($) {
6961          ## Step 1          ## Step 1
6962          my $i = -1;          my $i = -1;
6963          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6964            my $li_or_dtdd = {li => {li => 1},
6965                              dt => {dt => 1, dd => 1},
6966                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6967          LI: {          LI: {
6968            ## Step 2            ## Step 2
6969            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6970              if ($i != -1) {              if ($i != -1) {
6971                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6972                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6973              }                                text => $self->{open_elements}->[-1]->[0]
6974              splice @{$self->{open_elements}}, $i;                                    ->manakai_local_name,
6975              last LI;                                token => $token);
6976            }              } else {
6977                            !!!cp ('t356');
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
6978              }              }
6979              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6980              last LI;              last LI;
6981              } else {
6982                !!!cp ('t357');
6983            }            }
6984                        
6985            ## Step 3            ## Step 3
6986            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6987                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6988                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6989                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6990                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6991                  not ($node->[1] & DIV_EL)) {
6992                !!!cp ('t358');
6993              last LI;              last LI;
6994            }            }
6995                        
6996              !!!cp ('t359');
6997            ## Step 4            ## Step 4
6998            $i--;            $i--;
6999            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
7000            redo LI;            redo LI;
7001          } # LI          } # LI
7002                        
7003          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7004            !!!nack ('t359.1');
7005          !!!next-token;          !!!next-token;
7006          redo B;          next B;
7007        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
7008          ## has a p element in scope          ## has a p element in scope
7009          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
7010            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
7011              !!!back-token;              !!!cp ('t367');
7012              $token = {type => 'end tag', tag_name => 'p'};              !!!back-token; # <plaintext>
7013              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
7014            } elsif ({                        line => $token->{line}, column => $token->{column}};
7015                      table => 1, caption => 1, td => 1, th => 1,              next B;
7016                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
7017                     }->{$_->[1]}) {              !!!cp ('t368');
7018              last INSCOPE;              last INSCOPE;
7019            }            }
7020          } # INSCOPE          } # INSCOPE
7021                        
7022          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7023                        
7024          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
7025                        
7026            !!!nack ('t368.1');
7027          !!!next-token;          !!!next-token;
7028          redo B;          next B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
7029        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
7030          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
7031            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
7032            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
7033              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
7034                !!!parse-error (type => 'in a:a', token => $token);
7035                            
7036              !!!back-token;              !!!back-token; # <a>
7037              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
7038              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
7039                $formatting_end_tag->($token);
7040                            
7041              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
7042                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
7043                    !!!cp ('t372');
7044                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
7045                  last AFE2;                  last AFE2;
7046                }                }
7047              } # AFE2              } # AFE2
7048              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
7049                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
7050                    !!!cp ('t373');
7051                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
7052                  last OE;                  last OE;
7053                }                }
7054              } # OE              } # OE
7055              last AFE;              last AFE;
7056            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
7057                !!!cp ('t374');
7058              last AFE;              last AFE;
7059            }            }
7060          } # AFE          } # AFE
7061                        
7062          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7063    
7064          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7065          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7066    
7067            !!!nack ('t374.1');
7068          !!!next-token;          !!!next-token;
7069          redo B;          next B;
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         redo B;  
7070        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
7071          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7072    
7073          ## has a |nobr| element in scope          ## has a |nobr| element in scope
7074          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7075            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7076            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
7077              !!!parse-error (type => 'not closed:nobr');              !!!cp ('t376');
7078              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
7079              $token = {type => 'end tag', tag_name => 'nobr'};              !!!back-token; # <nobr>
7080              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7081            } elsif ({                        line => $token->{line}, column => $token->{column}};
7082                      table => 1, caption => 1, td => 1, th => 1,              next B;
7083                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
7084                     }->{$node->[1]}) {              !!!cp ('t377');
7085              last INSCOPE;              last INSCOPE;
7086            }            }
7087          } # INSCOPE          } # INSCOPE
7088                    
7089          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7090          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7091                    
7092            !!!nack ('t377.1');
7093          !!!next-token;          !!!next-token;
7094          redo B;          next B;
7095        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
7096          ## has a button element in scope          ## has a button element in scope
7097          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7098            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7099            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
7100              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
7101              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
7102              $token = {type => 'end tag', tag_name => 'button'};              !!!back-token; # <button>
7103              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
7104            } elsif ({                        line => $token->{line}, column => $token->{column}};
7105                      table => 1, caption => 1, td => 1, th => 1,              next B;
7106                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
7107                     }->{$node->[1]}) {              !!!cp ('t379');
7108              last INSCOPE;              last INSCOPE;
7109            }            }
7110          } # INSCOPE          } # INSCOPE
7111                        
7112          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7113                        
7114          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7115          push @$active_formatting_elements, ['#marker', ''];  
7116            ## TODO: associate with $self->{form_element} if defined
7117    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
7118          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
7119            
7120          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
7121          !!!next-token;          !!!next-token;
7122          redo B;          next B;
7123        } elsif ({        } elsif ({
7124                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
7125                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
7126                  image => 1,                  noembed => 1,
7127                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7128                    noscript => 0, ## TODO: 1 if scripting is enabled
7129                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7130          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
7131            !!!parse-error (type => 'image');            !!!cp ('t381');
7132            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
7133            } else {
7134              !!!cp ('t399');
7135          }          }
7136            ## NOTE: There is an "as if in body" code clone.
7137          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
7138          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
7139        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
7140          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
7141                    
7142          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
7143              !!!cp ('t389');
7144            ## Ignore the token            ## Ignore the token
7145              !!!nack ('t389'); ## NOTE: Not acknowledged.
7146            !!!next-token;            !!!next-token;
7147            redo B;            next B;
7148          } else {          } else {
7149              !!!ack ('t391.1');
7150    
7151            my $at = $token->{attributes};            my $at = $token->{attributes};
7152            my $form_attrs;            my $form_attrs;
7153            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 4737  sub _tree_construction_main ($) { Line 7156  sub _tree_construction_main ($) {
7156            delete $at->{action};            delete $at->{action};
7157            delete $at->{prompt};            delete $at->{prompt};
7158            my @tokens = (            my @tokens = (
7159                          {type => 'start tag', tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
7160                           attributes => $form_attrs},                           attributes => $form_attrs,
7161                          {type => 'start tag', tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
7162                          {type => 'start tag', tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
7163                          {type => 'start tag', tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
7164                            {type => START_TAG_TOKEN, tag_name => 'p',
7165                             line => $token->{line}, column => $token->{column}},
7166                            {type => START_TAG_TOKEN, tag_name => 'label',
7167                             line => $token->{line}, column => $token->{column}},
7168                         );                         );
7169            if ($prompt_attr) {            if ($prompt_attr) {
7170              push @tokens, {type => 'character', data => $prompt_attr->{value}};              !!!cp ('t390');
7171                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7172                               #line => $token->{line}, column => $token->{column},
7173                              };
7174            } else {            } else {
7175              push @tokens, {type => 'character',              !!!cp ('t391');
7176                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD              push @tokens, {type => CHARACTER_TOKEN,
7177                               data => 'This is a searchable index. Insert your search keywords here: ',
7178                               #line => $token->{line}, column => $token->{column},
7179                              }; # SHOULD
7180              ## TODO: make this configurable              ## TODO: make this configurable
7181            }            }
7182            push @tokens,            push @tokens,
7183                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7184                          #{type => 'character', data => ''}, # SHOULD                           line => $token->{line}, column => $token->{column}},
7185                          {type => 'end tag', tag_name => 'label'},                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7186                          {type => 'end tag', tag_name => 'p'},                          {type => END_TAG_TOKEN, tag_name => 'label',
7187                          {type => 'start tag', tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
7188                          {type => 'end tag', tag_name => 'form'};                          {type => END_TAG_TOKEN, tag_name => 'p',
7189            $token = shift @tokens;                           line => $token->{line}, column => $token->{column}},
7190                            {type => START_TAG_TOKEN, tag_name => 'hr',
7191                             line => $token->{line}, column => $token->{column}},
7192                            {type => END_TAG_TOKEN, tag_name => 'form',
7193                             line => $token->{line}, column => $token->{column}};
7194            !!!back-token (@tokens);            !!!back-token (@tokens);
7195            redo B;            !!!next-token;
7196              next B;
7197          }          }
7198        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
7199          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
7200          my $el;          my $el;
7201          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7202                    
7203          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
7204          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4773  sub _tree_construction_main ($) { Line 7207  sub _tree_construction_main ($) {
7207          $insert->($el);          $insert->($el);
7208                    
7209          my $text = '';          my $text = '';
7210            !!!nack ('t392.1');
7211          !!!next-token;          !!!next-token;
7212          if ($token->{type} eq 'character') {          if ($token->{type} == CHARACTER_TOKEN) {
7213            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
7214            unless (length $token->{data}) {            unless (length $token->{data}) {
7215                !!!cp ('t392');
7216              !!!next-token;              !!!next-token;
7217              } else {
7218                !!!cp ('t393');
7219            }            }
7220            } else {
7221              !!!cp ('t394');
7222          }          }
7223          while ($token->{type} eq 'character') {          while ($token->{type} == CHARACTER_TOKEN) {
7224              !!!cp ('t395');
7225            $text .= $token->{data};            $text .= $token->{data};
7226            !!!next-token;            !!!next-token;
7227          }          }
7228          if (length $text) {          if (length $text) {
7229              !!!cp ('t396');
7230            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
7231          }          }
7232                    
7233          $self->{content_model} = PCDATA_CONTENT_MODEL;          $self->{content_model} = PCDATA_CONTENT_MODEL;
7234                    
7235          if ($token->{type} eq 'end tag' and          if ($token->{type} == END_TAG_TOKEN and
7236              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
7237              !!!cp ('t397');
7238            ## Ignore the token            ## Ignore the token
7239          } else {          } else {
7240            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
7241              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7242          }          }
7243          !!!next-token;          !!!next-token;
7244            next B;
7245          } elsif ($token->{tag_name} eq 'rt' or
7246                   $token->{tag_name} eq 'rp') {
7247            ## has a |ruby| element in scope
7248            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7249              my $node = $self->{open_elements}->[$_];
7250              if ($node->[1] & RUBY_EL) {
7251                !!!cp ('t398.1');
7252                ## generate implied end tags
7253                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7254                  !!!cp ('t398.2');
7255                  pop @{$self->{open_elements}};
7256                }
7257                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7258                  !!!cp ('t398.3');
7259                  !!!parse-error (type => 'not closed',
7260                                  text => $self->{open_elements}->[-1]->[0]
7261                                      ->manakai_local_name,
7262                                  token => $token);
7263                  pop @{$self->{open_elements}}
7264                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7265                }
7266                last INSCOPE;
7267              } elsif ($node->[1] & SCOPING_EL) {
7268                !!!cp ('t398.4');
7269                last INSCOPE;
7270              }
7271            } # INSCOPE
7272    
7273            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7274    
7275            !!!nack ('t398.5');
7276            !!!next-token;
7277          redo B;          redo B;
7278        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
7279                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There are two "as if in body" code clones.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
7280          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7281    
7282            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7283    
7284            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7285    
7286            ## "adjust foreign attributes" - done in insert-element-f
7287                    
7288          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7289                    
7290          $self->{insertion_mode} = 'in select';          if ($self->{self_closing}) {
7291              pop @{$self->{open_elements}};
7292              !!!ack ('t398.1');
7293            } else {
7294              !!!cp ('t398.2');
7295              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7296              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7297              ## mode, "in body" (not "in foreign content") secondary insertion
7298              ## mode, maybe.
7299            }
7300    
7301          !!!next-token;          !!!next-token;
7302          redo B;          next B;
7303        } elsif ({        } elsif ({
7304                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7305                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
7306                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
7307                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7308                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7309          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
7310            !!!parse-error (type => 'in body',
7311                            text => $token->{tag_name}, token => $token);
7312          ## Ignore the token          ## Ignore the token
7313            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7314          !!!next-token;          !!!next-token;
7315          redo B;          next B;
7316                    
7317          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7318        } else {        } else {
7319            if ($token->{tag_name} eq 'image') {
7320              !!!cp ('t384');
7321              !!!parse-error (type => 'image', token => $token);
7322              $token->{tag_name} = 'img';
7323            } else {
7324              !!!cp ('t385');
7325            }
7326    
7327            ## NOTE: There is an "as if <br>" code clone.
7328          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7329                    
7330          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7331    
7332            if ({
7333                 applet => 1, marquee => 1, object => 1,
7334                }->{$token->{tag_name}}) {
7335              !!!cp ('t380');
7336              push @$active_formatting_elements, ['#marker', ''];
7337              !!!nack ('t380.1');
7338            } elsif ({
7339                      b => 1, big => 1, em => 1, font => 1, i => 1,
7340                      s => 1, small => 1, strile => 1,
7341                      strong => 1, tt => 1, u => 1,
7342                     }->{$token->{tag_name}}) {
7343              !!!cp ('t375');
7344              push @$active_formatting_elements, $self->{open_elements}->[-1];
7345              !!!nack ('t375.1');
7346            } elsif ($token->{tag_name} eq 'input') {
7347              !!!cp ('t388');
7348              ## TODO: associate with $self->{form_element} if defined
7349              pop @{$self->{open_elements}};
7350              !!!ack ('t388.2');
7351            } elsif ({
7352                      area => 1, basefont => 1, bgsound => 1, br => 1,
7353                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7354                      #image => 1,
7355                     }->{$token->{tag_name}}) {
7356              !!!cp ('t388.1');
7357              pop @{$self->{open_elements}};
7358              !!!ack ('t388.3');
7359            } elsif ($token->{tag_name} eq 'select') {
7360              ## TODO: associate with $self->{form_element} if defined
7361            
7362              if ($self->{insertion_mode} & TABLE_IMS or
7363                  $self->{insertion_mode} & BODY_TABLE_IMS or
7364                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7365                !!!cp ('t400.1');
7366                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7367              } else {
7368                !!!cp ('t400.2');
7369                $self->{insertion_mode} = IN_SELECT_IM;
7370              }
7371              !!!nack ('t400.3');
7372            } else {
7373              !!!nack ('t402');
7374            }
7375                    
7376          !!!next-token;          !!!next-token;
7377          redo B;          next B;
7378        }        }
7379      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} == END_TAG_TOKEN) {
7380        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7381          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7382              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7383            for (@{$self->{open_elements}}) {          INSCOPE: {
7384              unless ({            for (reverse @{$self->{open_elements}}) {
7385                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7386                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7387                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7388                      }->{$_->[1]}) {                last INSCOPE;
7389                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
7390                  !!!cp ('t405.1');
7391                  last;
7392              }              }
7393            }            }
7394    
7395            $self->{insertion_mode} = 'after body';            !!!parse-error (type => 'start tag not allowed',
7396                              text => $token->{tag_name}, token => $token);
7397              ## NOTE: Ignore the token.
7398            !!!next-token;            !!!next-token;
7399            redo B;            next B;
7400          } else {          } # INSCOPE
7401            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
7402            ## Ignore the token          for (@{$self->{open_elements}}) {
7403            !!!next-token;            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7404            redo B;              !!!cp ('t403');
7405                !!!parse-error (type => 'not closed',
7406                                text => $_->[0]->manakai_local_name,
7407                                token => $token);
7408                last;
7409              } else {
7410                !!!cp ('t404');
7411              }
7412          }          }
7413    
7414            $self->{insertion_mode} = AFTER_BODY_IM;
7415            !!!next-token;
7416            next B;
7417        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7418          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          ## TODO: Update this code.  It seems that the code below is not
7419            ## up-to-date, though it has same effect as speced.
7420            if (@{$self->{open_elements}} > 1 and
7421                $self->{open_elements}->[1]->[1] & BODY_EL) {
7422            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7423            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7424              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
7425                !!!parse-error (type => 'not closed',
7426                                text => $self->{open_elements}->[1]->[0]
7427                                    ->manakai_local_name,
7428                                token => $token);
7429              } else {
7430                !!!cp ('t407');
7431            }            }
7432            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
7433            ## reprocess            ## reprocess
7434            redo B;            next B;
7435          } else {          } else {
7436            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
7437              !!!parse-error (type => 'unmatched end tag',
7438                              text => $token->{tag_name}, token => $token);
7439            ## Ignore the token            ## Ignore the token
7440            !!!next-token;            !!!next-token;
7441            redo B;            next B;
7442          }          }
7443        } elsif ({        } elsif ({
7444                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7445                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7446                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7447                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7448                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7449                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7450          ## has an element in scope          ## has an element in scope
7451          my $i;          my $i;
7452          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7453            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7454            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7455              ## generate implied end tags              !!!cp ('t410');
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
7456              $i = $_;              $i = $_;
7457              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7458            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7459                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7460              last INSCOPE;              last INSCOPE;
7461            }            }
7462          } # INSCOPE          } # INSCOPE
7463            
7464          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7465            if (defined $i) {            !!!cp ('t413');
7466              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag',
7467                              text => $token->{tag_name}, token => $token);
7468              ## NOTE: Ignore the token.
7469            } else {
7470              ## Step 1. generate implied end tags
7471              while ({
7472                      ## END_TAG_OPTIONAL_EL
7473                      dd => ($token->{tag_name} ne 'dd'),
7474                      dt => ($token->{tag_name} ne 'dt'),
7475                      li => ($token->{tag_name} ne 'li'),
7476                      p => 1,
7477                      rt => 1,
7478                      rp => 1,
7479                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7480                !!!cp ('t409');
7481                pop @{$self->{open_elements}};
7482              }
7483    
7484              ## Step 2.
7485              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7486                      ne $token->{tag_name}) {
7487                !!!cp ('t412');
7488                !!!parse-error (type => 'not closed',
7489                                text => $self->{open_elements}->[-1]->[0]
7490                                    ->manakai_local_name,
7491                                token => $token);
7492            } else {            } else {
7493              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
7494            }            }
7495          }  
7496                      ## Step 3.
         if (defined $i) {  
7497            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7498          } elsif ($token->{tag_name} eq 'p') {  
7499            ## As if <p>, then reprocess the current token            ## Step 4.
7500            my $el;            $clear_up_to_marker->()
7501            !!!create-element ($el, 'p');                if {
7502            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
7503                  }->{$token->{tag_name}};
7504          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7505          !!!next-token;          !!!next-token;
7506          redo B;          next B;
7507        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7508            undef $self->{form_element};
7509    
7510          ## has an element in scope          ## has an element in scope
7511            my $i;
7512          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7513            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7514            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7515              ## generate implied end tags              !!!cp ('t418');
7516              if ({              $i = $_;
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
7517              last INSCOPE;              last INSCOPE;
7518            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7519                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7520              last INSCOPE;              last INSCOPE;
7521            }            }
7522          } # INSCOPE          } # INSCOPE
7523            
7524          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7525            pop @{$self->{open_elements}};            !!!cp ('t421');
7526              !!!parse-error (type => 'unmatched end tag',
7527                              text => $token->{tag_name}, token => $token);
7528              ## NOTE: Ignore the token.
7529          } else {          } else {
7530            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            ## Step 1. generate implied end tags
7531              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7532                !!!cp ('t417');
7533                pop @{$self->{open_elements}};
7534              }
7535              
7536              ## Step 2.
7537              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7538                      ne $token->{tag_name}) {
7539                !!!cp ('t417.1');
7540                !!!parse-error (type => 'not closed',
7541                                text => $self->{open_elements}->[-1]->[0]
7542                                    ->manakai_local_name,
7543                                token => $token);
7544              } else {
7545                !!!cp ('t420');
7546              }  
7547              
7548              ## Step 3.
7549              splice @{$self->{open_elements}}, $i;
7550          }          }
7551    
         undef $self->{form_element};  
7552          !!!next-token;          !!!next-token;
7553          redo B;          next B;
7554        } elsif ({        } elsif ({
7555                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7556                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 4973  sub _tree_construction_main ($) { Line 7558  sub _tree_construction_main ($) {
7558          my $i;          my $i;
7559          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7560            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7561            if ({            if ($node->[1] & HEADING_EL) {
7562                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,              !!!cp ('t423');
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
7563              $i = $_;              $i = $_;
7564              last INSCOPE;              last INSCOPE;
7565            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7566                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7567              last INSCOPE;              last INSCOPE;
7568            }            }
7569          } # INSCOPE          } # INSCOPE
7570            
7571          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7572            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!cp ('t425.1');
7573              !!!parse-error (type => 'unmatched end tag',
7574                              text => $token->{tag_name}, token => $token);
7575              ## NOTE: Ignore the token.
7576            } else {
7577              ## Step 1. generate implied end tags
7578              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7579                !!!cp ('t422');
7580                pop @{$self->{open_elements}};
7581              }
7582              
7583              ## Step 2.
7584              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7585                      ne $token->{tag_name}) {
7586                !!!cp ('t425');
7587                !!!parse-error (type => 'unmatched end tag',
7588                                text => $token->{tag_name}, token => $token);
7589              } else {
7590                !!!cp ('t426');
7591              }
7592    
7593              ## Step 3.
7594              splice @{$self->{open_elements}}, $i;
7595          }          }
7596                    
         splice @{$self->{open_elements}}, $i if defined $i;  
7597          !!!next-token;          !!!next-token;
7598          redo B;          next B;
7599          } elsif ($token->{tag_name} eq 'p') {
7600            ## has an element in scope
7601            my $i;
7602            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7603              my $node = $self->{open_elements}->[$_];
7604              if ($node->[1] & P_EL) {
7605                !!!cp ('t410.1');
7606                $i = $_;
7607                last INSCOPE;
7608              } elsif ($node->[1] & SCOPING_EL) {
7609                !!!cp ('t411.1');
7610                last INSCOPE;
7611              }
7612            } # INSCOPE
7613    
7614            if (defined $i) {
7615              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7616                      ne $token->{tag_name}) {
7617                !!!cp ('t412.1');
7618                !!!parse-error (type => 'not closed',
7619                                text => $self->{open_elements}->[-1]->[0]
7620                                    ->manakai_local_name,
7621                                token => $token);
7622              } else {
7623                !!!cp ('t414.1');
7624              }
7625    
7626              splice @{$self->{open_elements}}, $i;
7627            } else {
7628              !!!cp ('t413.1');
7629              !!!parse-error (type => 'unmatched end tag',
7630                              text => $token->{tag_name}, token => $token);
7631    
7632              !!!cp ('t415.1');
7633              ## As if <p>, then reprocess the current token
7634              my $el;
7635              !!!create-element ($el, $HTML_NS, 'p',, $token);
7636              $insert->($el);
7637              ## NOTE: Not inserted into |$self->{open_elements}|.
7638            }
7639    
7640            !!!next-token;
7641            next B;
7642        } elsif ({        } elsif ({
7643                  a => 1,                  a => 1,
7644                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
7645                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
7646                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7647                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7648          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
7649          redo B;          $formatting_end_tag->($token);
7650            next B;
7651        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7652          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
7653            !!!parse-error (type => 'unmatched end tag',
7654                            text => 'br', token => $token);
7655    
7656          ## As if <br>          ## As if <br>
7657          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7658                    
7659          my $el;          my $el;
7660          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7661          $insert->($el);          $insert->($el);
7662                    
7663          ## Ignore the token.          ## Ignore the token.
7664          !!!next-token;          !!!next-token;
7665          redo B;          next B;
7666        } elsif ({        } elsif ({
7667                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7668                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5037  sub _tree_construction_main ($) { Line 7675  sub _tree_construction_main ($) {
7675                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
7676                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7677                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7678          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
7679            !!!parse-error (type => 'unmatched end tag',
7680                            text => $token->{tag_name}, token => $token);
7681          ## Ignore the token          ## Ignore the token
7682          !!!next-token;          !!!next-token;
7683          redo B;          next B;
7684                    
7685          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7686                    
# Line 5051  sub _tree_construction_main ($) { Line 7691  sub _tree_construction_main ($) {
7691    
7692          ## Step 2          ## Step 2
7693          S2: {          S2: {
7694            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7695              ## Step 1              ## Step 1
7696              ## generate implied end tags              ## generate implied end tags
7697              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7698                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7699                   td => 1, th => 1, tr => 1,                ## NOTE: |<ruby><rt></ruby>|.
7700                   tbody => 1, tfoot=> 1, thead => 1,                ## ISSUE: <ruby><rt></rt> will also take this code path,
7701                  }->{$self->{open_elements}->[-1]->[1]}) {                ## which seems wrong.
7702                !!!back-token;                pop @{$self->{open_elements}};
7703                $token = {type => 'end tag',                $node_i++;
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7704              }              }
7705                    
7706              ## Step 2              ## Step 2
7707              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7708                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      ne $token->{tag_name}) {
7709                  !!!cp ('t431');
7710                  ## NOTE: <x><y></x>
7711                  !!!parse-error (type => 'not closed',
7712                                  text => $self->{open_elements}->[-1]->[0]
7713                                      ->manakai_local_name,
7714                                  token => $token);
7715                } else {
7716                  !!!cp ('t432');
7717              }              }
7718                            
7719              ## Step 3              ## Step 3
7720              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7721    
7722              !!!next-token;              !!!next-token;
7723              last S2;              last S2;
7724            } else {            } else {
7725              ## Step 3              ## Step 3
7726              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7727                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7728                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7729                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7730                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7731                  !!!parse-error (type => 'unmatched end tag',
7732                                  text => $token->{tag_name}, token => $token);
7733                ## Ignore the token                ## Ignore the token
7734                !!!next-token;                !!!next-token;
7735                last S2;                last S2;
7736              }              }
7737    
7738                !!!cp ('t434');
7739            }            }
7740                        
7741            ## Step 4            ## Step 4
# Line 5095  sub _tree_construction_main ($) { Line 7745  sub _tree_construction_main ($) {
7745            ## Step 5;            ## Step 5;
7746            redo S2;            redo S2;
7747          } # S2          } # S2
7748          redo B;          next B;
7749        }        }
7750      }      }
7751      redo B;      next B;
7752      } continue { # B
7753        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7754          ## NOTE: The code below is executed in cases where it does not have
7755          ## to be, but it it is harmless even in those cases.
7756          ## has an element in scope
7757          INSCOPE: {
7758            for (reverse 0..$#{$self->{open_elements}}) {
7759              my $node = $self->{open_elements}->[$_];
7760              if ($node->[1] & FOREIGN_EL) {
7761                last INSCOPE;
7762              } elsif ($node->[1] & SCOPING_EL) {
7763                last;
7764              }
7765            }
7766            
7767            ## NOTE: No foreign element in scope.
7768            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7769          } # INSCOPE
7770        }
7771    } # B    } # B
7772    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
7773    ## Stop parsing # MUST    ## Stop parsing # MUST
7774        
7775    ## TODO: script stuffs    ## TODO: script stuffs
7776  } # _tree_construct_main  } # _tree_construct_main
7777    
7778  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7779    my $class = shift;    my $class = shift;
7780    my $node = shift;    my $node = shift;
7781    my $s = \$_[0];    #my $s = \$_[0];
7782    my $onerror = $_[1];    my $onerror = $_[1];
7783      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7784    
7785      ## ISSUE: Should {confident} be true?
7786    
7787    my $nt = $node->node_type;    my $nt = $node->node_type;
7788    if ($nt == 9) {    if ($nt == 9) {
# Line 5132  sub set_inner_html ($$$) { Line 7799  sub set_inner_html ($$$) {
7799      }      }
7800    
7801      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7802      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7803    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7804      ## TODO: If non-html element      ## TODO: If non-html element
7805    
7806      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7807    
7808    ## TODO: Support for $get_wrapper
7809    
7810      ## Step 1 # MUST      ## Step 1 # MUST
7811      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7812      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 5145  sub set_inner_html ($$$) { Line 7814  sub set_inner_html ($$$) {
7814      my $p = $class->new;      my $p = $class->new;
7815      $p->{document} = $doc;      $p->{document} = $doc;
7816    
7817      ## Step 9 # MUST      ## Step 8 # MUST
7818      my $i = 0;      my $i = 0;
7819      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7820      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7821      $p->{set_next_input_character} = sub {      require Whatpm::Charset::DecodeHandle;
7822        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7823        $input = $get_wrapper->($input);
7824        $p->{set_next_char} = sub {
7825        my $self = shift;        my $self = shift;
7826    
7827        pop @{$self->{prev_input_character}};        my $char = '';
7828        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        if (defined $self->{next_next_char}) {
7829            $char = $self->{next_next_char};
7830            delete $self->{next_next_char};
7831            $self->{next_char} = ord $char;
7832          } else {
7833            $self->{char_buffer} = '';
7834            $self->{char_buffer_pos} = 0;
7835            
7836            my $count = $input->manakai_read_until
7837                ($self->{char_buffer},
7838                 qr/(?![\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
7839                   $self->{char_buffer_pos});
7840            if ($count) {
7841              $self->{line_prev} = $self->{line};
7842              $self->{column_prev} = $self->{column};
7843              $self->{column}++;
7844              $self->{next_char}
7845                  = ord substr ($self->{char_buffer},
7846                                $self->{char_buffer_pos}++, 1);
7847              return;
7848            }
7849            
7850            if ($input->read ($char, 1)) {
7851              $self->{next_char} = ord $char;
7852            } else {
7853              $self->{next_char} = -1;
7854              return;
7855            }
7856          }
7857    
7858          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7859          $p->{column}++;
7860    
7861        $self->{next_input_character} = -1 and return if $i >= length $$s;        if ($self->{next_char} == 0x000A) { # LF
7862        $self->{next_input_character} = ord substr $$s, $i++, 1;          $p->{line}++;
7863        $column++;          $p->{column} = 0;
7864            !!!cp ('i1');
7865        if ($self->{next_input_character} == 0x000A) { # LF        } elsif ($self->{next_char} == 0x000D) { # CR
7866          $line++;  ## TODO: support for abort/streaming
7867          $column = 0;          my $next = '';
7868        } elsif ($self->{next_input_character} == 0x000D) { # CR          if ($input->read ($next, 1) and $next ne "\x0A") {
7869          $i++ if substr ($$s, $i, 1) eq "\x0A";            $self->{next_next_char} = $next;
7870          $self->{next_input_character} = 0x000A; # LF # MUST          }
7871          $line++;          $self->{next_char} = 0x000A; # LF # MUST
7872          $column = 0;          $p->{line}++;
7873        } elsif ($self->{next_input_character} > 0x10FFFF) {          $p->{column} = 0;
7874          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          !!!cp ('i2');
7875        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_char} > 0x10FFFF) {
7876            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7877            !!!cp ('i3');
7878          } elsif ($self->{next_char} == 0x0000) { # NULL
7879            !!!cp ('i4');
7880          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7881          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7882          } elsif ($self->{next_char} <= 0x0008 or
7883                   (0x000E <= $self->{next_char} and
7884                    $self->{next_char} <= 0x001F) or
7885                   (0x007F <= $self->{next_char} and
7886                    $self->{next_char} <= 0x009F) or
7887                   (0xD800 <= $self->{next_char} and
7888                    $self->{next_char} <= 0xDFFF) or
7889                   (0xFDD0 <= $self->{next_char} and
7890                    $self->{next_char} <= 0xFDDF) or
7891                   {
7892                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7893                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7894                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7895                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7896                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7897                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7898                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7899                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7900                    0x10FFFE => 1, 0x10FFFF => 1,
7901                   }->{$self->{next_char}}) {
7902            !!!cp ('i4.1');
7903            if ($self->{next_char} < 0x10000) {
7904              !!!parse-error (type => 'control char',
7905                              text => (sprintf 'U+%04X', $self->{next_char}));
7906            } else {
7907              !!!parse-error (type => 'control char',
7908                              text => (sprintf 'U-%08X', $self->{next_char}));
7909            }
7910        }        }
7911      };      };
7912      $p->{prev_input_character} = [-1, -1, -1];  
7913      $p->{next_input_character} = -1;      $p->{read_until} = sub {
7914              #my ($scalar, $specials_range, $offset) = @_;
7915          return 0 if defined $p->{next_next_char};
7916    
7917          my $pattern = qr/(?![$_[1]\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/;
7918          my $offset = $_[2] || 0;
7919          
7920          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7921            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7922            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7923              substr ($_[0], $offset)
7924                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7925              my $count = $+[0] - $-[0];
7926              if ($count) {
7927                $p->{column} += $count;
7928                $p->{char_buffer_pos} += $count;
7929                $p->{line_prev} = $p->{line};
7930                $p->{column_prev} = $p->{column} - 1;
7931                $p->{prev_char} = [-1, -1, -1];
7932                $p->{next_char} = -1;
7933              }
7934              return $count;
7935            } else {
7936              return 0;
7937            }
7938          } else {
7939            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7940            if ($count) {
7941              $p->{column} += $count;
7942              $p->{column_prev} += $count;
7943              $p->{prev_char} = [-1, -1, -1];
7944              $p->{next_char} = -1;
7945            }
7946            return $count;
7947          }
7948        }; # $p->{read_until}
7949    
7950      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7951        my (%opt) = @_;        my (%opt) = @_;
7952        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7953          my $column = $opt{column};
7954          if (defined $opt{token} and defined $opt{token}->{line}) {
7955            $line = $opt{token}->{line};
7956            $column = $opt{token}->{column};
7957          }
7958          warn "Parse error ($opt{type}) at line $line column $column\n";
7959      };      };
7960      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7961        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7962      };      };
7963            
7964        my $char_onerror = sub {
7965          my (undef, $type, %opt) = @_;
7966          $ponerror->(layer => 'encode',
7967                      line => $p->{line}, column => $p->{column} + 1,
7968                      %opt, type => $type);
7969        }; # $char_onerror
7970        $input->onerror ($char_onerror);
7971    
7972      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7973      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7974    
7975      ## Step 2      ## Step 2
7976      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7977      $p->{content_model} = {      $p->{content_model} = {
7978        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7979        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5206  sub set_inner_html ($$$) { Line 7990  sub set_inner_html ($$$) {
7990          unless defined $p->{content_model};          unless defined $p->{content_model};
7991          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7992    
7993      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7994          ## TODO: Foreign element OK?
7995    
7996      ## Step 4      ## Step 3
7997      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7998        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7999    
8000      ## Step 5 # MUST      ## Step 4 # MUST
8001      $doc->append_child ($root);      $doc->append_child ($root);
8002    
8003      ## Step 6 # MUST      ## Step 5 # MUST
8004      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
8005    
8006      undef $p->{head_element};      undef $p->{head_element};
8007    
8008      ## Step 7 # MUST      ## Step 6 # MUST
8009      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
8010    
8011      ## Step 8 # MUST      ## Step 7 # MUST
8012      my $anode = $node;      my $anode = $node;
8013      AN: while (defined $anode) {      AN: while (defined $anode) {
8014        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
8015          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
8016          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
8017            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
8018                !!!cp ('i5');
8019              $p->{form_element} = $anode;              $p->{form_element} = $anode;
8020              last AN;              last AN;
8021            }            }
# Line 5238  sub set_inner_html ($$$) { Line 8024  sub set_inner_html ($$$) {
8024        $anode = $anode->parent_node;        $anode = $anode->parent_node;
8025      } # AN      } # AN
8026            
8027      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
8028      {      {
8029        my $self = $p;        my $self = $p;
8030        !!!next-token;        !!!next-token;
8031      }      }
8032      $p->_tree_construction_main;      $p->_tree_construction_main;
8033    
8034      ## Step 11 # MUST      ## Step 10 # MUST
8035      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
8036      for (@cn) {      for (@cn) {
8037        $node->remove_child ($_);        $node->remove_child ($_);
8038      }      }
8039      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
8040    
8041      ## Step 12 # MUST      ## Step 11 # MUST
8042      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
8043      for (@cn) {      for (@cn) {
8044        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5262  sub set_inner_html ($$$) { Line 8047  sub set_inner_html ($$$) {
8047      ## ISSUE: mutation events?      ## ISSUE: mutation events?
8048    
8049      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
8050    
8051        delete $p->{parse_error}; # delete loop
8052    } else {    } else {
8053      die "$0: |set_inner_html| is not defined for node of type $nt";      die "$0: |set_inner_html| is not defined for node of type $nt";
8054    }    }
# Line 5269  sub set_inner_html ($$$) { Line 8056  sub set_inner_html ($$$) {
8056    
8057  } # tree construction stage  } # tree construction stage
8058    
8059  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
8060    my (undef, $node, $on_error) = @_;  push our @ISA, 'Error';
   
   ## Step 1  
   my $s = '';  
   
   my $in_cdata;  
   my $parent = $node;  
   while (defined $parent) {  
     if ($parent->node_type == 1 and  
         $parent->namespace_uri eq 'http://www.w3.org/1999/xhtml' and  
         {  
           style => 1, script => 1, xmp => 1, iframe => 1,  
           noembed => 1, noframes => 1, noscript => 1,  
         }->{$parent->local_name}) { ## TODO: case thingy  
       $in_cdata = 1;  
     }  
     $parent = $parent->parent_node;  
   }  
   
   ## Step 2  
   my @node = @{$node->child_nodes};  
   C: while (@node) {  
     my $child = shift @node;  
     unless (ref $child) {  
       if ($child eq 'cdata-out') {  
         $in_cdata = 0;  
       } else {  
         $s .= $child; # end tag  
       }  
       next C;  
     }  
       
     my $nt = $child->node_type;  
     if ($nt == 1) { # Element  
       my $tag_name = $child->tag_name; ## TODO: manakai_tag_name  
       $s .= '<' . $tag_name;  
       ## NOTE: Non-HTML case:  
       ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = $attr->name; ## TODO: manakai_name  
         $s .= ' ' . $attr_name . '="';  
         my $attr_value = $attr->value;  
         ## escape  
         $attr_value =~ s/&/&amp;/g;  
         $attr_value =~ s/</&lt;/g;  
         $attr_value =~ s/>/&gt;/g;  
         $attr_value =~ s/"/&quot;/g;  
         $s .= $attr_value . '"';  
       }  
       $s .= '>';  
         
       next C if {  
         area => 1, base => 1, basefont => 1, bgsound => 1,  
         br => 1, col => 1, embed => 1, frame => 1, hr => 1,  
         img => 1, input => 1, link => 1, meta => 1, param => 1,  
         spacer => 1, wbr => 1,  
       }->{$tag_name};  
   
       $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 1,  
         plaintext => 1,  
       }->{$tag_name}) {  
         unshift @node, 'cdata-out';  
         $in_cdata = 1;  
       }  
   
       unshift @node, @{$child->child_nodes}, '</' . $tag_name . '>';  
     } elsif ($nt == 3 or $nt == 4) {  
       if ($in_cdata) {  
         $s .= $child->data;  
       } else {  
         my $value = $child->data;  
         $value =~ s/&/&amp;/g;  
         $value =~ s/</&lt;/g;  
         $value =~ s/>/&gt;/g;  
         $value =~ s/"/&quot;/g;  
         $s .= $value;  
       }  
     } elsif ($nt == 8) {  
       $s .= '<!--' . $child->data . '-->';  
     } elsif ($nt == 10) {  
       $s .= '<!DOCTYPE ' . $child->name . '>';  
     } elsif ($nt == 5) { # entrefs  
       push @node, @{$child->child_nodes};  
     } else {  
       $on_error->($child) if defined $on_error;  
     }  
     ## ISSUE: This code does not support PIs.  
   } # C  
     
   ## Step 3  
   return \$s;  
 } # get_inner_html  
8061    
8062  1;  1;
8063  # $Date$  # $Date$

Legend:
Removed from v.1.53  
changed lines
  Added in v.1.180

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24