/[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.36 by wakaba, Mon Jul 16 04:51:22 2007 UTC revision 1.136 by wakaba, Sat May 17 12:29:24 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,    open my $byte_stream, '<', ref $_[0] ? $_[0] : \($_[0]);
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
338    
339  sub parse_string ($$$;$) {    my $onerror = $_[2] || sub {
340    my $self = shift->new;      my (%opt) = @_;
341    my $s = \$_[0];      warn "Parse error ($opt{type})\n";
342      };
343      $self->{parse_error} = $onerror; # updated later by parse_char_string
344    
345      ## HTML5 encoding sniffing algorithm
346      require Message::Charset::Info;
347      my $charset;
348      my $buffer;
349      my ($char_stream, $e_status);
350    
351      SNIFFING: {
352    
353        ## Step 1
354        if (defined $charset_name) {
355          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
356    
357          ## ISSUE: Unsupported encoding is not ignored according to the spec.
358          ($char_stream, $e_status) = $charset->get_decode_handle
359              ($byte_stream, allow_error_reporting => 1,
360               allow_fallback => 1);
361          if ($char_stream) {
362            $self->{confident} = 1;
363            last SNIFFING;
364          } else {
365            ## TODO: unsupported error
366          }
367        }
368    
369        ## Step 2
370        my $byte_buffer = '';
371        for (1..1024) {
372          my $char = $byte_stream->getc;
373          last unless defined $char;
374          $byte_buffer .= $char;
375        } ## TODO: timeout
376    
377        ## Step 3
378        if ($byte_buffer =~ /^\xFE\xFF/) {
379          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
380          ($char_stream, $e_status) = $charset->get_decode_handle
381              ($byte_stream, allow_error_reporting => 1,
382               allow_fallback => 1, byte_buffer => \$byte_buffer);
383          $self->{confident} = 1;
384          last SNIFFING;
385        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
386          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
387          ($char_stream, $e_status) = $charset->get_decode_handle
388              ($byte_stream, allow_error_reporting => 1,
389               allow_fallback => 1, byte_buffer => \$byte_buffer);
390          $self->{confident} = 1;
391          last SNIFFING;
392        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
393          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1, byte_buffer => \$byte_buffer);
397          $self->{confident} = 1;
398          last SNIFFING;
399        }
400    
401        ## Step 4
402        ## TODO: <meta charset>
403    
404        ## Step 5
405        ## TODO: from history
406    
407        ## Step 6
408        require Whatpm::Charset::UniversalCharDet;
409        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
410            ($byte_buffer);
411        if (defined $charset_name) {
412          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
413    
414          ## ISSUE: Unsupported encoding is not ignored according to the spec.
415          require Whatpm::Charset::DecodeHandle;
416          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
417              ($byte_stream);
418          ($char_stream, $e_status) = $charset->get_decode_handle
419              ($buffer, allow_error_reporting => 1,
420               allow_fallback => 1, byte_buffer => \$byte_buffer);
421          if ($char_stream) {
422            $buffer->{buffer} = $byte_buffer;
423            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
424                            value => $charset_name,
425                            level => $self->{info_level},
426                            line => 1, column => 1);
427            $self->{confident} = 0;
428            last SNIFFING;
429          }
430        }
431    
432        ## Step 7: default
433        ## TODO: Make this configurable.
434        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
435            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
436            ## detectable in the step 6.
437        require Whatpm::Charset::DecodeHandle;
438        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
439            ($byte_stream);
440        ($char_stream, $e_status)
441            = $charset->get_decode_handle ($buffer,
442                                           allow_error_reporting => 1,
443                                           allow_fallback => 1,
444                                           byte_buffer => \$byte_buffer);
445        $buffer->{buffer} = $byte_buffer;
446        !!!parse-error (type => 'sniffing:default', ## TODO: type name
447                        value => 'windows-1252',
448                        level => $self->{info_level},
449                        line => 1, column => 1);
450        $self->{confident} = 0;
451      } # SNIFFING
452    
453      $self->{input_encoding} = $charset->get_iana_name;
454      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
455        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
456                        value => $self->{input_encoding},
457                        level => $self->{unsupported_level},
458                        line => 1, column => 1);
459      } elsif (not ($e_status &
460                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
461        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
462                        value => $self->{input_encoding},
463                        level => $self->{unsupported_level},
464                        line => 1, column => 1);
465      }
466    
467      $self->{change_encoding} = sub {
468        my $self = shift;
469        $charset_name = shift;
470        my $token = shift;
471    
472        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
473        ($char_stream, $e_status) = $charset->get_decode_handle
474            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
475             byte_buffer => \ $buffer->{buffer});
476        
477        if ($char_stream) { # if supported
478          ## "Change the encoding" algorithm:
479    
480          ## Step 1    
481          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
482            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
483            ($char_stream, $e_status) = $charset->get_decode_handle
484                ($byte_stream,
485                 byte_buffer => \ $buffer->{buffer});
486          }
487          $charset_name = $charset->get_iana_name;
488          
489          ## Step 2
490          if (defined $self->{input_encoding} and
491              $self->{input_encoding} eq $charset_name) {
492            !!!parse-error (type => 'charset label:matching', ## TODO: type
493                            value => $charset_name,
494                            level => $self->{info_level});
495            $self->{confident} = 1;
496            return;
497          }
498    
499          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
500              ':'.$charset_name, level => 'w', token => $token);
501          
502          ## Step 3
503          # if (can) {
504            ## change the encoding on the fly.
505            #$self->{confident} = 1;
506            #return;
507          # }
508          
509          ## Step 4
510          throw Whatpm::HTML::RestartParser ();
511        }
512      }; # $self->{change_encoding}
513    
514      my $char_onerror = sub {
515        my (undef, $type, %opt) = @_;
516        !!!parse-error (%opt, type => $type);
517        if ($opt{octets}) {
518          ${$opt{octets}} = "\x{FFFD}"; # relacement character
519        }
520      };
521      $char_stream->onerror ($char_onerror);
522    
523      my @args = @_; shift @args; # $s
524      my $return;
525      try {
526        $return = $self->parse_char_stream ($char_stream, @args);  
527      } catch Whatpm::HTML::RestartParser with {
528        ## NOTE: Invoked after {change_encoding}.
529    
530        $self->{input_encoding} = $charset->get_iana_name;
531        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
532          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
533                          value => $self->{input_encoding},
534                          level => $self->{unsupported_level},
535                          line => 1, column => 1);
536        } elsif (not ($e_status &
537                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
538          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
539                          value => $self->{input_encoding},
540                          level => $self->{unsupported_level},
541                          line => 1, column => 1);
542        }
543        $self->{confident} = 1;
544        $char_stream->onerror ($char_onerror);
545        $return = $self->parse_char_stream ($char_stream, @args);
546      };
547      return $return;
548    } # parse_byte_string
549    
550    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
551    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
552    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
553    ## because the core part of our HTML parser expects a string of character,
554    ## not a string of bytes or code units or anything which might contain a BOM.
555    ## Therefore, any parser interface that accepts a string of bytes,
556    ## such as |parse_byte_string| in this module, must ensure that it does
557    ## strip the BOM and never strip any ZWNBSP.
558    
559    sub parse_char_string ($$$;$) {
560      my $self = shift;
561      open my $input, '<:utf8', ref $_[0] ? $_[0] : \($_[0]);
562      return $self->parse_char_stream ($input, @_[1..$#_]);
563    } # parse_char_string
564    *parse_string = \&parse_char_string;
565    
566    sub parse_char_stream ($$$;$) {
567      my $self = ref $_[0] ? shift : shift->new;
568      my $input = $_[0];
569    $self->{document} = $_[1];    $self->{document} = $_[1];
570      @{$self->{document}->child_nodes} = ();
571    
572    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
573    
574      $self->{confident} = 1 unless exists $self->{confident};
575      $self->{document}->input_encoding ($self->{input_encoding})
576          if defined $self->{input_encoding};
577    
578    my $i = 0;    my $i = 0;
579    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
580    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
581    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
582      my $self = shift;      my $self = shift;
583    
584      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
585      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
586    
587      $self->{next_input_character} = -1 and return if $i >= length $$s;      my $char = $input->getc;
588      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_char} = -1 and return unless defined $char;
589      $column++;      $self->{next_char} = ord $char;
590    
591        ($self->{line_prev}, $self->{column_prev})
592            = ($self->{line}, $self->{column});
593        $self->{column}++;
594            
595      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
596        $line++;        !!!cp ('j1');
597        $column = 0;        $self->{line}++;
598      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
599        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
600        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
601        $line++;        my $next = $input->getc;
602        $column = 0;        if ($next ne "\x0A") {
603      } elsif ($self->{next_input_character} > 0x10FFFF) {          $input->ungetc ($next);
604        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        }
605      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0x000A; # LF # MUST
606          $self->{line}++;
607          $self->{column} = 0;
608        } elsif ($self->{next_char} > 0x10FFFF) {
609          !!!cp ('j3');
610          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
611        } elsif ($self->{next_char} == 0x0000) { # NULL
612          !!!cp ('j4');
613        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
614        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
615        } elsif ($self->{next_char} <= 0x0008 or
616                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
617                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
618                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
619                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
620                 {
621                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
622                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
623                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
624                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
625                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
626                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
627                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
628                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
629                  0x10FFFE => 1, 0x10FFFF => 1,
630                 }->{$self->{next_char}}) {
631          !!!cp ('j5');
632          !!!parse-error (type => 'control char', level => $self->{must_level});
633    ## TODO: error type documentation
634      }      }
635    };    };
636    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
637    $self->{next_input_character} = -1;    $self->{next_char} = -1;
638    
639    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
640      my (%opt) = @_;      my (%opt) = @_;
641      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
642        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
643        warn "Parse error ($opt{type}) at line $line column $column\n";
644    };    };
645    $self->{parse_error} = sub {    $self->{parse_error} = sub {
646      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
647    };    };
648    
649    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 651  sub parse_string ($$$;$) {
651    $self->_construct_tree;    $self->_construct_tree;
652    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
653    
654      delete $self->{parse_error}; # remove loop
655    
656    return $self->{document};    return $self->{document};
657  } # parse_string  } # parse_char_stream
658    
659  sub new ($) {  sub new ($) {
660    my $class = shift;    my $class = shift;
661    my $self = bless {}, $class;    my $self = bless {
662    $self->{set_next_input_character} = sub {      must_level => 'm',
663      $self->{next_input_character} = -1;      should_level => 's',
664        good_level => 'w',
665        warn_level => 'w',
666        info_level => 'i',
667        unsupported_level => 'u',
668      }, $class;
669      $self->{set_next_char} = sub {
670        $self->{next_char} = -1;
671    };    };
672    $self->{parse_error} = sub {    $self->{parse_error} = sub {
673      #      #
674    };    };
675      $self->{change_encoding} = sub {
676        # if ($_[0] is a supported encoding) {
677        #   run "change the encoding" algorithm;
678        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
679        # }
680      };
681      $self->{application_cache_selection} = sub {
682        #
683      };
684    return $self;    return $self;
685  } # new  } # new
686    
687    sub CM_ENTITY () { 0b001 } # & markup in data
688    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
689    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
690    
691    sub PLAINTEXT_CONTENT_MODEL () { 0 }
692    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
693    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
694    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
695    
696    sub DATA_STATE () { 0 }
697    sub ENTITY_DATA_STATE () { 1 }
698    sub TAG_OPEN_STATE () { 2 }
699    sub CLOSE_TAG_OPEN_STATE () { 3 }
700    sub TAG_NAME_STATE () { 4 }
701    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
702    sub ATTRIBUTE_NAME_STATE () { 6 }
703    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
704    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
705    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
706    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
707    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
708    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
709    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
710    sub COMMENT_START_STATE () { 14 }
711    sub COMMENT_START_DASH_STATE () { 15 }
712    sub COMMENT_STATE () { 16 }
713    sub COMMENT_END_STATE () { 17 }
714    sub COMMENT_END_DASH_STATE () { 18 }
715    sub BOGUS_COMMENT_STATE () { 19 }
716    sub DOCTYPE_STATE () { 20 }
717    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
718    sub DOCTYPE_NAME_STATE () { 22 }
719    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
720    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
721    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
722    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
723    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
724    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
725    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
726    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
727    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
728    sub BOGUS_DOCTYPE_STATE () { 32 }
729    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
730    sub SELF_CLOSING_START_TAG_STATE () { 34 }
731    sub CDATA_BLOCK_STATE () { 35 }
732    
733    sub DOCTYPE_TOKEN () { 1 }
734    sub COMMENT_TOKEN () { 2 }
735    sub START_TAG_TOKEN () { 3 }
736    sub END_TAG_TOKEN () { 4 }
737    sub END_OF_FILE_TOKEN () { 5 }
738    sub CHARACTER_TOKEN () { 6 }
739    
740    sub AFTER_HTML_IMS () { 0b100 }
741    sub HEAD_IMS ()       { 0b1000 }
742    sub BODY_IMS ()       { 0b10000 }
743    sub BODY_TABLE_IMS () { 0b100000 }
744    sub TABLE_IMS ()      { 0b1000000 }
745    sub ROW_IMS ()        { 0b10000000 }
746    sub BODY_AFTER_IMS () { 0b100000000 }
747    sub FRAME_IMS ()      { 0b1000000000 }
748    sub SELECT_IMS ()     { 0b10000000000 }
749    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
750        ## NOTE: "in foreign content" insertion mode is special; it is combined
751        ## with the secondary insertion mode.  In this parser, they are stored
752        ## together in the bit-or'ed form.
753    
754    ## NOTE: "initial" and "before html" insertion modes have no constants.
755    
756    ## NOTE: "after after body" insertion mode.
757    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
758    
759    ## NOTE: "after after frameset" insertion mode.
760    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
761    
762    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
763    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
764    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
765    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
766    sub IN_BODY_IM () { BODY_IMS }
767    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
768    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
769    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
770    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
771    sub IN_TABLE_IM () { TABLE_IMS }
772    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
773    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
774    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
775    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
776    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
777    sub IN_COLUMN_GROUP_IM () { 0b10 }
778    
779  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
780    
781  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
782    my $self = shift;    my $self = shift;
783    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
784    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
785    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
786    undef $self->{current_attribute};    undef $self->{current_attribute};
787    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
788    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
789      delete $self->{self_closing};
790    $self->{char} = [];    $self->{char} = [];
791    # $self->{next_input_character}    # $self->{next_char}
792    !!!next-input-character;    !!!next-input-character;
793    $self->{token} = [];    $self->{token} = [];
794    # $self->{escape}    # $self->{escape}
795  } # _initialize_tokenizer  } # _initialize_tokenizer
796    
797  ## A token has:  ## A token has:
798  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
799  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
800  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
801  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
802  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
803  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
804  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
805  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
806    ##        ->{name}
807    ##        ->{value}
808    ##        ->{has_reference} == 1 or 0
809    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
810    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
811    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
812    ##     while the token is pushed back to the stack.
813    
814    ## ISSUE: "When a DOCTYPE token is created, its
815    ## <i>self-closing flag</i> must be unset (its other state is that it
816    ## be set), and its attributes list must be empty.": Wrong subject?
817    
818  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
819    
# Line 185  sub _initialize_tokenizer ($) { Line 823  sub _initialize_tokenizer ($) {
823  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
824  ## and removed from the list.  ## and removed from the list.
825    
826    ## NOTE: HTML5 "Writing HTML documents" section, applied to
827    ## documents and not to user agents and conformance checkers,
828    ## contains some requirements that are not detected by the
829    ## parsing algorithm:
830    ## - Some requirements on character encoding declarations. ## TODO
831    ## - "Elements MUST NOT contain content that their content model disallows."
832    ##   ... Some are parse error, some are not (will be reported by c.c.).
833    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
834    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
835    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
836    
837    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
838    ## be detected by the HTML5 parsing algorithm:
839    ## - Text,
840    
841  sub _get_next_token ($) {  sub _get_next_token ($) {
842    my $self = shift;    my $self = shift;
843    
844      if ($self->{self_closing}) {
845        !!!parse-error (type => 'nestc', token => $self->{current_token});
846        ## NOTE: The |self_closing| flag is only set by start tag token.
847        ## In addition, when a start tag token is emitted, it is always set to
848        ## |current_token|.
849        delete $self->{self_closing};
850      }
851    
852    if (@{$self->{token}}) {    if (@{$self->{token}}) {
853        $self->{self_closing} = $self->{token}->[0]->{self_closing};
854      return shift @{$self->{token}};      return shift @{$self->{token}};
855    }    }
856    
857    A: {    A: {
858      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
859        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
860          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
861              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
862            $self->{state} = 'entity data';            !!!cp (1);
863              $self->{state} = ENTITY_DATA_STATE;
864            !!!next-input-character;            !!!next-input-character;
865            redo A;            redo A;
866          } else {          } else {
867              !!!cp (2);
868            #            #
869          }          }
870        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
871          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
872            unless ($self->{escape}) {            unless ($self->{escape}) {
873              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
874                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
875                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
876                  !!!cp (3);
877                $self->{escape} = 1;                $self->{escape} = 1;
878                } else {
879                  !!!cp (4);
880              }              }
881              } else {
882                !!!cp (5);
883            }            }
884          }          }
885                    
886          #          #
887        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
888          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
889              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
890               not $self->{escape})) {               not $self->{escape})) {
891            $self->{state} = 'tag open';            !!!cp (6);
892              $self->{state} = TAG_OPEN_STATE;
893            !!!next-input-character;            !!!next-input-character;
894            redo A;            redo A;
895          } else {          } else {
896              !!!cp (7);
897            #            #
898          }          }
899        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
900          if ($self->{escape} and          if ($self->{escape} and
901              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
902               $self->{content_model_flag} eq 'CDATA')) {            if ($self->{prev_char}->[0] == 0x002D and # -
903            if ($self->{prev_input_character}->[0] == 0x002D and # -                $self->{prev_char}->[1] == 0x002D) { # -
904                $self->{prev_input_character}->[1] == 0x002D) { # -              !!!cp (8);
905              delete $self->{escape};              delete $self->{escape};
906              } else {
907                !!!cp (9);
908            }            }
909            } else {
910              !!!cp (10);
911          }          }
912                    
913          #          #
914        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
915          !!!emit ({type => 'end-of-file'});          !!!cp (11);
916            !!!emit ({type => END_OF_FILE_TOKEN,
917                      line => $self->{line}, column => $self->{column}});
918          last A; ## TODO: ok?          last A; ## TODO: ok?
919          } else {
920            !!!cp (12);
921        }        }
922        # Anything else        # Anything else
923        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
924                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
925                       line => $self->{line}, column => $self->{column},
926                      };
927        ## Stay in the data state        ## Stay in the data state
928        !!!next-input-character;        !!!next-input-character;
929    
930        !!!emit ($token);        !!!emit ($token);
931    
932        redo A;        redo A;
933      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
934        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
935    
936          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
937                
938        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
939    
940        $self->{state} = 'data';        $self->{state} = DATA_STATE;
941        # next-input-character is already done        # next-input-character is already done
942    
943        unless (defined $token) {        unless (defined $token) {
944          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
945            !!!emit ({type => CHARACTER_TOKEN, data => '&',
946                      line => $l, column => $c,
947                     });
948        } else {        } else {
949            !!!cp (14);
950          !!!emit ($token);          !!!emit ($token);
951        }        }
952    
953        redo A;        redo A;
954      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
955        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
956            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
957          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
958            !!!next-input-character;            !!!next-input-character;
959            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
960            redo A;            redo A;
961          } else {          } else {
962              !!!cp (16);
963            ## reconsume            ## reconsume
964            $self->{state} = 'data';            $self->{state} = DATA_STATE;
965    
966            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
967                        line => $self->{line_prev},
968                        column => $self->{column_prev},
969                       });
970    
971            redo A;            redo A;
972          }          }
973        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
974          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
975            $self->{state} = 'markup declaration open';            !!!cp (17);
976              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
977            !!!next-input-character;            !!!next-input-character;
978            redo A;            redo A;
979          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
980            $self->{state} = 'close tag open';            !!!cp (18);
981              $self->{state} = CLOSE_TAG_OPEN_STATE;
982            !!!next-input-character;            !!!next-input-character;
983            redo A;            redo A;
984          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
985                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
986              !!!cp (19);
987            $self->{current_token}            $self->{current_token}
988              = {type => 'start tag',              = {type => START_TAG_TOKEN,
989                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
990            $self->{state} = 'tag name';                 line => $self->{line_prev},
991                   column => $self->{column_prev}};
992              $self->{state} = TAG_NAME_STATE;
993            !!!next-input-character;            !!!next-input-character;
994            redo A;            redo A;
995          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
996                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
997            $self->{current_token} = {type => 'start tag',            !!!cp (20);
998                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
999            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1000                                        line => $self->{line_prev},
1001                                        column => $self->{column_prev}};
1002              $self->{state} = TAG_NAME_STATE;
1003            !!!next-input-character;            !!!next-input-character;
1004            redo A;            redo A;
1005          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1006            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1007            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1008                              line => $self->{line_prev},
1009                              column => $self->{column_prev});
1010              $self->{state} = DATA_STATE;
1011            !!!next-input-character;            !!!next-input-character;
1012    
1013            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1014                        line => $self->{line_prev},
1015                        column => $self->{column_prev},
1016                       });
1017    
1018            redo A;            redo A;
1019          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1020            !!!parse-error (type => 'pio');            !!!cp (22);
1021            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1022            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1023                              column => $self->{column_prev});
1024              $self->{state} = BOGUS_COMMENT_STATE;
1025              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1026                                        line => $self->{line_prev},
1027                                        column => $self->{column_prev},
1028                                       };
1029              ## $self->{next_char} is intentionally left as is
1030            redo A;            redo A;
1031          } else {          } else {
1032            !!!parse-error (type => 'bare stago');            !!!cp (23);
1033            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1034                              line => $self->{line_prev},
1035                              column => $self->{column_prev});
1036              $self->{state} = DATA_STATE;
1037            ## reconsume            ## reconsume
1038    
1039            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1040                        line => $self->{line_prev},
1041                        column => $self->{column_prev},
1042                       });
1043    
1044            redo A;            redo A;
1045          }          }
1046        } else {        } else {
1047          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1048        }        }
1049      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1050        if ($self->{content_model_flag} eq 'RCDATA' or        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1051            $self->{content_model_flag} eq 'CDATA') {        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1052          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1053    
1054            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1055            my @next_char;            my @next_char;
1056            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++) {
1057              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1058              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1059              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1060              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1061                  !!!cp (24);
1062                !!!next-input-character;                !!!next-input-character;
1063                next TAGNAME;                next TAGNAME;
1064              } else {              } else {
1065                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
1066                  $self->{next_char} = shift @next_char; # reconsume
1067                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1068                $self->{state} = 'data';                $self->{state} = DATA_STATE;
1069    
1070                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1071                            line => $l, column => $c,
1072                           });
1073        
1074                redo A;                redo A;
1075              }              }
1076            }            }
1077            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1078                
1079            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
1080                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
1081                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
1082                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
1083                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
1084                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
1085                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
1086                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
1087              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1088                $self->{next_char} = shift @next_char; # reconsume
1089              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1090              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1091              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1092                          line => $l, column => $c,
1093                         });
1094              redo A;              redo A;
1095            } else {            } else {
1096              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1097                $self->{next_char} = shift @next_char;
1098              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1099              # and consume...              # and consume...
1100            }            }
1101          } else {          } else {
1102            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1103              !!!cp (28);
1104            # next-input-character is already done            # next-input-character is already done
1105            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1106            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1107                        line => $l, column => $c,
1108                       });
1109            redo A;            redo A;
1110          }          }
1111        }        }
1112                
1113        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1114            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1115          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1116                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1117          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1118          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1119          redo A;                 line => $l, column => $c};
1120        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1121                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1122          $self->{current_token} = {type => 'end tag',          redo A;
1123                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1124          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1125          !!!next-input-character;          !!!cp (30);
1126          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1127        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1128          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1129          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1130            !!!next-input-character;
1131            redo A;
1132          } elsif ($self->{next_char} == 0x003E) { # >
1133            !!!cp (31);
1134            !!!parse-error (type => 'empty end tag',
1135                            line => $self->{line_prev}, ## "<" in "</>"
1136                            column => $self->{column_prev} - 1);
1137            $self->{state} = DATA_STATE;
1138          !!!next-input-character;          !!!next-input-character;
1139          redo A;          redo A;
1140        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1141            !!!cp (32);
1142          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1143          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1144          # reconsume          # reconsume
1145    
1146          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1147                      line => $l, column => $c,
1148                     });
1149    
1150          redo A;          redo A;
1151        } else {        } else {
1152            !!!cp (33);
1153          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1154          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1155          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1156          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1157        }                                    column => $self->{column_prev} - 1,
1158      } elsif ($self->{state} eq 'tag name') {                                   };
1159        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1160            $self->{next_input_character} == 0x000A or # LF          redo A;
1161            $self->{next_input_character} == 0x000B or # VT        }
1162            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1163            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1164          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1165          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1166          redo A;            $self->{next_char} == 0x000C or # FF
1167        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1168          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1169            $self->{current_token}->{first_start_tag}          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1170                = not defined $self->{last_emitted_start_tag_name};          !!!next-input-character;
1171            redo A;
1172          } elsif ($self->{next_char} == 0x003E) { # >
1173            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1174              !!!cp (35);
1175            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1176          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1177            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1178            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1179              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1180            }            #  !!! cp (36);
1181              #  !!! parse-error (type => 'end tag attribute');
1182              #} else {
1183                !!!cp (37);
1184              #}
1185          } else {          } else {
1186            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1187          }          }
1188          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1189          !!!next-input-character;          !!!next-input-character;
1190    
1191          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1192    
1193          redo A;          redo A;
1194        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1195                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1196          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1197            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1198            # start tag or end tag            # start tag or end tag
1199          ## Stay in this state          ## Stay in this state
1200          !!!next-input-character;          !!!next-input-character;
1201          redo A;          redo A;
1202        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1203          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1204          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1205            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1206            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1207          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1208            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1209            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1210              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1211            }            #  !!! cp (40);
1212              #  !!! parse-error (type => 'end tag attribute');
1213              #} else {
1214                !!!cp (41);
1215              #}
1216          } else {          } else {
1217            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1218          }          }
1219          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1220          # reconsume          # reconsume
1221    
1222          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1223    
1224          redo A;          redo A;
1225        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1226            !!!cp (42);
1227            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1228          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
1229          redo A;          redo A;
1230        } else {        } else {
1231          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1232            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1233            # start tag or end tag            # start tag or end tag
1234          ## Stay in the state          ## Stay in the state
1235          !!!next-input-character;          !!!next-input-character;
1236          redo A;          redo A;
1237        }        }
1238      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1239        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1240            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1241            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1242            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1243            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1244            !!!cp (45);
1245          ## Stay in the state          ## Stay in the state
1246          !!!next-input-character;          !!!next-input-character;
1247          redo A;          redo A;
1248        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1249          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1250            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1251            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1252          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1253            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1254            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1255                !!!cp (47);
1256              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1257              } else {
1258                !!!cp (48);
1259            }            }
1260          } else {          } else {
1261            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1262          }          }
1263          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1264          !!!next-input-character;          !!!next-input-character;
1265    
1266          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1267    
1268          redo A;          redo A;
1269        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1270                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1271          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1272                                value => ''};          $self->{current_attribute}
1273          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1274                   value => '',
1275                   line => $self->{line}, column => $self->{column}};
1276            $self->{state} = ATTRIBUTE_NAME_STATE;
1277            !!!next-input-character;
1278            redo A;
1279          } elsif ($self->{next_char} == 0x002F) { # /
1280            !!!cp (50);
1281            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1282          !!!next-input-character;          !!!next-input-character;
1283          redo A;          redo A;
1284        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1285          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1286          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1287            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1288            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1289          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1290            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1291            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1292                !!!cp (53);
1293              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1294              } else {
1295                !!!cp (54);
1296            }            }
1297          } else {          } else {
1298            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1299          }          }
1300          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1301          # reconsume          # reconsume
1302    
1303          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1304    
1305          redo A;          redo A;
1306        } else {        } else {
1307          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1308                                value => ''};               0x0022 => 1, # "
1309          $self->{state} = 'attribute name';               0x0027 => 1, # '
1310                 0x003D => 1, # =
1311                }->{$self->{next_char}}) {
1312              !!!cp (55);
1313              !!!parse-error (type => 'bad attribute name');
1314            } else {
1315              !!!cp (56);
1316            }
1317            $self->{current_attribute}
1318                = {name => chr ($self->{next_char}),
1319                   value => '',
1320                   line => $self->{line}, column => $self->{column}};
1321            $self->{state} = ATTRIBUTE_NAME_STATE;
1322          !!!next-input-character;          !!!next-input-character;
1323          redo A;          redo A;
1324        }        }
1325      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1326        my $before_leave = sub {        my $before_leave = sub {
1327          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1328              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1329            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1330              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1331            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1332          } else {          } else {
1333              !!!cp (58);
1334            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1335              = $self->{current_attribute};              = $self->{current_attribute};
1336          }          }
1337        }; # $before_leave        }; # $before_leave
1338    
1339        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1340            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1341            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1342            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1343            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1344            !!!cp (59);
1345          $before_leave->();          $before_leave->();
1346          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1347          !!!next-input-character;          !!!next-input-character;
1348          redo A;          redo A;
1349        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1350            !!!cp (60);
1351          $before_leave->();          $before_leave->();
1352          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1353          !!!next-input-character;          !!!next-input-character;
1354          redo A;          redo A;
1355        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1356          $before_leave->();          $before_leave->();
1357          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1358            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1359            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1360          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1361            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
1362              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1363            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1364              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1365            }            }
1366          } else {          } else {
1367            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1368          }          }
1369          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1370          !!!next-input-character;          !!!next-input-character;
1371    
1372          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1373    
1374          redo A;          redo A;
1375        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1376                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1377          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1378            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1379          ## Stay in the state          ## Stay in the state
1380          !!!next-input-character;          !!!next-input-character;
1381          redo A;          redo A;
1382        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1383            !!!cp (64);
1384          $before_leave->();          $before_leave->();
1385            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1386          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
1387          redo A;          redo A;
1388        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1389          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1390          $before_leave->();          $before_leave->();
1391          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1392            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1393            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1394          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1395            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1396            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1397                !!!cp (67);
1398              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1399              } else {
1400                ## NOTE: This state should never be reached.
1401                !!!cp (68);
1402            }            }
1403          } else {          } else {
1404            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1405          }          }
1406          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1407          # reconsume          # reconsume
1408    
1409          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1410    
1411          redo A;          redo A;
1412        } else {        } else {
1413          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1414                $self->{next_char} == 0x0027) { # '
1415              !!!cp (69);
1416              !!!parse-error (type => 'bad attribute name');
1417            } else {
1418              !!!cp (70);
1419            }
1420            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1421          ## Stay in the state          ## Stay in the state
1422          !!!next-input-character;          !!!next-input-character;
1423          redo A;          redo A;
1424        }        }
1425      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1426        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1427            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1428            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1429            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1430            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1431            !!!cp (71);
1432          ## Stay in the state          ## Stay in the state
1433          !!!next-input-character;          !!!next-input-character;
1434          redo A;          redo A;
1435        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1436          $self->{state} = 'before attribute value';          !!!cp (72);
1437            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1438          !!!next-input-character;          !!!next-input-character;
1439          redo A;          redo A;
1440        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1441          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1442            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1443            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1444          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1445            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1446            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1447                !!!cp (74);
1448              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1449              } else {
1450                ## NOTE: This state should never be reached.
1451                !!!cp (75);
1452            }            }
1453          } else {          } else {
1454            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1455          }          }
1456          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1457          !!!next-input-character;          !!!next-input-character;
1458    
1459          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1460    
1461          redo A;          redo A;
1462        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1463                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1464          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1465                                value => ''};          $self->{current_attribute}
1466          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1467                   value => '',
1468                   line => $self->{line}, column => $self->{column}};
1469            $self->{state} = ATTRIBUTE_NAME_STATE;
1470            !!!next-input-character;
1471            redo A;
1472          } elsif ($self->{next_char} == 0x002F) { # /
1473            !!!cp (77);
1474            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1475          !!!next-input-character;          !!!next-input-character;
1476          redo A;          redo A;
1477        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1478          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1479          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1480            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1481            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1482          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1483            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1484            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1485                !!!cp (80);
1486              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1487              } else {
1488                ## NOTE: This state should never be reached.
1489                !!!cp (81);
1490            }            }
1491          } else {          } else {
1492            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1493          }          }
1494          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1495          # reconsume          # reconsume
1496    
1497          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1498    
1499          redo A;          redo A;
1500        } else {        } else {
1501          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1502                                value => ''};          $self->{current_attribute}
1503          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char}),
1504                   value => '',
1505                   line => $self->{line}, column => $self->{column}};
1506            $self->{state} = ATTRIBUTE_NAME_STATE;
1507          !!!next-input-character;          !!!next-input-character;
1508          redo A;                  redo A;        
1509        }        }
1510      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1511        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1512            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1513            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1514            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1515            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1516            !!!cp (83);
1517          ## Stay in the state          ## Stay in the state
1518          !!!next-input-character;          !!!next-input-character;
1519          redo A;          redo A;
1520        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1521          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1522            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1523          !!!next-input-character;          !!!next-input-character;
1524          redo A;          redo A;
1525        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1526          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1527            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1528          ## reconsume          ## reconsume
1529          redo A;          redo A;
1530        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1531          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1532            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1533          !!!next-input-character;          !!!next-input-character;
1534          redo A;          redo A;
1535        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1536          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1537            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1538            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1539          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1540            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1541            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1542                !!!cp (88);
1543              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1544              } else {
1545                ## NOTE: This state should never be reached.
1546                !!!cp (89);
1547            }            }
1548          } else {          } else {
1549            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1550          }          }
1551          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1552          !!!next-input-character;          !!!next-input-character;
1553    
1554          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1555    
1556          redo A;          redo A;
1557        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1558          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1559          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1560            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1561            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1562          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1563            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1564            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1565                !!!cp (91);
1566              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1567              } else {
1568                ## NOTE: This state should never be reached.
1569                !!!cp (92);
1570            }            }
1571          } else {          } else {
1572            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1573          }          }
1574          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1575          ## reconsume          ## reconsume
1576    
1577          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1578    
1579          redo A;          redo A;
1580        } else {        } else {
1581          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1582          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1583              !!!parse-error (type => 'bad attribute value');
1584            } else {
1585              !!!cp (94);
1586            }
1587            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1588            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1589          !!!next-input-character;          !!!next-input-character;
1590          redo A;          redo A;
1591        }        }
1592      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1593        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1594          $self->{state} = 'before attribute name';          !!!cp (95);
1595            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1596          !!!next-input-character;          !!!next-input-character;
1597          redo A;          redo A;
1598        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1599          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1600          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1601            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1602          !!!next-input-character;          !!!next-input-character;
1603          redo A;          redo A;
1604        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1605          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1606          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1607            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1608            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1609          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1610            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1611            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1612                !!!cp (98);
1613              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1614              } else {
1615                ## NOTE: This state should never be reached.
1616                !!!cp (99);
1617            }            }
1618          } else {          } else {
1619            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1620          }          }
1621          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1622          ## reconsume          ## reconsume
1623    
1624          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1625    
1626          redo A;          redo A;
1627        } else {        } else {
1628          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1629            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1630          ## Stay in the state          ## Stay in the state
1631          !!!next-input-character;          !!!next-input-character;
1632          redo A;          redo A;
1633        }        }
1634      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1635        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1636          $self->{state} = 'before attribute name';          !!!cp (101);
1637            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1638          !!!next-input-character;          !!!next-input-character;
1639          redo A;          redo A;
1640        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1641          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1642          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1643            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1644          !!!next-input-character;          !!!next-input-character;
1645          redo A;          redo A;
1646        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1647          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1648          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1649            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1650            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1651          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1652            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1653            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1654                !!!cp (104);
1655              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1656              } else {
1657                ## NOTE: This state should never be reached.
1658                !!!cp (105);
1659            }            }
1660          } else {          } else {
1661            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1662          }          }
1663          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1664          ## reconsume          ## reconsume
1665    
1666          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1667    
1668          redo A;          redo A;
1669        } else {        } else {
1670          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1671            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1672          ## Stay in the state          ## Stay in the state
1673          !!!next-input-character;          !!!next-input-character;
1674          redo A;          redo A;
1675        }        }
1676      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1677        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1678            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1679            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1680            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1681            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1682          $self->{state} = 'before attribute name';          !!!cp (107);
1683          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1684          redo A;          !!!next-input-character;
1685        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1686          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1687          $self->{state} = 'entity in attribute value';          !!!cp (108);
1688          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1689          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1690        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1691          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1692            $self->{current_token}->{first_start_tag}        } elsif ($self->{next_char} == 0x003E) { # >
1693                = not defined $self->{last_emitted_start_tag_name};          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1694              !!!cp (109);
1695            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1696          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1697            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1698            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1699                !!!cp (110);
1700              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1701              } else {
1702                ## NOTE: This state should never be reached.
1703                !!!cp (111);
1704            }            }
1705          } else {          } else {
1706            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1707          }          }
1708          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1709          !!!next-input-character;          !!!next-input-character;
1710    
1711          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1712    
1713          redo A;          redo A;
1714        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1715          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1716          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1717            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1718            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1719          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1720            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1721            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1722                !!!cp (113);
1723              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1724              } else {
1725                ## NOTE: This state should never be reached.
1726                !!!cp (114);
1727            }            }
1728          } else {          } else {
1729            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1730          }          }
1731          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1732          ## reconsume          ## reconsume
1733    
1734          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1735    
1736          redo A;          redo A;
1737        } else {        } else {
1738          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1739                 0x0022 => 1, # "
1740                 0x0027 => 1, # '
1741                 0x003D => 1, # =
1742                }->{$self->{next_char}}) {
1743              !!!cp (115);
1744              !!!parse-error (type => 'bad attribute value');
1745            } else {
1746              !!!cp (116);
1747            }
1748            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1749          ## Stay in the state          ## Stay in the state
1750          !!!next-input-character;          !!!next-input-character;
1751          redo A;          redo A;
1752        }        }
1753      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1754        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1755              (1,
1756               $self->{last_attribute_value_state}
1757                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1758               $self->{last_attribute_value_state}
1759                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1760               -1);
1761    
1762        unless (defined $token) {        unless (defined $token) {
1763            !!!cp (117);
1764          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1765        } else {        } else {
1766            !!!cp (118);
1767          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1768            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1769          ## 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"
1770        }        }
1771    
1772        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1773        # next-input-character is already done        # next-input-character is already done
1774        redo A;        redo A;
1775      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1776          if ($self->{next_char} == 0x0009 or # HT
1777              $self->{next_char} == 0x000A or # LF
1778              $self->{next_char} == 0x000B or # VT
1779              $self->{next_char} == 0x000C or # FF
1780              $self->{next_char} == 0x0020) { # SP
1781            !!!cp (118);
1782            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1783            !!!next-input-character;
1784            redo A;
1785          } elsif ($self->{next_char} == 0x003E) { # >
1786            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1787              !!!cp (119);
1788              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1789            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1790              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1791              if ($self->{current_token}->{attributes}) {
1792                !!!cp (120);
1793                !!!parse-error (type => 'end tag attribute');
1794              } else {
1795                ## NOTE: This state should never be reached.
1796                !!!cp (121);
1797              }
1798            } else {
1799              die "$0: $self->{current_token}->{type}: Unknown token type";
1800            }
1801            $self->{state} = DATA_STATE;
1802            !!!next-input-character;
1803    
1804            !!!emit ($self->{current_token}); # start tag or end tag
1805    
1806            redo A;
1807          } elsif ($self->{next_char} == 0x002F) { # /
1808            !!!cp (122);
1809            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1810            !!!next-input-character;
1811            redo A;
1812          } else {
1813            !!!cp ('124.1');
1814            !!!parse-error (type => 'no space between attributes');
1815            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1816            ## reconsume
1817            redo A;
1818          }
1819        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1820          if ($self->{next_char} == 0x003E) { # >
1821            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1822              !!!cp ('124.2');
1823              !!!parse-error (type => 'nestc', token => $self->{current_token});
1824              ## TODO: Different type than slash in start tag
1825              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1826              if ($self->{current_token}->{attributes}) {
1827                !!!cp ('124.4');
1828                !!!parse-error (type => 'end tag attribute');
1829              } else {
1830                !!!cp ('124.5');
1831              }
1832              ## TODO: Test |<title></title/>|
1833            } else {
1834              !!!cp ('124.3');
1835              $self->{self_closing} = 1;
1836            }
1837    
1838            $self->{state} = DATA_STATE;
1839            !!!next-input-character;
1840    
1841            !!!emit ($self->{current_token}); # start tag or end tag
1842    
1843            redo A;
1844          } else {
1845            !!!cp ('124.4');
1846            !!!parse-error (type => 'nestc');
1847            ## TODO: This error type is wrong.
1848            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1849            ## Reconsume.
1850            redo A;
1851          }
1852        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1853        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1854                
1855        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1856          #my $token = {type => COMMENT_TOKEN, data => ''};
1857    
1858        BC: {        BC: {
1859          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1860            $self->{state} = 'data';            !!!cp (124);
1861              $self->{state} = DATA_STATE;
1862            !!!next-input-character;            !!!next-input-character;
1863    
1864            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1865    
1866            redo A;            redo A;
1867          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1868            $self->{state} = 'data';            !!!cp (125);
1869              $self->{state} = DATA_STATE;
1870            ## reconsume            ## reconsume
1871    
1872            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1873    
1874            redo A;            redo A;
1875          } else {          } else {
1876            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1877              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1878            !!!next-input-character;            !!!next-input-character;
1879            redo BC;            redo BC;
1880          }          }
1881        } # BC        } # BC
1882      } elsif ($self->{state} eq 'markup declaration open') {  
1883          die "$0: _get_next_token: unexpected case [BC]";
1884        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1885        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1886    
1887          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1888    
1889        my @next_char;        my @next_char;
1890        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1891                
1892        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1893          !!!next-input-character;          !!!next-input-character;
1894          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1895          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1896            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1897            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1898                                        line => $l, column => $c,
1899                                       };
1900              $self->{state} = COMMENT_START_STATE;
1901            !!!next-input-character;            !!!next-input-character;
1902            redo A;            redo A;
1903            } else {
1904              !!!cp (128);
1905          }          }
1906        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1907                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1908          !!!next-input-character;          !!!next-input-character;
1909          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1910          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1911              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1912            !!!next-input-character;            !!!next-input-character;
1913            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1914            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1915                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1916              !!!next-input-character;              !!!next-input-character;
1917              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1918              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1919                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1920                !!!next-input-character;                !!!next-input-character;
1921                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1922                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1923                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1924                  !!!next-input-character;                  !!!next-input-character;
1925                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1926                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1927                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1928                    !!!next-input-character;                    !!!next-input-character;
1929                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1930                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1931                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1932                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1933                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1934                        $self->{state} = DOCTYPE_STATE;
1935                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1936                                                  quirks => 1,
1937                                                  line => $l, column => $c,
1938                                                 };
1939                      !!!next-input-character;                      !!!next-input-character;
1940                      redo A;                      redo A;
1941                      } else {
1942                        !!!cp (130);
1943                    }                    }
1944                    } else {
1945                      !!!cp (131);
1946                  }                  }
1947                  } else {
1948                    !!!cp (132);
1949                }                }
1950                } else {
1951                  !!!cp (133);
1952              }              }
1953              } else {
1954                !!!cp (134);
1955            }            }
1956            } else {
1957              !!!cp (135);
1958          }          }
1959          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1960                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1961                   $self->{next_char} == 0x005B) { # [
1962            !!!next-input-character;
1963            push @next_char, $self->{next_char};
1964            if ($self->{next_char} == 0x0043) { # C
1965              !!!next-input-character;
1966              push @next_char, $self->{next_char};
1967              if ($self->{next_char} == 0x0044) { # D
1968                !!!next-input-character;
1969                push @next_char, $self->{next_char};
1970                if ($self->{next_char} == 0x0041) { # A
1971                  !!!next-input-character;
1972                  push @next_char, $self->{next_char};
1973                  if ($self->{next_char} == 0x0054) { # T
1974                    !!!next-input-character;
1975                    push @next_char, $self->{next_char};
1976                    if ($self->{next_char} == 0x0041) { # A
1977                      !!!next-input-character;
1978                      push @next_char, $self->{next_char};
1979                      if ($self->{next_char} == 0x005B) { # [
1980                        !!!cp (135.1);
1981                        $self->{state} = CDATA_BLOCK_STATE;
1982                        !!!next-input-character;
1983                        redo A;
1984                      } else {
1985                        !!!cp (135.2);
1986                      }
1987                    } else {
1988                      !!!cp (135.3);
1989                    }
1990                  } else {
1991                    !!!cp (135.4);                
1992                  }
1993                } else {
1994                  !!!cp (135.5);
1995                }
1996              } else {
1997                !!!cp (135.6);
1998              }
1999            } else {
2000              !!!cp (135.7);
2001            }
2002          } else {
2003            !!!cp (136);
2004        }        }
2005    
2006        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
2007        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2008        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2009        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
2010          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2011                                    line => $l, column => $c,
2012                                   };
2013        redo A;        redo A;
2014                
2015        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2016        ## 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?
2017      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
2018        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2019          $self->{state} = 'comment start dash';          !!!cp (137);
2020            $self->{state} = COMMENT_START_DASH_STATE;
2021          !!!next-input-character;          !!!next-input-character;
2022          redo A;          redo A;
2023        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2024            !!!cp (138);
2025          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2026          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2027          !!!next-input-character;          !!!next-input-character;
2028    
2029          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2030    
2031          redo A;          redo A;
2032        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2033            !!!cp (139);
2034          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2035          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2036          ## reconsume          ## reconsume
2037    
2038          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2039    
2040          redo A;          redo A;
2041        } else {        } else {
2042            !!!cp (140);
2043          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2044              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2045          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2046          !!!next-input-character;          !!!next-input-character;
2047          redo A;          redo A;
2048        }        }
2049      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2050        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2051          $self->{state} = 'comment end';          !!!cp (141);
2052            $self->{state} = COMMENT_END_STATE;
2053          !!!next-input-character;          !!!next-input-character;
2054          redo A;          redo A;
2055        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2056            !!!cp (142);
2057          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2058          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2059          !!!next-input-character;          !!!next-input-character;
2060    
2061          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2062    
2063          redo A;          redo A;
2064        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2065            !!!cp (143);
2066          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2067          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2068          ## reconsume          ## reconsume
2069    
2070          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2071    
2072          redo A;          redo A;
2073        } else {        } else {
2074            !!!cp (144);
2075          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2076              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2077          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2078          !!!next-input-character;          !!!next-input-character;
2079          redo A;          redo A;
2080        }        }
2081      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
2082        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2083          $self->{state} = 'comment end dash';          !!!cp (145);
2084            $self->{state} = COMMENT_END_DASH_STATE;
2085          !!!next-input-character;          !!!next-input-character;
2086          redo A;          redo A;
2087        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2088            !!!cp (146);
2089          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2090          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2091          ## reconsume          ## reconsume
2092    
2093          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2094    
2095          redo A;          redo A;
2096        } else {        } else {
2097          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2098            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2099          ## Stay in the state          ## Stay in the state
2100          !!!next-input-character;          !!!next-input-character;
2101          redo A;          redo A;
2102        }        }
2103      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2104        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2105          $self->{state} = 'comment end';          !!!cp (148);
2106            $self->{state} = COMMENT_END_STATE;
2107          !!!next-input-character;          !!!next-input-character;
2108          redo A;          redo A;
2109        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2110            !!!cp (149);
2111          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2112          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2113          ## reconsume          ## reconsume
2114    
2115          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2116    
2117          redo A;          redo A;
2118        } else {        } else {
2119          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2120          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2121            $self->{state} = COMMENT_STATE;
2122          !!!next-input-character;          !!!next-input-character;
2123          redo A;          redo A;
2124        }        }
2125      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2126        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2127          $self->{state} = 'data';          !!!cp (151);
2128            $self->{state} = DATA_STATE;
2129          !!!next-input-character;          !!!next-input-character;
2130    
2131          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2132    
2133          redo A;          redo A;
2134        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2135          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2136            !!!parse-error (type => 'dash in comment',
2137                            line => $self->{line_prev},
2138                            column => $self->{column_prev});
2139          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2140          ## Stay in the state          ## Stay in the state
2141          !!!next-input-character;          !!!next-input-character;
2142          redo A;          redo A;
2143        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2144            !!!cp (153);
2145          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2146          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2147          ## reconsume          ## reconsume
2148    
2149          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2150    
2151          redo A;          redo A;
2152        } else {        } else {
2153          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2154          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2155          $self->{state} = 'comment';                          line => $self->{line_prev},
2156                            column => $self->{column_prev});
2157            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2158            $self->{state} = COMMENT_STATE;
2159          !!!next-input-character;          !!!next-input-character;
2160          redo A;          redo A;
2161        }        }
2162      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2163        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2164            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2165            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2166            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2167            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2168          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2169            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2170          !!!next-input-character;          !!!next-input-character;
2171          redo A;          redo A;
2172        } else {        } else {
2173            !!!cp (156);
2174          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2175          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2176          ## reconsume          ## reconsume
2177          redo A;          redo A;
2178        }        }
2179      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2180        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2181            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2182            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2183            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2184            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2185            !!!cp (157);
2186          ## Stay in the state          ## Stay in the state
2187          !!!next-input-character;          !!!next-input-character;
2188          redo A;          redo A;
2189        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2190            !!!cp (158);
2191          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2192          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2193          !!!next-input-character;          !!!next-input-character;
2194    
2195          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2196    
2197          redo A;          redo A;
2198        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2199            !!!cp (159);
2200          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2201          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2202          ## reconsume          ## reconsume
2203    
2204          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2205    
2206          redo A;          redo A;
2207        } else {        } else {
2208          $self->{current_token}          !!!cp (160);
2209              = {type => 'DOCTYPE',          $self->{current_token}->{name} = chr $self->{next_char};
2210                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2211  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2212          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2213          !!!next-input-character;          !!!next-input-character;
2214          redo A;          redo A;
2215        }        }
2216      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2217  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2218        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2219            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2220            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2221            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2222            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2223          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2224            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2225          !!!next-input-character;          !!!next-input-character;
2226          redo A;          redo A;
2227        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2228          $self->{state} = 'data';          !!!cp (162);
2229            $self->{state} = DATA_STATE;
2230          !!!next-input-character;          !!!next-input-character;
2231    
2232          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2233    
2234          redo A;          redo A;
2235        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2236            !!!cp (163);
2237          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2238          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2239          ## reconsume          ## reconsume
2240    
2241          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2242          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2243    
2244          redo A;          redo A;
2245        } else {        } else {
2246            !!!cp (164);
2247          $self->{current_token}->{name}          $self->{current_token}->{name}
2248            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2249          ## Stay in the state          ## Stay in the state
2250          !!!next-input-character;          !!!next-input-character;
2251          redo A;          redo A;
2252        }        }
2253      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2254        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2255            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2256            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2257            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2258            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2259            !!!cp (165);
2260          ## Stay in the state          ## Stay in the state
2261          !!!next-input-character;          !!!next-input-character;
2262          redo A;          redo A;
2263        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2264          $self->{state} = 'data';          !!!cp (166);
2265            $self->{state} = DATA_STATE;
2266          !!!next-input-character;          !!!next-input-character;
2267    
2268          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2269    
2270          redo A;          redo A;
2271        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2272            !!!cp (167);
2273          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2274          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2275          ## reconsume          ## reconsume
2276    
2277          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2278          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2279    
2280          redo A;          redo A;
2281        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2282                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2283          !!!next-input-character;          !!!next-input-character;
2284          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2285              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2286            !!!next-input-character;            !!!next-input-character;
2287            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2288                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2289              !!!next-input-character;              !!!next-input-character;
2290              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2291                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2292                !!!next-input-character;                !!!next-input-character;
2293                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2294                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2295                  !!!next-input-character;                  !!!next-input-character;
2296                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2297                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2298                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
2299                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2300                    !!!next-input-character;                    !!!next-input-character;
2301                    redo A;                    redo A;
2302                    } else {
2303                      !!!cp (169);
2304                  }                  }
2305                  } else {
2306                    !!!cp (170);
2307                }                }
2308                } else {
2309                  !!!cp (171);
2310              }              }
2311              } else {
2312                !!!cp (172);
2313            }            }
2314            } else {
2315              !!!cp (173);
2316          }          }
2317    
2318          #          #
2319        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2320                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2321          !!!next-input-character;          !!!next-input-character;
2322          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2323              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2324            !!!next-input-character;            !!!next-input-character;
2325            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2326                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2327              !!!next-input-character;              !!!next-input-character;
2328              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2329                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2330                !!!next-input-character;                !!!next-input-character;
2331                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2332                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2333                  !!!next-input-character;                  !!!next-input-character;
2334                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2335                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2336                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
2337                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2338                    !!!next-input-character;                    !!!next-input-character;
2339                    redo A;                    redo A;
2340                    } else {
2341                      !!!cp (175);
2342                  }                  }
2343                  } else {
2344                    !!!cp (176);
2345                }                }
2346                } else {
2347                  !!!cp (177);
2348              }              }
2349              } else {
2350                !!!cp (178);
2351            }            }
2352            } else {
2353              !!!cp (179);
2354          }          }
2355    
2356          #          #
2357        } else {        } else {
2358            !!!cp (180);
2359          !!!next-input-character;          !!!next-input-character;
2360          #          #
2361        }        }
2362    
2363        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2364        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
2365    
2366          $self->{state} = BOGUS_DOCTYPE_STATE;
2367        # next-input-character is already done        # next-input-character is already done
2368        redo A;        redo A;
2369      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2370        if ({        if ({
2371              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2372              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2373            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2374            !!!cp (181);
2375          ## Stay in the state          ## Stay in the state
2376          !!!next-input-character;          !!!next-input-character;
2377          redo A;          redo A;
2378        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2379            !!!cp (182);
2380          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2381          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2382          !!!next-input-character;          !!!next-input-character;
2383          redo A;          redo A;
2384        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2385            !!!cp (183);
2386          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2387          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2388          !!!next-input-character;          !!!next-input-character;
2389          redo A;          redo A;
2390        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2391            !!!cp (184);
2392          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2393    
2394          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2395          !!!next-input-character;          !!!next-input-character;
2396    
2397          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2398          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2399    
2400          redo A;          redo A;
2401        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2402            !!!cp (185);
2403          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2404    
2405          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2406          ## reconsume          ## reconsume
2407    
2408          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2409          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2410    
2411          redo A;          redo A;
2412        } else {        } else {
2413            !!!cp (186);
2414          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2415          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2416    
2417            $self->{state} = BOGUS_DOCTYPE_STATE;
2418          !!!next-input-character;          !!!next-input-character;
2419          redo A;          redo A;
2420        }        }
2421      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2422        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2423          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2424            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2425          !!!next-input-character;          !!!next-input-character;
2426          redo A;          redo A;
2427        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2428            !!!cp (188);
2429          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2430    
2431          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2432            !!!next-input-character;
2433    
2434            $self->{current_token}->{quirks} = 1;
2435            !!!emit ($self->{current_token}); # DOCTYPE
2436    
2437            redo A;
2438          } elsif ($self->{next_char} == -1) {
2439            !!!cp (189);
2440            !!!parse-error (type => 'unclosed PUBLIC literal');
2441    
2442            $self->{state} = DATA_STATE;
2443          ## reconsume          ## reconsume
2444    
2445          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2446          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2447    
2448          redo A;          redo A;
2449        } else {        } else {
2450            !!!cp (190);
2451          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2452              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2453          ## Stay in the state          ## Stay in the state
2454          !!!next-input-character;          !!!next-input-character;
2455          redo A;          redo A;
2456        }        }
2457      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2458        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2459          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2460            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2461            !!!next-input-character;
2462            redo A;
2463          } elsif ($self->{next_char} == 0x003E) { # >
2464            !!!cp (192);
2465            !!!parse-error (type => 'unclosed PUBLIC literal');
2466    
2467            $self->{state} = DATA_STATE;
2468          !!!next-input-character;          !!!next-input-character;
2469    
2470            $self->{current_token}->{quirks} = 1;
2471            !!!emit ($self->{current_token}); # DOCTYPE
2472    
2473          redo A;          redo A;
2474        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2475            !!!cp (193);
2476          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2477    
2478          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2479          ## reconsume          ## reconsume
2480    
2481          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2482          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2483    
2484          redo A;          redo A;
2485        } else {        } else {
2486            !!!cp (194);
2487          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2488              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2489          ## Stay in the state          ## Stay in the state
2490          !!!next-input-character;          !!!next-input-character;
2491          redo A;          redo A;
2492        }        }
2493      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2494        if ({        if ({
2495              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2496              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2497            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2498            !!!cp (195);
2499          ## Stay in the state          ## Stay in the state
2500          !!!next-input-character;          !!!next-input-character;
2501          redo A;          redo A;
2502        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2503            !!!cp (196);
2504          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2505          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2506          !!!next-input-character;          !!!next-input-character;
2507          redo A;          redo A;
2508        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2509            !!!cp (197);
2510          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2511          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2512          !!!next-input-character;          !!!next-input-character;
2513          redo A;          redo A;
2514        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2515          $self->{state} = 'data';          !!!cp (198);
2516            $self->{state} = DATA_STATE;
2517          !!!next-input-character;          !!!next-input-character;
2518    
2519          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2520    
2521          redo A;          redo A;
2522        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2523            !!!cp (199);
2524          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2525    
2526          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2527          ## reconsume          ## reconsume
2528    
2529          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2530          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2531    
2532          redo A;          redo A;
2533        } else {        } else {
2534            !!!cp (200);
2535          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2536          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2537    
2538            $self->{state} = BOGUS_DOCTYPE_STATE;
2539          !!!next-input-character;          !!!next-input-character;
2540          redo A;          redo A;
2541        }        }
2542      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2543        if ({        if ({
2544              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2545              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2546            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2547            !!!cp (201);
2548          ## Stay in the state          ## Stay in the state
2549          !!!next-input-character;          !!!next-input-character;
2550          redo A;          redo A;
2551        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2552            !!!cp (202);
2553          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2554          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2555          !!!next-input-character;          !!!next-input-character;
2556          redo A;          redo A;
2557        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2558            !!!cp (203);
2559          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2560          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2561          !!!next-input-character;          !!!next-input-character;
2562          redo A;          redo A;
2563        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2564            !!!cp (204);
2565          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2566          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2567          !!!next-input-character;          !!!next-input-character;
2568    
2569          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2570          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2571    
2572          redo A;          redo A;
2573        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2574            !!!cp (205);
2575          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2576    
2577          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2578          ## reconsume          ## reconsume
2579    
2580          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2581          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2582    
2583          redo A;          redo A;
2584        } else {        } else {
2585            !!!cp (206);
2586          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2587          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2588    
2589            $self->{state} = BOGUS_DOCTYPE_STATE;
2590          !!!next-input-character;          !!!next-input-character;
2591          redo A;          redo A;
2592        }        }
2593      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2594        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2595          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2596            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2597          !!!next-input-character;          !!!next-input-character;
2598          redo A;          redo A;
2599        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2600            !!!cp (208);
2601            !!!parse-error (type => 'unclosed PUBLIC literal');
2602    
2603            $self->{state} = DATA_STATE;
2604            !!!next-input-character;
2605    
2606            $self->{current_token}->{quirks} = 1;
2607            !!!emit ($self->{current_token}); # DOCTYPE
2608    
2609            redo A;
2610          } elsif ($self->{next_char} == -1) {
2611            !!!cp (209);
2612          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2613    
2614          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2615          ## reconsume          ## reconsume
2616    
2617          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2618          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2619    
2620          redo A;          redo A;
2621        } else {        } else {
2622            !!!cp (210);
2623          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2624              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2625          ## Stay in the state          ## Stay in the state
2626          !!!next-input-character;          !!!next-input-character;
2627          redo A;          redo A;
2628        }        }
2629      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2630        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2631          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2632            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2633          !!!next-input-character;          !!!next-input-character;
2634          redo A;          redo A;
2635        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2636            !!!cp (212);
2637            !!!parse-error (type => 'unclosed PUBLIC literal');
2638    
2639            $self->{state} = DATA_STATE;
2640            !!!next-input-character;
2641    
2642            $self->{current_token}->{quirks} = 1;
2643            !!!emit ($self->{current_token}); # DOCTYPE
2644    
2645            redo A;
2646          } elsif ($self->{next_char} == -1) {
2647            !!!cp (213);
2648          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2649    
2650          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2651          ## reconsume          ## reconsume
2652    
2653          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2654          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2655    
2656          redo A;          redo A;
2657        } else {        } else {
2658            !!!cp (214);
2659          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2660              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2661          ## Stay in the state          ## Stay in the state
2662          !!!next-input-character;          !!!next-input-character;
2663          redo A;          redo A;
2664        }        }
2665      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2666        if ({        if ({
2667              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2668              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2669            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2670            !!!cp (215);
2671          ## Stay in the state          ## Stay in the state
2672          !!!next-input-character;          !!!next-input-character;
2673          redo A;          redo A;
2674        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2675          $self->{state} = 'data';          !!!cp (216);
2676            $self->{state} = DATA_STATE;
2677          !!!next-input-character;          !!!next-input-character;
2678    
2679          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2680    
2681          redo A;          redo A;
2682        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2683            !!!cp (217);
2684          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2685    
2686          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2687          ## reconsume          ## reconsume
2688    
2689          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2690          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2691    
2692          redo A;          redo A;
2693        } else {        } else {
2694            !!!cp (218);
2695          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2696          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2697    
2698            $self->{state} = BOGUS_DOCTYPE_STATE;
2699          !!!next-input-character;          !!!next-input-character;
2700          redo A;          redo A;
2701        }        }
2702      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2703        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2704          $self->{state} = 'data';          !!!cp (219);
2705            $self->{state} = DATA_STATE;
2706          !!!next-input-character;          !!!next-input-character;
2707    
         delete $self->{current_token}->{correct};  
2708          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2709    
2710          redo A;          redo A;
2711        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2712            !!!cp (220);
2713          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2714          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2715          ## reconsume          ## reconsume
2716    
         delete $self->{current_token}->{correct};  
2717          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2718    
2719          redo A;          redo A;
2720        } else {        } else {
2721            !!!cp (221);
2722          ## Stay in the state          ## Stay in the state
2723          !!!next-input-character;          !!!next-input-character;
2724          redo A;          redo A;
2725        }        }
2726        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2727          my $s = '';
2728          
2729          my ($l, $c) = ($self->{line}, $self->{column});
2730    
2731          CS: while ($self->{next_char} != -1) {
2732            if ($self->{next_char} == 0x005D) { # ]
2733              !!!next-input-character;
2734              if ($self->{next_char} == 0x005D) { # ]
2735                !!!next-input-character;
2736                MDC: {
2737                  if ($self->{next_char} == 0x003E) { # >
2738                    !!!cp (221.1);
2739                    !!!next-input-character;
2740                    last CS;
2741                  } elsif ($self->{next_char} == 0x005D) { # ]
2742                    !!!cp (221.2);
2743                    $s .= ']';
2744                    !!!next-input-character;
2745                    redo MDC;
2746                  } else {
2747                    !!!cp (221.3);
2748                    $s .= ']]';
2749                    #
2750                  }
2751                } # MDC
2752              } else {
2753                !!!cp (221.4);
2754                $s .= ']';
2755                #
2756              }
2757            } else {
2758              !!!cp (221.5);
2759              #
2760            }
2761            $s .= chr $self->{next_char};
2762            !!!next-input-character;
2763          } # CS
2764    
2765          $self->{state} = DATA_STATE;
2766          ## next-input-character done or EOF, which is reconsumed.
2767    
2768          if (length $s) {
2769            !!!cp (221.6);
2770            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2771                      line => $l, column => $c});
2772          } else {
2773            !!!cp (221.7);
2774          }
2775    
2776          redo A;
2777    
2778          ## ISSUE: "text tokens" in spec.
2779          ## TODO: Streaming support
2780      } else {      } else {
2781        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2782      }      }
# Line 1606  sub _get_next_token ($) { Line 2785  sub _get_next_token ($) {
2785    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2786  } # _get_next_token  } # _get_next_token
2787    
2788  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2789    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2790    
2791      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2792    
2793    if ({    if ({
2794         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2795         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2796        }->{$self->{next_input_character}}) {         $additional => 1,
2797          }->{$self->{next_char}}) {
2798        !!!cp (1001);
2799      ## Don't consume      ## Don't consume
2800      ## No error      ## No error
2801      return undef;      return undef;
2802    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2803      !!!next-input-character;      !!!next-input-character;
2804      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2805          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2806        my $code;        my $code;
2807        X: {        X: {
2808          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2809          !!!next-input-character;          !!!next-input-character;
2810          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2811              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2812              !!!cp (1002);
2813            $code ||= 0;            $code ||= 0;
2814            $code *= 0x10;            $code *= 0x10;
2815            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2816            redo X;            redo X;
2817          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2818                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2819              !!!cp (1003);
2820            $code ||= 0;            $code ||= 0;
2821            $code *= 0x10;            $code *= 0x10;
2822            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2823            redo X;            redo X;
2824          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2825                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2826              !!!cp (1004);
2827            $code ||= 0;            $code ||= 0;
2828            $code *= 0x10;            $code *= 0x10;
2829            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2830            redo X;            redo X;
2831          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2832            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2833            $self->{next_input_character} = 0x0023; # #            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2834            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char, $self->{next_char});
2835              $self->{next_char} = 0x0023; # #
2836            return undef;            return undef;
2837          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2838              !!!cp (1006);
2839            !!!next-input-character;            !!!next-input-character;
2840          } else {          } else {
2841            !!!parse-error (type => 'no refc');            !!!cp (1007);
2842              !!!parse-error (type => 'no refc', line => $l, column => $c);
2843          }          }
2844    
2845          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2846            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2847              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2848            $code = 0xFFFD;            $code = 0xFFFD;
2849          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2850            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2851              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2852            $code = 0xFFFD;            $code = 0xFFFD;
2853          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2854            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2855              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2856            $code = 0x000A;            $code = 0x000A;
2857          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2858            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2859              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2860            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2861          }          }
2862    
2863          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2864                    has_reference => 1,
2865                    line => $l, column => $c,
2866                   };
2867        } # X        } # X
2868      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2869               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2870        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2871        !!!next-input-character;        !!!next-input-character;
2872                
2873        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2874                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2875            !!!cp (1012);
2876          $code *= 10;          $code *= 10;
2877          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2878                    
2879          !!!next-input-character;          !!!next-input-character;
2880        }        }
2881    
2882        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2883            !!!cp (1013);
2884          !!!next-input-character;          !!!next-input-character;
2885        } else {        } else {
2886          !!!parse-error (type => 'no refc');          !!!cp (1014);
2887            !!!parse-error (type => 'no refc', line => $l, column => $c);
2888        }        }
2889    
2890        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2891          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2892            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2893          $code = 0xFFFD;          $code = 0xFFFD;
2894        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2895          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2896            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2897          $code = 0xFFFD;          $code = 0xFFFD;
2898        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2899          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2900            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2901          $code = 0x000A;          $code = 0x000A;
2902        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2903          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2904            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2905          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2906        }        }
2907                
2908        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2909                  line => $l, column => $c,
2910                 };
2911      } else {      } else {
2912        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2913        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2914        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2915          $self->{next_char} = 0x0023; # #
2916        return undef;        return undef;
2917      }      }
2918    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2919              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2920             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2921              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2922      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2923      !!!next-input-character;      !!!next-input-character;
2924    
2925      my $value = $entity_name;      my $value = $entity_name;
2926      my $match;      my $match = 0;
2927      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2928      our $EntityChar;      our $EntityChar;
2929    
2930      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2931             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2932             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2933               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2934              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2935               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2936              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2937               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2938              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2939        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2940        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2941          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2942              !!!cp (1020);
2943            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2944            $match = 1;            $match = 1;
2945            !!!next-input-character;            !!!next-input-character;
2946            last;            last;
2947          } elsif (not $in_attr) {          } else {
2948              !!!cp (1021);
2949            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2950            $match = -1;            $match = -1;
2951          } else {            !!!next-input-character;
           $value .= chr $self->{next_input_character};  
2952          }          }
2953        } else {        } else {
2954          $value .= chr $self->{next_input_character};          !!!cp (1022);
2955            $value .= chr $self->{next_char};
2956            $match *= 2;
2957            !!!next-input-character;
2958        }        }
       !!!next-input-character;  
2959      }      }
2960            
2961      if ($match > 0) {      if ($match > 0) {
2962        return {type => 'character', data => $value};        !!!cp (1023);
2963          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2964                  line => $l, column => $c,
2965                 };
2966      } elsif ($match < 0) {      } elsif ($match < 0) {
2967        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2968        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
2969            !!!cp (1024);
2970            return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2971                    line => $l, column => $c,
2972                   };
2973          } else {
2974            !!!cp (1025);
2975            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2976                    line => $l, column => $c,
2977                   };
2978          }
2979      } else {      } else {
2980        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2981        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2982        return {type => 'character', data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
2983          return {type => CHARACTER_TOKEN, data => '&'.$value,
2984                  line => $l, column => $c,
2985                 };
2986      }      }
2987    } else {    } else {
2988        !!!cp (1027);
2989      ## no characters are consumed      ## no characters are consumed
2990      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2991      return undef;      return undef;
2992    }    }
2993  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1799  sub _construct_tree ($) { Line 3025  sub _construct_tree ($) {
3025        
3026    !!!next-token;    !!!next-token;
3027    
   $self->{insertion_mode} = 'before head';  
3028    undef $self->{form_element};    undef $self->{form_element};
3029    undef $self->{head_element};    undef $self->{head_element};
3030    $self->{open_elements} = [];    $self->{open_elements} = [];
3031    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3032    
3033      ## NOTE: The "initial" insertion mode.
3034    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3035    
3036      ## NOTE: The "before html" insertion mode.
3037    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3038      $self->{insertion_mode} = BEFORE_HEAD_IM;
3039    
3040      ## NOTE: The "before head" insertion mode and so on.
3041    $self->_tree_construction_main;    $self->_tree_construction_main;
3042  } # _construct_tree  } # _construct_tree
3043    
3044  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3045    my $self = shift;    my $self = shift;
3046    
3047      ## NOTE: "initial" insertion mode
3048    
3049    INITIAL: {    INITIAL: {
3050      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
3051        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3052        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
3053        ## language.        ## language.
# Line 1823  sub _tree_construction_initial ($) { Line 3057  sub _tree_construction_initial ($) {
3057        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3058            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3059            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3060          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3061            !!!parse-error (type => 'not HTML5', token => $token);
3062        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3063            !!!cp ('t2');
3064          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3065          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3066          } else {
3067            !!!cp ('t3');
3068        }        }
3069                
3070        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3071          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3072          ## NOTE: Default value for both |public_id| and |system_id| attributes
3073          ## are empty strings, so that we don't set any value in missing cases.
3074        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3075            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3076        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1839  sub _tree_construction_initial ($) { Line 3079  sub _tree_construction_initial ($) {
3079        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3080        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3081                
3082        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3083            !!!cp ('t4');
3084          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3085        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3086          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 1893  sub _tree_construction_initial ($) { Line 3134  sub _tree_construction_initial ($) {
3134            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
3135            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
3136            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
3137              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
3138              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
3139              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
3140            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
3141            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
3142            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 1915  sub _tree_construction_initial ($) { Line 3159  sub _tree_construction_initial ($) {
3159            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
3160            "HTML" => 1,            "HTML" => 1,
3161          }->{$pubid}) {          }->{$pubid}) {
3162              !!!cp ('t5');
3163            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3164          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
3165                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
3166            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3167                !!!cp ('t6');
3168              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3169            } else {            } else {
3170                !!!cp ('t7');
3171              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3172            }            }
3173          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
3174                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
3175              !!!cp ('t8');
3176            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3177            } else {
3178              !!!cp ('t9');
3179          }          }
3180          } else {
3181            !!!cp ('t10');
3182        }        }
3183        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3184          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3185          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3186          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") {
3187              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
3188            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3189              !!!cp ('t11');
3190            } else {
3191              !!!cp ('t12');
3192          }          }
3193          } else {
3194            !!!cp ('t13');
3195        }        }
3196                
3197        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3198        !!!next-token;        !!!next-token;
3199        return;        return;
3200      } elsif ({      } elsif ({
3201                'start tag' => 1,                START_TAG_TOKEN, 1,
3202                'end tag' => 1,                END_TAG_TOKEN, 1,
3203                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
3204               }->{$token->{type}}) {               }->{$token->{type}}) {
3205        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3206          !!!parse-error (type => 'no DOCTYPE', token => $token);
3207        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3208        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3209        ## reprocess        ## reprocess
3210          !!!ack-later;
3211        return;        return;
3212      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3213        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3214          ## Ignore the token          ## Ignore the token
3215    
3216          unless (length $token->{data}) {          unless (length $token->{data}) {
3217            ## Stay in the phase            !!!cp ('t15');
3218              ## Stay in the insertion mode.
3219            !!!next-token;            !!!next-token;
3220            redo INITIAL;            redo INITIAL;
3221            } else {
3222              !!!cp ('t16');
3223          }          }
3224          } else {
3225            !!!cp ('t17');
3226        }        }
3227    
3228        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3229        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3230        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3231        ## reprocess        ## reprocess
3232        return;        return;
3233      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3234          !!!cp ('t18');
3235        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3236        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3237                
3238        ## Stay in the phase.        ## Stay in the insertion mode.
3239        !!!next-token;        !!!next-token;
3240        redo INITIAL;        redo INITIAL;
3241      } else {      } else {
3242        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
3243      }      }
3244    } # INITIAL    } # INITIAL
3245    
3246      die "$0: _tree_construction_initial: This should be never reached";
3247  } # _tree_construction_initial  } # _tree_construction_initial
3248    
3249  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3250    my $self = shift;    my $self = shift;
3251    
3252      ## NOTE: "before html" insertion mode.
3253        
3254    B: {    B: {
3255        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3256          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3257            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3258          ## Ignore the token          ## Ignore the token
3259          ## Stay in the phase          ## Stay in the insertion mode.
3260          !!!next-token;          !!!next-token;
3261          redo B;          redo B;
3262        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3263            !!!cp ('t20');
3264          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3265          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3266          ## Stay in the phase          ## Stay in the insertion mode.
3267          !!!next-token;          !!!next-token;
3268          redo B;          redo B;
3269        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3270          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3271            ## Ignore the token.            ## Ignore the token.
3272    
3273            unless (length $token->{data}) {            unless (length $token->{data}) {
3274              ## Stay in the phase              !!!cp ('t21');
3275                ## Stay in the insertion mode.
3276              !!!next-token;              !!!next-token;
3277              redo B;              redo B;
3278              } else {
3279                !!!cp ('t22');
3280            }            }
3281            } else {
3282              !!!cp ('t23');
3283          }          }
3284    
3285            $self->{application_cache_selection}->(undef);
3286    
3287          #          #
3288          } elsif ($token->{type} == START_TAG_TOKEN) {
3289            if ($token->{tag_name} eq 'html') {
3290              my $root_element;
3291              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3292              $self->{document}->append_child ($root_element);
3293              push @{$self->{open_elements}},
3294                  [$root_element, $el_category->{html}];
3295    
3296              if ($token->{attributes}->{manifest}) {
3297                !!!cp ('t24');
3298                $self->{application_cache_selection}
3299                    ->($token->{attributes}->{manifest}->{value});
3300                ## ISSUE: Spec is unclear on relative references.
3301                ## According to Hixie (#whatwg 2008-03-19), it should be
3302                ## resolved against the base URI of the document in HTML
3303                ## or xml:base of the element in XHTML.
3304              } else {
3305                !!!cp ('t25');
3306                $self->{application_cache_selection}->(undef);
3307              }
3308    
3309              !!!nack ('t25c');
3310    
3311              !!!next-token;
3312              return; ## Go to the "before head" insertion mode.
3313            } else {
3314              !!!cp ('t25.1');
3315              #
3316            }
3317        } elsif ({        } elsif ({
3318                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3319                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3320                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3321          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3322          #          #
3323        } else {        } else {
3324          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3325        }        }
3326        my $root_element; !!!create-element ($root_element, 'html');  
3327        $self->{document}->append_child ($root_element);      my $root_element;
3328        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3329        ## reprocess      $self->{document}->append_child ($root_element);
3330        #redo B;      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3331        return; ## Go to the main phase.  
3332        $self->{application_cache_selection}->(undef);
3333    
3334        ## NOTE: Reprocess the token.
3335        !!!ack-later;
3336        return; ## Go to the "before head" insertion mode.
3337    
3338        ## ISSUE: There is an issue in the spec
3339    } # B    } # B
3340    
3341      die "$0: _tree_construction_root_element: This should never be reached";
3342  } # _tree_construction_root_element  } # _tree_construction_root_element
3343    
3344  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2036  sub _reset_insertion_mode ($) { Line 3353  sub _reset_insertion_mode ($) {
3353            
3354      ## Step 3      ## Step 3
3355      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"!?  
3356        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3357          $last = 1;          $last = 1;
3358          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3359            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
3360                $self->{inner_html_node}->[1] eq 'th') {              !!!cp ('t27');
3361              #              #
3362            } else {            } else {
3363                !!!cp ('t28');
3364              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
3365            }            }
3366          }          }
3367        }        }
3368            
3369        ## Step 4..13      ## Step 4..14
3370        my $new_mode = {      my $new_mode;
3371                        select => 'in select',      if ($node->[1] & FOREIGN_EL) {
3372                        td => 'in cell',        ## NOTE: Strictly spaking, the line below only applies to MathML and
3373                        th => 'in cell',        ## SVG elements.  Currently the HTML syntax supports only MathML and
3374                        tr => 'in row',        ## SVG elements as foreigners.
3375                        tbody => 'in table body',        $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3376                        thead => 'in table head',        ## ISSUE: What is set as the secondary insertion mode?
3377                        tfoot => 'in table foot',      } else {
3378                        caption => 'in caption',        $new_mode = {
3379                        colgroup => 'in column group',                        select => IN_SELECT_IM,
3380                        table => 'in table',                        ## NOTE: |option| and |optgroup| do not set
3381                        head => 'in body', # not in head!                        ## insertion mode to "in select" by themselves.
3382                        body => 'in body',                        td => IN_CELL_IM,
3383                        frameset => 'in frameset',                        th => IN_CELL_IM,
3384                       }->{$node->[1]};                        tr => IN_ROW_IM,
3385        $self->{insertion_mode} = $new_mode and return if defined $new_mode;                        tbody => IN_TABLE_BODY_IM,
3386                          thead => IN_TABLE_BODY_IM,
3387                          tfoot => IN_TABLE_BODY_IM,
3388                          caption => IN_CAPTION_IM,
3389                          colgroup => IN_COLUMN_GROUP_IM,
3390                          table => IN_TABLE_IM,
3391                          head => IN_BODY_IM, # not in head!
3392                          body => IN_BODY_IM,
3393                          frameset => IN_FRAMESET_IM,
3394                         }->{$node->[0]->manakai_local_name};
3395        }
3396        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3397                
3398        ## Step 14        ## Step 15
3399        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3400          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3401            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3402              $self->{insertion_mode} = BEFORE_HEAD_IM;
3403          } else {          } else {
3404            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3405              !!!cp ('t30');
3406              $self->{insertion_mode} = AFTER_HEAD_IM;
3407          }          }
3408          return;          return;
3409          } else {
3410            !!!cp ('t31');
3411        }        }
3412                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3413        ## Step 16        ## Step 16
3414          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3415          
3416          ## Step 17
3417        $i--;        $i--;
3418        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3419                
3420        ## Step 17        ## Step 18
3421        redo S3;        redo S3;
3422      } # S3      } # S3
3423    
3424      die "$0: _reset_insertion_mode: This line should never be reached";
3425  } # _reset_insertion_mode  } # _reset_insertion_mode
3426    
3427  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3428    my $self = shift;    my $self = shift;
3429    
   my $previous_insertion_mode;  
   
3430    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3431    
3432    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2114  sub _tree_construction_main ($) { Line 3443  sub _tree_construction_main ($) {
3443      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3444      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3445        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3446            !!!cp ('t32');
3447          return;          return;
3448        }        }
3449      }      }
# Line 2128  sub _tree_construction_main ($) { Line 3458  sub _tree_construction_main ($) {
3458    
3459        ## Step 6        ## Step 6
3460        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3461            !!!cp ('t33_1');
3462          #          #
3463        } else {        } else {
3464          my $in_open_elements;          my $in_open_elements;
3465          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3466            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3467                !!!cp ('t33');
3468              $in_open_elements = 1;              $in_open_elements = 1;
3469              last OE;              last OE;
3470            }            }
3471          }          }
3472          if ($in_open_elements) {          if ($in_open_elements) {
3473              !!!cp ('t34');
3474            #            #
3475          } else {          } else {
3476              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3477              !!!cp ('t35');
3478            redo S4;            redo S4;
3479          }          }
3480        }        }
# Line 2162  sub _tree_construction_main ($) { Line 3497  sub _tree_construction_main ($) {
3497    
3498        ## Step 11        ## Step 11
3499        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3500            !!!cp ('t36');
3501          ## Step 7'          ## Step 7'
3502          $i++;          $i++;
3503          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3504                    
3505          redo S7;          redo S7;
3506        }        }
3507    
3508          !!!cp ('t37');
3509      } # S7      } # S7
3510    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3511    
3512    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3513      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3514        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3515            !!!cp ('t38');
3516          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3517          return;          return;
3518        }        }
3519      }      }
3520    
3521        !!!cp ('t39');
3522    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3523    
3524    my $parse_rcdata = sub ($$) {    my $insert;
3525      my ($content_model_flag, $insert) = @_;  
3526      my $parse_rcdata = sub ($) {
3527        my ($content_model_flag) = @_;
3528    
3529      ## Step 1      ## Step 1
3530      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3531      my $el;      my $el;
3532      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3533    
3534      ## Step 2      ## Step 2
3535      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3536    
3537      ## Step 3      ## Step 3
3538      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3539      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3540    
3541      ## Step 4      ## Step 4
3542      my $text = '';      my $text = '';
3543        !!!nack ('t40.1');
3544      !!!next-token;      !!!next-token;
3545      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3546          !!!cp ('t40');
3547        $text .= $token->{data};        $text .= $token->{data};
3548        !!!next-token;        !!!next-token;
3549      }      }
3550    
3551      ## Step 5      ## Step 5
3552      if (length $text) {      if (length $text) {
3553          !!!cp ('t41');
3554        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3555        $el->append_child ($text);        $el->append_child ($text);
3556      }      }
3557    
3558      ## Step 6      ## Step 6
3559      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3560    
3561      ## Step 7      ## Step 7
3562      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3563            $token->{tag_name} eq $start_tag_name) {
3564          !!!cp ('t42');
3565        ## Ignore the token        ## Ignore the token
3566      } else {      } else {
3567        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        ## NOTE: An end-of-file token.
3568          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3569            !!!cp ('t43');
3570            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3571          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3572            !!!cp ('t44');
3573            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3574          } else {
3575            die "$0: $content_model_flag in parse_rcdata";
3576          }
3577      }      }
3578      !!!next-token;      !!!next-token;
3579    }; # $parse_rcdata    }; # $parse_rcdata
3580    
3581    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3582      my $script_el;      my $script_el;
3583      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3584      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3585    
3586      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3587      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3588            
3589      my $text = '';      my $text = '';
3590        !!!nack ('t45.1');
3591      !!!next-token;      !!!next-token;
3592      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3593          !!!cp ('t45');
3594        $text .= $token->{data};        $text .= $token->{data};
3595        !!!next-token;        !!!next-token;
3596      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3597      if (length $text) {      if (length $text) {
3598          !!!cp ('t46');
3599        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3600      }      }
3601                                
3602      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3603    
3604      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3605          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3606          !!!cp ('t47');
3607        ## Ignore the token        ## Ignore the token
3608      } else {      } else {
3609        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3610          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3611        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3612        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3613      }      }
3614            
3615      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3616          !!!cp ('t49');
3617        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3618      } else {      } else {
3619          !!!cp ('t50');
3620        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3621        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3622    
# Line 2267  sub _tree_construction_main ($) { Line 3630  sub _tree_construction_main ($) {
3630      !!!next-token;      !!!next-token;
3631    }; # $script_start_tag    }; # $script_start_tag
3632    
3633      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3634      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3635      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3636    
3637    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3638      my $tag_name = shift;      my $end_tag_token = shift;
3639        my $tag_name = $end_tag_token->{tag_name};
3640    
3641        ## NOTE: The adoption agency algorithm (AAA).
3642    
3643      FET: {      FET: {
3644        ## Step 1        ## Step 1
3645        my $formatting_element;        my $formatting_element;
3646        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3647        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3648          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3649              !!!cp ('t52');
3650              last AFE;
3651            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3652                         eq $tag_name) {
3653              !!!cp ('t51');
3654            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3655            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3656            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3657          }          }
3658        } # AFE        } # AFE
3659        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3660          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3661            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3662          ## Ignore the token          ## Ignore the token
3663          !!!next-token;          !!!next-token;
3664          return;          return;
# Line 2296  sub _tree_construction_main ($) { Line 3670  sub _tree_construction_main ($) {
3670          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3671          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3672            if ($in_scope) {            if ($in_scope) {
3673                !!!cp ('t54');
3674              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3675              last INSCOPE;              last INSCOPE;
3676            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3677              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3678                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3679                                token => $end_tag_token);
3680              ## Ignore the token              ## Ignore the token
3681              !!!next-token;              !!!next-token;
3682              return;              return;
3683            }            }
3684          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3685                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3686            $in_scope = 0;            $in_scope = 0;
3687          }          }
3688        } # INSCOPE        } # INSCOPE
3689        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3690          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3691            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3692                            token => $end_tag_token);
3693          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3694          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3695          return;          return;
3696        }        }
3697        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3698          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3699            !!!parse-error (type => 'not closed',
3700                            value => $self->{open_elements}->[-1]->[0]
3701                                ->manakai_local_name,
3702                            token => $end_tag_token);
3703        }        }
3704                
3705        ## Step 2        ## Step 2
# Line 2326  sub _tree_construction_main ($) { Line 3707  sub _tree_construction_main ($) {
3707        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3708        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3709          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3710          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3711              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3712              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3713               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3714              !!!cp ('t59');
3715            $furthest_block = $node;            $furthest_block = $node;
3716            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3717          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3718              !!!cp ('t60');
3719            last OE;            last OE;
3720          }          }
3721        } # OE        } # OE
3722                
3723        ## Step 3        ## Step 3
3724        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3725            !!!cp ('t61');
3726          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3727          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3728          !!!next-token;          !!!next-token;
# Line 2351  sub _tree_construction_main ($) { Line 3735  sub _tree_construction_main ($) {
3735        ## Step 5        ## Step 5
3736        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3737        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3738            !!!cp ('t62');
3739          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3740        }        }
3741                
# Line 2373  sub _tree_construction_main ($) { Line 3758  sub _tree_construction_main ($) {
3758          S7S2: {          S7S2: {
3759            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3760              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3761                  !!!cp ('t63');
3762                $node_i_in_active = $_;                $node_i_in_active = $_;
3763                last S7S2;                last S7S2;
3764              }              }
# Line 2386  sub _tree_construction_main ($) { Line 3772  sub _tree_construction_main ($) {
3772                    
3773          ## Step 4          ## Step 4
3774          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3775              !!!cp ('t64');
3776            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3777          }          }
3778                    
3779          ## Step 5          ## Step 5
3780          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3781              !!!cp ('t65');
3782            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3783            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3784            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2408  sub _tree_construction_main ($) { Line 3796  sub _tree_construction_main ($) {
3796        } # S7          } # S7  
3797                
3798        ## Step 8        ## Step 8
3799        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3800            my $foster_parent_element;
3801            my $next_sibling;
3802            OE: for (reverse 0..$#{$self->{open_elements}}) {
3803              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3804                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3805                                 if (defined $parent and $parent->node_type == 1) {
3806                                   !!!cp ('t65.1');
3807                                   $foster_parent_element = $parent;
3808                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3809                                 } else {
3810                                   !!!cp ('t65.2');
3811                                   $foster_parent_element
3812                                     = $self->{open_elements}->[$_ - 1]->[0];
3813                                 }
3814                                 last OE;
3815                               }
3816                             } # OE
3817                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3818                               unless defined $foster_parent_element;
3819            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3820            $open_tables->[-1]->[1] = 1; # tainted
3821          } else {
3822            !!!cp ('t65.3');
3823            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3824          }
3825                
3826        ## Step 9        ## Step 9
3827        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2425  sub _tree_construction_main ($) { Line 3838  sub _tree_construction_main ($) {
3838        my $i;        my $i;
3839        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3840          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3841              !!!cp ('t66');
3842            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3843            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3844          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3845              !!!cp ('t67');
3846            $i = $_;            $i = $_;
3847          }          }
3848        } # AFE        } # AFE
# Line 2437  sub _tree_construction_main ($) { Line 3852  sub _tree_construction_main ($) {
3852        undef $i;        undef $i;
3853        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3854          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3855              !!!cp ('t68');
3856            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3857            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3858          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3859              !!!cp ('t69');
3860            $i = $_;            $i = $_;
3861          }          }
3862        } # OE        } # OE
# Line 2450  sub _tree_construction_main ($) { Line 3867  sub _tree_construction_main ($) {
3867      } # FET      } # FET
3868    }; # $formatting_end_tag    }; # $formatting_end_tag
3869    
3870    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3871      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3872    }; # $insert_to_current    }; # $insert_to_current
3873    
3874    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3875                         my $child = shift;      my $child = shift;
3876                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3877                              table => 1, tbody => 1, tfoot => 1,        # MUST
3878                              thead => 1, tr => 1,        my $foster_parent_element;
3879                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3880                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3881                           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') {  
3882                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3883                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3884                                   !!!cp ('t70');
3885                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3886                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3887                               } else {                               } else {
3888                                   !!!cp ('t71');
3889                                 $foster_parent_element                                 $foster_parent_element
3890                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3891                               }                               }
# Line 2480  sub _tree_construction_main ($) { Line 3896  sub _tree_construction_main ($) {
3896                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3897                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3898                             ($child, $next_sibling);                             ($child, $next_sibling);
3899                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3900                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3901                         }        !!!cp ('t72');
3902          $self->{open_elements}->[-1]->[0]->append_child ($child);
3903        }
3904    }; # $insert_to_foster    }; # $insert_to_foster
3905    
3906    my $in_body = sub {    B: while (1) {
3907      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
3908      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
3909        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3910          ## NOTE: This is an "as if in head" code clone        ## Ignore the token
3911          $script_start_tag->($insert);        ## Stay in the phase
3912          return;        !!!next-token;
3913        } elsif ($token->{tag_name} eq 'style') {        next B;
3914          ## NOTE: This is an "as if in head" code clone      } elsif ($token->{type} == START_TAG_TOKEN and
3915          $parse_rcdata->('CDATA', $insert);               $token->{tag_name} eq 'html') {
3916          return;        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3917        } elsif ({          !!!cp ('t79');
3918                  base => 1, link => 1,          !!!parse-error (type => 'after html:html', token => $token);
3919                 }->{$token->{tag_name}}) {          $self->{insertion_mode} = AFTER_BODY_IM;
3920          ## NOTE: This is an "as if in head" code clone, only "-t" differs        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3921          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t80');
3922          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          !!!parse-error (type => 'after html:html', token => $token);
3923          !!!next-token;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3924          return;        } else {
3925        } elsif ($token->{tag_name} eq 'meta') {          !!!cp ('t81');
3926          ## NOTE: This is an "as if in head" code clone, only "-t" differs        }
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
   
         unless ($self->{confident}) {  
           my $charset;  
           if ($token->{attributes}->{charset}) { ## TODO: And if supported  
             $charset = $token->{attributes}->{charset}->{value};  
           }  
           if ($token->{attributes}->{'http-equiv'}) {  
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
             if ($token->{attributes}->{'http-equiv'}->{value}  
                 =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=  
                     [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|  
                     ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {  
               $charset = defined $1 ? $1 : defined $2 ? $2 : $3;  
             } ## TODO: And if supported  
           }  
           ## TODO: Change the encoding  
         }  
3927    
3928          !!!next-token;        !!!cp ('t82');
3929          return;        !!!parse-error (type => 'not first start tag', token => $token);
3930        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
3931          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
3932          ## NOTE: This is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3933          $parse_rcdata->('RCDATA', sub {            !!!cp ('t84');
3934            if (defined $self->{head_element}) {            $top_el->set_attribute_ns
3935              $self->{head_element}->append_child ($_[0]);              (undef, [undef, $attr_name],
3936            } else {               $token->{attributes}->{$attr_name}->{value});
             $insert->($_[0]);  
           }  
         });  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } 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});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } 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];  
           !!!next-token;  
           return;  
3937          }          }
3938        } elsif ($token->{tag_name} eq 'li') {        }
3939          ## has a p element in scope        !!!nack ('t84.1');
3940          INSCOPE: for (reverse @{$self->{open_elements}}) {        !!!next-token;
3941            if ($_->[1] eq 'p') {        next B;
3942              !!!back-token;      } elsif ($token->{type} == COMMENT_TOKEN) {
3943              $token = {type => 'end tag', tag_name => 'p'};        my $comment = $self->{document}->create_comment ($token->{data});
3944              return;        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3945            } elsif ({          !!!cp ('t85');
3946                      table => 1, caption => 1, td => 1, th => 1,          $self->{document}->append_child ($comment);
3947                      button => 1, marquee => 1, object => 1, html => 1,        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3948                     }->{$_->[1]}) {          !!!cp ('t86');
3949              last INSCOPE;          $self->{open_elements}->[0]->[0]->append_child ($comment);
3950            }        } else {
3951          } # INSCOPE          !!!cp ('t87');
3952                      $self->{open_elements}->[-1]->[0]->append_child ($comment);
3953          ## Step 1        }
3954          my $i = -1;        !!!next-token;
3955          my $node = $self->{open_elements}->[$i];        next B;
3956          LI: {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3957            ## Step 2        if ($token->{type} == CHARACTER_TOKEN) {
3958            if ($node->[1] eq 'li') {          !!!cp ('t87.1');
3959              if ($i != -1) {          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
               !!!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;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             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;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } 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->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } 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});  
             
3960          !!!next-token;          !!!next-token;
3961          return;          next B;
3962        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3963          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3964            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3965            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3966              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
3967                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3968              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
3969              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
3970              $formatting_end_tag->($token->{tag_name});            #
3971                        } elsif ({
3972              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3973                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3974                  splice @$active_formatting_elements, $_, 1;                    embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3975                  last AFE2;                    h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3976                }                    li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3977              } # AFE2                    ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3978              OE: for (reverse 0..$#{$self->{open_elements}}) {                    sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3979                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    var => 1,
3980                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
3981                  last OE;            !!!cp ('t87.2');
3982                }            !!!parse-error (type => 'not closed',
3983              } # OE                            value => $self->{open_elements}->[-1]->[0]
3984              last AFE;                                ->manakai_local_name,
3985            } elsif ($node->[0] eq '#marker') {                            token => $token);
3986              last AFE;  
3987              pop @{$self->{open_elements}}
3988                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3989    
3990              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3991              ## Reprocess.
3992              next B;
3993            } else {
3994              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3995              my $tag_name = $token->{tag_name};
3996              if ($nsuri eq $SVG_NS) {
3997                $tag_name = {
3998                   altglyph => 'altGlyph',
3999                   altglyphdef => 'altGlyphDef',
4000                   altglyphitem => 'altGlyphItem',
4001                   animatecolor => 'animateColor',
4002                   animatemotion => 'animateMotion',
4003                   animatetransform => 'animateTransform',
4004                   clippath => 'clipPath',
4005                   feblend => 'feBlend',
4006                   fecolormatrix => 'feColorMatrix',
4007                   fecomponenttransfer => 'feComponentTransfer',
4008                   fecomposite => 'feComposite',
4009                   feconvolvematrix => 'feConvolveMatrix',
4010                   fediffuselighting => 'feDiffuseLighting',
4011                   fedisplacementmap => 'feDisplacementMap',
4012                   fedistantlight => 'feDistantLight',
4013                   feflood => 'feFlood',
4014                   fefunca => 'feFuncA',
4015                   fefuncb => 'feFuncB',
4016                   fefuncg => 'feFuncG',
4017                   fefuncr => 'feFuncR',
4018                   fegaussianblur => 'feGaussianBlur',
4019                   feimage => 'feImage',
4020                   femerge => 'feMerge',
4021                   femergenode => 'feMergeNode',
4022                   femorphology => 'feMorphology',
4023                   feoffset => 'feOffset',
4024                   fepointlight => 'fePointLight',
4025                   fespecularlighting => 'feSpecularLighting',
4026                   fespotlight => 'feSpotLight',
4027                   fetile => 'feTile',
4028                   feturbulence => 'feTurbulence',
4029                   foreignobject => 'foreignObject',
4030                   glyphref => 'glyphRef',
4031                   lineargradient => 'linearGradient',
4032                   radialgradient => 'radialGradient',
4033                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4034                   textpath => 'textPath',  
4035                }->{$tag_name} || $tag_name;
4036            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4037    
4038          !!!insert-element-t ($token->{tag_name}, $token->{attributes});            ## "adjust SVG attributes" (SVG only) - done in insert-element-f
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
4039    
4040          !!!next-token;            ## "adjust foreign attributes" - done in insert-element-f
         return;  
       } 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;  
         return;  
       } elsif ($token->{tag_name} eq 'nobr') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
4041    
4042          ## has a |nobr| element in scope            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'nobr') {  
             !!!parse-error (type => 'not closed:nobr');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'nobr'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
   
         !!!next-token;  
         return;  
       } 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});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->('CDATA', $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } 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';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
4043    
4044          ## NOTE: There is an "as if <br>" code clone.            if ($self->{self_closing}) {
4045          $reconstruct_active_formatting_elements->($insert_to_current);              pop @{$self->{open_elements}};
4046                        !!!ack ('t87.3');
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } 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;  
         return;  
       } 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;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           my $form_attrs;  
           $form_attrs->{action} = $at->{action} if $at->{action};  
           my $prompt_attr = $at->{prompt};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           delete $at->{action};  
           delete $at->{prompt};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form',  
                          attributes => $form_attrs},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                        );  
           if ($prompt_attr) {  
             push @tokens, {type => 'character', data => $prompt_attr->{value}};  
4047            } else {            } else {
4048              push @tokens, {type => 'character',              !!!cp ('t87.4');
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
           }  
           push @tokens,  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'};  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'textarea') {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         ## TODO: $self->{form_element} if defined  
         $self->{content_model_flag} = 'RCDATA';  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         if ($token->{type} eq 'character') {  
           $token->{data} =~ s/^\x0A//;  
           unless (length $token->{data}) {  
             !!!next-token;  
4049            }            }
4050          }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
4051            !!!next-token;            !!!next-token;
4052              next B;
4053          }          }
4054          if (length $text) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4055            $el->manakai_append_text ($text);          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4056          }          !!!cp ('t87.5');
4057                    #
4058          $self->{content_model_flag} = 'PCDATA';        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4059                    ## NOTE: "using the rules for secondary insertion mode" then "continue"
4060          if ($token->{type} eq 'end tag' and          !!!cp ('t87.6');
4061              $token->{tag_name} eq $tag_name) {          #
4062            ## Ignore the token          ## TODO: ...
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         $parse_rcdata->('CDATA', $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
4063        } else {        } else {
4064          $reconstruct_active_formatting_elements->($insert_to_current);          die "$0: $token->{type}: Unknown token type";        
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
4065        }        }
4066      } elsif ($token->{type} eq 'end tag') {      }
       if ($token->{tag_name} eq 'body') {  
         if (@{$self->{open_elements}} > 1 and  
             $self->{open_elements}->[1]->[1] eq 'body') {  
           for (@{$self->{open_elements}}) {  
             unless ({  
                        dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                        th => 1, tr => 1, body => 1, html => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$_->[1]}) {  
               !!!parse-error (type => 'not closed:'.$_->[1]);  
             }  
           }  
4067    
4068            $self->{insertion_mode} = 'after body';      if ($self->{insertion_mode} & HEAD_IMS) {
4069            !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
4070            return;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4071          } else {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4072            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.2');
4073            ## Ignore the token              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           if (defined $i) {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4074            } else {            } else {
4075              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.1');
4076            }              ## Ignore the token.
4077          }              !!!next-token;
4078                        next B;
         if (defined $i) {  
           splice @{$self->{open_elements}}, $i;  
         } elsif ($token->{tag_name} eq 'p') {  
           ## As if <p>, then reprocess the current token  
           my $el;  
           !!!create-element ($el, 'p');  
           $insert->($el);  
         }  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4079            }            }
4080          } # INSCOPE            unless (length $token->{data}) {
4081                        !!!cp ('t88');
4082          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {              !!!next-token;
4083            pop @{$self->{open_elements}};              next B;
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         undef $self->{form_element};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## 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]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4084            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4085          }          }
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ($token->{tag_name} eq 'br') {  
         !!!parse-error (type => 'unmatched end tag:br');  
4086    
4087          ## As if <br>          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4088          $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t89');
4089                      ## As if <head>
4090          my $el;            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4091          !!!create-element ($el, 'br');            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4092          $insert->($el);            push @{$self->{open_elements}},
4093                          [$self->{head_element}, $el_category->{head}];
         ## Ignore the token.  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
4094    
4095          ## Step 2            ## Reprocess in the "in head" insertion mode...
4096          S2: {            pop @{$self->{open_elements}};
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
4097    
4098              !!!next-token;            ## Reprocess in the "after head" insertion mode...
4099              last S2;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4100            } else {            !!!cp ('t90');
4101              ## Step 3            ## As if </noscript>
4102              if (not $formatting_category->{$node->[1]} and            pop @{$self->{open_elements}};
4103                  #not $phrasing_category->{$node->[1]} and            !!!parse-error (type => 'in noscript:#character', token => $token);
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
4104                        
4105            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4106            redo S2;            ## As if </head>
4107          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
   
   B: {  
     if ($token->{type} eq 'DOCTYPE') {  
       !!!parse-error (type => 'DOCTYPE in the middle');  
       ## Ignore the token  
       ## Stay in the phase  
       !!!next-token;  
       redo B;  
     } elsif ($token->{type} eq 'end-of-file') {  
       if ($token->{insertion_mode} ne 'trailing end') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
4108    
4109        ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4110        last B;          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4111      } elsif ($token->{type} eq 'start tag' and            !!!cp ('t91');
4112               $token->{tag_name} eq 'html') {            pop @{$self->{open_elements}};
       if ($self->{insertion_mode} eq 'trailing end') {  
         ## Turn into the main phase  
         !!!parse-error (type => 'after html:html');  
         $self->{insertion_mode} = $previous_insertion_mode;  
       }  
4113    
4114  ## ISSUE: "aa<html>" is not a parse error.            ## Reprocess in the "after head" insertion mode...
4115  ## ISSUE: "<html>" in fragment is not a parse error.          } else {
4116        unless ($token->{first_start_tag}) {            !!!cp ('t92');
         !!!parse-error (type => 'not first start tag');  
       }  
       my $top_el = $self->{open_elements}->[0]->[0];  
       for my $attr_name (keys %{$token->{attributes}}) {  
         unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
           $top_el->set_attribute_ns  
             (undef, [undef, $attr_name],  
              $token->{attributes}->{$attr_name}->{value});  
4117          }          }
4118        }  
4119        !!!next-token;          ## "after head" insertion mode
4120        redo B;          ## As if <body>
4121      } elsif ($token->{type} eq 'comment') {          !!!insert-element ('body',, $token);
4122        my $comment = $self->{document}->create_comment ($token->{data});          $self->{insertion_mode} = IN_BODY_IM;
4123        if ($self->{insertion_mode} eq 'trailing end') {          ## reprocess
4124          $self->{document}->append_child ($comment);          next B;
4125        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4126          $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($token->{tag_name} eq 'head') {
4127        } else {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4128          $self->{open_elements}->[-1]->[0]->append_child ($comment);              !!!cp ('t93');
4129        }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4130        !!!next-token;              $self->{open_elements}->[-1]->[0]->append_child
4131        redo B;                  ($self->{head_element});
4132      } elsif ($self->{insertion_mode} eq 'before head') {              push @{$self->{open_elements}},
4133            if ($token->{type} eq 'character') {                  [$self->{head_element}, $el_category->{head}];
4134              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              $self->{insertion_mode} = IN_HEAD_IM;
4135                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              !!!nack ('t93.1');
4136                unless (length $token->{data}) {              !!!next-token;
4137                  !!!next-token;              next B;
4138                  redo B;            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4139                }              !!!cp ('t94');
4140              }              #
             ## As if <head>  
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  head => 1, body => 1, html => 1,  
                  p => 1, br => 1,  
                 }->{$token->{tag_name}}) {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
4141            } else {            } else {
4142              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4143                !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4144                ## Ignore the token
4145                !!!nack ('t95.1');
4146                !!!next-token;
4147                next B;
4148            }            }
4149          } elsif ($self->{insertion_mode} eq 'in head' or          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4150                   $self->{insertion_mode} eq 'in head noscript' or            !!!cp ('t96');
4151                   $self->{insertion_mode} eq 'after head') {            ## As if <head>
4152            if ($token->{type} eq 'character') {            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4153              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4154                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            push @{$self->{open_elements}},
4155                unless (length $token->{data}) {                [$self->{head_element}, $el_category->{head}];
4156                  !!!next-token;  
4157                  redo B;            $self->{insertion_mode} = IN_HEAD_IM;
4158              ## Reprocess in the "in head" insertion mode...
4159            } else {
4160              !!!cp ('t97');
4161            }
4162    
4163                if ($token->{tag_name} eq 'base') {
4164                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4165                    !!!cp ('t98');
4166                    ## As if </noscript>
4167                    pop @{$self->{open_elements}};
4168                    !!!parse-error (type => 'in noscript:base', token => $token);
4169                  
4170                    $self->{insertion_mode} = IN_HEAD_IM;
4171                    ## Reprocess in the "in head" insertion mode...
4172                  } else {
4173                    !!!cp ('t99');
4174                }                }
4175              }  
               
             #  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1}->{$token->{tag_name}}) {  
4176                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4177                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4178                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4179                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4180                    push @{$self->{open_elements}},
4181                        [$self->{head_element}, $el_category->{head}];
4182                  } else {
4183                    !!!cp ('t101');
4184                }                }
4185                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4186                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4187                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4188                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4189                  !!!nack ('t101.1');
4190                !!!next-token;                !!!next-token;
4191                redo B;                next B;
4192              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'link') {
4193                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4194                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4195                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4196                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4197                    push @{$self->{open_elements}},
4198                        [$self->{head_element}, $el_category->{head}];
4199                  } else {
4200                    !!!cp ('t103');
4201                }                }
4202                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4203                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4204                  pop @{$self->{open_elements}} # <head>
4205                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4206                  !!!ack ('t103.1');
4207                  !!!next-token;
4208                  next B;
4209                } elsif ($token->{tag_name} eq 'meta') {
4210                  ## NOTE: There is a "as if in head" code clone.
4211                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4212                    !!!cp ('t104');
4213                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4214                    push @{$self->{open_elements}},
4215                        [$self->{head_element}, $el_category->{head}];
4216                  } else {
4217                    !!!cp ('t105');
4218                  }
4219                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4220                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4221    
4222                unless ($self->{confident}) {                unless ($self->{confident}) {
4223                  my $charset;                  if ($token->{attributes}->{charset}) {
4224                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                    !!!cp ('t106');
4225                    $charset = $token->{attributes}->{charset}->{value};                    ## NOTE: Whether the encoding is supported or not is handled
4226                  }                    ## in the {change_encoding} callback.
4227                  if ($token->{attributes}->{'http-equiv'}) {                    $self->{change_encoding}
4228                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                        ->($self, $token->{attributes}->{charset}->{value},
4229                    if ($token->{attributes}->{'http-equiv'}->{value}                           $token);
4230                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                    
4231                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4232                          ->set_user_data (manakai_has_reference =>
4233                                               $token->{attributes}->{charset}
4234                                                   ->{has_reference});
4235                    } elsif ($token->{attributes}->{content}) {
4236                      if ($token->{attributes}->{content}->{value}
4237                          =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4238                              [\x09-\x0D\x20]*=
4239                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4240                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4241                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
4242                    } ## TODO: And if supported                      ## NOTE: Whether the encoding is supported or not is handled
4243                        ## in the {change_encoding} callback.
4244                        $self->{change_encoding}
4245                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4246                               $token);
4247                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4248                            ->set_user_data (manakai_has_reference =>
4249                                                 $token->{attributes}->{content}
4250                                                       ->{has_reference});
4251                      } else {
4252                        !!!cp ('t108');
4253                      }
4254                    }
4255                  } else {
4256                    if ($token->{attributes}->{charset}) {
4257                      !!!cp ('t109');
4258                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4259                          ->set_user_data (manakai_has_reference =>
4260                                               $token->{attributes}->{charset}
4261                                                   ->{has_reference});
4262                    }
4263                    if ($token->{attributes}->{content}) {
4264                      !!!cp ('t110');
4265                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4266                          ->set_user_data (manakai_has_reference =>
4267                                               $token->{attributes}->{content}
4268                                                   ->{has_reference});
4269                  }                  }
                 ## TODO: Change the encoding  
4270                }                }
4271    
4272                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
4273                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4274                    if $self->{insertion_mode} eq 'after head';                !!!ack ('t110.1');
4275                !!!next-token;                !!!next-token;
4276                redo B;                next B;
4277              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
4278                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4279                ## NOTE: There is a "as if in head" code clone.                  !!!cp ('t111');
4280                if ($self->{insertion_mode} eq 'after head') {                  ## As if </noscript>
4281                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  pop @{$self->{open_elements}};
4282                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'in noscript:title', token => $token);
4283                  
4284                    $self->{insertion_mode} = IN_HEAD_IM;
4285                    ## Reprocess in the "in head" insertion mode...
4286                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4287                    !!!cp ('t112');
4288                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4289                    push @{$self->{open_elements}},
4290                        [$self->{head_element}, $el_category->{head}];
4291                  } else {
4292                    !!!cp ('t113');
4293                }                }
4294    
4295                  ## NOTE: There is a "as if in head" code clone.
4296                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4297                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4298                $parse_rcdata->('RCDATA', sub { $parent->append_child ($_[0]) });                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4299                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4300                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4301                redo B;                next B;
4302              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4303                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4304                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
4305                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4306                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4307                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4308                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4309                }                  push @{$self->{open_elements}},
4310                $parse_rcdata->('CDATA', $insert_to_current);                      [$self->{head_element}, $el_category->{head}];
4311                pop @{$self->{open_elements}}                } else {
4312                    if $self->{insertion_mode} eq 'after head';                  !!!cp ('t115');
4313                redo B;                }
4314                  $parse_rcdata->(CDATA_CONTENT_MODEL);
4315                  pop @{$self->{open_elements}} # <head>
4316                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4317                  next B;
4318              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4319                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4320                    !!!cp ('t116');
4321                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4322                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4323                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4324                    !!!nack ('t116.1');
4325                  !!!next-token;                  !!!next-token;
4326                  redo B;                  next B;
4327                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4328                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4329                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4330                  ## Ignore the token                  ## Ignore the token
4331                  redo B;                  !!!nack ('t117.1');
4332                    !!!next-token;
4333                    next B;
4334                } else {                } else {
4335                    !!!cp ('t118');
4336                  #                  #
4337                }                }
4338              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
4339                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4340                !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t119');
4341                ## Ignore the token                  ## As if </noscript>
4342                !!!next-token;                  pop @{$self->{open_elements}};
4343                redo B;                  !!!parse-error (type => 'in noscript:script', token => $token);
4344              } elsif ($self->{insertion_mode} ne 'in head noscript' and                
4345                       $token->{tag_name} eq 'script') {                  $self->{insertion_mode} = IN_HEAD_IM;
4346                if ($self->{insertion_mode} eq 'after head') {                  ## Reprocess in the "in head" insertion mode...
4347                  !!!parse-error (type => 'after head:'.$token->{tag_name});                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4348                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!cp ('t120');
4349                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4350                    push @{$self->{open_elements}},
4351                        [$self->{head_element}, $el_category->{head}];
4352                  } else {
4353                    !!!cp ('t121');
4354                }                }
4355    
4356                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4357                $script_start_tag->($insert_to_current);                $script_start_tag->();
4358                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4359                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4360                redo B;                next B;
4361              } elsif ($self->{insertion_mode} eq 'after head' and              } elsif ($token->{tag_name} eq 'body' or
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
4362                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4363                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4364                $self->{insertion_mode} = 'in frameset';                  !!!cp ('t122');
4365                    ## As if </noscript>
4366                    pop @{$self->{open_elements}};
4367                    !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4368                    
4369                    ## Reprocess in the "in head" insertion mode...
4370                    ## As if </head>
4371                    pop @{$self->{open_elements}};
4372                    
4373                    ## Reprocess in the "after head" insertion mode...
4374                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4375                    !!!cp ('t124');
4376                    pop @{$self->{open_elements}};
4377                    
4378                    ## Reprocess in the "after head" insertion mode...
4379                  } else {
4380                    !!!cp ('t125');
4381                  }
4382    
4383                  ## "after head" insertion mode
4384                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4385                  if ($token->{tag_name} eq 'body') {
4386                    !!!cp ('t126');
4387                    $self->{insertion_mode} = IN_BODY_IM;
4388                  } elsif ($token->{tag_name} eq 'frameset') {
4389                    !!!cp ('t127');
4390                    $self->{insertion_mode} = IN_FRAMESET_IM;
4391                  } else {
4392                    die "$0: tag name: $self->{tag_name}";
4393                  }
4394                  !!!nack ('t127.1');
4395                !!!next-token;                !!!next-token;
4396                redo B;                next B;
4397              } else {              } else {
4398                  !!!cp ('t128');
4399                #                #
4400              }              }
4401            } elsif ($token->{type} eq 'end tag') {  
4402              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4403                  $token->{tag_name} eq 'head') {                !!!cp ('t129');
4404                  ## As if </noscript>
4405                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4406                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4407                !!!next-token;                
4408                redo B;                ## Reprocess in the "in head" insertion mode...
4409              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
4410                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
               $self->{insertion_mode} = 'in head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head' and  
                      {  
                       body => 1, html => 1,  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                      {  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </head> or </noscript> or <body>  
           if ($self->{insertion_mode} eq 'in head') {  
             pop @{$self->{open_elements}};  
             $self->{insertion_mode} = 'after head';  
           } elsif ($self->{insertion_mode} eq 'in head noscript') {  
             pop @{$self->{open_elements}};  
             !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));  
             $self->{insertion_mode} = 'in head';  
           } else { # 'after head'  
             !!!insert-element ('body');  
             $self->{insertion_mode} = 'in body';  
           }  
           ## reprocess  
           redo B;  
4411    
4412            ## ISSUE: An issue in the spec.                ## Reprocess in the "after head" insertion mode...
4413          } elsif ($self->{insertion_mode} eq 'in body') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4414            if ($token->{type} eq 'character') {                !!!cp ('t130');
4415              ## NOTE: There is a code clone of "character in body".                ## As if </head>
4416              $reconstruct_active_formatting_elements->($insert_to_current);                pop @{$self->{open_elements}};
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4417    
4418              !!!next-token;                ## Reprocess in the "after head" insertion mode...
4419              redo B;              } else {
4420            } else {                !!!cp ('t131');
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
4421              }              }
4422    
4423              !!!parse-error (type => 'in table:#character');              ## "after head" insertion mode
4424                ## As if <body>
4425                !!!insert-element ('body',, $token);
4426                $self->{insertion_mode} = IN_BODY_IM;
4427                ## reprocess
4428                !!!ack-later;
4429                next B;
4430              } elsif ($token->{type} == END_TAG_TOKEN) {
4431                if ($token->{tag_name} eq 'head') {
4432                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4433                    !!!cp ('t132');
4434                    ## As if <head>
4435                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4436                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4437                    push @{$self->{open_elements}},
4438                        [$self->{head_element}, $el_category->{head}];
4439    
4440              ## As if in body, but insert into foster parent element                  ## Reprocess in the "in head" insertion mode...
4441              ## ISSUE: Spec says that "whenever a node would be inserted                  pop @{$self->{open_elements}};
4442              ## into the current node" while characters might not be                  $self->{insertion_mode} = AFTER_HEAD_IM;
4443              ## result in a new Text node.                  !!!next-token;
4444              $reconstruct_active_formatting_elements->($insert_to_foster);                  next B;
4445                              } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4446              if ({                  !!!cp ('t133');
4447                   table => 1, tbody => 1, tfoot => 1,                  ## As if </noscript>
4448                   thead => 1, tr => 1,                  pop @{$self->{open_elements}};
4449                  }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'in noscript:/head', token => $token);
4450                # MUST                  
4451                my $foster_parent_element;                  ## Reprocess in the "in head" insertion mode...
4452                my $next_sibling;                  pop @{$self->{open_elements}};
4453                my $prev_sibling;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4454                OE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!next-token;
4455                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  next B;
4456                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4457                    if (defined $parent and $parent->node_type == 1) {                  !!!cp ('t134');
4458                      $foster_parent_element = $parent;                  pop @{$self->{open_elements}};
4459                      $next_sibling = $self->{open_elements}->[$_]->[0];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4460                      $prev_sibling = $next_sibling->previous_sibling;                  !!!next-token;
4461                    } else {                  next B;
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
4462                } else {                } else {
4463                  $foster_parent_element->insert_before                  !!!cp ('t135');
4464                    ($self->{document}->create_text_node ($token->{data}),                  #
                    $next_sibling);  
4465                }                }
4466              } else {              } elsif ($token->{tag_name} eq 'noscript') {
4467                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4468              }                  !!!cp ('t136');
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4469                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4470                    $self->{insertion_mode} = IN_HEAD_IM;
4471                    !!!next-token;
4472                    next B;
4473                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4474                    !!!cp ('t137');
4475                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4476                    ## Ignore the token ## ISSUE: An issue in the spec.
4477                    !!!next-token;
4478                    next B;
4479                  } else {
4480                    !!!cp ('t138');
4481                    #
4482                }                }
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
4483              } elsif ({              } elsif ({
4484                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
4485                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4486                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4487                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  !!!cp ('t139');
4488                       $self->{open_elements}->[-1]->[1] ne 'html') {                  ## As if <head>
4489                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4490                  pop @{$self->{open_elements}};                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4491                }                  push @{$self->{open_elements}},
4492                        [$self->{head_element}, $el_category->{head}];
4493                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
4494                $self->{insertion_mode} = $token->{tag_name} eq 'col'                  $self->{insertion_mode} = IN_HEAD_IM;
4495                  ? 'in column group' : 'in table body';                  ## Reprocess in the "in head" insertion mode...
4496                ## reprocess                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4497                redo B;                  !!!cp ('t140');
4498              } elsif ($token->{tag_name} eq 'table') {                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4499                ## NOTE: There are code clones for this "table in table"                  ## Ignore the token
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
4500                  !!!next-token;                  !!!next-token;
4501                  redo B;                  next B;
4502                }                } else {
4503                                  !!!cp ('t141');
               ## 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; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4504                }                }
4505                  
4506                  #
4507                } elsif ({
4508                          p => 1, br => 1,
4509                         }->{$token->{tag_name}}) {
4510                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4511                    !!!cp ('t142');
4512                    ## As if <head>
4513                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4514                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4515                    push @{$self->{open_elements}},
4516                        [$self->{head_element}, $el_category->{head}];
4517    
4518                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
4519                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
4520                  } else {
4521                    !!!cp ('t143');
4522                }                }
4523    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
4524                #                #
4525              }              } else {
4526            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4527              if ($token->{tag_name} eq 'table') {                  !!!cp ('t144');
4528                ## have a table element in table scope                  #
4529                my $i;                } else {
4530                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!cp ('t145');
4531                  my $node = $self->{open_elements}->[$_];                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
                 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});  
4532                  ## Ignore the token                  ## Ignore the token
4533                  !!!next-token;                  !!!next-token;
4534                  redo B;                  next B;
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4535                }                }
4536                }
4537    
4538                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4539                  !!!cp ('t146');
4540                  ## As if </noscript>
4541                  pop @{$self->{open_elements}};
4542                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4543                  
4544                  ## Reprocess in the "in head" insertion mode...
4545                  ## As if </head>
4546                  pop @{$self->{open_elements}};
4547    
4548                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
4549                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4550                  !!!cp ('t147');
4551                  ## As if </head>
4552                  pop @{$self->{open_elements}};
4553    
4554                  ## Reprocess in the "after head" insertion mode...
4555                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4556    ## ISSUE: This case cannot be reached?
4557                  !!!cp ('t148');
4558                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4559                  ## Ignore the token ## ISSUE: An issue in the spec.
4560                !!!next-token;                !!!next-token;
4561                redo B;                next B;
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
4562              } else {              } else {
4563                #                !!!cp ('t149');
4564              }              }
           } else {  
             #  
           }  
4565    
4566            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
4567            $in_body->($insert_to_foster);              ## As if <body>
4568            redo B;              !!!insert-element ('body',, $token);
4569          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
4570            if ($token->{type} eq 'character') {              ## reprocess
4571              ## NOTE: This is a code clone of "character in body".              next B;
4572          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4573            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4574              !!!cp ('t149.1');
4575    
4576              ## NOTE: As if <head>
4577              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4578              $self->{open_elements}->[-1]->[0]->append_child
4579                  ($self->{head_element});
4580              #push @{$self->{open_elements}},
4581              #    [$self->{head_element}, $el_category->{head}];
4582              #$self->{insertion_mode} = IN_HEAD_IM;
4583              ## NOTE: Reprocess.
4584    
4585              ## NOTE: As if </head>
4586              #pop @{$self->{open_elements}};
4587              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4588              ## NOTE: Reprocess.
4589              
4590              #
4591            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4592              !!!cp ('t149.2');
4593    
4594              ## NOTE: As if </head>
4595              pop @{$self->{open_elements}};
4596              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4597              ## NOTE: Reprocess.
4598    
4599              #
4600            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4601              !!!cp ('t149.3');
4602    
4603              !!!parse-error (type => 'in noscript:#eof', token => $token);
4604    
4605              ## As if </noscript>
4606              pop @{$self->{open_elements}};
4607              #$self->{insertion_mode} = IN_HEAD_IM;
4608              ## NOTE: Reprocess.
4609    
4610              ## NOTE: As if </head>
4611              pop @{$self->{open_elements}};
4612              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4613              ## NOTE: Reprocess.
4614    
4615              #
4616            } else {
4617              !!!cp ('t149.4');
4618              #
4619            }
4620    
4621            ## NOTE: As if <body>
4622            !!!insert-element ('body',, $token);
4623            $self->{insertion_mode} = IN_BODY_IM;
4624            ## NOTE: Reprocess.
4625            next B;
4626          } else {
4627            die "$0: $token->{type}: Unknown token type";
4628          }
4629    
4630              ## ISSUE: An issue in the spec.
4631        } elsif ($self->{insertion_mode} & BODY_IMS) {
4632              if ($token->{type} == CHARACTER_TOKEN) {
4633                !!!cp ('t150');
4634                ## NOTE: There is a code clone of "character in body".
4635              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4636                            
4637              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4638    
4639              !!!next-token;              !!!next-token;
4640              redo B;              next B;
4641            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
4642              if ({              if ({
4643                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4644                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4645                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4646                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
4647                    ## have an element in table scope
4648                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
4649                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
4650                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
4651                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
4652                  my $node = $self->{open_elements}->[$_];  
4653                  if ($node->[1] eq 'caption') {                      ## Close the cell
4654                    $i = $_;                      !!!back-token; # <x>
4655                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
4656                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
4657                            table => 1, html => 1,                                line => $token->{line},
4658                           }->{$node->[1]}) {                                column => $token->{column}};
4659                    last INSCOPE;                      next B;
4660                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4661                        !!!cp ('t152');
4662                        ## ISSUE: This case can never be reached, maybe.
4663                        last;
4664                      }
4665                  }                  }
4666                } # INSCOPE  
4667                unless (defined $i) {                  !!!cp ('t153');
4668                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
4669                        value => $token->{tag_name}, token => $token);
4670                  ## Ignore the token                  ## Ignore the token
4671                    !!!nack ('t153.1');
4672                  !!!next-token;                  !!!next-token;
4673                  redo B;                  next B;
4674                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4675                                  !!!parse-error (type => 'not closed:caption', token => $token);
4676                ## generate implied end tags                  
4677                if ({                  ## NOTE: As if </caption>.
4678                     dd => 1, dt => 1, li => 1, p => 1,                  ## have a table element in table scope
4679                     td => 1, th => 1, tr => 1,                  my $i;
4680                     tbody => 1, tfoot=> 1, thead => 1,                  INSCOPE: {
4681                    }->{$self->{open_elements}->[-1]->[1]}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4682                  !!!back-token; # <?>                      my $node = $self->{open_elements}->[$_];
4683                  $token = {type => 'end tag', tag_name => 'caption'};                      if ($node->[1] & CAPTION_EL) {
4684                  !!!back-token;                        !!!cp ('t155');
4685                  $token = {type => 'end tag',                        $i = $_;
4686                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        last INSCOPE;
4687                  redo B;                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4688                }                        !!!cp ('t156');
4689                          last;
4690                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                      }
4691                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    }
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
4692    
4693                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
4694                      !!!parse-error (type => 'start tag not allowed',
4695                                      value => $token->{tag_name}, token => $token);
4696                      ## Ignore the token
4697                      !!!nack ('t157.1');
4698                      !!!next-token;
4699                      next B;
4700                    } # INSCOPE
4701                    
4702                    ## generate implied end tags
4703                    while ($self->{open_elements}->[-1]->[1]
4704                               & END_TAG_OPTIONAL_EL) {
4705                      !!!cp ('t158');
4706                      pop @{$self->{open_elements}};
4707                    }
4708    
4709                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4710                redo B;                    !!!cp ('t159');
4711                      !!!parse-error (type => 'not closed',
4712                                      value => $self->{open_elements}->[-1]->[0]
4713                                          ->manakai_local_name,
4714                                      token => $token);
4715                    } else {
4716                      !!!cp ('t160');
4717                    }
4718                    
4719                    splice @{$self->{open_elements}}, $i;
4720                    
4721                    $clear_up_to_marker->();
4722                    
4723                    $self->{insertion_mode} = IN_TABLE_IM;
4724                    
4725                    ## reprocess
4726                    !!!ack-later;
4727                    next B;
4728                  } else {
4729                    !!!cp ('t161');
4730                    #
4731                  }
4732              } else {              } else {
4733                  !!!cp ('t162');
4734                #                #
4735              }              }
4736            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4737              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4738                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
4739                my $i;                  ## have an element in table scope
4740                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
4741                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4742                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4743                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4744                    last INSCOPE;                      !!!cp ('t163');
4745                  } elsif ({                      $i = $_;
4746                            table => 1, html => 1,                      last INSCOPE;
4747                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4748                    last INSCOPE;                      !!!cp ('t164');
4749                        last INSCOPE;
4750                      }
4751                    } # INSCOPE
4752                      unless (defined $i) {
4753                        !!!cp ('t165');
4754                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4755                        ## Ignore the token
4756                        !!!next-token;
4757                        next B;
4758                      }
4759                    
4760                    ## generate implied end tags
4761                    while ($self->{open_elements}->[-1]->[1]
4762                               & END_TAG_OPTIONAL_EL) {
4763                      !!!cp ('t166');
4764                      pop @{$self->{open_elements}};
4765                  }                  }
4766                } # INSCOPE  
4767                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4768                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
4769                      !!!cp ('t167');
4770                      !!!parse-error (type => 'not closed',
4771                                      value => $self->{open_elements}->[-1]->[0]
4772                                          ->manakai_local_name,
4773                                      token => $token);
4774                    } else {
4775                      !!!cp ('t168');
4776                    }
4777                    
4778                    splice @{$self->{open_elements}}, $i;
4779                    
4780                    $clear_up_to_marker->();
4781                    
4782                    $self->{insertion_mode} = IN_ROW_IM;
4783                    
4784                    !!!next-token;
4785                    next B;
4786                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4787                    !!!cp ('t169');
4788                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4789                  ## Ignore the token                  ## Ignore the token
4790                  !!!next-token;                  !!!next-token;
4791                  redo B;                  next B;
4792                }                } else {
4793                                  !!!cp ('t170');
4794                ## generate implied end tags                  #
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4795                }                }
4796                } elsif ($token->{tag_name} eq 'caption') {
4797                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
4798                    ## have a table element in table scope
4799                    my $i;
4800                    INSCOPE: {
4801                      for (reverse 0..$#{$self->{open_elements}}) {
4802                        my $node = $self->{open_elements}->[$_];
4803                        if ($node->[1] & CAPTION_EL) {
4804                          !!!cp ('t171');
4805                          $i = $_;
4806                          last INSCOPE;
4807                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
4808                          !!!cp ('t172');
4809                          last;
4810                        }
4811                      }
4812    
4813                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
4814                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
4815                                      value => $token->{tag_name}, token => $token);
4816                      ## Ignore the token
4817                      !!!next-token;
4818                      next B;
4819                    } # INSCOPE
4820                    
4821                    ## generate implied end tags
4822                    while ($self->{open_elements}->[-1]->[1]
4823                               & END_TAG_OPTIONAL_EL) {
4824                      !!!cp ('t174');
4825                      pop @{$self->{open_elements}};
4826                    }
4827                    
4828                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4829                      !!!cp ('t175');
4830                      !!!parse-error (type => 'not closed',
4831                                      value => $self->{open_elements}->[-1]->[0]
4832                                          ->manakai_local_name,
4833                                      token => $token);
4834                    } else {
4835                      !!!cp ('t176');
4836                    }
4837                    
4838                    splice @{$self->{open_elements}}, $i;
4839                    
4840                    $clear_up_to_marker->();
4841                    
4842                    $self->{insertion_mode} = IN_TABLE_IM;
4843                    
4844                    !!!next-token;
4845                    next B;
4846                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4847                    !!!cp ('t177');
4848                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4849                    ## Ignore the token
4850                    !!!next-token;
4851                    next B;
4852                  } else {
4853                    !!!cp ('t178');
4854                    #
4855                }                }
4856                } elsif ({
4857                          table => 1, tbody => 1, tfoot => 1,
4858                          thead => 1, tr => 1,
4859                         }->{$token->{tag_name}} and
4860                         $self->{insertion_mode} == IN_CELL_IM) {
4861                  ## have an element in table scope
4862                  my $i;
4863                  my $tn;
4864                  INSCOPE: {
4865                    for (reverse 0..$#{$self->{open_elements}}) {
4866                      my $node = $self->{open_elements}->[$_];
4867                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4868                        !!!cp ('t179');
4869                        $i = $_;
4870    
4871                        ## Close the cell
4872                        !!!back-token; # </x>
4873                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
4874                                  line => $token->{line},
4875                                  column => $token->{column}};
4876                        next B;
4877                      } elsif ($node->[1] & TABLE_CELL_EL) {
4878                        !!!cp ('t180');
4879                        $tn = $node->[0]->manakai_local_name;
4880                        ## NOTE: There is exactly one |td| or |th| element
4881                        ## in scope in the stack of open elements by definition.
4882                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4883                        ## ISSUE: Can this be reached?
4884                        !!!cp ('t181');
4885                        last;
4886                      }
4887                    }
4888    
4889                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
4890                    !!!parse-error (type => 'unmatched end tag',
4891                $clear_up_to_marker->();                      value => $token->{tag_name}, token => $token);
4892                    ## Ignore the token
4893                $self->{insertion_mode} = 'in table';                  !!!next-token;
4894                    next B;
4895                !!!next-token;                } # INSCOPE
4896                redo B;              } elsif ($token->{tag_name} eq 'table' and
4897              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4898                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4899    
4900                ## As if </caption>                ## As if </caption>
4901                ## have a table element in table scope                ## have a table element in table scope
4902                my $i;                my $i;
4903                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4904                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4905                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4906                      !!!cp ('t184');
4907                    $i = $_;                    $i = $_;
4908                    last INSCOPE;                    last INSCOPE;
4909                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4910                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4911                    last INSCOPE;                    last INSCOPE;
4912                  }                  }
4913                } # INSCOPE                } # INSCOPE
4914                unless (defined $i) {                unless (defined $i) {
4915                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4916                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4917                  ## Ignore the token                  ## Ignore the token
4918                  !!!next-token;                  !!!next-token;
4919                  redo B;                  next B;
4920                }                }
4921                                
4922                ## generate implied end tags                ## generate implied end tags
4923                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4924                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4925                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4926                }                }
4927    
4928                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4929                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4930                    !!!parse-error (type => 'not closed',
4931                                    value => $self->{open_elements}->[-1]->[0]
4932                                        ->manakai_local_name,
4933                                    token => $token);
4934                  } else {
4935                    !!!cp ('t189');
4936                }                }
4937    
4938                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4939    
4940                $clear_up_to_marker->();                $clear_up_to_marker->();
4941    
4942                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4943    
4944                ## reprocess                ## reprocess
4945                redo B;                next B;
4946              } elsif ({              } elsif ({
4947                        body => 1, col => 1, colgroup => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
4948                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4949                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4950                ## Ignore the token                  !!!cp ('t190');
4951                redo B;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
4952                  ## Ignore the token                  ## Ignore the token
4953                  !!!next-token;                  !!!next-token;
4954                  redo B;                  next B;
4955                } else {                } else {
4956                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
4957                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
4958                }                }
4959              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
4960                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
4961                          thead => 1, tr => 1,
4962                         }->{$token->{tag_name}} and
4963                         $self->{insertion_mode} == IN_CAPTION_IM) {
4964                  !!!cp ('t192');
4965                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4966                ## Ignore the token                ## Ignore the token
4967                !!!next-token;                !!!next-token;
4968                redo B;                next B;
4969              } else {              } else {
4970                #                !!!cp ('t193');
4971                  #
4972              }              }
4973            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4974              #          for my $entry (@{$self->{open_elements}}) {
4975              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4976                !!!cp ('t75');
4977                !!!parse-error (type => 'in body:#eof', token => $token);
4978                last;
4979            }            }
4980            }
4981    
4982            ## As if </colgroup>          ## Stop parsing.
4983            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
4984              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
4985              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
4986          }
4987    
4988          $insert = $insert_to_current;
4989          #
4990        } elsif ($self->{insertion_mode} & TABLE_IMS) {
4991          if ($token->{type} == CHARACTER_TOKEN) {
4992            if (not $open_tables->[-1]->[1] and # tainted
4993                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4994              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4995                  
4996              unless (length $token->{data}) {
4997                !!!cp ('t194');
4998              !!!next-token;              !!!next-token;
4999              redo B;              next B;
5000            } else {            } else {
5001              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5002            }            }
5003          } elsif ($self->{insertion_mode} eq 'in table body') {          }
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
5004    
5005              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5006    
5007              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5008              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5009              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5010              ## result in a new Text node.              ## result in a new Text node.
5011              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5012                
5013              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]}) {  
5014                # MUST                # MUST
5015                my $foster_parent_element;                my $foster_parent_element;
5016                my $next_sibling;                my $next_sibling;
5017                my $prev_sibling;                my $prev_sibling;
5018                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5019                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5020                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5021                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5022                        !!!cp ('t196');
5023                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5024                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5025                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5026                    } else {                    } else {
5027                        !!!cp ('t197');
5028                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5029                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5030                    }                    }
# Line 4145  sub _tree_construction_main ($) { Line 5036  sub _tree_construction_main ($) {
5036                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5037                if (defined $prev_sibling and                if (defined $prev_sibling and
5038                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5039                    !!!cp ('t198');
5040                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5041                } else {                } else {
5042                    !!!cp ('t199');
5043                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5044                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5045                     $next_sibling);                     $next_sibling);
5046                }                }
5047              } else {            $open_tables->[-1]->[1] = 1; # tainted
5048                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5049              }            !!!cp ('t200');
5050              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5051            }
5052                            
5053              !!!next-token;          !!!next-token;
5054              redo B;          next B;
5055            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
5056              if ({              if ({
5057                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
5058                   th => 1, td => 1,                   th => 1, td => 1,
5059                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5060                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5061                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
5062                    while (not ($self->{open_elements}->[-1]->[1]
5063                                    & TABLE_SCOPING_EL)) {
5064                      !!!cp ('t201');
5065                      pop @{$self->{open_elements}};
5066                    }
5067                    
5068                    !!!insert-element ('tbody',, $token);
5069                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5070                    ## reprocess in the "in table body" insertion mode...
5071                }                }
5072    
5073                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5074                while (not {                  unless ($token->{tag_name} eq 'tr') {
5075                  tbody => 1, tfoot => 1, thead => 1, html => 1,                    !!!cp ('t202');
5076                }->{$self->{open_elements}->[-1]->[1]}) {                    !!!parse-error (type => 'missing start tag:tr', token => $token);
5077                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  }
5078                    
5079                    ## Clear back to table body context
5080                    while (not ($self->{open_elements}->[-1]->[1]
5081                                    & TABLE_ROWS_SCOPING_EL)) {
5082                      !!!cp ('t203');
5083                      ## ISSUE: Can this case be reached?
5084                      pop @{$self->{open_elements}};
5085                    }
5086                    
5087                    $self->{insertion_mode} = IN_ROW_IM;
5088                    if ($token->{tag_name} eq 'tr') {
5089                      !!!cp ('t204');
5090                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5091                      !!!nack ('t204');
5092                      !!!next-token;
5093                      next B;
5094                    } else {
5095                      !!!cp ('t205');
5096                      !!!insert-element ('tr',, $token);
5097                      ## reprocess in the "in row" insertion mode
5098                    }
5099                  } else {
5100                    !!!cp ('t206');
5101                  }
5102    
5103                  ## Clear back to table row context
5104                  while (not ($self->{open_elements}->[-1]->[1]
5105                                  & TABLE_ROW_SCOPING_EL)) {
5106                    !!!cp ('t207');
5107                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5108                }                }
5109                                
5110                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5111                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5112                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5113                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5114                } else {                
5115                  !!!insert-element ('tr');                !!!nack ('t207.1');
5116                  ## reprocess                !!!next-token;
5117                }                next B;
               redo B;  
5118              } elsif ({              } elsif ({
5119                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5120                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5121                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5122                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5123                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5124                my $i;                  ## As if </tr>
5125                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5126                  my $node = $self->{open_elements}->[$_];                  my $i;
5127                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5128                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5129                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5130                    $i = $_;                      !!!cp ('t208');
5131                    last INSCOPE;                      $i = $_;
5132                  } elsif ({                      last INSCOPE;
5133                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5134                           }->{$node->[1]}) {                      !!!cp ('t209');
5135                    last INSCOPE;                      last INSCOPE;
5136                      }
5137                    } # INSCOPE
5138                    unless (defined $i) {
5139                      !!!cp ('t210');
5140    ## TODO: This type is wrong.
5141                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5142                      ## Ignore the token
5143                      !!!nack ('t210.1');
5144                      !!!next-token;
5145                      next B;
5146                    }
5147                    
5148                    ## Clear back to table row context
5149                    while (not ($self->{open_elements}->[-1]->[1]
5150                                    & TABLE_ROW_SCOPING_EL)) {
5151                      !!!cp ('t211');
5152                      ## ISSUE: Can this case be reached?
5153                      pop @{$self->{open_elements}};
5154                    }
5155                    
5156                    pop @{$self->{open_elements}}; # tr
5157                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5158                    if ($token->{tag_name} eq 'tr') {
5159                      !!!cp ('t212');
5160                      ## reprocess
5161                      !!!ack-later;
5162                      next B;
5163                    } else {
5164                      !!!cp ('t213');
5165                      ## reprocess in the "in table body" insertion mode...
5166                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5167                }                }
5168    
5169                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5170                while (not {                  ## have an element in table scope
5171                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5172                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5173                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5174                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5175                        !!!cp ('t214');
5176                        $i = $_;
5177                        last INSCOPE;
5178                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5179                        !!!cp ('t215');
5180                        last INSCOPE;
5181                      }
5182                    } # INSCOPE
5183                    unless (defined $i) {
5184                      !!!cp ('t216');
5185    ## TODO: This erorr type ios wrong.
5186                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5187                      ## Ignore the token
5188                      !!!nack ('t216.1');
5189                      !!!next-token;
5190                      next B;
5191                    }
5192    
5193                    ## Clear back to table body context
5194                    while (not ($self->{open_elements}->[-1]->[1]
5195                                    & TABLE_ROWS_SCOPING_EL)) {
5196                      !!!cp ('t217');
5197                      ## ISSUE: Can this state be reached?
5198                      pop @{$self->{open_elements}};
5199                    }
5200                    
5201                    ## As if <{current node}>
5202                    ## have an element in table scope
5203                    ## true by definition
5204                    
5205                    ## Clear back to table body context
5206                    ## nop by definition
5207                    
5208                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5209                    $self->{insertion_mode} = IN_TABLE_IM;
5210                    ## reprocess in "in table" insertion mode...
5211                  } else {
5212                    !!!cp ('t218');
5213                }                }
5214    
5215                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5216                ## have an element in table scope                  ## Clear back to table context
5217                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5218                                    & TABLE_SCOPING_EL)) {
5219                ## Clear back to table body context                    !!!cp ('t219');
5220                ## nop by definition                    ## ISSUE: Can this state be reached?
5221                      pop @{$self->{open_elements}};
5222                pop @{$self->{open_elements}};                  }
5223                $self->{insertion_mode} = 'in table';                  
5224                ## reprocess                  !!!insert-element ('colgroup',, $token);
5225                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5226                    ## reprocess
5227                    !!!ack-later;
5228                    next B;
5229                  } elsif ({
5230                            caption => 1,
5231                            colgroup => 1,
5232                            tbody => 1, tfoot => 1, thead => 1,
5233                           }->{$token->{tag_name}}) {
5234                    ## Clear back to table context
5235                    while (not ($self->{open_elements}->[-1]->[1]
5236                                    & TABLE_SCOPING_EL)) {
5237                      !!!cp ('t220');
5238                      ## ISSUE: Can this state be reached?
5239                      pop @{$self->{open_elements}};
5240                    }
5241                    
5242                    push @$active_formatting_elements, ['#marker', '']
5243                        if $token->{tag_name} eq 'caption';
5244                    
5245                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5246                    $self->{insertion_mode} = {
5247                                               caption => IN_CAPTION_IM,
5248                                               colgroup => IN_COLUMN_GROUP_IM,
5249                                               tbody => IN_TABLE_BODY_IM,
5250                                               tfoot => IN_TABLE_BODY_IM,
5251                                               thead => IN_TABLE_BODY_IM,
5252                                              }->{$token->{tag_name}};
5253                    !!!next-token;
5254                    !!!nack ('t220.1');
5255                    next B;
5256                  } else {
5257                    die "$0: in table: <>: $token->{tag_name}";
5258                  }
5259              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5260                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5261                !!!parse-error (type => 'not closed:table');                                value => $self->{open_elements}->[-1]->[0]
5262                                      ->manakai_local_name,
5263                                  token => $token);
5264    
5265                ## As if </table>                ## As if </table>
5266                ## have a table element in table scope                ## have a table element in table scope
5267                my $i;                my $i;
5268                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5269                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5270                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5271                      !!!cp ('t221');
5272                    $i = $_;                    $i = $_;
5273                    last INSCOPE;                    last INSCOPE;
5274                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5275                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5276                    last INSCOPE;                    last INSCOPE;
5277                  }                  }
5278                } # INSCOPE                } # INSCOPE
5279                unless (defined $i) {                unless (defined $i) {
5280                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5281    ## TODO: The following is wrong, maybe.
5282                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5283                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5284                    !!!nack ('t223.1');
5285                  !!!next-token;                  !!!next-token;
5286                  redo B;                  next B;
5287                }                }
5288                                
5289    ## TODO: Followings are removed from the latest spec.
5290                ## generate implied end tags                ## generate implied end tags
5291                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5292                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5293                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5294                }                }
5295    
5296                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5297                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5298                    ## NOTE: |<table><tr><table>|
5299                    !!!parse-error (type => 'not closed',
5300                                    value => $self->{open_elements}->[-1]->[0]
5301                                        ->manakai_local_name,
5302                                    token => $token);
5303                  } else {
5304                    !!!cp ('t226');
5305                }                }
5306    
5307                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5308                  pop @{$open_tables};
5309    
5310                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5311    
5312                ## reprocess            ## reprocess
5313                redo B;            !!!ack-later;
5314              } else {            next B;
5315                #          } elsif ($token->{tag_name} eq 'style') {
5316              }            if (not $open_tables->[-1]->[1]) { # tainted
5317            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5318              if ({              ## NOTE: This is a "as if in head" code clone.
5319                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5320                  }->{$token->{tag_name}}) {              next B;
5321                ## have an element in table scope            } else {
5322                my $i;              !!!cp ('t227.7');
5323                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5324                  my $node = $self->{open_elements}->[$_];            }
5325                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5326                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5327                    last INSCOPE;              !!!cp ('t227.6');
5328                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5329                            table => 1, html => 1,              $script_start_tag->();
5330                           }->{$node->[1]}) {              next B;
5331                    last INSCOPE;            } else {
5332                  }              !!!cp ('t227.5');
5333                } # INSCOPE              #
5334                unless (defined $i) {            }
5335                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5336                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5337                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5338                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5339                }                if ($type eq 'hidden') {
5340                    !!!cp ('t227.3');
5341                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5342    
5343                ## Clear back to table body context                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
5344    
5345                pop @{$self->{open_elements}};                  ## TODO: form element pointer
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $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;  
               }  
5346    
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5347                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
5348    
5349                pop @{$self->{open_elements}};                  !!!next-token;
5350                $self->{insertion_mode} = 'in table';                  !!!ack ('t227.2.1');
5351                ## reprocess                  next B;
5352                redo B;                } else {
5353              } elsif ({                  !!!cp ('t227.2');
5354                        body => 1, caption => 1, col => 1, colgroup => 1,                  #
5355                        html => 1, td => 1, th => 1, tr => 1,                }
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
5356              } else {              } else {
5357                  !!!cp ('t227.1');
5358                #                #
5359              }              }
5360            } else {            } else {
5361                !!!cp ('t227.4');
5362              #              #
5363            }            }
5364                      } else {
5365            ## As if in table            !!!cp ('t227');
5366            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5367            $in_body->($insert_to_foster);          }
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
5368    
5369              ## As if in body, but insert into foster parent element          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
5370    
5371                push @$active_formatting_elements, ['#marker', ''];          $insert = $insert_to_foster;
5372                          #
5373                !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
5374                redo B;              if ($token->{tag_name} eq 'tr' and
5375              } elsif ({                  $self->{insertion_mode} == IN_ROW_IM) {
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
5376                ## have an element in table scope                ## have an element in table scope
5377                my $i;                my $i;
5378                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5379                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5380                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5381                      !!!cp ('t228');
5382                    $i = $_;                    $i = $_;
5383                    last INSCOPE;                    last INSCOPE;
5384                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5385                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5386                    last INSCOPE;                    last INSCOPE;
5387                  }                  }
5388                } # INSCOPE                } # INSCOPE
5389                unless (defined $i) {                unless (defined $i) {
5390                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5391                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5392                  ## Ignore the token                  ## Ignore the token
5393                    !!!nack ('t230.1');
5394                  !!!next-token;                  !!!next-token;
5395                  redo B;                  next B;
5396                  } else {
5397                    !!!cp ('t232');
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 ('t231');
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    
5408                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5409                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5410                ## reprocess                !!!next-token;
5411                redo B;                !!!nack ('t231.1');
5412                  next B;
5413              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5414                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5415                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5416                    ## have an element in table scope
5417                ## As if </table>                  my $i;
5418                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5419                my $i;                    my $node = $self->{open_elements}->[$_];
5420                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5421                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5422                  if ($node->[1] eq 'table') {                      $i = $_;
5423                    $i = $_;                      last INSCOPE;
5424                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5425                  } elsif ({                      !!!cp ('t234');
5426                            table => 1, html => 1,                      last INSCOPE;
5427                           }->{$node->[1]}) {                    }
5428                    last INSCOPE;                  } # INSCOPE
5429                    unless (defined $i) {
5430                      !!!cp ('t235');
5431    ## TODO: The following is wrong.
5432                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5433                      ## Ignore the token
5434                      !!!nack ('t236.1');
5435                      !!!next-token;
5436                      next B;
5437                  }                  }
5438                } # INSCOPE                  
5439                unless (defined $i) {                  ## Clear back to table row context
5440                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5441                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5442                  !!!next-token;                    !!!cp ('t236');
5443                  redo B;  ## ISSUE: Can this state be reached?
5444                }                    pop @{$self->{open_elements}};
                 
               ## 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; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## 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;  
5445                  }                  }
5446                } # INSCOPE                  
5447                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5448                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5449                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5450                  !!!next-token;                }
5451                  redo B;  
5452                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5453                    ## have an element in table scope
5454                ## Clear back to table row context                  my $i;
5455                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5456                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5457                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5458                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5459                        $i = $_;
5460                        last INSCOPE;
5461                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5462                        !!!cp ('t238');
5463                        last INSCOPE;
5464                      }
5465                    } # INSCOPE
5466                    unless (defined $i) {
5467                      !!!cp ('t239');
5468                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5469                      ## Ignore the token
5470                      !!!nack ('t239.1');
5471                      !!!next-token;
5472                      next B;
5473                    }
5474                    
5475                    ## Clear back to table body context
5476                    while (not ($self->{open_elements}->[-1]->[1]
5477                                    & TABLE_ROWS_SCOPING_EL)) {
5478                      !!!cp ('t240');
5479                      pop @{$self->{open_elements}};
5480                    }
5481                    
5482                    ## As if <{current node}>
5483                    ## have an element in table scope
5484                    ## true by definition
5485                    
5486                    ## Clear back to table body context
5487                    ## nop by definition
5488                    
5489                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5490                    $self->{insertion_mode} = IN_TABLE_IM;
5491                    ## reprocess in the "in table" insertion mode...
5492                }                }
5493    
5494                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
5495                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
5496                !!!next-token;                ## the code for <table> in the "in table" insertion mode
5497                redo B;                ## is synced with it.
5498              } elsif ($token->{tag_name} eq 'table') {  
5499                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
5500                my $i;                my $i;
5501                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5502                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5503                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
5504                      !!!cp ('t241');
5505                    $i = $_;                    $i = $_;
5506                    last INSCOPE;                    last INSCOPE;
5507                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5508                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5509                    last INSCOPE;                    last INSCOPE;
5510                  }                  }
5511                } # INSCOPE                } # INSCOPE
5512                unless (defined $i) {                unless (defined $i) {
5513                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
5514                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5515                  ## Ignore the token                  ## Ignore the token
5516                    !!!nack ('t243.1');
5517                  !!!next-token;                  !!!next-token;
5518                  redo B;                  next B;
5519                }                }
5520                    
5521                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
5522                while (not {                pop @{$open_tables};
5523                  tr => 1, html => 1,                
5524                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
5525                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
5526                  pop @{$self->{open_elements}};                !!!next-token;
5527                }                next B;
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
5528              } elsif ({              } elsif ({
5529                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5530                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
5531                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
5532                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
5533                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5534                  my $node = $self->{open_elements}->[$_];                  my $i;
5535                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5536                    $i = $_;                    my $node = $self->{open_elements}->[$_];
5537                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5538                  } elsif ({                      !!!cp ('t247');
5539                            table => 1, html => 1,                      $i = $_;
5540                           }->{$node->[1]}) {                      last INSCOPE;
5541                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5542                        !!!cp ('t248');
5543                        last INSCOPE;
5544                      }
5545                    } # INSCOPE
5546                      unless (defined $i) {
5547                        !!!cp ('t249');
5548                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5549                        ## Ignore the token
5550                        !!!nack ('t249.1');
5551                        !!!next-token;
5552                        next B;
5553                      }
5554                    
5555                    ## As if </tr>
5556                    ## have an element in table scope
5557                    my $i;
5558                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5559                      my $node = $self->{open_elements}->[$_];
5560                      if ($node->[1] & TABLE_ROW_EL) {
5561                        !!!cp ('t250');
5562                        $i = $_;
5563                        last INSCOPE;
5564                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5565                        !!!cp ('t251');
5566                        last INSCOPE;
5567                      }
5568                    } # INSCOPE
5569                      unless (defined $i) {
5570                        !!!cp ('t252');
5571                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5572                        ## Ignore the token
5573                        !!!nack ('t252.1');
5574                        !!!next-token;
5575                        next B;
5576                      }
5577                    
5578                    ## Clear back to table row context
5579                    while (not ($self->{open_elements}->[-1]->[1]
5580                                    & TABLE_ROW_SCOPING_EL)) {
5581                      !!!cp ('t253');
5582    ## ISSUE: Can this case be reached?
5583                      pop @{$self->{open_elements}};
5584                  }                  }
5585                } # INSCOPE                  
5586                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5587                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5588                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
5589                }                }
5590    
               ## As if </tr>  
5591                ## have an element in table scope                ## have an element in table scope
5592                my $i;                my $i;
5593                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5594                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5595                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5596                      !!!cp ('t254');
5597                    $i = $_;                    $i = $_;
5598                    last INSCOPE;                    last INSCOPE;
5599                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5600                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5601                    last INSCOPE;                    last INSCOPE;
5602                  }                  }
5603                } # INSCOPE                } # INSCOPE
5604                unless (defined $i) {                unless (defined $i) {
5605                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
5606                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5607                  ## Ignore the token                  ## Ignore the token
5608                    !!!nack ('t256.1');
5609                  !!!next-token;                  !!!next-token;
5610                  redo B;                  next B;
5611                }                }
5612    
5613                ## Clear back to table row context                ## Clear back to table body context
5614                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5615                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5616                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5617                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5618                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5619                }                }
5620    
5621                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
5622                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
5623                ## reprocess                !!!nack ('t257.1');
5624                redo B;                !!!next-token;
5625                  next B;
5626              } elsif ({              } elsif ({
5627                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5628                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5629                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5630                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5631                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5632                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5633                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5634                !!!next-token;            ## Ignore the token
5635                redo B;            !!!nack ('t258.1');
5636              } else {             !!!next-token;
5637                #            next B;
5638              }          } else {
5639            } else {            !!!cp ('t259');
5640              #            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
           }  
5641    
5642            ## As if in table            $insert = $insert_to_foster;
5643            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5644            $in_body->($insert_to_foster);          }
5645            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5646          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5647            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
5648              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
5649              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
5650                          #
5651              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5652              !!!cp ('t259.2');
5653              #
5654            }
5655    
5656              !!!next-token;          ## Stop parsing
5657              redo B;          last B;
5658            } elsif ($token->{type} eq 'start tag') {        } else {
5659              if ({          die "$0: $token->{type}: Unknown token type";
5660                   caption => 1, col => 1, colgroup => 1,        }
5661                   tbody => 1, td => 1, tfoot => 1, th => 1,      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5662                   thead => 1, tr => 1,            if ($token->{type} == CHARACTER_TOKEN) {
5663                  }->{$token->{tag_name}}) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5664                ## have an element in table scope                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5665                my $tn;                unless (length $token->{data}) {
5666                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!cp ('t260');
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
5667                  !!!next-token;                  !!!next-token;
5668                  redo B;                  next B;
5669                }                }
5670                }
5671                ## Close the cell              
5672                !!!back-token; # <?>              !!!cp ('t261');
5673                $token = {type => 'end tag', tag_name => $tn};              #
5674                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
5675              } else {              if ($token->{tag_name} eq 'col') {
5676                  !!!cp ('t262');
5677                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5678                  pop @{$self->{open_elements}};
5679                  !!!ack ('t262.1');
5680                  !!!next-token;
5681                  next B;
5682                } else {
5683                  !!!cp ('t263');
5684                #                #
5685              }              }
5686            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5687              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
5688                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5689                my $i;                  !!!cp ('t264');
5690                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5691                  ## Ignore the token                  ## Ignore the token
5692                  !!!next-token;                  !!!next-token;
5693                  redo B;                  next B;
5694                }                } else {
5695                                  !!!cp ('t265');
5696                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
5697                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
5698                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
5699                     td => ($token->{tag_name} eq 'th'),                  next B;            
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5700                }                }
5701                } elsif ($token->{tag_name} eq 'col') {
5702                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
5703                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5704                ## Ignore the token                ## Ignore the token
5705                !!!next-token;                !!!next-token;
5706                redo B;                next B;
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } 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;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
5707              } else {              } else {
5708                #                !!!cp ('t267');
5709                  #
5710              }              }
5711          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5712            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5713                @{$self->{open_elements}} == 1) { # redundant, maybe
5714              !!!cp ('t270.2');
5715              ## Stop parsing.
5716              last B;
5717            } else {
5718              ## NOTE: As if </colgroup>.
5719              !!!cp ('t270.1');
5720              pop @{$self->{open_elements}}; # colgroup
5721              $self->{insertion_mode} = IN_TABLE_IM;
5722              ## Reprocess.
5723              next B;
5724            }
5725          } else {
5726            die "$0: $token->{type}: Unknown token type";
5727          }
5728    
5729              ## As if </colgroup>
5730              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5731                !!!cp ('t269');
5732    ## TODO: Wrong error type?
5733                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5734                ## Ignore the token
5735                !!!nack ('t269.1');
5736                !!!next-token;
5737                next B;
5738            } else {            } else {
5739              #              !!!cp ('t270');
5740                pop @{$self->{open_elements}}; # colgroup
5741                $self->{insertion_mode} = IN_TABLE_IM;
5742                !!!ack-later;
5743                ## reprocess
5744                next B;
5745              }
5746        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5747          if ($token->{type} == CHARACTER_TOKEN) {
5748            !!!cp ('t271');
5749            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5750            !!!next-token;
5751            next B;
5752          } elsif ($token->{type} == START_TAG_TOKEN) {
5753            if ($token->{tag_name} eq 'option') {
5754              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5755                !!!cp ('t272');
5756                ## As if </option>
5757                pop @{$self->{open_elements}};
5758              } else {
5759                !!!cp ('t273');
5760            }            }
             
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
5761    
5762                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5763                !!!next-token;            !!!nack ('t273.1');
5764                redo B;            !!!next-token;
5765              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5766                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5767                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5768                  pop @{$self->{open_elements}};              !!!cp ('t274');
5769                }              ## As if </option>
5770                pop @{$self->{open_elements}};
5771              } else {
5772                !!!cp ('t275');
5773              }
5774    
5775                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5776                  ## As if </optgroup>              !!!cp ('t276');
5777                  pop @{$self->{open_elements}};              ## As if </optgroup>
5778                }              pop @{$self->{open_elements}};
5779              } else {
5780                !!!cp ('t277');
5781              }
5782    
5783                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5784                !!!next-token;            !!!nack ('t277.1');
5785                redo B;            !!!next-token;
5786              } elsif ($token->{tag_name} eq 'select') {            next B;
5787                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5788                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5789                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5790                my $i;                    {
5791                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5792                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5793                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5794                    $i = $_;                    }->{$token->{tag_name}})) {
5795                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5796                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5797                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5798                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5799                    last INSCOPE;            ## have an element in table scope
5800                  }            my $i;
5801                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5802                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5803                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5804                  ## Ignore the token                !!!cp ('t278');
5805                  !!!next-token;                $i = $_;
5806                  redo B;                last INSCOPE;
5807                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5808                  !!!cp ('t279');
5809                  last INSCOPE;
5810                }
5811              } # INSCOPE
5812              unless (defined $i) {
5813                !!!cp ('t280');
5814                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5815                ## Ignore the token
5816                !!!nack ('t280.1');
5817                !!!next-token;
5818                next B;
5819              }
5820                                
5821                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5822              splice @{$self->{open_elements}}, $i;
5823    
5824                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5825    
5826                !!!next-token;            if ($token->{tag_name} eq 'select') {
5827                redo B;              !!!nack ('t281.2');
5828              } else {              !!!next-token;
5829                #              next B;
5830              } else {
5831                !!!cp ('t281.1');
5832                !!!ack-later;
5833                ## Reprocess the token.
5834                next B;
5835              }
5836            } else {
5837              !!!cp ('t282');
5838              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5839              ## Ignore the token
5840              !!!nack ('t282.1');
5841              !!!next-token;
5842              next B;
5843            }
5844          } elsif ($token->{type} == END_TAG_TOKEN) {
5845            if ($token->{tag_name} eq 'optgroup') {
5846              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5847                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5848                !!!cp ('t283');
5849                ## As if </option>
5850                splice @{$self->{open_elements}}, -2;
5851              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5852                !!!cp ('t284');
5853                pop @{$self->{open_elements}};
5854              } else {
5855                !!!cp ('t285');
5856                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5857                ## Ignore the token
5858              }
5859              !!!nack ('t285.1');
5860              !!!next-token;
5861              next B;
5862            } elsif ($token->{tag_name} eq 'option') {
5863              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5864                !!!cp ('t286');
5865                pop @{$self->{open_elements}};
5866              } else {
5867                !!!cp ('t287');
5868                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5869                ## Ignore the token
5870              }
5871              !!!nack ('t287.1');
5872              !!!next-token;
5873              next B;
5874            } elsif ($token->{tag_name} eq 'select') {
5875              ## have an element in table scope
5876              my $i;
5877              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5878                my $node = $self->{open_elements}->[$_];
5879                if ($node->[1] & SELECT_EL) {
5880                  !!!cp ('t288');
5881                  $i = $_;
5882                  last INSCOPE;
5883                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5884                  !!!cp ('t289');
5885                  last INSCOPE;
5886              }              }
5887            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
5888              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
5889                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
5890                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5891                  ## As if </option>              ## Ignore the token
5892                  splice @{$self->{open_elements}}, -2;              !!!nack ('t290.1');
5893                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!next-token;
5894                  pop @{$self->{open_elements}};              next B;
5895                } 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;  
               }  
5896                                
5897                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5898              splice @{$self->{open_elements}}, $i;
5899    
5900                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5901    
5902                !!!next-token;            !!!nack ('t291.1');
5903                redo B;            !!!next-token;
5904              } elsif ({            next B;
5905                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5906                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5907                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5908                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5909                                   }->{$token->{tag_name}}) {
5910                ## have an element in table scope  ## TODO: The following is wrong?
5911                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;  
               }  
5912                                
5913                ## As if </select>            ## have an element in table scope
5914                ## have an element in table scope            my $i;
5915                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5916                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
5917                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5918                  if ($node->[1] eq 'select') {                !!!cp ('t292');
5919                    $i = $_;                $i = $_;
5920                    last INSCOPE;                last INSCOPE;
5921                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5922                            table => 1, html => 1,                !!!cp ('t293');
5923                           }->{$node->[1]}) {                last INSCOPE;
5924                    last INSCOPE;              }
5925                  }            } # INSCOPE
5926                } # INSCOPE            unless (defined $i) {
5927                unless (defined $i) {              !!!cp ('t294');
5928                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
5929                  ## Ignore the </select> token              !!!nack ('t294.1');
5930                  !!!next-token; ## TODO: ok?              !!!next-token;
5931                  redo B;              next B;
5932                }            }
5933                                
5934                splice @{$self->{open_elements}}, $i;            ## As if </select>
5935              ## have an element in table scope
5936                $self->_reset_insertion_mode;            undef $i;
5937              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5938                ## reprocess              my $node = $self->{open_elements}->[$_];
5939                redo B;              if ($node->[1] & SELECT_EL) {
5940              } else {                !!!cp ('t295');
5941                #                $i = $_;
5942                  last INSCOPE;
5943                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5944    ## ISSUE: Can this state be reached?
5945                  !!!cp ('t296');
5946                  last INSCOPE;
5947              }              }
5948            } else {            } # INSCOPE
5949              #            unless (defined $i) {
5950                !!!cp ('t297');
5951    ## TODO: The following error type is correct?
5952                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5953                ## Ignore the </select> token
5954                !!!nack ('t297.1');
5955                !!!next-token; ## TODO: ok?
5956                next B;
5957            }            }
5958                  
5959              !!!cp ('t298');
5960              splice @{$self->{open_elements}}, $i;
5961    
5962            !!!parse-error (type => 'in select:'.$token->{tag_name});            $self->_reset_insertion_mode;
5963    
5964              !!!ack-later;
5965              ## reprocess
5966              next B;
5967            } else {
5968              !!!cp ('t299');
5969              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5970            ## Ignore the token            ## Ignore the token
5971              !!!nack ('t299.3');
5972            !!!next-token;            !!!next-token;
5973            redo B;            next B;
5974          } elsif ($self->{insertion_mode} eq 'after body') {          }
5975            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5976              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5977                my $data = $1;                  @{$self->{open_elements}} == 1) { # redundant, maybe
5978                ## As if in body            !!!cp ('t299.1');
5979                $reconstruct_active_formatting_elements->($insert_to_current);            !!!parse-error (type => 'in body:#eof', token => $token);
5980            } else {
5981              !!!cp ('t299.2');
5982            }
5983    
5984            ## Stop parsing.
5985            last B;
5986          } else {
5987            die "$0: $token->{type}: Unknown token type";
5988          }
5989        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5990          if ($token->{type} == CHARACTER_TOKEN) {
5991            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5992              my $data = $1;
5993              ## As if in body
5994              $reconstruct_active_formatting_elements->($insert_to_current);
5995                                
5996                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5997              
5998              unless (length $token->{data}) {
5999                !!!cp ('t300');
6000                !!!next-token;
6001                next B;
6002              }
6003            }
6004            
6005            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6006              !!!cp ('t301');
6007              !!!parse-error (type => 'after html:#character', token => $token);
6008    
6009                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
6010                  !!!next-token;          } else {
6011                  redo B;            !!!cp ('t302');
6012                }          }
6013              }          
6014                        ## "after body" insertion mode
6015              #          !!!parse-error (type => 'after body:#character', token => $token);
6016              !!!parse-error (type => 'after body:#character');  
6017            } elsif ($token->{type} eq 'start tag') {          $self->{insertion_mode} = IN_BODY_IM;
6018              !!!parse-error (type => 'after body:'.$token->{tag_name});          ## reprocess
6019              #          next B;
6020            } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
6021              if ($token->{tag_name} eq 'html') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6022                if (defined $self->{inner_html_node}) {            !!!cp ('t303');
6023                  !!!parse-error (type => 'unmatched end tag:html');            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6024                  ## Ignore the token            
6025                  !!!next-token;            ## Reprocess in the "after body" insertion mode.
6026                  redo B;          } else {
6027                } else {            !!!cp ('t304');
6028                  $previous_insertion_mode = $self->{insertion_mode};          }
6029                  $self->{insertion_mode} = 'trailing end';  
6030                  !!!next-token;          ## "after body" insertion mode
6031                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6032                }  
6033              } else {          $self->{insertion_mode} = IN_BODY_IM;
6034                !!!parse-error (type => 'after body:/'.$token->{tag_name});          !!!ack-later;
6035              }          ## reprocess
6036            next B;
6037          } elsif ($token->{type} == END_TAG_TOKEN) {
6038            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6039              !!!cp ('t305');
6040              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6041              
6042              $self->{insertion_mode} = AFTER_BODY_IM;
6043              ## Reprocess in the "after body" insertion mode.
6044            } else {
6045              !!!cp ('t306');
6046            }
6047    
6048            ## "after body" insertion mode
6049            if ($token->{tag_name} eq 'html') {
6050              if (defined $self->{inner_html_node}) {
6051                !!!cp ('t307');
6052                !!!parse-error (type => 'unmatched end tag:html', token => $token);
6053                ## Ignore the token
6054                !!!next-token;
6055                next B;
6056            } else {            } else {
6057              die "$0: $token->{type}: Unknown token type";              !!!cp ('t308');
6058                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6059                !!!next-token;
6060                next B;
6061            }            }
6062            } else {
6063              !!!cp ('t309');
6064              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6065    
6066            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
6067            ## reprocess            ## reprocess
6068            redo B;            next B;
6069      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6070        if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6071            !!!cp ('t309.2');
6072            ## Stop parsing
6073            last B;
6074          } else {
6075            die "$0: $token->{type}: Unknown token type";
6076          }
6077        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6078          if ($token->{type} == CHARACTER_TOKEN) {
6079          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6080            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6081              
6082            unless (length $token->{data}) {            unless (length $token->{data}) {
6083                !!!cp ('t310');
6084              !!!next-token;              !!!next-token;
6085              redo B;              next B;
6086            }            }
6087          }          }
6088            
6089          !!!parse-error (type => 'in frameset:#character');          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6090          ## Ignore the token            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6091          !!!next-token;              !!!cp ('t311');
6092          redo B;              !!!parse-error (type => 'in frameset:#character', token => $token);
6093        } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6094          if ($token->{tag_name} eq 'frameset') {              !!!cp ('t312');
6095            !!!insert-element ($token->{tag_name}, $token->{attributes});              !!!parse-error (type => 'after frameset:#character', token => $token);
6096              } else { # "after html frameset"
6097                !!!cp ('t313');
6098                !!!parse-error (type => 'after html:#character', token => $token);
6099    
6100                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6101                ## Reprocess in the "after frameset" insertion mode.
6102                !!!parse-error (type => 'after frameset:#character', token => $token);
6103              }
6104              
6105              ## Ignore the token.
6106              if (length $token->{data}) {
6107                !!!cp ('t314');
6108                ## reprocess the rest of characters
6109              } else {
6110                !!!cp ('t315');
6111                !!!next-token;
6112              }
6113              next B;
6114            }
6115            
6116            die qq[$0: Character "$token->{data}"];
6117          } elsif ($token->{type} == START_TAG_TOKEN) {
6118            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6119              !!!cp ('t316');
6120              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6121    
6122              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6123              ## Process in the "after frameset" insertion mode.
6124            } else {
6125              !!!cp ('t317');
6126            }
6127    
6128            if ($token->{tag_name} eq 'frameset' and
6129                $self->{insertion_mode} == IN_FRAMESET_IM) {
6130              !!!cp ('t318');
6131              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6132              !!!nack ('t318.1');
6133            !!!next-token;            !!!next-token;
6134            redo B;            next B;
6135          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
6136            !!!insert-element ($token->{tag_name}, $token->{attributes});                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6137              !!!cp ('t319');
6138              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6139            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6140              !!!ack ('t319.1');
6141            !!!next-token;            !!!next-token;
6142            redo B;            next B;
6143          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6144            $in_body->($insert_to_current);            !!!cp ('t320');
6145            redo B;            ## NOTE: As if in body.
6146          } else {            $parse_rcdata->(CDATA_CONTENT_MODEL);
6147            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            next B;
6148            } else {
6149              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6150                !!!cp ('t321');
6151                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6152              } else {
6153                !!!cp ('t322');
6154                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6155              }
6156            ## Ignore the token            ## Ignore the token
6157              !!!nack ('t322.1');
6158            !!!next-token;            !!!next-token;
6159            redo B;            next B;
6160          }          }
6161        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6162          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6163            if ($self->{open_elements}->[-1]->[1] eq 'html' and            !!!cp ('t323');
6164              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6165    
6166              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6167              ## Process in the "after frameset" insertion mode.
6168            } else {
6169              !!!cp ('t324');
6170            }
6171    
6172            if ($token->{tag_name} eq 'frameset' and
6173                $self->{insertion_mode} == IN_FRAMESET_IM) {
6174              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6175                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6176              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6177                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6178              ## Ignore the token              ## Ignore the token
6179              !!!next-token;              !!!next-token;
6180            } else {            } else {
6181                !!!cp ('t326');
6182              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6183              !!!next-token;              !!!next-token;
6184            }            }
6185    
6186            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6187                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6188              $self->{insertion_mode} = 'after frameset';              !!!cp ('t327');
6189                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6190              } else {
6191                !!!cp ('t328');
6192            }            }
6193            redo B;            next B;
6194            } elsif ($token->{tag_name} eq 'html' and
6195                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6196              !!!cp ('t329');
6197              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6198              !!!next-token;
6199              next B;
6200          } else {          } else {
6201            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6202                !!!cp ('t330');
6203                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6204              } else {
6205                !!!cp ('t331');
6206                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6207              }
6208            ## Ignore the token            ## Ignore the token
6209            !!!next-token;            !!!next-token;
6210            redo B;            next B;
6211            }
6212          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6213            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6214                    @{$self->{open_elements}} == 1) { # redundant, maybe
6215              !!!cp ('t331.1');
6216              !!!parse-error (type => 'in body:#eof', token => $token);
6217            } else {
6218              !!!cp ('t331.2');
6219          }          }
6220            
6221            ## Stop parsing
6222            last B;
6223        } else {        } else {
6224          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6225        }        }
     } elsif ($self->{insertion_mode} eq 'after frameset') {  
       if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
6226    
6227                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
6228                  !!!next-token;      } else {
6229                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
6230                }      }
             }  
6231    
6232              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
6233                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
6234          if ($token->{tag_name} eq 'script') {
6235            !!!cp ('t332');
6236            ## NOTE: This is an "as if in head" code clone
6237            $script_start_tag->();
6238            next B;
6239          } elsif ($token->{tag_name} eq 'style') {
6240            !!!cp ('t333');
6241            ## NOTE: This is an "as if in head" code clone
6242            $parse_rcdata->(CDATA_CONTENT_MODEL);
6243            next B;
6244          } elsif ({
6245                    base => 1, link => 1,
6246                   }->{$token->{tag_name}}) {
6247            !!!cp ('t334');
6248            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6249            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6250            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6251            !!!ack ('t334.1');
6252            !!!next-token;
6253            next B;
6254          } elsif ($token->{tag_name} eq 'meta') {
6255            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6256            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6257            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6258    
6259                ## Ignore the token.          unless ($self->{confident}) {
6260                if (length $token->{data}) {            if ($token->{attributes}->{charset}) {
6261                  ## reprocess the rest of characters              !!!cp ('t335');
6262                } else {              ## NOTE: Whether the encoding is supported or not is handled
6263                  !!!next-token;              ## in the {change_encoding} callback.
6264                }              $self->{change_encoding}
6265                redo B;                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6266                
6267                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6268                    ->set_user_data (manakai_has_reference =>
6269                                         $token->{attributes}->{charset}
6270                                             ->{has_reference});
6271              } elsif ($token->{attributes}->{content}) {
6272                if ($token->{attributes}->{content}->{value}
6273                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6274                        [\x09-\x0D\x20]*=
6275                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6276                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6277                  !!!cp ('t336');
6278                  ## NOTE: Whether the encoding is supported or not is handled
6279                  ## in the {change_encoding} callback.
6280                  $self->{change_encoding}
6281                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6282                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6283                      ->set_user_data (manakai_has_reference =>
6284                                           $token->{attributes}->{content}
6285                                                 ->{has_reference});
6286              }              }
6287              }
6288            } else {
6289              if ($token->{attributes}->{charset}) {
6290                !!!cp ('t337');
6291                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6292                    ->set_user_data (manakai_has_reference =>
6293                                         $token->{attributes}->{charset}
6294                                             ->{has_reference});
6295              }
6296              if ($token->{attributes}->{content}) {
6297                !!!cp ('t338');
6298                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6299                    ->set_user_data (manakai_has_reference =>
6300                                         $token->{attributes}->{content}
6301                                             ->{has_reference});
6302              }
6303            }
6304    
6305          die qq[$0: Character "$token->{data}"];          !!!ack ('t338.1');
6306        } elsif ($token->{type} eq 'start tag') {          !!!next-token;
6307          if ($token->{tag_name} eq 'noframes') {          next B;
6308            $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'title') {
6309            redo B;          !!!cp ('t341');
6310            ## NOTE: This is an "as if in head" code clone
6311            $parse_rcdata->(RCDATA_CONTENT_MODEL);
6312            next B;
6313          } elsif ($token->{tag_name} eq 'body') {
6314            !!!parse-error (type => 'in body:body', token => $token);
6315                  
6316            if (@{$self->{open_elements}} == 1 or
6317                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6318              !!!cp ('t342');
6319              ## Ignore the token
6320          } else {          } else {
6321            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            my $body_el = $self->{open_elements}->[1]->[0];
6322              for my $attr_name (keys %{$token->{attributes}}) {
6323                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6324                  !!!cp ('t343');
6325                  $body_el->set_attribute_ns
6326                    (undef, [undef, $attr_name],
6327                     $token->{attributes}->{$attr_name}->{value});
6328                }
6329              }
6330            }
6331            !!!nack ('t343.1');
6332            !!!next-token;
6333            next B;
6334          } elsif ({
6335                    address => 1, blockquote => 1, center => 1, dir => 1,
6336                    div => 1, dl => 1, fieldset => 1,
6337                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6338                    menu => 1, ol => 1, p => 1, ul => 1,
6339                    pre => 1, listing => 1,
6340                    form => 1,
6341                    table => 1,
6342                    hr => 1,
6343                   }->{$token->{tag_name}}) {
6344            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6345              !!!cp ('t350');
6346              !!!parse-error (type => 'in form:form', token => $token);
6347            ## Ignore the token            ## Ignore the token
6348              !!!nack ('t350.1');
6349            !!!next-token;            !!!next-token;
6350            redo B;            next B;
6351          }          }
6352        } elsif ($token->{type} eq 'end tag') {  
6353          if ($token->{tag_name} eq 'html') {          ## has a p element in scope
6354            $previous_insertion_mode = $self->{insertion_mode};          INSCOPE: for (reverse @{$self->{open_elements}}) {
6355            $self->{insertion_mode} = 'trailing end';            if ($_->[1] & P_EL) {
6356                !!!cp ('t344');
6357                !!!back-token; # <form>
6358                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6359                          line => $token->{line}, column => $token->{column}};
6360                next B;
6361              } elsif ($_->[1] & SCOPING_EL) {
6362                !!!cp ('t345');
6363                last INSCOPE;
6364              }
6365            } # INSCOPE
6366              
6367            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6368            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6369              !!!nack ('t346.1');
6370              !!!next-token;
6371              if ($token->{type} == CHARACTER_TOKEN) {
6372                $token->{data} =~ s/^\x0A//;
6373                unless (length $token->{data}) {
6374                  !!!cp ('t346');
6375                  !!!next-token;
6376                } else {
6377                  !!!cp ('t349');
6378                }
6379              } else {
6380                !!!cp ('t348');
6381              }
6382            } elsif ($token->{tag_name} eq 'form') {
6383              !!!cp ('t347.1');
6384              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6385    
6386              !!!nack ('t347.2');
6387              !!!next-token;
6388            } elsif ($token->{tag_name} eq 'table') {
6389              !!!cp ('t382');
6390              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6391              
6392              $self->{insertion_mode} = IN_TABLE_IM;
6393    
6394              !!!nack ('t382.1');
6395              !!!next-token;
6396            } elsif ($token->{tag_name} eq 'hr') {
6397              !!!cp ('t386');
6398              pop @{$self->{open_elements}};
6399            
6400              !!!nack ('t386.1');
6401            !!!next-token;            !!!next-token;
           redo B;  
6402          } else {          } else {
6403            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            !!!nack ('t347.1');
6404              !!!next-token;
6405            }
6406            next B;
6407          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6408            ## has a p element in scope
6409            INSCOPE: for (reverse @{$self->{open_elements}}) {
6410              if ($_->[1] & P_EL) {
6411                !!!cp ('t353');
6412                !!!back-token; # <x>
6413                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6414                          line => $token->{line}, column => $token->{column}};
6415                next B;
6416              } elsif ($_->[1] & SCOPING_EL) {
6417                !!!cp ('t354');
6418                last INSCOPE;
6419              }
6420            } # INSCOPE
6421              
6422            ## Step 1
6423            my $i = -1;
6424            my $node = $self->{open_elements}->[$i];
6425            my $li_or_dtdd = {li => {li => 1},
6426                              dt => {dt => 1, dd => 1},
6427                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6428            LI: {
6429              ## Step 2
6430              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6431                if ($i != -1) {
6432                  !!!cp ('t355');
6433                  !!!parse-error (type => 'not closed',
6434                                  value => $self->{open_elements}->[-1]->[0]
6435                                      ->manakai_local_name,
6436                                  token => $token);
6437                } else {
6438                  !!!cp ('t356');
6439                }
6440                splice @{$self->{open_elements}}, $i;
6441                last LI;
6442              } else {
6443                !!!cp ('t357');
6444              }
6445              
6446              ## Step 3
6447              if (not ($node->[1] & FORMATTING_EL) and
6448                  #not $phrasing_category->{$node->[1]} and
6449                  ($node->[1] & SPECIAL_EL or
6450                   $node->[1] & SCOPING_EL) and
6451                  not ($node->[1] & ADDRESS_EL) and
6452                  not ($node->[1] & DIV_EL)) {
6453                !!!cp ('t358');
6454                last LI;
6455              }
6456              
6457              !!!cp ('t359');
6458              ## Step 4
6459              $i--;
6460              $node = $self->{open_elements}->[$i];
6461              redo LI;
6462            } # LI
6463              
6464            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6465            !!!nack ('t359.1');
6466            !!!next-token;
6467            next B;
6468          } elsif ($token->{tag_name} eq 'plaintext') {
6469            ## has a p element in scope
6470            INSCOPE: for (reverse @{$self->{open_elements}}) {
6471              if ($_->[1] & P_EL) {
6472                !!!cp ('t367');
6473                !!!back-token; # <plaintext>
6474                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6475                          line => $token->{line}, column => $token->{column}};
6476                next B;
6477              } elsif ($_->[1] & SCOPING_EL) {
6478                !!!cp ('t368');
6479                last INSCOPE;
6480              }
6481            } # INSCOPE
6482              
6483            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6484              
6485            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6486              
6487            !!!nack ('t368.1');
6488            !!!next-token;
6489            next B;
6490          } elsif ($token->{tag_name} eq 'a') {
6491            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6492              my $node = $active_formatting_elements->[$i];
6493              if ($node->[1] & A_EL) {
6494                !!!cp ('t371');
6495                !!!parse-error (type => 'in a:a', token => $token);
6496                
6497                !!!back-token; # <a>
6498                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6499                          line => $token->{line}, column => $token->{column}};
6500                $formatting_end_tag->($token);
6501                
6502                AFE2: for (reverse 0..$#$active_formatting_elements) {
6503                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6504                    !!!cp ('t372');
6505                    splice @$active_formatting_elements, $_, 1;
6506                    last AFE2;
6507                  }
6508                } # AFE2
6509                OE: for (reverse 0..$#{$self->{open_elements}}) {
6510                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6511                    !!!cp ('t373');
6512                    splice @{$self->{open_elements}}, $_, 1;
6513                    last OE;
6514                  }
6515                } # OE
6516                last AFE;
6517              } elsif ($node->[0] eq '#marker') {
6518                !!!cp ('t374');
6519                last AFE;
6520              }
6521            } # AFE
6522              
6523            $reconstruct_active_formatting_elements->($insert_to_current);
6524    
6525            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6526            push @$active_formatting_elements, $self->{open_elements}->[-1];
6527    
6528            !!!nack ('t374.1');
6529            !!!next-token;
6530            next B;
6531          } elsif ($token->{tag_name} eq 'nobr') {
6532            $reconstruct_active_formatting_elements->($insert_to_current);
6533    
6534            ## has a |nobr| element in scope
6535            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6536              my $node = $self->{open_elements}->[$_];
6537              if ($node->[1] & NOBR_EL) {
6538                !!!cp ('t376');
6539                !!!parse-error (type => 'in nobr:nobr', token => $token);
6540                !!!back-token; # <nobr>
6541                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6542                          line => $token->{line}, column => $token->{column}};
6543                next B;
6544              } elsif ($node->[1] & SCOPING_EL) {
6545                !!!cp ('t377');
6546                last INSCOPE;
6547              }
6548            } # INSCOPE
6549            
6550            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6551            push @$active_formatting_elements, $self->{open_elements}->[-1];
6552            
6553            !!!nack ('t377.1');
6554            !!!next-token;
6555            next B;
6556          } elsif ($token->{tag_name} eq 'button') {
6557            ## has a button element in scope
6558            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6559              my $node = $self->{open_elements}->[$_];
6560              if ($node->[1] & BUTTON_EL) {
6561                !!!cp ('t378');
6562                !!!parse-error (type => 'in button:button', token => $token);
6563                !!!back-token; # <button>
6564                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6565                          line => $token->{line}, column => $token->{column}};
6566                next B;
6567              } elsif ($node->[1] & SCOPING_EL) {
6568                !!!cp ('t379');
6569                last INSCOPE;
6570              }
6571            } # INSCOPE
6572              
6573            $reconstruct_active_formatting_elements->($insert_to_current);
6574              
6575            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6576    
6577            ## TODO: associate with $self->{form_element} if defined
6578    
6579            push @$active_formatting_elements, ['#marker', ''];
6580    
6581            !!!nack ('t379.1');
6582            !!!next-token;
6583            next B;
6584          } elsif ({
6585                    xmp => 1,
6586                    iframe => 1,
6587                    noembed => 1,
6588                    noframes => 1,
6589                    noscript => 0, ## TODO: 1 if scripting is enabled
6590                   }->{$token->{tag_name}}) {
6591            if ($token->{tag_name} eq 'xmp') {
6592              !!!cp ('t381');
6593              $reconstruct_active_formatting_elements->($insert_to_current);
6594            } else {
6595              !!!cp ('t399');
6596            }
6597            ## NOTE: There is an "as if in body" code clone.
6598            $parse_rcdata->(CDATA_CONTENT_MODEL);
6599            next B;
6600          } elsif ($token->{tag_name} eq 'isindex') {
6601            !!!parse-error (type => 'isindex', token => $token);
6602            
6603            if (defined $self->{form_element}) {
6604              !!!cp ('t389');
6605            ## Ignore the token            ## Ignore the token
6606              !!!nack ('t389'); ## NOTE: Not acknowledged.
6607              !!!next-token;
6608              next B;
6609            } else {
6610              my $at = $token->{attributes};
6611              my $form_attrs;
6612              $form_attrs->{action} = $at->{action} if $at->{action};
6613              my $prompt_attr = $at->{prompt};
6614              $at->{name} = {name => 'name', value => 'isindex'};
6615              delete $at->{action};
6616              delete $at->{prompt};
6617              my @tokens = (
6618                            {type => START_TAG_TOKEN, tag_name => 'form',
6619                             attributes => $form_attrs,
6620                             line => $token->{line}, column => $token->{column}},
6621                            {type => START_TAG_TOKEN, tag_name => 'hr',
6622                             line => $token->{line}, column => $token->{column}},
6623                            {type => START_TAG_TOKEN, tag_name => 'p',
6624                             line => $token->{line}, column => $token->{column}},
6625                            {type => START_TAG_TOKEN, tag_name => 'label',
6626                             line => $token->{line}, column => $token->{column}},
6627                           );
6628              if ($prompt_attr) {
6629                !!!cp ('t390');
6630                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6631                               #line => $token->{line}, column => $token->{column},
6632                              };
6633              } else {
6634                !!!cp ('t391');
6635                push @tokens, {type => CHARACTER_TOKEN,
6636                               data => 'This is a searchable index. Insert your search keywords here: ',
6637                               #line => $token->{line}, column => $token->{column},
6638                              }; # SHOULD
6639                ## TODO: make this configurable
6640              }
6641              push @tokens,
6642                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6643                             line => $token->{line}, column => $token->{column}},
6644                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6645                            {type => END_TAG_TOKEN, tag_name => 'label',
6646                             line => $token->{line}, column => $token->{column}},
6647                            {type => END_TAG_TOKEN, tag_name => 'p',
6648                             line => $token->{line}, column => $token->{column}},
6649                            {type => START_TAG_TOKEN, tag_name => 'hr',
6650                             line => $token->{line}, column => $token->{column}},
6651                            {type => END_TAG_TOKEN, tag_name => 'form',
6652                             line => $token->{line}, column => $token->{column}};
6653              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6654              !!!back-token (@tokens);
6655              !!!next-token;
6656              next B;
6657            }
6658          } elsif ($token->{tag_name} eq 'textarea') {
6659            my $tag_name = $token->{tag_name};
6660            my $el;
6661            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6662            
6663            ## TODO: $self->{form_element} if defined
6664            $self->{content_model} = RCDATA_CONTENT_MODEL;
6665            delete $self->{escape}; # MUST
6666            
6667            $insert->($el);
6668            
6669            my $text = '';
6670            !!!nack ('t392.1');
6671            !!!next-token;
6672            if ($token->{type} == CHARACTER_TOKEN) {
6673              $token->{data} =~ s/^\x0A//;
6674              unless (length $token->{data}) {
6675                !!!cp ('t392');
6676                !!!next-token;
6677              } else {
6678                !!!cp ('t393');
6679              }
6680            } else {
6681              !!!cp ('t394');
6682            }
6683            while ($token->{type} == CHARACTER_TOKEN) {
6684              !!!cp ('t395');
6685              $text .= $token->{data};
6686            !!!next-token;            !!!next-token;
           redo B;  
6687          }          }
6688            if (length $text) {
6689              !!!cp ('t396');
6690              $el->manakai_append_text ($text);
6691            }
6692            
6693            $self->{content_model} = PCDATA_CONTENT_MODEL;
6694            
6695            if ($token->{type} == END_TAG_TOKEN and
6696                $token->{tag_name} eq $tag_name) {
6697              !!!cp ('t397');
6698              ## Ignore the token
6699            } else {
6700              !!!cp ('t398');
6701              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6702            }
6703            !!!next-token;
6704            next B;
6705          } elsif ($token->{tag_name} eq 'math' or
6706                   $token->{tag_name} eq 'svg') {
6707            $reconstruct_active_formatting_elements->($insert_to_current);
6708    
6709            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6710    
6711            ## "adjust foreign attributes" - done in insert-element-f
6712            
6713            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6714            
6715            if ($self->{self_closing}) {
6716              pop @{$self->{open_elements}};
6717              !!!ack ('t398.1');
6718            } else {
6719              !!!cp ('t398.2');
6720              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6721              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6722              ## mode, "in body" (not "in foreign content") secondary insertion
6723              ## mode, maybe.
6724            }
6725    
6726            !!!next-token;
6727            next B;
6728          } elsif ({
6729                    caption => 1, col => 1, colgroup => 1, frame => 1,
6730                    frameset => 1, head => 1, option => 1, optgroup => 1,
6731                    tbody => 1, td => 1, tfoot => 1, th => 1,
6732                    thead => 1, tr => 1,
6733                   }->{$token->{tag_name}}) {
6734            !!!cp ('t401');
6735            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6736            ## Ignore the token
6737            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6738            !!!next-token;
6739            next B;
6740            
6741            ## ISSUE: An issue on HTML5 new elements in the spec.
6742        } else {        } else {
6743          die "$0: $token->{type}: Unknown token type";          if ($token->{tag_name} eq 'image') {
6744              !!!cp ('t384');
6745              !!!parse-error (type => 'image', token => $token);
6746              $token->{tag_name} = 'img';
6747            } else {
6748              !!!cp ('t385');
6749            }
6750    
6751            ## NOTE: There is an "as if <br>" code clone.
6752            $reconstruct_active_formatting_elements->($insert_to_current);
6753            
6754            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6755    
6756            if ({
6757                 applet => 1, marquee => 1, object => 1,
6758                }->{$token->{tag_name}}) {
6759              !!!cp ('t380');
6760              push @$active_formatting_elements, ['#marker', ''];
6761              !!!nack ('t380.1');
6762            } elsif ({
6763                      b => 1, big => 1, em => 1, font => 1, i => 1,
6764                      s => 1, small => 1, strile => 1,
6765                      strong => 1, tt => 1, u => 1,
6766                     }->{$token->{tag_name}}) {
6767              !!!cp ('t375');
6768              push @$active_formatting_elements, $self->{open_elements}->[-1];
6769              !!!nack ('t375.1');
6770            } elsif ($token->{tag_name} eq 'input') {
6771              !!!cp ('t388');
6772              ## TODO: associate with $self->{form_element} if defined
6773              pop @{$self->{open_elements}};
6774              !!!ack ('t388.2');
6775            } elsif ({
6776                      area => 1, basefont => 1, bgsound => 1, br => 1,
6777                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6778                      #image => 1,
6779                     }->{$token->{tag_name}}) {
6780              !!!cp ('t388.1');
6781              pop @{$self->{open_elements}};
6782              !!!ack ('t388.3');
6783            } elsif ($token->{tag_name} eq 'select') {
6784              ## TODO: associate with $self->{form_element} if defined
6785            
6786              if ($self->{insertion_mode} & TABLE_IMS or
6787                  $self->{insertion_mode} & BODY_TABLE_IMS or
6788                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6789                !!!cp ('t400.1');
6790                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6791              } else {
6792                !!!cp ('t400.2');
6793                $self->{insertion_mode} = IN_SELECT_IM;
6794              }
6795              !!!nack ('t400.3');
6796            } else {
6797              !!!nack ('t402');
6798            }
6799            
6800            !!!next-token;
6801            next B;
6802        }        }
6803        } elsif ($token->{type} == END_TAG_TOKEN) {
6804          if ($token->{tag_name} eq 'body') {
6805            ## has a |body| element in scope
6806            my $i;
6807            INSCOPE: {
6808              for (reverse @{$self->{open_elements}}) {
6809                if ($_->[1] & BODY_EL) {
6810                  !!!cp ('t405');
6811                  $i = $_;
6812                  last INSCOPE;
6813                } elsif ($_->[1] & SCOPING_EL) {
6814                  !!!cp ('t405.1');
6815                  last;
6816                }
6817              }
6818    
6819        ## ISSUE: An issue in spec here            !!!parse-error (type => 'start tag not allowed',
6820      } elsif ($self->{insertion_mode} eq 'trailing end') {                            value => $token->{tag_name}, token => $token);
6821        ## states in the main stage is preserved yet # MUST            ## NOTE: Ignore the token.
6822                    !!!next-token;
6823        if ($token->{type} eq 'character') {            next B;
6824          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          } # INSCOPE
6825            my $data = $1;  
6826            ## As if in the main phase.          for (@{$self->{open_elements}}) {
6827            ## NOTE: The insertion mode in the main phase            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6828            ## just before the phase has been changed to the trailing              !!!cp ('t403');
6829            ## end phase is either "after body" or "after frameset".              !!!parse-error (type => 'not closed',
6830            $reconstruct_active_formatting_elements->($insert_to_current);                              value => $_->[0]->manakai_local_name,
6831                                token => $token);
6832                last;
6833              } else {
6834                !!!cp ('t404');
6835              }
6836            }
6837    
6838            $self->{insertion_mode} = AFTER_BODY_IM;
6839            !!!next-token;
6840            next B;
6841          } elsif ($token->{tag_name} eq 'html') {
6842            ## TODO: Update this code.  It seems that the code below is not
6843            ## up-to-date, though it has same effect as speced.
6844            if (@{$self->{open_elements}} > 1 and
6845                $self->{open_elements}->[1]->[1] & BODY_EL) {
6846              ## ISSUE: There is an issue in the spec.
6847              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6848                !!!cp ('t406');
6849                !!!parse-error (type => 'not closed',
6850                                value => $self->{open_elements}->[1]->[0]
6851                                    ->manakai_local_name,
6852                                token => $token);
6853              } else {
6854                !!!cp ('t407');
6855              }
6856              $self->{insertion_mode} = AFTER_BODY_IM;
6857              ## reprocess
6858              next B;
6859            } else {
6860              !!!cp ('t408');
6861              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6862              ## Ignore the token
6863              !!!next-token;
6864              next B;
6865            }
6866          } elsif ({
6867                    address => 1, blockquote => 1, center => 1, dir => 1,
6868                    div => 1, dl => 1, fieldset => 1, listing => 1,
6869                    menu => 1, ol => 1, pre => 1, ul => 1,
6870                    dd => 1, dt => 1, li => 1,
6871                    applet => 1, button => 1, marquee => 1, object => 1,
6872                   }->{$token->{tag_name}}) {
6873            ## has an element in scope
6874            my $i;
6875            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6876              my $node = $self->{open_elements}->[$_];
6877              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6878                !!!cp ('t410');
6879                $i = $_;
6880                last INSCOPE;
6881              } elsif ($node->[1] & SCOPING_EL) {
6882                !!!cp ('t411');
6883                last INSCOPE;
6884              }
6885            } # INSCOPE
6886    
6887            unless (defined $i) { # has an element in scope
6888              !!!cp ('t413');
6889              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6890            } else {
6891              ## Step 1. generate implied end tags
6892              while ({
6893                      dd => ($token->{tag_name} ne 'dd'),
6894                      dt => ($token->{tag_name} ne 'dt'),
6895                      li => ($token->{tag_name} ne 'li'),
6896                      p => 1,
6897                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6898                !!!cp ('t409');
6899                pop @{$self->{open_elements}};
6900              }
6901    
6902              ## Step 2.
6903              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6904                      ne $token->{tag_name}) {
6905                !!!cp ('t412');
6906                !!!parse-error (type => 'not closed',
6907                                value => $self->{open_elements}->[-1]->[0]
6908                                    ->manakai_local_name,
6909                                token => $token);
6910              } else {
6911                !!!cp ('t414');
6912              }
6913    
6914              ## Step 3.
6915              splice @{$self->{open_elements}}, $i;
6916    
6917              ## Step 4.
6918              $clear_up_to_marker->()
6919                  if {
6920                    applet => 1, button => 1, marquee => 1, object => 1,
6921                  }->{$token->{tag_name}};
6922            }
6923            !!!next-token;
6924            next B;
6925          } elsif ($token->{tag_name} eq 'form') {
6926            undef $self->{form_element};
6927    
6928            ## has an element in scope
6929            my $i;
6930            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6931              my $node = $self->{open_elements}->[$_];
6932              if ($node->[1] & FORM_EL) {
6933                !!!cp ('t418');
6934                $i = $_;
6935                last INSCOPE;
6936              } elsif ($node->[1] & SCOPING_EL) {
6937                !!!cp ('t419');
6938                last INSCOPE;
6939              }
6940            } # INSCOPE
6941    
6942            unless (defined $i) { # has an element in scope
6943              !!!cp ('t421');
6944              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6945            } else {
6946              ## Step 1. generate implied end tags
6947              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6948                !!!cp ('t417');
6949                pop @{$self->{open_elements}};
6950              }
6951                        
6952            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
6953              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6954                      ne $token->{tag_name}) {
6955                !!!cp ('t417.1');
6956                !!!parse-error (type => 'not closed',
6957                                value => $self->{open_elements}->[-1]->[0]
6958                                    ->manakai_local_name,
6959                                token => $token);
6960              } else {
6961                !!!cp ('t420');
6962              }  
6963                        
6964            unless (length $token->{data}) {            ## Step 3.
6965              !!!next-token;            splice @{$self->{open_elements}}, $i;
6966              redo B;          }
6967    
6968            !!!next-token;
6969            next B;
6970          } elsif ({
6971                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6972                   }->{$token->{tag_name}}) {
6973            ## has an element in scope
6974            my $i;
6975            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6976              my $node = $self->{open_elements}->[$_];
6977              if ($node->[1] & HEADING_EL) {
6978                !!!cp ('t423');
6979                $i = $_;
6980                last INSCOPE;
6981              } elsif ($node->[1] & SCOPING_EL) {
6982                !!!cp ('t424');
6983                last INSCOPE;
6984            }            }
6985            } # INSCOPE
6986    
6987            unless (defined $i) { # has an element in scope
6988              !!!cp ('t425.1');
6989              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6990            } else {
6991              ## Step 1. generate implied end tags
6992              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6993                !!!cp ('t422');
6994                pop @{$self->{open_elements}};
6995              }
6996              
6997              ## Step 2.
6998              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6999                      ne $token->{tag_name}) {
7000                !!!cp ('t425');
7001                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7002              } else {
7003                !!!cp ('t426');
7004              }
7005    
7006              ## Step 3.
7007              splice @{$self->{open_elements}}, $i;
7008          }          }
7009            
7010            !!!next-token;
7011            next B;
7012          } elsif ($token->{tag_name} eq 'p') {
7013            ## has an element in scope
7014            my $i;
7015            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7016              my $node = $self->{open_elements}->[$_];
7017              if ($node->[1] & P_EL) {
7018                !!!cp ('t410.1');
7019                $i = $_;
7020                last INSCOPE;
7021              } elsif ($node->[1] & SCOPING_EL) {
7022                !!!cp ('t411.1');
7023                last INSCOPE;
7024              }
7025            } # INSCOPE
7026    
7027          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7028          $self->{insertion_mode} = $previous_insertion_mode;            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7029          ## reprocess                    ne $token->{tag_name}) {
7030          redo B;              !!!cp ('t412.1');
7031        } elsif ($token->{type} eq 'start tag') {              !!!parse-error (type => 'not closed',
7032          !!!parse-error (type => 'after html:'.$token->{tag_name});                              value => $self->{open_elements}->[-1]->[0]
7033          $self->{insertion_mode} = $previous_insertion_mode;                                  ->manakai_local_name,
7034          ## reprocess                              token => $token);
7035          redo B;            } else {
7036        } elsif ($token->{type} eq 'end tag') {              !!!cp ('t414.1');
7037          !!!parse-error (type => 'after html:/'.$token->{tag_name});            }
7038          $self->{insertion_mode} = $previous_insertion_mode;  
7039          ## reprocess            splice @{$self->{open_elements}}, $i;
7040          redo B;          } else {
7041              !!!cp ('t413.1');
7042              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7043    
7044              !!!cp ('t415.1');
7045              ## As if <p>, then reprocess the current token
7046              my $el;
7047              !!!create-element ($el, $HTML_NS, 'p',, $token);
7048              $insert->($el);
7049              ## NOTE: Not inserted into |$self->{open_elements}|.
7050            }
7051    
7052            !!!next-token;
7053            next B;
7054          } elsif ({
7055                    a => 1,
7056                    b => 1, big => 1, em => 1, font => 1, i => 1,
7057                    nobr => 1, s => 1, small => 1, strile => 1,
7058                    strong => 1, tt => 1, u => 1,
7059                   }->{$token->{tag_name}}) {
7060            !!!cp ('t427');
7061            $formatting_end_tag->($token);
7062            next B;
7063          } elsif ($token->{tag_name} eq 'br') {
7064            !!!cp ('t428');
7065            !!!parse-error (type => 'unmatched end tag:br', token => $token);
7066    
7067            ## As if <br>
7068            $reconstruct_active_formatting_elements->($insert_to_current);
7069            
7070            my $el;
7071            !!!create-element ($el, $HTML_NS, 'br',, $token);
7072            $insert->($el);
7073            
7074            ## Ignore the token.
7075            !!!next-token;
7076            next B;
7077          } elsif ({
7078                    caption => 1, col => 1, colgroup => 1, frame => 1,
7079                    frameset => 1, head => 1, option => 1, optgroup => 1,
7080                    tbody => 1, td => 1, tfoot => 1, th => 1,
7081                    thead => 1, tr => 1,
7082                    area => 1, basefont => 1, bgsound => 1,
7083                    embed => 1, hr => 1, iframe => 1, image => 1,
7084                    img => 1, input => 1, isindex => 1, noembed => 1,
7085                    noframes => 1, param => 1, select => 1, spacer => 1,
7086                    table => 1, textarea => 1, wbr => 1,
7087                    noscript => 0, ## TODO: if scripting is enabled
7088                   }->{$token->{tag_name}}) {
7089            !!!cp ('t429');
7090            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7091            ## Ignore the token
7092            !!!next-token;
7093            next B;
7094            
7095            ## ISSUE: Issue on HTML5 new elements in spec
7096            
7097        } else {        } else {
7098          die "$0: $token->{type}: Unknown token";          ## Step 1
7099            my $node_i = -1;
7100            my $node = $self->{open_elements}->[$node_i];
7101    
7102            ## Step 2
7103            S2: {
7104              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7105                ## Step 1
7106                ## generate implied end tags
7107                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7108                  !!!cp ('t430');
7109                  ## ISSUE: Can this case be reached?
7110                  pop @{$self->{open_elements}};
7111                }
7112            
7113                ## Step 2
7114                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7115                        ne $token->{tag_name}) {
7116                  !!!cp ('t431');
7117                  ## NOTE: <x><y></x>
7118                  !!!parse-error (type => 'not closed',
7119                                  value => $self->{open_elements}->[-1]->[0]
7120                                      ->manakai_local_name,
7121                                  token => $token);
7122                } else {
7123                  !!!cp ('t432');
7124                }
7125                
7126                ## Step 3
7127                splice @{$self->{open_elements}}, $node_i;
7128    
7129                !!!next-token;
7130                last S2;
7131              } else {
7132                ## Step 3
7133                if (not ($node->[1] & FORMATTING_EL) and
7134                    #not $phrasing_category->{$node->[1]} and
7135                    ($node->[1] & SPECIAL_EL or
7136                     $node->[1] & SCOPING_EL)) {
7137                  !!!cp ('t433');
7138                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7139                  ## Ignore the token
7140                  !!!next-token;
7141                  last S2;
7142                }
7143    
7144                !!!cp ('t434');
7145              }
7146              
7147              ## Step 4
7148              $node_i--;
7149              $node = $self->{open_elements}->[$node_i];
7150              
7151              ## Step 5;
7152              redo S2;
7153            } # S2
7154            next B;
7155        }        }
7156      } else {      }
7157        die "$0: $self->{insertion_mode}: Unknown insertion mode";      next B;
7158      } continue { # B
7159        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7160          ## NOTE: The code below is executed in cases where it does not have
7161          ## to be, but it it is harmless even in those cases.
7162          ## has an element in scope
7163          INSCOPE: {
7164            for (reverse 0..$#{$self->{open_elements}}) {
7165              my $node = $self->{open_elements}->[$_];
7166              if ($node->[1] & FOREIGN_EL) {
7167                last INSCOPE;
7168              } elsif ($node->[1] & SCOPING_EL) {
7169                last;
7170              }
7171            }
7172            
7173            ## NOTE: No foreign element in scope.
7174            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7175          } # INSCOPE
7176      }      }
7177    } # B    } # B
7178    
# Line 5207  sub set_inner_html ($$$) { Line 7187  sub set_inner_html ($$$) {
7187    my $s = \$_[0];    my $s = \$_[0];
7188    my $onerror = $_[1];    my $onerror = $_[1];
7189    
7190      ## ISSUE: Should {confident} be true?
7191    
7192    my $nt = $node->node_type;    my $nt = $node->node_type;
7193    if ($nt == 9) {    if ($nt == 9) {
7194      # MUST      # MUST
# Line 5235  sub set_inner_html ($$$) { Line 7217  sub set_inner_html ($$$) {
7217      my $p = $class->new;      my $p = $class->new;
7218      $p->{document} = $doc;      $p->{document} = $doc;
7219    
7220      ## Step 9 # MUST      ## Step 8 # MUST
7221      my $i = 0;      my $i = 0;
7222      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7223      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7224      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7225        my $self = shift;        my $self = shift;
7226    
7227        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7228        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7229    
7230          $self->{next_char} = -1 and return if $i >= length $$s;
7231          $self->{next_char} = ord substr $$s, $i++, 1;
7232    
7233          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7234          $p->{column}++;
7235    
7236        $self->{next_input_character} = -1 and return if $i >= length $$s;        if ($self->{next_char} == 0x000A) { # LF
7237        $self->{next_input_character} = ord substr $$s, $i++, 1;          $p->{line}++;
7238        $column++;          $p->{column} = 0;
7239            !!!cp ('i1');
7240        if ($self->{next_input_character} == 0x000A) { # LF        } elsif ($self->{next_char} == 0x000D) { # CR
         $line++;  
         $column = 0;  
       } elsif ($self->{next_input_character} == 0x000D) { # CR  
7241          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7242          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7243          $line++;          $p->{line}++;
7244          $column = 0;          $p->{column} = 0;
7245        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7246          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7247        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7248            !!!cp ('i3');
7249          } elsif ($self->{next_char} == 0x0000) { # NULL
7250            !!!cp ('i4');
7251          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7252          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7253          } elsif ($self->{next_char} <= 0x0008 or
7254                   (0x000E <= $self->{next_char} and
7255                    $self->{next_char} <= 0x001F) or
7256                   (0x007F <= $self->{next_char} and
7257                    $self->{next_char} <= 0x009F) or
7258                   (0xD800 <= $self->{next_char} and
7259                    $self->{next_char} <= 0xDFFF) or
7260                   (0xFDD0 <= $self->{next_char} and
7261                    $self->{next_char} <= 0xFDDF) or
7262                   {
7263                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7264                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7265                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7266                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7267                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7268                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7269                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7270                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7271                    0x10FFFE => 1, 0x10FFFF => 1,
7272                   }->{$self->{next_char}}) {
7273            !!!cp ('i4.1');
7274            !!!parse-error (type => 'control char', level => $self->{must_level});
7275    ## TODO: error type documentation
7276        }        }
7277      };      };
7278      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7279      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7280            
7281      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7282        my (%opt) = @_;        my (%opt) = @_;
7283        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7284          my $column = $opt{column};
7285          if (defined $opt{token} and defined $opt{token}->{line}) {
7286            $line = $opt{token}->{line};
7287            $column = $opt{token}->{column};
7288          }
7289          warn "Parse error ($opt{type}) at line $line column $column\n";
7290      };      };
7291      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7292        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7293      };      };
7294            
7295      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7296      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7297    
7298      ## Step 2      ## Step 2
7299      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7300      $p->{content_model_flag} = {      $p->{content_model} = {
7301        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7302        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7303        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7304        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7305        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7306        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7307        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7308        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7309        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7310        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7311      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7312         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7313            unless defined $p->{content_model};
7314            ## ISSUE: What is "the name of the element"? local name?
7315    
7316      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7317          ## TODO: Foreign element OK?
7318    
7319      ## Step 4      ## Step 3
7320      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7321        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7322    
7323      ## Step 5 # MUST      ## Step 4 # MUST
7324      $doc->append_child ($root);      $doc->append_child ($root);
7325    
7326      ## Step 6 # MUST      ## Step 5 # MUST
7327      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7328    
7329      undef $p->{head_element};      undef $p->{head_element};
7330    
7331      ## Step 7 # MUST      ## Step 6 # MUST
7332      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7333    
7334      ## Step 8 # MUST      ## Step 7 # MUST
7335      my $anode = $node;      my $anode = $node;
7336      AN: while (defined $anode) {      AN: while (defined $anode) {
7337        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7338          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7339          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7340            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7341                !!!cp ('i5');
7342              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7343              last AN;              last AN;
7344            }            }
# Line 5326  sub set_inner_html ($$$) { Line 7347  sub set_inner_html ($$$) {
7347        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7348      } # AN      } # AN
7349            
7350      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7351      {      {
7352        my $self = $p;        my $self = $p;
7353        !!!next-token;        !!!next-token;
7354      }      }
7355      $p->_tree_construction_main;      $p->_tree_construction_main;
7356    
7357      ## Step 11 # MUST      ## Step 10 # MUST
7358      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7359      for (@cn) {      for (@cn) {
7360        $node->remove_child ($_);        $node->remove_child ($_);
7361      }      }
7362      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7363    
7364      ## Step 12 # MUST      ## Step 11 # MUST
7365      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7366      for (@cn) {      for (@cn) {
7367        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5350  sub set_inner_html ($$$) { Line 7370  sub set_inner_html ($$$) {
7370      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7371    
7372      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7373    
7374        delete $p->{parse_error}; # delete loop
7375    } else {    } else {
7376      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";
7377    }    }
# Line 5357  sub set_inner_html ($$$) { Line 7379  sub set_inner_html ($$$) {
7379    
7380  } # tree construction stage  } # tree construction stage
7381    
7382  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7383    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  
7384    
7385  1;  1;
7386  # $Date$  # $Date$

Legend:
Removed from v.1.36  
changed lines
  Added in v.1.136

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24