/[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.56 by wakaba, Sat Aug 11 07:19:18 2007 UTC revision 1.134 by wakaba, Sat May 17 05:34:23 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  ## TODO: 1252 parse error (revision 1264)
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  ## TODO: 8859-11 = 874 (revision 1271)
13  ## is not yet clear.  
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
15  ## "{U+FEFF}..." in GB18030?  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
16    my $SVG_NS = q<http://www.w3.org/2000/svg>;
17  my $permitted_slash_tag_name = {  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
18    base => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
19    link => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
20    meta => 1,  
21    hr => 1,  sub A_EL () { 0b1 }
22    br => 1,  sub ADDRESS_EL () { 0b10 }
23    img=> 1,  sub BODY_EL () { 0b100 }
24    embed => 1,  sub BUTTON_EL () { 0b1000 }
25    param => 1,  sub CAPTION_EL () { 0b10000 }
26    area => 1,  sub DD_EL () { 0b100000 }
27    col => 1,  sub DIV_EL () { 0b1000000 }
28    input => 1,  sub DT_EL () { 0b10000000 }
29    sub FORM_EL () { 0b100000000 }
30    sub FORMATTING_EL () { 0b1000000000 }
31    sub FRAMESET_EL () { 0b10000000000 }
32    sub HEADING_EL () { 0b100000000000 }
33    sub HTML_EL () { 0b1000000000000 }
34    sub LI_EL () { 0b10000000000000 }
35    sub NOBR_EL () { 0b100000000000000 }
36    sub OPTION_EL () { 0b1000000000000000 }
37    sub OPTGROUP_EL () { 0b10000000000000000 }
38    sub P_EL () { 0b100000000000000000 }
39    sub SELECT_EL () { 0b1000000000000000000 }
40    sub TABLE_EL () { 0b10000000000000000000 }
41    sub TABLE_CELL_EL () { 0b100000000000000000000 }
42    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
43    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
44    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
45    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
46    sub FOREIGN_EL () { 0b10000000000000000000000000 }
47    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
48    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
49    
50    sub TABLE_ROWS_EL () {
51      TABLE_EL |
52      TABLE_ROW_EL |
53      TABLE_ROW_GROUP_EL
54    }
55    
56    sub END_TAG_OPTIONAL_EL () {
57      DD_EL |
58      DT_EL |
59      LI_EL |
60      P_EL
61    }
62    
63    sub ALL_END_TAG_OPTIONAL_EL () {
64      END_TAG_OPTIONAL_EL |
65      BODY_EL |
66      HTML_EL |
67      TABLE_CELL_EL |
68      TABLE_ROW_EL |
69      TABLE_ROW_GROUP_EL
70    }
71    
72    sub SCOPING_EL () {
73      BUTTON_EL |
74      CAPTION_EL |
75      HTML_EL |
76      TABLE_EL |
77      TABLE_CELL_EL |
78      MISC_SCOPING_EL
79    }
80    
81    sub TABLE_SCOPING_EL () {
82      HTML_EL |
83      TABLE_EL
84    }
85    
86    sub TABLE_ROWS_SCOPING_EL () {
87      HTML_EL |
88      TABLE_ROW_GROUP_EL
89    }
90    
91    sub TABLE_ROW_SCOPING_EL () {
92      HTML_EL |
93      TABLE_ROW_EL
94    }
95    
96    sub SPECIAL_EL () {
97      ADDRESS_EL |
98      BODY_EL |
99      DIV_EL |
100      END_TAG_OPTIONAL_EL |
101      FORM_EL |
102      FRAMESET_EL |
103      HEADING_EL |
104      OPTION_EL |
105      OPTGROUP_EL |
106      SELECT_EL |
107      TABLE_ROW_EL |
108      TABLE_ROW_GROUP_EL |
109      MISC_SPECIAL_EL
110    }
111    
112    my $el_category = {
113      a => A_EL | FORMATTING_EL,
114      address => ADDRESS_EL,
115      applet => MISC_SCOPING_EL,
116      area => MISC_SPECIAL_EL,
117      b => FORMATTING_EL,
118      base => MISC_SPECIAL_EL,
119      basefont => MISC_SPECIAL_EL,
120      bgsound => MISC_SPECIAL_EL,
121      big => FORMATTING_EL,
122      blockquote => MISC_SPECIAL_EL,
123      body => BODY_EL,
124      br => MISC_SPECIAL_EL,
125      button => BUTTON_EL,
126      caption => CAPTION_EL,
127      center => MISC_SPECIAL_EL,
128      col => MISC_SPECIAL_EL,
129      colgroup => MISC_SPECIAL_EL,
130      dd => DD_EL,
131      dir => MISC_SPECIAL_EL,
132      div => DIV_EL,
133      dl => MISC_SPECIAL_EL,
134      dt => DT_EL,
135      em => FORMATTING_EL,
136      embed => MISC_SPECIAL_EL,
137      fieldset => MISC_SPECIAL_EL,
138      font => FORMATTING_EL,
139      form => FORM_EL,
140      frame => MISC_SPECIAL_EL,
141      frameset => FRAMESET_EL,
142      h1 => HEADING_EL,
143      h2 => HEADING_EL,
144      h3 => HEADING_EL,
145      h4 => HEADING_EL,
146      h5 => HEADING_EL,
147      h6 => HEADING_EL,
148      head => MISC_SPECIAL_EL,
149      hr => MISC_SPECIAL_EL,
150      html => HTML_EL,
151      i => FORMATTING_EL,
152      iframe => MISC_SPECIAL_EL,
153      img => MISC_SPECIAL_EL,
154      input => MISC_SPECIAL_EL,
155      isindex => MISC_SPECIAL_EL,
156      li => LI_EL,
157      link => MISC_SPECIAL_EL,
158      listing => MISC_SPECIAL_EL,
159      marquee => MISC_SCOPING_EL,
160      menu => MISC_SPECIAL_EL,
161      meta => MISC_SPECIAL_EL,
162      nobr => NOBR_EL | FORMATTING_EL,
163      noembed => MISC_SPECIAL_EL,
164      noframes => MISC_SPECIAL_EL,
165      noscript => MISC_SPECIAL_EL,
166      object => MISC_SCOPING_EL,
167      ol => MISC_SPECIAL_EL,
168      optgroup => OPTGROUP_EL,
169      option => OPTION_EL,
170      p => P_EL,
171      param => MISC_SPECIAL_EL,
172      plaintext => MISC_SPECIAL_EL,
173      pre => MISC_SPECIAL_EL,
174      s => FORMATTING_EL,
175      script => MISC_SPECIAL_EL,
176      select => SELECT_EL,
177      small => FORMATTING_EL,
178      spacer => MISC_SPECIAL_EL,
179      strike => FORMATTING_EL,
180      strong => FORMATTING_EL,
181      style => MISC_SPECIAL_EL,
182      table => TABLE_EL,
183      tbody => TABLE_ROW_GROUP_EL,
184      td => TABLE_CELL_EL,
185      textarea => MISC_SPECIAL_EL,
186      tfoot => TABLE_ROW_GROUP_EL,
187      th => TABLE_CELL_EL,
188      thead => TABLE_ROW_GROUP_EL,
189      title => MISC_SPECIAL_EL,
190      tr => TABLE_ROW_EL,
191      tt => FORMATTING_EL,
192      u => FORMATTING_EL,
193      ul => MISC_SPECIAL_EL,
194      wbr => MISC_SPECIAL_EL,
195    };
196    
197    my $el_category_f = {
198      $MML_NS => {
199        'annotation-xml' => MML_AXML_EL,
200        mi => FOREIGN_FLOW_CONTENT_EL,
201        mo => FOREIGN_FLOW_CONTENT_EL,
202        mn => FOREIGN_FLOW_CONTENT_EL,
203        ms => FOREIGN_FLOW_CONTENT_EL,
204        mtext => FOREIGN_FLOW_CONTENT_EL,
205      },
206      $SVG_NS => {
207        foreignObject => FOREIGN_FLOW_CONTENT_EL,
208        desc => FOREIGN_FLOW_CONTENT_EL,
209        title => FOREIGN_FLOW_CONTENT_EL,
210      },
211      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
212    };
213    
214    my $svg_attr_name = {
215      attributetype => 'attributeType',
216      basefrequency => 'baseFrequency',
217      baseprofile => 'baseProfile',
218      calcmode => 'calcMode',
219      clippathunits => 'clipPathUnits',
220      contentscripttype => 'contentScriptType',
221      contentstyletype => 'contentStyleType',
222      diffuseconstant => 'diffuseConstant',
223      edgemode => 'edgeMode',
224      externalresourcesrequired => 'externalResourcesRequired',
225      fecolormatrix => 'feColorMatrix',
226      fecomposite => 'feComposite',
227      fegaussianblur => 'feGaussianBlur',
228      femorphology => 'feMorphology',
229      fetile => 'feTile',
230      filterres => 'filterRes',
231      filterunits => 'filterUnits',
232      glyphref => 'glyphRef',
233      gradienttransform => 'gradientTransform',
234      gradientunits => 'gradientUnits',
235      kernelmatrix => 'kernelMatrix',
236      kernelunitlength => 'kernelUnitLength',
237      keypoints => 'keyPoints',
238      keysplines => 'keySplines',
239      keytimes => 'keyTimes',
240      lengthadjust => 'lengthAdjust',
241      limitingconeangle => 'limitingConeAngle',
242      markerheight => 'markerHeight',
243      markerunits => 'markerUnits',
244      markerwidth => 'markerWidth',
245      maskcontentunits => 'maskContentUnits',
246      maskunits => 'maskUnits',
247      numoctaves => 'numOctaves',
248      pathlength => 'pathLength',
249      patterncontentunits => 'patternContentUnits',
250      patterntransform => 'patternTransform',
251      patternunits => 'patternUnits',
252      pointsatx => 'pointsAtX',
253      pointsaty => 'pointsAtY',
254      pointsatz => 'pointsAtZ',
255      preservealpha => 'preserveAlpha',
256      preserveaspectratio => 'preserveAspectRatio',
257      primitiveunits => 'primitiveUnits',
258      refx => 'refX',
259      refy => 'refY',
260      repeatcount => 'repeatCount',
261      repeatdur => 'repeatDur',
262      requiredextensions => 'requiredExtensions',
263      specularconstant => 'specularConstant',
264      specularexponent => 'specularExponent',
265      spreadmethod => 'spreadMethod',
266      startoffset => 'startOffset',
267      stddeviation => 'stdDeviation',
268      stitchtiles => 'stitchTiles',
269      surfacescale => 'surfaceScale',
270      systemlanguage => 'systemLanguage',
271      tablevalues => 'tableValues',
272      targetx => 'targetX',
273      targety => 'targetY',
274      textlength => 'textLength',
275      viewbox => 'viewBox',
276      viewtarget => 'viewTarget',
277      xchannelselector => 'xChannelSelector',
278      ychannelselector => 'yChannelSelector',
279      zoomandpan => 'zoomAndPan',
280  };  };
281    
282    my $foreign_attr_xname = {
283      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
284      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
285      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
286      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
287      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
288      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
289      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
290      'xml:base' => [$XML_NS, ['xml', 'base']],
291      'xml:lang' => [$XML_NS, ['xml', 'lang']],
292      'xml:space' => [$XML_NS, ['xml', 'space']],
293      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
294      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
295    };
296    
297    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
298    
299  my $c1_entity_char = {  my $c1_entity_char = {
300    0x80 => 0x20AC,    0x80 => 0x20AC,
301    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 62  my $c1_entity_char = { Line 331  my $c1_entity_char = {
331    0x9F => 0x0178,    0x9F => 0x0178,
332  }; # $c1_entity_char  }; # $c1_entity_char
333    
334  my $special_category = {  sub parse_byte_string ($$$$;$) {
335    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = ref $_[0] ? shift : shift->new;
336    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
337    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
338    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    my $s;
339    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
340    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,    my $onerror = $_[2] || sub {
341    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,      my (%opt) = @_;
342    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,      warn "Parse error ($opt{type})\n";
343    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    };
344    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    $self->{parse_error} = $onerror; # updated later by parse_char_string
345  };  
346  my $scoping_category = {    ## HTML5 encoding sniffing algorithm
347    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    require Message::Charset::Info;
348    table => 1, td => 1, th => 1,    my $charset;
349  };    my ($e, $e_status);
350  my $formatting_category = {  
351    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,    SNIFFING: {
352    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
353  };      ## Step 1
354  # $phrasing_category: all other elements      if (defined $charset_name) {
355          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
356    
357          ## ISSUE: Unsupported encoding is not ignored according to the spec.
358          ($e, $e_status) = $charset->get_perl_encoding
359              (allow_error_reporting => 1,
360               allow_fallback => 1);
361          if ($e) {
362            $self->{confident} = 1;
363            last SNIFFING;
364          }
365        }
366    
367        ## Step 2
368        # wait
369    
370        ## Step 3
371        my $head = substr ($$bytes_s, 0, 3);
372        if ($head =~ /^\xFE\xFF/) {
373          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
374          ($e, $e_status) = $charset->get_perl_encoding
375              (allow_error_reporting => 1,
376               allow_fallback => 1);
377          $self->{confident} = 1;
378          last SNIFFING;
379        } elsif ($head =~ /^\xFF\xFE/) {
380          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
381          ($e, $e_status) = $charset->get_perl_encoding
382              (allow_error_reporting => 1,
383               allow_fallback => 1);
384          $self->{confident} = 1;
385          last SNIFFING;
386        } elsif ($head eq "\xEF\xBB\xBF") {
387          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
388          ($e, $e_status) = $charset->get_perl_encoding
389              (allow_error_reporting => 1,
390               allow_fallback => 1);
391          $self->{confident} = 1;
392          last SNIFFING;
393        }
394    
395        ## Step 4
396        ## TODO: <meta charset>
397    
398        ## Step 5
399        ## TODO: from history
400    
401        ## Step 6
402        require Whatpm::Charset::UniversalCharDet;
403        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
404            (substr ($$bytes_s, 0, 1024));
405        if (defined $charset_name) {
406          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
407    
408          ## ISSUE: Unsupported encoding is not ignored according to the spec.
409          ($e, $e_status) = $charset->get_perl_encoding
410              (allow_error_reporting => 1,
411               allow_fallback => 1);
412          if ($e) {
413            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
414                            value => $charset_name,
415                            level => $self->{info_level},
416                            line => 1, column => 1);
417            $self->{confident} = 0;
418            last SNIFFING;
419          }
420        }
421    
422        ## Step 7: default
423        ## TODO: Make this configurable.
424        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
425            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
426            ## detectable in the step 6.
427        ($e, $e_status) = $charset->get_perl_encoding (allow_error_reporting => 1,
428                                                       allow_fallback => 1);
429        !!!parse-error (type => 'sniffing:default', ## TODO: type name
430                        value => 'windows-1252',
431                        level => $self->{info_level},
432                        line => 1, column => 1);
433        $self->{confident} = 0;
434      } # SNIFFING
435    
436      $self->{input_encoding} = $charset->get_iana_name;
437      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
438        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
439                        value => $e->name,
440                        level => $self->{unsupported_level},
441                        line => 1, column => 1);
442      } elsif (not ($e_status &
443                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
444        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
445                        value => $self->{input_encoding},
446                        level => $self->{unsupported_level},
447                        line => 1, column => 1);
448      }
449      $s = \ $e->decode ($$bytes_s);
450    
451      $self->{change_encoding} = sub {
452        my $self = shift;
453        $charset_name = shift;
454        my $token = shift;
455    
456        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
457        ($e, $e_status) = $charset->get_perl_encoding
458            (allow_error_reporting => 1, allow_fallback => 1);
459        
460        if ($e) { # if supported
461          ## "Change the encoding" algorithm:
462    
463          ## Step 1    
464          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
465            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
466            ($e, $e_status) = $charset->get_perl_encoding;
467          }
468          $charset_name = $charset->get_iana_name;
469          
470          ## Step 2
471          if (defined $self->{input_encoding} and
472              $self->{input_encoding} eq $charset_name) {
473            !!!parse-error (type => 'charset label:matching', ## TODO: type
474                            value => $charset_name,
475                            level => $self->{info_level});
476            $self->{confident} = 1;
477            return;
478          }
479    
480          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
481              ':'.$charset_name, level => 'w', token => $token);
482          
483          ## Step 3
484          # if (can) {
485            ## change the encoding on the fly.
486            #$self->{confident} = 1;
487            #return;
488          # }
489          
490          ## Step 4
491          throw Whatpm::HTML::RestartParser ();
492        }
493      }; # $self->{change_encoding}
494    
495      my @args = @_; shift @args; # $s
496      my $return;
497      try {
498        $return = $self->parse_char_string ($s, @args);  
499      } catch Whatpm::HTML::RestartParser with {
500        ## NOTE: Invoked after {change_encoding}.
501    
502        $self->{input_encoding} = $charset->get_iana_name;
503        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
504          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
505                          value => $e->name,
506                          level => $self->{unsupported_level},
507                          line => 1, column => 1);
508        } elsif (not ($e_status &
509                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
510          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
511                          value => $self->{input_encoding},
512                          level => $self->{unsupported_level},
513                          line => 1, column => 1);
514        }
515        $s = \ $e->decode ($$bytes_s);
516        $self->{confident} = 1;
517        $return = $self->parse_char_string ($s, @args);
518      };
519      return $return;
520    } # parse_byte_string
521    
522    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
523    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
524    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
525    ## because the core part of our HTML parser expects a string of character,
526    ## not a string of bytes or code units or anything which might contain a BOM.
527    ## Therefore, any parser interface that accepts a string of bytes,
528    ## such as |parse_byte_string| in this module, must ensure that it does
529    ## strip the BOM and never strip any ZWNBSP.
530    
531    *parse_char_string = \&parse_string;
532    
533  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
534    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
535    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
536    $self->{document} = $_[1];    $self->{document} = $_[1];
537      @{$self->{document}->child_nodes} = ();
538    
539    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
540    
541      $self->{confident} = 1 unless exists $self->{confident};
542      $self->{document}->input_encoding ($self->{input_encoding})
543          if defined $self->{input_encoding};
544    
545    my $i = 0;    my $i = 0;
546    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
547    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
548    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
549      my $self = shift;      my $self = shift;
550    
551      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
552      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
553    
554        $self->{next_char} = -1 and return if $i >= length $$s;
555        $self->{next_char} = ord substr $$s, $i++, 1;
556    
557      $self->{next_input_character} = -1 and return if $i >= length $$s;      ($self->{line_prev}, $self->{column_prev})
558      $self->{next_input_character} = ord substr $$s, $i++, 1;          = ($self->{line}, $self->{column});
559      $column++;      $self->{column}++;
560            
561      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
562        $line++;        !!!cp ('j1');
563        $column = 0;        $self->{line}++;
564      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
565        } elsif ($self->{next_char} == 0x000D) { # CR
566          !!!cp ('j2');
567        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
568        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
569        $line++;        $self->{line}++;
570        $column = 0;        $self->{column} = 0;
571      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
572        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        !!!cp ('j3');
573      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
574        } elsif ($self->{next_char} == 0x0000) { # NULL
575          !!!cp ('j4');
576        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
577        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
578        } elsif ($self->{next_char} <= 0x0008 or
579                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
580                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
581                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
582                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
583                 {
584                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
585                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
586                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
587                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
588                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
589                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
590                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
591                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
592                  0x10FFFE => 1, 0x10FFFF => 1,
593                 }->{$self->{next_char}}) {
594          !!!cp ('j5');
595          !!!parse-error (type => 'control char', level => $self->{must_level});
596    ## TODO: error type documentation
597      }      }
598    };    };
599    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
600    $self->{next_input_character} = -1;    $self->{next_char} = -1;
601    
602    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
603      my (%opt) = @_;      my (%opt) = @_;
604      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
605        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
606        warn "Parse error ($opt{type}) at line $line column $column\n";
607    };    };
608    $self->{parse_error} = sub {    $self->{parse_error} = sub {
609      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
610    };    };
611    
612    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 614  sub parse_string ($$$;$) {
614    $self->_construct_tree;    $self->_construct_tree;
615    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
616    
617      delete $self->{parse_error}; # remove loop
618    
619    return $self->{document};    return $self->{document};
620  } # parse_string  } # parse_string
621    
622  sub new ($) {  sub new ($) {
623    my $class = shift;    my $class = shift;
624    my $self = bless {}, $class;    my $self = bless {
625    $self->{set_next_input_character} = sub {      must_level => 'm',
626      $self->{next_input_character} = -1;      should_level => 's',
627        good_level => 'w',
628        warn_level => 'w',
629        info_level => 'i',
630        unsupported_level => 'u',
631      }, $class;
632      $self->{set_next_char} = sub {
633        $self->{next_char} = -1;
634    };    };
635    $self->{parse_error} = sub {    $self->{parse_error} = sub {
636      #      #
637    };    };
638      $self->{change_encoding} = sub {
639        # if ($_[0] is a supported encoding) {
640        #   run "change the encoding" algorithm;
641        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
642        # }
643      };
644      $self->{application_cache_selection} = sub {
645        #
646      };
647    return $self;    return $self;
648  } # new  } # new
649    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 656  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
656  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
657  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
658    
659    sub DATA_STATE () { 0 }
660    sub ENTITY_DATA_STATE () { 1 }
661    sub TAG_OPEN_STATE () { 2 }
662    sub CLOSE_TAG_OPEN_STATE () { 3 }
663    sub TAG_NAME_STATE () { 4 }
664    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
665    sub ATTRIBUTE_NAME_STATE () { 6 }
666    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
667    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
668    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
669    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
670    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
671    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
672    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
673    sub COMMENT_START_STATE () { 14 }
674    sub COMMENT_START_DASH_STATE () { 15 }
675    sub COMMENT_STATE () { 16 }
676    sub COMMENT_END_STATE () { 17 }
677    sub COMMENT_END_DASH_STATE () { 18 }
678    sub BOGUS_COMMENT_STATE () { 19 }
679    sub DOCTYPE_STATE () { 20 }
680    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
681    sub DOCTYPE_NAME_STATE () { 22 }
682    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
683    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
684    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
685    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
686    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
687    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
688    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
689    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
690    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
691    sub BOGUS_DOCTYPE_STATE () { 32 }
692    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
693    sub SELF_CLOSING_START_TAG_STATE () { 34 }
694    sub CDATA_BLOCK_STATE () { 35 }
695    
696  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
697  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
698  sub START_TAG_TOKEN () { 3 }  sub START_TAG_TOKEN () { 3 }
# Line 174  sub TABLE_IMS ()      { 0b1000000 } Line 708  sub TABLE_IMS ()      { 0b1000000 }
708  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
709  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
710  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
711    sub SELECT_IMS ()     { 0b10000000000 }
712    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
713        ## NOTE: "in foreign content" insertion mode is special; it is combined
714        ## with the secondary insertion mode.  In this parser, they are stored
715        ## together in the bit-or'ed form.
716    
717    ## NOTE: "initial" and "before html" insertion modes have no constants.
718    
719    ## NOTE: "after after body" insertion mode.
720  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
721    
722    ## NOTE: "after after frameset" insertion mode.
723  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
724    
725  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
726  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
727  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 190  sub IN_TABLE_IM () { TABLE_IMS } Line 735  sub IN_TABLE_IM () { TABLE_IMS }
735  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
736  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
737  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
738  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
739    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
740  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
741    
742  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
743    
744  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
745    my $self = shift;    my $self = shift;
746    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
747    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
748    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
749    undef $self->{current_attribute};    undef $self->{current_attribute};
750    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
751    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
752      delete $self->{self_closing};
753    $self->{char} = [];    $self->{char} = [];
754    # $self->{next_input_character}    # $self->{next_char}
755    !!!next-input-character;    !!!next-input-character;
756    $self->{token} = [];    $self->{token} = [];
757    # $self->{escape}    # $self->{escape}
# Line 217  sub _initialize_tokenizer ($) { Line 764  sub _initialize_tokenizer ($) {
764  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
765  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
766  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
767  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
768  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
769    ##        ->{name}
770    ##        ->{value}
771    ##        ->{has_reference} == 1 or 0
772  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
773    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
774    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
775    ##     while the token is pushed back to the stack.
776    
777    ## ISSUE: "When a DOCTYPE token is created, its
778    ## <i>self-closing flag</i> must be unset (its other state is that it
779    ## be set), and its attributes list must be empty.": Wrong subject?
780    
781  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
782    
# Line 229  sub _initialize_tokenizer ($) { Line 786  sub _initialize_tokenizer ($) {
786  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
787  ## and removed from the list.  ## and removed from the list.
788    
789    ## NOTE: HTML5 "Writing HTML documents" section, applied to
790    ## documents and not to user agents and conformance checkers,
791    ## contains some requirements that are not detected by the
792    ## parsing algorithm:
793    ## - Some requirements on character encoding declarations. ## TODO
794    ## - "Elements MUST NOT contain content that their content model disallows."
795    ##   ... Some are parse error, some are not (will be reported by c.c.).
796    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
797    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
798    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
799    
800    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
801    ## be detected by the HTML5 parsing algorithm:
802    ## - Text,
803    
804  sub _get_next_token ($) {  sub _get_next_token ($) {
805    my $self = shift;    my $self = shift;
806    
807      if ($self->{self_closing}) {
808        !!!parse-error (type => 'nestc', token => $self->{current_token});
809        ## NOTE: The |self_closing| flag is only set by start tag token.
810        ## In addition, when a start tag token is emitted, it is always set to
811        ## |current_token|.
812        delete $self->{self_closing};
813      }
814    
815    if (@{$self->{token}}) {    if (@{$self->{token}}) {
816        $self->{self_closing} = $self->{token}->[0]->{self_closing};
817      return shift @{$self->{token}};      return shift @{$self->{token}};
818    }    }
819    
820    A: {    A: {
821      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
822        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
823          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
824            $self->{state} = 'entity data';              not $self->{escape}) {
825              !!!cp (1);
826              $self->{state} = ENTITY_DATA_STATE;
827            !!!next-input-character;            !!!next-input-character;
828            redo A;            redo A;
829          } else {          } else {
830              !!!cp (2);
831            #            #
832          }          }
833        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
834          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
835            unless ($self->{escape}) {            unless ($self->{escape}) {
836              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
837                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
838                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
839                  !!!cp (3);
840                $self->{escape} = 1;                $self->{escape} = 1;
841                } else {
842                  !!!cp (4);
843              }              }
844              } else {
845                !!!cp (5);
846            }            }
847          }          }
848                    
849          #          #
850        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
851          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
852              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
853               not $self->{escape})) {               not $self->{escape})) {
854            $self->{state} = 'tag open';            !!!cp (6);
855              $self->{state} = TAG_OPEN_STATE;
856            !!!next-input-character;            !!!next-input-character;
857            redo A;            redo A;
858          } else {          } else {
859              !!!cp (7);
860            #            #
861          }          }
862        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
863          if ($self->{escape} and          if ($self->{escape} and
864              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
865            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
866                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
867                !!!cp (8);
868              delete $self->{escape};              delete $self->{escape};
869              } else {
870                !!!cp (9);
871            }            }
872            } else {
873              !!!cp (10);
874          }          }
875                    
876          #          #
877        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
878          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
879            !!!emit ({type => END_OF_FILE_TOKEN,
880                      line => $self->{line}, column => $self->{column}});
881          last A; ## TODO: ok?          last A; ## TODO: ok?
882          } else {
883            !!!cp (12);
884        }        }
885        # Anything else        # Anything else
886        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
887                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
888                       line => $self->{line}, column => $self->{column},
889                      };
890        ## Stay in the data state        ## Stay in the data state
891        !!!next-input-character;        !!!next-input-character;
892    
893        !!!emit ($token);        !!!emit ($token);
894    
895        redo A;        redo A;
896      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
897        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
898    
899          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
900                
901        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
902    
903        $self->{state} = 'data';        $self->{state} = DATA_STATE;
904        # next-input-character is already done        # next-input-character is already done
905    
906        unless (defined $token) {        unless (defined $token) {
907          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!cp (13);
908            !!!emit ({type => CHARACTER_TOKEN, data => '&',
909                      line => $l, column => $c,
910                     });
911        } else {        } else {
912            !!!cp (14);
913          !!!emit ($token);          !!!emit ($token);
914        }        }
915    
916        redo A;        redo A;
917      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
918        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
919          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
920              !!!cp (15);
921            !!!next-input-character;            !!!next-input-character;
922            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
923            redo A;            redo A;
924          } else {          } else {
925              !!!cp (16);
926            ## reconsume            ## reconsume
927            $self->{state} = 'data';            $self->{state} = DATA_STATE;
928    
929            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
930                        line => $self->{line_prev},
931                        column => $self->{column_prev},
932                       });
933    
934            redo A;            redo A;
935          }          }
936        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
937          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
938            $self->{state} = 'markup declaration open';            !!!cp (17);
939              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
940            !!!next-input-character;            !!!next-input-character;
941            redo A;            redo A;
942          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
943            $self->{state} = 'close tag open';            !!!cp (18);
944              $self->{state} = CLOSE_TAG_OPEN_STATE;
945            !!!next-input-character;            !!!next-input-character;
946            redo A;            redo A;
947          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
948                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
949              !!!cp (19);
950            $self->{current_token}            $self->{current_token}
951              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
952                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
953            $self->{state} = 'tag name';                 line => $self->{line_prev},
954                   column => $self->{column_prev}};
955              $self->{state} = TAG_NAME_STATE;
956            !!!next-input-character;            !!!next-input-character;
957            redo A;            redo A;
958          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
959                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
960              !!!cp (20);
961            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
962                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
963            $self->{state} = 'tag name';                                      line => $self->{line_prev},
964                                        column => $self->{column_prev}};
965              $self->{state} = TAG_NAME_STATE;
966            !!!next-input-character;            !!!next-input-character;
967            redo A;            redo A;
968          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
969            !!!parse-error (type => 'empty start tag');            !!!cp (21);
970            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
971                              line => $self->{line_prev},
972                              column => $self->{column_prev});
973              $self->{state} = DATA_STATE;
974            !!!next-input-character;            !!!next-input-character;
975    
976            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
977                        line => $self->{line_prev},
978                        column => $self->{column_prev},
979                       });
980    
981            redo A;            redo A;
982          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
983            !!!parse-error (type => 'pio');            !!!cp (22);
984            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
985            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
986                              column => $self->{column_prev});
987              $self->{state} = BOGUS_COMMENT_STATE;
988              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
989                                        line => $self->{line_prev},
990                                        column => $self->{column_prev},
991                                       };
992              ## $self->{next_char} is intentionally left as is
993            redo A;            redo A;
994          } else {          } else {
995              !!!cp (23);
996            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
997            $self->{state} = 'data';            $self->{state} = DATA_STATE;
998            ## reconsume            ## reconsume
999    
1000            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1001                        line => $self->{line_prev},
1002                        column => $self->{column_prev},
1003                       });
1004    
1005            redo A;            redo A;
1006          }          }
1007        } else {        } else {
1008          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1009        }        }
1010      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1011          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1012        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1013          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1014    
1015            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1016            my @next_char;            my @next_char;
1017            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
1018              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1019              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1020              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1021              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1022                  !!!cp (24);
1023                !!!next-input-character;                !!!next-input-character;
1024                next TAGNAME;                next TAGNAME;
1025              } else {              } else {
1026                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
1027                  $self->{next_char} = shift @next_char; # reconsume
1028                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1029                $self->{state} = 'data';                $self->{state} = DATA_STATE;
1030    
1031                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1032                            line => $l, column => $c,
1033                           });
1034        
1035                redo A;                redo A;
1036              }              }
1037            }            }
1038            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1039                
1040            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
1041                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
1042                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
1043                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
1044                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
1045                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
1046                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
1047                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
1048              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1049                $self->{next_char} = shift @next_char; # reconsume
1050              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1051              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1052              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1053                          line => $l, column => $c,
1054                         });
1055              redo A;              redo A;
1056            } else {            } else {
1057              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1058                $self->{next_char} = shift @next_char;
1059              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1060              # and consume...              # and consume...
1061            }            }
1062          } else {          } else {
1063            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1064              !!!cp (28);
1065            # next-input-character is already done            # next-input-character is already done
1066            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1067            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1068                        line => $l, column => $c,
1069                       });
1070            redo A;            redo A;
1071          }          }
1072        }        }
1073                
1074        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1075            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1076          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
1077                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1078          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1079                   tag_name => chr ($self->{next_char} + 0x0020),
1080                   line => $l, column => $c};
1081            $self->{state} = TAG_NAME_STATE;
1082          !!!next-input-character;          !!!next-input-character;
1083          redo A;          redo A;
1084        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
1085                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1086            !!!cp (30);
1087          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1088                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
1089          $self->{state} = 'tag name';                                    line => $l, column => $c};
1090            $self->{state} = TAG_NAME_STATE;
1091          !!!next-input-character;          !!!next-input-character;
1092          redo A;          redo A;
1093        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1094          !!!parse-error (type => 'empty end tag');          !!!cp (31);
1095          $self->{state} = 'data';          !!!parse-error (type => 'empty end tag',
1096                            line => $self->{line_prev}, ## "<" in "</>"
1097                            column => $self->{column_prev} - 1);
1098            $self->{state} = DATA_STATE;
1099          !!!next-input-character;          !!!next-input-character;
1100          redo A;          redo A;
1101        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1102            !!!cp (32);
1103          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1104          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1105          # reconsume          # reconsume
1106    
1107          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1108                      line => $l, column => $c,
1109                     });
1110    
1111          redo A;          redo A;
1112        } else {        } else {
1113            !!!cp (33);
1114          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1115          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1116          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1117          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1118        }                                    column => $self->{column_prev} - 1,
1119      } elsif ($self->{state} eq 'tag name') {                                   };
1120        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1121            $self->{next_input_character} == 0x000A or # LF          redo A;
1122            $self->{next_input_character} == 0x000B or # VT        }
1123            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1124            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1125          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1126              $self->{next_char} == 0x000B or # VT
1127              $self->{next_char} == 0x000C or # FF
1128              $self->{next_char} == 0x0020) { # SP
1129            !!!cp (34);
1130            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1131          !!!next-input-character;          !!!next-input-character;
1132          redo A;          redo A;
1133        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1134          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1135            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
1136            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1137          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1138            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1139            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1140              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1141            }            #  !!! cp (36);
1142              #  !!! parse-error (type => 'end tag attribute');
1143              #} else {
1144                !!!cp (37);
1145              #}
1146          } else {          } else {
1147            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1148          }          }
1149          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1150          !!!next-input-character;          !!!next-input-character;
1151    
1152          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1153    
1154          redo A;          redo A;
1155        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1156                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1157          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1158            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1159            # start tag or end tag            # start tag or end tag
1160          ## Stay in this state          ## Stay in this state
1161          !!!next-input-character;          !!!next-input-character;
1162          redo A;          redo A;
1163        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1164          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1165          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1166            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1167            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1168          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1169            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1170            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1171              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1172            }            #  !!! cp (40);
1173              #  !!! parse-error (type => 'end tag attribute');
1174              #} else {
1175                !!!cp (41);
1176              #}
1177          } else {          } else {
1178            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1179          }          }
1180          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1181          # reconsume          # reconsume
1182    
1183          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1184    
1185          redo A;          redo A;
1186        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1187            !!!cp (42);
1188            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1189          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
1190          redo A;          redo A;
1191        } else {        } else {
1192          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1193            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1194            # start tag or end tag            # start tag or end tag
1195          ## Stay in the state          ## Stay in the state
1196          !!!next-input-character;          !!!next-input-character;
1197          redo A;          redo A;
1198        }        }
1199      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1200        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1201            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1202            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1203            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1204            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1205            !!!cp (45);
1206          ## Stay in the state          ## Stay in the state
1207          !!!next-input-character;          !!!next-input-character;
1208          redo A;          redo A;
1209        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1210          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1211            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1212            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1213          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1214            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1215            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1216                !!!cp (47);
1217              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1218              } else {
1219                !!!cp (48);
1220            }            }
1221          } else {          } else {
1222            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1223          }          }
1224          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1225          !!!next-input-character;          !!!next-input-character;
1226    
1227          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1228    
1229          redo A;          redo A;
1230        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1231                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1232          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1233                                value => ''};          $self->{current_attribute}
1234          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1235                   value => '',
1236                   line => $self->{line}, column => $self->{column}};
1237            $self->{state} = ATTRIBUTE_NAME_STATE;
1238            !!!next-input-character;
1239            redo A;
1240          } elsif ($self->{next_char} == 0x002F) { # /
1241            !!!cp (50);
1242            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1243          !!!next-input-character;          !!!next-input-character;
1244          redo A;          redo A;
1245        } 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} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1246          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1247          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1248            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1249            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1250          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1251            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1252            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1253                !!!cp (53);
1254              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1255              } else {
1256                !!!cp (54);
1257            }            }
1258          } else {          } else {
1259            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1260          }          }
1261          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1262          # reconsume          # reconsume
1263    
1264          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1265    
1266          redo A;          redo A;
1267        } else {        } else {
1268          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1269                                value => ''};               0x0022 => 1, # "
1270          $self->{state} = 'attribute name';               0x0027 => 1, # '
1271                 0x003D => 1, # =
1272                }->{$self->{next_char}}) {
1273              !!!cp (55);
1274              !!!parse-error (type => 'bad attribute name');
1275            } else {
1276              !!!cp (56);
1277            }
1278            $self->{current_attribute}
1279                = {name => chr ($self->{next_char}),
1280                   value => '',
1281                   line => $self->{line}, column => $self->{column}};
1282            $self->{state} = ATTRIBUTE_NAME_STATE;
1283          !!!next-input-character;          !!!next-input-character;
1284          redo A;          redo A;
1285        }        }
1286      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1287        my $before_leave = sub {        my $before_leave = sub {
1288          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1289              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1290            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1291              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1292            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1293          } else {          } else {
1294              !!!cp (58);
1295            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1296              = $self->{current_attribute};              = $self->{current_attribute};
1297          }          }
1298        }; # $before_leave        }; # $before_leave
1299    
1300        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1301            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1302            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1303            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1304            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1305            !!!cp (59);
1306          $before_leave->();          $before_leave->();
1307          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1308          !!!next-input-character;          !!!next-input-character;
1309          redo A;          redo A;
1310        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1311            !!!cp (60);
1312          $before_leave->();          $before_leave->();
1313          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1314          !!!next-input-character;          !!!next-input-character;
1315          redo A;          redo A;
1316        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1317          $before_leave->();          $before_leave->();
1318          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1319            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
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} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1322              !!!cp (62);
1323            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1324            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1325              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 642  sub _get_next_token ($) { Line 1327  sub _get_next_token ($) {
1327          } else {          } else {
1328            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1329          }          }
1330          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1331          !!!next-input-character;          !!!next-input-character;
1332    
1333          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1334    
1335          redo A;          redo A;
1336        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1337                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1338          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1339            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1340          ## Stay in the state          ## Stay in the state
1341          !!!next-input-character;          !!!next-input-character;
1342          redo A;          redo A;
1343        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1344            !!!cp (64);
1345          $before_leave->();          $before_leave->();
1346            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1347          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
1348          redo A;          redo A;
1349        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1350          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1351          $before_leave->();          $before_leave->();
1352          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1353            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1354            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1355          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1356            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1357            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1358                !!!cp (67);
1359              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1360              } else {
1361                ## NOTE: This state should never be reached.
1362                !!!cp (68);
1363            }            }
1364          } else {          } else {
1365            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1366          }          }
1367          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1368          # reconsume          # reconsume
1369    
1370          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1371    
1372          redo A;          redo A;
1373        } else {        } else {
1374          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1375                $self->{next_char} == 0x0027) { # '
1376              !!!cp (69);
1377              !!!parse-error (type => 'bad attribute name');
1378            } else {
1379              !!!cp (70);
1380            }
1381            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1382          ## Stay in the state          ## Stay in the state
1383          !!!next-input-character;          !!!next-input-character;
1384          redo A;          redo A;
1385        }        }
1386      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1387        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1388            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1389            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1390            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1391            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1392            !!!cp (71);
1393          ## Stay in the state          ## Stay in the state
1394          !!!next-input-character;          !!!next-input-character;
1395          redo A;          redo A;
1396        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1397          $self->{state} = 'before attribute value';          !!!cp (72);
1398            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1399          !!!next-input-character;          !!!next-input-character;
1400          redo A;          redo A;
1401        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1402          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1403            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1404            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1405          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1406            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1407            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1408                !!!cp (74);
1409              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1410              } else {
1411                ## NOTE: This state should never be reached.
1412                !!!cp (75);
1413            }            }
1414          } else {          } else {
1415            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1416          }          }
1417          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1418          !!!next-input-character;          !!!next-input-character;
1419    
1420          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1421    
1422          redo A;          redo A;
1423        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1424                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1425          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1426                                value => ''};          $self->{current_attribute}
1427          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1428                   value => '',
1429                   line => $self->{line}, column => $self->{column}};
1430            $self->{state} = ATTRIBUTE_NAME_STATE;
1431            !!!next-input-character;
1432            redo A;
1433          } elsif ($self->{next_char} == 0x002F) { # /
1434            !!!cp (77);
1435            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1436          !!!next-input-character;          !!!next-input-character;
1437          redo A;          redo A;
1438        } 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} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1439          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1440          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1441            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1442            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1443          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1444            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1445            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1446                !!!cp (80);
1447              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1448              } else {
1449                ## NOTE: This state should never be reached.
1450                !!!cp (81);
1451            }            }
1452          } else {          } else {
1453            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1454          }          }
1455          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1456          # reconsume          # reconsume
1457    
1458          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1459    
1460          redo A;          redo A;
1461        } else {        } else {
1462          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1463                                value => ''};          $self->{current_attribute}
1464          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char}),
1465                   value => '',
1466                   line => $self->{line}, column => $self->{column}};
1467            $self->{state} = ATTRIBUTE_NAME_STATE;
1468          !!!next-input-character;          !!!next-input-character;
1469          redo A;                  redo A;        
1470        }        }
1471      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1472        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1473            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1474            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1475            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1476            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1477            !!!cp (83);
1478          ## Stay in the state          ## Stay in the state
1479          !!!next-input-character;          !!!next-input-character;
1480          redo A;          redo A;
1481        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1482          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1483            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1484          !!!next-input-character;          !!!next-input-character;
1485          redo A;          redo A;
1486        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1487          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1488            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1489          ## reconsume          ## reconsume
1490          redo A;          redo A;
1491        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1492          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1493            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1494          !!!next-input-character;          !!!next-input-character;
1495          redo A;          redo A;
1496        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1497          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1498            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1499            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1500          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1501            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1502            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1503                !!!cp (88);
1504              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1505              } else {
1506                ## NOTE: This state should never be reached.
1507                !!!cp (89);
1508            }            }
1509          } else {          } else {
1510            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1511          }          }
1512          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1513          !!!next-input-character;          !!!next-input-character;
1514    
1515          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1516    
1517          redo A;          redo A;
1518        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1519          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1520          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1521            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1522            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1523          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1524            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1525            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1526                !!!cp (91);
1527              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1528              } else {
1529                ## NOTE: This state should never be reached.
1530                !!!cp (92);
1531            }            }
1532          } else {          } else {
1533            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1534          }          }
1535          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1536          ## reconsume          ## reconsume
1537    
1538          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1539    
1540          redo A;          redo A;
1541        } else {        } else {
1542          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1543          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1544              !!!parse-error (type => 'bad attribute value');
1545            } else {
1546              !!!cp (94);
1547            }
1548            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1549            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1550          !!!next-input-character;          !!!next-input-character;
1551          redo A;          redo A;
1552        }        }
1553      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1554        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1555          $self->{state} = 'before attribute name';          !!!cp (95);
1556            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1557          !!!next-input-character;          !!!next-input-character;
1558          redo A;          redo A;
1559        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1560          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1561          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1562            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1563          !!!next-input-character;          !!!next-input-character;
1564          redo A;          redo A;
1565        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1566          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1567          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1568            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1569            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1570          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1571            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1572            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1573                !!!cp (98);
1574              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1575              } else {
1576                ## NOTE: This state should never be reached.
1577                !!!cp (99);
1578            }            }
1579          } else {          } else {
1580            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1581          }          }
1582          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1583          ## reconsume          ## reconsume
1584    
1585          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1586    
1587          redo A;          redo A;
1588        } else {        } else {
1589          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1590            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1591          ## Stay in the state          ## Stay in the state
1592          !!!next-input-character;          !!!next-input-character;
1593          redo A;          redo A;
1594        }        }
1595      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1596        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1597          $self->{state} = 'before attribute name';          !!!cp (101);
1598            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1599          !!!next-input-character;          !!!next-input-character;
1600          redo A;          redo A;
1601        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1602          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1603          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1604            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1605          !!!next-input-character;          !!!next-input-character;
1606          redo A;          redo A;
1607        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1608          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1609          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1610            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1611            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1612          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1613            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1614            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1615                !!!cp (104);
1616              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1617              } else {
1618                ## NOTE: This state should never be reached.
1619                !!!cp (105);
1620            }            }
1621          } else {          } else {
1622            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1623          }          }
1624          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1625          ## reconsume          ## reconsume
1626    
1627          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1628    
1629          redo A;          redo A;
1630        } else {        } else {
1631          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1632            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1633          ## Stay in the state          ## Stay in the state
1634          !!!next-input-character;          !!!next-input-character;
1635          redo A;          redo A;
1636        }        }
1637      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1638        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1639            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1640            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1641            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1642            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1643          $self->{state} = 'before attribute name';          !!!cp (107);
1644          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1645          redo A;          !!!next-input-character;
1646        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1647          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1648          $self->{state} = 'entity in attribute value';          !!!cp (108);
1649            $self->{last_attribute_value_state} = $self->{state};
1650            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1651          !!!next-input-character;          !!!next-input-character;
1652          redo A;          redo A;
1653        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1654          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1655            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1656            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1657          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1658            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1659            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1660                !!!cp (110);
1661              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1662              } else {
1663                ## NOTE: This state should never be reached.
1664                !!!cp (111);
1665            }            }
1666          } else {          } else {
1667            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1668          }          }
1669          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1670          !!!next-input-character;          !!!next-input-character;
1671    
1672          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1673    
1674          redo A;          redo A;
1675        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1676          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1677          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1678            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1679            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1680          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1681            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1682            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1683                !!!cp (113);
1684              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1685              } else {
1686                ## NOTE: This state should never be reached.
1687                !!!cp (114);
1688            }            }
1689          } else {          } else {
1690            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1691          }          }
1692          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1693          ## reconsume          ## reconsume
1694    
1695          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1696    
1697          redo A;          redo A;
1698        } else {        } else {
1699          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1700                 0x0022 => 1, # "
1701                 0x0027 => 1, # '
1702                 0x003D => 1, # =
1703                }->{$self->{next_char}}) {
1704              !!!cp (115);
1705              !!!parse-error (type => 'bad attribute value');
1706            } else {
1707              !!!cp (116);
1708            }
1709            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1710          ## Stay in the state          ## Stay in the state
1711          !!!next-input-character;          !!!next-input-character;
1712          redo A;          redo A;
1713        }        }
1714      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1715        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1716              (1,
1717               $self->{last_attribute_value_state}
1718                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1719               $self->{last_attribute_value_state}
1720                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1721               -1);
1722    
1723        unless (defined $token) {        unless (defined $token) {
1724            !!!cp (117);
1725          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1726        } else {        } else {
1727            !!!cp (118);
1728          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1729            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1730          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1731        }        }
1732    
1733        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1734        # next-input-character is already done        # next-input-character is already done
1735        redo A;        redo A;
1736      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1737          if ($self->{next_char} == 0x0009 or # HT
1738              $self->{next_char} == 0x000A or # LF
1739              $self->{next_char} == 0x000B or # VT
1740              $self->{next_char} == 0x000C or # FF
1741              $self->{next_char} == 0x0020) { # SP
1742            !!!cp (118);
1743            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1744            !!!next-input-character;
1745            redo A;
1746          } elsif ($self->{next_char} == 0x003E) { # >
1747            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1748              !!!cp (119);
1749              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1750            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1751              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1752              if ($self->{current_token}->{attributes}) {
1753                !!!cp (120);
1754                !!!parse-error (type => 'end tag attribute');
1755              } else {
1756                ## NOTE: This state should never be reached.
1757                !!!cp (121);
1758              }
1759            } else {
1760              die "$0: $self->{current_token}->{type}: Unknown token type";
1761            }
1762            $self->{state} = DATA_STATE;
1763            !!!next-input-character;
1764    
1765            !!!emit ($self->{current_token}); # start tag or end tag
1766    
1767            redo A;
1768          } elsif ($self->{next_char} == 0x002F) { # /
1769            !!!cp (122);
1770            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1771            !!!next-input-character;
1772            redo A;
1773          } else {
1774            !!!cp ('124.1');
1775            !!!parse-error (type => 'no space between attributes');
1776            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1777            ## reconsume
1778            redo A;
1779          }
1780        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1781          if ($self->{next_char} == 0x003E) { # >
1782            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1783              !!!cp ('124.2');
1784              !!!parse-error (type => 'nestc', token => $self->{current_token});
1785              ## TODO: Different type than slash in start tag
1786              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1787              if ($self->{current_token}->{attributes}) {
1788                !!!cp ('124.4');
1789                !!!parse-error (type => 'end tag attribute');
1790              } else {
1791                !!!cp ('124.5');
1792              }
1793              ## TODO: Test |<title></title/>|
1794            } else {
1795              !!!cp ('124.3');
1796              $self->{self_closing} = 1;
1797            }
1798    
1799            $self->{state} = DATA_STATE;
1800            !!!next-input-character;
1801    
1802            !!!emit ($self->{current_token}); # start tag or end tag
1803    
1804            redo A;
1805          } else {
1806            !!!cp ('124.4');
1807            !!!parse-error (type => 'nestc');
1808            ## TODO: This error type is wrong.
1809            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1810            ## Reconsume.
1811            redo A;
1812          }
1813        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1814        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1815                
1816        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1817          #my $token = {type => COMMENT_TOKEN, data => ''};
1818    
1819        BC: {        BC: {
1820          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1821            $self->{state} = 'data';            !!!cp (124);
1822              $self->{state} = DATA_STATE;
1823            !!!next-input-character;            !!!next-input-character;
1824    
1825            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1826    
1827            redo A;            redo A;
1828          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1829            $self->{state} = 'data';            !!!cp (125);
1830              $self->{state} = DATA_STATE;
1831            ## reconsume            ## reconsume
1832    
1833            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1834    
1835            redo A;            redo A;
1836          } else {          } else {
1837            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1838              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1839            !!!next-input-character;            !!!next-input-character;
1840            redo BC;            redo BC;
1841          }          }
1842        } # BC        } # BC
1843      } elsif ($self->{state} eq 'markup declaration open') {  
1844          die "$0: _get_next_token: unexpected case [BC]";
1845        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1846        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1847    
1848          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1849    
1850        my @next_char;        my @next_char;
1851        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1852                
1853        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1854          !!!next-input-character;          !!!next-input-character;
1855          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1856          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1857            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            !!!cp (127);
1858            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1859                                        line => $l, column => $c,
1860                                       };
1861              $self->{state} = COMMENT_START_STATE;
1862            !!!next-input-character;            !!!next-input-character;
1863            redo A;            redo A;
1864            } else {
1865              !!!cp (128);
1866            }
1867          } elsif ($self->{next_char} == 0x0044 or # D
1868                   $self->{next_char} == 0x0064) { # d
1869            !!!next-input-character;
1870            push @next_char, $self->{next_char};
1871            if ($self->{next_char} == 0x004F or # O
1872                $self->{next_char} == 0x006F) { # o
1873              !!!next-input-character;
1874              push @next_char, $self->{next_char};
1875              if ($self->{next_char} == 0x0043 or # C
1876                  $self->{next_char} == 0x0063) { # c
1877                !!!next-input-character;
1878                push @next_char, $self->{next_char};
1879                if ($self->{next_char} == 0x0054 or # T
1880                    $self->{next_char} == 0x0074) { # t
1881                  !!!next-input-character;
1882                  push @next_char, $self->{next_char};
1883                  if ($self->{next_char} == 0x0059 or # Y
1884                      $self->{next_char} == 0x0079) { # y
1885                    !!!next-input-character;
1886                    push @next_char, $self->{next_char};
1887                    if ($self->{next_char} == 0x0050 or # P
1888                        $self->{next_char} == 0x0070) { # p
1889                      !!!next-input-character;
1890                      push @next_char, $self->{next_char};
1891                      if ($self->{next_char} == 0x0045 or # E
1892                          $self->{next_char} == 0x0065) { # e
1893                        !!!cp (129);
1894                        ## TODO: What a stupid code this is!
1895                        $self->{state} = DOCTYPE_STATE;
1896                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1897                                                  quirks => 1,
1898                                                  line => $l, column => $c,
1899                                                 };
1900                        !!!next-input-character;
1901                        redo A;
1902                      } else {
1903                        !!!cp (130);
1904                      }
1905                    } else {
1906                      !!!cp (131);
1907                    }
1908                  } else {
1909                    !!!cp (132);
1910                  }
1911                } else {
1912                  !!!cp (133);
1913                }
1914              } else {
1915                !!!cp (134);
1916              }
1917            } else {
1918              !!!cp (135);
1919          }          }
1920        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1921                 $self->{next_input_character} == 0x0064) { # d                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1922                   $self->{next_char} == 0x005B) { # [
1923          !!!next-input-character;          !!!next-input-character;
1924          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1925          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x0043) { # C
             $self->{next_input_character} == 0x006F) { # o  
1926            !!!next-input-character;            !!!next-input-character;
1927            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1928            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0044) { # D
               $self->{next_input_character} == 0x0063) { # c  
1929              !!!next-input-character;              !!!next-input-character;
1930              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1931              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0041) { # A
                 $self->{next_input_character} == 0x0074) { # t  
1932                !!!next-input-character;                !!!next-input-character;
1933                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1934                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0054) { # T
                   $self->{next_input_character} == 0x0079) { # y  
1935                  !!!next-input-character;                  !!!next-input-character;
1936                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1937                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0041) { # A
                     $self->{next_input_character} == 0x0070) { # p  
1938                    !!!next-input-character;                    !!!next-input-character;
1939                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1940                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x005B) { # [
1941                        $self->{next_input_character} == 0x0065) { # e                      !!!cp (135.1);
1942                      ## ISSUE: What a stupid code this is!                      $self->{state} = CDATA_BLOCK_STATE;
                     $self->{state} = 'DOCTYPE';  
1943                      !!!next-input-character;                      !!!next-input-character;
1944                      redo A;                      redo A;
1945                      } else {
1946                        !!!cp (135.2);
1947                    }                    }
1948                    } else {
1949                      !!!cp (135.3);
1950                  }                  }
1951                  } else {
1952                    !!!cp (135.4);                
1953                }                }
1954                } else {
1955                  !!!cp (135.5);
1956              }              }
1957              } else {
1958                !!!cp (135.6);
1959            }            }
1960            } else {
1961              !!!cp (135.7);
1962          }          }
1963          } else {
1964            !!!cp (136);
1965        }        }
1966    
1967        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1968        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1969        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1970        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1971          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1972                                    line => $l, column => $c,
1973                                   };
1974        redo A;        redo A;
1975                
1976        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1977        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
1978      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1979        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1980          $self->{state} = 'comment start dash';          !!!cp (137);
1981            $self->{state} = COMMENT_START_DASH_STATE;
1982          !!!next-input-character;          !!!next-input-character;
1983          redo A;          redo A;
1984        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1985            !!!cp (138);
1986          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1987          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1988          !!!next-input-character;          !!!next-input-character;
1989    
1990          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1991    
1992          redo A;          redo A;
1993        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1994            !!!cp (139);
1995          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1996          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1997          ## reconsume          ## reconsume
1998    
1999          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2000    
2001          redo A;          redo A;
2002        } else {        } else {
2003            !!!cp (140);
2004          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2005              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2006          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2007          !!!next-input-character;          !!!next-input-character;
2008          redo A;          redo A;
2009        }        }
2010      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2011        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2012          $self->{state} = 'comment end';          !!!cp (141);
2013            $self->{state} = COMMENT_END_STATE;
2014          !!!next-input-character;          !!!next-input-character;
2015          redo A;          redo A;
2016        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2017            !!!cp (142);
2018          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2019          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2020          !!!next-input-character;          !!!next-input-character;
2021    
2022          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2023    
2024          redo A;          redo A;
2025        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2026            !!!cp (143);
2027          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2028          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2029          ## reconsume          ## reconsume
2030    
2031          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2032    
2033          redo A;          redo A;
2034        } else {        } else {
2035            !!!cp (144);
2036          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2037              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2038          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2039          !!!next-input-character;          !!!next-input-character;
2040          redo A;          redo A;
2041        }        }
2042      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
2043        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2044          $self->{state} = 'comment end dash';          !!!cp (145);
2045            $self->{state} = COMMENT_END_DASH_STATE;
2046          !!!next-input-character;          !!!next-input-character;
2047          redo A;          redo A;
2048        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2049            !!!cp (146);
2050          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2051          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2052          ## reconsume          ## reconsume
2053    
2054          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2055    
2056          redo A;          redo A;
2057        } else {        } else {
2058          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2059            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2060          ## Stay in the state          ## Stay in the state
2061          !!!next-input-character;          !!!next-input-character;
2062          redo A;          redo A;
2063        }        }
2064      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2065        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2066          $self->{state} = 'comment end';          !!!cp (148);
2067            $self->{state} = COMMENT_END_STATE;
2068          !!!next-input-character;          !!!next-input-character;
2069          redo A;          redo A;
2070        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2071            !!!cp (149);
2072          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2073          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2074          ## reconsume          ## reconsume
2075    
2076          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2077    
2078          redo A;          redo A;
2079        } else {        } else {
2080          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2081          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2082            $self->{state} = COMMENT_STATE;
2083          !!!next-input-character;          !!!next-input-character;
2084          redo A;          redo A;
2085        }        }
2086      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2087        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2088          $self->{state} = 'data';          !!!cp (151);
2089            $self->{state} = DATA_STATE;
2090          !!!next-input-character;          !!!next-input-character;
2091    
2092          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2093    
2094          redo A;          redo A;
2095        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2096          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2097            !!!parse-error (type => 'dash in comment',
2098                            line => $self->{line_prev},
2099                            column => $self->{column_prev});
2100          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2101          ## Stay in the state          ## Stay in the state
2102          !!!next-input-character;          !!!next-input-character;
2103          redo A;          redo A;
2104        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2105            !!!cp (153);
2106          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2107          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2108          ## reconsume          ## reconsume
2109    
2110          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2111    
2112          redo A;          redo A;
2113        } else {        } else {
2114          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2115          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2116          $self->{state} = 'comment';                          line => $self->{line_prev},
2117                            column => $self->{column_prev});
2118            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2119            $self->{state} = COMMENT_STATE;
2120          !!!next-input-character;          !!!next-input-character;
2121          redo A;          redo A;
2122        }        }
2123      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2124        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2125            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2126            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2127            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2128            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2129          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2130            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2131          !!!next-input-character;          !!!next-input-character;
2132          redo A;          redo A;
2133        } else {        } else {
2134            !!!cp (156);
2135          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2136          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2137          ## reconsume          ## reconsume
2138          redo A;          redo A;
2139        }        }
2140      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2141        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2142            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2143            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2144            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2145            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2146            !!!cp (157);
2147          ## Stay in the state          ## Stay in the state
2148          !!!next-input-character;          !!!next-input-character;
2149          redo A;          redo A;
2150        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2151            !!!cp (158);
2152          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2153          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2154          !!!next-input-character;          !!!next-input-character;
2155    
2156          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2157    
2158          redo A;          redo A;
2159        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2160            !!!cp (159);
2161          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2162          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2163          ## reconsume          ## reconsume
2164    
2165          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2166    
2167          redo A;          redo A;
2168        } else {        } else {
2169          $self->{current_token}          !!!cp (160);
2170              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
2171                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2172  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2173          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2174          !!!next-input-character;          !!!next-input-character;
2175          redo A;          redo A;
2176        }        }
2177      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2178  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2179        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2180            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2181            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2182            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2183            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2184          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2185            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2186          !!!next-input-character;          !!!next-input-character;
2187          redo A;          redo A;
2188        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2189          $self->{state} = 'data';          !!!cp (162);
2190            $self->{state} = DATA_STATE;
2191          !!!next-input-character;          !!!next-input-character;
2192    
2193          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2194    
2195          redo A;          redo A;
2196        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2197            !!!cp (163);
2198          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2199          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2200          ## reconsume          ## reconsume
2201    
2202          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2203          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2204    
2205          redo A;          redo A;
2206        } else {        } else {
2207            !!!cp (164);
2208          $self->{current_token}->{name}          $self->{current_token}->{name}
2209            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2210          ## Stay in the state          ## Stay in the state
2211          !!!next-input-character;          !!!next-input-character;
2212          redo A;          redo A;
2213        }        }
2214      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2215        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2216            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2217            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2218            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2219            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2220            !!!cp (165);
2221          ## Stay in the state          ## Stay in the state
2222          !!!next-input-character;          !!!next-input-character;
2223          redo A;          redo A;
2224        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2225          $self->{state} = 'data';          !!!cp (166);
2226            $self->{state} = DATA_STATE;
2227          !!!next-input-character;          !!!next-input-character;
2228    
2229          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2230    
2231          redo A;          redo A;
2232        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2233            !!!cp (167);
2234          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2235          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2236          ## reconsume          ## reconsume
2237    
2238          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2239          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2240    
2241          redo A;          redo A;
2242        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2243                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2244          !!!next-input-character;          !!!next-input-character;
2245          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2246              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2247            !!!next-input-character;            !!!next-input-character;
2248            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2249                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2250              !!!next-input-character;              !!!next-input-character;
2251              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2252                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2253                !!!next-input-character;                !!!next-input-character;
2254                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2255                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2256                  !!!next-input-character;                  !!!next-input-character;
2257                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2258                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2259                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
2260                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2261                    !!!next-input-character;                    !!!next-input-character;
2262                    redo A;                    redo A;
2263                    } else {
2264                      !!!cp (169);
2265                  }                  }
2266                  } else {
2267                    !!!cp (170);
2268                }                }
2269                } else {
2270                  !!!cp (171);
2271              }              }
2272              } else {
2273                !!!cp (172);
2274            }            }
2275            } else {
2276              !!!cp (173);
2277          }          }
2278    
2279          #          #
2280        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2281                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2282          !!!next-input-character;          !!!next-input-character;
2283          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2284              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2285            !!!next-input-character;            !!!next-input-character;
2286            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2287                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2288              !!!next-input-character;              !!!next-input-character;
2289              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2290                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2291                !!!next-input-character;                !!!next-input-character;
2292                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2293                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2294                  !!!next-input-character;                  !!!next-input-character;
2295                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2296                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2297                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
2298                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2299                    !!!next-input-character;                    !!!next-input-character;
2300                    redo A;                    redo A;
2301                    } else {
2302                      !!!cp (175);
2303                  }                  }
2304                  } else {
2305                    !!!cp (176);
2306                }                }
2307                } else {
2308                  !!!cp (177);
2309              }              }
2310              } else {
2311                !!!cp (178);
2312            }            }
2313            } else {
2314              !!!cp (179);
2315          }          }
2316    
2317          #          #
2318        } else {        } else {
2319            !!!cp (180);
2320          !!!next-input-character;          !!!next-input-character;
2321          #          #
2322        }        }
2323    
2324        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2325        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
2326    
2327          $self->{state} = BOGUS_DOCTYPE_STATE;
2328        # next-input-character is already done        # next-input-character is already done
2329        redo A;        redo A;
2330      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2331        if ({        if ({
2332              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2333              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2334            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2335            !!!cp (181);
2336          ## Stay in the state          ## Stay in the state
2337          !!!next-input-character;          !!!next-input-character;
2338          redo A;          redo A;
2339        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2340            !!!cp (182);
2341          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2342          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2343          !!!next-input-character;          !!!next-input-character;
2344          redo A;          redo A;
2345        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2346            !!!cp (183);
2347          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2348          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2349          !!!next-input-character;          !!!next-input-character;
2350          redo A;          redo A;
2351        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2352            !!!cp (184);
2353          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2354    
2355          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2356          !!!next-input-character;          !!!next-input-character;
2357    
2358          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2359          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2360    
2361          redo A;          redo A;
2362        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2363            !!!cp (185);
2364          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2365    
2366          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2367          ## reconsume          ## reconsume
2368    
2369          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2370          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2371    
2372          redo A;          redo A;
2373        } else {        } else {
2374            !!!cp (186);
2375          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2376          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2377    
2378            $self->{state} = BOGUS_DOCTYPE_STATE;
2379          !!!next-input-character;          !!!next-input-character;
2380          redo A;          redo A;
2381        }        }
2382      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2383        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2384          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2385            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2386          !!!next-input-character;          !!!next-input-character;
2387          redo A;          redo A;
2388        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2389            !!!cp (188);
2390          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2391    
2392          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2393            !!!next-input-character;
2394    
2395            $self->{current_token}->{quirks} = 1;
2396            !!!emit ($self->{current_token}); # DOCTYPE
2397    
2398            redo A;
2399          } elsif ($self->{next_char} == -1) {
2400            !!!cp (189);
2401            !!!parse-error (type => 'unclosed PUBLIC literal');
2402    
2403            $self->{state} = DATA_STATE;
2404          ## reconsume          ## reconsume
2405    
2406          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2407          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2408    
2409          redo A;          redo A;
2410        } else {        } else {
2411            !!!cp (190);
2412          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2413              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2414          ## Stay in the state          ## Stay in the state
2415          !!!next-input-character;          !!!next-input-character;
2416          redo A;          redo A;
2417        }        }
2418      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2419        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2420          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2421            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2422          !!!next-input-character;          !!!next-input-character;
2423          redo A;          redo A;
2424        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2425            !!!cp (192);
2426          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2427    
2428          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2429            !!!next-input-character;
2430    
2431            $self->{current_token}->{quirks} = 1;
2432            !!!emit ($self->{current_token}); # DOCTYPE
2433    
2434            redo A;
2435          } elsif ($self->{next_char} == -1) {
2436            !!!cp (193);
2437            !!!parse-error (type => 'unclosed PUBLIC literal');
2438    
2439            $self->{state} = DATA_STATE;
2440          ## reconsume          ## reconsume
2441    
2442          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2443          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2444    
2445          redo A;          redo A;
2446        } else {        } else {
2447            !!!cp (194);
2448          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2449              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2450          ## Stay in the state          ## Stay in the state
2451          !!!next-input-character;          !!!next-input-character;
2452          redo A;          redo A;
2453        }        }
2454      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2455        if ({        if ({
2456              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2457              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2458            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2459            !!!cp (195);
2460          ## Stay in the state          ## Stay in the state
2461          !!!next-input-character;          !!!next-input-character;
2462          redo A;          redo A;
2463        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2464            !!!cp (196);
2465          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2466          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2467          !!!next-input-character;          !!!next-input-character;
2468          redo A;          redo A;
2469        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2470            !!!cp (197);
2471          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2472          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2473          !!!next-input-character;          !!!next-input-character;
2474          redo A;          redo A;
2475        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2476          $self->{state} = 'data';          !!!cp (198);
2477            $self->{state} = DATA_STATE;
2478          !!!next-input-character;          !!!next-input-character;
2479    
2480          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2481    
2482          redo A;          redo A;
2483        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2484            !!!cp (199);
2485          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2486    
2487          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2488          ## reconsume          ## reconsume
2489    
2490          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2491          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2492    
2493          redo A;          redo A;
2494        } else {        } else {
2495            !!!cp (200);
2496          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2497          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2498    
2499            $self->{state} = BOGUS_DOCTYPE_STATE;
2500          !!!next-input-character;          !!!next-input-character;
2501          redo A;          redo A;
2502        }        }
2503      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2504        if ({        if ({
2505              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2506              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2507            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2508            !!!cp (201);
2509          ## Stay in the state          ## Stay in the state
2510          !!!next-input-character;          !!!next-input-character;
2511          redo A;          redo A;
2512        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2513            !!!cp (202);
2514          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2515          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2516          !!!next-input-character;          !!!next-input-character;
2517          redo A;          redo A;
2518        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2519            !!!cp (203);
2520          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2521          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2522          !!!next-input-character;          !!!next-input-character;
2523          redo A;          redo A;
2524        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2525            !!!cp (204);
2526          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2527          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2528          !!!next-input-character;          !!!next-input-character;
2529    
2530          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2531          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2532    
2533          redo A;          redo A;
2534        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2535            !!!cp (205);
2536          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2537    
2538          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2539          ## reconsume          ## reconsume
2540    
2541          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2542          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2543    
2544          redo A;          redo A;
2545        } else {        } else {
2546            !!!cp (206);
2547          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2548          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2549    
2550            $self->{state} = BOGUS_DOCTYPE_STATE;
2551          !!!next-input-character;          !!!next-input-character;
2552          redo A;          redo A;
2553        }        }
2554      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2555        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2556          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2557            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2558          !!!next-input-character;          !!!next-input-character;
2559          redo A;          redo A;
2560        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2561            !!!cp (208);
2562            !!!parse-error (type => 'unclosed PUBLIC literal');
2563    
2564            $self->{state} = DATA_STATE;
2565            !!!next-input-character;
2566    
2567            $self->{current_token}->{quirks} = 1;
2568            !!!emit ($self->{current_token}); # DOCTYPE
2569    
2570            redo A;
2571          } elsif ($self->{next_char} == -1) {
2572            !!!cp (209);
2573          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2574    
2575          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2576          ## reconsume          ## reconsume
2577    
2578          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2579          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2580    
2581          redo A;          redo A;
2582        } else {        } else {
2583            !!!cp (210);
2584          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2585              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2586          ## Stay in the state          ## Stay in the state
2587          !!!next-input-character;          !!!next-input-character;
2588          redo A;          redo A;
2589        }        }
2590      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2591        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2592          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2593            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2594            !!!next-input-character;
2595            redo A;
2596          } elsif ($self->{next_char} == 0x003E) { # >
2597            !!!cp (212);
2598            !!!parse-error (type => 'unclosed PUBLIC literal');
2599    
2600            $self->{state} = DATA_STATE;
2601          !!!next-input-character;          !!!next-input-character;
2602    
2603            $self->{current_token}->{quirks} = 1;
2604            !!!emit ($self->{current_token}); # DOCTYPE
2605    
2606          redo A;          redo A;
2607        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2608            !!!cp (213);
2609          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2610    
2611          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2612          ## reconsume          ## reconsume
2613    
2614          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2615          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2616    
2617          redo A;          redo A;
2618        } else {        } else {
2619            !!!cp (214);
2620          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2621              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2622          ## Stay in the state          ## Stay in the state
2623          !!!next-input-character;          !!!next-input-character;
2624          redo A;          redo A;
2625        }        }
2626      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2627        if ({        if ({
2628              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2629              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2630            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2631            !!!cp (215);
2632          ## Stay in the state          ## Stay in the state
2633          !!!next-input-character;          !!!next-input-character;
2634          redo A;          redo A;
2635        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2636          $self->{state} = 'data';          !!!cp (216);
2637            $self->{state} = DATA_STATE;
2638          !!!next-input-character;          !!!next-input-character;
2639    
2640          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2641    
2642          redo A;          redo A;
2643        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2644            !!!cp (217);
2645          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2646    
2647          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2648          ## reconsume          ## reconsume
2649    
2650          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2651          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2652    
2653          redo A;          redo A;
2654        } else {        } else {
2655            !!!cp (218);
2656          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2657          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2658    
2659            $self->{state} = BOGUS_DOCTYPE_STATE;
2660          !!!next-input-character;          !!!next-input-character;
2661          redo A;          redo A;
2662        }        }
2663      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2664        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2665          $self->{state} = 'data';          !!!cp (219);
2666            $self->{state} = DATA_STATE;
2667          !!!next-input-character;          !!!next-input-character;
2668    
         delete $self->{current_token}->{correct};  
2669          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2670    
2671          redo A;          redo A;
2672        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2673            !!!cp (220);
2674          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2675          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2676          ## reconsume          ## reconsume
2677    
         delete $self->{current_token}->{correct};  
2678          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2679    
2680          redo A;          redo A;
2681        } else {        } else {
2682            !!!cp (221);
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} == CDATA_BLOCK_STATE) {
2688          my $s = '';
2689          
2690          my ($l, $c) = ($self->{line}, $self->{column});
2691    
2692          CS: while ($self->{next_char} != -1) {
2693            if ($self->{next_char} == 0x005D) { # ]
2694              !!!next-input-character;
2695              if ($self->{next_char} == 0x005D) { # ]
2696                !!!next-input-character;
2697                MDC: {
2698                  if ($self->{next_char} == 0x003E) { # >
2699                    !!!cp (221.1);
2700                    !!!next-input-character;
2701                    last CS;
2702                  } elsif ($self->{next_char} == 0x005D) { # ]
2703                    !!!cp (221.2);
2704                    $s .= ']';
2705                    !!!next-input-character;
2706                    redo MDC;
2707                  } else {
2708                    !!!cp (221.3);
2709                    $s .= ']]';
2710                    #
2711                  }
2712                } # MDC
2713              } else {
2714                !!!cp (221.4);
2715                $s .= ']';
2716                #
2717              }
2718            } else {
2719              !!!cp (221.5);
2720              #
2721            }
2722            $s .= chr $self->{next_char};
2723            !!!next-input-character;
2724          } # CS
2725    
2726          $self->{state} = DATA_STATE;
2727          ## next-input-character done or EOF, which is reconsumed.
2728    
2729          if (length $s) {
2730            !!!cp (221.6);
2731            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2732                      line => $l, column => $c});
2733          } else {
2734            !!!cp (221.7);
2735          }
2736    
2737          redo A;
2738    
2739          ## ISSUE: "text tokens" in spec.
2740          ## TODO: Streaming support
2741      } else {      } else {
2742        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2743      }      }
# Line 1644  sub _get_next_token ($) { Line 2746  sub _get_next_token ($) {
2746    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2747  } # _get_next_token  } # _get_next_token
2748    
2749  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2750    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2751    
2752      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2753    
2754    if ({    if ({
2755         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2756         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2757        }->{$self->{next_input_character}}) {         $additional => 1,
2758          }->{$self->{next_char}}) {
2759        !!!cp (1001);
2760      ## Don't consume      ## Don't consume
2761      ## No error      ## No error
2762      return undef;      return undef;
2763    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2764      !!!next-input-character;      !!!next-input-character;
2765      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2766          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2767        my $code;        my $code;
2768        X: {        X: {
2769          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2770          !!!next-input-character;          !!!next-input-character;
2771          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2772              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2773              !!!cp (1002);
2774            $code ||= 0;            $code ||= 0;
2775            $code *= 0x10;            $code *= 0x10;
2776            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2777            redo X;            redo X;
2778          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2779                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2780              !!!cp (1003);
2781            $code ||= 0;            $code ||= 0;
2782            $code *= 0x10;            $code *= 0x10;
2783            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2784            redo X;            redo X;
2785          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2786                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2787              !!!cp (1004);
2788            $code ||= 0;            $code ||= 0;
2789            $code *= 0x10;            $code *= 0x10;
2790            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2791            redo X;            redo X;
2792          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2793            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2794            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2795            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2796              $self->{next_char} = 0x0023; # #
2797            return undef;            return undef;
2798          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2799              !!!cp (1006);
2800            !!!next-input-character;            !!!next-input-character;
2801          } else {          } else {
2802            !!!parse-error (type => 'no refc');            !!!cp (1007);
2803              !!!parse-error (type => 'no refc', line => $l, column => $c);
2804          }          }
2805    
2806          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2807            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2808              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2809            $code = 0xFFFD;            $code = 0xFFFD;
2810          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2811            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2812              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2813            $code = 0xFFFD;            $code = 0xFFFD;
2814          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2815            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2816              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2817            $code = 0x000A;            $code = 0x000A;
2818          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2819            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2820              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2821            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2822          }          }
2823    
2824          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2825                    has_reference => 1,
2826                    line => $l, column => $c,
2827                   };
2828        } # X        } # X
2829      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2830               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2831        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2832        !!!next-input-character;        !!!next-input-character;
2833                
2834        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2835                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2836            !!!cp (1012);
2837          $code *= 10;          $code *= 10;
2838          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2839                    
2840          !!!next-input-character;          !!!next-input-character;
2841        }        }
2842    
2843        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2844            !!!cp (1013);
2845          !!!next-input-character;          !!!next-input-character;
2846        } else {        } else {
2847          !!!parse-error (type => 'no refc');          !!!cp (1014);
2848            !!!parse-error (type => 'no refc', line => $l, column => $c);
2849        }        }
2850    
2851        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2852          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2853            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2854          $code = 0xFFFD;          $code = 0xFFFD;
2855        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2856          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2857            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2858          $code = 0xFFFD;          $code = 0xFFFD;
2859        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2860          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2861            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2862          $code = 0x000A;          $code = 0x000A;
2863        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2864          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2865            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2866          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2867        }        }
2868                
2869        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2870                  line => $l, column => $c,
2871                 };
2872      } else {      } else {
2873        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2874        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2875        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2876          $self->{next_char} = 0x0023; # #
2877        return undef;        return undef;
2878      }      }
2879    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2880              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2881             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2882              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2883      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2884      !!!next-input-character;      !!!next-input-character;
2885    
2886      my $value = $entity_name;      my $value = $entity_name;
# Line 1759  sub _tokenize_attempt_to_consume_an_enti Line 2888  sub _tokenize_attempt_to_consume_an_enti
2888      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2889      our $EntityChar;      our $EntityChar;
2890    
2891      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2892             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2893             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2894               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2895              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2896               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2897              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2898               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2899              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2900        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2901        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2902          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2903              !!!cp (1020);
2904            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2905            $match = 1;            $match = 1;
2906            !!!next-input-character;            !!!next-input-character;
2907            last;            last;
2908          } else {          } else {
2909              !!!cp (1021);
2910            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2911            $match = -1;            $match = -1;
2912            !!!next-input-character;            !!!next-input-character;
2913          }          }
2914        } else {        } else {
2915          $value .= chr $self->{next_input_character};          !!!cp (1022);
2916            $value .= chr $self->{next_char};
2917          $match *= 2;          $match *= 2;
2918          !!!next-input-character;          !!!next-input-character;
2919        }        }
2920      }      }
2921            
2922      if ($match > 0) {      if ($match > 0) {
2923        return {type => CHARACTER_TOKEN, data => $value};        !!!cp (1023);
2924          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2925                  line => $l, column => $c,
2926                 };
2927      } elsif ($match < 0) {      } elsif ($match < 0) {
2928        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2929        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2930          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          !!!cp (1024);
2931        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2932          return {type => CHARACTER_TOKEN, data => $value};                  line => $l, column => $c,
2933                   };
2934          } else {
2935            !!!cp (1025);
2936            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2937                    line => $l, column => $c,
2938                   };
2939        }        }
2940      } else {      } else {
2941        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2942        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2943        return {type => CHARACTER_TOKEN, data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
2944          return {type => CHARACTER_TOKEN, data => '&'.$value,
2945                  line => $l, column => $c,
2946                 };
2947      }      }
2948    } else {    } else {
2949        !!!cp (1027);
2950      ## no characters are consumed      ## no characters are consumed
2951      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2952      return undef;      return undef;
2953    }    }
2954  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1841  sub _construct_tree ($) { Line 2986  sub _construct_tree ($) {
2986        
2987    !!!next-token;    !!!next-token;
2988    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
2989    undef $self->{form_element};    undef $self->{form_element};
2990    undef $self->{head_element};    undef $self->{head_element};
2991    $self->{open_elements} = [];    $self->{open_elements} = [];
2992    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2993    
2994      ## NOTE: The "initial" insertion mode.
2995    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2996    
2997      ## NOTE: The "before html" insertion mode.
2998    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2999      $self->{insertion_mode} = BEFORE_HEAD_IM;
3000    
3001      ## NOTE: The "before head" insertion mode and so on.
3002    $self->_tree_construction_main;    $self->_tree_construction_main;
3003  } # _construct_tree  } # _construct_tree
3004    
3005  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3006    my $self = shift;    my $self = shift;
3007    
3008      ## NOTE: "initial" insertion mode
3009    
3010    INITIAL: {    INITIAL: {
3011      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3012        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 1865  sub _tree_construction_initial ($) { Line 3018  sub _tree_construction_initial ($) {
3018        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3019            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3020            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3021          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3022            !!!parse-error (type => 'not HTML5', token => $token);
3023        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3024            !!!cp ('t2');
3025          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3026          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3027          } else {
3028            !!!cp ('t3');
3029        }        }
3030                
3031        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3032          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3033          ## NOTE: Default value for both |public_id| and |system_id| attributes
3034          ## are empty strings, so that we don't set any value in missing cases.
3035        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3036            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3037        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1881  sub _tree_construction_initial ($) { Line 3040  sub _tree_construction_initial ($) {
3040        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3041        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3042                
3043        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3044            !!!cp ('t4');
3045          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3046        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3047          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 1935  sub _tree_construction_initial ($) { Line 3095  sub _tree_construction_initial ($) {
3095            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
3096            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
3097            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
3098              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
3099              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
3100              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
3101            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
3102            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
3103            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 1957  sub _tree_construction_initial ($) { Line 3120  sub _tree_construction_initial ($) {
3120            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
3121            "HTML" => 1,            "HTML" => 1,
3122          }->{$pubid}) {          }->{$pubid}) {
3123              !!!cp ('t5');
3124            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3125          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
3126                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
3127            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3128                !!!cp ('t6');
3129              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3130            } else {            } else {
3131                !!!cp ('t7');
3132              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3133            }            }
3134          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
3135                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
3136              !!!cp ('t8');
3137            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3138            } else {
3139              !!!cp ('t9');
3140          }          }
3141          } else {
3142            !!!cp ('t10');
3143        }        }
3144        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3145          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3146          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3147          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") {
3148              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
3149            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3150              !!!cp ('t11');
3151            } else {
3152              !!!cp ('t12');
3153          }          }
3154          } else {
3155            !!!cp ('t13');
3156        }        }
3157                
3158        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3159        !!!next-token;        !!!next-token;
3160        return;        return;
3161      } elsif ({      } elsif ({
# Line 1986  sub _tree_construction_initial ($) { Line 3163  sub _tree_construction_initial ($) {
3163                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3164                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3165               }->{$token->{type}}) {               }->{$token->{type}}) {
3166        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3167          !!!parse-error (type => 'no DOCTYPE', token => $token);
3168        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3169        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3170        ## reprocess        ## reprocess
3171          !!!ack-later;
3172        return;        return;
3173      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3174        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3175          ## Ignore the token          ## Ignore the token
3176    
3177          unless (length $token->{data}) {          unless (length $token->{data}) {
3178            ## Stay in the phase            !!!cp ('t15');
3179              ## Stay in the insertion mode.
3180            !!!next-token;            !!!next-token;
3181            redo INITIAL;            redo INITIAL;
3182            } else {
3183              !!!cp ('t16');
3184          }          }
3185          } else {
3186            !!!cp ('t17');
3187        }        }
3188    
3189        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3190        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3191        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3192        ## reprocess        ## reprocess
3193        return;        return;
3194      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3195          !!!cp ('t18');
3196        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3197        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3198                
3199        ## Stay in the phase.        ## Stay in the insertion mode.
3200        !!!next-token;        !!!next-token;
3201        redo INITIAL;        redo INITIAL;
3202      } else {      } else {
3203        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3204      }      }
3205    } # INITIAL    } # INITIAL
3206    
3207      die "$0: _tree_construction_initial: This should be never reached";
3208  } # _tree_construction_initial  } # _tree_construction_initial
3209    
3210  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3211    my $self = shift;    my $self = shift;
3212    
3213      ## NOTE: "before html" insertion mode.
3214        
3215    B: {    B: {
3216        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3217          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3218            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3219          ## Ignore the token          ## Ignore the token
3220          ## Stay in the phase          ## Stay in the insertion mode.
3221          !!!next-token;          !!!next-token;
3222          redo B;          redo B;
3223        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3224            !!!cp ('t20');
3225          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3226          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3227          ## Stay in the phase          ## Stay in the insertion mode.
3228          !!!next-token;          !!!next-token;
3229          redo B;          redo B;
3230        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2041  sub _tree_construction_root_element ($) Line 3232  sub _tree_construction_root_element ($)
3232            ## Ignore the token.            ## Ignore the token.
3233    
3234            unless (length $token->{data}) {            unless (length $token->{data}) {
3235              ## Stay in the phase              !!!cp ('t21');
3236                ## Stay in the insertion mode.
3237              !!!next-token;              !!!next-token;
3238              redo B;              redo B;
3239              } else {
3240                !!!cp ('t22');
3241            }            }
3242            } else {
3243              !!!cp ('t23');
3244          }          }
3245    
3246            $self->{application_cache_selection}->(undef);
3247    
3248          #          #
3249          } elsif ($token->{type} == START_TAG_TOKEN) {
3250            if ($token->{tag_name} eq 'html') {
3251              my $root_element;
3252              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3253              $self->{document}->append_child ($root_element);
3254              push @{$self->{open_elements}},
3255                  [$root_element, $el_category->{html}];
3256    
3257              if ($token->{attributes}->{manifest}) {
3258                !!!cp ('t24');
3259                $self->{application_cache_selection}
3260                    ->($token->{attributes}->{manifest}->{value});
3261                ## ISSUE: Spec is unclear on relative references.
3262                ## According to Hixie (#whatwg 2008-03-19), it should be
3263                ## resolved against the base URI of the document in HTML
3264                ## or xml:base of the element in XHTML.
3265              } else {
3266                !!!cp ('t25');
3267                $self->{application_cache_selection}->(undef);
3268              }
3269    
3270              !!!nack ('t25c');
3271    
3272              !!!next-token;
3273              return; ## Go to the "before head" insertion mode.
3274            } else {
3275              !!!cp ('t25.1');
3276              #
3277            }
3278        } elsif ({        } elsif ({
                 START_TAG_TOKEN, 1,  
3279                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3280                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3281                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3282          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3283          #          #
3284        } else {        } else {
3285          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3286        }        }
3287        my $root_element; !!!create-element ($root_element, 'html');  
3288        $self->{document}->append_child ($root_element);      my $root_element;
3289        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3290        ## reprocess      $self->{document}->append_child ($root_element);
3291        #redo B;      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3292        return; ## Go to the main phase.  
3293        $self->{application_cache_selection}->(undef);
3294    
3295        ## NOTE: Reprocess the token.
3296        !!!ack-later;
3297        return; ## Go to the "before head" insertion mode.
3298    
3299        ## ISSUE: There is an issue in the spec
3300    } # B    } # B
3301    
3302      die "$0: _tree_construction_root_element: This should never be reached";
3303  } # _tree_construction_root_element  } # _tree_construction_root_element
3304    
3305  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2078  sub _reset_insertion_mode ($) { Line 3314  sub _reset_insertion_mode ($) {
3314            
3315      ## Step 3      ## Step 3
3316      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"!?  
3317        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3318          $last = 1;          $last = 1;
3319          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3320            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
3321                $self->{inner_html_node}->[1] eq 'th') {              !!!cp ('t27');
3322              #              #
3323            } else {            } else {
3324                !!!cp ('t28');
3325              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
3326            }            }
3327          }          }
3328        }        }
3329            
3330        ## Step 4..13      ## Step 4..14
3331        my $new_mode = {      my $new_mode;
3332        if ($node->[1] & FOREIGN_EL) {
3333          ## NOTE: Strictly spaking, the line below only applies to MathML and
3334          ## SVG elements.  Currently the HTML syntax supports only MathML and
3335          ## SVG elements as foreigners.
3336          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3337          ## ISSUE: What is set as the secondary insertion mode?
3338        } else {
3339          $new_mode = {
3340                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3341                          ## NOTE: |option| and |optgroup| do not set
3342                          ## insertion mode to "in select" by themselves.
3343                        td => IN_CELL_IM,                        td => IN_CELL_IM,
3344                        th => IN_CELL_IM,                        th => IN_CELL_IM,
3345                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2110  sub _reset_insertion_mode ($) { Line 3352  sub _reset_insertion_mode ($) {
3352                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3353                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3354                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3355                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3356        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3357        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3358                
3359        ## Step 14        ## Step 15
3360        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3361          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3362              !!!cp ('t29');
3363            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3364          } else {          } else {
3365              ## ISSUE: Can this state be reached?
3366              !!!cp ('t30');
3367            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3368          }          }
3369          return;          return;
3370          } else {
3371            !!!cp ('t31');
3372        }        }
3373                
3374        ## Step 15        ## Step 16
3375        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3376                
3377        ## Step 16        ## Step 17
3378        $i--;        $i--;
3379        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3380                
3381        ## Step 17        ## Step 18
3382        redo S3;        redo S3;
3383      } # S3      } # S3
3384    
3385      die "$0: _reset_insertion_mode: This line should never be reached";
3386  } # _reset_insertion_mode  } # _reset_insertion_mode
3387    
3388  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2154  sub _tree_construction_main ($) { Line 3404  sub _tree_construction_main ($) {
3404      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3405      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3406        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3407            !!!cp ('t32');
3408          return;          return;
3409        }        }
3410      }      }
# Line 2168  sub _tree_construction_main ($) { Line 3419  sub _tree_construction_main ($) {
3419    
3420        ## Step 6        ## Step 6
3421        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3422            !!!cp ('t33_1');
3423          #          #
3424        } else {        } else {
3425          my $in_open_elements;          my $in_open_elements;
3426          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3427            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3428                !!!cp ('t33');
3429              $in_open_elements = 1;              $in_open_elements = 1;
3430              last OE;              last OE;
3431            }            }
3432          }          }
3433          if ($in_open_elements) {          if ($in_open_elements) {
3434              !!!cp ('t34');
3435            #            #
3436          } else {          } else {
3437              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3438              !!!cp ('t35');
3439            redo S4;            redo S4;
3440          }          }
3441        }        }
# Line 2202  sub _tree_construction_main ($) { Line 3458  sub _tree_construction_main ($) {
3458    
3459        ## Step 11        ## Step 11
3460        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3461            !!!cp ('t36');
3462          ## Step 7'          ## Step 7'
3463          $i++;          $i++;
3464          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3465                    
3466          redo S7;          redo S7;
3467        }        }
3468    
3469          !!!cp ('t37');
3470      } # S7      } # S7
3471    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3472    
3473    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3474      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3475        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3476            !!!cp ('t38');
3477          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3478          return;          return;
3479        }        }
3480      }      }
3481    
3482        !!!cp ('t39');
3483    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3484    
3485    my $parse_rcdata = sub ($$) {    my $insert;
3486      my ($content_model_flag, $insert) = @_;  
3487      my $parse_rcdata = sub ($) {
3488        my ($content_model_flag) = @_;
3489    
3490      ## Step 1      ## Step 1
3491      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3492      my $el;      my $el;
3493      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3494    
3495      ## Step 2      ## Step 2
3496      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3497    
3498      ## Step 3      ## Step 3
3499      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2237  sub _tree_construction_main ($) { Line 3501  sub _tree_construction_main ($) {
3501    
3502      ## Step 4      ## Step 4
3503      my $text = '';      my $text = '';
3504        !!!nack ('t40.1');
3505      !!!next-token;      !!!next-token;
3506      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3507          !!!cp ('t40');
3508        $text .= $token->{data};        $text .= $token->{data};
3509        !!!next-token;        !!!next-token;
3510      }      }
3511    
3512      ## Step 5      ## Step 5
3513      if (length $text) {      if (length $text) {
3514          !!!cp ('t41');
3515        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3516        $el->append_child ($text);        $el->append_child ($text);
3517      }      }
# Line 2253  sub _tree_construction_main ($) { Line 3520  sub _tree_construction_main ($) {
3520      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3521    
3522      ## Step 7      ## Step 7
3523      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3524            $token->{tag_name} eq $start_tag_name) {
3525          !!!cp ('t42');
3526        ## 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});  
3527      } else {      } else {
3528        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3529          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3530            !!!cp ('t43');
3531            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3532          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3533            !!!cp ('t44');
3534            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3535          } else {
3536            die "$0: $content_model_flag in parse_rcdata";
3537          }
3538      }      }
3539      !!!next-token;      !!!next-token;
3540    }; # $parse_rcdata    }; # $parse_rcdata
3541    
3542    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3543      my $script_el;      my $script_el;
3544      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3545      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3546    
3547      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3548      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3549            
3550      my $text = '';      my $text = '';
3551        !!!nack ('t45.1');
3552      !!!next-token;      !!!next-token;
3553      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3554          !!!cp ('t45');
3555        $text .= $token->{data};        $text .= $token->{data};
3556        !!!next-token;        !!!next-token;
3557      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3558      if (length $text) {      if (length $text) {
3559          !!!cp ('t46');
3560        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3561      }      }
3562                                
# Line 2288  sub _tree_construction_main ($) { Line 3564  sub _tree_construction_main ($) {
3564    
3565      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3566          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3567          !!!cp ('t47');
3568        ## Ignore the token        ## Ignore the token
3569      } else {      } else {
3570        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3571          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3572        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3573        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3574      }      }
3575            
3576      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3577          !!!cp ('t49');
3578        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3579      } else {      } else {
3580          !!!cp ('t50');
3581        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3582        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3583    
# Line 2311  sub _tree_construction_main ($) { Line 3591  sub _tree_construction_main ($) {
3591      !!!next-token;      !!!next-token;
3592    }; # $script_start_tag    }; # $script_start_tag
3593    
3594      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3595      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3596      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3597    
3598    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3599      my $tag_name = shift;      my $end_tag_token = shift;
3600        my $tag_name = $end_tag_token->{tag_name};
3601    
3602        ## NOTE: The adoption agency algorithm (AAA).
3603    
3604      FET: {      FET: {
3605        ## Step 1        ## Step 1
3606        my $formatting_element;        my $formatting_element;
3607        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3608        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3609          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3610              !!!cp ('t52');
3611              last AFE;
3612            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3613                         eq $tag_name) {
3614              !!!cp ('t51');
3615            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3616            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3617            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3618          }          }
3619        } # AFE        } # AFE
3620        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3621          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3622            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3623          ## Ignore the token          ## Ignore the token
3624          !!!next-token;          !!!next-token;
3625          return;          return;
# Line 2340  sub _tree_construction_main ($) { Line 3631  sub _tree_construction_main ($) {
3631          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3632          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3633            if ($in_scope) {            if ($in_scope) {
3634                !!!cp ('t54');
3635              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3636              last INSCOPE;              last INSCOPE;
3637            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3638              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3639                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3640                                token => $end_tag_token);
3641              ## Ignore the token              ## Ignore the token
3642              !!!next-token;              !!!next-token;
3643              return;              return;
3644            }            }
3645          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3646                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3647            $in_scope = 0;            $in_scope = 0;
3648          }          }
3649        } # INSCOPE        } # INSCOPE
3650        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3651          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3652            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3653                            token => $end_tag_token);
3654          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3655          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3656          return;          return;
3657        }        }
3658        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3659          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3660            !!!parse-error (type => 'not closed',
3661                            value => $self->{open_elements}->[-1]->[0]
3662                                ->manakai_local_name,
3663                            token => $end_tag_token);
3664        }        }
3665                
3666        ## Step 2        ## Step 2
# Line 2370  sub _tree_construction_main ($) { Line 3668  sub _tree_construction_main ($) {
3668        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3669        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3670          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3671          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3672              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3673              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3674               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3675              !!!cp ('t59');
3676            $furthest_block = $node;            $furthest_block = $node;
3677            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3678          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3679              !!!cp ('t60');
3680            last OE;            last OE;
3681          }          }
3682        } # OE        } # OE
3683                
3684        ## Step 3        ## Step 3
3685        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3686            !!!cp ('t61');
3687          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3688          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3689          !!!next-token;          !!!next-token;
# Line 2395  sub _tree_construction_main ($) { Line 3696  sub _tree_construction_main ($) {
3696        ## Step 5        ## Step 5
3697        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3698        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3699            !!!cp ('t62');
3700          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3701        }        }
3702                
# Line 2417  sub _tree_construction_main ($) { Line 3719  sub _tree_construction_main ($) {
3719          S7S2: {          S7S2: {
3720            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3721              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3722                  !!!cp ('t63');
3723                $node_i_in_active = $_;                $node_i_in_active = $_;
3724                last S7S2;                last S7S2;
3725              }              }
# Line 2430  sub _tree_construction_main ($) { Line 3733  sub _tree_construction_main ($) {
3733                    
3734          ## Step 4          ## Step 4
3735          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3736              !!!cp ('t64');
3737            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3738          }          }
3739                    
3740          ## Step 5          ## Step 5
3741          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3742              !!!cp ('t65');
3743            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3744            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3745            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2452  sub _tree_construction_main ($) { Line 3757  sub _tree_construction_main ($) {
3757        } # S7          } # S7  
3758                
3759        ## Step 8        ## Step 8
3760        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3761            my $foster_parent_element;
3762            my $next_sibling;
3763            OE: for (reverse 0..$#{$self->{open_elements}}) {
3764              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3765                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3766                                 if (defined $parent and $parent->node_type == 1) {
3767                                   !!!cp ('t65.1');
3768                                   $foster_parent_element = $parent;
3769                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3770                                 } else {
3771                                   !!!cp ('t65.2');
3772                                   $foster_parent_element
3773                                     = $self->{open_elements}->[$_ - 1]->[0];
3774                                 }
3775                                 last OE;
3776                               }
3777                             } # OE
3778                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3779                               unless defined $foster_parent_element;
3780            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3781            $open_tables->[-1]->[1] = 1; # tainted
3782          } else {
3783            !!!cp ('t65.3');
3784            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3785          }
3786                
3787        ## Step 9        ## Step 9
3788        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2469  sub _tree_construction_main ($) { Line 3799  sub _tree_construction_main ($) {
3799        my $i;        my $i;
3800        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3801          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3802              !!!cp ('t66');
3803            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3804            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3805          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3806              !!!cp ('t67');
3807            $i = $_;            $i = $_;
3808          }          }
3809        } # AFE        } # AFE
# Line 2481  sub _tree_construction_main ($) { Line 3813  sub _tree_construction_main ($) {
3813        undef $i;        undef $i;
3814        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3815          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3816              !!!cp ('t68');
3817            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3818            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3819          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3820              !!!cp ('t69');
3821            $i = $_;            $i = $_;
3822          }          }
3823        } # OE        } # OE
# Line 2494  sub _tree_construction_main ($) { Line 3828  sub _tree_construction_main ($) {
3828      } # FET      } # FET
3829    }; # $formatting_end_tag    }; # $formatting_end_tag
3830    
3831    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3832      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3833    }; # $insert_to_current    }; # $insert_to_current
3834    
3835    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3836                         my $child = shift;      my $child = shift;
3837                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3838                              table => 1, tbody => 1, tfoot => 1,        # MUST
3839                              thead => 1, tr => 1,        my $foster_parent_element;
3840                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3841                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3842                           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') {  
3843                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3844                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3845                                   !!!cp ('t70');
3846                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3847                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3848                               } else {                               } else {
3849                                   !!!cp ('t71');
3850                                 $foster_parent_element                                 $foster_parent_element
3851                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3852                               }                               }
# Line 2524  sub _tree_construction_main ($) { Line 3857  sub _tree_construction_main ($) {
3857                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3858                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3859                             ($child, $next_sibling);                             ($child, $next_sibling);
3860                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3861                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3862                         }        !!!cp ('t72');
3863          $self->{open_elements}->[-1]->[0]->append_child ($child);
3864        }
3865    }; # $insert_to_foster    }; # $insert_to_foster
3866    
3867    my $insert;    B: while (1) {
   
   B: {  
3868      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3869        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
3870          !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3871        ## Ignore the token        ## Ignore the token
3872        ## Stay in the phase        ## Stay in the phase
3873        !!!next-token;        !!!next-token;
3874        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3875      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3876               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3877        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3878          ## Turn into the main phase          !!!cp ('t79');
3879          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3880          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3881        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3882          ## Turn into the main phase          !!!cp ('t80');
3883          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3884          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3885          } else {
3886            !!!cp ('t81');
3887        }        }
3888    
3889  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3890  ## 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');  
       }  
3891        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3892        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3893          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3894              !!!cp ('t84');
3895            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3896              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3897               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3898          }          }
3899        }        }
3900          !!!nack ('t84.1');
3901        !!!next-token;        !!!next-token;
3902        redo B;        next B;
3903      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3904        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3905        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3906            !!!cp ('t85');
3907          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3908        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3909            !!!cp ('t86');
3910          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3911        } else {        } else {
3912            !!!cp ('t87');
3913          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3914        }        }
3915        !!!next-token;        !!!next-token;
3916        redo B;        next B;
3917      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3918          if ($token->{type} == CHARACTER_TOKEN) {
3919            !!!cp ('t87.1');
3920            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3921            !!!next-token;
3922            next B;
3923          } elsif ($token->{type} == START_TAG_TOKEN) {
3924            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3925                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3926                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3927                ($token->{tag_name} eq 'svg' and
3928                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3929              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3930              !!!cp ('t87.2');
3931              #
3932            } elsif ({
3933                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3934                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3935                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3936                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3937                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3938                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3939                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3940                      var => 1,
3941                     }->{$token->{tag_name}}) {
3942              !!!cp ('t87.2');
3943              !!!parse-error (type => 'not closed',
3944                              value => $self->{open_elements}->[-1]->[0]
3945                                  ->manakai_local_name,
3946                              token => $token);
3947    
3948              pop @{$self->{open_elements}}
3949                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3950    
3951              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3952              ## Reprocess.
3953              next B;
3954            } else {
3955              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3956              my $tag_name = $token->{tag_name};
3957              if ($nsuri eq $SVG_NS) {
3958                $tag_name = {
3959                   altglyph => 'altGlyph',
3960                   altglyphdef => 'altGlyphDef',
3961                   altglyphitem => 'altGlyphItem',
3962                   animatecolor => 'animateColor',
3963                   animatemotion => 'animateMotion',
3964                   animatetransform => 'animateTransform',
3965                   clippath => 'clipPath',
3966                   feblend => 'feBlend',
3967                   fecolormatrix => 'feColorMatrix',
3968                   fecomponenttransfer => 'feComponentTransfer',
3969                   fecomposite => 'feComposite',
3970                   feconvolvematrix => 'feConvolveMatrix',
3971                   fediffuselighting => 'feDiffuseLighting',
3972                   fedisplacementmap => 'feDisplacementMap',
3973                   fedistantlight => 'feDistantLight',
3974                   feflood => 'feFlood',
3975                   fefunca => 'feFuncA',
3976                   fefuncb => 'feFuncB',
3977                   fefuncg => 'feFuncG',
3978                   fefuncr => 'feFuncR',
3979                   fegaussianblur => 'feGaussianBlur',
3980                   feimage => 'feImage',
3981                   femerge => 'feMerge',
3982                   femergenode => 'feMergeNode',
3983                   femorphology => 'feMorphology',
3984                   feoffset => 'feOffset',
3985                   fepointlight => 'fePointLight',
3986                   fespecularlighting => 'feSpecularLighting',
3987                   fespotlight => 'feSpotLight',
3988                   fetile => 'feTile',
3989                   feturbulence => 'feTurbulence',
3990                   foreignobject => 'foreignObject',
3991                   glyphref => 'glyphRef',
3992                   lineargradient => 'linearGradient',
3993                   radialgradient => 'radialGradient',
3994                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
3995                   textpath => 'textPath',  
3996                }->{$tag_name} || $tag_name;
3997              }
3998    
3999              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4000    
4001              ## "adjust foreign attributes" - done in insert-element-f
4002    
4003              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4004    
4005              if ($self->{self_closing}) {
4006                pop @{$self->{open_elements}};
4007                !!!ack ('t87.3');
4008              } else {
4009                !!!cp ('t87.4');
4010              }
4011    
4012              !!!next-token;
4013              next B;
4014            }
4015          } elsif ($token->{type} == END_TAG_TOKEN) {
4016            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4017            !!!cp ('t87.5');
4018            #
4019          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4020            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4021            !!!cp ('t87.6');
4022            #
4023            ## TODO: ...
4024          } else {
4025            die "$0: $token->{type}: Unknown token type";        
4026          }
4027        }
4028    
4029        if ($self->{insertion_mode} & HEAD_IMS) {
4030        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4031          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4032            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4033                !!!cp ('t88.2');
4034                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4035              } else {
4036                !!!cp ('t88.1');
4037                ## Ignore the token.
4038                !!!next-token;
4039                next B;
4040              }
4041            unless (length $token->{data}) {            unless (length $token->{data}) {
4042                !!!cp ('t88');
4043              !!!next-token;              !!!next-token;
4044              redo B;              next B;
4045            }            }
4046          }          }
4047    
4048          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4049              !!!cp ('t89');
4050            ## As if <head>            ## As if <head>
4051            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4052            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4053            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4054                  [$self->{head_element}, $el_category->{head}];
4055    
4056            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4057            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4058    
4059            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4060          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4061              !!!cp ('t90');
4062            ## As if </noscript>            ## As if </noscript>
4063            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4064            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4065                        
4066            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4067            ## As if </head>            ## As if </head>
# Line 2635  sub _tree_construction_main ($) { Line 4069  sub _tree_construction_main ($) {
4069    
4070            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4071          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4072              !!!cp ('t91');
4073            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4074    
4075            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4076            } else {
4077              !!!cp ('t92');
4078          }          }
4079    
4080              ## "after head" insertion mode          ## "after head" insertion mode
4081              ## As if <body>          ## As if <body>
4082              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4083              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4084              ## reprocess          ## reprocess
4085              redo B;          next B;
4086            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4087              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4088                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4089                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4090                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4091                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4092                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4093                  !!!next-token;              push @{$self->{open_elements}},
4094                  redo B;                  [$self->{head_element}, $el_category->{head}];
4095                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4096                  #              !!!nack ('t93.1');
4097                } else {              !!!next-token;
4098                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4099                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4100                  !!!next-token;              !!!cp ('t94');
4101                  redo B;              #
4102                }            } else {
4103              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!cp ('t95');
4104                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4105                !!!create-element ($self->{head_element}, 'head');              ## Ignore the token
4106                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
4107                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4108                next B;
4109              }
4110            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4111              !!!cp ('t96');
4112              ## As if <head>
4113              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4114              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4115              push @{$self->{open_elements}},
4116                  [$self->{head_element}, $el_category->{head}];
4117    
4118                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4119                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4120              }          } else {
4121              !!!cp ('t97');
4122            }
4123    
4124              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4125                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4126                    !!!cp ('t98');
4127                  ## As if </noscript>                  ## As if </noscript>
4128                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4129                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4130                                
4131                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4132                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4133                  } else {
4134                    !!!cp ('t99');
4135                }                }
4136    
4137                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4138                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4139                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4140                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4141                    push @{$self->{open_elements}},
4142                        [$self->{head_element}, $el_category->{head}];
4143                  } else {
4144                    !!!cp ('t101');
4145                }                }
4146                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4147                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4148                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4149                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4150                  !!!nack ('t101.1');
4151                !!!next-token;                !!!next-token;
4152                redo B;                next B;
4153              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4154                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4155                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4156                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4157                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4158                    push @{$self->{open_elements}},
4159                        [$self->{head_element}, $el_category->{head}];
4160                  } else {
4161                    !!!cp ('t103');
4162                }                }
4163                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4164                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4165                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4166                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4167                  !!!ack ('t103.1');
4168                !!!next-token;                !!!next-token;
4169                redo B;                next B;
4170              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4171                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4172                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4173                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4174                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4175                    push @{$self->{open_elements}},
4176                        [$self->{head_element}, $el_category->{head}];
4177                  } else {
4178                    !!!cp ('t105');
4179                }                }
4180                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4181                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.
4182    
4183                unless ($self->{confident}) {                unless ($self->{confident}) {
4184                  my $charset;                  if ($token->{attributes}->{charset}) {
4185                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                    !!!cp ('t106');
4186                    $charset = $token->{attributes}->{charset}->{value};                    ## NOTE: Whether the encoding is supported or not is handled
4187                  }                    ## in the {change_encoding} callback.
4188                  if ($token->{attributes}->{'http-equiv'}) {                    $self->{change_encoding}
4189                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                        ->($self, $token->{attributes}->{charset}->{value},
4190                    if ($token->{attributes}->{'http-equiv'}->{value}                           $token);
4191                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                    
4192                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4193                          ->set_user_data (manakai_has_reference =>
4194                                               $token->{attributes}->{charset}
4195                                                   ->{has_reference});
4196                    } elsif ($token->{attributes}->{content}) {
4197                      if ($token->{attributes}->{content}->{value}
4198                          =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4199                              [\x09-\x0D\x20]*=
4200                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4201                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4202                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
4203                    } ## TODO: And if supported                      ## NOTE: Whether the encoding is supported or not is handled
4204                        ## in the {change_encoding} callback.
4205                        $self->{change_encoding}
4206                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4207                               $token);
4208                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4209                            ->set_user_data (manakai_has_reference =>
4210                                                 $token->{attributes}->{content}
4211                                                       ->{has_reference});
4212                      } else {
4213                        !!!cp ('t108');
4214                      }
4215                    }
4216                  } else {
4217                    if ($token->{attributes}->{charset}) {
4218                      !!!cp ('t109');
4219                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4220                          ->set_user_data (manakai_has_reference =>
4221                                               $token->{attributes}->{charset}
4222                                                   ->{has_reference});
4223                    }
4224                    if ($token->{attributes}->{content}) {
4225                      !!!cp ('t110');
4226                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4227                          ->set_user_data (manakai_has_reference =>
4228                                               $token->{attributes}->{content}
4229                                                   ->{has_reference});
4230                  }                  }
                 ## TODO: Change the encoding  
4231                }                }
4232    
4233                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4234                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4235                  !!!ack ('t110.1');
4236                !!!next-token;                !!!next-token;
4237                redo B;                next B;
4238              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4239                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4240                    !!!cp ('t111');
4241                  ## As if </noscript>                  ## As if </noscript>
4242                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4243                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4244                                
4245                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4246                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4247                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4248                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4249                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4250                    push @{$self->{open_elements}},
4251                        [$self->{head_element}, $el_category->{head}];
4252                  } else {
4253                    !!!cp ('t113');
4254                }                }
4255    
4256                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4257                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4258                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4259                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4260                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4261                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4262                redo B;                next B;
4263              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4264                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4265                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4266                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4267                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4268                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4269                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4270                    push @{$self->{open_elements}},
4271                        [$self->{head_element}, $el_category->{head}];
4272                  } else {
4273                    !!!cp ('t115');
4274                }                }
4275                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4276                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4277                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4278                redo B;                next B;
4279              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4280                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4281                    !!!cp ('t116');
4282                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4283                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4284                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4285                    !!!nack ('t116.1');
4286                  !!!next-token;                  !!!next-token;
4287                  redo B;                  next B;
4288                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4289                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4290                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4291                  ## Ignore the token                  ## Ignore the token
4292                    !!!nack ('t117.1');
4293                  !!!next-token;                  !!!next-token;
4294                  redo B;                  next B;
4295                } else {                } else {
4296                    !!!cp ('t118');
4297                  #                  #
4298                }                }
4299              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4300                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4301                    !!!cp ('t119');
4302                  ## As if </noscript>                  ## As if </noscript>
4303                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4304                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4305                                
4306                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4307                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4308                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4309                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4310                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4311                    push @{$self->{open_elements}},
4312                        [$self->{head_element}, $el_category->{head}];
4313                  } else {
4314                    !!!cp ('t121');
4315                }                }
4316    
4317                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4318                $script_start_tag->($insert_to_current);                $script_start_tag->();
4319                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4320                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4321                redo B;                next B;
4322              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4323                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4324                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4325                    !!!cp ('t122');
4326                  ## As if </noscript>                  ## As if </noscript>
4327                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4328                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4329                                    
4330                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4331                  ## As if </head>                  ## As if </head>
# Line 2816  sub _tree_construction_main ($) { Line 4333  sub _tree_construction_main ($) {
4333                                    
4334                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4335                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4336                    !!!cp ('t124');
4337                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4338                                    
4339                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4340                  } else {
4341                    !!!cp ('t125');
4342                }                }
4343    
4344                ## "after head" insertion mode                ## "after head" insertion mode
4345                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4346                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4347                    !!!cp ('t126');
4348                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4349                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4350                    !!!cp ('t127');
4351                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4352                } else {                } else {
4353                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4354                }                }
4355                  !!!nack ('t127.1');
4356                !!!next-token;                !!!next-token;
4357                redo B;                next B;
4358              } else {              } else {
4359                  !!!cp ('t128');
4360                #                #
4361              }              }
4362    
4363              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4364                  !!!cp ('t129');
4365                ## As if </noscript>                ## As if </noscript>
4366                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4367                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4368                                
4369                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4370                ## As if </head>                ## As if </head>
# Line 2847  sub _tree_construction_main ($) { Line 4372  sub _tree_construction_main ($) {
4372    
4373                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4374              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4375                  !!!cp ('t130');
4376                ## As if </head>                ## As if </head>
4377                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4378    
4379                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4380                } else {
4381                  !!!cp ('t131');
4382              }              }
4383    
4384              ## "after head" insertion mode              ## "after head" insertion mode
4385              ## As if <body>              ## As if <body>
4386              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4387              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4388              ## reprocess              ## reprocess
4389              redo B;              !!!ack-later;
4390                next B;
4391            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4392              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4393                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4394                    !!!cp ('t132');
4395                  ## As if <head>                  ## As if <head>
4396                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4397                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4398                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4399                        [$self->{head_element}, $el_category->{head}];
4400    
4401                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4402                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4403                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4404                  !!!next-token;                  !!!next-token;
4405                  redo B;                  next B;
4406                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4407                    !!!cp ('t133');
4408                  ## As if </noscript>                  ## As if </noscript>
4409                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4410                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4411                                    
4412                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4413                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4414                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4415                  !!!next-token;                  !!!next-token;
4416                  redo B;                  next B;
4417                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4418                    !!!cp ('t134');
4419                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4420                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4421                  !!!next-token;                  !!!next-token;
4422                  redo B;                  next B;
4423                } else {                } else {
4424                    !!!cp ('t135');
4425                  #                  #
4426                }                }
4427              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4428                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4429                    !!!cp ('t136');
4430                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4431                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4432                  !!!next-token;                  !!!next-token;
4433                  redo B;                  next B;
4434                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4435                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!cp ('t137');
4436                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4437                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4438                  !!!next-token;                  !!!next-token;
4439                  redo B;                  next B;
4440                } else {                } else {
4441                    !!!cp ('t138');
4442                  #                  #
4443                }                }
4444              } elsif ({              } elsif ({
4445                        body => 1, html => 1,                        body => 1, html => 1,
4446                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4447                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4448                    !!!cp ('t139');
4449                  ## As if <head>                  ## As if <head>
4450                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4451                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4452                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4453                        [$self->{head_element}, $el_category->{head}];
4454    
4455                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4456                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4457                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4458                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t140');
4459                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4460                  ## Ignore the token                  ## Ignore the token
4461                  !!!next-token;                  !!!next-token;
4462                  redo B;                  next B;
4463                  } else {
4464                    !!!cp ('t141');
4465                }                }
4466                                
4467                #                #
# Line 2927  sub _tree_construction_main ($) { Line 4469  sub _tree_construction_main ($) {
4469                        p => 1, br => 1,                        p => 1, br => 1,
4470                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4471                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4472                    !!!cp ('t142');
4473                  ## As if <head>                  ## As if <head>
4474                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4475                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4476                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4477                        [$self->{head_element}, $el_category->{head}];
4478    
4479                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4480                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4481                  } else {
4482                    !!!cp ('t143');
4483                }                }
4484    
4485                #                #
4486              } else {              } else {
4487                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4488                    !!!cp ('t144');
4489                  #                  #
4490                } else {                } else {
4491                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t145');
4492                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4493                  ## Ignore the token                  ## Ignore the token
4494                  !!!next-token;                  !!!next-token;
4495                  redo B;                  next B;
4496                }                }
4497              }              }
4498    
4499              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4500                  !!!cp ('t146');
4501                ## As if </noscript>                ## As if </noscript>
4502                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4503                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4504                                
4505                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4506                ## As if </head>                ## As if </head>
# Line 2959  sub _tree_construction_main ($) { Line 4508  sub _tree_construction_main ($) {
4508    
4509                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4510              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4511                  !!!cp ('t147');
4512                ## As if </head>                ## As if </head>
4513                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4514    
4515                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4516              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4517                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4518                  !!!cp ('t148');
4519                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4520                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4521                !!!next-token;                !!!next-token;
4522                redo B;                next B;
4523                } else {
4524                  !!!cp ('t149');
4525              }              }
4526    
4527              ## "after head" insertion mode              ## "after head" insertion mode
4528              ## As if <body>              ## As if <body>
4529              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4530              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4531              ## reprocess              ## reprocess
4532              redo B;              next B;
4533            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4534              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4535            }            !!!cp ('t149.1');
4536    
4537              ## NOTE: As if <head>
4538              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4539              $self->{open_elements}->[-1]->[0]->append_child
4540                  ($self->{head_element});
4541              #push @{$self->{open_elements}},
4542              #    [$self->{head_element}, $el_category->{head}];
4543              #$self->{insertion_mode} = IN_HEAD_IM;
4544              ## NOTE: Reprocess.
4545    
4546              ## NOTE: As if </head>
4547              #pop @{$self->{open_elements}};
4548              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4549              ## NOTE: Reprocess.
4550              
4551              #
4552            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4553              !!!cp ('t149.2');
4554    
4555              ## NOTE: As if </head>
4556              pop @{$self->{open_elements}};
4557              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4558              ## NOTE: Reprocess.
4559    
4560              #
4561            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4562              !!!cp ('t149.3');
4563    
4564              !!!parse-error (type => 'in noscript:#eof', token => $token);
4565    
4566              ## As if </noscript>
4567              pop @{$self->{open_elements}};
4568              #$self->{insertion_mode} = IN_HEAD_IM;
4569              ## NOTE: Reprocess.
4570    
4571              ## NOTE: As if </head>
4572              pop @{$self->{open_elements}};
4573              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4574              ## NOTE: Reprocess.
4575    
4576              #
4577            } else {
4578              !!!cp ('t149.4');
4579              #
4580            }
4581    
4582            ## NOTE: As if <body>
4583            !!!insert-element ('body',, $token);
4584            $self->{insertion_mode} = IN_BODY_IM;
4585            ## NOTE: Reprocess.
4586            next B;
4587          } else {
4588            die "$0: $token->{type}: Unknown token type";
4589          }
4590    
4591            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4592      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
4593            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4594                !!!cp ('t150');
4595              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4596              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4597                            
4598              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4599    
4600              !!!next-token;              !!!next-token;
4601              redo B;              next B;
4602            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4603              if ({              if ({
4604                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 2997  sub _tree_construction_main ($) { Line 4606  sub _tree_construction_main ($) {
4606                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4607                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4608                  ## have an element in table scope                  ## have an element in table scope
4609                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4610                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4611                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4612                      $tn = $node->[1];                      !!!cp ('t151');
4613                      last INSCOPE;  
4614                    } elsif ({                      ## Close the cell
4615                              table => 1, html => 1,                      !!!back-token; # <x>
4616                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4617                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4618                    }                                line => $token->{line},
4619                  } # INSCOPE                                column => $token->{column}};
4620                    unless (defined $tn) {                      next B;
4621                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4622                      ## Ignore the token                      !!!cp ('t152');
4623                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4624                      redo B;                      last;
4625                    }                    }
4626                                    }
4627                  ## Close the cell  
4628                  !!!back-token; # <?>                  !!!cp ('t153');
4629                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
4630                  redo B;                      value => $token->{tag_name}, token => $token);
4631                    ## Ignore the token
4632                    !!!nack ('t153.1');
4633                    !!!next-token;
4634                    next B;
4635                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4636                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4637                                    
4638                  ## As if </caption>                  ## NOTE: As if </caption>.
4639                  ## have a table element in table scope                  ## have a table element in table scope
4640                  my $i;                  my $i;
4641                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4642                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4643                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4644                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4645                      last INSCOPE;                        !!!cp ('t155');
4646                    } elsif ({                        $i = $_;
4647                              table => 1, html => 1,                        last INSCOPE;
4648                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4649                      last INSCOPE;                        !!!cp ('t156');
4650                          last;
4651                        }
4652                    }                    }
4653    
4654                      !!!cp ('t157');
4655                      !!!parse-error (type => 'start tag not allowed',
4656                                      value => $token->{tag_name}, token => $token);
4657                      ## Ignore the token
4658                      !!!nack ('t157.1');
4659                      !!!next-token;
4660                      next B;
4661                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4662                                    
4663                  ## generate implied end tags                  ## generate implied end tags
4664                  if ({                  while ($self->{open_elements}->[-1]->[1]
4665                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4666                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4667                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token; # <?>  
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4668                  }                  }
4669    
4670                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4671                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4672                      !!!parse-error (type => 'not closed',
4673                                      value => $self->{open_elements}->[-1]->[0]
4674                                          ->manakai_local_name,
4675                                      token => $token);
4676                    } else {
4677                      !!!cp ('t160');
4678                  }                  }
4679                                    
4680                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3069  sub _tree_construction_main ($) { Line 4684  sub _tree_construction_main ($) {
4684                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4685                                    
4686                  ## reprocess                  ## reprocess
4687                  redo B;                  !!!ack-later;
4688                    next B;
4689                } else {                } else {
4690                    !!!cp ('t161');
4691                  #                  #
4692                }                }
4693              } else {              } else {
4694                  !!!cp ('t162');
4695                #                #
4696              }              }
4697            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3083  sub _tree_construction_main ($) { Line 4701  sub _tree_construction_main ($) {
4701                  my $i;                  my $i;
4702                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4703                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4704                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4705                        !!!cp ('t163');
4706                      $i = $_;                      $i = $_;
4707                      last INSCOPE;                      last INSCOPE;
4708                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4709                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4710                      last INSCOPE;                      last INSCOPE;
4711                    }                    }
4712                  } # INSCOPE                  } # INSCOPE
4713                    unless (defined $i) {                    unless (defined $i) {
4714                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4715                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4716                      ## Ignore the token                      ## Ignore the token
4717                      !!!next-token;                      !!!next-token;
4718                      redo B;                      next B;
4719                    }                    }
4720                                    
4721                  ## generate implied end tags                  ## generate implied end tags
4722                  if ({                  while ($self->{open_elements}->[-1]->[1]
4723                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4724                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4725                       th => ($token->{tag_name} eq 'td'),                    pop @{$self->{open_elements}};
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4726                  }                  }
4727                    
4728                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4729                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4730                      !!!cp ('t167');
4731                      !!!parse-error (type => 'not closed',
4732                                      value => $self->{open_elements}->[-1]->[0]
4733                                          ->manakai_local_name,
4734                                      token => $token);
4735                    } else {
4736                      !!!cp ('t168');
4737                  }                  }
4738                                    
4739                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3124  sub _tree_construction_main ($) { Line 4743  sub _tree_construction_main ($) {
4743                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4744                                    
4745                  !!!next-token;                  !!!next-token;
4746                  redo B;                  next B;
4747                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4748                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4749                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4750                  ## Ignore the token                  ## Ignore the token
4751                  !!!next-token;                  !!!next-token;
4752                  redo B;                  next B;
4753                } else {                } else {
4754                    !!!cp ('t170');
4755                  #                  #
4756                }                }
4757              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4758                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4759                  ## have a table element in table scope                  ## have a table element in table scope
4760                  my $i;                  my $i;
4761                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4762                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4763                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4764                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4765                      last INSCOPE;                        !!!cp ('t171');
4766                    } elsif ({                        $i = $_;
4767                              table => 1, html => 1,                        last INSCOPE;
4768                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4769                      last INSCOPE;                        !!!cp ('t172');
4770                          last;
4771                        }
4772                    }                    }
4773    
4774                      !!!cp ('t173');
4775                      !!!parse-error (type => 'unmatched end tag',
4776                                      value => $token->{tag_name}, token => $token);
4777                      ## Ignore the token
4778                      !!!next-token;
4779                      next B;
4780                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4781                                    
4782                  ## generate implied end tags                  ## generate implied end tags
4783                  if ({                  while ($self->{open_elements}->[-1]->[1]
4784                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4785                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
4786                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4787                  }                  }
4788                                    
4789                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4790                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4791                      !!!parse-error (type => 'not closed',
4792                                      value => $self->{open_elements}->[-1]->[0]
4793                                          ->manakai_local_name,
4794                                      token => $token);
4795                    } else {
4796                      !!!cp ('t176');
4797                  }                  }
4798                                    
4799                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3178  sub _tree_construction_main ($) { Line 4803  sub _tree_construction_main ($) {
4803                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4804                                    
4805                  !!!next-token;                  !!!next-token;
4806                  redo B;                  next B;
4807                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4808                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4809                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4810                  ## Ignore the token                  ## Ignore the token
4811                  !!!next-token;                  !!!next-token;
4812                  redo B;                  next B;
4813                } else {                } else {
4814                    !!!cp ('t178');
4815                  #                  #
4816                }                }
4817              } elsif ({              } elsif ({
# Line 3195  sub _tree_construction_main ($) { Line 4822  sub _tree_construction_main ($) {
4822                ## have an element in table scope                ## have an element in table scope
4823                my $i;                my $i;
4824                my $tn;                my $tn;
4825                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4826                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4827                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4828                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4829                    last INSCOPE;                      !!!cp ('t179');
4830                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4831                    $tn = $node->[1];  
4832                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4833                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
4834                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4835                            table => 1, html => 1,                                line => $token->{line},
4836                           }->{$node->[1]}) {                                column => $token->{column}};
4837                    last INSCOPE;                      next B;
4838                      } elsif ($node->[1] & TABLE_CELL_EL) {
4839                        !!!cp ('t180');
4840                        $tn = $node->[0]->manakai_local_name;
4841                        ## NOTE: There is exactly one |td| or |th| element
4842                        ## in scope in the stack of open elements by definition.
4843                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4844                        ## ISSUE: Can this be reached?
4845                        !!!cp ('t181');
4846                        last;
4847                      }
4848                  }                  }
4849                } # INSCOPE  
4850                unless (defined $i) {                  !!!cp ('t182');
4851                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4852                        value => $token->{tag_name}, token => $token);
4853                  ## Ignore the token                  ## Ignore the token
4854                  !!!next-token;                  !!!next-token;
4855                  redo B;                  next B;
4856                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4857              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4858                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4859                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4860    
4861                ## As if </caption>                ## As if </caption>
4862                ## have a table element in table scope                ## have a table element in table scope
4863                my $i;                my $i;
4864                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4865                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4866                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4867                      !!!cp ('t184');
4868                    $i = $_;                    $i = $_;
4869                    last INSCOPE;                    last INSCOPE;
4870                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4871                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4872                    last INSCOPE;                    last INSCOPE;
4873                  }                  }
4874                } # INSCOPE                } # INSCOPE
4875                unless (defined $i) {                unless (defined $i) {
4876                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4877                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4878                  ## Ignore the token                  ## Ignore the token
4879                  !!!next-token;                  !!!next-token;
4880                  redo B;                  next B;
4881                }                }
4882                                
4883                ## generate implied end tags                ## generate implied end tags
4884                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4885                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4886                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4887                }                }
4888    
4889                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4890                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4891                    !!!parse-error (type => 'not closed',
4892                                    value => $self->{open_elements}->[-1]->[0]
4893                                        ->manakai_local_name,
4894                                    token => $token);
4895                  } else {
4896                    !!!cp ('t189');
4897                }                }
4898    
4899                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3271  sub _tree_construction_main ($) { Line 4903  sub _tree_construction_main ($) {
4903                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4904    
4905                ## reprocess                ## reprocess
4906                redo B;                next B;
4907              } elsif ({              } elsif ({
4908                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4909                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4910                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4911                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
4912                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4913                  ## Ignore the token                  ## Ignore the token
4914                  !!!next-token;                  !!!next-token;
4915                  redo B;                  next B;
4916                } else {                } else {
4917                    !!!cp ('t191');
4918                  #                  #
4919                }                }
4920              } elsif ({              } elsif ({
# Line 3288  sub _tree_construction_main ($) { Line 4922  sub _tree_construction_main ($) {
4922                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4923                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4924                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4925                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
4926                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4927                ## Ignore the token                ## Ignore the token
4928                !!!next-token;                !!!next-token;
4929                redo B;                next B;
4930              } else {              } else {
4931                  !!!cp ('t193');
4932                #                #
4933              }              }
4934          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4935            for my $entry (@{$self->{open_elements}}) {
4936              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4937                !!!cp ('t75');
4938                !!!parse-error (type => 'in body:#eof', token => $token);
4939                last;
4940              }
4941            }
4942    
4943            ## Stop parsing.
4944            last B;
4945        } else {        } else {
4946          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4947        }        }
# Line 3302  sub _tree_construction_main ($) { Line 4949  sub _tree_construction_main ($) {
4949        $insert = $insert_to_current;        $insert = $insert_to_current;
4950        #        #
4951      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4952            if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4953              ## NOTE: There are "character in table" code clones.          if (not $open_tables->[-1]->[1] and # tainted
4954              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4955                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4956                                
4957                unless (length $token->{data}) {            unless (length $token->{data}) {
4958                  !!!next-token;              !!!cp ('t194');
4959                  redo B;              !!!next-token;
4960                }              next B;
4961              }            } else {
4962                !!!cp ('t195');
4963              }
4964            }
4965    
4966              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4967    
4968              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4969              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3321  sub _tree_construction_main ($) { Line 4971  sub _tree_construction_main ($) {
4971              ## result in a new Text node.              ## result in a new Text node.
4972              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4973                            
4974              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]}) {  
4975                # MUST                # MUST
4976                my $foster_parent_element;                my $foster_parent_element;
4977                my $next_sibling;                my $next_sibling;
4978                my $prev_sibling;                my $prev_sibling;
4979                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4980                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4981                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4982                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4983                        !!!cp ('t196');
4984                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4985                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4986                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4987                    } else {                    } else {
4988                        !!!cp ('t197');
4989                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4990                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4991                    }                    }
# Line 3348  sub _tree_construction_main ($) { Line 4997  sub _tree_construction_main ($) {
4997                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4998                if (defined $prev_sibling and                if (defined $prev_sibling and
4999                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5000                    !!!cp ('t198');
5001                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5002                } else {                } else {
5003                    !!!cp ('t199');
5004                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5005                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5006                     $next_sibling);                     $next_sibling);
5007                }                }
5008              } else {            $open_tables->[-1]->[1] = 1; # tainted
5009                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5010              }            !!!cp ('t200');
5011              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5012            }
5013                            
5014              !!!next-token;          !!!next-token;
5015              redo B;          next B;
5016            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5017              if ({              if ({
5018                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
5019                   th => 1, td => 1,                   th => 1, td => 1,
5020                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5021                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5022                  ## Clear back to table context                  ## Clear back to table context
5023                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5024                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5025                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
5026                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5027                  }                  }
5028                                    
5029                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5030                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5031                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5032                }                }
5033    
5034                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5035                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
5036                    !!!parse-error (type => 'missing start tag:tr');                    !!!cp ('t202');
5037                      !!!parse-error (type => 'missing start tag:tr', token => $token);
5038                  }                  }
5039                                    
5040                  ## Clear back to table body context                  ## Clear back to table body context
5041                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5042                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5043                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t203');
5044                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5045                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5046                  }                  }
5047                                    
5048                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5049                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5050                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5051                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5052                      !!!nack ('t204');
5053                    !!!next-token;                    !!!next-token;
5054                    redo B;                    next B;
5055                  } else {                  } else {
5056                    !!!insert-element ('tr');                    !!!cp ('t205');
5057                      !!!insert-element ('tr',, $token);
5058                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5059                  }                  }
5060                  } else {
5061                    !!!cp ('t206');
5062                }                }
5063    
5064                ## Clear back to table row context                ## Clear back to table row context
5065                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5066                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5067                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5068                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5069                }                }
5070                                
5071                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5072                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5073    
5074                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5075                                
5076                  !!!nack ('t207.1');
5077                !!!next-token;                !!!next-token;
5078                redo B;                next B;
5079              } elsif ({              } elsif ({
5080                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5081                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 3428  sub _tree_construction_main ($) { Line 5087  sub _tree_construction_main ($) {
5087                  my $i;                  my $i;
5088                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5089                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5090                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5091                        !!!cp ('t208');
5092                      $i = $_;                      $i = $_;
5093                      last INSCOPE;                      last INSCOPE;
5094                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5095                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5096                      last INSCOPE;                      last INSCOPE;
5097                    }                    }
5098                  } # INSCOPE                  } # INSCOPE
5099                  unless (defined $i) {                  unless (defined $i) {
5100                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5101    ## TODO: This type is wrong.
5102                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5103                    ## Ignore the token                    ## Ignore the token
5104                      !!!nack ('t210.1');
5105                    !!!next-token;                    !!!next-token;
5106                    redo B;                    next B;
5107                  }                  }
5108                                    
5109                  ## Clear back to table row context                  ## Clear back to table row context
5110                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5111                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5112                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5113                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5114                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5115                  }                  }
5116                                    
5117                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5118                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5119                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5120                      !!!cp ('t212');
5121                    ## reprocess                    ## reprocess
5122                    redo B;                    !!!ack-later;
5123                      next B;
5124                  } else {                  } else {
5125                      !!!cp ('t213');
5126                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5127                  }                  }
5128                }                }
# Line 3467  sub _tree_construction_main ($) { Line 5132  sub _tree_construction_main ($) {
5132                  my $i;                  my $i;
5133                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5134                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5135                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5136                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5137                      $i = $_;                      $i = $_;
5138                      last INSCOPE;                      last INSCOPE;
5139                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5140                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5141                      last INSCOPE;                      last INSCOPE;
5142                    }                    }
5143                  } # INSCOPE                  } # INSCOPE
5144                  unless (defined $i) {                  unless (defined $i) {
5145                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5146    ## TODO: This erorr type ios wrong.
5147                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5148                    ## Ignore the token                    ## Ignore the token
5149                      !!!nack ('t216.1');
5150                    !!!next-token;                    !!!next-token;
5151                    redo B;                    next B;
5152                  }                  }
5153    
5154                  ## Clear back to table body context                  ## Clear back to table body context
5155                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5156                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5157                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5158                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5159                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5160                  }                  }
5161                                    
# Line 3503  sub _tree_construction_main ($) { Line 5169  sub _tree_construction_main ($) {
5169                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5170                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5171                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5172                  } else {
5173                    !!!cp ('t218');
5174                }                }
5175    
5176                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5177                  ## Clear back to table context                  ## Clear back to table context
5178                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5179                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5180                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5181                      ## ISSUE: Can this state be reached?
5182                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5183                  }                  }
5184                                    
5185                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5186                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5187                  ## reprocess                  ## reprocess
5188                  redo B;                  !!!ack-later;
5189                    next B;
5190                } elsif ({                } elsif ({
5191                          caption => 1,                          caption => 1,
5192                          colgroup => 1,                          colgroup => 1,
5193                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5194                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5195                  ## Clear back to table context                  ## Clear back to table context
5196                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5197                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5198                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5199                      ## ISSUE: Can this state be reached?
5200                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5201                  }                  }
5202                                    
5203                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5204                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5205                                    
5206                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5207                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5208                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5209                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3541  sub _tree_construction_main ($) { Line 5212  sub _tree_construction_main ($) {
5212                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5213                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5214                  !!!next-token;                  !!!next-token;
5215                  redo B;                  !!!nack ('t220.1');
5216                    next B;
5217                } else {                } else {
5218                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5219                }                }
5220              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5221                ## NOTE: There are code clones for this "table in table"                !!!parse-error (type => 'not closed',
5222                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
5223                                      ->manakai_local_name,
5224                                  token => $token);
5225    
5226                ## As if </table>                ## As if </table>
5227                ## have a table element in table scope                ## have a table element in table scope
5228                my $i;                my $i;
5229                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5230                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5231                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5232                      !!!cp ('t221');
5233                    $i = $_;                    $i = $_;
5234                    last INSCOPE;                    last INSCOPE;
5235                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5236                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5237                    last INSCOPE;                    last INSCOPE;
5238                  }                  }
5239                } # INSCOPE                } # INSCOPE
5240                unless (defined $i) {                unless (defined $i) {
5241                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5242    ## TODO: The following is wrong, maybe.
5243                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5244                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5245                    !!!nack ('t223.1');
5246                  !!!next-token;                  !!!next-token;
5247                  redo B;                  next B;
5248                }                }
5249                                
5250    ## TODO: Followings are removed from the latest spec.
5251                ## generate implied end tags                ## generate implied end tags
5252                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5253                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5254                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5255                }                }
5256    
5257                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5258                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5259                    ## NOTE: |<table><tr><table>|
5260                    !!!parse-error (type => 'not closed',
5261                                    value => $self->{open_elements}->[-1]->[0]
5262                                        ->manakai_local_name,
5263                                    token => $token);
5264                  } else {
5265                    !!!cp ('t226');
5266                }                }
5267    
5268                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5269                  pop @{$open_tables};
5270    
5271                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5272    
5273                ## reprocess            ## reprocess
5274                redo B;            !!!ack-later;
5275              next B;
5276            } elsif ($token->{tag_name} eq 'style') {
5277              if (not $open_tables->[-1]->[1]) { # tainted
5278                !!!cp ('t227.8');
5279                ## NOTE: This is a "as if in head" code clone.
5280                $parse_rcdata->(CDATA_CONTENT_MODEL);
5281                next B;
5282              } else {
5283                !!!cp ('t227.7');
5284                #
5285              }
5286            } elsif ($token->{tag_name} eq 'script') {
5287              if (not $open_tables->[-1]->[1]) { # tainted
5288                !!!cp ('t227.6');
5289                ## NOTE: This is a "as if in head" code clone.
5290                $script_start_tag->();
5291                next B;
5292              } else {
5293                !!!cp ('t227.5');
5294                #
5295              }
5296            } elsif ($token->{tag_name} eq 'input') {
5297              if (not $open_tables->[-1]->[1]) { # tainted
5298                if ($token->{attributes}->{type}) { ## TODO: case
5299                  my $type = lc $token->{attributes}->{type}->{value};
5300                  if ($type eq 'hidden') {
5301                    !!!cp ('t227.3');
5302                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5303    
5304                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5305    
5306                    ## TODO: form element pointer
5307    
5308                    pop @{$self->{open_elements}};
5309    
5310                    !!!next-token;
5311                    !!!ack ('t227.2.1');
5312                    next B;
5313                  } else {
5314                    !!!cp ('t227.2');
5315                    #
5316                  }
5317              } else {              } else {
5318                  !!!cp ('t227.1');
5319                #                #
5320              }              }
5321            } elsif ($token->{type} == END_TAG_TOKEN) {            } else {
5322                !!!cp ('t227.4');
5323                #
5324              }
5325            } else {
5326              !!!cp ('t227');
5327              #
5328            }
5329    
5330            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5331    
5332            $insert = $insert_to_foster;
5333            #
5334          } elsif ($token->{type} == END_TAG_TOKEN) {
5335              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5336                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
5337                ## have an element in table scope                ## have an element in table scope
5338                my $i;                my $i;
5339                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5340                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5341                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5342                      !!!cp ('t228');
5343                    $i = $_;                    $i = $_;
5344                    last INSCOPE;                    last INSCOPE;
5345                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5346                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5347                    last INSCOPE;                    last INSCOPE;
5348                  }                  }
5349                } # INSCOPE                } # INSCOPE
5350                unless (defined $i) {                unless (defined $i) {
5351                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5352                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5353                  ## Ignore the token                  ## Ignore the token
5354                    !!!nack ('t230.1');
5355                  !!!next-token;                  !!!next-token;
5356                  redo B;                  next B;
5357                  } else {
5358                    !!!cp ('t232');
5359                }                }
5360    
5361                ## Clear back to table row context                ## Clear back to table row context
5362                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5363                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5364                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5365                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5366                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5367                }                }
5368    
5369                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5370                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5371                !!!next-token;                !!!next-token;
5372                redo B;                !!!nack ('t231.1');
5373                  next B;
5374              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5375                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5376                  ## As if </tr>                  ## As if </tr>
# Line 3639  sub _tree_construction_main ($) { Line 5378  sub _tree_construction_main ($) {
5378                  my $i;                  my $i;
5379                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5380                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5381                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5382                        !!!cp ('t233');
5383                      $i = $_;                      $i = $_;
5384                      last INSCOPE;                      last INSCOPE;
5385                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5386                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5387                      last INSCOPE;                      last INSCOPE;
5388                    }                    }
5389                  } # INSCOPE                  } # INSCOPE
5390                  unless (defined $i) {                  unless (defined $i) {
5391                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5392    ## TODO: The following is wrong.
5393                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5394                    ## Ignore the token                    ## Ignore the token
5395                      !!!nack ('t236.1');
5396                    !!!next-token;                    !!!next-token;
5397                    redo B;                    next B;
5398                  }                  }
5399                                    
5400                  ## Clear back to table row context                  ## Clear back to table row context
5401                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5402                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5403                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5404                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5405                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5406                  }                  }
5407                                    
# Line 3673  sub _tree_construction_main ($) { Line 5415  sub _tree_construction_main ($) {
5415                  my $i;                  my $i;
5416                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5417                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5418                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5419                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5420                      $i = $_;                      $i = $_;
5421                      last INSCOPE;                      last INSCOPE;
5422                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5423                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5424                      last INSCOPE;                      last INSCOPE;
5425                    }                    }
5426                  } # INSCOPE                  } # INSCOPE
5427                  unless (defined $i) {                  unless (defined $i) {
5428                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5429                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5430                    ## Ignore the token                    ## Ignore the token
5431                      !!!nack ('t239.1');
5432                    !!!next-token;                    !!!next-token;
5433                    redo B;                    next B;
5434                  }                  }
5435                                    
5436                  ## Clear back to table body context                  ## Clear back to table body context
5437                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5438                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5439                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5440                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5441                  }                  }
5442                                    
# Line 3711  sub _tree_construction_main ($) { Line 5452  sub _tree_construction_main ($) {
5452                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5453                }                }
5454    
5455                  ## NOTE: </table> in the "in table" insertion mode.
5456                  ## When you edit the code fragment below, please ensure that
5457                  ## the code for <table> in the "in table" insertion mode
5458                  ## is synced with it.
5459    
5460                ## have a table element in table scope                ## have a table element in table scope
5461                my $i;                my $i;
5462                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5463                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5464                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5465                      !!!cp ('t241');
5466                    $i = $_;                    $i = $_;
5467                    last INSCOPE;                    last INSCOPE;
5468                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5469                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5470                    last INSCOPE;                    last INSCOPE;
5471                  }                  }
5472                } # INSCOPE                } # INSCOPE
5473                unless (defined $i) {                unless (defined $i) {
5474                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5475                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5476                  ## Ignore the token                  ## Ignore the token
5477                    !!!nack ('t243.1');
5478                  !!!next-token;                  !!!next-token;
5479                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5480                }                }
5481                                    
5482                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5483                  pop @{$open_tables};
5484                                
5485                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5486                                
5487                !!!next-token;                !!!next-token;
5488                redo B;                next B;
5489              } elsif ({              } elsif ({
5490                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5491                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 3762  sub _tree_construction_main ($) { Line 5495  sub _tree_construction_main ($) {
5495                  my $i;                  my $i;
5496                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5497                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5498                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5499                        !!!cp ('t247');
5500                      $i = $_;                      $i = $_;
5501                      last INSCOPE;                      last INSCOPE;
5502                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5503                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
5504                      last INSCOPE;                      last INSCOPE;
5505                    }                    }
5506                  } # INSCOPE                  } # INSCOPE
5507                    unless (defined $i) {                    unless (defined $i) {
5508                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
5509                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5510                      ## Ignore the token                      ## Ignore the token
5511                        !!!nack ('t249.1');
5512                      !!!next-token;                      !!!next-token;
5513                      redo B;                      next B;
5514                    }                    }
5515                                    
5516                  ## As if </tr>                  ## As if </tr>
# Line 3783  sub _tree_construction_main ($) { Line 5518  sub _tree_construction_main ($) {
5518                  my $i;                  my $i;
5519                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5520                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5521                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5522                        !!!cp ('t250');
5523                      $i = $_;                      $i = $_;
5524                      last INSCOPE;                      last INSCOPE;
5525                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5526                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
5527                      last INSCOPE;                      last INSCOPE;
5528                    }                    }
5529                  } # INSCOPE                  } # INSCOPE
5530                    unless (defined $i) {                    unless (defined $i) {
5531                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
5532                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5533                      ## Ignore the token                      ## Ignore the token
5534                        !!!nack ('t252.1');
5535                      !!!next-token;                      !!!next-token;
5536                      redo B;                      next B;
5537                    }                    }
5538                                    
5539                  ## Clear back to table row context                  ## Clear back to table row context
5540                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5541                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5542                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
5543                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5544                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5545                  }                  }
5546                                    
# Line 3816  sub _tree_construction_main ($) { Line 5553  sub _tree_construction_main ($) {
5553                my $i;                my $i;
5554                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5555                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5556                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5557                      !!!cp ('t254');
5558                    $i = $_;                    $i = $_;
5559                    last INSCOPE;                    last INSCOPE;
5560                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5561                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5562                    last INSCOPE;                    last INSCOPE;
5563                  }                  }
5564                } # INSCOPE                } # INSCOPE
5565                unless (defined $i) {                unless (defined $i) {
5566                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5567                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5568                  ## Ignore the token                  ## Ignore the token
5569                    !!!nack ('t256.1');
5570                  !!!next-token;                  !!!next-token;
5571                  redo B;                  next B;
5572                }                }
5573    
5574                ## Clear back to table body context                ## Clear back to table body context
5575                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5576                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5577                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5578                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5579                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5580                }                }
5581    
5582                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5583                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5584                  !!!nack ('t257.1');
5585                !!!next-token;                !!!next-token;
5586                redo B;                next B;
5587              } elsif ({              } elsif ({
5588                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5589                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5590                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5591                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5592                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5593                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5594                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5595                !!!next-token;            ## Ignore the token
5596                redo B;            !!!nack ('t258.1');
5597              } else {             !!!next-token;
5598                #            next B;
5599              }          } else {
5600            } else {            !!!cp ('t259');
5601              die "$0: $token->{type}: Unknown token type";            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
           }  
5602    
5603        !!!parse-error (type => 'in table:'.$token->{tag_name});            $insert = $insert_to_foster;
5604              #
5605            }
5606          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5607            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5608                    @{$self->{open_elements}} == 1) { # redundant, maybe
5609              !!!parse-error (type => 'in body:#eof', token => $token);
5610              !!!cp ('t259.1');
5611              #
5612            } else {
5613              !!!cp ('t259.2');
5614              #
5615            }
5616    
5617        $insert = $insert_to_foster;          ## Stop parsing
5618        #          last B;
5619          } else {
5620            die "$0: $token->{type}: Unknown token type";
5621          }
5622      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5623            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5624              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5625                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5626                unless (length $token->{data}) {                unless (length $token->{data}) {
5627                    !!!cp ('t260');
5628                  !!!next-token;                  !!!next-token;
5629                  redo B;                  next B;
5630                }                }
5631              }              }
5632                            
5633                !!!cp ('t261');
5634              #              #
5635            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5636              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5637                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
5638                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5639                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5640                  !!!ack ('t262.1');
5641                !!!next-token;                !!!next-token;
5642                redo B;                next B;
5643              } else {              } else {
5644                  !!!cp ('t263');
5645                #                #
5646              }              }
5647            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5648              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5649                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5650                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5651                    !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5652                  ## Ignore the token                  ## Ignore the token
5653                  !!!next-token;                  !!!next-token;
5654                  redo B;                  next B;
5655                } else {                } else {
5656                    !!!cp ('t265');
5657                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5658                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5659                  !!!next-token;                  !!!next-token;
5660                  redo B;                              next B;            
5661                }                }
5662              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5663                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5664                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
5665                ## Ignore the token                ## Ignore the token
5666                !!!next-token;                !!!next-token;
5667                redo B;                next B;
5668              } else {              } else {
5669                  !!!cp ('t267');
5670                #                #
5671              }              }
5672            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5673              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5674            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5675              !!!cp ('t270.2');
5676              ## Stop parsing.
5677              last B;
5678            } else {
5679              ## NOTE: As if </colgroup>.
5680              !!!cp ('t270.1');
5681              pop @{$self->{open_elements}}; # colgroup
5682              $self->{insertion_mode} = IN_TABLE_IM;
5683              ## Reprocess.
5684              next B;
5685            }
5686          } else {
5687            die "$0: $token->{type}: Unknown token type";
5688          }
5689    
5690            ## As if </colgroup>            ## As if </colgroup>
5691            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5692              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5693    ## TODO: Wrong error type?
5694                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5695              ## Ignore the token              ## Ignore the token
5696                !!!nack ('t269.1');
5697              !!!next-token;              !!!next-token;
5698              redo B;              next B;
5699            } else {            } else {
5700                !!!cp ('t270');
5701              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5702              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5703                !!!ack-later;
5704              ## reprocess              ## reprocess
5705              redo B;              next B;
5706              }
5707        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5708          if ($token->{type} == CHARACTER_TOKEN) {
5709            !!!cp ('t271');
5710            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5711            !!!next-token;
5712            next B;
5713          } elsif ($token->{type} == START_TAG_TOKEN) {
5714            if ($token->{tag_name} eq 'option') {
5715              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5716                !!!cp ('t272');
5717                ## As if </option>
5718                pop @{$self->{open_elements}};
5719              } else {
5720                !!!cp ('t273');
5721            }            }
     } elsif ($self->{insertion_mode} == IN_SELECT_IM) {  
           if ($token->{type} == CHARACTER_TOKEN) {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} == START_TAG_TOKEN) {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
5722    
5723                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5724                !!!next-token;            !!!nack ('t273.1');
5725                redo B;            !!!next-token;
5726              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5727                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5728                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5729                  pop @{$self->{open_elements}};              !!!cp ('t274');
5730                }              ## As if </option>
5731                pop @{$self->{open_elements}};
5732              } else {
5733                !!!cp ('t275');
5734              }
5735    
5736                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5737                  ## As if </optgroup>              !!!cp ('t276');
5738                  pop @{$self->{open_elements}};              ## As if </optgroup>
5739                }              pop @{$self->{open_elements}};
5740              } else {
5741                !!!cp ('t277');
5742              }
5743    
5744                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5745                !!!next-token;            !!!nack ('t277.1');
5746                redo B;            !!!next-token;
5747              } elsif ($token->{tag_name} eq 'select') {            next B;
5748                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5749                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5750                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5751                my $i;                    {
5752                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5753                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5754                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5755                    $i = $_;                    }->{$token->{tag_name}})) {
5756                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5757                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5758                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5759                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5760                    last INSCOPE;            ## have an element in table scope
5761                  }            my $i;
5762                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5763                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5764                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5765                  ## Ignore the token                !!!cp ('t278');
5766                  !!!next-token;                $i = $_;
5767                  redo B;                last INSCOPE;
5768                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5769                  !!!cp ('t279');
5770                  last INSCOPE;
5771                }
5772              } # INSCOPE
5773              unless (defined $i) {
5774                !!!cp ('t280');
5775                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5776                ## Ignore the token
5777                !!!nack ('t280.1');
5778                !!!next-token;
5779                next B;
5780              }
5781                                
5782                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5783              splice @{$self->{open_elements}}, $i;
5784    
5785                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5786    
5787                !!!next-token;            if ($token->{tag_name} eq 'select') {
5788                redo B;              !!!nack ('t281.2');
5789              } else {              !!!next-token;
5790                #              next B;
5791              } else {
5792                !!!cp ('t281.1');
5793                !!!ack-later;
5794                ## Reprocess the token.
5795                next B;
5796              }
5797            } else {
5798              !!!cp ('t282');
5799              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5800              ## Ignore the token
5801              !!!nack ('t282.1');
5802              !!!next-token;
5803              next B;
5804            }
5805          } elsif ($token->{type} == END_TAG_TOKEN) {
5806            if ($token->{tag_name} eq 'optgroup') {
5807              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5808                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5809                !!!cp ('t283');
5810                ## As if </option>
5811                splice @{$self->{open_elements}}, -2;
5812              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5813                !!!cp ('t284');
5814                pop @{$self->{open_elements}};
5815              } else {
5816                !!!cp ('t285');
5817                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5818                ## Ignore the token
5819              }
5820              !!!nack ('t285.1');
5821              !!!next-token;
5822              next B;
5823            } elsif ($token->{tag_name} eq 'option') {
5824              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5825                !!!cp ('t286');
5826                pop @{$self->{open_elements}};
5827              } else {
5828                !!!cp ('t287');
5829                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5830                ## Ignore the token
5831              }
5832              !!!nack ('t287.1');
5833              !!!next-token;
5834              next B;
5835            } elsif ($token->{tag_name} eq 'select') {
5836              ## have an element in table scope
5837              my $i;
5838              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5839                my $node = $self->{open_elements}->[$_];
5840                if ($node->[1] & SELECT_EL) {
5841                  !!!cp ('t288');
5842                  $i = $_;
5843                  last INSCOPE;
5844                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5845                  !!!cp ('t289');
5846                  last INSCOPE;
5847              }              }
5848            } elsif ($token->{type} == END_TAG_TOKEN) {            } # INSCOPE
5849              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
5850                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
5851                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5852                  ## As if </option>              ## Ignore the token
5853                  splice @{$self->{open_elements}}, -2;              !!!nack ('t290.1');
5854                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!next-token;
5855                  pop @{$self->{open_elements}};              next B;
5856                } else {            }
                 !!!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;  
               }  
5857                                
5858                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5859              splice @{$self->{open_elements}}, $i;
5860    
5861                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5862    
5863                !!!next-token;            !!!nack ('t291.1');
5864                redo B;            !!!next-token;
5865              } elsif ({            next B;
5866                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5867                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5868                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5869                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5870                                   }->{$token->{tag_name}}) {
5871                ## have an element in table scope  ## TODO: The following is wrong?
5872                my $i;            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
               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) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
5873                                
5874                ## As if </select>            ## have an element in table scope
5875                ## have an element in table scope            my $i;
5876                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5877                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
5878                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5879                  if ($node->[1] eq 'select') {                !!!cp ('t292');
5880                    $i = $_;                $i = $_;
5881                    last INSCOPE;                last INSCOPE;
5882                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5883                            table => 1, html => 1,                !!!cp ('t293');
5884                           }->{$node->[1]}) {                last INSCOPE;
5885                    last INSCOPE;              }
5886                  }            } # INSCOPE
5887                } # INSCOPE            unless (defined $i) {
5888                unless (defined $i) {              !!!cp ('t294');
5889                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
5890                  ## Ignore the </select> token              !!!nack ('t294.1');
5891                  !!!next-token; ## TODO: ok?              !!!next-token;
5892                  redo B;              next B;
5893                }            }
5894                                
5895                splice @{$self->{open_elements}}, $i;            ## As if </select>
5896              ## have an element in table scope
5897                $self->_reset_insertion_mode;            undef $i;
5898              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5899                ## reprocess              my $node = $self->{open_elements}->[$_];
5900                redo B;              if ($node->[1] & SELECT_EL) {
5901              } else {                !!!cp ('t295');
5902                #                $i = $_;
5903                  last INSCOPE;
5904                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5905    ## ISSUE: Can this state be reached?
5906                  !!!cp ('t296');
5907                  last INSCOPE;
5908              }              }
5909            } else {            } # INSCOPE
5910              #            unless (defined $i) {
5911                !!!cp ('t297');
5912    ## TODO: The following error type is correct?
5913                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5914                ## Ignore the </select> token
5915                !!!nack ('t297.1');
5916                !!!next-token; ## TODO: ok?
5917                next B;
5918            }            }
5919                  
5920              !!!cp ('t298');
5921              splice @{$self->{open_elements}}, $i;
5922    
5923              $self->_reset_insertion_mode;
5924    
5925            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
5926              ## reprocess
5927              next B;
5928            } else {
5929              !!!cp ('t299');
5930              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5931            ## Ignore the token            ## Ignore the token
5932              !!!nack ('t299.3');
5933            !!!next-token;            !!!next-token;
5934            redo B;            next B;
5935            }
5936          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5937            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5938                    @{$self->{open_elements}} == 1) { # redundant, maybe
5939              !!!cp ('t299.1');
5940              !!!parse-error (type => 'in body:#eof', token => $token);
5941            } else {
5942              !!!cp ('t299.2');
5943            }
5944    
5945            ## Stop parsing.
5946            last B;
5947          } else {
5948            die "$0: $token->{type}: Unknown token type";
5949          }
5950      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5951        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5952          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
# Line 4106  sub _tree_construction_main ($) { Line 5957  sub _tree_construction_main ($) {
5957            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5958                        
5959            unless (length $token->{data}) {            unless (length $token->{data}) {
5960                !!!cp ('t300');
5961              !!!next-token;              !!!next-token;
5962              redo B;              next B;
5963            }            }
5964          }          }
5965                    
5966          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5967            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
5968              !!!parse-error (type => 'after html:#character', token => $token);
5969    
5970            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5971            } else {
5972              !!!cp ('t302');
5973          }          }
5974                    
5975          ## "after body" insertion mode          ## "after body" insertion mode
5976          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5977    
5978          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5979          ## reprocess          ## reprocess
5980          redo B;          next B;
5981        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5982          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5983            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
5984              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5985                        
5986            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5987            } else {
5988              !!!cp ('t304');
5989          }          }
5990    
5991          ## "after body" insertion mode          ## "after body" insertion mode
5992          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5993    
5994          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5995            !!!ack-later;
5996          ## reprocess          ## reprocess
5997          redo B;          next B;
5998        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5999          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6000            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
6001              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6002                        
6003            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6004            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6005            } else {
6006              !!!cp ('t306');
6007          }          }
6008    
6009          ## "after body" insertion mode          ## "after body" insertion mode
6010          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6011            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6012              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
6013                !!!parse-error (type => 'unmatched end tag:html', token => $token);
6014              ## Ignore the token              ## Ignore the token
6015              !!!next-token;              !!!next-token;
6016              redo B;              next B;
6017            } else {            } else {
6018                !!!cp ('t308');
6019              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6020              !!!next-token;              !!!next-token;
6021              redo B;              next B;
6022            }            }
6023          } else {          } else {
6024            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
6025              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6026    
6027            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6028            ## reprocess            ## reprocess
6029            redo B;            next B;
6030          }          }
6031          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6032            !!!cp ('t309.2');
6033            ## Stop parsing
6034            last B;
6035        } else {        } else {
6036          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6037        }        }
# Line 4172  sub _tree_construction_main ($) { Line 6041  sub _tree_construction_main ($) {
6041            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6042                        
6043            unless (length $token->{data}) {            unless (length $token->{data}) {
6044                !!!cp ('t310');
6045              !!!next-token;              !!!next-token;
6046              redo B;              next B;
6047            }            }
6048          }          }
6049                    
6050          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6051            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6052              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6053                !!!parse-error (type => 'in frameset:#character', token => $token);
6054            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6055              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
6056                !!!parse-error (type => 'after frameset:#character', token => $token);
6057            } else { # "after html frameset"            } else { # "after html frameset"
6058              !!!parse-error (type => 'after html:#character');              !!!cp ('t313');
6059                !!!parse-error (type => 'after html:#character', token => $token);
6060    
6061              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6062              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
6063              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6064            }            }
6065                        
6066            ## Ignore the token.            ## Ignore the token.
6067            if (length $token->{data}) {            if (length $token->{data}) {
6068                !!!cp ('t314');
6069              ## reprocess the rest of characters              ## reprocess the rest of characters
6070            } else {            } else {
6071                !!!cp ('t315');
6072              !!!next-token;              !!!next-token;
6073            }            }
6074            redo B;            next B;
6075          }          }
6076                    
6077          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6078        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6079          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6080            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t316');
6081              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6082    
6083            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6084            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6085          }          } else {
6086              !!!cp ('t317');
6087            }
6088    
6089          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6090              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6091            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6092              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6093              !!!nack ('t318.1');
6094            !!!next-token;            !!!next-token;
6095            redo B;            next B;
6096          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6097                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6098            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6099              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6100            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6101              !!!ack ('t319.1');
6102            !!!next-token;            !!!next-token;
6103            redo B;            next B;
6104          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6105              !!!cp ('t320');
6106            ## NOTE: As if in body.            ## NOTE: As if in body.
6107            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6108            redo B;            next B;
6109          } else {          } else {
6110            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6111              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6112                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6113            } else {            } else {
6114              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!cp ('t322');
6115                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6116            }            }
6117            ## Ignore the token            ## Ignore the token
6118              !!!nack ('t322.1');
6119            !!!next-token;            !!!next-token;
6120            redo B;            next B;
6121          }          }
6122        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6123          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6124            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t323');
6125              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6126    
6127            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6128            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6129            } else {
6130              !!!cp ('t324');
6131          }          }
6132    
6133          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6134              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6135            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6136                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6137              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6138                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6139              ## Ignore the token              ## Ignore the token
6140              !!!next-token;              !!!next-token;
6141            } else {            } else {
6142                !!!cp ('t326');
6143              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6144              !!!next-token;              !!!next-token;
6145            }            }
6146    
6147            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6148                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6149                !!!cp ('t327');
6150              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6151              } else {
6152                !!!cp ('t328');
6153            }            }
6154            redo B;            next B;
6155          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6156                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6157              !!!cp ('t329');
6158            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6159            !!!next-token;            !!!next-token;
6160            redo B;            next B;
6161          } else {          } else {
6162            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6163              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6164                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6165            } else {            } else {
6166              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!cp ('t331');
6167                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6168            }            }
6169            ## Ignore the token            ## Ignore the token
6170            !!!next-token;            !!!next-token;
6171            redo B;            next B;
6172            }
6173          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6174            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6175                    @{$self->{open_elements}} == 1) { # redundant, maybe
6176              !!!cp ('t331.1');
6177              !!!parse-error (type => 'in body:#eof', token => $token);
6178            } else {
6179              !!!cp ('t331.2');
6180          }          }
6181            
6182            ## Stop parsing
6183            last B;
6184        } else {        } else {
6185          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6186        }        }
# Line 4285  sub _tree_construction_main ($) { Line 6193  sub _tree_construction_main ($) {
6193      ## "in body" insertion mode      ## "in body" insertion mode
6194      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
6195        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6196            !!!cp ('t332');
6197          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6198          $script_start_tag->($insert);          $script_start_tag->();
6199          redo B;          next B;
6200        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6201            !!!cp ('t333');
6202          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6203          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6204          redo B;          next B;
6205        } elsif ({        } elsif ({
6206                  base => 1, link => 1,                  base => 1, link => 1,
6207                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6208            !!!cp ('t334');
6209          ## 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
6210          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6211          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6212            !!!ack ('t334.1');
6213          !!!next-token;          !!!next-token;
6214          redo B;          next B;
6215        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6216          ## 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
6217          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6218          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.
6219    
6220          unless ($self->{confident}) {          unless ($self->{confident}) {
6221            my $charset;            if ($token->{attributes}->{charset}) {
6222            if ($token->{attributes}->{charset}) { ## TODO: And if supported              !!!cp ('t335');
6223              $charset = $token->{attributes}->{charset}->{value};              ## NOTE: Whether the encoding is supported or not is handled
6224            }              ## in the {change_encoding} callback.
6225            if ($token->{attributes}->{'http-equiv'}) {              $self->{change_encoding}
6226              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6227              if ($token->{attributes}->{'http-equiv'}->{value}              
6228                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6229                    ->set_user_data (manakai_has_reference =>
6230                                         $token->{attributes}->{charset}
6231                                             ->{has_reference});
6232              } elsif ($token->{attributes}->{content}) {
6233                if ($token->{attributes}->{content}->{value}
6234                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6235                        [\x09-\x0D\x20]*=
6236                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6237                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6238                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                !!!cp ('t336');
6239              } ## TODO: And if supported                ## NOTE: Whether the encoding is supported or not is handled
6240                  ## in the {change_encoding} callback.
6241                  $self->{change_encoding}
6242                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6243                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6244                      ->set_user_data (manakai_has_reference =>
6245                                           $token->{attributes}->{content}
6246                                                 ->{has_reference});
6247                }
6248              }
6249            } else {
6250              if ($token->{attributes}->{charset}) {
6251                !!!cp ('t337');
6252                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6253                    ->set_user_data (manakai_has_reference =>
6254                                         $token->{attributes}->{charset}
6255                                             ->{has_reference});
6256              }
6257              if ($token->{attributes}->{content}) {
6258                !!!cp ('t338');
6259                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6260                    ->set_user_data (manakai_has_reference =>
6261                                         $token->{attributes}->{content}
6262                                             ->{has_reference});
6263            }            }
           ## TODO: Change the encoding  
6264          }          }
6265    
6266            !!!ack ('t338.1');
6267          !!!next-token;          !!!next-token;
6268          redo B;          next B;
6269        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6270          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6271          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6272          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6273            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6274        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6275          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6276                                
6277          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6278              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6279              !!!cp ('t342');
6280            ## Ignore the token            ## Ignore the token
6281          } else {          } else {
6282            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6283            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6284              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6285                  !!!cp ('t343');
6286                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6287                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6288                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6289              }              }
6290            }            }
6291          }          }
6292            !!!nack ('t343.1');
6293          !!!next-token;          !!!next-token;
6294          redo B;          next B;
6295        } elsif ({        } elsif ({
6296                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6297                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6298                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6299                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6300                  pre => 1,                  pre => 1, listing => 1,
6301                    form => 1,
6302                    table => 1,
6303                    hr => 1,
6304                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6305            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6306              !!!cp ('t350');
6307              !!!parse-error (type => 'in form:form', token => $token);
6308              ## Ignore the token
6309              !!!nack ('t350.1');
6310              !!!next-token;
6311              next B;
6312            }
6313    
6314          ## has a p element in scope          ## has a p element in scope
6315          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6316            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6317              !!!back-token;              !!!cp ('t344');
6318              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
6319              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6320            } elsif ({                        line => $token->{line}, column => $token->{column}};
6321                      table => 1, caption => 1, td => 1, th => 1,              next B;
6322                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6323                     }->{$_->[1]}) {              !!!cp ('t345');
6324              last INSCOPE;              last INSCOPE;
6325            }            }
6326          } # INSCOPE          } # INSCOPE
6327                        
6328          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6329          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6330              !!!nack ('t346.1');
6331            !!!next-token;            !!!next-token;
6332            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6333              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6334              unless (length $token->{data}) {              unless (length $token->{data}) {
6335                  !!!cp ('t346');
6336                !!!next-token;                !!!next-token;
6337                } else {
6338                  !!!cp ('t349');
6339              }              }
6340              } else {
6341                !!!cp ('t348');
6342            }            }
6343          } else {          } elsif ($token->{tag_name} eq 'form') {
6344              !!!cp ('t347.1');
6345              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6346    
6347              !!!nack ('t347.2');
6348            !!!next-token;            !!!next-token;
6349          }          } elsif ($token->{tag_name} eq 'table') {
6350          redo B;            !!!cp ('t382');
6351        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6352          if (defined $self->{form_element}) {            
6353            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6354            ## Ignore the token  
6355              !!!nack ('t382.1');
6356              !!!next-token;
6357            } elsif ($token->{tag_name} eq 'hr') {
6358              !!!cp ('t386');
6359              pop @{$self->{open_elements}};
6360            
6361              !!!nack ('t386.1');
6362            !!!next-token;            !!!next-token;
           redo B;  
6363          } else {          } else {
6364            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6365            !!!next-token;            !!!next-token;
           redo B;  
6366          }          }
6367        } elsif ($token->{tag_name} eq 'li') {          next B;
6368          ## has a p element in scope        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
6369          ## has a p element in scope          ## has a p element in scope
6370          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6371            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6372              !!!back-token;              !!!cp ('t353');
6373              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
6374              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6375            } elsif ({                        line => $token->{line}, column => $token->{column}};
6376                      table => 1, caption => 1, td => 1, th => 1,              next B;
6377                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6378                     }->{$_->[1]}) {              !!!cp ('t354');
6379              last INSCOPE;              last INSCOPE;
6380            }            }
6381          } # INSCOPE          } # INSCOPE
# Line 4477  sub _tree_construction_main ($) { Line 6383  sub _tree_construction_main ($) {
6383          ## Step 1          ## Step 1
6384          my $i = -1;          my $i = -1;
6385          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6386            my $li_or_dtdd = {li => {li => 1},
6387                              dt => {dt => 1, dd => 1},
6388                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6389          LI: {          LI: {
6390            ## Step 2            ## Step 2
6391            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6392              if ($i != -1) {              if ($i != -1) {
6393                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6394                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6395                                  value => $self->{open_elements}->[-1]->[0]
6396                                      ->manakai_local_name,
6397                                  token => $token);
6398                } else {
6399                  !!!cp ('t356');
6400              }              }
6401              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6402              last LI;              last LI;
6403              } else {
6404                !!!cp ('t357');
6405            }            }
6406                        
6407            ## Step 3            ## Step 3
6408            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6409                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6410                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6411                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6412                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6413                  not ($node->[1] & DIV_EL)) {
6414                !!!cp ('t358');
6415              last LI;              last LI;
6416            }            }
6417                        
6418              !!!cp ('t359');
6419            ## Step 4            ## Step 4
6420            $i--;            $i--;
6421            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
6422            redo LI;            redo LI;
6423          } # LI          } # LI
6424                        
6425          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6426            !!!nack ('t359.1');
6427          !!!next-token;          !!!next-token;
6428          redo B;          next B;
6429        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6430          ## has a p element in scope          ## has a p element in scope
6431          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6432            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6433              !!!back-token;              !!!cp ('t367');
6434              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
6435              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6436            } elsif ({                        line => $token->{line}, column => $token->{column}};
6437                      table => 1, caption => 1, td => 1, th => 1,              next B;
6438                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6439                     }->{$_->[1]}) {              !!!cp ('t368');
6440              last INSCOPE;              last INSCOPE;
6441            }            }
6442          } # INSCOPE          } # INSCOPE
6443                        
6444          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6445                        
6446          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6447                        
6448            !!!nack ('t368.1');
6449          !!!next-token;          !!!next-token;
6450          redo B;          next B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
6451        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6452          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6453            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6454            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6455              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
6456                !!!parse-error (type => 'in a:a', token => $token);
6457                            
6458              !!!back-token;              !!!back-token; # <a>
6459              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6460              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6461                $formatting_end_tag->($token);
6462                            
6463              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6464                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6465                    !!!cp ('t372');
6466                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
6467                  last AFE2;                  last AFE2;
6468                }                }
6469              } # AFE2              } # AFE2
6470              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
6471                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6472                    !!!cp ('t373');
6473                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
6474                  last OE;                  last OE;
6475                }                }
6476              } # OE              } # OE
6477              last AFE;              last AFE;
6478            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
6479                !!!cp ('t374');
6480              last AFE;              last AFE;
6481            }            }
6482          } # AFE          } # AFE
6483                        
6484          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6485    
6486          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6487          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6488    
6489            !!!nack ('t374.1');
6490          !!!next-token;          !!!next-token;
6491          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;  
6492        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6493          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6494    
6495          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6496          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6497            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6498            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6499              !!!parse-error (type => 'not closed:nobr');              !!!cp ('t376');
6500              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
6501              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
6502              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6503            } elsif ({                        line => $token->{line}, column => $token->{column}};
6504                      table => 1, caption => 1, td => 1, th => 1,              next B;
6505                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6506                     }->{$node->[1]}) {              !!!cp ('t377');
6507              last INSCOPE;              last INSCOPE;
6508            }            }
6509          } # INSCOPE          } # INSCOPE
6510                    
6511          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6512          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6513                    
6514            !!!nack ('t377.1');
6515          !!!next-token;          !!!next-token;
6516          redo B;          next B;
6517        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6518          ## has a button element in scope          ## has a button element in scope
6519          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6520            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6521            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6522              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
6523              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
6524              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
6525              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6526            } elsif ({                        line => $token->{line}, column => $token->{column}};
6527                      table => 1, caption => 1, td => 1, th => 1,              next B;
6528                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6529                     }->{$node->[1]}) {              !!!cp ('t379');
6530              last INSCOPE;              last INSCOPE;
6531            }            }
6532          } # INSCOPE          } # INSCOPE
6533                        
6534          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6535                        
6536          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6537          push @$active_formatting_elements, ['#marker', ''];  
6538            ## TODO: associate with $self->{form_element} if defined
6539    
         !!!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});  
6540          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6541            
6542          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
6543          !!!next-token;          !!!next-token;
6544          redo B;          next B;
6545        } elsif ({        } elsif ({
6546                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6547                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6548                  image => 1,                  noembed => 1,
6549                    noframes => 1,
6550                    noscript => 0, ## TODO: 1 if scripting is enabled
6551                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6552          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6553            !!!parse-error (type => 'image');            !!!cp ('t381');
6554            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
6555            } else {
6556              !!!cp ('t399');
6557          }          }
6558            ## NOTE: There is an "as if in body" code clone.
6559          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6560          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
6561        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6562          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6563                    
6564          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6565              !!!cp ('t389');
6566            ## Ignore the token            ## Ignore the token
6567              !!!nack ('t389'); ## NOTE: Not acknowledged.
6568            !!!next-token;            !!!next-token;
6569            redo B;            next B;
6570          } else {          } else {
6571            my $at = $token->{attributes};            my $at = $token->{attributes};
6572            my $form_attrs;            my $form_attrs;
# Line 4765  sub _tree_construction_main ($) { Line 6577  sub _tree_construction_main ($) {
6577            delete $at->{prompt};            delete $at->{prompt};
6578            my @tokens = (            my @tokens = (
6579                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6580                           attributes => $form_attrs},                           attributes => $form_attrs,
6581                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6582                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6583                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6584                            {type => START_TAG_TOKEN, tag_name => 'p',
6585                             line => $token->{line}, column => $token->{column}},
6586                            {type => START_TAG_TOKEN, tag_name => 'label',
6587                             line => $token->{line}, column => $token->{column}},
6588                         );                         );
6589            if ($prompt_attr) {            if ($prompt_attr) {
6590              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
6591                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6592                               #line => $token->{line}, column => $token->{column},
6593                              };
6594            } else {            } else {
6595                !!!cp ('t391');
6596              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6597                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6598                               #line => $token->{line}, column => $token->{column},
6599                              }; # SHOULD
6600              ## TODO: make this configurable              ## TODO: make this configurable
6601            }            }
6602            push @tokens,            push @tokens,
6603                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6604                             line => $token->{line}, column => $token->{column}},
6605                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6606                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6607                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6608                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6609                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6610            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6611                             line => $token->{line}, column => $token->{column}},
6612                            {type => END_TAG_TOKEN, tag_name => 'form',
6613                             line => $token->{line}, column => $token->{column}};
6614              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6615            !!!back-token (@tokens);            !!!back-token (@tokens);
6616            redo B;            !!!next-token;
6617              next B;
6618          }          }
6619        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6620          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6621          my $el;          my $el;
6622          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6623                    
6624          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6625          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4800  sub _tree_construction_main ($) { Line 6628  sub _tree_construction_main ($) {
6628          $insert->($el);          $insert->($el);
6629                    
6630          my $text = '';          my $text = '';
6631            !!!nack ('t392.1');
6632          !!!next-token;          !!!next-token;
6633          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6634            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
6635            unless (length $token->{data}) {            unless (length $token->{data}) {
6636                !!!cp ('t392');
6637              !!!next-token;              !!!next-token;
6638              } else {
6639                !!!cp ('t393');
6640            }            }
6641            } else {
6642              !!!cp ('t394');
6643          }          }
6644          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
6645              !!!cp ('t395');
6646            $text .= $token->{data};            $text .= $token->{data};
6647            !!!next-token;            !!!next-token;
6648          }          }
6649          if (length $text) {          if (length $text) {
6650              !!!cp ('t396');
6651            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6652          }          }
6653                    
# Line 4819  sub _tree_construction_main ($) { Line 6655  sub _tree_construction_main ($) {
6655                    
6656          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
6657              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6658              !!!cp ('t397');
6659            ## Ignore the token            ## Ignore the token
6660          } else {          } else {
6661            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6662              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6663          }          }
6664          !!!next-token;          !!!next-token;
6665          redo B;          next B;
6666        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6667                  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') {  
6668          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6669    
6670            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6671    
6672            ## "adjust foreign attributes" - done in insert-element-f
6673                    
6674          !!!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);
6675                    
6676          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6677              pop @{$self->{open_elements}};
6678              !!!ack ('t398.1');
6679            } else {
6680              !!!cp ('t398.2');
6681              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6682              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6683              ## mode, "in body" (not "in foreign content") secondary insertion
6684              ## mode, maybe.
6685            }
6686    
6687          !!!next-token;          !!!next-token;
6688          redo B;          next B;
6689        } elsif ({        } elsif ({
6690                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6691                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6692                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6693                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6694                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6695          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
6696            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6697          ## Ignore the token          ## Ignore the token
6698            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6699          !!!next-token;          !!!next-token;
6700          redo B;          next B;
6701                    
6702          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6703        } else {        } else {
6704            if ($token->{tag_name} eq 'image') {
6705              !!!cp ('t384');
6706              !!!parse-error (type => 'image', token => $token);
6707              $token->{tag_name} = 'img';
6708            } else {
6709              !!!cp ('t385');
6710            }
6711    
6712            ## NOTE: There is an "as if <br>" code clone.
6713          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6714                    
6715          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6716    
6717            if ({
6718                 applet => 1, marquee => 1, object => 1,
6719                }->{$token->{tag_name}}) {
6720              !!!cp ('t380');
6721              push @$active_formatting_elements, ['#marker', ''];
6722              !!!nack ('t380.1');
6723            } elsif ({
6724                      b => 1, big => 1, em => 1, font => 1, i => 1,
6725                      s => 1, small => 1, strile => 1,
6726                      strong => 1, tt => 1, u => 1,
6727                     }->{$token->{tag_name}}) {
6728              !!!cp ('t375');
6729              push @$active_formatting_elements, $self->{open_elements}->[-1];
6730              !!!nack ('t375.1');
6731            } elsif ($token->{tag_name} eq 'input') {
6732              !!!cp ('t388');
6733              ## TODO: associate with $self->{form_element} if defined
6734              pop @{$self->{open_elements}};
6735              !!!ack ('t388.2');
6736            } elsif ({
6737                      area => 1, basefont => 1, bgsound => 1, br => 1,
6738                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6739                      #image => 1,
6740                     }->{$token->{tag_name}}) {
6741              !!!cp ('t388.1');
6742              pop @{$self->{open_elements}};
6743              !!!ack ('t388.3');
6744            } elsif ($token->{tag_name} eq 'select') {
6745              ## TODO: associate with $self->{form_element} if defined
6746            
6747              if ($self->{insertion_mode} & TABLE_IMS or
6748                  $self->{insertion_mode} & BODY_TABLE_IMS or
6749                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6750                !!!cp ('t400.1');
6751                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6752              } else {
6753                !!!cp ('t400.2');
6754                $self->{insertion_mode} = IN_SELECT_IM;
6755              }
6756              !!!nack ('t400.3');
6757            } else {
6758              !!!nack ('t402');
6759            }
6760                    
6761          !!!next-token;          !!!next-token;
6762          redo B;          next B;
6763        }        }
6764      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6765        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6766          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6767              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6768            for (@{$self->{open_elements}}) {          INSCOPE: {
6769              unless ({            for (reverse @{$self->{open_elements}}) {
6770                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6771                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6772                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6773                      }->{$_->[1]}) {                last INSCOPE;
6774                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
6775                  !!!cp ('t405.1');
6776                  last;
6777              }              }
6778            }            }
6779    
6780            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6781            !!!next-token;                            value => $token->{tag_name}, token => $token);
6782            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6783            !!!next-token;            !!!next-token;
6784            redo B;            next B;
6785            } # INSCOPE
6786    
6787            for (@{$self->{open_elements}}) {
6788              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6789                !!!cp ('t403');
6790                !!!parse-error (type => 'not closed',
6791                                value => $_->[0]->manakai_local_name,
6792                                token => $token);
6793                last;
6794              } else {
6795                !!!cp ('t404');
6796              }
6797          }          }
6798    
6799            $self->{insertion_mode} = AFTER_BODY_IM;
6800            !!!next-token;
6801            next B;
6802        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6803          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
6804            ## up-to-date, though it has same effect as speced.
6805            if (@{$self->{open_elements}} > 1 and
6806                $self->{open_elements}->[1]->[1] & BODY_EL) {
6807            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6808            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6809              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
6810                !!!parse-error (type => 'not closed',
6811                                value => $self->{open_elements}->[1]->[0]
6812                                    ->manakai_local_name,
6813                                token => $token);
6814              } else {
6815                !!!cp ('t407');
6816            }            }
6817            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6818            ## reprocess            ## reprocess
6819            redo B;            next B;
6820          } else {          } else {
6821            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
6822              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6823            ## Ignore the token            ## Ignore the token
6824            !!!next-token;            !!!next-token;
6825            redo B;            next B;
6826          }          }
6827        } elsif ({        } elsif ({
6828                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6829                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6830                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6831                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6832                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6833                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6834          ## has an element in scope          ## has an element in scope
6835          my $i;          my $i;
6836          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6837            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6838            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6839              ## generate implied end tags              !!!cp ('t410');
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6840              $i = $_;              $i = $_;
6841              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6842            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6843                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6844              last INSCOPE;              last INSCOPE;
6845            }            }
6846          } # INSCOPE          } # INSCOPE
6847            
6848          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6849            if (defined $i) {            !!!cp ('t413');
6850              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6851            } else {
6852              ## Step 1. generate implied end tags
6853              while ({
6854                      dd => ($token->{tag_name} ne 'dd'),
6855                      dt => ($token->{tag_name} ne 'dt'),
6856                      li => ($token->{tag_name} ne 'li'),
6857                      p => 1,
6858                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6859                !!!cp ('t409');
6860                pop @{$self->{open_elements}};
6861              }
6862    
6863              ## Step 2.
6864              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6865                      ne $token->{tag_name}) {
6866                !!!cp ('t412');
6867                !!!parse-error (type => 'not closed',
6868                                value => $self->{open_elements}->[-1]->[0]
6869                                    ->manakai_local_name,
6870                                token => $token);
6871            } else {            } else {
6872              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6873            }            }
6874          }  
6875                      ## Step 3.
         if (defined $i) {  
6876            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6877          } elsif ($token->{tag_name} eq 'p') {  
6878            ## As if <p>, then reprocess the current token            ## Step 4.
6879            my $el;            $clear_up_to_marker->()
6880            !!!create-element ($el, 'p');                if {
6881            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
6882                  }->{$token->{tag_name}};
6883          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6884          !!!next-token;          !!!next-token;
6885          redo B;          next B;
6886        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6887            undef $self->{form_element};
6888    
6889          ## has an element in scope          ## has an element in scope
6890            my $i;
6891          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6892            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6893            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6894              ## generate implied end tags              !!!cp ('t418');
6895              if ({              $i = $_;
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6896              last INSCOPE;              last INSCOPE;
6897            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6898                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6899              last INSCOPE;              last INSCOPE;
6900            }            }
6901          } # INSCOPE          } # INSCOPE
6902            
6903          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6904            pop @{$self->{open_elements}};            !!!cp ('t421');
6905              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6906          } else {          } else {
6907            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            ## Step 1. generate implied end tags
6908              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6909                !!!cp ('t417');
6910                pop @{$self->{open_elements}};
6911              }
6912              
6913              ## Step 2.
6914              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6915                      ne $token->{tag_name}) {
6916                !!!cp ('t417.1');
6917                !!!parse-error (type => 'not closed',
6918                                value => $self->{open_elements}->[-1]->[0]
6919                                    ->manakai_local_name,
6920                                token => $token);
6921              } else {
6922                !!!cp ('t420');
6923              }  
6924              
6925              ## Step 3.
6926              splice @{$self->{open_elements}}, $i;
6927          }          }
6928    
         undef $self->{form_element};  
6929          !!!next-token;          !!!next-token;
6930          redo B;          next B;
6931        } elsif ({        } elsif ({
6932                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6933                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5000  sub _tree_construction_main ($) { Line 6935  sub _tree_construction_main ($) {
6935          my $i;          my $i;
6936          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6937            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6938            if ({            if ($node->[1] & HEADING_EL) {
6939                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,              !!!cp ('t423');
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6940              $i = $_;              $i = $_;
6941              last INSCOPE;              last INSCOPE;
6942            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6943                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6944              last INSCOPE;              last INSCOPE;
6945            }            }
6946          } # INSCOPE          } # INSCOPE
6947            
6948          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6949            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!cp ('t425.1');
6950              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6951            } else {
6952              ## Step 1. generate implied end tags
6953              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6954                !!!cp ('t422');
6955                pop @{$self->{open_elements}};
6956              }
6957              
6958              ## Step 2.
6959              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6960                      ne $token->{tag_name}) {
6961                !!!cp ('t425');
6962                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6963              } else {
6964                !!!cp ('t426');
6965              }
6966    
6967              ## Step 3.
6968              splice @{$self->{open_elements}}, $i;
6969          }          }
6970                    
         splice @{$self->{open_elements}}, $i if defined $i;  
6971          !!!next-token;          !!!next-token;
6972          redo B;          next B;
6973          } elsif ($token->{tag_name} eq 'p') {
6974            ## has an element in scope
6975            my $i;
6976            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6977              my $node = $self->{open_elements}->[$_];
6978              if ($node->[1] & P_EL) {
6979                !!!cp ('t410.1');
6980                $i = $_;
6981                last INSCOPE;
6982              } elsif ($node->[1] & SCOPING_EL) {
6983                !!!cp ('t411.1');
6984                last INSCOPE;
6985              }
6986            } # INSCOPE
6987    
6988            if (defined $i) {
6989              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6990                      ne $token->{tag_name}) {
6991                !!!cp ('t412.1');
6992                !!!parse-error (type => 'not closed',
6993                                value => $self->{open_elements}->[-1]->[0]
6994                                    ->manakai_local_name,
6995                                token => $token);
6996              } else {
6997                !!!cp ('t414.1');
6998              }
6999    
7000              splice @{$self->{open_elements}}, $i;
7001            } else {
7002              !!!cp ('t413.1');
7003              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7004    
7005              !!!cp ('t415.1');
7006              ## As if <p>, then reprocess the current token
7007              my $el;
7008              !!!create-element ($el, $HTML_NS, 'p',, $token);
7009              $insert->($el);
7010              ## NOTE: Not inserted into |$self->{open_elements}|.
7011            }
7012    
7013            !!!next-token;
7014            next B;
7015        } elsif ({        } elsif ({
7016                  a => 1,                  a => 1,
7017                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
7018                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
7019                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7020                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7021          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
7022          redo B;          $formatting_end_tag->($token);
7023            next B;
7024        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7025          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
7026            !!!parse-error (type => 'unmatched end tag:br', token => $token);
7027    
7028          ## As if <br>          ## As if <br>
7029          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7030                    
7031          my $el;          my $el;
7032          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7033          $insert->($el);          $insert->($el);
7034                    
7035          ## Ignore the token.          ## Ignore the token.
7036          !!!next-token;          !!!next-token;
7037          redo B;          next B;
7038        } elsif ({        } elsif ({
7039                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7040                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5064  sub _tree_construction_main ($) { Line 7047  sub _tree_construction_main ($) {
7047                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
7048                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7049                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7050          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
7051            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7052          ## Ignore the token          ## Ignore the token
7053          !!!next-token;          !!!next-token;
7054          redo B;          next B;
7055                    
7056          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7057                    
# Line 5078  sub _tree_construction_main ($) { Line 7062  sub _tree_construction_main ($) {
7062    
7063          ## Step 2          ## Step 2
7064          S2: {          S2: {
7065            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7066              ## Step 1              ## Step 1
7067              ## generate implied end tags              ## generate implied end tags
7068              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7069                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7070                   td => 1, th => 1, tr => 1,                ## ISSUE: Can this case be reached?
7071                   tbody => 1, tfoot => 1, thead => 1,                pop @{$self->{open_elements}};
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7072              }              }
7073                    
7074              ## Step 2              ## Step 2
7075              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7076                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      ne $token->{tag_name}) {
7077                  !!!cp ('t431');
7078                  ## NOTE: <x><y></x>
7079                  !!!parse-error (type => 'not closed',
7080                                  value => $self->{open_elements}->[-1]->[0]
7081                                      ->manakai_local_name,
7082                                  token => $token);
7083                } else {
7084                  !!!cp ('t432');
7085              }              }
7086                            
7087              ## Step 3              ## Step 3
# Line 5104  sub _tree_construction_main ($) { Line 7091  sub _tree_construction_main ($) {
7091              last S2;              last S2;
7092            } else {            } else {
7093              ## Step 3              ## Step 3
7094              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7095                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7096                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7097                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7098                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7099                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7100                ## Ignore the token                ## Ignore the token
7101                !!!next-token;                !!!next-token;
7102                last S2;                last S2;
7103              }              }
7104    
7105                !!!cp ('t434');
7106            }            }
7107                        
7108            ## Step 4            ## Step 4
# Line 5122  sub _tree_construction_main ($) { Line 7112  sub _tree_construction_main ($) {
7112            ## Step 5;            ## Step 5;
7113            redo S2;            redo S2;
7114          } # S2          } # S2
7115          redo B;          next B;
7116        }        }
7117      }      }
7118      redo B;      next B;
7119      } continue { # B
7120        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7121          ## NOTE: The code below is executed in cases where it does not have
7122          ## to be, but it it is harmless even in those cases.
7123          ## has an element in scope
7124          INSCOPE: {
7125            for (reverse 0..$#{$self->{open_elements}}) {
7126              my $node = $self->{open_elements}->[$_];
7127              if ($node->[1] & FOREIGN_EL) {
7128                last INSCOPE;
7129              } elsif ($node->[1] & SCOPING_EL) {
7130                last;
7131              }
7132            }
7133            
7134            ## NOTE: No foreign element in scope.
7135            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7136          } # INSCOPE
7137        }
7138    } # B    } # B
7139    
   ## 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  
   
7140    ## Stop parsing # MUST    ## Stop parsing # MUST
7141        
7142    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5144  sub set_inner_html ($$$) { Line 7148  sub set_inner_html ($$$) {
7148    my $s = \$_[0];    my $s = \$_[0];
7149    my $onerror = $_[1];    my $onerror = $_[1];
7150    
7151      ## ISSUE: Should {confident} be true?
7152    
7153    my $nt = $node->node_type;    my $nt = $node->node_type;
7154    if ($nt == 9) {    if ($nt == 9) {
7155      # MUST      # MUST
# Line 5172  sub set_inner_html ($$$) { Line 7178  sub set_inner_html ($$$) {
7178      my $p = $class->new;      my $p = $class->new;
7179      $p->{document} = $doc;      $p->{document} = $doc;
7180    
7181      ## Step 9 # MUST      ## Step 8 # MUST
7182      my $i = 0;      my $i = 0;
7183      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7184      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7185      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7186        my $self = shift;        my $self = shift;
7187    
7188        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7189        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7190    
7191          $self->{next_char} = -1 and return if $i >= length $$s;
7192          $self->{next_char} = ord substr $$s, $i++, 1;
7193    
7194          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7195          $p->{column}++;
7196    
7197        $self->{next_input_character} = -1 and return if $i >= length $$s;        if ($self->{next_char} == 0x000A) { # LF
7198        $self->{next_input_character} = ord substr $$s, $i++, 1;          $p->{line}++;
7199        $column++;          $p->{column} = 0;
7200            !!!cp ('i1');
7201        if ($self->{next_input_character} == 0x000A) { # LF        } elsif ($self->{next_char} == 0x000D) { # CR
         $line++;  
         $column = 0;  
       } elsif ($self->{next_input_character} == 0x000D) { # CR  
7202          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7203          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7204          $line++;          $p->{line}++;
7205          $column = 0;          $p->{column} = 0;
7206        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7207          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7208        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7209            !!!cp ('i3');
7210          } elsif ($self->{next_char} == 0x0000) { # NULL
7211            !!!cp ('i4');
7212          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7213          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7214          } elsif ($self->{next_char} <= 0x0008 or
7215                   (0x000E <= $self->{next_char} and
7216                    $self->{next_char} <= 0x001F) or
7217                   (0x007F <= $self->{next_char} and
7218                    $self->{next_char} <= 0x009F) or
7219                   (0xD800 <= $self->{next_char} and
7220                    $self->{next_char} <= 0xDFFF) or
7221                   (0xFDD0 <= $self->{next_char} and
7222                    $self->{next_char} <= 0xFDDF) or
7223                   {
7224                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7225                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7226                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7227                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7228                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7229                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7230                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7231                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7232                    0x10FFFE => 1, 0x10FFFF => 1,
7233                   }->{$self->{next_char}}) {
7234            !!!cp ('i4.1');
7235            !!!parse-error (type => 'control char', level => $self->{must_level});
7236    ## TODO: error type documentation
7237        }        }
7238      };      };
7239      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7240      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7241            
7242      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7243        my (%opt) = @_;        my (%opt) = @_;
7244        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7245          my $column = $opt{column};
7246          if (defined $opt{token} and defined $opt{token}->{line}) {
7247            $line = $opt{token}->{line};
7248            $column = $opt{token}->{column};
7249          }
7250          warn "Parse error ($opt{type}) at line $line column $column\n";
7251      };      };
7252      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7253        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7254      };      };
7255            
7256      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7257      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7258    
7259      ## Step 2      ## Step 2
7260      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7261      $p->{content_model} = {      $p->{content_model} = {
7262        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7263        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5233  sub set_inner_html ($$$) { Line 7274  sub set_inner_html ($$$) {
7274          unless defined $p->{content_model};          unless defined $p->{content_model};
7275          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7276    
7277      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7278          ## TODO: Foreign element OK?
7279    
7280      ## Step 4      ## Step 3
7281      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7282        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7283    
7284      ## Step 5 # MUST      ## Step 4 # MUST
7285      $doc->append_child ($root);      $doc->append_child ($root);
7286    
7287      ## Step 6 # MUST      ## Step 5 # MUST
7288      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7289    
7290      undef $p->{head_element};      undef $p->{head_element};
7291    
7292      ## Step 7 # MUST      ## Step 6 # MUST
7293      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7294    
7295      ## Step 8 # MUST      ## Step 7 # MUST
7296      my $anode = $node;      my $anode = $node;
7297      AN: while (defined $anode) {      AN: while (defined $anode) {
7298        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7299          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7300          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7301            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7302                !!!cp ('i5');
7303              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7304              last AN;              last AN;
7305            }            }
# Line 5265  sub set_inner_html ($$$) { Line 7308  sub set_inner_html ($$$) {
7308        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7309      } # AN      } # AN
7310            
7311      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7312      {      {
7313        my $self = $p;        my $self = $p;
7314        !!!next-token;        !!!next-token;
7315      }      }
7316      $p->_tree_construction_main;      $p->_tree_construction_main;
7317    
7318      ## Step 11 # MUST      ## Step 10 # MUST
7319      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7320      for (@cn) {      for (@cn) {
7321        $node->remove_child ($_);        $node->remove_child ($_);
7322      }      }
7323      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7324    
7325      ## Step 12 # MUST      ## Step 11 # MUST
7326      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7327      for (@cn) {      for (@cn) {
7328        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5289  sub set_inner_html ($$$) { Line 7331  sub set_inner_html ($$$) {
7331      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7332    
7333      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7334    
7335        delete $p->{parse_error}; # delete loop
7336    } else {    } else {
7337      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";
7338    }    }
# Line 5296  sub set_inner_html ($$$) { Line 7340  sub set_inner_html ($$$) {
7340    
7341  } # tree construction stage  } # tree construction stage
7342    
7343  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7344    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  
7345    
7346  1;  1;
7347  # $Date$  # $Date$

Legend:
Removed from v.1.56  
changed lines
  Added in v.1.134

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24