/[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.54 by wakaba, Sat Aug 11 06:37:12 2007 UTC revision 1.133 by wakaba, Sat May 17 04:54:11 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  ## TODO: 1252 parse error (revision 1264)
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  ## TODO: 8859-11 = 874 (revision 1271)
13  ## is not yet clear.  
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
15  ## "{U+FEFF}..." in GB18030?  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
16    my $SVG_NS = q<http://www.w3.org/2000/svg>;
17  my $permitted_slash_tag_name = {  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
18    base => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
19    link => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
20    meta => 1,  
21    hr => 1,  sub A_EL () { 0b1 }
22    br => 1,  sub ADDRESS_EL () { 0b10 }
23    img=> 1,  sub BODY_EL () { 0b100 }
24    embed => 1,  sub BUTTON_EL () { 0b1000 }
25    param => 1,  sub CAPTION_EL () { 0b10000 }
26    area => 1,  sub DD_EL () { 0b100000 }
27    col => 1,  sub DIV_EL () { 0b1000000 }
28    input => 1,  sub DT_EL () { 0b10000000 }
29    sub FORM_EL () { 0b100000000 }
30    sub FORMATTING_EL () { 0b1000000000 }
31    sub FRAMESET_EL () { 0b10000000000 }
32    sub HEADING_EL () { 0b100000000000 }
33    sub HTML_EL () { 0b1000000000000 }
34    sub LI_EL () { 0b10000000000000 }
35    sub NOBR_EL () { 0b100000000000000 }
36    sub OPTION_EL () { 0b1000000000000000 }
37    sub OPTGROUP_EL () { 0b10000000000000000 }
38    sub P_EL () { 0b100000000000000000 }
39    sub SELECT_EL () { 0b1000000000000000000 }
40    sub TABLE_EL () { 0b10000000000000000000 }
41    sub TABLE_CELL_EL () { 0b100000000000000000000 }
42    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
43    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
44    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
45    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
46    sub FOREIGN_EL () { 0b10000000000000000000000000 }
47    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
48    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
49    
50    sub TABLE_ROWS_EL () {
51      TABLE_EL |
52      TABLE_ROW_EL |
53      TABLE_ROW_GROUP_EL
54    }
55    
56    sub END_TAG_OPTIONAL_EL () {
57      DD_EL |
58      DT_EL |
59      LI_EL |
60      P_EL
61    }
62    
63    sub ALL_END_TAG_OPTIONAL_EL () {
64      END_TAG_OPTIONAL_EL |
65      BODY_EL |
66      HTML_EL |
67      TABLE_CELL_EL |
68      TABLE_ROW_EL |
69      TABLE_ROW_GROUP_EL
70    }
71    
72    sub SCOPING_EL () {
73      BUTTON_EL |
74      CAPTION_EL |
75      HTML_EL |
76      TABLE_EL |
77      TABLE_CELL_EL |
78      MISC_SCOPING_EL
79    }
80    
81    sub TABLE_SCOPING_EL () {
82      HTML_EL |
83      TABLE_EL
84    }
85    
86    sub TABLE_ROWS_SCOPING_EL () {
87      HTML_EL |
88      TABLE_ROW_GROUP_EL
89    }
90    
91    sub TABLE_ROW_SCOPING_EL () {
92      HTML_EL |
93      TABLE_ROW_EL
94    }
95    
96    sub SPECIAL_EL () {
97      ADDRESS_EL |
98      BODY_EL |
99      DIV_EL |
100      END_TAG_OPTIONAL_EL |
101      FORM_EL |
102      FRAMESET_EL |
103      HEADING_EL |
104      OPTION_EL |
105      OPTGROUP_EL |
106      SELECT_EL |
107      TABLE_ROW_EL |
108      TABLE_ROW_GROUP_EL |
109      MISC_SPECIAL_EL
110    }
111    
112    my $el_category = {
113      a => A_EL | FORMATTING_EL,
114      address => ADDRESS_EL,
115      applet => MISC_SCOPING_EL,
116      area => MISC_SPECIAL_EL,
117      b => FORMATTING_EL,
118      base => MISC_SPECIAL_EL,
119      basefont => MISC_SPECIAL_EL,
120      bgsound => MISC_SPECIAL_EL,
121      big => FORMATTING_EL,
122      blockquote => MISC_SPECIAL_EL,
123      body => BODY_EL,
124      br => MISC_SPECIAL_EL,
125      button => BUTTON_EL,
126      caption => CAPTION_EL,
127      center => MISC_SPECIAL_EL,
128      col => MISC_SPECIAL_EL,
129      colgroup => MISC_SPECIAL_EL,
130      dd => DD_EL,
131      dir => MISC_SPECIAL_EL,
132      div => DIV_EL,
133      dl => MISC_SPECIAL_EL,
134      dt => DT_EL,
135      em => FORMATTING_EL,
136      embed => MISC_SPECIAL_EL,
137      fieldset => MISC_SPECIAL_EL,
138      font => FORMATTING_EL,
139      form => FORM_EL,
140      frame => MISC_SPECIAL_EL,
141      frameset => FRAMESET_EL,
142      h1 => HEADING_EL,
143      h2 => HEADING_EL,
144      h3 => HEADING_EL,
145      h4 => HEADING_EL,
146      h5 => HEADING_EL,
147      h6 => HEADING_EL,
148      head => MISC_SPECIAL_EL,
149      hr => MISC_SPECIAL_EL,
150      html => HTML_EL,
151      i => FORMATTING_EL,
152      iframe => MISC_SPECIAL_EL,
153      img => MISC_SPECIAL_EL,
154      input => MISC_SPECIAL_EL,
155      isindex => MISC_SPECIAL_EL,
156      li => LI_EL,
157      link => MISC_SPECIAL_EL,
158      listing => MISC_SPECIAL_EL,
159      marquee => MISC_SCOPING_EL,
160      menu => MISC_SPECIAL_EL,
161      meta => MISC_SPECIAL_EL,
162      nobr => NOBR_EL | FORMATTING_EL,
163      noembed => MISC_SPECIAL_EL,
164      noframes => MISC_SPECIAL_EL,
165      noscript => MISC_SPECIAL_EL,
166      object => MISC_SCOPING_EL,
167      ol => MISC_SPECIAL_EL,
168      optgroup => OPTGROUP_EL,
169      option => OPTION_EL,
170      p => P_EL,
171      param => MISC_SPECIAL_EL,
172      plaintext => MISC_SPECIAL_EL,
173      pre => MISC_SPECIAL_EL,
174      s => FORMATTING_EL,
175      script => MISC_SPECIAL_EL,
176      select => SELECT_EL,
177      small => FORMATTING_EL,
178      spacer => MISC_SPECIAL_EL,
179      strike => FORMATTING_EL,
180      strong => FORMATTING_EL,
181      style => MISC_SPECIAL_EL,
182      table => TABLE_EL,
183      tbody => TABLE_ROW_GROUP_EL,
184      td => TABLE_CELL_EL,
185      textarea => MISC_SPECIAL_EL,
186      tfoot => TABLE_ROW_GROUP_EL,
187      th => TABLE_CELL_EL,
188      thead => TABLE_ROW_GROUP_EL,
189      title => MISC_SPECIAL_EL,
190      tr => TABLE_ROW_EL,
191      tt => FORMATTING_EL,
192      u => FORMATTING_EL,
193      ul => MISC_SPECIAL_EL,
194      wbr => MISC_SPECIAL_EL,
195    };
196    
197    my $el_category_f = {
198      $MML_NS => {
199        'annotation-xml' => MML_AXML_EL,
200        mi => FOREIGN_FLOW_CONTENT_EL,
201        mo => FOREIGN_FLOW_CONTENT_EL,
202        mn => FOREIGN_FLOW_CONTENT_EL,
203        ms => FOREIGN_FLOW_CONTENT_EL,
204        mtext => FOREIGN_FLOW_CONTENT_EL,
205      },
206      $SVG_NS => {
207        foreignObject => FOREIGN_FLOW_CONTENT_EL,
208        desc => FOREIGN_FLOW_CONTENT_EL,
209        title => FOREIGN_FLOW_CONTENT_EL,
210      },
211      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
212    };
213    
214    my $svg_attr_name = {
215      attributetype => 'attributeType',
216      basefrequency => 'baseFrequency',
217      baseprofile => 'baseProfile',
218      calcmode => 'calcMode',
219      clippathunits => 'clipPathUnits',
220      contentscripttype => 'contentScriptType',
221      contentstyletype => 'contentStyleType',
222      diffuseconstant => 'diffuseConstant',
223      edgemode => 'edgeMode',
224      externalresourcesrequired => 'externalResourcesRequired',
225      fecolormatrix => 'feColorMatrix',
226      fecomposite => 'feComposite',
227      fegaussianblur => 'feGaussianBlur',
228      femorphology => 'feMorphology',
229      fetile => 'feTile',
230      filterres => 'filterRes',
231      filterunits => 'filterUnits',
232      glyphref => 'glyphRef',
233      gradienttransform => 'gradientTransform',
234      gradientunits => 'gradientUnits',
235      kernelmatrix => 'kernelMatrix',
236      kernelunitlength => 'kernelUnitLength',
237      keypoints => 'keyPoints',
238      keysplines => 'keySplines',
239      keytimes => 'keyTimes',
240      lengthadjust => 'lengthAdjust',
241      limitingconeangle => 'limitingConeAngle',
242      markerheight => 'markerHeight',
243      markerunits => 'markerUnits',
244      markerwidth => 'markerWidth',
245      maskcontentunits => 'maskContentUnits',
246      maskunits => 'maskUnits',
247      numoctaves => 'numOctaves',
248      pathlength => 'pathLength',
249      patterncontentunits => 'patternContentUnits',
250      patterntransform => 'patternTransform',
251      patternunits => 'patternUnits',
252      pointsatx => 'pointsAtX',
253      pointsaty => 'pointsAtY',
254      pointsatz => 'pointsAtZ',
255      preservealpha => 'preserveAlpha',
256      preserveaspectratio => 'preserveAspectRatio',
257      primitiveunits => 'primitiveUnits',
258      refx => 'refX',
259      refy => 'refY',
260      repeatcount => 'repeatCount',
261      repeatdur => 'repeatDur',
262      requiredextensions => 'requiredExtensions',
263      specularconstant => 'specularConstant',
264      specularexponent => 'specularExponent',
265      spreadmethod => 'spreadMethod',
266      startoffset => 'startOffset',
267      stddeviation => 'stdDeviation',
268      stitchtiles => 'stitchTiles',
269      surfacescale => 'surfaceScale',
270      systemlanguage => 'systemLanguage',
271      tablevalues => 'tableValues',
272      targetx => 'targetX',
273      targety => 'targetY',
274      textlength => 'textLength',
275      viewbox => 'viewBox',
276      viewtarget => 'viewTarget',
277      xchannelselector => 'xChannelSelector',
278      ychannelselector => 'yChannelSelector',
279      zoomandpan => 'zoomAndPan',
280  };  };
281    
282    my $foreign_attr_xname = {
283      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
284      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
285      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
286      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
287      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
288      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
289      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
290      'xml:base' => [$XML_NS, ['xml', 'base']],
291      'xml:lang' => [$XML_NS, ['xml', 'lang']],
292      'xml:space' => [$XML_NS, ['xml', 'space']],
293      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
294      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
295    };
296    
297    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
298    
299  my $c1_entity_char = {  my $c1_entity_char = {
300    0x80 => 0x20AC,    0x80 => 0x20AC,
301    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 62  my $c1_entity_char = { Line 331  my $c1_entity_char = {
331    0x9F => 0x0178,    0x9F => 0x0178,
332  }; # $c1_entity_char  }; # $c1_entity_char
333    
334  my $special_category = {  sub parse_byte_string ($$$$;$) {
335    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = ref $_[0] ? shift : shift->new;
336    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
337    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
338    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    my $s;
339    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
340    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,    ## HTML5 encoding sniffing algorithm
341    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,    require Message::Charset::Info;
342    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    my $charset;
343    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my ($e, $e_status);
344    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
345  };    SNIFFING: {
346  my $scoping_category = {  
347    button => 1, caption => 1, html => 1, marquee => 1, object => 1,      ## Step 1
348    table => 1, td => 1, th => 1,      if (defined $charset_name) {
349  };        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
350  my $formatting_category = {  
351    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,        ## ISSUE: Unsupported encoding is not ignored according to the spec.
352    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,        ($e, $e_status) = $charset->get_perl_encoding
353  };            (allow_error_reporting => 1,
354  # $phrasing_category: all other elements             allow_fallback => 1);
355          if ($e) {
356            $self->{confident} = 1;
357            last SNIFFING;
358          }
359        }
360    
361        ## Step 2
362        # wait
363    
364        ## Step 3
365        my $head = substr ($$bytes_s, 0, 3);
366        if ($head =~ /^\xFE\xFF/) {
367          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
368          ($e, $e_status) = $charset->get_perl_encoding
369              (allow_error_reporting => 1,
370               allow_fallback => 1);
371          $self->{confident} = 1;
372          last SNIFFING;
373        } elsif ($head =~ /^\xFF\xFE/) {
374          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
375          ($e, $e_status) = $charset->get_perl_encoding
376              (allow_error_reporting => 1,
377               allow_fallback => 1);
378          $self->{confident} = 1;
379          last SNIFFING;
380        } elsif ($head eq "\xEF\xBB\xBF") {
381          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
382          ($e, $e_status) = $charset->get_perl_encoding
383              (allow_error_reporting => 1,
384               allow_fallback => 1);
385          $self->{confident} = 1;
386          last SNIFFING;
387        }
388    
389        ## Step 4
390        ## TODO: <meta charset>
391    
392        ## Step 5
393        ## TODO: from history
394    
395        ## Step 6
396        require Whatpm::Charset::UniversalCharDet;
397        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
398            (substr ($$bytes_s, 0, 1024));
399        if (defined $charset_name) {
400          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
401    
402          ## ISSUE: Unsupported encoding is not ignored according to the spec.
403          ($e, $e_status) = $charset->get_perl_encoding
404              (allow_error_reporting => 1,
405               allow_fallback => 1);
406          if ($e) {
407            $self->{confident} = 0;
408            last SNIFFING;
409          }
410        }
411    
412        ## Step 7: default
413        ## TODO: Make this configurable.
414        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
415            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
416            ## detectable in the step 6.
417        ($e, $e_status) = $charset->get_perl_encoding (allow_error_reporting => 1,
418                                                       allow_fallback => 1);
419        $self->{confident} = 0;
420      } # SNIFFING
421    
422      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
423        
424      } elsif (not ($e_status &
425                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
426        
427      }
428      $s = \ $e->decode ($$bytes_s);
429      $self->{input_encoding} = $charset->get_iana_name;
430    
431      $self->{change_encoding} = sub {
432        my $self = shift;
433        my $charset_name = lc shift;
434        my $token = shift;
435        ## TODO: if $charset_name is supported
436        ## TODO: normalize charset name
437    
438        ## "Change the encoding" algorithm:
439    
440        ## Step 1    
441        if ($charset_name eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
442          $charset_name = 'utf-8';
443        }
444    
445        ## Step 2
446        if (defined $self->{input_encoding} and
447            $self->{input_encoding} eq $charset_name) {
448          $self->{confident} = 1;
449          return;
450        }
451    
452        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
453            ':'.$charset_name, level => 'w', token => $token);
454    
455        ## Step 3
456        # if (can) {
457          ## change the encoding on the fly.
458          #$self->{confident} = 1;
459          #return;
460        # }
461    
462        ## Step 4
463        throw Whatpm::HTML::RestartParser (charset => $charset_name);
464      }; # $self->{change_encoding}
465    
466      my @args = @_; shift @args; # $s
467      my $return;
468      try {
469        $return = $self->parse_char_string ($s, @args);  
470      } catch Whatpm::HTML::RestartParser with {
471        my $charset_name = shift->{charset};
472        $s = \ (Encode::decode ($charset_name, $$bytes_s));    
473        $self->{input_encoding} = $charset_name; ## TODO: normalize
474        $self->{confident} = 1;
475        $return = $self->parse_char_string ($s, @args);
476      };
477      return $return;
478    } # parse_byte_string
479    
480    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
481    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
482    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
483    ## because the core part of our HTML parser expects a string of character,
484    ## not a string of bytes or code units or anything which might contain a BOM.
485    ## Therefore, any parser interface that accepts a string of bytes,
486    ## such as |parse_byte_string| in this module, must ensure that it does
487    ## strip the BOM and never strip any ZWNBSP.
488    
489    *parse_char_string = \&parse_string;
490    
491  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
492    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
493    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
494    $self->{document} = $_[1];    $self->{document} = $_[1];
495      @{$self->{document}->child_nodes} = ();
496    
497    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
498    
499      $self->{confident} = 1 unless exists $self->{confident};
500      $self->{document}->input_encoding ($self->{input_encoding})
501          if defined $self->{input_encoding};
502    
503    my $i = 0;    my $i = 0;
504    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
505    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
506    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
507      my $self = shift;      my $self = shift;
508    
509      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
510      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
511    
512      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
513      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
514      $column++;  
515        ($self->{line_prev}, $self->{column_prev})
516            = ($self->{line}, $self->{column});
517        $self->{column}++;
518            
519      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
520        $line++;        !!!cp ('j1');
521        $column = 0;        $self->{line}++;
522      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
523        } elsif ($self->{next_char} == 0x000D) { # CR
524          !!!cp ('j2');
525        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
526        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
527        $line++;        $self->{line}++;
528        $column = 0;        $self->{column} = 0;
529      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
530        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        !!!cp ('j3');
531      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
532        } elsif ($self->{next_char} == 0x0000) { # NULL
533          !!!cp ('j4');
534        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
535        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
536        } elsif ($self->{next_char} <= 0x0008 or
537                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
538                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
539                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
540                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
541                 {
542                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
543                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
544                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
545                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
546                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
547                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
548                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
549                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
550                  0x10FFFE => 1, 0x10FFFF => 1,
551                 }->{$self->{next_char}}) {
552          !!!cp ('j5');
553          !!!parse-error (type => 'control char', level => $self->{must_level});
554    ## TODO: error type documentation
555      }      }
556    };    };
557    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
558    $self->{next_input_character} = -1;    $self->{next_char} = -1;
559    
560    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
561      my (%opt) = @_;      my (%opt) = @_;
562      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
563        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
564        warn "Parse error ($opt{type}) at line $line column $column\n";
565    };    };
566    $self->{parse_error} = sub {    $self->{parse_error} = sub {
567      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
568    };    };
569    
570    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 572  sub parse_string ($$$;$) {
572    $self->_construct_tree;    $self->_construct_tree;
573    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
574    
575      delete $self->{parse_error}; # remove loop
576    
577    return $self->{document};    return $self->{document};
578  } # parse_string  } # parse_string
579    
580  sub new ($) {  sub new ($) {
581    my $class = shift;    my $class = shift;
582    my $self = bless {}, $class;    my $self = bless {}, $class;
583    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
584      $self->{next_input_character} = -1;      $self->{next_char} = -1;
585    };    };
586    $self->{parse_error} = sub {    $self->{parse_error} = sub {
587      #      #
588    };    };
589      $self->{change_encoding} = sub {
590        # if ($_[0] is a supported encoding) {
591        #   run "change the encoding" algorithm;
592        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
593        # }
594      };
595      $self->{application_cache_selection} = sub {
596        #
597      };
598    return $self;    return $self;
599  } # new  } # new
600    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 607  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
607  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
608  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
609    
610    sub DATA_STATE () { 0 }
611    sub ENTITY_DATA_STATE () { 1 }
612    sub TAG_OPEN_STATE () { 2 }
613    sub CLOSE_TAG_OPEN_STATE () { 3 }
614    sub TAG_NAME_STATE () { 4 }
615    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
616    sub ATTRIBUTE_NAME_STATE () { 6 }
617    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
618    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
619    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
620    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
621    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
622    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
623    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
624    sub COMMENT_START_STATE () { 14 }
625    sub COMMENT_START_DASH_STATE () { 15 }
626    sub COMMENT_STATE () { 16 }
627    sub COMMENT_END_STATE () { 17 }
628    sub COMMENT_END_DASH_STATE () { 18 }
629    sub BOGUS_COMMENT_STATE () { 19 }
630    sub DOCTYPE_STATE () { 20 }
631    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
632    sub DOCTYPE_NAME_STATE () { 22 }
633    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
634    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
635    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
636    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
637    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
638    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
639    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
640    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
641    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
642    sub BOGUS_DOCTYPE_STATE () { 32 }
643    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
644    sub SELF_CLOSING_START_TAG_STATE () { 34 }
645    sub CDATA_BLOCK_STATE () { 35 }
646    
647    sub DOCTYPE_TOKEN () { 1 }
648    sub COMMENT_TOKEN () { 2 }
649    sub START_TAG_TOKEN () { 3 }
650    sub END_TAG_TOKEN () { 4 }
651    sub END_OF_FILE_TOKEN () { 5 }
652    sub CHARACTER_TOKEN () { 6 }
653    
654  sub AFTER_HTML_IMS () { 0b100 }  sub AFTER_HTML_IMS () { 0b100 }
655  sub HEAD_IMS ()       { 0b1000 }  sub HEAD_IMS ()       { 0b1000 }
656  sub BODY_IMS ()       { 0b10000 }  sub BODY_IMS ()       { 0b10000 }
657  sub BODY_TABLE_IMS () { 0b100000 | BODY_IMS }  sub BODY_TABLE_IMS () { 0b100000 }
658  sub TABLE_IMS ()      { 0b1000000 }  sub TABLE_IMS ()      { 0b1000000 }
659  sub ROW_IMS ()        { 0b10000000 | TABLE_IMS }  sub ROW_IMS ()        { 0b10000000 }
660  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
661  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
662    sub SELECT_IMS ()     { 0b10000000000 }
663    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
664        ## NOTE: "in foreign content" insertion mode is special; it is combined
665        ## with the secondary insertion mode.  In this parser, they are stored
666        ## together in the bit-or'ed form.
667    
668    ## NOTE: "initial" and "before html" insertion modes have no constants.
669    
670    ## NOTE: "after after body" insertion mode.
671  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
672    
673    ## NOTE: "after after frameset" insertion mode.
674  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
675    
676  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
677  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
678  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
679  sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }  sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
680  sub IN_BODY_IM () { BODY_IMS }  sub IN_BODY_IM () { BODY_IMS }
681  sub IN_CELL_IM () { BODY_TABLE_IMS | 0b01 }  sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
682  sub IN_CAPTION_IM () { BODY_TABLE_IMS | 0b10 }  sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
683  sub IN_ROW_IM () { ROW_IMS | 0b01 }  sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
684  sub IN_TABLE_BODY_IM () { ROW_IMS | 0b10 }  sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
685  sub IN_TABLE_IM () { TABLE_IMS }  sub IN_TABLE_IM () { TABLE_IMS }
686  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
687  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
688  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
689  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
690    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
691  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
692    
693  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
694    
695  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
696    my $self = shift;    my $self = shift;
697    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
698    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
699    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
700    undef $self->{current_attribute};    undef $self->{current_attribute};
701    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
702    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
703      delete $self->{self_closing};
704    $self->{char} = [];    $self->{char} = [];
705    # $self->{next_input_character}    # $self->{next_char}
706    !!!next-input-character;    !!!next-input-character;
707    $self->{token} = [];    $self->{token} = [];
708    # $self->{escape}    # $self->{escape}
709  } # _initialize_tokenizer  } # _initialize_tokenizer
710    
711  ## A token has:  ## A token has:
712  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
713  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
714  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
715  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
716  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
717  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
718  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
719  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
720    ##        ->{name}
721    ##        ->{value}
722    ##        ->{has_reference} == 1 or 0
723    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
724    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
725    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
726    ##     while the token is pushed back to the stack.
727    
728    ## ISSUE: "When a DOCTYPE token is created, its
729    ## <i>self-closing flag</i> must be unset (its other state is that it
730    ## be set), and its attributes list must be empty.": Wrong subject?
731    
732  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
733    
# Line 221  sub _initialize_tokenizer ($) { Line 737  sub _initialize_tokenizer ($) {
737  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
738  ## and removed from the list.  ## and removed from the list.
739    
740    ## NOTE: HTML5 "Writing HTML documents" section, applied to
741    ## documents and not to user agents and conformance checkers,
742    ## contains some requirements that are not detected by the
743    ## parsing algorithm:
744    ## - Some requirements on character encoding declarations. ## TODO
745    ## - "Elements MUST NOT contain content that their content model disallows."
746    ##   ... Some are parse error, some are not (will be reported by c.c.).
747    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
748    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
749    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
750    
751    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
752    ## be detected by the HTML5 parsing algorithm:
753    ## - Text,
754    
755  sub _get_next_token ($) {  sub _get_next_token ($) {
756    my $self = shift;    my $self = shift;
757    
758      if ($self->{self_closing}) {
759        !!!parse-error (type => 'nestc', token => $self->{current_token});
760        ## NOTE: The |self_closing| flag is only set by start tag token.
761        ## In addition, when a start tag token is emitted, it is always set to
762        ## |current_token|.
763        delete $self->{self_closing};
764      }
765    
766    if (@{$self->{token}}) {    if (@{$self->{token}}) {
767        $self->{self_closing} = $self->{token}->[0]->{self_closing};
768      return shift @{$self->{token}};      return shift @{$self->{token}};
769    }    }
770    
771    A: {    A: {
772      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
773        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
774          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
775            $self->{state} = 'entity data';              not $self->{escape}) {
776              !!!cp (1);
777              $self->{state} = ENTITY_DATA_STATE;
778            !!!next-input-character;            !!!next-input-character;
779            redo A;            redo A;
780          } else {          } else {
781              !!!cp (2);
782            #            #
783          }          }
784        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
785          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
786            unless ($self->{escape}) {            unless ($self->{escape}) {
787              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
788                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
789                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
790                  !!!cp (3);
791                $self->{escape} = 1;                $self->{escape} = 1;
792                } else {
793                  !!!cp (4);
794              }              }
795              } else {
796                !!!cp (5);
797            }            }
798          }          }
799                    
800          #          #
801        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
802          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
803              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
804               not $self->{escape})) {               not $self->{escape})) {
805            $self->{state} = 'tag open';            !!!cp (6);
806              $self->{state} = TAG_OPEN_STATE;
807            !!!next-input-character;            !!!next-input-character;
808            redo A;            redo A;
809          } else {          } else {
810              !!!cp (7);
811            #            #
812          }          }
813        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
814          if ($self->{escape} and          if ($self->{escape} and
815              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
816            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
817                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
818                !!!cp (8);
819              delete $self->{escape};              delete $self->{escape};
820              } else {
821                !!!cp (9);
822            }            }
823            } else {
824              !!!cp (10);
825          }          }
826                    
827          #          #
828        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
829          !!!emit ({type => 'end-of-file'});          !!!cp (11);
830            !!!emit ({type => END_OF_FILE_TOKEN,
831                      line => $self->{line}, column => $self->{column}});
832          last A; ## TODO: ok?          last A; ## TODO: ok?
833          } else {
834            !!!cp (12);
835        }        }
836        # Anything else        # Anything else
837        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
838                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
839                       line => $self->{line}, column => $self->{column},
840                      };
841        ## Stay in the data state        ## Stay in the data state
842        !!!next-input-character;        !!!next-input-character;
843    
844        !!!emit ($token);        !!!emit ($token);
845    
846        redo A;        redo A;
847      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
848        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
849    
850          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
851                
852        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
853    
854        $self->{state} = 'data';        $self->{state} = DATA_STATE;
855        # next-input-character is already done        # next-input-character is already done
856    
857        unless (defined $token) {        unless (defined $token) {
858          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
859            !!!emit ({type => CHARACTER_TOKEN, data => '&',
860                      line => $l, column => $c,
861                     });
862        } else {        } else {
863            !!!cp (14);
864          !!!emit ($token);          !!!emit ($token);
865        }        }
866    
867        redo A;        redo A;
868      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
869        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
870          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
871              !!!cp (15);
872            !!!next-input-character;            !!!next-input-character;
873            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
874            redo A;            redo A;
875          } else {          } else {
876              !!!cp (16);
877            ## reconsume            ## reconsume
878            $self->{state} = 'data';            $self->{state} = DATA_STATE;
879    
880            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
881                        line => $self->{line_prev},
882                        column => $self->{column_prev},
883                       });
884    
885            redo A;            redo A;
886          }          }
887        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
888          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
889            $self->{state} = 'markup declaration open';            !!!cp (17);
890              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
891            !!!next-input-character;            !!!next-input-character;
892            redo A;            redo A;
893          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
894            $self->{state} = 'close tag open';            !!!cp (18);
895              $self->{state} = CLOSE_TAG_OPEN_STATE;
896            !!!next-input-character;            !!!next-input-character;
897            redo A;            redo A;
898          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
899                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
900              !!!cp (19);
901            $self->{current_token}            $self->{current_token}
902              = {type => 'start tag',              = {type => START_TAG_TOKEN,
903                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
904            $self->{state} = 'tag name';                 line => $self->{line_prev},
905                   column => $self->{column_prev}};
906              $self->{state} = TAG_NAME_STATE;
907            !!!next-input-character;            !!!next-input-character;
908            redo A;            redo A;
909          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
910                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
911            $self->{current_token} = {type => 'start tag',            !!!cp (20);
912                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
913            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
914                                        line => $self->{line_prev},
915                                        column => $self->{column_prev}};
916              $self->{state} = TAG_NAME_STATE;
917            !!!next-input-character;            !!!next-input-character;
918            redo A;            redo A;
919          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
920            !!!parse-error (type => 'empty start tag');            !!!cp (21);
921            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
922                              line => $self->{line_prev},
923                              column => $self->{column_prev});
924              $self->{state} = DATA_STATE;
925            !!!next-input-character;            !!!next-input-character;
926    
927            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
928                        line => $self->{line_prev},
929                        column => $self->{column_prev},
930                       });
931    
932            redo A;            redo A;
933          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
934            !!!parse-error (type => 'pio');            !!!cp (22);
935            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
936            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
937                              column => $self->{column_prev});
938              $self->{state} = BOGUS_COMMENT_STATE;
939              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
940                                        line => $self->{line_prev},
941                                        column => $self->{column_prev},
942                                       };
943              ## $self->{next_char} is intentionally left as is
944            redo A;            redo A;
945          } else {          } else {
946              !!!cp (23);
947            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
948            $self->{state} = 'data';            $self->{state} = DATA_STATE;
949            ## reconsume            ## reconsume
950    
951            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
952                        line => $self->{line_prev},
953                        column => $self->{column_prev},
954                       });
955    
956            redo A;            redo A;
957          }          }
958        } else {        } else {
959          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
960        }        }
961      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
962          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
963        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
964          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
965    
966            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
967            my @next_char;            my @next_char;
968            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++) {
969              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
970              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
971              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
972              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
973                  !!!cp (24);
974                !!!next-input-character;                !!!next-input-character;
975                next TAGNAME;                next TAGNAME;
976              } else {              } else {
977                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
978                  $self->{next_char} = shift @next_char; # reconsume
979                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
980                $self->{state} = 'data';                $self->{state} = DATA_STATE;
981    
982                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
983                            line => $l, column => $c,
984                           });
985        
986                redo A;                redo A;
987              }              }
988            }            }
989            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
990                
991            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
992                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
993                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
994                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
995                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
996                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
997                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
998                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
999              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1000                $self->{next_char} = shift @next_char; # reconsume
1001              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1002              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1003              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1004                          line => $l, column => $c,
1005                         });
1006              redo A;              redo A;
1007            } else {            } else {
1008              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1009                $self->{next_char} = shift @next_char;
1010              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1011              # and consume...              # and consume...
1012            }            }
1013          } else {          } else {
1014            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1015              !!!cp (28);
1016            # next-input-character is already done            # next-input-character is already done
1017            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1018            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1019                        line => $l, column => $c,
1020                       });
1021            redo A;            redo A;
1022          }          }
1023        }        }
1024                
1025        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1026            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1027          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1028                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1029          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1030          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1031          redo A;                 line => $l, column => $c};
1032        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1033                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1034          $self->{current_token} = {type => 'end tag',          redo A;
1035                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1036          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1037          !!!next-input-character;          !!!cp (30);
1038          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1039        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1040          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1041          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1042            !!!next-input-character;
1043            redo A;
1044          } elsif ($self->{next_char} == 0x003E) { # >
1045            !!!cp (31);
1046            !!!parse-error (type => 'empty end tag',
1047                            line => $self->{line_prev}, ## "<" in "</>"
1048                            column => $self->{column_prev} - 1);
1049            $self->{state} = DATA_STATE;
1050          !!!next-input-character;          !!!next-input-character;
1051          redo A;          redo A;
1052        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1053            !!!cp (32);
1054          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1055          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1056          # reconsume          # reconsume
1057    
1058          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1059                      line => $l, column => $c,
1060                     });
1061    
1062          redo A;          redo A;
1063        } else {        } else {
1064            !!!cp (33);
1065          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1066          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1067          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1068          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1069        }                                    column => $self->{column_prev} - 1,
1070      } elsif ($self->{state} eq 'tag name') {                                   };
1071        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1072            $self->{next_input_character} == 0x000A or # LF          redo A;
1073            $self->{next_input_character} == 0x000B or # VT        }
1074            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1075            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1076          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1077          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1078          redo A;            $self->{next_char} == 0x000C or # FF
1079        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1080          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1081            $self->{current_token}->{first_start_tag}          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1082                = not defined $self->{last_emitted_start_tag_name};          !!!next-input-character;
1083            redo A;
1084          } elsif ($self->{next_char} == 0x003E) { # >
1085            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1086              !!!cp (35);
1087            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1088          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1089            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1090            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1091              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1092            }            #  !!! cp (36);
1093              #  !!! parse-error (type => 'end tag attribute');
1094              #} else {
1095                !!!cp (37);
1096              #}
1097          } else {          } else {
1098            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1099          }          }
1100          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1101          !!!next-input-character;          !!!next-input-character;
1102    
1103          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1104    
1105          redo A;          redo A;
1106        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1107                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1108          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1109            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1110            # start tag or end tag            # start tag or end tag
1111          ## Stay in this state          ## Stay in this state
1112          !!!next-input-character;          !!!next-input-character;
1113          redo A;          redo A;
1114        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1115          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1116          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1117            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1118            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1119          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1120            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1121            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1122              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1123            }            #  !!! cp (40);
1124              #  !!! parse-error (type => 'end tag attribute');
1125              #} else {
1126                !!!cp (41);
1127              #}
1128          } else {          } else {
1129            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1130          }          }
1131          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1132          # reconsume          # reconsume
1133    
1134          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1135    
1136          redo A;          redo A;
1137        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1138            !!!cp (42);
1139            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1140          !!!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  
1141          redo A;          redo A;
1142        } else {        } else {
1143          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1144            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1145            # start tag or end tag            # start tag or end tag
1146          ## Stay in the state          ## Stay in the state
1147          !!!next-input-character;          !!!next-input-character;
1148          redo A;          redo A;
1149        }        }
1150      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1151        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1152            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1153            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1154            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1155            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1156            !!!cp (45);
1157          ## Stay in the state          ## Stay in the state
1158          !!!next-input-character;          !!!next-input-character;
1159          redo A;          redo A;
1160        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1161          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1162            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1163            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1164          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1165            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1166            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1167                !!!cp (47);
1168              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1169              } else {
1170                !!!cp (48);
1171            }            }
1172          } else {          } else {
1173            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1174          }          }
1175          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1176          !!!next-input-character;          !!!next-input-character;
1177    
1178          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1179    
1180          redo A;          redo A;
1181        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1182                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1183          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1184                                value => ''};          $self->{current_attribute}
1185          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1186                   value => '',
1187                   line => $self->{line}, column => $self->{column}};
1188            $self->{state} = ATTRIBUTE_NAME_STATE;
1189            !!!next-input-character;
1190            redo A;
1191          } elsif ($self->{next_char} == 0x002F) { # /
1192            !!!cp (50);
1193            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1194          !!!next-input-character;          !!!next-input-character;
1195          redo A;          redo A;
1196        } 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) {  
1197          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1198          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1199            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1200            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1201          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1202            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1203            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1204                !!!cp (53);
1205              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1206              } else {
1207                !!!cp (54);
1208            }            }
1209          } else {          } else {
1210            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1211          }          }
1212          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1213          # reconsume          # reconsume
1214    
1215          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1216    
1217          redo A;          redo A;
1218        } else {        } else {
1219          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1220                                value => ''};               0x0022 => 1, # "
1221          $self->{state} = 'attribute name';               0x0027 => 1, # '
1222                 0x003D => 1, # =
1223                }->{$self->{next_char}}) {
1224              !!!cp (55);
1225              !!!parse-error (type => 'bad attribute name');
1226            } else {
1227              !!!cp (56);
1228            }
1229            $self->{current_attribute}
1230                = {name => chr ($self->{next_char}),
1231                   value => '',
1232                   line => $self->{line}, column => $self->{column}};
1233            $self->{state} = ATTRIBUTE_NAME_STATE;
1234          !!!next-input-character;          !!!next-input-character;
1235          redo A;          redo A;
1236        }        }
1237      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1238        my $before_leave = sub {        my $before_leave = sub {
1239          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1240              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1241            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1242              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1243            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1244          } else {          } else {
1245              !!!cp (58);
1246            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1247              = $self->{current_attribute};              = $self->{current_attribute};
1248          }          }
1249        }; # $before_leave        }; # $before_leave
1250    
1251        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1252            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1253            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1254            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1255            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1256            !!!cp (59);
1257          $before_leave->();          $before_leave->();
1258          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1259          !!!next-input-character;          !!!next-input-character;
1260          redo A;          redo A;
1261        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1262            !!!cp (60);
1263          $before_leave->();          $before_leave->();
1264          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1265          !!!next-input-character;          !!!next-input-character;
1266          redo A;          redo A;
1267        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1268          $before_leave->();          $before_leave->();
1269          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1270            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1271            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1272          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1273              !!!cp (62);
1274            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1275            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1276              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 634  sub _get_next_token ($) { Line 1278  sub _get_next_token ($) {
1278          } else {          } else {
1279            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1280          }          }
1281          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1282          !!!next-input-character;          !!!next-input-character;
1283    
1284          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1285    
1286          redo A;          redo A;
1287        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1288                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1289          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1290            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1291          ## Stay in the state          ## Stay in the state
1292          !!!next-input-character;          !!!next-input-character;
1293          redo A;          redo A;
1294        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1295            !!!cp (64);
1296          $before_leave->();          $before_leave->();
1297            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1298          !!!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  
1299          redo A;          redo A;
1300        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1301          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1302          $before_leave->();          $before_leave->();
1303          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1304            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1305            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1306          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1307            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1308            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1309                !!!cp (67);
1310              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1311              } else {
1312                ## NOTE: This state should never be reached.
1313                !!!cp (68);
1314            }            }
1315          } else {          } else {
1316            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1317          }          }
1318          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1319          # reconsume          # reconsume
1320    
1321          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1322    
1323          redo A;          redo A;
1324        } else {        } else {
1325          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1326                $self->{next_char} == 0x0027) { # '
1327              !!!cp (69);
1328              !!!parse-error (type => 'bad attribute name');
1329            } else {
1330              !!!cp (70);
1331            }
1332            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1333          ## Stay in the state          ## Stay in the state
1334          !!!next-input-character;          !!!next-input-character;
1335          redo A;          redo A;
1336        }        }
1337      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1338        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1339            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1340            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1341            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1342            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1343            !!!cp (71);
1344          ## Stay in the state          ## Stay in the state
1345          !!!next-input-character;          !!!next-input-character;
1346          redo A;          redo A;
1347        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1348          $self->{state} = 'before attribute value';          !!!cp (72);
1349            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1350          !!!next-input-character;          !!!next-input-character;
1351          redo A;          redo A;
1352        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1353          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1354            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1355            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1356          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1357            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1358            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1359                !!!cp (74);
1360              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1361              } else {
1362                ## NOTE: This state should never be reached.
1363                !!!cp (75);
1364            }            }
1365          } else {          } else {
1366            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1367          }          }
1368          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1369          !!!next-input-character;          !!!next-input-character;
1370    
1371          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1372    
1373          redo A;          redo A;
1374        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1375                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1376          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1377                                value => ''};          $self->{current_attribute}
1378          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1379                   value => '',
1380                   line => $self->{line}, column => $self->{column}};
1381            $self->{state} = ATTRIBUTE_NAME_STATE;
1382            !!!next-input-character;
1383            redo A;
1384          } elsif ($self->{next_char} == 0x002F) { # /
1385            !!!cp (77);
1386            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1387          !!!next-input-character;          !!!next-input-character;
1388          redo A;          redo A;
1389        } 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) {  
1390          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
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 (79);
               = 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} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1396            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1397                !!!cp (80);
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 (81);
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}),          !!!cp (82);
1414                                value => ''};          $self->{current_attribute}
1415          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char}),
1416                   value => '',
1417                   line => $self->{line}, column => $self->{column}};
1418            $self->{state} = ATTRIBUTE_NAME_STATE;
1419          !!!next-input-character;          !!!next-input-character;
1420          redo A;                  redo A;        
1421        }        }
1422      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1423        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1424            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1425            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1426            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1427            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1428            !!!cp (83);
1429          ## Stay in the state          ## Stay in the state
1430          !!!next-input-character;          !!!next-input-character;
1431          redo A;          redo A;
1432        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1433          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1434            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1435          !!!next-input-character;          !!!next-input-character;
1436          redo A;          redo A;
1437        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1438          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1439            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1440          ## reconsume          ## reconsume
1441          redo A;          redo A;
1442        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1443          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1444            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1445          !!!next-input-character;          !!!next-input-character;
1446          redo A;          redo A;
1447        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1448          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1449            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1450            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1451          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1452            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1453            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1454                !!!cp (88);
1455              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1456              } else {
1457                ## NOTE: This state should never be reached.
1458                !!!cp (89);
1459            }            }
1460          } else {          } else {
1461            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1462          }          }
1463          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1464          !!!next-input-character;          !!!next-input-character;
1465    
1466          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1467    
1468          redo A;          redo A;
1469        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1470          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1471          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1472            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1473            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1474          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1475            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1476            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1477                !!!cp (91);
1478              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1479              } else {
1480                ## NOTE: This state should never be reached.
1481                !!!cp (92);
1482            }            }
1483          } else {          } else {
1484            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1485          }          }
1486          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1487          ## reconsume          ## reconsume
1488    
1489          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1490    
1491          redo A;          redo A;
1492        } else {        } else {
1493          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1494          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1495              !!!parse-error (type => 'bad attribute value');
1496            } else {
1497              !!!cp (94);
1498            }
1499            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1500            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1501          !!!next-input-character;          !!!next-input-character;
1502          redo A;          redo A;
1503        }        }
1504      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1505        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1506          $self->{state} = 'before attribute name';          !!!cp (95);
1507            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1508          !!!next-input-character;          !!!next-input-character;
1509          redo A;          redo A;
1510        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1511          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1512          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1513            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1514          !!!next-input-character;          !!!next-input-character;
1515          redo A;          redo A;
1516        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1517          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1518          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1519            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1520            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1521          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1522            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1523            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1524                !!!cp (98);
1525              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1526              } else {
1527                ## NOTE: This state should never be reached.
1528                !!!cp (99);
1529            }            }
1530          } else {          } else {
1531            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1532          }          }
1533          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1534          ## reconsume          ## reconsume
1535    
1536          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1537    
1538          redo A;          redo A;
1539        } else {        } else {
1540          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1541            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1542          ## Stay in the state          ## Stay in the state
1543          !!!next-input-character;          !!!next-input-character;
1544          redo A;          redo A;
1545        }        }
1546      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1547        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1548          $self->{state} = 'before attribute name';          !!!cp (101);
1549            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1550          !!!next-input-character;          !!!next-input-character;
1551          redo A;          redo A;
1552        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1553          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1554          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1555            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1556          !!!next-input-character;          !!!next-input-character;
1557          redo A;          redo A;
1558        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1559          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1560          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1561            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1562            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1563          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1564            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1565            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1566                !!!cp (104);
1567              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1568              } else {
1569                ## NOTE: This state should never be reached.
1570                !!!cp (105);
1571            }            }
1572          } else {          } else {
1573            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1574          }          }
1575          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1576          ## reconsume          ## reconsume
1577    
1578          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1579    
1580          redo A;          redo A;
1581        } else {        } else {
1582          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1583            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1584          ## Stay in the state          ## Stay in the state
1585          !!!next-input-character;          !!!next-input-character;
1586          redo A;          redo A;
1587        }        }
1588      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1589        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1590            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1591            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1592            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1593            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1594          $self->{state} = 'before attribute name';          !!!cp (107);
1595          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1596          redo A;          !!!next-input-character;
1597        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1598          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1599          $self->{state} = 'entity in attribute value';          !!!cp (108);
1600          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1601          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1602        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1603          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1604            $self->{current_token}->{first_start_tag}        } elsif ($self->{next_char} == 0x003E) { # >
1605                = not defined $self->{last_emitted_start_tag_name};          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1606              !!!cp (109);
1607            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1608          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1609            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1610            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1611                !!!cp (110);
1612              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1613              } else {
1614                ## NOTE: This state should never be reached.
1615                !!!cp (111);
1616            }            }
1617          } else {          } else {
1618            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1619          }          }
1620          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1621          !!!next-input-character;          !!!next-input-character;
1622    
1623          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1624    
1625          redo A;          redo A;
1626        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1627          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1628          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1629            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1630            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1631          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1632            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1633            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1634                !!!cp (113);
1635              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1636              } else {
1637                ## NOTE: This state should never be reached.
1638                !!!cp (114);
1639            }            }
1640          } else {          } else {
1641            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1642          }          }
1643          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1644          ## reconsume          ## reconsume
1645    
1646          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1647    
1648          redo A;          redo A;
1649        } else {        } else {
1650          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1651                 0x0022 => 1, # "
1652                 0x0027 => 1, # '
1653                 0x003D => 1, # =
1654                }->{$self->{next_char}}) {
1655              !!!cp (115);
1656              !!!parse-error (type => 'bad attribute value');
1657            } else {
1658              !!!cp (116);
1659            }
1660            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1661          ## Stay in the state          ## Stay in the state
1662          !!!next-input-character;          !!!next-input-character;
1663          redo A;          redo A;
1664        }        }
1665      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1666        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1667              (1,
1668               $self->{last_attribute_value_state}
1669                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1670               $self->{last_attribute_value_state}
1671                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1672               -1);
1673    
1674        unless (defined $token) {        unless (defined $token) {
1675            !!!cp (117);
1676          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1677        } else {        } else {
1678            !!!cp (118);
1679          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1680            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1681          ## 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"
1682        }        }
1683    
1684        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1685        # next-input-character is already done        # next-input-character is already done
1686        redo A;        redo A;
1687      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1688          if ($self->{next_char} == 0x0009 or # HT
1689              $self->{next_char} == 0x000A or # LF
1690              $self->{next_char} == 0x000B or # VT
1691              $self->{next_char} == 0x000C or # FF
1692              $self->{next_char} == 0x0020) { # SP
1693            !!!cp (118);
1694            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1695            !!!next-input-character;
1696            redo A;
1697          } elsif ($self->{next_char} == 0x003E) { # >
1698            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1699              !!!cp (119);
1700              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1701            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1702              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1703              if ($self->{current_token}->{attributes}) {
1704                !!!cp (120);
1705                !!!parse-error (type => 'end tag attribute');
1706              } else {
1707                ## NOTE: This state should never be reached.
1708                !!!cp (121);
1709              }
1710            } else {
1711              die "$0: $self->{current_token}->{type}: Unknown token type";
1712            }
1713            $self->{state} = DATA_STATE;
1714            !!!next-input-character;
1715    
1716            !!!emit ($self->{current_token}); # start tag or end tag
1717    
1718            redo A;
1719          } elsif ($self->{next_char} == 0x002F) { # /
1720            !!!cp (122);
1721            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1722            !!!next-input-character;
1723            redo A;
1724          } else {
1725            !!!cp ('124.1');
1726            !!!parse-error (type => 'no space between attributes');
1727            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1728            ## reconsume
1729            redo A;
1730          }
1731        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1732          if ($self->{next_char} == 0x003E) { # >
1733            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1734              !!!cp ('124.2');
1735              !!!parse-error (type => 'nestc', token => $self->{current_token});
1736              ## TODO: Different type than slash in start tag
1737              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1738              if ($self->{current_token}->{attributes}) {
1739                !!!cp ('124.4');
1740                !!!parse-error (type => 'end tag attribute');
1741              } else {
1742                !!!cp ('124.5');
1743              }
1744              ## TODO: Test |<title></title/>|
1745            } else {
1746              !!!cp ('124.3');
1747              $self->{self_closing} = 1;
1748            }
1749    
1750            $self->{state} = DATA_STATE;
1751            !!!next-input-character;
1752    
1753            !!!emit ($self->{current_token}); # start tag or end tag
1754    
1755            redo A;
1756          } else {
1757            !!!cp ('124.4');
1758            !!!parse-error (type => 'nestc');
1759            ## TODO: This error type is wrong.
1760            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1761            ## Reconsume.
1762            redo A;
1763          }
1764        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1765        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1766                
1767        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1768          #my $token = {type => COMMENT_TOKEN, data => ''};
1769    
1770        BC: {        BC: {
1771          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1772            $self->{state} = 'data';            !!!cp (124);
1773              $self->{state} = DATA_STATE;
1774            !!!next-input-character;            !!!next-input-character;
1775    
1776            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1777    
1778            redo A;            redo A;
1779          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1780            $self->{state} = 'data';            !!!cp (125);
1781              $self->{state} = DATA_STATE;
1782            ## reconsume            ## reconsume
1783    
1784            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1785    
1786            redo A;            redo A;
1787          } else {          } else {
1788            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1789              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1790            !!!next-input-character;            !!!next-input-character;
1791            redo BC;            redo BC;
1792          }          }
1793        } # BC        } # BC
1794      } elsif ($self->{state} eq 'markup declaration open') {  
1795          die "$0: _get_next_token: unexpected case [BC]";
1796        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1797        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1798    
1799          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1800    
1801        my @next_char;        my @next_char;
1802        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1803                
1804        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1805          !!!next-input-character;          !!!next-input-character;
1806          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1807          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1808            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1809            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1810                                        line => $l, column => $c,
1811                                       };
1812              $self->{state} = COMMENT_START_STATE;
1813            !!!next-input-character;            !!!next-input-character;
1814            redo A;            redo A;
1815            } else {
1816              !!!cp (128);
1817            }
1818          } elsif ($self->{next_char} == 0x0044 or # D
1819                   $self->{next_char} == 0x0064) { # d
1820            !!!next-input-character;
1821            push @next_char, $self->{next_char};
1822            if ($self->{next_char} == 0x004F or # O
1823                $self->{next_char} == 0x006F) { # o
1824              !!!next-input-character;
1825              push @next_char, $self->{next_char};
1826              if ($self->{next_char} == 0x0043 or # C
1827                  $self->{next_char} == 0x0063) { # c
1828                !!!next-input-character;
1829                push @next_char, $self->{next_char};
1830                if ($self->{next_char} == 0x0054 or # T
1831                    $self->{next_char} == 0x0074) { # t
1832                  !!!next-input-character;
1833                  push @next_char, $self->{next_char};
1834                  if ($self->{next_char} == 0x0059 or # Y
1835                      $self->{next_char} == 0x0079) { # y
1836                    !!!next-input-character;
1837                    push @next_char, $self->{next_char};
1838                    if ($self->{next_char} == 0x0050 or # P
1839                        $self->{next_char} == 0x0070) { # p
1840                      !!!next-input-character;
1841                      push @next_char, $self->{next_char};
1842                      if ($self->{next_char} == 0x0045 or # E
1843                          $self->{next_char} == 0x0065) { # e
1844                        !!!cp (129);
1845                        ## TODO: What a stupid code this is!
1846                        $self->{state} = DOCTYPE_STATE;
1847                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1848                                                  quirks => 1,
1849                                                  line => $l, column => $c,
1850                                                 };
1851                        !!!next-input-character;
1852                        redo A;
1853                      } else {
1854                        !!!cp (130);
1855                      }
1856                    } else {
1857                      !!!cp (131);
1858                    }
1859                  } else {
1860                    !!!cp (132);
1861                  }
1862                } else {
1863                  !!!cp (133);
1864                }
1865              } else {
1866                !!!cp (134);
1867              }
1868            } else {
1869              !!!cp (135);
1870          }          }
1871        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1872                 $self->{next_input_character} == 0x0064) { # d                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1873                   $self->{next_char} == 0x005B) { # [
1874          !!!next-input-character;          !!!next-input-character;
1875          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1876          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x0043) { # C
             $self->{next_input_character} == 0x006F) { # o  
1877            !!!next-input-character;            !!!next-input-character;
1878            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1879            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0044) { # D
               $self->{next_input_character} == 0x0063) { # c  
1880              !!!next-input-character;              !!!next-input-character;
1881              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1882              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0041) { # A
                 $self->{next_input_character} == 0x0074) { # t  
1883                !!!next-input-character;                !!!next-input-character;
1884                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1885                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0054) { # T
                   $self->{next_input_character} == 0x0079) { # y  
1886                  !!!next-input-character;                  !!!next-input-character;
1887                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1888                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0041) { # A
                     $self->{next_input_character} == 0x0070) { # p  
1889                    !!!next-input-character;                    !!!next-input-character;
1890                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1891                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x005B) { # [
1892                        $self->{next_input_character} == 0x0065) { # e                      !!!cp (135.1);
1893                      ## ISSUE: What a stupid code this is!                      $self->{state} = CDATA_BLOCK_STATE;
                     $self->{state} = 'DOCTYPE';  
1894                      !!!next-input-character;                      !!!next-input-character;
1895                      redo A;                      redo A;
1896                      } else {
1897                        !!!cp (135.2);
1898                    }                    }
1899                    } else {
1900                      !!!cp (135.3);
1901                  }                  }
1902                  } else {
1903                    !!!cp (135.4);                
1904                }                }
1905                } else {
1906                  !!!cp (135.5);
1907              }              }
1908              } else {
1909                !!!cp (135.6);
1910            }            }
1911            } else {
1912              !!!cp (135.7);
1913          }          }
1914          } else {
1915            !!!cp (136);
1916        }        }
1917    
1918        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1919        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1920        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1921        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1922          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1923                                    line => $l, column => $c,
1924                                   };
1925        redo A;        redo A;
1926                
1927        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1928        ## 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?
1929      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1930        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1931          $self->{state} = 'comment start dash';          !!!cp (137);
1932            $self->{state} = COMMENT_START_DASH_STATE;
1933          !!!next-input-character;          !!!next-input-character;
1934          redo A;          redo A;
1935        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1936            !!!cp (138);
1937          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1938          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1939          !!!next-input-character;          !!!next-input-character;
1940    
1941          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1942    
1943          redo A;          redo A;
1944        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1945            !!!cp (139);
1946          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1947          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1948          ## reconsume          ## reconsume
1949    
1950          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1951    
1952          redo A;          redo A;
1953        } else {        } else {
1954            !!!cp (140);
1955          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1956              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
1957          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1958          !!!next-input-character;          !!!next-input-character;
1959          redo A;          redo A;
1960        }        }
1961      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1962        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1963          $self->{state} = 'comment end';          !!!cp (141);
1964            $self->{state} = COMMENT_END_STATE;
1965          !!!next-input-character;          !!!next-input-character;
1966          redo A;          redo A;
1967        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1968            !!!cp (142);
1969          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1970          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1971          !!!next-input-character;          !!!next-input-character;
1972    
1973          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1974    
1975          redo A;          redo A;
1976        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1977            !!!cp (143);
1978          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1979          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1980          ## reconsume          ## reconsume
1981    
1982          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1983    
1984          redo A;          redo A;
1985        } else {        } else {
1986            !!!cp (144);
1987          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1988              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
1989          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1990          !!!next-input-character;          !!!next-input-character;
1991          redo A;          redo A;
1992        }        }
1993      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1994        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1995          $self->{state} = 'comment end dash';          !!!cp (145);
1996            $self->{state} = COMMENT_END_DASH_STATE;
1997          !!!next-input-character;          !!!next-input-character;
1998          redo A;          redo A;
1999        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2000            !!!cp (146);
2001          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2002          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2003          ## reconsume          ## reconsume
2004    
2005          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2006    
2007          redo A;          redo A;
2008        } else {        } else {
2009          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2010            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2011          ## Stay in the state          ## Stay in the state
2012          !!!next-input-character;          !!!next-input-character;
2013          redo A;          redo A;
2014        }        }
2015      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2016        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2017          $self->{state} = 'comment end';          !!!cp (148);
2018            $self->{state} = COMMENT_END_STATE;
2019          !!!next-input-character;          !!!next-input-character;
2020          redo A;          redo A;
2021        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2022            !!!cp (149);
2023          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2024          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2025          ## reconsume          ## reconsume
2026    
2027          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2028    
2029          redo A;          redo A;
2030        } else {        } else {
2031          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2032          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2033            $self->{state} = COMMENT_STATE;
2034          !!!next-input-character;          !!!next-input-character;
2035          redo A;          redo A;
2036        }        }
2037      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2038        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2039          $self->{state} = 'data';          !!!cp (151);
2040            $self->{state} = DATA_STATE;
2041          !!!next-input-character;          !!!next-input-character;
2042    
2043          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2044    
2045          redo A;          redo A;
2046        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2047          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2048            !!!parse-error (type => 'dash in comment',
2049                            line => $self->{line_prev},
2050                            column => $self->{column_prev});
2051          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2052          ## Stay in the state          ## Stay in the state
2053          !!!next-input-character;          !!!next-input-character;
2054          redo A;          redo A;
2055        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2056            !!!cp (153);
2057          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2058          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2059          ## reconsume          ## reconsume
2060    
2061          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2062    
2063          redo A;          redo A;
2064        } else {        } else {
2065          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2066          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2067          $self->{state} = 'comment';                          line => $self->{line_prev},
2068                            column => $self->{column_prev});
2069            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2070            $self->{state} = COMMENT_STATE;
2071          !!!next-input-character;          !!!next-input-character;
2072          redo A;          redo A;
2073        }        }
2074      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2075        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2076            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2077            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2078            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2079            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2080          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2081            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2082          !!!next-input-character;          !!!next-input-character;
2083          redo A;          redo A;
2084        } else {        } else {
2085            !!!cp (156);
2086          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2087          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2088          ## reconsume          ## reconsume
2089          redo A;          redo A;
2090        }        }
2091      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2092        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2093            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2094            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2095            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2096            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2097            !!!cp (157);
2098          ## Stay in the state          ## Stay in the state
2099          !!!next-input-character;          !!!next-input-character;
2100          redo A;          redo A;
2101        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2102            !!!cp (158);
2103          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2104          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2105          !!!next-input-character;          !!!next-input-character;
2106    
2107          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2108    
2109          redo A;          redo A;
2110        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2111            !!!cp (159);
2112          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2113          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2114          ## reconsume          ## reconsume
2115    
2116          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2117    
2118          redo A;          redo A;
2119        } else {        } else {
2120          $self->{current_token}          !!!cp (160);
2121              = {type => 'DOCTYPE',          $self->{current_token}->{name} = chr $self->{next_char};
2122                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2123  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2124          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2125          !!!next-input-character;          !!!next-input-character;
2126          redo A;          redo A;
2127        }        }
2128      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2129  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2130        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2131            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2132            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2133            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2134            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2135          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2136            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2137          !!!next-input-character;          !!!next-input-character;
2138          redo A;          redo A;
2139        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2140          $self->{state} = 'data';          !!!cp (162);
2141            $self->{state} = DATA_STATE;
2142          !!!next-input-character;          !!!next-input-character;
2143    
2144          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2145    
2146          redo A;          redo A;
2147        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2148            !!!cp (163);
2149          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2150          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2151          ## reconsume          ## reconsume
2152    
2153          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2154          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2155    
2156          redo A;          redo A;
2157        } else {        } else {
2158            !!!cp (164);
2159          $self->{current_token}->{name}          $self->{current_token}->{name}
2160            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2161          ## Stay in the state          ## Stay in the state
2162          !!!next-input-character;          !!!next-input-character;
2163          redo A;          redo A;
2164        }        }
2165      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2166        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2167            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2168            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2169            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2170            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2171            !!!cp (165);
2172          ## Stay in the state          ## Stay in the state
2173          !!!next-input-character;          !!!next-input-character;
2174          redo A;          redo A;
2175        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2176          $self->{state} = 'data';          !!!cp (166);
2177            $self->{state} = DATA_STATE;
2178          !!!next-input-character;          !!!next-input-character;
2179    
2180          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2181    
2182          redo A;          redo A;
2183        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2184            !!!cp (167);
2185          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2186          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2187          ## reconsume          ## reconsume
2188    
2189          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2190          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2191    
2192          redo A;          redo A;
2193        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2194                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2195          !!!next-input-character;          !!!next-input-character;
2196          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2197              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2198            !!!next-input-character;            !!!next-input-character;
2199            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2200                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2201              !!!next-input-character;              !!!next-input-character;
2202              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2203                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2204                !!!next-input-character;                !!!next-input-character;
2205                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2206                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2207                  !!!next-input-character;                  !!!next-input-character;
2208                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2209                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2210                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
2211                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2212                    !!!next-input-character;                    !!!next-input-character;
2213                    redo A;                    redo A;
2214                    } else {
2215                      !!!cp (169);
2216                  }                  }
2217                  } else {
2218                    !!!cp (170);
2219                }                }
2220                } else {
2221                  !!!cp (171);
2222              }              }
2223              } else {
2224                !!!cp (172);
2225            }            }
2226            } else {
2227              !!!cp (173);
2228          }          }
2229    
2230          #          #
2231        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2232                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2233          !!!next-input-character;          !!!next-input-character;
2234          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2235              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2236            !!!next-input-character;            !!!next-input-character;
2237            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2238                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2239              !!!next-input-character;              !!!next-input-character;
2240              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2241                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2242                !!!next-input-character;                !!!next-input-character;
2243                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2244                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2245                  !!!next-input-character;                  !!!next-input-character;
2246                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2247                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2248                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
2249                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2250                    !!!next-input-character;                    !!!next-input-character;
2251                    redo A;                    redo A;
2252                    } else {
2253                      !!!cp (175);
2254                  }                  }
2255                  } else {
2256                    !!!cp (176);
2257                }                }
2258                } else {
2259                  !!!cp (177);
2260              }              }
2261              } else {
2262                !!!cp (178);
2263            }            }
2264            } else {
2265              !!!cp (179);
2266          }          }
2267    
2268          #          #
2269        } else {        } else {
2270            !!!cp (180);
2271          !!!next-input-character;          !!!next-input-character;
2272          #          #
2273        }        }
2274    
2275        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2276        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
2277    
2278          $self->{state} = BOGUS_DOCTYPE_STATE;
2279        # next-input-character is already done        # next-input-character is already done
2280        redo A;        redo A;
2281      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2282        if ({        if ({
2283              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2284              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2285            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2286            !!!cp (181);
2287          ## Stay in the state          ## Stay in the state
2288          !!!next-input-character;          !!!next-input-character;
2289          redo A;          redo A;
2290        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2291            !!!cp (182);
2292          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2293          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2294          !!!next-input-character;          !!!next-input-character;
2295          redo A;          redo A;
2296        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2297            !!!cp (183);
2298          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2299          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2300          !!!next-input-character;          !!!next-input-character;
2301          redo A;          redo A;
2302        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2303            !!!cp (184);
2304          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2305    
2306          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2307          !!!next-input-character;          !!!next-input-character;
2308    
2309          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2310          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2311    
2312          redo A;          redo A;
2313        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2314            !!!cp (185);
2315          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2316    
2317          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2318          ## reconsume          ## reconsume
2319    
2320          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2321          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2322    
2323          redo A;          redo A;
2324        } else {        } else {
2325            !!!cp (186);
2326          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2327          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2328    
2329            $self->{state} = BOGUS_DOCTYPE_STATE;
2330          !!!next-input-character;          !!!next-input-character;
2331          redo A;          redo A;
2332        }        }
2333      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2334        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2335          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2336            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2337          !!!next-input-character;          !!!next-input-character;
2338          redo A;          redo A;
2339        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2340            !!!cp (188);
2341          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2342    
2343          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2344            !!!next-input-character;
2345    
2346            $self->{current_token}->{quirks} = 1;
2347            !!!emit ($self->{current_token}); # DOCTYPE
2348    
2349            redo A;
2350          } elsif ($self->{next_char} == -1) {
2351            !!!cp (189);
2352            !!!parse-error (type => 'unclosed PUBLIC literal');
2353    
2354            $self->{state} = DATA_STATE;
2355          ## reconsume          ## reconsume
2356    
2357          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2358          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2359    
2360          redo A;          redo A;
2361        } else {        } else {
2362            !!!cp (190);
2363          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2364              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2365          ## Stay in the state          ## Stay in the state
2366          !!!next-input-character;          !!!next-input-character;
2367          redo A;          redo A;
2368        }        }
2369      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2370        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2371          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2372            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2373            !!!next-input-character;
2374            redo A;
2375          } elsif ($self->{next_char} == 0x003E) { # >
2376            !!!cp (192);
2377            !!!parse-error (type => 'unclosed PUBLIC literal');
2378    
2379            $self->{state} = DATA_STATE;
2380          !!!next-input-character;          !!!next-input-character;
2381    
2382            $self->{current_token}->{quirks} = 1;
2383            !!!emit ($self->{current_token}); # DOCTYPE
2384    
2385          redo A;          redo A;
2386        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2387            !!!cp (193);
2388          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2389    
2390          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2391          ## reconsume          ## reconsume
2392    
2393          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2394          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2395    
2396          redo A;          redo A;
2397        } else {        } else {
2398            !!!cp (194);
2399          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2400              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2401          ## Stay in the state          ## Stay in the state
2402          !!!next-input-character;          !!!next-input-character;
2403          redo A;          redo A;
2404        }        }
2405      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2406        if ({        if ({
2407              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2408              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2409            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2410            !!!cp (195);
2411          ## Stay in the state          ## Stay in the state
2412          !!!next-input-character;          !!!next-input-character;
2413          redo A;          redo A;
2414        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2415            !!!cp (196);
2416          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2417          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2418          !!!next-input-character;          !!!next-input-character;
2419          redo A;          redo A;
2420        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2421            !!!cp (197);
2422          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2423          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2424          !!!next-input-character;          !!!next-input-character;
2425          redo A;          redo A;
2426        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2427          $self->{state} = 'data';          !!!cp (198);
2428            $self->{state} = DATA_STATE;
2429          !!!next-input-character;          !!!next-input-character;
2430    
2431          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2432    
2433          redo A;          redo A;
2434        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2435            !!!cp (199);
2436          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2437    
2438          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2439          ## reconsume          ## reconsume
2440    
2441          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2442          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2443    
2444          redo A;          redo A;
2445        } else {        } else {
2446            !!!cp (200);
2447          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2448          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2449    
2450            $self->{state} = BOGUS_DOCTYPE_STATE;
2451          !!!next-input-character;          !!!next-input-character;
2452          redo A;          redo A;
2453        }        }
2454      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2455        if ({        if ({
2456              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2457              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2458            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2459            !!!cp (201);
2460          ## Stay in the state          ## Stay in the state
2461          !!!next-input-character;          !!!next-input-character;
2462          redo A;          redo A;
2463        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2464            !!!cp (202);
2465          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2466          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2467          !!!next-input-character;          !!!next-input-character;
2468          redo A;          redo A;
2469        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2470            !!!cp (203);
2471          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2472          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2473          !!!next-input-character;          !!!next-input-character;
2474          redo A;          redo A;
2475        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2476            !!!cp (204);
2477          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2478          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2479          !!!next-input-character;          !!!next-input-character;
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        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2486            !!!cp (205);
2487          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2488    
2489          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2490          ## reconsume          ## reconsume
2491    
2492          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2493          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2494    
2495          redo A;          redo A;
2496        } else {        } else {
2497            !!!cp (206);
2498          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2499          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2500    
2501            $self->{state} = BOGUS_DOCTYPE_STATE;
2502          !!!next-input-character;          !!!next-input-character;
2503          redo A;          redo A;
2504        }        }
2505      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2506        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2507          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2508            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2509            !!!next-input-character;
2510            redo A;
2511          } elsif ($self->{next_char} == 0x003E) { # >
2512            !!!cp (208);
2513            !!!parse-error (type => 'unclosed PUBLIC literal');
2514    
2515            $self->{state} = DATA_STATE;
2516          !!!next-input-character;          !!!next-input-character;
2517    
2518            $self->{current_token}->{quirks} = 1;
2519            !!!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 (209);
2524          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
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 (210);
2535          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2536              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2537          ## Stay in the state          ## Stay in the state
2538          !!!next-input-character;          !!!next-input-character;
2539          redo A;          redo A;
2540        }        }
2541      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2542        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2543          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2544            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2545          !!!next-input-character;          !!!next-input-character;
2546          redo A;          redo A;
2547        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2548            !!!cp (212);
2549            !!!parse-error (type => 'unclosed PUBLIC literal');
2550    
2551            $self->{state} = DATA_STATE;
2552            !!!next-input-character;
2553    
2554            $self->{current_token}->{quirks} = 1;
2555            !!!emit ($self->{current_token}); # DOCTYPE
2556    
2557            redo A;
2558          } elsif ($self->{next_char} == -1) {
2559            !!!cp (213);
2560          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2561    
2562          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2563          ## reconsume          ## reconsume
2564    
2565          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2566          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2567    
2568          redo A;          redo A;
2569        } else {        } else {
2570            !!!cp (214);
2571          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2572              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2573          ## Stay in the state          ## Stay in the state
2574          !!!next-input-character;          !!!next-input-character;
2575          redo A;          redo A;
2576        }        }
2577      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2578        if ({        if ({
2579              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2580              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2581            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2582            !!!cp (215);
2583          ## Stay in the state          ## Stay in the state
2584          !!!next-input-character;          !!!next-input-character;
2585          redo A;          redo A;
2586        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2587          $self->{state} = 'data';          !!!cp (216);
2588            $self->{state} = DATA_STATE;
2589          !!!next-input-character;          !!!next-input-character;
2590    
2591          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2592    
2593          redo A;          redo A;
2594        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2595            !!!cp (217);
2596          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2597    
2598          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2599          ## reconsume          ## reconsume
2600    
2601          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2602          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2603    
2604          redo A;          redo A;
2605        } else {        } else {
2606            !!!cp (218);
2607          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2608          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2609    
2610            $self->{state} = BOGUS_DOCTYPE_STATE;
2611          !!!next-input-character;          !!!next-input-character;
2612          redo A;          redo A;
2613        }        }
2614      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2615        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2616          $self->{state} = 'data';          !!!cp (219);
2617            $self->{state} = DATA_STATE;
2618          !!!next-input-character;          !!!next-input-character;
2619    
         delete $self->{current_token}->{correct};  
2620          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2621    
2622          redo A;          redo A;
2623        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2624            !!!cp (220);
2625          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2626          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2627          ## reconsume          ## reconsume
2628    
         delete $self->{current_token}->{correct};  
2629          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2630    
2631          redo A;          redo A;
2632        } else {        } else {
2633            !!!cp (221);
2634          ## Stay in the state          ## Stay in the state
2635          !!!next-input-character;          !!!next-input-character;
2636          redo A;          redo A;
2637        }        }
2638        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2639          my $s = '';
2640          
2641          my ($l, $c) = ($self->{line}, $self->{column});
2642    
2643          CS: while ($self->{next_char} != -1) {
2644            if ($self->{next_char} == 0x005D) { # ]
2645              !!!next-input-character;
2646              if ($self->{next_char} == 0x005D) { # ]
2647                !!!next-input-character;
2648                MDC: {
2649                  if ($self->{next_char} == 0x003E) { # >
2650                    !!!cp (221.1);
2651                    !!!next-input-character;
2652                    last CS;
2653                  } elsif ($self->{next_char} == 0x005D) { # ]
2654                    !!!cp (221.2);
2655                    $s .= ']';
2656                    !!!next-input-character;
2657                    redo MDC;
2658                  } else {
2659                    !!!cp (221.3);
2660                    $s .= ']]';
2661                    #
2662                  }
2663                } # MDC
2664              } else {
2665                !!!cp (221.4);
2666                $s .= ']';
2667                #
2668              }
2669            } else {
2670              !!!cp (221.5);
2671              #
2672            }
2673            $s .= chr $self->{next_char};
2674            !!!next-input-character;
2675          } # CS
2676    
2677          $self->{state} = DATA_STATE;
2678          ## next-input-character done or EOF, which is reconsumed.
2679    
2680          if (length $s) {
2681            !!!cp (221.6);
2682            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2683                      line => $l, column => $c});
2684          } else {
2685            !!!cp (221.7);
2686          }
2687    
2688          redo A;
2689    
2690          ## ISSUE: "text tokens" in spec.
2691          ## TODO: Streaming support
2692      } else {      } else {
2693        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2694      }      }
# Line 1636  sub _get_next_token ($) { Line 2697  sub _get_next_token ($) {
2697    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2698  } # _get_next_token  } # _get_next_token
2699    
2700  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2701    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2702    
2703      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2704    
2705    if ({    if ({
2706         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2707         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2708        }->{$self->{next_input_character}}) {         $additional => 1,
2709          }->{$self->{next_char}}) {
2710        !!!cp (1001);
2711      ## Don't consume      ## Don't consume
2712      ## No error      ## No error
2713      return undef;      return undef;
2714    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2715      !!!next-input-character;      !!!next-input-character;
2716      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2717          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2718        my $code;        my $code;
2719        X: {        X: {
2720          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2721          !!!next-input-character;          !!!next-input-character;
2722          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2723              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2724              !!!cp (1002);
2725            $code ||= 0;            $code ||= 0;
2726            $code *= 0x10;            $code *= 0x10;
2727            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2728            redo X;            redo X;
2729          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2730                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2731              !!!cp (1003);
2732            $code ||= 0;            $code ||= 0;
2733            $code *= 0x10;            $code *= 0x10;
2734            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2735            redo X;            redo X;
2736          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2737                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2738              !!!cp (1004);
2739            $code ||= 0;            $code ||= 0;
2740            $code *= 0x10;            $code *= 0x10;
2741            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2742            redo X;            redo X;
2743          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2744            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2745            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2746            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2747              $self->{next_char} = 0x0023; # #
2748            return undef;            return undef;
2749          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2750              !!!cp (1006);
2751            !!!next-input-character;            !!!next-input-character;
2752          } else {          } else {
2753            !!!parse-error (type => 'no refc');            !!!cp (1007);
2754              !!!parse-error (type => 'no refc', line => $l, column => $c);
2755          }          }
2756    
2757          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2758            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2759              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2760            $code = 0xFFFD;            $code = 0xFFFD;
2761          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2762            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2763              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2764            $code = 0xFFFD;            $code = 0xFFFD;
2765          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2766            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2767              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2768            $code = 0x000A;            $code = 0x000A;
2769          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2770            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2771              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2772            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2773          }          }
2774    
2775          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2776                    has_reference => 1,
2777                    line => $l, column => $c,
2778                   };
2779        } # X        } # X
2780      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2781               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2782        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2783        !!!next-input-character;        !!!next-input-character;
2784                
2785        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2786                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2787            !!!cp (1012);
2788          $code *= 10;          $code *= 10;
2789          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2790                    
2791          !!!next-input-character;          !!!next-input-character;
2792        }        }
2793    
2794        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2795            !!!cp (1013);
2796          !!!next-input-character;          !!!next-input-character;
2797        } else {        } else {
2798          !!!parse-error (type => 'no refc');          !!!cp (1014);
2799            !!!parse-error (type => 'no refc', line => $l, column => $c);
2800        }        }
2801    
2802        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2803          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2804            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2805          $code = 0xFFFD;          $code = 0xFFFD;
2806        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2807          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2808            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2809          $code = 0xFFFD;          $code = 0xFFFD;
2810        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2811          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2812            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2813          $code = 0x000A;          $code = 0x000A;
2814        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2815          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2816            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2817          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2818        }        }
2819                
2820        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2821                  line => $l, column => $c,
2822                 };
2823      } else {      } else {
2824        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2825        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2826        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2827          $self->{next_char} = 0x0023; # #
2828        return undef;        return undef;
2829      }      }
2830    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2831              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2832             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2833              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2834      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2835      !!!next-input-character;      !!!next-input-character;
2836    
2837      my $value = $entity_name;      my $value = $entity_name;
# Line 1751  sub _tokenize_attempt_to_consume_an_enti Line 2839  sub _tokenize_attempt_to_consume_an_enti
2839      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2840      our $EntityChar;      our $EntityChar;
2841    
2842      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2843             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2844             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2845               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2846              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2847               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2848              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2849               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2850              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2851        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2852        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2853          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2854              !!!cp (1020);
2855            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2856            $match = 1;            $match = 1;
2857            !!!next-input-character;            !!!next-input-character;
2858            last;            last;
2859          } else {          } else {
2860              !!!cp (1021);
2861            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2862            $match = -1;            $match = -1;
2863            !!!next-input-character;            !!!next-input-character;
2864          }          }
2865        } else {        } else {
2866          $value .= chr $self->{next_input_character};          !!!cp (1022);
2867            $value .= chr $self->{next_char};
2868          $match *= 2;          $match *= 2;
2869          !!!next-input-character;          !!!next-input-character;
2870        }        }
2871      }      }
2872            
2873      if ($match > 0) {      if ($match > 0) {
2874        return {type => 'character', data => $value};        !!!cp (1023);
2875          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2876                  line => $l, column => $c,
2877                 };
2878      } elsif ($match < 0) {      } elsif ($match < 0) {
2879        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2880        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2881          return {type => 'character', data => '&'.$entity_name};          !!!cp (1024);
2882        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2883          return {type => 'character', data => $value};                  line => $l, column => $c,
2884                   };
2885          } else {
2886            !!!cp (1025);
2887            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2888                    line => $l, column => $c,
2889                   };
2890        }        }
2891      } else {      } else {
2892        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2893        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2894        return {type => 'character', data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
2895          return {type => CHARACTER_TOKEN, data => '&'.$value,
2896                  line => $l, column => $c,
2897                 };
2898      }      }
2899    } else {    } else {
2900        !!!cp (1027);
2901      ## no characters are consumed      ## no characters are consumed
2902      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2903      return undef;      return undef;
2904    }    }
2905  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1833  sub _construct_tree ($) { Line 2937  sub _construct_tree ($) {
2937        
2938    !!!next-token;    !!!next-token;
2939    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
2940    undef $self->{form_element};    undef $self->{form_element};
2941    undef $self->{head_element};    undef $self->{head_element};
2942    $self->{open_elements} = [];    $self->{open_elements} = [];
2943    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2944    
2945      ## NOTE: The "initial" insertion mode.
2946    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2947    
2948      ## NOTE: The "before html" insertion mode.
2949    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2950      $self->{insertion_mode} = BEFORE_HEAD_IM;
2951    
2952      ## NOTE: The "before head" insertion mode and so on.
2953    $self->_tree_construction_main;    $self->_tree_construction_main;
2954  } # _construct_tree  } # _construct_tree
2955    
2956  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2957    my $self = shift;    my $self = shift;
2958    
2959      ## NOTE: "initial" insertion mode
2960    
2961    INITIAL: {    INITIAL: {
2962      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2963        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2964        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2965        ## language.        ## language.
# Line 1857  sub _tree_construction_initial ($) { Line 2969  sub _tree_construction_initial ($) {
2969        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
2970            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2971            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2972          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
2973            !!!parse-error (type => 'not HTML5', token => $token);
2974        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2975            !!!cp ('t2');
2976          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2977          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2978          } else {
2979            !!!cp ('t3');
2980        }        }
2981                
2982        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
2983          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2984          ## NOTE: Default value for both |public_id| and |system_id| attributes
2985          ## are empty strings, so that we don't set any value in missing cases.
2986        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
2987            if defined $token->{public_identifier};            if defined $token->{public_identifier};
2988        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1873  sub _tree_construction_initial ($) { Line 2991  sub _tree_construction_initial ($) {
2991        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
2992        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2993                
2994        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2995            !!!cp ('t4');
2996          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2997        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2998          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 1927  sub _tree_construction_initial ($) { Line 3046  sub _tree_construction_initial ($) {
3046            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
3047            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
3048            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
3049              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
3050              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
3051              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
3052            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
3053            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
3054            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 1949  sub _tree_construction_initial ($) { Line 3071  sub _tree_construction_initial ($) {
3071            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
3072            "HTML" => 1,            "HTML" => 1,
3073          }->{$pubid}) {          }->{$pubid}) {
3074              !!!cp ('t5');
3075            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3076          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
3077                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
3078            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3079                !!!cp ('t6');
3080              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3081            } else {            } else {
3082                !!!cp ('t7');
3083              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3084            }            }
3085          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
3086                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
3087              !!!cp ('t8');
3088            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3089            } else {
3090              !!!cp ('t9');
3091          }          }
3092          } else {
3093            !!!cp ('t10');
3094        }        }
3095        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3096          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3097          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3098          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") {
3099              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
3100            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3101              !!!cp ('t11');
3102            } else {
3103              !!!cp ('t12');
3104          }          }
3105          } else {
3106            !!!cp ('t13');
3107        }        }
3108                
3109        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3110        !!!next-token;        !!!next-token;
3111        return;        return;
3112      } elsif ({      } elsif ({
3113                'start tag' => 1,                START_TAG_TOKEN, 1,
3114                'end tag' => 1,                END_TAG_TOKEN, 1,
3115                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
3116               }->{$token->{type}}) {               }->{$token->{type}}) {
3117        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3118          !!!parse-error (type => 'no DOCTYPE', token => $token);
3119        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3120        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3121        ## reprocess        ## reprocess
3122          !!!ack-later;
3123        return;        return;
3124      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3125        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3126          ## Ignore the token          ## Ignore the token
3127    
3128          unless (length $token->{data}) {          unless (length $token->{data}) {
3129            ## Stay in the phase            !!!cp ('t15');
3130              ## Stay in the insertion mode.
3131            !!!next-token;            !!!next-token;
3132            redo INITIAL;            redo INITIAL;
3133            } else {
3134              !!!cp ('t16');
3135          }          }
3136          } else {
3137            !!!cp ('t17');
3138        }        }
3139    
3140        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3141        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3142        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3143        ## reprocess        ## reprocess
3144        return;        return;
3145      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3146          !!!cp ('t18');
3147        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3148        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3149                
3150        ## Stay in the phase.        ## Stay in the insertion mode.
3151        !!!next-token;        !!!next-token;
3152        redo INITIAL;        redo INITIAL;
3153      } else {      } else {
3154        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
3155      }      }
3156    } # INITIAL    } # INITIAL
3157    
3158      die "$0: _tree_construction_initial: This should be never reached";
3159  } # _tree_construction_initial  } # _tree_construction_initial
3160    
3161  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3162    my $self = shift;    my $self = shift;
3163    
3164      ## NOTE: "before html" insertion mode.
3165        
3166    B: {    B: {
3167        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3168          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3169            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3170          ## Ignore the token          ## Ignore the token
3171          ## Stay in the phase          ## Stay in the insertion mode.
3172          !!!next-token;          !!!next-token;
3173          redo B;          redo B;
3174        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3175            !!!cp ('t20');
3176          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3177          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3178          ## Stay in the phase          ## Stay in the insertion mode.
3179          !!!next-token;          !!!next-token;
3180          redo B;          redo B;
3181        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3182          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3183            ## Ignore the token.            ## Ignore the token.
3184    
3185            unless (length $token->{data}) {            unless (length $token->{data}) {
3186              ## Stay in the phase              !!!cp ('t21');
3187                ## Stay in the insertion mode.
3188              !!!next-token;              !!!next-token;
3189              redo B;              redo B;
3190              } else {
3191                !!!cp ('t22');
3192            }            }
3193            } else {
3194              !!!cp ('t23');
3195          }          }
3196    
3197            $self->{application_cache_selection}->(undef);
3198    
3199          #          #
3200          } elsif ($token->{type} == START_TAG_TOKEN) {
3201            if ($token->{tag_name} eq 'html') {
3202              my $root_element;
3203              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3204              $self->{document}->append_child ($root_element);
3205              push @{$self->{open_elements}},
3206                  [$root_element, $el_category->{html}];
3207    
3208              if ($token->{attributes}->{manifest}) {
3209                !!!cp ('t24');
3210                $self->{application_cache_selection}
3211                    ->($token->{attributes}->{manifest}->{value});
3212                ## ISSUE: Spec is unclear on relative references.
3213                ## According to Hixie (#whatwg 2008-03-19), it should be
3214                ## resolved against the base URI of the document in HTML
3215                ## or xml:base of the element in XHTML.
3216              } else {
3217                !!!cp ('t25');
3218                $self->{application_cache_selection}->(undef);
3219              }
3220    
3221              !!!nack ('t25c');
3222    
3223              !!!next-token;
3224              return; ## Go to the "before head" insertion mode.
3225            } else {
3226              !!!cp ('t25.1');
3227              #
3228            }
3229        } elsif ({        } elsif ({
3230                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3231                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3232                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3233          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3234          #          #
3235        } else {        } else {
3236          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3237        }        }
3238        my $root_element; !!!create-element ($root_element, 'html');  
3239        $self->{document}->append_child ($root_element);      my $root_element;
3240        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3241        ## reprocess      $self->{document}->append_child ($root_element);
3242        #redo B;      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3243        return; ## Go to the main phase.  
3244        $self->{application_cache_selection}->(undef);
3245    
3246        ## NOTE: Reprocess the token.
3247        !!!ack-later;
3248        return; ## Go to the "before head" insertion mode.
3249    
3250        ## ISSUE: There is an issue in the spec
3251    } # B    } # B
3252    
3253      die "$0: _tree_construction_root_element: This should never be reached";
3254  } # _tree_construction_root_element  } # _tree_construction_root_element
3255    
3256  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2070  sub _reset_insertion_mode ($) { Line 3265  sub _reset_insertion_mode ($) {
3265            
3266      ## Step 3      ## Step 3
3267      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"!?  
3268        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3269          $last = 1;          $last = 1;
3270          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3271            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
3272                $self->{inner_html_node}->[1] eq 'th') {              !!!cp ('t27');
3273              #              #
3274            } else {            } else {
3275                !!!cp ('t28');
3276              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
3277            }            }
3278          }          }
3279        }        }
3280            
3281        ## Step 4..13      ## Step 4..14
3282        my $new_mode = {      my $new_mode;
3283        if ($node->[1] & FOREIGN_EL) {
3284          ## NOTE: Strictly spaking, the line below only applies to MathML and
3285          ## SVG elements.  Currently the HTML syntax supports only MathML and
3286          ## SVG elements as foreigners.
3287          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3288          ## ISSUE: What is set as the secondary insertion mode?
3289        } else {
3290          $new_mode = {
3291                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3292                          ## NOTE: |option| and |optgroup| do not set
3293                          ## insertion mode to "in select" by themselves.
3294                        td => IN_CELL_IM,                        td => IN_CELL_IM,
3295                        th => IN_CELL_IM,                        th => IN_CELL_IM,
3296                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2102  sub _reset_insertion_mode ($) { Line 3303  sub _reset_insertion_mode ($) {
3303                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3304                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3305                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3306                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3307        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3308        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3309                
3310        ## Step 14        ## Step 15
3311        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3312          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3313              !!!cp ('t29');
3314            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3315          } else {          } else {
3316              ## ISSUE: Can this state be reached?
3317              !!!cp ('t30');
3318            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3319          }          }
3320          return;          return;
3321          } else {
3322            !!!cp ('t31');
3323        }        }
3324                
3325        ## Step 15        ## Step 16
3326        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3327                
3328        ## Step 16        ## Step 17
3329        $i--;        $i--;
3330        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3331                
3332        ## Step 17        ## Step 18
3333        redo S3;        redo S3;
3334      } # S3      } # S3
3335    
3336      die "$0: _reset_insertion_mode: This line should never be reached";
3337  } # _reset_insertion_mode  } # _reset_insertion_mode
3338    
3339  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2146  sub _tree_construction_main ($) { Line 3355  sub _tree_construction_main ($) {
3355      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3356      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3357        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3358            !!!cp ('t32');
3359          return;          return;
3360        }        }
3361      }      }
# Line 2160  sub _tree_construction_main ($) { Line 3370  sub _tree_construction_main ($) {
3370    
3371        ## Step 6        ## Step 6
3372        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3373            !!!cp ('t33_1');
3374          #          #
3375        } else {        } else {
3376          my $in_open_elements;          my $in_open_elements;
3377          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3378            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3379                !!!cp ('t33');
3380              $in_open_elements = 1;              $in_open_elements = 1;
3381              last OE;              last OE;
3382            }            }
3383          }          }
3384          if ($in_open_elements) {          if ($in_open_elements) {
3385              !!!cp ('t34');
3386            #            #
3387          } else {          } else {
3388              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3389              !!!cp ('t35');
3390            redo S4;            redo S4;
3391          }          }
3392        }        }
# Line 2194  sub _tree_construction_main ($) { Line 3409  sub _tree_construction_main ($) {
3409    
3410        ## Step 11        ## Step 11
3411        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3412            !!!cp ('t36');
3413          ## Step 7'          ## Step 7'
3414          $i++;          $i++;
3415          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3416                    
3417          redo S7;          redo S7;
3418        }        }
3419    
3420          !!!cp ('t37');
3421      } # S7      } # S7
3422    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3423    
3424    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3425      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3426        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3427            !!!cp ('t38');
3428          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3429          return;          return;
3430        }        }
3431      }      }
3432    
3433        !!!cp ('t39');
3434    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3435    
3436    my $parse_rcdata = sub ($$) {    my $insert;
3437      my ($content_model_flag, $insert) = @_;  
3438      my $parse_rcdata = sub ($) {
3439        my ($content_model_flag) = @_;
3440    
3441      ## Step 1      ## Step 1
3442      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3443      my $el;      my $el;
3444      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3445    
3446      ## Step 2      ## Step 2
3447      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3448    
3449      ## Step 3      ## Step 3
3450      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2229  sub _tree_construction_main ($) { Line 3452  sub _tree_construction_main ($) {
3452    
3453      ## Step 4      ## Step 4
3454      my $text = '';      my $text = '';
3455        !!!nack ('t40.1');
3456      !!!next-token;      !!!next-token;
3457      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3458          !!!cp ('t40');
3459        $text .= $token->{data};        $text .= $token->{data};
3460        !!!next-token;        !!!next-token;
3461      }      }
3462    
3463      ## Step 5      ## Step 5
3464      if (length $text) {      if (length $text) {
3465          !!!cp ('t41');
3466        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3467        $el->append_child ($text);        $el->append_child ($text);
3468      }      }
# Line 2245  sub _tree_construction_main ($) { Line 3471  sub _tree_construction_main ($) {
3471      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3472    
3473      ## Step 7      ## Step 7
3474      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3475            $token->{tag_name} eq $start_tag_name) {
3476          !!!cp ('t42');
3477        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3478      } else {      } else {
3479        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3480          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3481            !!!cp ('t43');
3482            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3483          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3484            !!!cp ('t44');
3485            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3486          } else {
3487            die "$0: $content_model_flag in parse_rcdata";
3488          }
3489      }      }
3490      !!!next-token;      !!!next-token;
3491    }; # $parse_rcdata    }; # $parse_rcdata
3492    
3493    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3494      my $script_el;      my $script_el;
3495      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3496      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3497    
3498      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3499      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3500            
3501      my $text = '';      my $text = '';
3502        !!!nack ('t45.1');
3503      !!!next-token;      !!!next-token;
3504      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3505          !!!cp ('t45');
3506        $text .= $token->{data};        $text .= $token->{data};
3507        !!!next-token;        !!!next-token;
3508      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3509      if (length $text) {      if (length $text) {
3510          !!!cp ('t46');
3511        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3512      }      }
3513                                
3514      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3515    
3516      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3517          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3518          !!!cp ('t47');
3519        ## Ignore the token        ## Ignore the token
3520      } else {      } else {
3521        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3522          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3523        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3524        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3525      }      }
3526            
3527      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3528          !!!cp ('t49');
3529        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3530      } else {      } else {
3531          !!!cp ('t50');
3532        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3533        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3534    
# Line 2303  sub _tree_construction_main ($) { Line 3542  sub _tree_construction_main ($) {
3542      !!!next-token;      !!!next-token;
3543    }; # $script_start_tag    }; # $script_start_tag
3544    
3545      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3546      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3547      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3548    
3549    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3550      my $tag_name = shift;      my $end_tag_token = shift;
3551        my $tag_name = $end_tag_token->{tag_name};
3552    
3553        ## NOTE: The adoption agency algorithm (AAA).
3554    
3555      FET: {      FET: {
3556        ## Step 1        ## Step 1
3557        my $formatting_element;        my $formatting_element;
3558        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3559        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3560          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3561              !!!cp ('t52');
3562              last AFE;
3563            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3564                         eq $tag_name) {
3565              !!!cp ('t51');
3566            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3567            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3568            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3569          }          }
3570        } # AFE        } # AFE
3571        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3572          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3573            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3574          ## Ignore the token          ## Ignore the token
3575          !!!next-token;          !!!next-token;
3576          return;          return;
# Line 2332  sub _tree_construction_main ($) { Line 3582  sub _tree_construction_main ($) {
3582          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3583          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3584            if ($in_scope) {            if ($in_scope) {
3585                !!!cp ('t54');
3586              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3587              last INSCOPE;              last INSCOPE;
3588            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3589              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3590                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3591                                token => $end_tag_token);
3592              ## Ignore the token              ## Ignore the token
3593              !!!next-token;              !!!next-token;
3594              return;              return;
3595            }            }
3596          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3597                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3598            $in_scope = 0;            $in_scope = 0;
3599          }          }
3600        } # INSCOPE        } # INSCOPE
3601        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3602          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3603            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3604                            token => $end_tag_token);
3605          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3606          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3607          return;          return;
3608        }        }
3609        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3610          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3611            !!!parse-error (type => 'not closed',
3612                            value => $self->{open_elements}->[-1]->[0]
3613                                ->manakai_local_name,
3614                            token => $end_tag_token);
3615        }        }
3616                
3617        ## Step 2        ## Step 2
# Line 2362  sub _tree_construction_main ($) { Line 3619  sub _tree_construction_main ($) {
3619        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3620        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3621          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3622          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3623              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3624              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3625               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3626              !!!cp ('t59');
3627            $furthest_block = $node;            $furthest_block = $node;
3628            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3629          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3630              !!!cp ('t60');
3631            last OE;            last OE;
3632          }          }
3633        } # OE        } # OE
3634                
3635        ## Step 3        ## Step 3
3636        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3637            !!!cp ('t61');
3638          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3639          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3640          !!!next-token;          !!!next-token;
# Line 2387  sub _tree_construction_main ($) { Line 3647  sub _tree_construction_main ($) {
3647        ## Step 5        ## Step 5
3648        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3649        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3650            !!!cp ('t62');
3651          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3652        }        }
3653                
# Line 2409  sub _tree_construction_main ($) { Line 3670  sub _tree_construction_main ($) {
3670          S7S2: {          S7S2: {
3671            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3672              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3673                  !!!cp ('t63');
3674                $node_i_in_active = $_;                $node_i_in_active = $_;
3675                last S7S2;                last S7S2;
3676              }              }
# Line 2422  sub _tree_construction_main ($) { Line 3684  sub _tree_construction_main ($) {
3684                    
3685          ## Step 4          ## Step 4
3686          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3687              !!!cp ('t64');
3688            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3689          }          }
3690                    
3691          ## Step 5          ## Step 5
3692          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3693              !!!cp ('t65');
3694            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3695            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3696            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2444  sub _tree_construction_main ($) { Line 3708  sub _tree_construction_main ($) {
3708        } # S7          } # S7  
3709                
3710        ## Step 8        ## Step 8
3711        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3712            my $foster_parent_element;
3713            my $next_sibling;
3714            OE: for (reverse 0..$#{$self->{open_elements}}) {
3715              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3716                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3717                                 if (defined $parent and $parent->node_type == 1) {
3718                                   !!!cp ('t65.1');
3719                                   $foster_parent_element = $parent;
3720                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3721                                 } else {
3722                                   !!!cp ('t65.2');
3723                                   $foster_parent_element
3724                                     = $self->{open_elements}->[$_ - 1]->[0];
3725                                 }
3726                                 last OE;
3727                               }
3728                             } # OE
3729                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3730                               unless defined $foster_parent_element;
3731            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3732            $open_tables->[-1]->[1] = 1; # tainted
3733          } else {
3734            !!!cp ('t65.3');
3735            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3736          }
3737                
3738        ## Step 9        ## Step 9
3739        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2461  sub _tree_construction_main ($) { Line 3750  sub _tree_construction_main ($) {
3750        my $i;        my $i;
3751        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3752          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3753              !!!cp ('t66');
3754            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3755            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3756          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3757              !!!cp ('t67');
3758            $i = $_;            $i = $_;
3759          }          }
3760        } # AFE        } # AFE
# Line 2473  sub _tree_construction_main ($) { Line 3764  sub _tree_construction_main ($) {
3764        undef $i;        undef $i;
3765        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3766          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3767              !!!cp ('t68');
3768            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3769            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3770          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3771              !!!cp ('t69');
3772            $i = $_;            $i = $_;
3773          }          }
3774        } # OE        } # OE
# Line 2486  sub _tree_construction_main ($) { Line 3779  sub _tree_construction_main ($) {
3779      } # FET      } # FET
3780    }; # $formatting_end_tag    }; # $formatting_end_tag
3781    
3782    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3783      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3784    }; # $insert_to_current    }; # $insert_to_current
3785    
3786    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3787                         my $child = shift;      my $child = shift;
3788                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3789                              table => 1, tbody => 1, tfoot => 1,        # MUST
3790                              thead => 1, tr => 1,        my $foster_parent_element;
3791                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3792                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3793                           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') {  
3794                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3795                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3796                                   !!!cp ('t70');
3797                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3798                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3799                               } else {                               } else {
3800                                   !!!cp ('t71');
3801                                 $foster_parent_element                                 $foster_parent_element
3802                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3803                               }                               }
# Line 2516  sub _tree_construction_main ($) { Line 3808  sub _tree_construction_main ($) {
3808                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3809                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3810                             ($child, $next_sibling);                             ($child, $next_sibling);
3811                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3812                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3813                         }        !!!cp ('t72');
3814          $self->{open_elements}->[-1]->[0]->append_child ($child);
3815        }
3816    }; # $insert_to_foster    }; # $insert_to_foster
3817    
3818    my $insert;    B: while (1) {
3819        if ($token->{type} == DOCTYPE_TOKEN) {
3820    B: {        !!!cp ('t73');
3821      if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
       !!!parse-error (type => 'DOCTYPE in the middle');  
3822        ## Ignore the token        ## Ignore the token
3823        ## Stay in the phase        ## Stay in the phase
3824        !!!next-token;        !!!next-token;
3825        redo B;        next B;
3826      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == START_TAG_TOKEN and
       if ($self->{insertion_mode} == AFTER_HTML_BODY_IM or  
           $self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
     } elsif ($token->{type} eq 'start tag' and  
3827               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3828        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3829          ## Turn into the main phase          !!!cp ('t79');
3830          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3831          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3832        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3833          ## Turn into the main phase          !!!cp ('t80');
3834          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3835          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3836          } else {
3837            !!!cp ('t81');
3838        }        }
3839    
3840  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3841  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag', token => $token);
       unless ($token->{first_start_tag}) {  
         !!!parse-error (type => 'not first start tag');  
       }  
3842        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3843        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3844          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3845              !!!cp ('t84');
3846            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3847              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3848               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3849          }          }
3850        }        }
3851          !!!nack ('t84.1');
3852        !!!next-token;        !!!next-token;
3853        redo B;        next B;
3854      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3855        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3856        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM or        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3857            $self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          !!!cp ('t85');
3858          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3859        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3860            !!!cp ('t86');
3861          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3862        } else {        } else {
3863            !!!cp ('t87');
3864          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3865        }        }
3866        !!!next-token;        !!!next-token;
3867        redo B;        next B;
3868      } elsif ($self->{insertion_mode} == IN_HEAD_IM or      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3869               $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM or        if ($token->{type} == CHARACTER_TOKEN) {
3870               $self->{insertion_mode} == AFTER_HEAD_IM or          !!!cp ('t87.1');
3871               $self->{insertion_mode} == BEFORE_HEAD_IM) {          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3872        if ($token->{type} eq 'character') {          !!!next-token;
3873            next B;
3874          } elsif ($token->{type} == START_TAG_TOKEN) {
3875            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3876                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3877                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3878                ($token->{tag_name} eq 'svg' and
3879                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3880              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3881              !!!cp ('t87.2');
3882              #
3883            } elsif ({
3884                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3885                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3886                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3887                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3888                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3889                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3890                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3891                      var => 1,
3892                     }->{$token->{tag_name}}) {
3893              !!!cp ('t87.2');
3894              !!!parse-error (type => 'not closed',
3895                              value => $self->{open_elements}->[-1]->[0]
3896                                  ->manakai_local_name,
3897                              token => $token);
3898    
3899              pop @{$self->{open_elements}}
3900                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3901    
3902              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3903              ## Reprocess.
3904              next B;
3905            } else {
3906              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3907              my $tag_name = $token->{tag_name};
3908              if ($nsuri eq $SVG_NS) {
3909                $tag_name = {
3910                   altglyph => 'altGlyph',
3911                   altglyphdef => 'altGlyphDef',
3912                   altglyphitem => 'altGlyphItem',
3913                   animatecolor => 'animateColor',
3914                   animatemotion => 'animateMotion',
3915                   animatetransform => 'animateTransform',
3916                   clippath => 'clipPath',
3917                   feblend => 'feBlend',
3918                   fecolormatrix => 'feColorMatrix',
3919                   fecomponenttransfer => 'feComponentTransfer',
3920                   fecomposite => 'feComposite',
3921                   feconvolvematrix => 'feConvolveMatrix',
3922                   fediffuselighting => 'feDiffuseLighting',
3923                   fedisplacementmap => 'feDisplacementMap',
3924                   fedistantlight => 'feDistantLight',
3925                   feflood => 'feFlood',
3926                   fefunca => 'feFuncA',
3927                   fefuncb => 'feFuncB',
3928                   fefuncg => 'feFuncG',
3929                   fefuncr => 'feFuncR',
3930                   fegaussianblur => 'feGaussianBlur',
3931                   feimage => 'feImage',
3932                   femerge => 'feMerge',
3933                   femergenode => 'feMergeNode',
3934                   femorphology => 'feMorphology',
3935                   feoffset => 'feOffset',
3936                   fepointlight => 'fePointLight',
3937                   fespecularlighting => 'feSpecularLighting',
3938                   fespotlight => 'feSpotLight',
3939                   fetile => 'feTile',
3940                   feturbulence => 'feTurbulence',
3941                   foreignobject => 'foreignObject',
3942                   glyphref => 'glyphRef',
3943                   lineargradient => 'linearGradient',
3944                   radialgradient => 'radialGradient',
3945                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
3946                   textpath => 'textPath',  
3947                }->{$tag_name} || $tag_name;
3948              }
3949    
3950              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
3951    
3952              ## "adjust foreign attributes" - done in insert-element-f
3953    
3954              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
3955    
3956              if ($self->{self_closing}) {
3957                pop @{$self->{open_elements}};
3958                !!!ack ('t87.3');
3959              } else {
3960                !!!cp ('t87.4');
3961              }
3962    
3963              !!!next-token;
3964              next B;
3965            }
3966          } elsif ($token->{type} == END_TAG_TOKEN) {
3967            ## NOTE: "using the rules for secondary insertion mode" then "continue"
3968            !!!cp ('t87.5');
3969            #
3970          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3971            ## NOTE: "using the rules for secondary insertion mode" then "continue"
3972            !!!cp ('t87.6');
3973            #
3974            ## TODO: ...
3975          } else {
3976            die "$0: $token->{type}: Unknown token type";        
3977          }
3978        }
3979    
3980        if ($self->{insertion_mode} & HEAD_IMS) {
3981          if ($token->{type} == CHARACTER_TOKEN) {
3982          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3983            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3984                !!!cp ('t88.2');
3985                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3986              } else {
3987                !!!cp ('t88.1');
3988                ## Ignore the token.
3989                !!!next-token;
3990                next B;
3991              }
3992            unless (length $token->{data}) {            unless (length $token->{data}) {
3993                !!!cp ('t88');
3994              !!!next-token;              !!!next-token;
3995              redo B;              next B;
3996            }            }
3997          }          }
3998    
3999          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4000              !!!cp ('t89');
4001            ## As if <head>            ## As if <head>
4002            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4003            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4004            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4005                  [$self->{head_element}, $el_category->{head}];
4006    
4007            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4008            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4009    
4010            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4011          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4012              !!!cp ('t90');
4013            ## As if </noscript>            ## As if </noscript>
4014            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4015            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4016                        
4017            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4018            ## As if </head>            ## As if </head>
# Line 2632  sub _tree_construction_main ($) { Line 4020  sub _tree_construction_main ($) {
4020    
4021            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4022          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4023              !!!cp ('t91');
4024            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4025    
4026            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4027            } else {
4028              !!!cp ('t92');
4029          }          }
4030    
4031              ## "after head" insertion mode          ## "after head" insertion mode
4032              ## As if <body>          ## As if <body>
4033              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4034              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4035              ## reprocess          ## reprocess
4036              redo B;          next B;
4037            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4038              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4039                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4040                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4041                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4042                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4043                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4044                  !!!next-token;              push @{$self->{open_elements}},
4045                  redo B;                  [$self->{head_element}, $el_category->{head}];
4046                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4047                  #              !!!nack ('t93.1');
4048                } else {              !!!next-token;
4049                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4050                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4051                  !!!next-token;              !!!cp ('t94');
4052                  redo B;              #
4053                }            } else {
4054              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!cp ('t95');
4055                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4056                !!!create-element ($self->{head_element}, 'head');              ## Ignore the token
4057                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
4058                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4059                next B;
4060              }
4061            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4062              !!!cp ('t96');
4063              ## As if <head>
4064              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4065              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4066              push @{$self->{open_elements}},
4067                  [$self->{head_element}, $el_category->{head}];
4068    
4069                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4070                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4071              }          } else {
4072              !!!cp ('t97');
4073            }
4074    
4075              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4076                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4077                    !!!cp ('t98');
4078                  ## As if </noscript>                  ## As if </noscript>
4079                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4080                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4081                                
4082                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4083                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4084                  } else {
4085                    !!!cp ('t99');
4086                }                }
4087    
4088                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4089                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4090                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4091                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4092                    push @{$self->{open_elements}},
4093                        [$self->{head_element}, $el_category->{head}];
4094                  } else {
4095                    !!!cp ('t101');
4096                }                }
4097                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4098                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4099                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4100                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4101                  !!!nack ('t101.1');
4102                !!!next-token;                !!!next-token;
4103                redo B;                next B;
4104              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4105                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4106                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4107                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4108                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4109                    push @{$self->{open_elements}},
4110                        [$self->{head_element}, $el_category->{head}];
4111                  } else {
4112                    !!!cp ('t103');
4113                }                }
4114                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4115                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4116                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4117                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4118                  !!!ack ('t103.1');
4119                !!!next-token;                !!!next-token;
4120                redo B;                next B;
4121              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4122                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4123                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4124                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4125                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4126                    push @{$self->{open_elements}},
4127                        [$self->{head_element}, $el_category->{head}];
4128                  } else {
4129                    !!!cp ('t105');
4130                }                }
4131                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4132                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4133    
4134                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
4135                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
4136                    $charset = $token->{attributes}->{charset}->{value};                    !!!cp ('t106');
4137                  }                    $self->{change_encoding}
4138                  if ($token->{attributes}->{'http-equiv'}) {                        ->($self, $token->{attributes}->{charset}->{value},
4139                             $token);
4140                      
4141                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4142                          ->set_user_data (manakai_has_reference =>
4143                                               $token->{attributes}->{charset}
4144                                                   ->{has_reference});
4145                    } elsif ($token->{attributes}->{content}) {
4146                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4147                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
4148                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4149                              [\x09-\x0D\x20]*=
4150                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4151                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4152                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
4153                    } ## TODO: And if supported                      $self->{change_encoding}
4154                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4155                               $token);
4156                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4157                            ->set_user_data (manakai_has_reference =>
4158                                                 $token->{attributes}->{content}
4159                                                       ->{has_reference});
4160                      } else {
4161                        !!!cp ('t108');
4162                      }
4163                    }
4164                  } else {
4165                    if ($token->{attributes}->{charset}) {
4166                      !!!cp ('t109');
4167                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4168                          ->set_user_data (manakai_has_reference =>
4169                                               $token->{attributes}->{charset}
4170                                                   ->{has_reference});
4171                    }
4172                    if ($token->{attributes}->{content}) {
4173                      !!!cp ('t110');
4174                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4175                          ->set_user_data (manakai_has_reference =>
4176                                               $token->{attributes}->{content}
4177                                                   ->{has_reference});
4178                  }                  }
                 ## TODO: Change the encoding  
4179                }                }
4180    
4181                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4182                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4183                  !!!ack ('t110.1');
4184                !!!next-token;                !!!next-token;
4185                redo B;                next B;
4186              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4187                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4188                    !!!cp ('t111');
4189                  ## As if </noscript>                  ## As if </noscript>
4190                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4191                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4192                                
4193                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4194                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4195                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4196                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4197                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4198                    push @{$self->{open_elements}},
4199                        [$self->{head_element}, $el_category->{head}];
4200                  } else {
4201                    !!!cp ('t113');
4202                }                }
4203    
4204                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4205                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4206                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4207                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4208                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4209                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4210                redo B;                next B;
4211              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4212                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4213                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4214                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4215                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4216                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4217                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4218                    push @{$self->{open_elements}},
4219                        [$self->{head_element}, $el_category->{head}];
4220                  } else {
4221                    !!!cp ('t115');
4222                }                }
4223                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4224                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4225                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4226                redo B;                next B;
4227              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4228                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4229                    !!!cp ('t116');
4230                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4231                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4232                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4233                    !!!nack ('t116.1');
4234                  !!!next-token;                  !!!next-token;
4235                  redo B;                  next B;
4236                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4237                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4238                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4239                  ## Ignore the token                  ## Ignore the token
4240                    !!!nack ('t117.1');
4241                  !!!next-token;                  !!!next-token;
4242                  redo B;                  next B;
4243                } else {                } else {
4244                    !!!cp ('t118');
4245                  #                  #
4246                }                }
4247              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4248                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4249                    !!!cp ('t119');
4250                  ## As if </noscript>                  ## As if </noscript>
4251                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4252                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4253                                
4254                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4255                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4256                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4257                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4258                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4259                    push @{$self->{open_elements}},
4260                        [$self->{head_element}, $el_category->{head}];
4261                  } else {
4262                    !!!cp ('t121');
4263                }                }
4264    
4265                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4266                $script_start_tag->($insert_to_current);                $script_start_tag->();
4267                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4268                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4269                redo B;                next B;
4270              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4271                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4272                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4273                    !!!cp ('t122');
4274                  ## As if </noscript>                  ## As if </noscript>
4275                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4276                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4277                                    
4278                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4279                  ## As if </head>                  ## As if </head>
# Line 2813  sub _tree_construction_main ($) { Line 4281  sub _tree_construction_main ($) {
4281                                    
4282                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4283                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4284                    !!!cp ('t124');
4285                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4286                                    
4287                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4288                  } else {
4289                    !!!cp ('t125');
4290                }                }
4291    
4292                ## "after head" insertion mode                ## "after head" insertion mode
4293                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4294                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4295                    !!!cp ('t126');
4296                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4297                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4298                    !!!cp ('t127');
4299                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4300                } else {                } else {
4301                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4302                }                }
4303                  !!!nack ('t127.1');
4304                !!!next-token;                !!!next-token;
4305                redo B;                next B;
4306              } else {              } else {
4307                  !!!cp ('t128');
4308                #                #
4309              }              }
4310    
4311              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4312                  !!!cp ('t129');
4313                ## As if </noscript>                ## As if </noscript>
4314                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4315                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4316                                
4317                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4318                ## As if </head>                ## As if </head>
# Line 2844  sub _tree_construction_main ($) { Line 4320  sub _tree_construction_main ($) {
4320    
4321                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4322              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4323                  !!!cp ('t130');
4324                ## As if </head>                ## As if </head>
4325                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4326    
4327                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4328                } else {
4329                  !!!cp ('t131');
4330              }              }
4331    
4332              ## "after head" insertion mode              ## "after head" insertion mode
4333              ## As if <body>              ## As if <body>
4334              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4335              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4336              ## reprocess              ## reprocess
4337              redo B;              !!!ack-later;
4338            } elsif ($token->{type} eq 'end tag') {              next B;
4339              } elsif ($token->{type} == END_TAG_TOKEN) {
4340              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4341                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4342                    !!!cp ('t132');
4343                  ## As if <head>                  ## As if <head>
4344                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4345                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4346                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4347                        [$self->{head_element}, $el_category->{head}];
4348    
4349                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4350                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4351                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4352                  !!!next-token;                  !!!next-token;
4353                  redo B;                  next B;
4354                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4355                    !!!cp ('t133');
4356                  ## As if </noscript>                  ## As if </noscript>
4357                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4358                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4359                                    
4360                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4361                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4362                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4363                  !!!next-token;                  !!!next-token;
4364                  redo B;                  next B;
4365                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4366                    !!!cp ('t134');
4367                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4368                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4369                  !!!next-token;                  !!!next-token;
4370                  redo B;                  next B;
4371                } else {                } else {
4372                    !!!cp ('t135');
4373                  #                  #
4374                }                }
4375              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4376                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4377                    !!!cp ('t136');
4378                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4379                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4380                  !!!next-token;                  !!!next-token;
4381                  redo B;                  next B;
4382                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4383                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!cp ('t137');
4384                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4385                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4386                  !!!next-token;                  !!!next-token;
4387                  redo B;                  next B;
4388                } else {                } else {
4389                    !!!cp ('t138');
4390                  #                  #
4391                }                }
4392              } elsif ({              } elsif ({
4393                        body => 1, html => 1,                        body => 1, html => 1,
4394                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4395                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4396                    !!!cp ('t139');
4397                  ## As if <head>                  ## As if <head>
4398                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4399                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4400                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4401                        [$self->{head_element}, $el_category->{head}];
4402    
4403                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4404                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4405                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4406                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t140');
4407                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4408                  ## Ignore the token                  ## Ignore the token
4409                  !!!next-token;                  !!!next-token;
4410                  redo B;                  next B;
4411                  } else {
4412                    !!!cp ('t141');
4413                }                }
4414                                
4415                #                #
# Line 2924  sub _tree_construction_main ($) { Line 4417  sub _tree_construction_main ($) {
4417                        p => 1, br => 1,                        p => 1, br => 1,
4418                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4419                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4420                    !!!cp ('t142');
4421                  ## As if <head>                  ## As if <head>
4422                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4423                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4424                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4425                        [$self->{head_element}, $el_category->{head}];
4426    
4427                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4428                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4429                  } else {
4430                    !!!cp ('t143');
4431                }                }
4432    
4433                #                #
4434              } else {              } else {
4435                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4436                    !!!cp ('t144');
4437                  #                  #
4438                } else {                } else {
4439                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t145');
4440                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4441                  ## Ignore the token                  ## Ignore the token
4442                  !!!next-token;                  !!!next-token;
4443                  redo B;                  next B;
4444                }                }
4445              }              }
4446    
4447              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4448                  !!!cp ('t146');
4449                ## As if </noscript>                ## As if </noscript>
4450                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4451                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4452                                
4453                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4454                ## As if </head>                ## As if </head>
# Line 2956  sub _tree_construction_main ($) { Line 4456  sub _tree_construction_main ($) {
4456    
4457                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4458              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4459                  !!!cp ('t147');
4460                ## As if </head>                ## As if </head>
4461                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4462    
4463                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4464              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4465                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4466                  !!!cp ('t148');
4467                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4468                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4469                !!!next-token;                !!!next-token;
4470                redo B;                next B;
4471                } else {
4472                  !!!cp ('t149');
4473              }              }
4474    
4475              ## "after head" insertion mode              ## "after head" insertion mode
4476              ## As if <body>              ## As if <body>
4477              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4478              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4479              ## reprocess              ## reprocess
4480              redo B;              next B;
4481            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4482              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4483            }            !!!cp ('t149.1');
4484    
4485              ## NOTE: As if <head>
4486              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4487              $self->{open_elements}->[-1]->[0]->append_child
4488                  ($self->{head_element});
4489              #push @{$self->{open_elements}},
4490              #    [$self->{head_element}, $el_category->{head}];
4491              #$self->{insertion_mode} = IN_HEAD_IM;
4492              ## NOTE: Reprocess.
4493    
4494              ## NOTE: As if </head>
4495              #pop @{$self->{open_elements}};
4496              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4497              ## NOTE: Reprocess.
4498              
4499              #
4500            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4501              !!!cp ('t149.2');
4502    
4503              ## NOTE: As if </head>
4504              pop @{$self->{open_elements}};
4505              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4506              ## NOTE: Reprocess.
4507    
4508              #
4509            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4510              !!!cp ('t149.3');
4511    
4512              !!!parse-error (type => 'in noscript:#eof', token => $token);
4513    
4514              ## As if </noscript>
4515              pop @{$self->{open_elements}};
4516              #$self->{insertion_mode} = IN_HEAD_IM;
4517              ## NOTE: Reprocess.
4518    
4519              ## NOTE: As if </head>
4520              pop @{$self->{open_elements}};
4521              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4522              ## NOTE: Reprocess.
4523    
4524              #
4525            } else {
4526              !!!cp ('t149.4');
4527              #
4528            }
4529    
4530            ## NOTE: As if <body>
4531            !!!insert-element ('body',, $token);
4532            $self->{insertion_mode} = IN_BODY_IM;
4533            ## NOTE: Reprocess.
4534            next B;
4535          } else {
4536            die "$0: $token->{type}: Unknown token type";
4537          }
4538    
4539            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4540      } elsif ($self->{insertion_mode} == IN_BODY_IM or      } elsif ($self->{insertion_mode} & BODY_IMS) {
4541               $self->{insertion_mode} == IN_CELL_IM or            if ($token->{type} == CHARACTER_TOKEN) {
4542               $self->{insertion_mode} == IN_CAPTION_IM) {              !!!cp ('t150');
           if ($token->{type} eq 'character') {  
4543              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4544              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4545                            
4546              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4547    
4548              !!!next-token;              !!!next-token;
4549              redo B;              next B;
4550            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
4551              if ({              if ({
4552                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4553                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4554                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4555                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4556                  ## have an element in table scope                  ## have an element in table scope
4557                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4558                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4559                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4560                      $tn = $node->[1];                      !!!cp ('t151');
4561                      last INSCOPE;  
4562                    } elsif ({                      ## Close the cell
4563                              table => 1, html => 1,                      !!!back-token; # <x>
4564                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4565                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4566                    }                                line => $token->{line},
4567                  } # INSCOPE                                column => $token->{column}};
4568                    unless (defined $tn) {                      next B;
4569                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4570                      ## Ignore the token                      !!!cp ('t152');
4571                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4572                      redo B;                      last;
4573                    }                    }
4574                                    }
4575                  ## Close the cell  
4576                  !!!back-token; # <?>                  !!!cp ('t153');
4577                  $token = {type => 'end tag', tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
4578                  redo B;                      value => $token->{tag_name}, token => $token);
4579                    ## Ignore the token
4580                    !!!nack ('t153.1');
4581                    !!!next-token;
4582                    next B;
4583                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4584                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4585                                    
4586                  ## As if </caption>                  ## NOTE: As if </caption>.
4587                  ## have a table element in table scope                  ## have a table element in table scope
4588                  my $i;                  my $i;
4589                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4590                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4591                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4592                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4593                      last INSCOPE;                        !!!cp ('t155');
4594                    } elsif ({                        $i = $_;
4595                              table => 1, html => 1,                        last INSCOPE;
4596                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4597                      last INSCOPE;                        !!!cp ('t156');
4598                          last;
4599                        }
4600                    }                    }
4601    
4602                      !!!cp ('t157');
4603                      !!!parse-error (type => 'start tag not allowed',
4604                                      value => $token->{tag_name}, token => $token);
4605                      ## Ignore the token
4606                      !!!nack ('t157.1');
4607                      !!!next-token;
4608                      next B;
4609                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4610                                    
4611                  ## generate implied end tags                  ## generate implied end tags
4612                  if ({                  while ($self->{open_elements}->[-1]->[1]
4613                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4614                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4615                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token; # <?>  
                   $token = {type => 'end tag', tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4616                  }                  }
4617    
4618                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4619                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4620                      !!!parse-error (type => 'not closed',
4621                                      value => $self->{open_elements}->[-1]->[0]
4622                                          ->manakai_local_name,
4623                                      token => $token);
4624                    } else {
4625                      !!!cp ('t160');
4626                  }                  }
4627                                    
4628                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3068  sub _tree_construction_main ($) { Line 4632  sub _tree_construction_main ($) {
4632                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4633                                    
4634                  ## reprocess                  ## reprocess
4635                  redo B;                  !!!ack-later;
4636                    next B;
4637                } else {                } else {
4638                    !!!cp ('t161');
4639                  #                  #
4640                }                }
4641              } else {              } else {
4642                  !!!cp ('t162');
4643                #                #
4644              }              }
4645            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4646              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4647                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4648                  ## have an element in table scope                  ## have an element in table scope
4649                  my $i;                  my $i;
4650                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4651                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4652                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4653                        !!!cp ('t163');
4654                      $i = $_;                      $i = $_;
4655                      last INSCOPE;                      last INSCOPE;
4656                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4657                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4658                      last INSCOPE;                      last INSCOPE;
4659                    }                    }
4660                  } # INSCOPE                  } # INSCOPE
4661                    unless (defined $i) {                    unless (defined $i) {
4662                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4663                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4664                      ## Ignore the token                      ## Ignore the token
4665                      !!!next-token;                      !!!next-token;
4666                      redo B;                      next B;
4667                    }                    }
4668                                    
4669                  ## generate implied end tags                  ## generate implied end tags
4670                  if ({                  while ($self->{open_elements}->[-1]->[1]
4671                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4672                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4673                       th => ($token->{tag_name} eq 'td'),                    pop @{$self->{open_elements}};
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4674                  }                  }
4675                    
4676                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4677                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4678                      !!!cp ('t167');
4679                      !!!parse-error (type => 'not closed',
4680                                      value => $self->{open_elements}->[-1]->[0]
4681                                          ->manakai_local_name,
4682                                      token => $token);
4683                    } else {
4684                      !!!cp ('t168');
4685                  }                  }
4686                                    
4687                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3123  sub _tree_construction_main ($) { Line 4691  sub _tree_construction_main ($) {
4691                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4692                                    
4693                  !!!next-token;                  !!!next-token;
4694                  redo B;                  next B;
4695                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4696                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4697                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4698                  ## Ignore the token                  ## Ignore the token
4699                  !!!next-token;                  !!!next-token;
4700                  redo B;                  next B;
4701                } else {                } else {
4702                    !!!cp ('t170');
4703                  #                  #
4704                }                }
4705              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4706                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4707                  ## have a table element in table scope                  ## have a table element in table scope
4708                  my $i;                  my $i;
4709                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4710                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4711                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4712                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4713                      last INSCOPE;                        !!!cp ('t171');
4714                    } elsif ({                        $i = $_;
4715                              table => 1, html => 1,                        last INSCOPE;
4716                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4717                      last INSCOPE;                        !!!cp ('t172');
4718                          last;
4719                        }
4720                    }                    }
4721    
4722                      !!!cp ('t173');
4723                      !!!parse-error (type => 'unmatched end tag',
4724                                      value => $token->{tag_name}, token => $token);
4725                      ## Ignore the token
4726                      !!!next-token;
4727                      next B;
4728                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4729                                    
4730                  ## generate implied end tags                  ## generate implied end tags
4731                  if ({                  while ($self->{open_elements}->[-1]->[1]
4732                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4733                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
4734                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4735                  }                  }
4736                                    
4737                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4738                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4739                      !!!parse-error (type => 'not closed',
4740                                      value => $self->{open_elements}->[-1]->[0]
4741                                          ->manakai_local_name,
4742                                      token => $token);
4743                    } else {
4744                      !!!cp ('t176');
4745                  }                  }
4746                                    
4747                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3177  sub _tree_construction_main ($) { Line 4751  sub _tree_construction_main ($) {
4751                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4752                                    
4753                  !!!next-token;                  !!!next-token;
4754                  redo B;                  next B;
4755                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4756                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4757                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4758                  ## Ignore the token                  ## Ignore the token
4759                  !!!next-token;                  !!!next-token;
4760                  redo B;                  next B;
4761                } else {                } else {
4762                    !!!cp ('t178');
4763                  #                  #
4764                }                }
4765              } elsif ({              } elsif ({
# Line 3194  sub _tree_construction_main ($) { Line 4770  sub _tree_construction_main ($) {
4770                ## have an element in table scope                ## have an element in table scope
4771                my $i;                my $i;
4772                my $tn;                my $tn;
4773                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4774                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4775                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4776                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4777                    last INSCOPE;                      !!!cp ('t179');
4778                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4779                    $tn = $node->[1];  
4780                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4781                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
4782                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4783                            table => 1, html => 1,                                line => $token->{line},
4784                           }->{$node->[1]}) {                                column => $token->{column}};
4785                    last INSCOPE;                      next B;
4786                      } elsif ($node->[1] & TABLE_CELL_EL) {
4787                        !!!cp ('t180');
4788                        $tn = $node->[0]->manakai_local_name;
4789                        ## NOTE: There is exactly one |td| or |th| element
4790                        ## in scope in the stack of open elements by definition.
4791                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4792                        ## ISSUE: Can this be reached?
4793                        !!!cp ('t181');
4794                        last;
4795                      }
4796                  }                  }
4797                } # INSCOPE  
4798                unless (defined $i) {                  !!!cp ('t182');
4799                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4800                        value => $token->{tag_name}, token => $token);
4801                  ## Ignore the token                  ## Ignore the token
4802                  !!!next-token;                  !!!next-token;
4803                  redo B;                  next B;
4804                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
4805              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4806                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4807                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4808    
4809                ## As if </caption>                ## As if </caption>
4810                ## have a table element in table scope                ## have a table element in table scope
4811                my $i;                my $i;
4812                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4813                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4814                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4815                      !!!cp ('t184');
4816                    $i = $_;                    $i = $_;
4817                    last INSCOPE;                    last INSCOPE;
4818                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4819                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4820                    last INSCOPE;                    last INSCOPE;
4821                  }                  }
4822                } # INSCOPE                } # INSCOPE
4823                unless (defined $i) {                unless (defined $i) {
4824                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4825                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4826                  ## Ignore the token                  ## Ignore the token
4827                  !!!next-token;                  !!!next-token;
4828                  redo B;                  next B;
4829                }                }
4830                                
4831                ## generate implied end tags                ## generate implied end tags
4832                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4833                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4834                     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;  
4835                }                }
4836    
4837                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4838                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4839                    !!!parse-error (type => 'not closed',
4840                                    value => $self->{open_elements}->[-1]->[0]
4841                                        ->manakai_local_name,
4842                                    token => $token);
4843                  } else {
4844                    !!!cp ('t189');
4845                }                }
4846    
4847                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3270  sub _tree_construction_main ($) { Line 4851  sub _tree_construction_main ($) {
4851                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4852    
4853                ## reprocess                ## reprocess
4854                redo B;                next B;
4855              } elsif ({              } elsif ({
4856                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4857                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4858                if ($self->{insertion_mode} == IN_CELL_IM or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4859                    $self->{insertion_mode} == IN_CAPTION_IM) {                  !!!cp ('t190');
4860                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4861                  ## Ignore the token                  ## Ignore the token
4862                  !!!next-token;                  !!!next-token;
4863                  redo B;                  next B;
4864                } else {                } else {
4865                    !!!cp ('t191');
4866                  #                  #
4867                }                }
4868              } elsif ({              } elsif ({
# Line 3288  sub _tree_construction_main ($) { Line 4870  sub _tree_construction_main ($) {
4870                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4871                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4872                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4873                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
4874                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4875                ## Ignore the token                ## Ignore the token
4876                !!!next-token;                !!!next-token;
4877                redo B;                next B;
4878              } else {              } else {
4879                  !!!cp ('t193');
4880                #                #
4881              }              }
4882          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4883            for my $entry (@{$self->{open_elements}}) {
4884              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4885                !!!cp ('t75');
4886                !!!parse-error (type => 'in body:#eof', token => $token);
4887                last;
4888              }
4889            }
4890    
4891            ## Stop parsing.
4892            last B;
4893        } else {        } else {
4894          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4895        }        }
4896    
4897        $insert = $insert_to_current;        $insert = $insert_to_current;
4898        #        #
4899      } elsif ($self->{insertion_mode} == IN_ROW_IM or      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4900               $self->{insertion_mode} == IN_TABLE_BODY_IM or        if ($token->{type} == CHARACTER_TOKEN) {
4901               $self->{insertion_mode} == IN_TABLE_IM) {          if (not $open_tables->[-1]->[1] and # tainted
4902            if ($token->{type} eq 'character') {              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4903              ## NOTE: There are "character in table" code clones.            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
4904                                
4905                unless (length $token->{data}) {            unless (length $token->{data}) {
4906                  !!!next-token;              !!!cp ('t194');
4907                  redo B;              !!!next-token;
4908                }              next B;
4909              }            } else {
4910                !!!cp ('t195');
4911              }
4912            }
4913    
4914              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4915    
4916              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4917              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3323  sub _tree_construction_main ($) { Line 4919  sub _tree_construction_main ($) {
4919              ## result in a new Text node.              ## result in a new Text node.
4920              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4921                            
4922              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]}) {  
4923                # MUST                # MUST
4924                my $foster_parent_element;                my $foster_parent_element;
4925                my $next_sibling;                my $next_sibling;
4926                my $prev_sibling;                my $prev_sibling;
4927                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4928                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4929                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4930                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4931                        !!!cp ('t196');
4932                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4933                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4934                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4935                    } else {                    } else {
4936                        !!!cp ('t197');
4937                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4938                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4939                    }                    }
# Line 3350  sub _tree_construction_main ($) { Line 4945  sub _tree_construction_main ($) {
4945                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4946                if (defined $prev_sibling and                if (defined $prev_sibling and
4947                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4948                    !!!cp ('t198');
4949                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4950                } else {                } else {
4951                    !!!cp ('t199');
4952                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4953                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4954                     $next_sibling);                     $next_sibling);
4955                }                }
4956              } else {            $open_tables->[-1]->[1] = 1; # tainted
4957                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4958              }            !!!cp ('t200');
4959              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4960            }
4961                            
4962              !!!next-token;          !!!next-token;
4963              redo B;          next B;
4964            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4965              if ({              if ({
4966                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
4967                   th => 1, td => 1,                   th => 1, td => 1,
4968                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4969                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4970                  ## Clear back to table context                  ## Clear back to table context
4971                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4972                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4973                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
4974                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4975                  }                  }
4976                                    
4977                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
4978                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4979                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4980                }                }
4981    
4982                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4983                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4984                    !!!parse-error (type => 'missing start tag:tr');                    !!!cp ('t202');
4985                      !!!parse-error (type => 'missing start tag:tr', token => $token);
4986                  }                  }
4987                                    
4988                  ## Clear back to table body context                  ## Clear back to table body context
4989                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4990                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
4991                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t203');
4992                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
4993                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4994                  }                  }
4995                                    
4996                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4997                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4998                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
4999                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5000                      !!!nack ('t204');
5001                    !!!next-token;                    !!!next-token;
5002                    redo B;                    next B;
5003                  } else {                  } else {
5004                    !!!insert-element ('tr');                    !!!cp ('t205');
5005                      !!!insert-element ('tr',, $token);
5006                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5007                  }                  }
5008                  } else {
5009                    !!!cp ('t206');
5010                }                }
5011    
5012                ## Clear back to table row context                ## Clear back to table row context
5013                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5014                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5015                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5016                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5017                }                }
5018                                
5019                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5020                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5021    
5022                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5023                                
5024                  !!!nack ('t207.1');
5025                !!!next-token;                !!!next-token;
5026                redo B;                next B;
5027              } elsif ({              } elsif ({
5028                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5029                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 3430  sub _tree_construction_main ($) { Line 5035  sub _tree_construction_main ($) {
5035                  my $i;                  my $i;
5036                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5037                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5038                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5039                        !!!cp ('t208');
5040                      $i = $_;                      $i = $_;
5041                      last INSCOPE;                      last INSCOPE;
5042                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5043                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5044                      last INSCOPE;                      last INSCOPE;
5045                    }                    }
5046                  } # INSCOPE                  } # INSCOPE
5047                  unless (defined $i) {                  unless (defined $i) {
5048                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5049    ## TODO: This type is wrong.
5050                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5051                    ## Ignore the token                    ## Ignore the token
5052                      !!!nack ('t210.1');
5053                    !!!next-token;                    !!!next-token;
5054                    redo B;                    next B;
5055                  }                  }
5056                                    
5057                  ## Clear back to table row context                  ## Clear back to table row context
5058                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5059                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5060                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5061                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5062                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5063                  }                  }
5064                                    
5065                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5066                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5067                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5068                      !!!cp ('t212');
5069                    ## reprocess                    ## reprocess
5070                    redo B;                    !!!ack-later;
5071                      next B;
5072                  } else {                  } else {
5073                      !!!cp ('t213');
5074                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5075                  }                  }
5076                }                }
# Line 3469  sub _tree_construction_main ($) { Line 5080  sub _tree_construction_main ($) {
5080                  my $i;                  my $i;
5081                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5082                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5083                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5084                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5085                      $i = $_;                      $i = $_;
5086                      last INSCOPE;                      last INSCOPE;
5087                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5088                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5089                      last INSCOPE;                      last INSCOPE;
5090                    }                    }
5091                  } # INSCOPE                  } # INSCOPE
5092                  unless (defined $i) {                  unless (defined $i) {
5093                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5094    ## TODO: This erorr type ios wrong.
5095                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5096                    ## Ignore the token                    ## Ignore the token
5097                      !!!nack ('t216.1');
5098                    !!!next-token;                    !!!next-token;
5099                    redo B;                    next B;
5100                  }                  }
5101    
5102                  ## Clear back to table body context                  ## Clear back to table body context
5103                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5104                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5105                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5106                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5107                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5108                  }                  }
5109                                    
# Line 3505  sub _tree_construction_main ($) { Line 5117  sub _tree_construction_main ($) {
5117                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5118                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5119                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5120                  } else {
5121                    !!!cp ('t218');
5122                }                }
5123    
5124                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5125                  ## Clear back to table context                  ## Clear back to table context
5126                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5127                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5128                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5129                      ## ISSUE: Can this state be reached?
5130                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5131                  }                  }
5132                                    
5133                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5134                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5135                  ## reprocess                  ## reprocess
5136                  redo B;                  !!!ack-later;
5137                    next B;
5138                } elsif ({                } elsif ({
5139                          caption => 1,                          caption => 1,
5140                          colgroup => 1,                          colgroup => 1,
5141                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5142                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5143                  ## Clear back to table context                  ## Clear back to table context
5144                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5145                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5146                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5147                      ## ISSUE: Can this state be reached?
5148                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5149                  }                  }
5150                                    
5151                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5152                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5153                                    
5154                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5155                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5156                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5157                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3543  sub _tree_construction_main ($) { Line 5160  sub _tree_construction_main ($) {
5160                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5161                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5162                  !!!next-token;                  !!!next-token;
5163                  redo B;                  !!!nack ('t220.1');
5164                    next B;
5165                } else {                } else {
5166                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5167                }                }
5168              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5169                ## NOTE: There are code clones for this "table in table"                !!!parse-error (type => 'not closed',
5170                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
5171                                      ->manakai_local_name,
5172                                  token => $token);
5173    
5174                ## As if </table>                ## As if </table>
5175                ## have a table element in table scope                ## have a table element in table scope
5176                my $i;                my $i;
5177                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5178                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5179                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5180                      !!!cp ('t221');
5181                    $i = $_;                    $i = $_;
5182                    last INSCOPE;                    last INSCOPE;
5183                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5184                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5185                    last INSCOPE;                    last INSCOPE;
5186                  }                  }
5187                } # INSCOPE                } # INSCOPE
5188                unless (defined $i) {                unless (defined $i) {
5189                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5190    ## TODO: The following is wrong, maybe.
5191                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5192                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5193                    !!!nack ('t223.1');
5194                  !!!next-token;                  !!!next-token;
5195                  redo B;                  next B;
5196                }                }
5197                                
5198    ## TODO: Followings are removed from the latest spec.
5199                ## generate implied end tags                ## generate implied end tags
5200                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5201                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5202                     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;  
5203                }                }
5204    
5205                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5206                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5207                    ## NOTE: |<table><tr><table>|
5208                    !!!parse-error (type => 'not closed',
5209                                    value => $self->{open_elements}->[-1]->[0]
5210                                        ->manakai_local_name,
5211                                    token => $token);
5212                  } else {
5213                    !!!cp ('t226');
5214                }                }
5215    
5216                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5217                  pop @{$open_tables};
5218    
5219                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5220    
5221                ## reprocess            ## reprocess
5222                redo B;            !!!ack-later;
5223              next B;
5224            } elsif ($token->{tag_name} eq 'style') {
5225              if (not $open_tables->[-1]->[1]) { # tainted
5226                !!!cp ('t227.8');
5227                ## NOTE: This is a "as if in head" code clone.
5228                $parse_rcdata->(CDATA_CONTENT_MODEL);
5229                next B;
5230              } else {
5231                !!!cp ('t227.7');
5232                #
5233              }
5234            } elsif ($token->{tag_name} eq 'script') {
5235              if (not $open_tables->[-1]->[1]) { # tainted
5236                !!!cp ('t227.6');
5237                ## NOTE: This is a "as if in head" code clone.
5238                $script_start_tag->();
5239                next B;
5240              } else {
5241                !!!cp ('t227.5');
5242                #
5243              }
5244            } elsif ($token->{tag_name} eq 'input') {
5245              if (not $open_tables->[-1]->[1]) { # tainted
5246                if ($token->{attributes}->{type}) { ## TODO: case
5247                  my $type = lc $token->{attributes}->{type}->{value};
5248                  if ($type eq 'hidden') {
5249                    !!!cp ('t227.3');
5250                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5251    
5252                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5253    
5254                    ## TODO: form element pointer
5255    
5256                    pop @{$self->{open_elements}};
5257    
5258                    !!!next-token;
5259                    !!!ack ('t227.2.1');
5260                    next B;
5261                  } else {
5262                    !!!cp ('t227.2');
5263                    #
5264                  }
5265              } else {              } else {
5266                  !!!cp ('t227.1');
5267                #                #
5268              }              }
5269            } elsif ($token->{type} eq 'end tag') {            } else {
5270                !!!cp ('t227.4');
5271                #
5272              }
5273            } else {
5274              !!!cp ('t227');
5275              #
5276            }
5277    
5278            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5279    
5280            $insert = $insert_to_foster;
5281            #
5282          } elsif ($token->{type} == END_TAG_TOKEN) {
5283              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5284                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
5285                ## have an element in table scope                ## have an element in table scope
5286                my $i;                my $i;
5287                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5288                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5289                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5290                      !!!cp ('t228');
5291                    $i = $_;                    $i = $_;
5292                    last INSCOPE;                    last INSCOPE;
5293                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5294                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5295                    last INSCOPE;                    last INSCOPE;
5296                  }                  }
5297                } # INSCOPE                } # INSCOPE
5298                unless (defined $i) {                unless (defined $i) {
5299                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5300                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5301                  ## Ignore the token                  ## Ignore the token
5302                    !!!nack ('t230.1');
5303                  !!!next-token;                  !!!next-token;
5304                  redo B;                  next B;
5305                  } else {
5306                    !!!cp ('t232');
5307                }                }
5308    
5309                ## Clear back to table row context                ## Clear back to table row context
5310                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5311                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5312                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5313                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5314                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5315                }                }
5316    
5317                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5318                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5319                !!!next-token;                !!!next-token;
5320                redo B;                !!!nack ('t231.1');
5321                  next B;
5322              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5323                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5324                  ## As if </tr>                  ## As if </tr>
# Line 3641  sub _tree_construction_main ($) { Line 5326  sub _tree_construction_main ($) {
5326                  my $i;                  my $i;
5327                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5328                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5329                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5330                        !!!cp ('t233');
5331                      $i = $_;                      $i = $_;
5332                      last INSCOPE;                      last INSCOPE;
5333                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5334                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5335                      last INSCOPE;                      last INSCOPE;
5336                    }                    }
5337                  } # INSCOPE                  } # INSCOPE
5338                  unless (defined $i) {                  unless (defined $i) {
5339                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5340    ## TODO: The following is wrong.
5341                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5342                    ## Ignore the token                    ## Ignore the token
5343                      !!!nack ('t236.1');
5344                    !!!next-token;                    !!!next-token;
5345                    redo B;                    next B;
5346                  }                  }
5347                                    
5348                  ## Clear back to table row context                  ## Clear back to table row context
5349                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5350                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5351                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5352                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5353                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5354                  }                  }
5355                                    
# Line 3675  sub _tree_construction_main ($) { Line 5363  sub _tree_construction_main ($) {
5363                  my $i;                  my $i;
5364                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5365                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5366                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5367                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5368                      $i = $_;                      $i = $_;
5369                      last INSCOPE;                      last INSCOPE;
5370                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5371                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5372                      last INSCOPE;                      last INSCOPE;
5373                    }                    }
5374                  } # INSCOPE                  } # INSCOPE
5375                  unless (defined $i) {                  unless (defined $i) {
5376                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5377                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5378                    ## Ignore the token                    ## Ignore the token
5379                      !!!nack ('t239.1');
5380                    !!!next-token;                    !!!next-token;
5381                    redo B;                    next B;
5382                  }                  }
5383                                    
5384                  ## Clear back to table body context                  ## Clear back to table body context
5385                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5386                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5387                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5388                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5389                  }                  }
5390                                    
# Line 3713  sub _tree_construction_main ($) { Line 5400  sub _tree_construction_main ($) {
5400                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5401                }                }
5402    
5403                  ## NOTE: </table> in the "in table" insertion mode.
5404                  ## When you edit the code fragment below, please ensure that
5405                  ## the code for <table> in the "in table" insertion mode
5406                  ## is synced with it.
5407    
5408                ## have a table element in table scope                ## have a table element in table scope
5409                my $i;                my $i;
5410                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5411                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5412                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5413                      !!!cp ('t241');
5414                    $i = $_;                    $i = $_;
5415                    last INSCOPE;                    last INSCOPE;
5416                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5417                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5418                    last INSCOPE;                    last INSCOPE;
5419                  }                  }
5420                } # INSCOPE                } # INSCOPE
5421                unless (defined $i) {                unless (defined $i) {
5422                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5423                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5424                  ## Ignore the token                  ## Ignore the token
5425                    !!!nack ('t243.1');
5426                  !!!next-token;                  !!!next-token;
5427                  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]);  
5428                }                }
5429                                    
5430                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5431                  pop @{$open_tables};
5432                                
5433                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5434                                
5435                !!!next-token;                !!!next-token;
5436                redo B;                next B;
5437              } elsif ({              } elsif ({
5438                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5439                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5440                       ($self->{insertion_mode} == IN_ROW_IM or                       $self->{insertion_mode} & ROW_IMS) {
                       $self->{insertion_mode} == IN_TABLE_BODY_IM)) {  
5441                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5442                  ## have an element in table scope                  ## have an element in table scope
5443                  my $i;                  my $i;
5444                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5445                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5446                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5447                        !!!cp ('t247');
5448                      $i = $_;                      $i = $_;
5449                      last INSCOPE;                      last INSCOPE;
5450                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5451                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
5452                      last INSCOPE;                      last INSCOPE;
5453                    }                    }
5454                  } # INSCOPE                  } # INSCOPE
5455                    unless (defined $i) {                    unless (defined $i) {
5456                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
5457                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5458                      ## Ignore the token                      ## Ignore the token
5459                        !!!nack ('t249.1');
5460                      !!!next-token;                      !!!next-token;
5461                      redo B;                      next B;
5462                    }                    }
5463                                    
5464                  ## As if </tr>                  ## As if </tr>
# Line 3786  sub _tree_construction_main ($) { Line 5466  sub _tree_construction_main ($) {
5466                  my $i;                  my $i;
5467                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5468                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5469                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5470                        !!!cp ('t250');
5471                      $i = $_;                      $i = $_;
5472                      last INSCOPE;                      last INSCOPE;
5473                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5474                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
5475                      last INSCOPE;                      last INSCOPE;
5476                    }                    }
5477                  } # INSCOPE                  } # INSCOPE
5478                    unless (defined $i) {                    unless (defined $i) {
5479                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
5480                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5481                      ## Ignore the token                      ## Ignore the token
5482                        !!!nack ('t252.1');
5483                      !!!next-token;                      !!!next-token;
5484                      redo B;                      next B;
5485                    }                    }
5486                                    
5487                  ## Clear back to table row context                  ## Clear back to table row context
5488                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5489                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5490                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
5491                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5492                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5493                  }                  }
5494                                    
# Line 3819  sub _tree_construction_main ($) { Line 5501  sub _tree_construction_main ($) {
5501                my $i;                my $i;
5502                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5503                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5504                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5505                      !!!cp ('t254');
5506                    $i = $_;                    $i = $_;
5507                    last INSCOPE;                    last INSCOPE;
5508                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5509                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5510                    last INSCOPE;                    last INSCOPE;
5511                  }                  }
5512                } # INSCOPE                } # INSCOPE
5513                unless (defined $i) {                unless (defined $i) {
5514                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5515                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5516                  ## Ignore the token                  ## Ignore the token
5517                    !!!nack ('t256.1');
5518                  !!!next-token;                  !!!next-token;
5519                  redo B;                  next B;
5520                }                }
5521    
5522                ## Clear back to table body context                ## Clear back to table body context
5523                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5524                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5525                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5526                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5527                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5528                }                }
5529    
5530                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5531                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5532                  !!!nack ('t257.1');
5533                !!!next-token;                !!!next-token;
5534                redo B;                next B;
5535              } elsif ({              } elsif ({
5536                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5537                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5538                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5539                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5540                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5541                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5542                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5543                !!!next-token;            ## Ignore the token
5544                redo B;            !!!nack ('t258.1');
5545              } else {             !!!next-token;
5546                #            next B;
5547              }          } else {
5548            } else {            !!!cp ('t259');
5549              die "$0: $token->{type}: Unknown token type";            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
           }  
5550    
5551        !!!parse-error (type => 'in table:'.$token->{tag_name});            $insert = $insert_to_foster;
5552              #
5553            }
5554          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5555            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5556                    @{$self->{open_elements}} == 1) { # redundant, maybe
5557              !!!parse-error (type => 'in body:#eof', token => $token);
5558              !!!cp ('t259.1');
5559              #
5560            } else {
5561              !!!cp ('t259.2');
5562              #
5563            }
5564    
5565        $insert = $insert_to_foster;          ## Stop parsing
5566        #          last B;
5567          } else {
5568            die "$0: $token->{type}: Unknown token type";
5569          }
5570      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5571            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
5572              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5573                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5574                unless (length $token->{data}) {                unless (length $token->{data}) {
5575                    !!!cp ('t260');
5576                  !!!next-token;                  !!!next-token;
5577                  redo B;                  next B;
5578                }                }
5579              }              }
5580                            
5581                !!!cp ('t261');
5582              #              #
5583            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
5584              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5585                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
5586                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5587                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5588                  !!!ack ('t262.1');
5589                !!!next-token;                !!!next-token;
5590                redo B;                next B;
5591              } else {              } else {
5592                  !!!cp ('t263');
5593                #                #
5594              }              }
5595            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5596              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5597                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5598                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5599                    !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5600                  ## Ignore the token                  ## Ignore the token
5601                  !!!next-token;                  !!!next-token;
5602                  redo B;                  next B;
5603                } else {                } else {
5604                    !!!cp ('t265');
5605                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5606                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5607                  !!!next-token;                  !!!next-token;
5608                  redo B;                              next B;            
5609                }                }
5610              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5611                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5612                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
5613                ## Ignore the token                ## Ignore the token
5614                !!!next-token;                !!!next-token;
5615                redo B;                next B;
5616              } else {              } else {
5617                  !!!cp ('t267');
5618                #                #
5619              }              }
5620            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5621              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5622            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5623              !!!cp ('t270.2');
5624              ## Stop parsing.
5625              last B;
5626            } else {
5627              ## NOTE: As if </colgroup>.
5628              !!!cp ('t270.1');
5629              pop @{$self->{open_elements}}; # colgroup
5630              $self->{insertion_mode} = IN_TABLE_IM;
5631              ## Reprocess.
5632              next B;
5633            }
5634          } else {
5635            die "$0: $token->{type}: Unknown token type";
5636          }
5637    
5638            ## As if </colgroup>            ## As if </colgroup>
5639            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5640              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5641    ## TODO: Wrong error type?
5642                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5643              ## Ignore the token              ## Ignore the token
5644                !!!nack ('t269.1');
5645              !!!next-token;              !!!next-token;
5646              redo B;              next B;
5647            } else {            } else {
5648                !!!cp ('t270');
5649              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5650              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5651                !!!ack-later;
5652              ## reprocess              ## reprocess
5653              redo B;              next B;
5654              }
5655        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5656          if ($token->{type} == CHARACTER_TOKEN) {
5657            !!!cp ('t271');
5658            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5659            !!!next-token;
5660            next B;
5661          } elsif ($token->{type} == START_TAG_TOKEN) {
5662            if ($token->{tag_name} eq 'option') {
5663              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5664                !!!cp ('t272');
5665                ## As if </option>
5666                pop @{$self->{open_elements}};
5667              } else {
5668                !!!cp ('t273');
5669            }            }
     } elsif ($self->{insertion_mode} == IN_SELECT_IM) {  
           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}};  
               }  
5670    
5671                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5672                !!!next-token;            !!!nack ('t273.1');
5673                redo B;            !!!next-token;
5674              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5675                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5676                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5677                  pop @{$self->{open_elements}};              !!!cp ('t274');
5678                }              ## As if </option>
5679                pop @{$self->{open_elements}};
5680              } else {
5681                !!!cp ('t275');
5682              }
5683    
5684                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5685                  ## As if </optgroup>              !!!cp ('t276');
5686                  pop @{$self->{open_elements}};              ## As if </optgroup>
5687                }              pop @{$self->{open_elements}};
5688              } else {
5689                !!!cp ('t277');
5690              }
5691    
5692                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5693                !!!next-token;            !!!nack ('t277.1');
5694                redo B;            !!!next-token;
5695              } elsif ($token->{tag_name} eq 'select') {            next B;
5696                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5697                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5698                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5699                my $i;                    {
5700                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5701                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5702                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5703                    $i = $_;                    }->{$token->{tag_name}})) {
5704                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5705                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5706                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5707                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5708                    last INSCOPE;            ## have an element in table scope
5709                  }            my $i;
5710                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5711                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5712                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5713                  ## Ignore the token                !!!cp ('t278');
5714                  !!!next-token;                $i = $_;
5715                  redo B;                last INSCOPE;
5716                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5717                  !!!cp ('t279');
5718                  last INSCOPE;
5719                }
5720              } # INSCOPE
5721              unless (defined $i) {
5722                !!!cp ('t280');
5723                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5724                ## Ignore the token
5725                !!!nack ('t280.1');
5726                !!!next-token;
5727                next B;
5728              }
5729                                
5730                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5731              splice @{$self->{open_elements}}, $i;
5732    
5733                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5734    
5735                !!!next-token;            if ($token->{tag_name} eq 'select') {
5736                redo B;              !!!nack ('t281.2');
5737              } else {              !!!next-token;
5738                #              next B;
5739              } else {
5740                !!!cp ('t281.1');
5741                !!!ack-later;
5742                ## Reprocess the token.
5743                next B;
5744              }
5745            } else {
5746              !!!cp ('t282');
5747              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5748              ## Ignore the token
5749              !!!nack ('t282.1');
5750              !!!next-token;
5751              next B;
5752            }
5753          } elsif ($token->{type} == END_TAG_TOKEN) {
5754            if ($token->{tag_name} eq 'optgroup') {
5755              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5756                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5757                !!!cp ('t283');
5758                ## As if </option>
5759                splice @{$self->{open_elements}}, -2;
5760              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5761                !!!cp ('t284');
5762                pop @{$self->{open_elements}};
5763              } else {
5764                !!!cp ('t285');
5765                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5766                ## Ignore the token
5767              }
5768              !!!nack ('t285.1');
5769              !!!next-token;
5770              next B;
5771            } elsif ($token->{tag_name} eq 'option') {
5772              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5773                !!!cp ('t286');
5774                pop @{$self->{open_elements}};
5775              } else {
5776                !!!cp ('t287');
5777                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5778                ## Ignore the token
5779              }
5780              !!!nack ('t287.1');
5781              !!!next-token;
5782              next B;
5783            } elsif ($token->{tag_name} eq 'select') {
5784              ## have an element in table scope
5785              my $i;
5786              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5787                my $node = $self->{open_elements}->[$_];
5788                if ($node->[1] & SELECT_EL) {
5789                  !!!cp ('t288');
5790                  $i = $_;
5791                  last INSCOPE;
5792                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5793                  !!!cp ('t289');
5794                  last INSCOPE;
5795              }              }
5796            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
5797              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
5798                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
5799                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5800                  ## As if </option>              ## Ignore the token
5801                  splice @{$self->{open_elements}}, -2;              !!!nack ('t290.1');
5802                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!next-token;
5803                  pop @{$self->{open_elements}};              next B;
5804                } 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;  
               }  
5805                                
5806                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5807              splice @{$self->{open_elements}}, $i;
5808    
5809                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5810    
5811                !!!next-token;            !!!nack ('t291.1');
5812                redo B;            !!!next-token;
5813              } elsif ({            next B;
5814                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5815                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5816                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5817                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5818                                   }->{$token->{tag_name}}) {
5819                ## have an element in table scope  ## TODO: The following is wrong?
5820                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;  
               }  
5821                                
5822                ## As if </select>            ## have an element in table scope
5823                ## have an element in table scope            my $i;
5824                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5825                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
5826                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5827                  if ($node->[1] eq 'select') {                !!!cp ('t292');
5828                    $i = $_;                $i = $_;
5829                    last INSCOPE;                last INSCOPE;
5830                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5831                            table => 1, html => 1,                !!!cp ('t293');
5832                           }->{$node->[1]}) {                last INSCOPE;
5833                    last INSCOPE;              }
5834                  }            } # INSCOPE
5835                } # INSCOPE            unless (defined $i) {
5836                unless (defined $i) {              !!!cp ('t294');
5837                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
5838                  ## Ignore the </select> token              !!!nack ('t294.1');
5839                  !!!next-token; ## TODO: ok?              !!!next-token;
5840                  redo B;              next B;
5841                }            }
5842                                
5843                splice @{$self->{open_elements}}, $i;            ## As if </select>
5844              ## have an element in table scope
5845                $self->_reset_insertion_mode;            undef $i;
5846              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5847                ## reprocess              my $node = $self->{open_elements}->[$_];
5848                redo B;              if ($node->[1] & SELECT_EL) {
5849              } else {                !!!cp ('t295');
5850                #                $i = $_;
5851                  last INSCOPE;
5852                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5853    ## ISSUE: Can this state be reached?
5854                  !!!cp ('t296');
5855                  last INSCOPE;
5856              }              }
5857            } else {            } # INSCOPE
5858              #            unless (defined $i) {
5859                !!!cp ('t297');
5860    ## TODO: The following error type is correct?
5861                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5862                ## Ignore the </select> token
5863                !!!nack ('t297.1');
5864                !!!next-token; ## TODO: ok?
5865                next B;
5866            }            }
5867                  
5868              !!!cp ('t298');
5869              splice @{$self->{open_elements}}, $i;
5870    
5871            !!!parse-error (type => 'in select:'.$token->{tag_name});            $self->_reset_insertion_mode;
5872    
5873              !!!ack-later;
5874              ## reprocess
5875              next B;
5876            } else {
5877              !!!cp ('t299');
5878              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5879            ## Ignore the token            ## Ignore the token
5880              !!!nack ('t299.3');
5881            !!!next-token;            !!!next-token;
5882            redo B;            next B;
5883      } elsif ($self->{insertion_mode} == AFTER_BODY_IM or          }
5884               $self->{insertion_mode} == AFTER_HTML_BODY_IM) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5885        if ($token->{type} eq 'character') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5886                    @{$self->{open_elements}} == 1) { # redundant, maybe
5887              !!!cp ('t299.1');
5888              !!!parse-error (type => 'in body:#eof', token => $token);
5889            } else {
5890              !!!cp ('t299.2');
5891            }
5892    
5893            ## Stop parsing.
5894            last B;
5895          } else {
5896            die "$0: $token->{type}: Unknown token type";
5897          }
5898        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5899          if ($token->{type} == CHARACTER_TOKEN) {
5900          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5901            my $data = $1;            my $data = $1;
5902            ## As if in body            ## As if in body
# Line 4110  sub _tree_construction_main ($) { Line 5905  sub _tree_construction_main ($) {
5905            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5906                        
5907            unless (length $token->{data}) {            unless (length $token->{data}) {
5908                !!!cp ('t300');
5909              !!!next-token;              !!!next-token;
5910              redo B;              next B;
5911            }            }
5912          }          }
5913                    
5914          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5915            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
5916              !!!parse-error (type => 'after html:#character', token => $token);
5917    
5918            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5919            } else {
5920              !!!cp ('t302');
5921          }          }
5922                    
5923          ## "after body" insertion mode          ## "after body" insertion mode
5924          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5925    
5926          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5927          ## reprocess          ## reprocess
5928          redo B;          next B;
5929        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
5930          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5931            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
5932              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5933                        
5934            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5935            } else {
5936              !!!cp ('t304');
5937          }          }
5938    
5939          ## "after body" insertion mode          ## "after body" insertion mode
5940          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5941    
5942          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5943            !!!ack-later;
5944          ## reprocess          ## reprocess
5945          redo B;          next B;
5946        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
5947          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5948            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
5949              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5950                        
5951            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5952            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5953            } else {
5954              !!!cp ('t306');
5955          }          }
5956    
5957          ## "after body" insertion mode          ## "after body" insertion mode
5958          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5959            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5960              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
5961                !!!parse-error (type => 'unmatched end tag:html', token => $token);
5962              ## Ignore the token              ## Ignore the token
5963              !!!next-token;              !!!next-token;
5964              redo B;              next B;
5965            } else {            } else {
5966                !!!cp ('t308');
5967              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5968              !!!next-token;              !!!next-token;
5969              redo B;              next B;
5970            }            }
5971          } else {          } else {
5972            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
5973              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5974    
5975            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5976            ## reprocess            ## reprocess
5977            redo B;            next B;
5978          }          }
5979          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5980            !!!cp ('t309.2');
5981            ## Stop parsing
5982            last B;
5983        } else {        } else {
5984          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5985        }        }
5986      } elsif ($self->{insertion_mode} == IN_FRAMESET_IM or      } elsif ($self->{insertion_mode} & FRAME_IMS) {
5987               $self->{insertion_mode} == AFTER_FRAMESET_IM or        if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
       if ($token->{type} eq 'character') {  
5988          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5989            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5990                        
5991            unless (length $token->{data}) {            unless (length $token->{data}) {
5992                !!!cp ('t310');
5993              !!!next-token;              !!!next-token;
5994              redo B;              next B;
5995            }            }
5996          }          }
5997                    
5998          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5999            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6000              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6001                !!!parse-error (type => 'in frameset:#character', token => $token);
6002            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6003              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
6004                !!!parse-error (type => 'after frameset:#character', token => $token);
6005            } else { # "after html frameset"            } else { # "after html frameset"
6006              !!!parse-error (type => 'after html:#character');              !!!cp ('t313');
6007                !!!parse-error (type => 'after html:#character', token => $token);
6008    
6009              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6010              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
6011              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6012            }            }
6013                        
6014            ## Ignore the token.            ## Ignore the token.
6015            if (length $token->{data}) {            if (length $token->{data}) {
6016                !!!cp ('t314');
6017              ## reprocess the rest of characters              ## reprocess the rest of characters
6018            } else {            } else {
6019                !!!cp ('t315');
6020              !!!next-token;              !!!next-token;
6021            }            }
6022            redo B;            next B;
6023          }          }
6024                    
6025          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6026        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
6027          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6028            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t316');
6029              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6030    
6031            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6032            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6033          }          } else {
6034              !!!cp ('t317');
6035            }
6036    
6037          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6038              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6039            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6040              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6041              !!!nack ('t318.1');
6042            !!!next-token;            !!!next-token;
6043            redo B;            next B;
6044          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6045                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6046            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6047              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6048            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6049              !!!ack ('t319.1');
6050            !!!next-token;            !!!next-token;
6051            redo B;            next B;
6052          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6053              !!!cp ('t320');
6054            ## NOTE: As if in body.            ## NOTE: As if in body.
6055            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6056            redo B;            next B;
6057          } else {          } else {
6058            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6059              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6060                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6061            } else {            } else {
6062              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!cp ('t322');
6063                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6064            }            }
6065            ## Ignore the token            ## Ignore the token
6066              !!!nack ('t322.1');
6067            !!!next-token;            !!!next-token;
6068            redo B;            next B;
6069          }          }
6070        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6071          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6072            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t323');
6073              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6074    
6075            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6076            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6077            } else {
6078              !!!cp ('t324');
6079          }          }
6080    
6081          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6082              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6083            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6084                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6085              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6086                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6087              ## Ignore the token              ## Ignore the token
6088              !!!next-token;              !!!next-token;
6089            } else {            } else {
6090                !!!cp ('t326');
6091              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6092              !!!next-token;              !!!next-token;
6093            }            }
6094    
6095            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6096                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6097                !!!cp ('t327');
6098              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6099              } else {
6100                !!!cp ('t328');
6101            }            }
6102            redo B;            next B;
6103          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6104                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6105              !!!cp ('t329');
6106            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6107            !!!next-token;            !!!next-token;
6108            redo B;            next B;
6109          } else {          } else {
6110            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6111              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6112                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6113            } else {            } else {
6114              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!cp ('t331');
6115                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6116            }            }
6117            ## Ignore the token            ## Ignore the token
6118            !!!next-token;            !!!next-token;
6119            redo B;            next B;
6120            }
6121          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6122            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6123                    @{$self->{open_elements}} == 1) { # redundant, maybe
6124              !!!cp ('t331.1');
6125              !!!parse-error (type => 'in body:#eof', token => $token);
6126            } else {
6127              !!!cp ('t331.2');
6128          }          }
6129            
6130            ## Stop parsing
6131            last B;
6132        } else {        } else {
6133          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6134        }        }
# Line 4289  sub _tree_construction_main ($) { Line 6139  sub _tree_construction_main ($) {
6139      }      }
6140    
6141      ## "in body" insertion mode      ## "in body" insertion mode
6142      if ($token->{type} eq 'start tag') {      if ($token->{type} == START_TAG_TOKEN) {
6143        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6144            !!!cp ('t332');
6145          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6146          $script_start_tag->($insert);          $script_start_tag->();
6147          redo B;          next B;
6148        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6149            !!!cp ('t333');
6150          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6151          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6152          redo B;          next B;
6153        } elsif ({        } elsif ({
6154                  base => 1, link => 1,                  base => 1, link => 1,
6155                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6156            !!!cp ('t334');
6157          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
6158          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6159          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6160            !!!ack ('t334.1');
6161          !!!next-token;          !!!next-token;
6162          redo B;          next B;
6163        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6164          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
6165          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6166          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6167    
6168          unless ($self->{confident}) {          unless ($self->{confident}) {
           my $charset;  
6169            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
6170              $charset = $token->{attributes}->{charset}->{value};              !!!cp ('t335');
6171            }              $self->{change_encoding}
6172            if ($token->{attributes}->{'http-equiv'}) {                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6173                
6174                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6175                    ->set_user_data (manakai_has_reference =>
6176                                         $token->{attributes}->{charset}
6177                                             ->{has_reference});
6178              } elsif ($token->{attributes}->{content}) {
6179              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
6180              if ($token->{attributes}->{'http-equiv'}->{value}              if ($token->{attributes}->{content}->{value}
6181                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6182                        [\x09-\x0D\x20]*=
6183                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6184                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6185                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                !!!cp ('t336');
6186              } ## TODO: And if supported                $self->{change_encoding}
6187                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6188                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6189                      ->set_user_data (manakai_has_reference =>
6190                                           $token->{attributes}->{content}
6191                                                 ->{has_reference});
6192                }
6193              }
6194            } else {
6195              if ($token->{attributes}->{charset}) {
6196                !!!cp ('t337');
6197                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6198                    ->set_user_data (manakai_has_reference =>
6199                                         $token->{attributes}->{charset}
6200                                             ->{has_reference});
6201              }
6202              if ($token->{attributes}->{content}) {
6203                !!!cp ('t338');
6204                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6205                    ->set_user_data (manakai_has_reference =>
6206                                         $token->{attributes}->{content}
6207                                             ->{has_reference});
6208            }            }
           ## TODO: Change the encoding  
6209          }          }
6210    
6211            !!!ack ('t338.1');
6212          !!!next-token;          !!!next-token;
6213          redo B;          next B;
6214        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6215          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6216          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6217          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6218            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6219        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6220          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6221                                
6222          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6223              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6224              !!!cp ('t342');
6225            ## Ignore the token            ## Ignore the token
6226          } else {          } else {
6227            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6228            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6229              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6230                  !!!cp ('t343');
6231                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6232                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6233                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6234              }              }
6235            }            }
6236          }          }
6237            !!!nack ('t343.1');
6238          !!!next-token;          !!!next-token;
6239          redo B;          next B;
6240        } elsif ({        } elsif ({
6241                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6242                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6243                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6244                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6245                  pre => 1,                  pre => 1, listing => 1,
6246                    form => 1,
6247                    table => 1,
6248                    hr => 1,
6249                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6250            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6251              !!!cp ('t350');
6252              !!!parse-error (type => 'in form:form', token => $token);
6253              ## Ignore the token
6254              !!!nack ('t350.1');
6255              !!!next-token;
6256              next B;
6257            }
6258    
6259          ## has a p element in scope          ## has a p element in scope
6260          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6261            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6262              !!!back-token;              !!!cp ('t344');
6263              $token = {type => 'end tag', tag_name => 'p'};              !!!back-token; # <form>
6264              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6265            } elsif ({                        line => $token->{line}, column => $token->{column}};
6266                      table => 1, caption => 1, td => 1, th => 1,              next B;
6267                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6268                     }->{$_->[1]}) {              !!!cp ('t345');
6269              last INSCOPE;              last INSCOPE;
6270            }            }
6271          } # INSCOPE          } # INSCOPE
6272                        
6273          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6274          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6275              !!!nack ('t346.1');
6276            !!!next-token;            !!!next-token;
6277            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
6278              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6279              unless (length $token->{data}) {              unless (length $token->{data}) {
6280                  !!!cp ('t346');
6281                !!!next-token;                !!!next-token;
6282                } else {
6283                  !!!cp ('t349');
6284              }              }
6285              } else {
6286                !!!cp ('t348');
6287            }            }
6288          } else {          } elsif ($token->{tag_name} eq 'form') {
6289              !!!cp ('t347.1');
6290              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6291    
6292              !!!nack ('t347.2');
6293            !!!next-token;            !!!next-token;
6294          }          } elsif ($token->{tag_name} eq 'table') {
6295          redo B;            !!!cp ('t382');
6296        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6297          if (defined $self->{form_element}) {            
6298            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6299            ## Ignore the token  
6300              !!!nack ('t382.1');
6301              !!!next-token;
6302            } elsif ($token->{tag_name} eq 'hr') {
6303              !!!cp ('t386');
6304              pop @{$self->{open_elements}};
6305            
6306              !!!nack ('t386.1');
6307            !!!next-token;            !!!next-token;
           redo B;  
6308          } else {          } else {
6309            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6310            !!!next-token;            !!!next-token;
           redo B;  
6311          }          }
6312        } elsif ($token->{tag_name} eq 'li') {          next B;
6313          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6314          ## has a p element in scope          ## has a p element in scope
6315          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6316            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6317              !!!back-token;              !!!cp ('t353');
6318              $token = {type => 'end tag', tag_name => 'p'};              !!!back-token; # <x>
6319              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6320            } elsif ({                        line => $token->{line}, column => $token->{column}};
6321                      table => 1, caption => 1, td => 1, th => 1,              next B;
6322                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6323                     }->{$_->[1]}) {              !!!cp ('t354');
6324              last INSCOPE;              last INSCOPE;
6325            }            }
6326          } # INSCOPE          } # INSCOPE
# Line 4436  sub _tree_construction_main ($) { Line 6328  sub _tree_construction_main ($) {
6328          ## Step 1          ## Step 1
6329          my $i = -1;          my $i = -1;
6330          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6331            my $li_or_dtdd = {li => {li => 1},
6332                              dt => {dt => 1, dd => 1},
6333                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6334          LI: {          LI: {
6335            ## Step 2            ## Step 2
6336            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6337              if ($i != -1) {              if ($i != -1) {
6338                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6339                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6340              }                                value => $self->{open_elements}->[-1]->[0]
6341              splice @{$self->{open_elements}}, $i;                                    ->manakai_local_name,
6342              last LI;                                token => $token);
6343            }              } else {
6344                            !!!cp ('t356');
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
6345              }              }
6346              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6347              last LI;              last LI;
6348              } else {
6349                !!!cp ('t357');
6350            }            }
6351                        
6352            ## Step 3            ## Step 3
6353            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6354                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6355                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6356                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6357                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6358                  not ($node->[1] & DIV_EL)) {
6359                !!!cp ('t358');
6360              last LI;              last LI;
6361            }            }
6362                        
6363              !!!cp ('t359');
6364            ## Step 4            ## Step 4
6365            $i--;            $i--;
6366            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
6367            redo LI;            redo LI;
6368          } # LI          } # LI
6369                        
6370          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6371            !!!nack ('t359.1');
6372          !!!next-token;          !!!next-token;
6373          redo B;          next B;
6374        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6375          ## has a p element in scope          ## has a p element in scope
6376          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6377            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6378              !!!back-token;              !!!cp ('t367');
6379              $token = {type => 'end tag', tag_name => 'p'};              !!!back-token; # <plaintext>
6380              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6381            } elsif ({                        line => $token->{line}, column => $token->{column}};
6382                      table => 1, caption => 1, td => 1, th => 1,              next B;
6383                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6384                     }->{$_->[1]}) {              !!!cp ('t368');
6385              last INSCOPE;              last INSCOPE;
6386            }            }
6387          } # INSCOPE          } # INSCOPE
6388                        
6389          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6390                        
6391          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6392                        
6393            !!!nack ('t368.1');
6394          !!!next-token;          !!!next-token;
6395          redo B;          next B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
6396        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6397          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6398            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6399            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6400              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
6401                !!!parse-error (type => 'in a:a', token => $token);
6402                            
6403              !!!back-token;              !!!back-token; # <a>
6404              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6405              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6406                $formatting_end_tag->($token);
6407                            
6408              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6409                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6410                    !!!cp ('t372');
6411                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
6412                  last AFE2;                  last AFE2;
6413                }                }
6414              } # AFE2              } # AFE2
6415              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
6416                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6417                    !!!cp ('t373');
6418                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
6419                  last OE;                  last OE;
6420                }                }
6421              } # OE              } # OE
6422              last AFE;              last AFE;
6423            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
6424                !!!cp ('t374');
6425              last AFE;              last AFE;
6426            }            }
6427          } # AFE          } # AFE
6428                        
6429          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6430    
6431          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6432          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6433    
6434            !!!nack ('t374.1');
6435          !!!next-token;          !!!next-token;
6436          redo B;          next B;
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         redo B;  
6437        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6438          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6439    
6440          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6441          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6442            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6443            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6444              !!!parse-error (type => 'not closed:nobr');              !!!cp ('t376');
6445              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
6446              $token = {type => 'end tag', tag_name => 'nobr'};              !!!back-token; # <nobr>
6447              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6448            } elsif ({                        line => $token->{line}, column => $token->{column}};
6449                      table => 1, caption => 1, td => 1, th => 1,              next B;
6450                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6451                     }->{$node->[1]}) {              !!!cp ('t377');
6452              last INSCOPE;              last INSCOPE;
6453            }            }
6454          } # INSCOPE          } # INSCOPE
6455                    
6456          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6457          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6458                    
6459            !!!nack ('t377.1');
6460          !!!next-token;          !!!next-token;
6461          redo B;          next B;
6462        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6463          ## has a button element in scope          ## has a button element in scope
6464          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6465            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6466            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6467              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
6468              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
6469              $token = {type => 'end tag', tag_name => 'button'};              !!!back-token; # <button>
6470              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6471            } elsif ({                        line => $token->{line}, column => $token->{column}};
6472                      table => 1, caption => 1, td => 1, th => 1,              next B;
6473                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6474                     }->{$node->[1]}) {              !!!cp ('t379');
6475              last INSCOPE;              last INSCOPE;
6476            }            }
6477          } # INSCOPE          } # INSCOPE
6478                        
6479          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6480                        
6481          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6482          push @$active_formatting_elements, ['#marker', ''];  
6483            ## TODO: associate with $self->{form_element} if defined
6484    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6485          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6486            
6487          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
6488          !!!next-token;          !!!next-token;
6489          redo B;          next B;
6490        } elsif ({        } elsif ({
6491                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6492                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6493                  image => 1,                  noembed => 1,
6494                    noframes => 1,
6495                    noscript => 0, ## TODO: 1 if scripting is enabled
6496                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6497          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6498            !!!parse-error (type => 'image');            !!!cp ('t381');
6499            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
6500            } else {
6501              !!!cp ('t399');
6502          }          }
6503            ## NOTE: There is an "as if in body" code clone.
6504          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6505          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
6506        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6507          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6508                    
6509          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6510              !!!cp ('t389');
6511            ## Ignore the token            ## Ignore the token
6512              !!!nack ('t389'); ## NOTE: Not acknowledged.
6513            !!!next-token;            !!!next-token;
6514            redo B;            next B;
6515          } else {          } else {
6516            my $at = $token->{attributes};            my $at = $token->{attributes};
6517            my $form_attrs;            my $form_attrs;
# Line 4770  sub _tree_construction_main ($) { Line 6521  sub _tree_construction_main ($) {
6521            delete $at->{action};            delete $at->{action};
6522            delete $at->{prompt};            delete $at->{prompt};
6523            my @tokens = (            my @tokens = (
6524                          {type => 'start tag', tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6525                           attributes => $form_attrs},                           attributes => $form_attrs,
6526                          {type => 'start tag', tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6527                          {type => 'start tag', tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6528                          {type => 'start tag', tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6529                            {type => START_TAG_TOKEN, tag_name => 'p',
6530                             line => $token->{line}, column => $token->{column}},
6531                            {type => START_TAG_TOKEN, tag_name => 'label',
6532                             line => $token->{line}, column => $token->{column}},
6533                         );                         );
6534            if ($prompt_attr) {            if ($prompt_attr) {
6535              push @tokens, {type => 'character', data => $prompt_attr->{value}};              !!!cp ('t390');
6536                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6537                               #line => $token->{line}, column => $token->{column},
6538                              };
6539            } else {            } else {
6540              push @tokens, {type => 'character',              !!!cp ('t391');
6541                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD              push @tokens, {type => CHARACTER_TOKEN,
6542                               data => 'This is a searchable index. Insert your search keywords here: ',
6543                               #line => $token->{line}, column => $token->{column},
6544                              }; # SHOULD
6545              ## TODO: make this configurable              ## TODO: make this configurable
6546            }            }
6547            push @tokens,            push @tokens,
6548                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6549                          #{type => 'character', data => ''}, # SHOULD                           line => $token->{line}, column => $token->{column}},
6550                          {type => 'end tag', tag_name => 'label'},                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6551                          {type => 'end tag', tag_name => 'p'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6552                          {type => 'start tag', tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6553                          {type => 'end tag', tag_name => 'form'};                          {type => END_TAG_TOKEN, tag_name => 'p',
6554            $token = shift @tokens;                           line => $token->{line}, column => $token->{column}},
6555                            {type => START_TAG_TOKEN, tag_name => 'hr',
6556                             line => $token->{line}, column => $token->{column}},
6557                            {type => END_TAG_TOKEN, tag_name => 'form',
6558                             line => $token->{line}, column => $token->{column}};
6559              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6560            !!!back-token (@tokens);            !!!back-token (@tokens);
6561            redo B;            !!!next-token;
6562              next B;
6563          }          }
6564        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6565          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6566          my $el;          my $el;
6567          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6568                    
6569          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6570          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4806  sub _tree_construction_main ($) { Line 6573  sub _tree_construction_main ($) {
6573          $insert->($el);          $insert->($el);
6574                    
6575          my $text = '';          my $text = '';
6576            !!!nack ('t392.1');
6577          !!!next-token;          !!!next-token;
6578          if ($token->{type} eq 'character') {          if ($token->{type} == CHARACTER_TOKEN) {
6579            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
6580            unless (length $token->{data}) {            unless (length $token->{data}) {
6581                !!!cp ('t392');
6582              !!!next-token;              !!!next-token;
6583              } else {
6584                !!!cp ('t393');
6585            }            }
6586            } else {
6587              !!!cp ('t394');
6588          }          }
6589          while ($token->{type} eq 'character') {          while ($token->{type} == CHARACTER_TOKEN) {
6590              !!!cp ('t395');
6591            $text .= $token->{data};            $text .= $token->{data};
6592            !!!next-token;            !!!next-token;
6593          }          }
6594          if (length $text) {          if (length $text) {
6595              !!!cp ('t396');
6596            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6597          }          }
6598                    
6599          $self->{content_model} = PCDATA_CONTENT_MODEL;          $self->{content_model} = PCDATA_CONTENT_MODEL;
6600                    
6601          if ($token->{type} eq 'end tag' and          if ($token->{type} == END_TAG_TOKEN and
6602              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6603              !!!cp ('t397');
6604            ## Ignore the token            ## Ignore the token
6605          } else {          } else {
6606            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6607              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6608          }          }
6609          !!!next-token;          !!!next-token;
6610          redo B;          next B;
6611        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6612                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There are two "as if in body" code clones.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
6613          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6614    
6615            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6616    
6617            ## "adjust foreign attributes" - done in insert-element-f
6618                    
6619          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6620                    
6621          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6622              pop @{$self->{open_elements}};
6623              !!!ack ('t398.1');
6624            } else {
6625              !!!cp ('t398.2');
6626              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6627              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6628              ## mode, "in body" (not "in foreign content") secondary insertion
6629              ## mode, maybe.
6630            }
6631    
6632          !!!next-token;          !!!next-token;
6633          redo B;          next B;
6634        } elsif ({        } elsif ({
6635                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6636                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6637                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6638                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6639                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6640          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
6641            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6642          ## Ignore the token          ## Ignore the token
6643            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6644          !!!next-token;          !!!next-token;
6645          redo B;          next B;
6646                    
6647          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6648        } else {        } else {
6649            if ($token->{tag_name} eq 'image') {
6650              !!!cp ('t384');
6651              !!!parse-error (type => 'image', token => $token);
6652              $token->{tag_name} = 'img';
6653            } else {
6654              !!!cp ('t385');
6655            }
6656    
6657            ## NOTE: There is an "as if <br>" code clone.
6658          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6659                    
6660          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6661    
6662            if ({
6663                 applet => 1, marquee => 1, object => 1,
6664                }->{$token->{tag_name}}) {
6665              !!!cp ('t380');
6666              push @$active_formatting_elements, ['#marker', ''];
6667              !!!nack ('t380.1');
6668            } elsif ({
6669                      b => 1, big => 1, em => 1, font => 1, i => 1,
6670                      s => 1, small => 1, strile => 1,
6671                      strong => 1, tt => 1, u => 1,
6672                     }->{$token->{tag_name}}) {
6673              !!!cp ('t375');
6674              push @$active_formatting_elements, $self->{open_elements}->[-1];
6675              !!!nack ('t375.1');
6676            } elsif ($token->{tag_name} eq 'input') {
6677              !!!cp ('t388');
6678              ## TODO: associate with $self->{form_element} if defined
6679              pop @{$self->{open_elements}};
6680              !!!ack ('t388.2');
6681            } elsif ({
6682                      area => 1, basefont => 1, bgsound => 1, br => 1,
6683                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6684                      #image => 1,
6685                     }->{$token->{tag_name}}) {
6686              !!!cp ('t388.1');
6687              pop @{$self->{open_elements}};
6688              !!!ack ('t388.3');
6689            } elsif ($token->{tag_name} eq 'select') {
6690              ## TODO: associate with $self->{form_element} if defined
6691            
6692              if ($self->{insertion_mode} & TABLE_IMS or
6693                  $self->{insertion_mode} & BODY_TABLE_IMS or
6694                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6695                !!!cp ('t400.1');
6696                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6697              } else {
6698                !!!cp ('t400.2');
6699                $self->{insertion_mode} = IN_SELECT_IM;
6700              }
6701              !!!nack ('t400.3');
6702            } else {
6703              !!!nack ('t402');
6704            }
6705                    
6706          !!!next-token;          !!!next-token;
6707          redo B;          next B;
6708        }        }
6709      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} == END_TAG_TOKEN) {
6710        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6711          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6712              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6713            for (@{$self->{open_elements}}) {          INSCOPE: {
6714              unless ({            for (reverse @{$self->{open_elements}}) {
6715                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6716                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6717                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6718                      }->{$_->[1]}) {                last INSCOPE;
6719                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
6720                  !!!cp ('t405.1');
6721                  last;
6722              }              }
6723            }            }
6724    
6725            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6726            !!!next-token;                            value => $token->{tag_name}, token => $token);
6727            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6728            !!!next-token;            !!!next-token;
6729            redo B;            next B;
6730            } # INSCOPE
6731    
6732            for (@{$self->{open_elements}}) {
6733              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6734                !!!cp ('t403');
6735                !!!parse-error (type => 'not closed',
6736                                value => $_->[0]->manakai_local_name,
6737                                token => $token);
6738                last;
6739              } else {
6740                !!!cp ('t404');
6741              }
6742          }          }
6743    
6744            $self->{insertion_mode} = AFTER_BODY_IM;
6745            !!!next-token;
6746            next B;
6747        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6748          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          ## TODO: Update this code.  It seems that the code below is not
6749            ## up-to-date, though it has same effect as speced.
6750            if (@{$self->{open_elements}} > 1 and
6751                $self->{open_elements}->[1]->[1] & BODY_EL) {
6752            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6753            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6754              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
6755                !!!parse-error (type => 'not closed',
6756                                value => $self->{open_elements}->[1]->[0]
6757                                    ->manakai_local_name,
6758                                token => $token);
6759              } else {
6760                !!!cp ('t407');
6761            }            }
6762            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6763            ## reprocess            ## reprocess
6764            redo B;            next B;
6765          } else {          } else {
6766            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
6767              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6768            ## Ignore the token            ## Ignore the token
6769            !!!next-token;            !!!next-token;
6770            redo B;            next B;
6771          }          }
6772        } elsif ({        } elsif ({
6773                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6774                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6775                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6776                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6777                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6778                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6779          ## has an element in scope          ## has an element in scope
6780          my $i;          my $i;
6781          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6782            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6783            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6784              ## generate implied end tags              !!!cp ('t410');
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6785              $i = $_;              $i = $_;
6786              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6787            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6788                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6789              last INSCOPE;              last INSCOPE;
6790            }            }
6791          } # INSCOPE          } # INSCOPE
6792            
6793          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6794            if (defined $i) {            !!!cp ('t413');
6795              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6796            } else {
6797              ## Step 1. generate implied end tags
6798              while ({
6799                      dd => ($token->{tag_name} ne 'dd'),
6800                      dt => ($token->{tag_name} ne 'dt'),
6801                      li => ($token->{tag_name} ne 'li'),
6802                      p => 1,
6803                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6804                !!!cp ('t409');
6805                pop @{$self->{open_elements}};
6806              }
6807    
6808              ## Step 2.
6809              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6810                      ne $token->{tag_name}) {
6811                !!!cp ('t412');
6812                !!!parse-error (type => 'not closed',
6813                                value => $self->{open_elements}->[-1]->[0]
6814                                    ->manakai_local_name,
6815                                token => $token);
6816            } else {            } else {
6817              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6818            }            }
6819          }  
6820                      ## Step 3.
         if (defined $i) {  
6821            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6822          } elsif ($token->{tag_name} eq 'p') {  
6823            ## As if <p>, then reprocess the current token            ## Step 4.
6824            my $el;            $clear_up_to_marker->()
6825            !!!create-element ($el, 'p');                if {
6826            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
6827                  }->{$token->{tag_name}};
6828          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6829          !!!next-token;          !!!next-token;
6830          redo B;          next B;
6831        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6832            undef $self->{form_element};
6833    
6834          ## has an element in scope          ## has an element in scope
6835            my $i;
6836          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6837            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6838            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6839              ## generate implied end tags              !!!cp ('t418');
6840              if ({              $i = $_;
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6841              last INSCOPE;              last INSCOPE;
6842            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6843                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6844              last INSCOPE;              last INSCOPE;
6845            }            }
6846          } # INSCOPE          } # INSCOPE
6847            
6848          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6849            pop @{$self->{open_elements}};            !!!cp ('t421');
6850              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6851          } else {          } else {
6852            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            ## Step 1. generate implied end tags
6853              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6854                !!!cp ('t417');
6855                pop @{$self->{open_elements}};
6856              }
6857              
6858              ## Step 2.
6859              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6860                      ne $token->{tag_name}) {
6861                !!!cp ('t417.1');
6862                !!!parse-error (type => 'not closed',
6863                                value => $self->{open_elements}->[-1]->[0]
6864                                    ->manakai_local_name,
6865                                token => $token);
6866              } else {
6867                !!!cp ('t420');
6868              }  
6869              
6870              ## Step 3.
6871              splice @{$self->{open_elements}}, $i;
6872          }          }
6873    
         undef $self->{form_element};  
6874          !!!next-token;          !!!next-token;
6875          redo B;          next B;
6876        } elsif ({        } elsif ({
6877                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6878                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5006  sub _tree_construction_main ($) { Line 6880  sub _tree_construction_main ($) {
6880          my $i;          my $i;
6881          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6882            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6883            if ({            if ($node->[1] & HEADING_EL) {
6884                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,              !!!cp ('t423');
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6885              $i = $_;              $i = $_;
6886              last INSCOPE;              last INSCOPE;
6887            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6888                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6889              last INSCOPE;              last INSCOPE;
6890            }            }
6891          } # INSCOPE          } # INSCOPE
6892            
6893          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6894            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!cp ('t425.1');
6895              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6896            } else {
6897              ## Step 1. generate implied end tags
6898              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6899                !!!cp ('t422');
6900                pop @{$self->{open_elements}};
6901              }
6902              
6903              ## Step 2.
6904              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6905                      ne $token->{tag_name}) {
6906                !!!cp ('t425');
6907                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6908              } else {
6909                !!!cp ('t426');
6910              }
6911    
6912              ## Step 3.
6913              splice @{$self->{open_elements}}, $i;
6914          }          }
6915                    
         splice @{$self->{open_elements}}, $i if defined $i;  
6916          !!!next-token;          !!!next-token;
6917          redo B;          next B;
6918          } elsif ($token->{tag_name} eq 'p') {
6919            ## has an element in scope
6920            my $i;
6921            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6922              my $node = $self->{open_elements}->[$_];
6923              if ($node->[1] & P_EL) {
6924                !!!cp ('t410.1');
6925                $i = $_;
6926                last INSCOPE;
6927              } elsif ($node->[1] & SCOPING_EL) {
6928                !!!cp ('t411.1');
6929                last INSCOPE;
6930              }
6931            } # INSCOPE
6932    
6933            if (defined $i) {
6934              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6935                      ne $token->{tag_name}) {
6936                !!!cp ('t412.1');
6937                !!!parse-error (type => 'not closed',
6938                                value => $self->{open_elements}->[-1]->[0]
6939                                    ->manakai_local_name,
6940                                token => $token);
6941              } else {
6942                !!!cp ('t414.1');
6943              }
6944    
6945              splice @{$self->{open_elements}}, $i;
6946            } else {
6947              !!!cp ('t413.1');
6948              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6949    
6950              !!!cp ('t415.1');
6951              ## As if <p>, then reprocess the current token
6952              my $el;
6953              !!!create-element ($el, $HTML_NS, 'p',, $token);
6954              $insert->($el);
6955              ## NOTE: Not inserted into |$self->{open_elements}|.
6956            }
6957    
6958            !!!next-token;
6959            next B;
6960        } elsif ({        } elsif ({
6961                  a => 1,                  a => 1,
6962                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
6963                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
6964                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6965                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6966          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
6967          redo B;          $formatting_end_tag->($token);
6968            next B;
6969        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6970          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
6971            !!!parse-error (type => 'unmatched end tag:br', token => $token);
6972    
6973          ## As if <br>          ## As if <br>
6974          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6975                    
6976          my $el;          my $el;
6977          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
6978          $insert->($el);          $insert->($el);
6979                    
6980          ## Ignore the token.          ## Ignore the token.
6981          !!!next-token;          !!!next-token;
6982          redo B;          next B;
6983        } elsif ({        } elsif ({
6984                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6985                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5070  sub _tree_construction_main ($) { Line 6992  sub _tree_construction_main ($) {
6992                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
6993                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6994                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6995          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
6996            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6997          ## Ignore the token          ## Ignore the token
6998          !!!next-token;          !!!next-token;
6999          redo B;          next B;
7000                    
7001          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7002                    
# Line 5084  sub _tree_construction_main ($) { Line 7007  sub _tree_construction_main ($) {
7007    
7008          ## Step 2          ## Step 2
7009          S2: {          S2: {
7010            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7011              ## Step 1              ## Step 1
7012              ## generate implied end tags              ## generate implied end tags
7013              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7014                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7015                   td => 1, th => 1, tr => 1,                ## ISSUE: Can this case be reached?
7016                   tbody => 1, tfoot=> 1, thead => 1,                pop @{$self->{open_elements}};
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7017              }              }
7018                    
7019              ## Step 2              ## Step 2
7020              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7021                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      ne $token->{tag_name}) {
7022                  !!!cp ('t431');
7023                  ## NOTE: <x><y></x>
7024                  !!!parse-error (type => 'not closed',
7025                                  value => $self->{open_elements}->[-1]->[0]
7026                                      ->manakai_local_name,
7027                                  token => $token);
7028                } else {
7029                  !!!cp ('t432');
7030              }              }
7031                            
7032              ## Step 3              ## Step 3
# Line 5110  sub _tree_construction_main ($) { Line 7036  sub _tree_construction_main ($) {
7036              last S2;              last S2;
7037            } else {            } else {
7038              ## Step 3              ## Step 3
7039              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7040                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7041                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7042                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7043                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7044                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7045                ## Ignore the token                ## Ignore the token
7046                !!!next-token;                !!!next-token;
7047                last S2;                last S2;
7048              }              }
7049    
7050                !!!cp ('t434');
7051            }            }
7052                        
7053            ## Step 4            ## Step 4
# Line 5128  sub _tree_construction_main ($) { Line 7057  sub _tree_construction_main ($) {
7057            ## Step 5;            ## Step 5;
7058            redo S2;            redo S2;
7059          } # S2          } # S2
7060          redo B;          next B;
7061        }        }
7062      }      }
7063      redo B;      next B;
7064      } continue { # B
7065        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7066          ## NOTE: The code below is executed in cases where it does not have
7067          ## to be, but it it is harmless even in those cases.
7068          ## has an element in scope
7069          INSCOPE: {
7070            for (reverse 0..$#{$self->{open_elements}}) {
7071              my $node = $self->{open_elements}->[$_];
7072              if ($node->[1] & FOREIGN_EL) {
7073                last INSCOPE;
7074              } elsif ($node->[1] & SCOPING_EL) {
7075                last;
7076              }
7077            }
7078            
7079            ## NOTE: No foreign element in scope.
7080            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7081          } # INSCOPE
7082        }
7083    } # B    } # B
7084    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
7085    ## Stop parsing # MUST    ## Stop parsing # MUST
7086        
7087    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5150  sub set_inner_html ($$$) { Line 7093  sub set_inner_html ($$$) {
7093    my $s = \$_[0];    my $s = \$_[0];
7094    my $onerror = $_[1];    my $onerror = $_[1];
7095    
7096      ## ISSUE: Should {confident} be true?
7097    
7098    my $nt = $node->node_type;    my $nt = $node->node_type;
7099    if ($nt == 9) {    if ($nt == 9) {
7100      # MUST      # MUST
# Line 5178  sub set_inner_html ($$$) { Line 7123  sub set_inner_html ($$$) {
7123      my $p = $class->new;      my $p = $class->new;
7124      $p->{document} = $doc;      $p->{document} = $doc;
7125    
7126      ## Step 9 # MUST      ## Step 8 # MUST
7127      my $i = 0;      my $i = 0;
7128      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7129      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7130      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7131        my $self = shift;        my $self = shift;
7132    
7133        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7134        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7135    
7136          $self->{next_char} = -1 and return if $i >= length $$s;
7137          $self->{next_char} = ord substr $$s, $i++, 1;
7138    
7139        $self->{next_input_character} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7140        $self->{next_input_character} = ord substr $$s, $i++, 1;        $p->{column}++;
7141        $column++;  
7142          if ($self->{next_char} == 0x000A) { # LF
7143        if ($self->{next_input_character} == 0x000A) { # LF          $p->{line}++;
7144          $line++;          $p->{column} = 0;
7145          $column = 0;          !!!cp ('i1');
7146        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7147          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7148          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7149          $line++;          $p->{line}++;
7150          $column = 0;          $p->{column} = 0;
7151        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7152          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7153        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7154            !!!cp ('i3');
7155          } elsif ($self->{next_char} == 0x0000) { # NULL
7156            !!!cp ('i4');
7157          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7158          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7159          } elsif ($self->{next_char} <= 0x0008 or
7160                   (0x000E <= $self->{next_char} and
7161                    $self->{next_char} <= 0x001F) or
7162                   (0x007F <= $self->{next_char} and
7163                    $self->{next_char} <= 0x009F) or
7164                   (0xD800 <= $self->{next_char} and
7165                    $self->{next_char} <= 0xDFFF) or
7166                   (0xFDD0 <= $self->{next_char} and
7167                    $self->{next_char} <= 0xFDDF) or
7168                   {
7169                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7170                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7171                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7172                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7173                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7174                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7175                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7176                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7177                    0x10FFFE => 1, 0x10FFFF => 1,
7178                   }->{$self->{next_char}}) {
7179            !!!cp ('i4.1');
7180            !!!parse-error (type => 'control char', level => $self->{must_level});
7181    ## TODO: error type documentation
7182        }        }
7183      };      };
7184      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7185      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7186            
7187      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7188        my (%opt) = @_;        my (%opt) = @_;
7189        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7190          my $column = $opt{column};
7191          if (defined $opt{token} and defined $opt{token}->{line}) {
7192            $line = $opt{token}->{line};
7193            $column = $opt{token}->{column};
7194          }
7195          warn "Parse error ($opt{type}) at line $line column $column\n";
7196      };      };
7197      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7198        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7199      };      };
7200            
7201      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7202      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7203    
7204      ## Step 2      ## Step 2
7205      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7206      $p->{content_model} = {      $p->{content_model} = {
7207        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7208        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5239  sub set_inner_html ($$$) { Line 7219  sub set_inner_html ($$$) {
7219          unless defined $p->{content_model};          unless defined $p->{content_model};
7220          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7221    
7222      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7223          ## TODO: Foreign element OK?
7224    
7225      ## Step 4      ## Step 3
7226      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7227        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7228    
7229      ## Step 5 # MUST      ## Step 4 # MUST
7230      $doc->append_child ($root);      $doc->append_child ($root);
7231    
7232      ## Step 6 # MUST      ## Step 5 # MUST
7233      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7234    
7235      undef $p->{head_element};      undef $p->{head_element};
7236    
7237      ## Step 7 # MUST      ## Step 6 # MUST
7238      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7239    
7240      ## Step 8 # MUST      ## Step 7 # MUST
7241      my $anode = $node;      my $anode = $node;
7242      AN: while (defined $anode) {      AN: while (defined $anode) {
7243        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7244          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7245          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7246            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7247                !!!cp ('i5');
7248              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7249              last AN;              last AN;
7250            }            }
# Line 5271  sub set_inner_html ($$$) { Line 7253  sub set_inner_html ($$$) {
7253        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7254      } # AN      } # AN
7255            
7256      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7257      {      {
7258        my $self = $p;        my $self = $p;
7259        !!!next-token;        !!!next-token;
7260      }      }
7261      $p->_tree_construction_main;      $p->_tree_construction_main;
7262    
7263      ## Step 11 # MUST      ## Step 10 # MUST
7264      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7265      for (@cn) {      for (@cn) {
7266        $node->remove_child ($_);        $node->remove_child ($_);
7267      }      }
7268      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7269    
7270      ## Step 12 # MUST      ## Step 11 # MUST
7271      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7272      for (@cn) {      for (@cn) {
7273        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5295  sub set_inner_html ($$$) { Line 7276  sub set_inner_html ($$$) {
7276      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7277    
7278      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7279    
7280        delete $p->{parse_error}; # delete loop
7281    } else {    } else {
7282      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";
7283    }    }
# Line 5302  sub set_inner_html ($$$) { Line 7285  sub set_inner_html ($$$) {
7285    
7286  } # tree construction stage  } # tree construction stage
7287    
7288  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7289    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  
7290    
7291  1;  1;
7292  # $Date$  # $Date$

Legend:
Removed from v.1.54  
changed lines
  Added in v.1.133

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24