/[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.64 by wakaba, Sun Nov 11 08:39:42 2007 UTC revision 1.210 by wakaba, Tue Oct 14 13:24:52 2008 UTC
# Line 3  use strict; Line 3  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);  use Error qw(:try);
5    
6    use Whatpm::HTML::Tokenizer;
7    
8    ## NOTE: This module don't check all HTML5 parse errors; character
9    ## encoding related parse errors are expected to be handled by relevant
10    ## modules.
11    ## Parse errors for control characters that are not allowed in HTML5
12    ## documents, for surrogate code points, and for noncharacter code
13    ## points, as well as U+FFFD substitions for characters whose code points
14    ## is higher than U+10FFFF may be detected by combining the parser with
15    ## the checker implemented by Whatpm::Charset::UnicodeChecker (for its
16    ## usage example, see |t/HTML-tree.t| in the Whatpm package or the
17    ## WebHACC::Language::HTML module in the WebHACC package).
18    
19  ## ISSUE:  ## ISSUE:
20  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
21  ## doc.write ('');  ## doc.write ('');
22  ## alert (doc.compatMode);  ## alert (doc.compatMode);
23    
24  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  require IO::Handle;
25  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  
26  ## is not yet clear.  ## Namespace URLs
27  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  
28  ## "{U+FEFF}..." in GB18030?  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
29    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
30  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
31    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
32    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
33    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
34    hr => 1,  
35    br => 1,  ## Element categories
36    img=> 1,  
37    embed => 1,  ## Bits 12-15
38    param => 1,  sub SPECIAL_EL () { 0b1_000000000000000 }
39    area => 1,  sub SCOPING_EL () { 0b1_00000000000000 }
40    col => 1,  sub FORMATTING_EL () { 0b1_0000000000000 }
41    input => 1,  sub PHRASING_EL () { 0b1_000000000000 }
42    
43    ## Bits 10-11
44    #sub FOREIGN_EL () { 0b1_00000000000 } # see Whatpm::HTML::Tokenizer
45    sub FOREIGN_FLOW_CONTENT_EL () { 0b1_0000000000 }
46    
47    ## Bits 6-9
48    sub TABLE_SCOPING_EL () { 0b1_000000000 }
49    sub TABLE_ROWS_SCOPING_EL () { 0b1_00000000 }
50    sub TABLE_ROW_SCOPING_EL () { 0b1_0000000 }
51    sub TABLE_ROWS_EL () { 0b1_000000 }
52    
53    ## Bit 5
54    sub ADDRESS_DIV_P_EL () { 0b1_00000 }
55    
56    ## NOTE: Used in </body> and EOF algorithms.
57    ## Bit 4
58    sub ALL_END_TAG_OPTIONAL_EL () { 0b1_0000 }
59    
60    ## NOTE: Used in "generate implied end tags" algorithm.
61    ## NOTE: There is a code where a modified version of
62    ## END_TAG_OPTIONAL_EL is used in "generate implied end tags"
63    ## implementation (search for the algorithm name).
64    ## Bit 3
65    sub END_TAG_OPTIONAL_EL () { 0b1_000 }
66    
67    ## Bits 0-2
68    
69    sub MISC_SPECIAL_EL () { SPECIAL_EL | 0b000 }
70    sub FORM_EL () { SPECIAL_EL | 0b001 }
71    sub FRAMESET_EL () { SPECIAL_EL | 0b010 }
72    sub HEADING_EL () { SPECIAL_EL | 0b011 }
73    sub SELECT_EL () { SPECIAL_EL | 0b100 }
74    sub SCRIPT_EL () { SPECIAL_EL | 0b101 }
75    
76    sub ADDRESS_DIV_EL () { SPECIAL_EL | ADDRESS_DIV_P_EL | 0b001 }
77    sub BODY_EL () { SPECIAL_EL | ALL_END_TAG_OPTIONAL_EL | 0b001 }
78    
79    sub DTDD_EL () {
80      SPECIAL_EL |
81      END_TAG_OPTIONAL_EL |
82      ALL_END_TAG_OPTIONAL_EL |
83      0b010
84    }
85    sub LI_EL () {
86      SPECIAL_EL |
87      END_TAG_OPTIONAL_EL |
88      ALL_END_TAG_OPTIONAL_EL |
89      0b100
90    }
91    sub P_EL () {
92      SPECIAL_EL |
93      ADDRESS_DIV_P_EL |
94      END_TAG_OPTIONAL_EL |
95      ALL_END_TAG_OPTIONAL_EL |
96      0b001
97    }
98    
99    sub TABLE_ROW_EL () {
100      SPECIAL_EL |
101      TABLE_ROWS_EL |
102      TABLE_ROW_SCOPING_EL |
103      ALL_END_TAG_OPTIONAL_EL |
104      0b001
105    }
106    sub TABLE_ROW_GROUP_EL () {
107      SPECIAL_EL |
108      TABLE_ROWS_EL |
109      TABLE_ROWS_SCOPING_EL |
110      ALL_END_TAG_OPTIONAL_EL |
111      0b001
112    }
113    
114    sub MISC_SCOPING_EL () { SCOPING_EL | 0b000 }
115    sub BUTTON_EL () { SCOPING_EL | 0b001 }
116    sub CAPTION_EL () { SCOPING_EL | 0b010 }
117    sub HTML_EL () {
118      SCOPING_EL |
119      TABLE_SCOPING_EL |
120      TABLE_ROWS_SCOPING_EL |
121      TABLE_ROW_SCOPING_EL |
122      ALL_END_TAG_OPTIONAL_EL |
123      0b001
124    }
125    sub TABLE_EL () {
126      SCOPING_EL |
127      TABLE_ROWS_EL |
128      TABLE_SCOPING_EL |
129      0b001
130    }
131    sub TABLE_CELL_EL () {
132      SCOPING_EL |
133      TABLE_ROW_SCOPING_EL |
134      ALL_END_TAG_OPTIONAL_EL |
135      0b001
136    }
137    
138    sub MISC_FORMATTING_EL () { FORMATTING_EL | 0b000 }
139    sub A_EL () { FORMATTING_EL | 0b001 }
140    sub NOBR_EL () { FORMATTING_EL | 0b010 }
141    
142    sub RUBY_EL () { PHRASING_EL | 0b001 }
143    
144    ## ISSUE: ALL_END_TAG_OPTIONAL_EL?
145    sub OPTGROUP_EL () { PHRASING_EL | END_TAG_OPTIONAL_EL | 0b001 }
146    sub OPTION_EL () { PHRASING_EL | END_TAG_OPTIONAL_EL | 0b010 }
147    sub RUBY_COMPONENT_EL () { PHRASING_EL | END_TAG_OPTIONAL_EL | 0b100 }
148    
149    sub MML_AXML_EL () { PHRASING_EL | FOREIGN_EL | 0b001 }
150    
151    my $el_category = {
152      a => A_EL,
153      address => ADDRESS_DIV_EL,
154      applet => MISC_SCOPING_EL,
155      area => MISC_SPECIAL_EL,
156      article => MISC_SPECIAL_EL,
157      aside => MISC_SPECIAL_EL,
158      b => FORMATTING_EL,
159      base => MISC_SPECIAL_EL,
160      basefont => MISC_SPECIAL_EL,
161      bgsound => MISC_SPECIAL_EL,
162      big => FORMATTING_EL,
163      blockquote => MISC_SPECIAL_EL,
164      body => BODY_EL,
165      br => MISC_SPECIAL_EL,
166      button => BUTTON_EL,
167      caption => CAPTION_EL,
168      center => MISC_SPECIAL_EL,
169      col => MISC_SPECIAL_EL,
170      colgroup => MISC_SPECIAL_EL,
171      command => MISC_SPECIAL_EL,
172      datagrid => MISC_SPECIAL_EL,
173      dd => DTDD_EL,
174      details => MISC_SPECIAL_EL,
175      dialog => MISC_SPECIAL_EL,
176      dir => MISC_SPECIAL_EL,
177      div => ADDRESS_DIV_EL,
178      dl => MISC_SPECIAL_EL,
179      dt => DTDD_EL,
180      em => FORMATTING_EL,
181      embed => MISC_SPECIAL_EL,
182      eventsource => MISC_SPECIAL_EL,
183      fieldset => MISC_SPECIAL_EL,
184      figure => MISC_SPECIAL_EL,
185      font => FORMATTING_EL,
186      footer => MISC_SPECIAL_EL,
187      form => FORM_EL,
188      frame => MISC_SPECIAL_EL,
189      frameset => FRAMESET_EL,
190      h1 => HEADING_EL,
191      h2 => HEADING_EL,
192      h3 => HEADING_EL,
193      h4 => HEADING_EL,
194      h5 => HEADING_EL,
195      h6 => HEADING_EL,
196      head => MISC_SPECIAL_EL,
197      header => MISC_SPECIAL_EL,
198      hr => MISC_SPECIAL_EL,
199      html => HTML_EL,
200      i => FORMATTING_EL,
201      iframe => MISC_SPECIAL_EL,
202      img => MISC_SPECIAL_EL,
203      #image => MISC_SPECIAL_EL, ## NOTE: Commented out in the spec.
204      input => MISC_SPECIAL_EL,
205      isindex => MISC_SPECIAL_EL,
206      li => LI_EL,
207      link => MISC_SPECIAL_EL,
208      listing => MISC_SPECIAL_EL,
209      marquee => MISC_SCOPING_EL,
210      menu => MISC_SPECIAL_EL,
211      meta => MISC_SPECIAL_EL,
212      nav => MISC_SPECIAL_EL,
213      nobr => NOBR_EL,
214      noembed => MISC_SPECIAL_EL,
215      noframes => MISC_SPECIAL_EL,
216      noscript => MISC_SPECIAL_EL,
217      object => MISC_SCOPING_EL,
218      ol => MISC_SPECIAL_EL,
219      optgroup => OPTGROUP_EL,
220      option => OPTION_EL,
221      p => P_EL,
222      param => MISC_SPECIAL_EL,
223      plaintext => MISC_SPECIAL_EL,
224      pre => MISC_SPECIAL_EL,
225      rp => RUBY_COMPONENT_EL,
226      rt => RUBY_COMPONENT_EL,
227      ruby => RUBY_EL,
228      s => FORMATTING_EL,
229      script => MISC_SPECIAL_EL,
230      select => SELECT_EL,
231      section => MISC_SPECIAL_EL,
232      small => FORMATTING_EL,
233      spacer => MISC_SPECIAL_EL,
234      strike => FORMATTING_EL,
235      strong => FORMATTING_EL,
236      style => MISC_SPECIAL_EL,
237      table => TABLE_EL,
238      tbody => TABLE_ROW_GROUP_EL,
239      td => TABLE_CELL_EL,
240      textarea => MISC_SPECIAL_EL,
241      tfoot => TABLE_ROW_GROUP_EL,
242      th => TABLE_CELL_EL,
243      thead => TABLE_ROW_GROUP_EL,
244      title => MISC_SPECIAL_EL,
245      tr => TABLE_ROW_EL,
246      tt => FORMATTING_EL,
247      u => FORMATTING_EL,
248      ul => MISC_SPECIAL_EL,
249      wbr => MISC_SPECIAL_EL,
250  };  };
251    
252  my $c1_entity_char = {  my $el_category_f = {
253    0x80 => 0x20AC,    $MML_NS => {
254    0x81 => 0xFFFD,      'annotation-xml' => MML_AXML_EL,
255    0x82 => 0x201A,      mi => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
256    0x83 => 0x0192,      mo => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
257    0x84 => 0x201E,      mn => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
258    0x85 => 0x2026,      ms => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
259    0x86 => 0x2020,      mtext => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
260    0x87 => 0x2021,    },
261    0x88 => 0x02C6,    $SVG_NS => {
262    0x89 => 0x2030,      foreignObject => SCOPING_EL | FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
263    0x8A => 0x0160,      desc => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
264    0x8B => 0x2039,      title => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
265    0x8C => 0x0152,    },
266    0x8D => 0xFFFD,    ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
   0x8E => 0x017D,  
   0x8F => 0xFFFD,  
   0x90 => 0xFFFD,  
   0x91 => 0x2018,  
   0x92 => 0x2019,  
   0x93 => 0x201C,  
   0x94 => 0x201D,  
   0x95 => 0x2022,  
   0x96 => 0x2013,  
   0x97 => 0x2014,  
   0x98 => 0x02DC,  
   0x99 => 0x2122,  
   0x9A => 0x0161,  
   0x9B => 0x203A,  
   0x9C => 0x0153,  
   0x9D => 0xFFFD,  
   0x9E => 0x017E,  
   0x9F => 0x0178,  
 }; # $c1_entity_char  
   
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
267  };  };
268  my $scoping_category = {  
269    button => 1, caption => 1, html => 1, marquee => 1, object => 1,  my $svg_attr_name = {
270    table => 1, td => 1, th => 1,    attributename => 'attributeName',
271      attributetype => 'attributeType',
272      basefrequency => 'baseFrequency',
273      baseprofile => 'baseProfile',
274      calcmode => 'calcMode',
275      clippathunits => 'clipPathUnits',
276      contentscripttype => 'contentScriptType',
277      contentstyletype => 'contentStyleType',
278      diffuseconstant => 'diffuseConstant',
279      edgemode => 'edgeMode',
280      externalresourcesrequired => 'externalResourcesRequired',
281      filterres => 'filterRes',
282      filterunits => 'filterUnits',
283      glyphref => 'glyphRef',
284      gradienttransform => 'gradientTransform',
285      gradientunits => 'gradientUnits',
286      kernelmatrix => 'kernelMatrix',
287      kernelunitlength => 'kernelUnitLength',
288      keypoints => 'keyPoints',
289      keysplines => 'keySplines',
290      keytimes => 'keyTimes',
291      lengthadjust => 'lengthAdjust',
292      limitingconeangle => 'limitingConeAngle',
293      markerheight => 'markerHeight',
294      markerunits => 'markerUnits',
295      markerwidth => 'markerWidth',
296      maskcontentunits => 'maskContentUnits',
297      maskunits => 'maskUnits',
298      numoctaves => 'numOctaves',
299      pathlength => 'pathLength',
300      patterncontentunits => 'patternContentUnits',
301      patterntransform => 'patternTransform',
302      patternunits => 'patternUnits',
303      pointsatx => 'pointsAtX',
304      pointsaty => 'pointsAtY',
305      pointsatz => 'pointsAtZ',
306      preservealpha => 'preserveAlpha',
307      preserveaspectratio => 'preserveAspectRatio',
308      primitiveunits => 'primitiveUnits',
309      refx => 'refX',
310      refy => 'refY',
311      repeatcount => 'repeatCount',
312      repeatdur => 'repeatDur',
313      requiredextensions => 'requiredExtensions',
314      requiredfeatures => 'requiredFeatures',
315      specularconstant => 'specularConstant',
316      specularexponent => 'specularExponent',
317      spreadmethod => 'spreadMethod',
318      startoffset => 'startOffset',
319      stddeviation => 'stdDeviation',
320      stitchtiles => 'stitchTiles',
321      surfacescale => 'surfaceScale',
322      systemlanguage => 'systemLanguage',
323      tablevalues => 'tableValues',
324      targetx => 'targetX',
325      targety => 'targetY',
326      textlength => 'textLength',
327      viewbox => 'viewBox',
328      viewtarget => 'viewTarget',
329      xchannelselector => 'xChannelSelector',
330      ychannelselector => 'yChannelSelector',
331      zoomandpan => 'zoomAndPan',
332  };  };
333  my $formatting_category = {  
334    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  my $foreign_attr_xname = {
335    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,    'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
336      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
337      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
338      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
339      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
340      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
341      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
342      'xml:base' => [$XML_NS, ['xml', 'base']],
343      'xml:lang' => [$XML_NS, ['xml', 'lang']],
344      'xml:space' => [$XML_NS, ['xml', 'space']],
345      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
346      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
347  };  };
348  # $phrasing_category: all other elements  
349    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
350    
351    ## TODO: Invoke the reset algorithm when a resettable element is
352    ## created (cf. HTML5 revision 2259).
353    
354  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
355      my $self = shift;
356      my $charset_name = shift;
357      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
358      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
359    } # parse_byte_string
360    
361    sub parse_byte_stream ($$$$;$$) {
362      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
363    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
364    my $charset = shift;    my $charset_name = shift;
365    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     $charset = 'windows-1252'; ## TODO: for now.  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
366    
367    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
368      my $self = shift;      my (%opt) = @_;
369      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
370      ## TODO: if $charset is supported    };
371      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
372    
373      ## "Change the encoding" algorithm:    my $get_wrapper = $_[3] || sub ($) {
374        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
375      ## Step 1        };
376      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?  
377        $charset = 'utf-8';    ## HTML5 encoding sniffing algorithm
378      require Message::Charset::Info;
379      my $charset;
380      my $buffer;
381      my ($char_stream, $e_status);
382    
383      SNIFFING: {
384        ## NOTE: By setting |allow_fallback| option true when the
385        ## |get_decode_handle| method is invoked, we ignore what the HTML5
386        ## spec requires, i.e. unsupported encoding should be ignored.
387          ## TODO: We should not do this unless the parser is invoked
388          ## in the conformance checking mode, in which this behavior
389          ## would be useful.
390    
391        ## Step 1
392        if (defined $charset_name) {
393          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
394              ## TODO: Is this ok?  Transfer protocol's parameter should be
395              ## interpreted in its semantics?
396    
397          ($char_stream, $e_status) = $charset->get_decode_handle
398              ($byte_stream, allow_error_reporting => 1,
399               allow_fallback => 1);
400          if ($char_stream) {
401            $self->{confident} = 1;
402            last SNIFFING;
403          } else {
404            !!!parse-error (type => 'charset:not supported',
405                            layer => 'encode',
406                            line => 1, column => 1,
407                            value => $charset_name,
408                            level => $self->{level}->{uncertain});
409          }
410      }      }
411    
412      ## Step 2      ## Step 2
413      if (defined $self->{input_encoding} and      my $byte_buffer = '';
414          $self->{input_encoding} eq $charset) {      for (1..1024) {
415          my $char = $byte_stream->getc;
416          last unless defined $char;
417          $byte_buffer .= $char;
418        } ## TODO: timeout
419    
420        ## Step 3
421        if ($byte_buffer =~ /^\xFE\xFF/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426        $self->{confident} = 1;        $self->{confident} = 1;
427        return;        last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
436          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
437          ($char_stream, $e_status) = $charset->get_decode_handle
438              ($byte_stream, allow_error_reporting => 1,
439               allow_fallback => 1, byte_buffer => \$byte_buffer);
440          $self->{confident} = 1;
441          last SNIFFING;
442      }      }
443    
444      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
445          ':'.$charset, level => 'w');      ## TODO: <meta charset>
446    
447      ## Step 3      ## Step 5
448      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
449    
450      ## Step 4      ## Step 6
451      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
452        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
453            ($byte_buffer);
454        if (defined $charset_name) {
455          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
456    
457          require Whatpm::Charset::DecodeHandle;
458          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
459              ($byte_stream);
460          ($char_stream, $e_status) = $charset->get_decode_handle
461              ($buffer, allow_error_reporting => 1,
462               allow_fallback => 1, byte_buffer => \$byte_buffer);
463          if ($char_stream) {
464            $buffer->{buffer} = $byte_buffer;
465            !!!parse-error (type => 'sniffing:chardet',
466                            text => $charset_name,
467                            level => $self->{level}->{info},
468                            layer => 'encode',
469                            line => 1, column => 1);
470            $self->{confident} = 0;
471            last SNIFFING;
472          }
473        }
474    
475        ## Step 7: default
476        ## TODO: Make this configurable.
477        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
478            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
479            ## detectable in the step 6.
480        require Whatpm::Charset::DecodeHandle;
481        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
482            ($byte_stream);
483        ($char_stream, $e_status)
484            = $charset->get_decode_handle ($buffer,
485                                           allow_error_reporting => 1,
486                                           allow_fallback => 1,
487                                           byte_buffer => \$byte_buffer);
488        $buffer->{buffer} = $byte_buffer;
489        !!!parse-error (type => 'sniffing:default',
490                        text => 'windows-1252',
491                        level => $self->{level}->{info},
492                        line => 1, column => 1,
493                        layer => 'encode');
494        $self->{confident} = 0;
495      } # SNIFFING
496    
497      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
498        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
499        !!!parse-error (type => 'chardecode:fallback',
500                        #text => $self->{input_encoding},
501                        level => $self->{level}->{uncertain},
502                        line => 1, column => 1,
503                        layer => 'encode');
504      } elsif (not ($e_status &
505                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
506        $self->{input_encoding} = $charset->get_iana_name;
507        !!!parse-error (type => 'chardecode:no error',
508                        text => $self->{input_encoding},
509                        level => $self->{level}->{uncertain},
510                        line => 1, column => 1,
511                        layer => 'encode');
512      } else {
513        $self->{input_encoding} = $charset->get_iana_name;
514      }
515    
516      $self->{change_encoding} = sub {
517        my $self = shift;
518        $charset_name = shift;
519        my $token = shift;
520    
521        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
522        ($char_stream, $e_status) = $charset->get_decode_handle
523            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
524             byte_buffer => \ $buffer->{buffer});
525        
526        if ($char_stream) { # if supported
527          ## "Change the encoding" algorithm:
528    
529          ## Step 1    
530          if ($charset->{category} &
531              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
532            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
533            ($char_stream, $e_status) = $charset->get_decode_handle
534                ($byte_stream,
535                 byte_buffer => \ $buffer->{buffer});
536          }
537          $charset_name = $charset->get_iana_name;
538          
539          ## Step 2
540          if (defined $self->{input_encoding} and
541              $self->{input_encoding} eq $charset_name) {
542            !!!parse-error (type => 'charset label:matching',
543                            text => $charset_name,
544                            level => $self->{level}->{info});
545            $self->{confident} = 1;
546            return;
547          }
548    
549          !!!parse-error (type => 'charset label detected',
550                          text => $self->{input_encoding},
551                          value => $charset_name,
552                          level => $self->{level}->{warn},
553                          token => $token);
554          
555          ## Step 3
556          # if (can) {
557            ## change the encoding on the fly.
558            #$self->{confident} = 1;
559            #return;
560          # }
561          
562          ## Step 4
563          throw Whatpm::HTML::RestartParser ();
564        }
565    }; # $self->{change_encoding}    }; # $self->{change_encoding}
566    
567    my @args = @_; shift @args; # $s    my $char_onerror = sub {
568        my (undef, $type, %opt) = @_;
569        !!!parse-error (layer => 'encode',
570                        line => $self->{line}, column => $self->{column} + 1,
571                        %opt, type => $type);
572        if ($opt{octets}) {
573          ${$opt{octets}} = "\x{FFFD}"; # relacement character
574        }
575      };
576    
577      my $wrapped_char_stream = $get_wrapper->($char_stream);
578      $wrapped_char_stream->onerror ($char_onerror);
579    
580      my @args = ($_[1], $_[2]); # $doc, $onerror - $get_wrapper = undef;
581    my $return;    my $return;
582    try {    try {
583      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
584    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
585      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
586      $s = \ (Encode::decode ($charset, $$bytes_s));      
587      $self->{input_encoding} = $charset; ## TODO: normalize      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
588          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
589          !!!parse-error (type => 'chardecode:fallback',
590                          level => $self->{level}->{uncertain},
591                          #text => $self->{input_encoding},
592                          line => 1, column => 1,
593                          layer => 'encode');
594        } elsif (not ($e_status &
595                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
596          $self->{input_encoding} = $charset->get_iana_name;
597          !!!parse-error (type => 'chardecode:no error',
598                          text => $self->{input_encoding},
599                          level => $self->{level}->{uncertain},
600                          line => 1, column => 1,
601                          layer => 'encode');
602        } else {
603          $self->{input_encoding} = $charset->get_iana_name;
604        }
605      $self->{confident} = 1;      $self->{confident} = 1;
606      $return = $self->parse_char_string ($s, @args);  
607        $wrapped_char_stream = $get_wrapper->($char_stream);
608        $wrapped_char_stream->onerror ($char_onerror);
609    
610        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
611    };    };
612    return $return;    return $return;
613  } # parse_byte_string  } # parse_byte_stream
614    
615  *parse_char_string = \&parse_string;  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
616    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
617    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
618    ## because the core part of our HTML parser expects a string of character,
619    ## not a string of bytes or code units or anything which might contain a BOM.
620    ## Therefore, any parser interface that accepts a string of bytes,
621    ## such as |parse_byte_string| in this module, must ensure that it does
622    ## strip the BOM and never strip any ZWNBSP.
623    
624  sub parse_string ($$$;$) {  sub parse_char_string ($$$;$$) {
625    my $self = ref $_[0] ? shift : shift->new;    #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
626      my $self = shift;
627    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $s = ref $_[0] ? $_[0] : \($_[0]);
628      require Whatpm::Charset::DecodeHandle;
629      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
630      return $self->parse_char_stream ($input, @_[1..$#_]);
631    } # parse_char_string
632    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
633    
634    sub parse_char_stream ($$$;$$) {
635      my $self = ref $_[0] ? shift : shift->new;
636      my $input = $_[0];
637    $self->{document} = $_[1];    $self->{document} = $_[1];
638    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
639    
# Line 164  sub parse_string ($$$;$) { Line 642  sub parse_string ($$$;$) {
642    $self->{confident} = 1 unless exists $self->{confident};    $self->{confident} = 1 unless exists $self->{confident};
643    $self->{document}->input_encoding ($self->{input_encoding})    $self->{document}->input_encoding ($self->{input_encoding})
644        if defined $self->{input_encoding};        if defined $self->{input_encoding};
645    ## TODO: |{input_encoding}| is needless?
646    
647    my $i = 0;    $self->{line_prev} = $self->{line} = 1;
648    my $line = 1;    $self->{column_prev} = -1;
649    my $column = 0;    $self->{column} = 0;
650    $self->{set_next_input_character} = sub {    $self->{set_nc} = sub {
651      my $self = shift;      my $self = shift;
652    
653      pop @{$self->{prev_input_character}};      my $char = '';
654      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      if (defined $self->{next_nc}) {
655          $char = $self->{next_nc};
656          delete $self->{next_nc};
657          $self->{nc} = ord $char;
658        } else {
659          $self->{char_buffer} = '';
660          $self->{char_buffer_pos} = 0;
661    
662          my $count = $input->manakai_read_until
663             ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/, $self->{char_buffer_pos});
664          if ($count) {
665            $self->{line_prev} = $self->{line};
666            $self->{column_prev} = $self->{column};
667            $self->{column}++;
668            $self->{nc}
669                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
670            return;
671          }
672    
673          if ($input->read ($char, 1)) {
674            $self->{nc} = ord $char;
675          } else {
676            $self->{nc} = -1;
677            return;
678          }
679        }
680    
681      $self->{next_input_character} = -1 and return if $i >= length $$s;      ($self->{line_prev}, $self->{column_prev})
682      $self->{next_input_character} = ord substr $$s, $i++, 1;          = ($self->{line}, $self->{column});
683      $column++;      $self->{column}++;
684            
685      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{nc} == 0x000A) { # LF
686        $line++;        !!!cp ('j1');
687        $column = 0;        $self->{line}++;
688      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
689        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{nc} == 0x000D) { # CR
690        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
691        $line++;  ## TODO: support for abort/streaming
692        $column = 0;        my $next = '';
693      } elsif ($self->{next_input_character} > 0x10FFFF) {        if ($input->read ($next, 1) and $next ne "\x0A") {
694        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_nc} = $next;
695      } elsif ($self->{next_input_character} == 0x0000) { # NULL        }
696          $self->{nc} = 0x000A; # LF # MUST
697          $self->{line}++;
698          $self->{column} = 0;
699        } elsif ($self->{nc} == 0x0000) { # NULL
700          !!!cp ('j4');
701        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
702        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
703      }      }
704    };    };
705    $self->{prev_input_character} = [-1, -1, -1];  
706    $self->{next_input_character} = -1;    $self->{read_until} = sub {
707        #my ($scalar, $specials_range, $offset) = @_;
708        return 0 if defined $self->{next_nc};
709    
710        my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
711        my $offset = $_[2] || 0;
712    
713        if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
714          pos ($self->{char_buffer}) = $self->{char_buffer_pos};
715          if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
716            substr ($_[0], $offset)
717                = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
718            my $count = $+[0] - $-[0];
719            if ($count) {
720              $self->{column} += $count;
721              $self->{char_buffer_pos} += $count;
722              $self->{line_prev} = $self->{line};
723              $self->{column_prev} = $self->{column} - 1;
724              $self->{nc} = -1;
725            }
726            return $count;
727          } else {
728            return 0;
729          }
730        } else {
731          my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
732          if ($count) {
733            $self->{column} += $count;
734            $self->{line_prev} = $self->{line};
735            $self->{column_prev} = $self->{column} - 1;
736            $self->{nc} = -1;
737          }
738          return $count;
739        }
740      }; # $self->{read_until}
741    
742    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
743      my (%opt) = @_;      my (%opt) = @_;
744      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
745        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
746        warn "Parse error ($opt{type}) at line $line column $column\n";
747    };    };
748    $self->{parse_error} = sub {    $self->{parse_error} = sub {
749      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
750    };    };
751    
752      my $char_onerror = sub {
753        my (undef, $type, %opt) = @_;
754        !!!parse-error (layer => 'encode',
755                        line => $self->{line}, column => $self->{column} + 1,
756                        %opt, type => $type);
757      }; # $char_onerror
758    
759      if ($_[3]) {
760        $input = $_[3]->($input);
761        $input->onerror ($char_onerror);
762      } else {
763        $input->onerror ($char_onerror) unless defined $input->onerror;
764      }
765    
766    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
767    $self->_initialize_tree_constructor;    $self->_initialize_tree_constructor;
768    $self->_construct_tree;    $self->_construct_tree;
769    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
770    
771      delete $self->{parse_error}; # remove loop
772    
773    return $self->{document};    return $self->{document};
774  } # parse_string  } # parse_char_stream
775    
776  sub new ($) {  sub new ($) {
777    my $class = shift;    my $class = shift;
778    my $self = bless {}, $class;    my $self = bless {
779    $self->{set_next_input_character} = sub {      level => {must => 'm',
780      $self->{next_input_character} = -1;                should => 's',
781                  warn => 'w',
782                  info => 'i',
783                  uncertain => 'u'},
784      }, $class;
785      $self->{set_nc} = sub {
786        $self->{nc} = -1;
787    };    };
788    $self->{parse_error} = sub {    $self->{parse_error} = sub {
789      #      #
# Line 233  sub new ($) { Line 800  sub new ($) {
800    return $self;    return $self;
801  } # new  } # new
802    
803  sub CM_ENTITY () { 0b001 } # & markup in data  ## Insertion modes
 sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)  
 sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)  
   
 sub PLAINTEXT_CONTENT_MODEL () { 0 }  
 sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }  
 sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  
 sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  
   
 sub DATA_STATE () { 0 }  
 sub ENTITY_DATA_STATE () { 1 }  
 sub TAG_OPEN_STATE () { 2 }  
 sub CLOSE_TAG_OPEN_STATE () { 3 }  
 sub TAG_NAME_STATE () { 4 }  
 sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }  
 sub ATTRIBUTE_NAME_STATE () { 6 }  
 sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }  
 sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }  
 sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  
 sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  
 sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  
 sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  
 sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  
 sub COMMENT_START_STATE () { 14 }  
 sub COMMENT_START_DASH_STATE () { 15 }  
 sub COMMENT_STATE () { 16 }  
 sub COMMENT_END_STATE () { 17 }  
 sub COMMENT_END_DASH_STATE () { 18 }  
 sub BOGUS_COMMENT_STATE () { 19 }  
 sub DOCTYPE_STATE () { 20 }  
 sub BEFORE_DOCTYPE_NAME_STATE () { 21 }  
 sub DOCTYPE_NAME_STATE () { 22 }  
 sub AFTER_DOCTYPE_NAME_STATE () { 23 }  
 sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }  
 sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }  
 sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }  
 sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }  
 sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }  
 sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }  
 sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }  
 sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  
 sub BOGUS_DOCTYPE_STATE () { 32 }  
   
 sub DOCTYPE_TOKEN () { 1 }  
 sub COMMENT_TOKEN () { 2 }  
 sub START_TAG_TOKEN () { 3 }  
 sub END_TAG_TOKEN () { 4 }  
 sub END_OF_FILE_TOKEN () { 5 }  
 sub CHARACTER_TOKEN () { 6 }  
804    
805  sub AFTER_HTML_IMS () { 0b100 }  sub AFTER_HTML_IMS () { 0b100 }
806  sub HEAD_IMS ()       { 0b1000 }  sub HEAD_IMS ()       { 0b1000 }
# Line 291  sub TABLE_IMS ()      { 0b1000000 } Line 810  sub TABLE_IMS ()      { 0b1000000 }
810  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
811  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
812  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
813    sub SELECT_IMS ()     { 0b10000000000 }
814    #sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 } # see Whatpm::HTML::Tokenizer
815        ## NOTE: "in foreign content" insertion mode is special; it is combined
816        ## with the secondary insertion mode.  In this parser, they are stored
817        ## together in the bit-or'ed form.
818    sub IN_CDATA_RCDATA_IM () { 0b1000000000000 }
819        ## NOTE: "in CDATA/RCDATA" insertion mode is also special; it is
820        ## combined with the original insertion mode.  In thie parser,
821        ## they are stored together in the bit-or'ed form.
822    
823    sub IM_MASK () { 0b11111111111 }
824    
825    ## NOTE: "initial" and "before html" insertion modes have no constants.
826    
827    ## NOTE: "after after body" insertion mode.
828  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
829    
830    ## NOTE: "after after frameset" insertion mode.
831  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
832    
833  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
834  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
835  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 307  sub IN_TABLE_IM () { TABLE_IMS } Line 843  sub IN_TABLE_IM () { TABLE_IMS }
843  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
844  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
845  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
846  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
847    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
848  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
849    
 ## Implementations MUST act as if state machine in the spec  
   
 sub _initialize_tokenizer ($) {  
   my $self = shift;  
   $self->{state} = DATA_STATE; # MUST  
   $self->{content_model} = PCDATA_CONTENT_MODEL; # be  
   undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE  
   undef $self->{current_attribute};  
   undef $self->{last_emitted_start_tag_name};  
   undef $self->{last_attribute_value_state};  
   $self->{char} = [];  
   # $self->{next_input_character}  
   !!!next-input-character;  
   $self->{token} = [];  
   # $self->{escape}  
 } # _initialize_tokenizer  
   
 ## A token has:  
 ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,  
 ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN  
 ##   ->{name} (DOCTYPE_TOKEN)  
 ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  
 ##   ->{public_identifier} (DOCTYPE_TOKEN)  
 ##   ->{system_identifier} (DOCTYPE_TOKEN)  
 ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  
 ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  
 ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  
   
 ## Emitted token MUST immediately be handled by the tree construction state.  
   
 ## Before each step, UA MAY check to see if either one of the scripts in  
 ## "list of scripts that will execute as soon as possible" or the first  
 ## script in the "list of scripts that will execute asynchronously",  
 ## has completed loading.  If one has, then it MUST be executed  
 ## and removed from the list.  
   
 ## NOTE: HTML5 "Writing HTML documents" section, applied to  
 ## documents and not to user agents and conformance checkers,  
 ## contains some requirements that are not detected by the  
 ## parsing algorithm:  
 ## - Some requirements on character encoding declarations. ## TODO  
 ## - "Elements MUST NOT contain content that their content model disallows."  
 ##   ... Some are parse error, some are not (will be reported by c.c.).  
 ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO  
 ## - Text (in elements, attributes, and comments) SHOULD NOT contain  
 ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)  
   
 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot  
 ## be detected by the HTML5 parsing algorithm:  
 ## - Text,  
   
 sub _get_next_token ($) {  
   my $self = shift;  
   if (@{$self->{token}}) {  
     return shift @{$self->{token}};  
   }  
   
   A: {  
     if ($self->{state} == DATA_STATE) {  
       if ($self->{next_input_character} == 0x0026) { # &  
         if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA  
           $self->{state} = ENTITY_DATA_STATE;  
           !!!next-input-character;  
           redo A;  
         } else {  
           #  
         }  
       } elsif ($self->{next_input_character} == 0x002D) { # -  
         if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA  
           unless ($self->{escape}) {  
             if ($self->{prev_input_character}->[0] == 0x002D and # -  
                 $self->{prev_input_character}->[1] == 0x0021 and # !  
                 $self->{prev_input_character}->[2] == 0x003C) { # <  
               $self->{escape} = 1;  
             }  
           }  
         }  
           
         #  
       } elsif ($self->{next_input_character} == 0x003C) { # <  
         if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA  
             (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA  
              not $self->{escape})) {  
           $self->{state} = TAG_OPEN_STATE;  
           !!!next-input-character;  
           redo A;  
         } else {  
           #  
         }  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         if ($self->{escape} and  
             ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA  
           if ($self->{prev_input_character}->[0] == 0x002D and # -  
               $self->{prev_input_character}->[1] == 0x002D) { # -  
             delete $self->{escape};  
           }  
         }  
           
         #  
       } elsif ($self->{next_input_character} == -1) {  
         !!!emit ({type => END_OF_FILE_TOKEN});  
         last A; ## TODO: ok?  
       }  
       # Anything else  
       my $token = {type => CHARACTER_TOKEN,  
                    data => chr $self->{next_input_character}};  
       ## Stay in the data state  
       !!!next-input-character;  
   
       !!!emit ($token);  
   
       redo A;  
     } elsif ($self->{state} == ENTITY_DATA_STATE) {  
       ## (cannot happen in CDATA state)  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity (0);  
   
       $self->{state} = DATA_STATE;  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!emit ({type => CHARACTER_TOKEN, data => '&'});  
       } else {  
         !!!emit ($token);  
       }  
   
       redo A;  
     } elsif ($self->{state} == TAG_OPEN_STATE) {  
       if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA  
         if ($self->{next_input_character} == 0x002F) { # /  
           !!!next-input-character;  
           $self->{state} = CLOSE_TAG_OPEN_STATE;  
           redo A;  
         } else {  
           ## reconsume  
           $self->{state} = DATA_STATE;  
   
           !!!emit ({type => CHARACTER_TOKEN, data => '<'});  
   
           redo A;  
         }  
       } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA  
         if ($self->{next_input_character} == 0x0021) { # !  
           $self->{state} = MARKUP_DECLARATION_OPEN_STATE;  
           !!!next-input-character;  
           redo A;  
         } elsif ($self->{next_input_character} == 0x002F) { # /  
           $self->{state} = CLOSE_TAG_OPEN_STATE;  
           !!!next-input-character;  
           redo A;  
         } elsif (0x0041 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x005A) { # A..Z  
           $self->{current_token}  
             = {type => START_TAG_TOKEN,  
                tag_name => chr ($self->{next_input_character} + 0x0020)};  
           $self->{state} = TAG_NAME_STATE;  
           !!!next-input-character;  
           redo A;  
         } elsif (0x0061 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x007A) { # a..z  
           $self->{current_token} = {type => START_TAG_TOKEN,  
                             tag_name => chr ($self->{next_input_character})};  
           $self->{state} = TAG_NAME_STATE;  
           !!!next-input-character;  
           redo A;  
         } elsif ($self->{next_input_character} == 0x003E) { # >  
           !!!parse-error (type => 'empty start tag');  
           $self->{state} = DATA_STATE;  
           !!!next-input-character;  
   
           !!!emit ({type => CHARACTER_TOKEN, data => '<>'});  
   
           redo A;  
         } elsif ($self->{next_input_character} == 0x003F) { # ?  
           !!!parse-error (type => 'pio');  
           $self->{state} = BOGUS_COMMENT_STATE;  
           ## $self->{next_input_character} is intentionally left as is  
           redo A;  
         } else {  
           !!!parse-error (type => 'bare stago');  
           $self->{state} = DATA_STATE;  
           ## reconsume  
   
           !!!emit ({type => CHARACTER_TOKEN, data => '<'});  
   
           redo A;  
         }  
       } else {  
         die "$0: $self->{content_model} in tag open";  
       }  
     } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {  
       if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA  
         if (defined $self->{last_emitted_start_tag_name}) {  
           ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>  
           my @next_char;  
           TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
             push @next_char, $self->{next_input_character};  
             my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
             my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
             if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {  
               !!!next-input-character;  
               next TAGNAME;  
             } else {  
               $self->{next_input_character} = shift @next_char; # reconsume  
               !!!back-next-input-character (@next_char);  
               $self->{state} = DATA_STATE;  
   
               !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
     
               redo A;  
             }  
           }  
           push @next_char, $self->{next_input_character};  
         
           unless ($self->{next_input_character} == 0x0009 or # HT  
                   $self->{next_input_character} == 0x000A or # LF  
                   $self->{next_input_character} == 0x000B or # VT  
                   $self->{next_input_character} == 0x000C or # FF  
                   $self->{next_input_character} == 0x0020 or # SP  
                   $self->{next_input_character} == 0x003E or # >  
                   $self->{next_input_character} == 0x002F or # /  
                   $self->{next_input_character} == -1) {  
             $self->{next_input_character} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = DATA_STATE;  
             !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
             redo A;  
           } else {  
             $self->{next_input_character} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
         } else {  
           ## No start tag token has ever been emitted  
           # next-input-character is already done  
           $self->{state} = DATA_STATE;  
           !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
           redo A;  
         }  
       }  
         
       if (0x0041 <= $self->{next_input_character} and  
           $self->{next_input_character} <= 0x005A) { # A..Z  
         $self->{current_token} = {type => END_TAG_TOKEN,  
                           tag_name => chr ($self->{next_input_character} + 0x0020)};  
         $self->{state} = TAG_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif (0x0061 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x007A) { # a..z  
         $self->{current_token} = {type => END_TAG_TOKEN,  
                           tag_name => chr ($self->{next_input_character})};  
         $self->{state} = TAG_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         !!!parse-error (type => 'empty end tag');  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'bare etago');  
         $self->{state} = DATA_STATE;  
         # reconsume  
   
         !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
   
         redo A;  
       } else {  
         !!!parse-error (type => 'bogus end tag');  
         $self->{state} = BOGUS_COMMENT_STATE;  
         ## $self->{next_input_character} is intentionally left as is  
         redo A;  
       }  
     } elsif ($self->{state} == TAG_NAME_STATE) {  
       if ($self->{next_input_character} == 0x0009 or # HT  
           $self->{next_input_character} == 0x000A or # LF  
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } elsif (0x0041 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x005A) { # A..Z  
         $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);  
           # start tag or end tag  
         ## Stay in this state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         # reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } elsif ($self->{next_input_character} == 0x002F) { # /  
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
         redo A;  
       } else {  
         $self->{current_token}->{tag_name} .= chr $self->{next_input_character};  
           # start tag or end tag  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {  
       if ($self->{next_input_character} == 0x0009 or # HT  
           $self->{next_input_character} == 0x000A or # LF  
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } elsif (0x0041 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x005A) { # A..Z  
         $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),  
                               value => ''};  
         $self->{state} = ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x002F) { # /  
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         # reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } else {  
         $self->{current_attribute} = {name => chr ($self->{next_input_character}),  
                               value => ''};  
         $self->{state} = ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {  
       my $before_leave = sub {  
         if (exists $self->{current_token}->{attributes} # start tag or end tag  
             ->{$self->{current_attribute}->{name}}) { # MUST  
           !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});  
           ## Discard $self->{current_attribute} # MUST  
         } else {  
           $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}  
             = $self->{current_attribute};  
         }  
       }; # $before_leave  
   
       if ($self->{next_input_character} == 0x0009 or # HT  
           $self->{next_input_character} == 0x000A or # LF  
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
         $before_leave->();  
         $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003D) { # =  
         $before_leave->();  
         $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         $before_leave->();  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } elsif (0x0041 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x005A) { # A..Z  
         $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x002F) { # /  
         $before_leave->();  
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         $before_leave->();  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         # reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } else {  
         $self->{current_attribute}->{name} .= chr ($self->{next_input_character});  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {  
       if ($self->{next_input_character} == 0x0009 or # HT  
           $self->{next_input_character} == 0x000A or # LF  
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003D) { # =  
         $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } elsif (0x0041 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x005A) { # A..Z  
         $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),  
                               value => ''};  
         $self->{state} = ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x002F) { # /  
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         # reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } else {  
         $self->{current_attribute} = {name => chr ($self->{next_input_character}),  
                               value => ''};  
         $self->{state} = ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;          
       }  
     } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {  
       if ($self->{next_input_character} == 0x0009 or # HT  
           $self->{next_input_character} == 0x000A or # LF  
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP        
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0022) { # "  
         $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0026) { # &  
         $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;  
         ## reconsume  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0027) { # '  
         $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } else {  
         $self->{current_attribute}->{value} .= chr ($self->{next_input_character});  
         $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {  
       if ($self->{next_input_character} == 0x0022) { # "  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0026) { # &  
         $self->{last_attribute_value_state} = $self->{state};  
         $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed attribute value');  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } else {  
         $self->{current_attribute}->{value} .= chr ($self->{next_input_character});  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {  
       if ($self->{next_input_character} == 0x0027) { # '  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0026) { # &  
         $self->{last_attribute_value_state} = $self->{state};  
         $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed attribute value');  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } else {  
         $self->{current_attribute}->{value} .= chr ($self->{next_input_character});  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {  
       if ($self->{next_input_character} == 0x0009 or # HT  
           $self->{next_input_character} == 0x000A or # LF  
           $self->{next_input_character} == 0x000B or # HT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0026) { # &  
         $self->{last_attribute_value_state} = $self->{state};  
         $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} == START_TAG_TOKEN) {  
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
           $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
   
         redo A;  
       } else {  
         $self->{current_attribute}->{value} .= chr ($self->{next_input_character});  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {  
       my $token = $self->_tokenize_attempt_to_consume_an_entity (1);  
   
       unless (defined $token) {  
         $self->{current_attribute}->{value} .= '&';  
       } else {  
         $self->{current_attribute}->{value} .= $token->{data};  
         ## ISSUE: spec says "append the returned character token to the current attribute's value"  
       }  
   
       $self->{state} = $self->{last_attribute_value_state};  
       # next-input-character is already done  
       redo A;  
     } elsif ($self->{state} == BOGUS_COMMENT_STATE) {  
       ## (only happen if PCDATA state)  
         
       my $token = {type => COMMENT_TOKEN, data => ''};  
   
       BC: {  
         if ($self->{next_input_character} == 0x003E) { # >  
           $self->{state} = DATA_STATE;  
           !!!next-input-character;  
   
           !!!emit ($token);  
   
           redo A;  
         } elsif ($self->{next_input_character} == -1) {  
           $self->{state} = DATA_STATE;  
           ## reconsume  
   
           !!!emit ($token);  
   
           redo A;  
         } else {  
           $token->{data} .= chr ($self->{next_input_character});  
           !!!next-input-character;  
           redo BC;  
         }  
       } # BC  
     } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {  
       ## (only happen if PCDATA state)  
   
       my @next_char;  
       push @next_char, $self->{next_input_character};  
         
       if ($self->{next_input_character} == 0x002D) { # -  
         !!!next-input-character;  
         push @next_char, $self->{next_input_character};  
         if ($self->{next_input_character} == 0x002D) { # -  
           $self->{current_token} = {type => COMMENT_TOKEN, data => ''};  
           $self->{state} = COMMENT_START_STATE;  
           !!!next-input-character;  
           redo A;  
         }  
       } elsif ($self->{next_input_character} == 0x0044 or # D  
                $self->{next_input_character} == 0x0064) { # d  
         !!!next-input-character;  
         push @next_char, $self->{next_input_character};  
         if ($self->{next_input_character} == 0x004F or # O  
             $self->{next_input_character} == 0x006F) { # o  
           !!!next-input-character;  
           push @next_char, $self->{next_input_character};  
           if ($self->{next_input_character} == 0x0043 or # C  
               $self->{next_input_character} == 0x0063) { # c  
             !!!next-input-character;  
             push @next_char, $self->{next_input_character};  
             if ($self->{next_input_character} == 0x0054 or # T  
                 $self->{next_input_character} == 0x0074) { # t  
               !!!next-input-character;  
               push @next_char, $self->{next_input_character};  
               if ($self->{next_input_character} == 0x0059 or # Y  
                   $self->{next_input_character} == 0x0079) { # y  
                 !!!next-input-character;  
                 push @next_char, $self->{next_input_character};  
                 if ($self->{next_input_character} == 0x0050 or # P  
                     $self->{next_input_character} == 0x0070) { # p  
                   !!!next-input-character;  
                   push @next_char, $self->{next_input_character};  
                   if ($self->{next_input_character} == 0x0045 or # E  
                       $self->{next_input_character} == 0x0065) { # e  
                     ## ISSUE: What a stupid code this is!  
                     $self->{state} = DOCTYPE_STATE;  
                     !!!next-input-character;  
                     redo A;  
                   }  
                 }  
               }  
             }  
           }  
         }  
       }  
   
       !!!parse-error (type => 'bogus comment');  
       $self->{next_input_character} = shift @next_char;  
       !!!back-next-input-character (@next_char);  
       $self->{state} = BOGUS_COMMENT_STATE;  
       redo A;  
         
       ## ISSUE: typos in spec: chacacters, is is a parse error  
       ## 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?  
     } elsif ($self->{state} == COMMENT_START_STATE) {  
       if ($self->{next_input_character} == 0x002D) { # -  
         $self->{state} = COMMENT_START_DASH_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         !!!parse-error (type => 'bogus comment');  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } else {  
         $self->{current_token}->{data} # comment  
             .= chr ($self->{next_input_character});  
         $self->{state} = COMMENT_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == COMMENT_START_DASH_STATE) {  
       if ($self->{next_input_character} == 0x002D) { # -  
         $self->{state} = COMMENT_END_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         !!!parse-error (type => 'bogus comment');  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } else {  
         $self->{current_token}->{data} # comment  
             .= '-' . chr ($self->{next_input_character});  
         $self->{state} = COMMENT_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == COMMENT_STATE) {  
       if ($self->{next_input_character} == 0x002D) { # -  
         $self->{state} = COMMENT_END_DASH_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } else {  
         $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == COMMENT_END_DASH_STATE) {  
       if ($self->{next_input_character} == 0x002D) { # -  
         $self->{state} = COMMENT_END_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } else {  
         $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment  
         $self->{state} = COMMENT_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == COMMENT_END_STATE) {  
       if ($self->{next_input_character} == 0x003E) { # >  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } elsif ($self->{next_input_character} == 0x002D) { # -  
         !!!parse-error (type => 'dash in comment');  
         $self->{current_token}->{data} .= '-'; # comment  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } else {  
         !!!parse-error (type => 'dash in comment');  
         $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment  
         $self->{state} = COMMENT_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == DOCTYPE_STATE) {  
       if ($self->{next_input_character} == 0x0009 or # HT  
           $self->{next_input_character} == 0x000A or # LF  
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
         $self->{state} = BEFORE_DOCTYPE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } else {  
         !!!parse-error (type => 'no space before DOCTYPE name');  
         $self->{state} = BEFORE_DOCTYPE_NAME_STATE;  
         ## reconsume  
         redo A;  
       }  
     } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {  
       if ($self->{next_input_character} == 0x0009 or # HT  
           $self->{next_input_character} == 0x000A or # LF  
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         !!!parse-error (type => 'no DOCTYPE name');  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ({type => DOCTYPE_TOKEN}); # incorrect  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'no DOCTYPE name');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         !!!emit ({type => DOCTYPE_TOKEN}); # incorrect  
   
         redo A;  
       } else {  
         $self->{current_token}  
             = {type => DOCTYPE_TOKEN,  
                name => chr ($self->{next_input_character}),  
                correct => 1};  
 ## ISSUE: "Set the token's name name to the" in the spec  
         $self->{state} = DOCTYPE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == DOCTYPE_NAME_STATE) {  
 ## ISSUE: Redundant "First," in the spec.  
       if ($self->{next_input_character} == 0x0009 or # HT  
           $self->{next_input_character} == 0x000A or # LF  
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
         $self->{state} = AFTER_DOCTYPE_NAME_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed DOCTYPE');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         $self->{current_token}->{name}  
           .= chr ($self->{next_input_character}); # DOCTYPE  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {  
       if ($self->{next_input_character} == 0x0009 or # HT  
           $self->{next_input_character} == 0x000A or # LF  
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed DOCTYPE');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_input_character} == 0x0050 or # P  
                $self->{next_input_character} == 0x0070) { # p  
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x0055 or # U  
             $self->{next_input_character} == 0x0075) { # u  
           !!!next-input-character;  
           if ($self->{next_input_character} == 0x0042 or # B  
               $self->{next_input_character} == 0x0062) { # b  
             !!!next-input-character;  
             if ($self->{next_input_character} == 0x004C or # L  
                 $self->{next_input_character} == 0x006C) { # l  
               !!!next-input-character;  
               if ($self->{next_input_character} == 0x0049 or # I  
                   $self->{next_input_character} == 0x0069) { # i  
                 !!!next-input-character;  
                 if ($self->{next_input_character} == 0x0043 or # C  
                     $self->{next_input_character} == 0x0063) { # c  
                   $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 }  
               }  
             }  
           }  
         }  
   
         #  
       } elsif ($self->{next_input_character} == 0x0053 or # S  
                $self->{next_input_character} == 0x0073) { # s  
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x0059 or # Y  
             $self->{next_input_character} == 0x0079) { # y  
           !!!next-input-character;  
           if ($self->{next_input_character} == 0x0053 or # S  
               $self->{next_input_character} == 0x0073) { # s  
             !!!next-input-character;  
             if ($self->{next_input_character} == 0x0054 or # T  
                 $self->{next_input_character} == 0x0074) { # t  
               !!!next-input-character;  
               if ($self->{next_input_character} == 0x0045 or # E  
                   $self->{next_input_character} == 0x0065) { # e  
                 !!!next-input-character;  
                 if ($self->{next_input_character} == 0x004D or # M  
                     $self->{next_input_character} == 0x006D) { # m  
                   $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 }  
               }  
             }  
           }  
         }  
   
         #  
       } else {  
         !!!next-input-character;  
         #  
       }  
   
       !!!parse-error (type => 'string after DOCTYPE name');  
       $self->{state} = BOGUS_DOCTYPE_STATE;  
       # next-input-character is already done  
       redo A;  
     } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {  
       if ({  
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_input_character}}) {  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} eq 0x0022) { # "  
         $self->{current_token}->{public_identifier} = ''; # DOCTYPE  
         $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} eq 0x0027) { # '  
         $self->{current_token}->{public_identifier} = ''; # DOCTYPE  
         $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} eq 0x003E) { # >  
         !!!parse-error (type => 'no PUBLIC literal');  
   
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed DOCTYPE');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!parse-error (type => 'string after PUBLIC');  
         $self->{state} = BOGUS_DOCTYPE_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {  
       if ($self->{next_input_character} == 0x0022) { # "  
         $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed PUBLIC literal');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         $self->{current_token}->{public_identifier} # DOCTYPE  
             .= chr $self->{next_input_character};  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {  
       if ($self->{next_input_character} == 0x0027) { # '  
         $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed PUBLIC literal');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         $self->{current_token}->{public_identifier} # DOCTYPE  
             .= chr $self->{next_input_character};  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {  
       if ({  
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_input_character}}) {  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0022) { # "  
         $self->{current_token}->{system_identifier} = ''; # DOCTYPE  
         $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0027) { # '  
         $self->{current_token}->{system_identifier} = ''; # DOCTYPE  
         $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed DOCTYPE');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!parse-error (type => 'string after PUBLIC literal');  
         $self->{state} = BOGUS_DOCTYPE_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {  
       if ({  
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_input_character}}) {  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0022) { # "  
         $self->{current_token}->{system_identifier} = ''; # DOCTYPE  
         $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0027) { # '  
         $self->{current_token}->{system_identifier} = ''; # DOCTYPE  
         $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         !!!parse-error (type => 'no SYSTEM literal');  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed DOCTYPE');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!parse-error (type => 'string after SYSTEM');  
         $self->{state} = BOGUS_DOCTYPE_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {  
       if ($self->{next_input_character} == 0x0022) { # "  
         $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed SYSTEM literal');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         $self->{current_token}->{system_identifier} # DOCTYPE  
             .= chr $self->{next_input_character};  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {  
       if ($self->{next_input_character} == 0x0027) { # '  
         $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed SYSTEM literal');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         $self->{current_token}->{system_identifier} # DOCTYPE  
             .= chr $self->{next_input_character};  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {  
       if ({  
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_input_character}}) {  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed DOCTYPE');  
   
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!parse-error (type => 'string after SYSTEM literal');  
         $self->{state} = BOGUS_DOCTYPE_STATE;  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {  
       if ($self->{next_input_character} == 0x003E) { # >  
         $self->{state} = DATA_STATE;  
         !!!next-input-character;  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed DOCTYPE');  
         $self->{state} = DATA_STATE;  
         ## reconsume  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } else {  
       die "$0: $self->{state}: Unknown state";  
     }  
   } # A    
   
   die "$0: _get_next_token: unexpected case";  
 } # _get_next_token  
   
 sub _tokenize_attempt_to_consume_an_entity ($$) {  
   my ($self, $in_attr) = @_;  
   
   if ({  
        0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,  
        0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR  
       }->{$self->{next_input_character}}) {  
     ## Don't consume  
     ## No error  
     return undef;  
   } elsif ($self->{next_input_character} == 0x0023) { # #  
     !!!next-input-character;  
     if ($self->{next_input_character} == 0x0078 or # x  
         $self->{next_input_character} == 0x0058) { # X  
       my $code;  
       X: {  
         my $x_char = $self->{next_input_character};  
         !!!next-input-character;  
         if (0x0030 <= $self->{next_input_character} and  
             $self->{next_input_character} <= 0x0039) { # 0..9  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_input_character} - 0x0030;  
           redo X;  
         } elsif (0x0061 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0066) { # a..f  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_input_character} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0046) { # A..F  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_input_character} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $code) { # no hexadecimal digit  
           !!!parse-error (type => 'bare hcro');  
           !!!back-next-input-character ($x_char, $self->{next_input_character});  
           $self->{next_input_character} = 0x0023; # #  
           return undef;  
         } elsif ($self->{next_input_character} == 0x003B) { # ;  
           !!!next-input-character;  
         } else {  
           !!!parse-error (type => 'no refc');  
         }  
   
         if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {  
           !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);  
           $code = 0xFFFD;  
         } elsif ($code > 0x10FFFF) {  
           !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);  
           $code = 0xFFFD;  
         } elsif ($code == 0x000D) {  
           !!!parse-error (type => 'CR character reference');  
           $code = 0x000A;  
         } elsif (0x80 <= $code and $code <= 0x9F) {  
           !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);  
           $code = $c1_entity_char->{$code};  
         }  
   
         return {type => CHARACTER_TOKEN, data => chr $code};  
       } # X  
     } elsif (0x0030 <= $self->{next_input_character} and  
              $self->{next_input_character} <= 0x0039) { # 0..9  
       my $code = $self->{next_input_character} - 0x0030;  
       !!!next-input-character;  
         
       while (0x0030 <= $self->{next_input_character} and  
                 $self->{next_input_character} <= 0x0039) { # 0..9  
         $code *= 10;  
         $code += $self->{next_input_character} - 0x0030;  
           
         !!!next-input-character;  
       }  
   
       if ($self->{next_input_character} == 0x003B) { # ;  
         !!!next-input-character;  
       } else {  
         !!!parse-error (type => 'no refc');  
       }  
   
       if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {  
         !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);  
         $code = 0xFFFD;  
       } elsif ($code > 0x10FFFF) {  
         !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);  
         $code = 0xFFFD;  
       } elsif ($code == 0x000D) {  
         !!!parse-error (type => 'CR character reference');  
         $code = 0x000A;  
       } elsif (0x80 <= $code and $code <= 0x9F) {  
         !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);  
         $code = $c1_entity_char->{$code};  
       }  
         
       return {type => CHARACTER_TOKEN, data => chr $code};  
     } else {  
       !!!parse-error (type => 'bare nero');  
       !!!back-next-input-character ($self->{next_input_character});  
       $self->{next_input_character} = 0x0023; # #  
       return undef;  
     }  
   } elsif ((0x0041 <= $self->{next_input_character} and  
             $self->{next_input_character} <= 0x005A) or  
            (0x0061 <= $self->{next_input_character} and  
             $self->{next_input_character} <= 0x007A)) {  
     my $entity_name = chr $self->{next_input_character};  
     !!!next-input-character;  
   
     my $value = $entity_name;  
     my $match = 0;  
     require Whatpm::_NamedEntityList;  
     our $EntityChar;  
   
     while (length $entity_name < 10 and  
            ## NOTE: Some number greater than the maximum length of entity name  
            ((0x0041 <= $self->{next_input_character} and # a  
              $self->{next_input_character} <= 0x005A) or # x  
             (0x0061 <= $self->{next_input_character} and # a  
              $self->{next_input_character} <= 0x007A) or # z  
             (0x0030 <= $self->{next_input_character} and # 0  
              $self->{next_input_character} <= 0x0039) or # 9  
             $self->{next_input_character} == 0x003B)) { # ;  
       $entity_name .= chr $self->{next_input_character};  
       if (defined $EntityChar->{$entity_name}) {  
         if ($self->{next_input_character} == 0x003B) { # ;  
           $value = $EntityChar->{$entity_name};  
           $match = 1;  
           !!!next-input-character;  
           last;  
         } else {  
           $value = $EntityChar->{$entity_name};  
           $match = -1;  
           !!!next-input-character;  
         }  
       } else {  
         $value .= chr $self->{next_input_character};  
         $match *= 2;  
         !!!next-input-character;  
       }  
     }  
       
     if ($match > 0) {  
       return {type => CHARACTER_TOKEN, data => $value};  
     } elsif ($match < 0) {  
       !!!parse-error (type => 'no refc');  
       if ($in_attr and $match < -1) {  
         return {type => CHARACTER_TOKEN, data => '&'.$entity_name};  
       } else {  
         return {type => CHARACTER_TOKEN, data => $value};  
       }  
     } else {  
       !!!parse-error (type => 'bare ero');  
       ## NOTE: No characters are consumed in the spec.  
       return {type => CHARACTER_TOKEN, data => '&'.$value};  
     }  
   } else {  
     ## no characters are consumed  
     !!!parse-error (type => 'bare ero');  
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
   
850  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
851    my $self = shift;    my $self = shift;
852    ## NOTE: $self->{document} MUST be specified before this method is called    ## NOTE: $self->{document} MUST be specified before this method is called
# Line 1947  sub _initialize_tree_constructor ($) { Line 854  sub _initialize_tree_constructor ($) {
854    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
855    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
856    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
857      $self->{document}->set_user_data (manakai_source_line => 1);
858      $self->{document}->set_user_data (manakai_source_column => 1);
859  } # _initialize_tree_constructor  } # _initialize_tree_constructor
860    
861  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1966  sub _construct_tree ($) { Line 875  sub _construct_tree ($) {
875    ## When an interactive UA render the $self->{document} available    ## When an interactive UA render the $self->{document} available
876    ## to the user, or when it begin accepting user input, are    ## to the user, or when it begin accepting user input, are
877    ## not defined.    ## not defined.
   
   ## Append a character: collect it and all subsequent consecutive  
   ## characters and insert one Text node whose data is concatenation  
   ## of all those characters. # MUST  
878        
879    !!!next-token;    !!!next-token;
880    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
881    undef $self->{form_element};    undef $self->{form_element};
882    undef $self->{head_element};    undef $self->{head_element};
883      undef $self->{head_element_inserted};
884    $self->{open_elements} = [];    $self->{open_elements} = [];
885    undef $self->{inner_html_node};    undef $self->{inner_html_node};
886      undef $self->{ignore_newline};
887    
888      ## NOTE: The "initial" insertion mode.
889    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
890    
891      ## NOTE: The "before html" insertion mode.
892    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
893      $self->{insertion_mode} = BEFORE_HEAD_IM;
894    
895      ## NOTE: The "before head" insertion mode and so on.
896    $self->_tree_construction_main;    $self->_tree_construction_main;
897  } # _construct_tree  } # _construct_tree
898    
899  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
900    my $self = shift;    my $self = shift;
901    
902      ## NOTE: "initial" insertion mode
903    
904    INITIAL: {    INITIAL: {
905      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
906        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 1993  sub _tree_construction_initial ($) { Line 908  sub _tree_construction_initial ($) {
908        ## language.        ## language.
909        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
910        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
911        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
912        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
913            defined $token->{public_identifier} or            defined $token->{sysid}) {
914            defined $token->{system_identifier}) {          !!!cp ('t1');
915          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
916        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
917          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!cp ('t2');
918          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
919          } elsif (defined $token->{pubid}) {
920            if ($token->{pubid} eq 'XSLT-compat') {
921              !!!cp ('t1.2');
922              !!!parse-error (type => 'XSLT-compat', token => $token,
923                              level => $self->{level}->{should});
924            } else {
925              !!!parse-error (type => 'not HTML5', token => $token);
926            }
927          } else {
928            !!!cp ('t3');
929            #
930        }        }
931                
932        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
933          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
934        $doctype->public_id ($token->{public_identifier})        ## NOTE: Default value for both |public_id| and |system_id| attributes
935            if defined $token->{public_identifier};        ## are empty strings, so that we don't set any value in missing cases.
936        $doctype->system_id ($token->{system_identifier})        $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
937            if defined $token->{system_identifier};        $doctype->system_id ($token->{sysid}) if defined $token->{sysid};
938        ## NOTE: Other DocumentType attributes are null or empty lists.        ## NOTE: Other DocumentType attributes are null or empty lists.
939        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
940        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
941                
942        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
943            !!!cp ('t4');
944          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
945        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{pubid}) {
946          my $pubid = $token->{public_identifier};          my $pubid = $token->{pubid};
947          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
948          if ({          my $prefix = [
949            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
950            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
951            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
952            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
953            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
954            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
955            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
956            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
957            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
958            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
959            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
960            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
961            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
962            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
963            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
964            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
965            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
966            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
967            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
968            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
969            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
970            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
971            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
972            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
973            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
974            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
975            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
976            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
977            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
978            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
979            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
980            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
981            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
982            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
983            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
984            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
985            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
986            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
987            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
988            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
989            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
990            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
991            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
992            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
993            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
994            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
995            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
996            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
997            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
998            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
999            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
1000            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
1001            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
1002            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
1003            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
1004            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
1005            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
1006            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
1007            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
1008            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
1009            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
1010            "-//W3C//DTD W3 HTML//EN" => 1,            }
1011            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
1012            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
1013            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
1014            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
1015            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
1016            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
1017            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
1018          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
1019                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
1020            if (defined $token->{system_identifier}) {            if (defined $token->{sysid}) {
1021                !!!cp ('t6');
1022              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
1023            } else {            } else {
1024                !!!cp ('t7');
1025              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
1026            }            }
1027          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
1028                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
1029              !!!cp ('t8');
1030            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
1031            } else {
1032              !!!cp ('t9');
1033          }          }
1034          } else {
1035            !!!cp ('t10');
1036        }        }
1037        if (defined $token->{system_identifier}) {        if (defined $token->{sysid}) {
1038          my $sysid = $token->{system_identifier};          my $sysid = $token->{sysid};
1039          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
1040          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") {
1041              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
1042              ## marked as quirks.
1043            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
1044              !!!cp ('t11');
1045            } else {
1046              !!!cp ('t12');
1047          }          }
1048          } else {
1049            !!!cp ('t13');
1050        }        }
1051                
1052        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
1053        !!!next-token;        !!!next-token;
1054        return;        return;
1055      } elsif ({      } elsif ({
# Line 2118  sub _tree_construction_initial ($) { Line 1057  sub _tree_construction_initial ($) {
1057                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
1058                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
1059               }->{$token->{type}}) {               }->{$token->{type}}) {
1060        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
1061          !!!parse-error (type => 'no DOCTYPE', token => $token);
1062        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
1063        ## Go to the root element phase        ## Go to the "before html" insertion mode.
1064        ## reprocess        ## reprocess
1065          !!!ack-later;
1066        return;        return;
1067      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
1068        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
1069          ## Ignore the token          ## Ignore the token
1070    
1071          unless (length $token->{data}) {          unless (length $token->{data}) {
1072            ## Stay in the phase            !!!cp ('t15');
1073              ## Stay in the insertion mode.
1074            !!!next-token;            !!!next-token;
1075            redo INITIAL;            redo INITIAL;
1076            } else {
1077              !!!cp ('t16');
1078          }          }
1079          } else {
1080            !!!cp ('t17');
1081        }        }
1082    
1083        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
1084        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
1085        ## Go to the root element phase        ## Go to the "before html" insertion mode.
1086        ## reprocess        ## reprocess
1087        return;        return;
1088      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
1089          !!!cp ('t18');
1090        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
1091        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
1092                
1093        ## Stay in the phase.        ## Stay in the insertion mode.
1094        !!!next-token;        !!!next-token;
1095        redo INITIAL;        redo INITIAL;
1096      } else {      } else {
1097        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
1098      }      }
1099    } # INITIAL    } # INITIAL
1100    
1101      die "$0: _tree_construction_initial: This should be never reached";
1102  } # _tree_construction_initial  } # _tree_construction_initial
1103    
1104  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
1105    my $self = shift;    my $self = shift;
1106    
1107      ## NOTE: "before html" insertion mode.
1108        
1109    B: {    B: {
1110        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
1111          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
1112            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
1113          ## Ignore the token          ## Ignore the token
1114          ## Stay in the phase          ## Stay in the insertion mode.
1115          !!!next-token;          !!!next-token;
1116          redo B;          redo B;
1117        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
1118            !!!cp ('t20');
1119          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
1120          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
1121          ## Stay in the phase          ## Stay in the insertion mode.
1122          !!!next-token;          !!!next-token;
1123          redo B;          redo B;
1124        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
1125          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
1126            ## Ignore the token.            ## Ignore the token.
1127    
1128            unless (length $token->{data}) {            unless (length $token->{data}) {
1129              ## Stay in the phase              !!!cp ('t21');
1130                ## Stay in the insertion mode.
1131              !!!next-token;              !!!next-token;
1132              redo B;              redo B;
1133              } else {
1134                !!!cp ('t22');
1135            }            }
1136            } else {
1137              !!!cp ('t23');
1138          }          }
1139    
1140          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
1141    
1142          #          #
1143        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
1144          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
1145              $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"            my $root_element;
1146            $self->{application_cache_selection}            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
1147                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
1148            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
1149                  [$root_element, $el_category->{html}];
1150    
1151              if ($token->{attributes}->{manifest}) {
1152                !!!cp ('t24');
1153                $self->{application_cache_selection}
1154                    ->($token->{attributes}->{manifest}->{value});
1155                ## ISSUE: Spec is unclear on relative references.
1156                ## According to Hixie (#whatwg 2008-03-19), it should be
1157                ## resolved against the base URI of the document in HTML
1158                ## or xml:base of the element in XHTML.
1159              } else {
1160                !!!cp ('t25');
1161                $self->{application_cache_selection}->(undef);
1162              }
1163    
1164              !!!nack ('t25c');
1165    
1166              !!!next-token;
1167              return; ## Go to the "before head" insertion mode.
1168          } else {          } else {
1169            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
1170              #
1171          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
1172        } elsif ({        } elsif ({
1173                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
1174                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
1175                 }->{$token->{type}}) {                 }->{$token->{type}}) {
1176          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
1177          #          #
1178        } else {        } else {
1179          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
1180        }        }
1181    
1182        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
1183        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
1184        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
1185        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
1186        #redo B;  
1187        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
1188    
1189        ## NOTE: Reprocess the token.
1190        !!!ack-later;
1191        return; ## Go to the "before head" insertion mode.
1192    } # B    } # B
1193    
1194      die "$0: _tree_construction_root_element: This should never be reached";
1195  } # _tree_construction_root_element  } # _tree_construction_root_element
1196    
1197  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2227  sub _reset_insertion_mode ($) { Line 1206  sub _reset_insertion_mode ($) {
1206            
1207      ## Step 3      ## Step 3
1208      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"!?  
1209        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
1210          $last = 1;          $last = 1;
1211          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
1212            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
1213                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
1214              #          } else {
1215            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
1216          }          }
1217        }        }
1218              
1219        ## Step 4..13        ## Step 4..14
1220        my $new_mode = {        my $new_mode;
1221          if ($node->[1] & FOREIGN_EL) {
1222            !!!cp ('t28.1');
1223            ## NOTE: Strictly spaking, the line below only applies to MathML and
1224            ## SVG elements.  Currently the HTML syntax supports only MathML and
1225            ## SVG elements as foreigners.
1226            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
1227          } elsif ($node->[1] == TABLE_CELL_EL) {
1228            if ($last) {
1229              !!!cp ('t28.2');
1230              #
1231            } else {
1232              !!!cp ('t28.3');
1233              $new_mode = IN_CELL_IM;
1234            }
1235          } else {
1236            !!!cp ('t28.4');
1237            $new_mode = {
1238                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
1239                        td => IN_CELL_IM,                        ## NOTE: |option| and |optgroup| do not set
1240                        th => IN_CELL_IM,                        ## insertion mode to "in select" by themselves.
1241                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
1242                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
1243                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2259  sub _reset_insertion_mode ($) { Line 1248  sub _reset_insertion_mode ($) {
1248                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
1249                        body => IN_BODY_IM,                        body => IN_BODY_IM,
1250                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
1251                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
1252          }
1253        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
1254                
1255        ## Step 14        ## Step 15
1256        if ($node->[1] eq 'html') {        if ($node->[1] == HTML_EL) {
1257          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
1258              !!!cp ('t29');
1259            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
1260          } else {          } else {
1261              ## ISSUE: Can this state be reached?
1262              !!!cp ('t30');
1263            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
1264          }          }
1265          return;          return;
1266          } else {
1267            !!!cp ('t31');
1268        }        }
1269                
1270        ## Step 15        ## Step 16
1271        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
1272                
1273        ## Step 16        ## Step 17
1274        $i--;        $i--;
1275        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
1276                
1277        ## Step 17        ## Step 18
1278        redo S3;        redo S3;
1279      } # S3      } # S3
1280    
1281      die "$0: _reset_insertion_mode: This line should never be reached";
1282  } # _reset_insertion_mode  } # _reset_insertion_mode
1283    
1284  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2303  sub _tree_construction_main ($) { Line 1300  sub _tree_construction_main ($) {
1300      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
1301      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
1302        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
1303            !!!cp ('t32');
1304          return;          return;
1305        }        }
1306      }      }
# Line 2317  sub _tree_construction_main ($) { Line 1315  sub _tree_construction_main ($) {
1315    
1316        ## Step 6        ## Step 6
1317        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
1318            !!!cp ('t33_1');
1319          #          #
1320        } else {        } else {
1321          my $in_open_elements;          my $in_open_elements;
1322          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
1323            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
1324                !!!cp ('t33');
1325              $in_open_elements = 1;              $in_open_elements = 1;
1326              last OE;              last OE;
1327            }            }
1328          }          }
1329          if ($in_open_elements) {          if ($in_open_elements) {
1330              !!!cp ('t34');
1331            #            #
1332          } else {          } else {
1333              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
1334              !!!cp ('t35');
1335            redo S4;            redo S4;
1336          }          }
1337        }        }
# Line 2351  sub _tree_construction_main ($) { Line 1354  sub _tree_construction_main ($) {
1354    
1355        ## Step 11        ## Step 11
1356        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
1357            !!!cp ('t36');
1358          ## Step 7'          ## Step 7'
1359          $i++;          $i++;
1360          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
1361                    
1362          redo S7;          redo S7;
1363        }        }
1364    
1365          !!!cp ('t37');
1366      } # S7      } # S7
1367    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
1368    
1369    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
1370      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
1371        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
1372            !!!cp ('t38');
1373          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
1374          return;          return;
1375        }        }
1376      }      }
1377    
1378        !!!cp ('t39');
1379    }; # $clear_up_to_marker    }; # $clear_up_to_marker
1380    
1381    my $parse_rcdata = sub ($$) {    my $insert;
1382      my ($content_model_flag, $insert) = @_;  
1383      my $parse_rcdata = sub ($) {
1384        my ($content_model_flag) = @_;
1385    
1386      ## Step 1      ## Step 1
1387      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
1388      my $el;      !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
     !!!create-element ($el, $start_tag_name, $token->{attributes});  
1389    
1390      ## Step 2      ## Step 2
     $insert->($el); # /context node/->append_child ($el)  
   
     ## Step 3  
1391      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
1392      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
1393    
1394      ## Step 4      ## Step 3, 4
1395      my $text = '';      $self->{insertion_mode} |= IN_CDATA_RCDATA_IM;
     !!!next-token;  
     while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing  
       $text .= $token->{data};  
       !!!next-token;  
     }  
   
     ## Step 5  
     if (length $text) {  
       my $text = $self->{document}->create_text_node ($text);  
       $el->append_child ($text);  
     }  
   
     ## Step 6  
     $self->{content_model} = PCDATA_CONTENT_MODEL;  
1396    
1397      ## Step 7      !!!nack ('t40.1');
     if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {  
       ## 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});  
     } else {  
       die "$0: $content_model_flag in parse_rcdata";  
     }  
1398      !!!next-token;      !!!next-token;
1399    }; # $parse_rcdata    }; # $parse_rcdata
1400    
1401    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
1402      my $insert = $_[0];      ## Step 1
1403      my $script_el;      my $script_el;
1404      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
1405    
1406        ## Step 2
1407      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
1408    
1409        ## Step 3
1410        ## TODO: Mark as "already executed", if ...
1411    
1412        ## Step 4
1413        $insert->($script_el);
1414    
1415        ## ISSUE: $script_el is not put into the stack
1416        push @{$self->{open_elements}}, [$script_el, $el_category->{script}];
1417    
1418        ## Step 5
1419      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
1420      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
       
     my $text = '';  
     !!!next-token;  
     while ($token->{type} == CHARACTER_TOKEN) {  
       $text .= $token->{data};  
       !!!next-token;  
     } # stop if non-character token or tokenizer stops tokenising  
     if (length $text) {  
       $script_el->manakai_append_text ($text);  
     }  
                 
     $self->{content_model} = PCDATA_CONTENT_MODEL;  
1421    
1422      if ($token->{type} == END_TAG_TOKEN and      ## Step 6-7
1423          $token->{tag_name} eq 'script') {      $self->{insertion_mode} |= IN_CDATA_RCDATA_IM;
       ## Ignore the token  
     } else {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
       ## ISSUE: And ignore?  
       ## TODO: mark as "already executed"  
     }  
       
     if (defined $self->{inner_html_node}) {  
       ## TODO: mark as "already executed"  
     } else {  
       ## TODO: $old_insertion_point = current insertion point  
       ## TODO: insertion point = just before the next input character  
1424    
1425        $insert->($script_el);      !!!nack ('t40.2');
         
       ## TODO: insertion point = $old_insertion_point (might be "undefined")  
         
       ## TODO: if there is a script that will execute as soon as the parser resume, then...  
     }  
       
1426      !!!next-token;      !!!next-token;
1427    }; # $script_start_tag    }; # $script_start_tag
1428    
1429      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
1430      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
1431      ## NOTE: $open_tables->[-1]->[2] is set false when non-Text node inserted.
1432      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
1433    
1434    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
1435      my $tag_name = shift;      my $end_tag_token = shift;
1436        my $tag_name = $end_tag_token->{tag_name};
1437    
1438        ## NOTE: The adoption agency algorithm (AAA).
1439    
1440      FET: {      FET: {
1441        ## Step 1        ## Step 1
1442        my $formatting_element;        my $formatting_element;
1443        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
1444        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
1445          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
1446              !!!cp ('t52');
1447              last AFE;
1448            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
1449                         eq $tag_name) {
1450              !!!cp ('t51');
1451            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
1452            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
1453            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
1454          }          }
1455        } # AFE        } # AFE
1456        unless (defined $formatting_element) {        unless (defined $formatting_element) {
1457          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
1458            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
1459          ## Ignore the token          ## Ignore the token
1460          !!!next-token;          !!!next-token;
1461          return;          return;
# Line 2489  sub _tree_construction_main ($) { Line 1467  sub _tree_construction_main ($) {
1467          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
1468          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
1469            if ($in_scope) {            if ($in_scope) {
1470                !!!cp ('t54');
1471              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
1472              last INSCOPE;              last INSCOPE;
1473            } else { # in open elements but not in scope            } else { # in open elements but not in scope
1474              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
1475                !!!parse-error (type => 'unmatched end tag',
1476                                text => $token->{tag_name},
1477                                token => $end_tag_token);
1478              ## Ignore the token              ## Ignore the token
1479              !!!next-token;              !!!next-token;
1480              return;              return;
1481            }            }
1482          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
1483                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
1484            $in_scope = 0;            $in_scope = 0;
1485          }          }
1486        } # INSCOPE        } # INSCOPE
1487        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
1488          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
1489            !!!parse-error (type => 'unmatched end tag',
1490                            text => $token->{tag_name},
1491                            token => $end_tag_token);
1492          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
1493          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
1494          return;          return;
1495        }        }
1496        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
1497          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
1498            !!!parse-error (type => 'not closed',
1499                            text => $self->{open_elements}->[-1]->[0]
1500                                ->manakai_local_name,
1501                            token => $end_tag_token);
1502        }        }
1503                
1504        ## Step 2        ## Step 2
# Line 2519  sub _tree_construction_main ($) { Line 1506  sub _tree_construction_main ($) {
1506        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
1507        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
1508          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
1509          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
1510              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
1511              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
1512               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
1513              !!!cp ('t59');
1514            $furthest_block = $node;            $furthest_block = $node;
1515            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
1516              ## NOTE: The topmost (eldest) node.
1517          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
1518              !!!cp ('t60');
1519            last OE;            last OE;
1520          }          }
1521        } # OE        } # OE
1522                
1523        ## Step 3        ## Step 3
1524        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
1525            !!!cp ('t61');
1526          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
1527          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
1528          !!!next-token;          !!!next-token;
# Line 2544  sub _tree_construction_main ($) { Line 1535  sub _tree_construction_main ($) {
1535        ## Step 5        ## Step 5
1536        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
1537        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
1538            !!!cp ('t62');
1539          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
1540        }        }
1541                
# Line 2566  sub _tree_construction_main ($) { Line 1558  sub _tree_construction_main ($) {
1558          S7S2: {          S7S2: {
1559            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
1560              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
1561                  !!!cp ('t63');
1562                $node_i_in_active = $_;                $node_i_in_active = $_;
1563                last S7S2;                last S7S2;
1564              }              }
# Line 2579  sub _tree_construction_main ($) { Line 1572  sub _tree_construction_main ($) {
1572                    
1573          ## Step 4          ## Step 4
1574          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
1575              !!!cp ('t64');
1576            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
1577          }          }
1578                    
1579          ## Step 5          ## Step 5
1580          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
1581              !!!cp ('t65');
1582            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
1583            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
1584            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2601  sub _tree_construction_main ($) { Line 1596  sub _tree_construction_main ($) {
1596        } # S7          } # S7  
1597                
1598        ## Step 8        ## Step 8
1599        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
1600            my $foster_parent_element;
1601            my $next_sibling;
1602            OE: for (reverse 0..$#{$self->{open_elements}}) {
1603              if ($self->{open_elements}->[$_]->[1] == TABLE_EL) {
1604                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
1605                                 if (defined $parent and $parent->node_type == 1) {
1606                                   !!!cp ('t65.1');
1607                                   $foster_parent_element = $parent;
1608                                   $next_sibling = $self->{open_elements}->[$_]->[0];
1609                                 } else {
1610                                   !!!cp ('t65.2');
1611                                   $foster_parent_element
1612                                     = $self->{open_elements}->[$_ - 1]->[0];
1613                                 }
1614                                 last OE;
1615                               }
1616                             } # OE
1617                             $foster_parent_element = $self->{open_elements}->[0]->[0]
1618                               unless defined $foster_parent_element;
1619            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
1620            $open_tables->[-1]->[1] = 1; # tainted
1621          } else {
1622            !!!cp ('t65.3');
1623            $common_ancestor_node->[0]->append_child ($last_node->[0]);
1624          }
1625                
1626        ## Step 9        ## Step 9
1627        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2618  sub _tree_construction_main ($) { Line 1638  sub _tree_construction_main ($) {
1638        my $i;        my $i;
1639        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
1640          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
1641              !!!cp ('t66');
1642            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
1643            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
1644          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
1645              !!!cp ('t67');
1646            $i = $_;            $i = $_;
1647          }          }
1648        } # AFE        } # AFE
# Line 2630  sub _tree_construction_main ($) { Line 1652  sub _tree_construction_main ($) {
1652        undef $i;        undef $i;
1653        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
1654          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
1655              !!!cp ('t68');
1656            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
1657            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
1658          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
1659              !!!cp ('t69');
1660            $i = $_;            $i = $_;
1661          }          }
1662        } # OE        } # OE
1663        splice @{$self->{open_elements}}, $i + 1, 1, $clone;        splice @{$self->{open_elements}}, $i + 1, 0, $clone;
1664                
1665        ## Step 14        ## Step 14
1666        redo FET;        redo FET;
1667      } # FET      } # FET
1668    }; # $formatting_end_tag    }; # $formatting_end_tag
1669    
1670    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
1671      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
1672    }; # $insert_to_current    }; # $insert_to_current
1673    
1674    my $insert_to_foster = sub {    my $insert_to_foster = sub {
1675                         my $child = shift;      my $child = shift;
1676                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
1677                              table => 1, tbody => 1, tfoot => 1,        # MUST
1678                              thead => 1, tr => 1,        my $foster_parent_element;
1679                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
1680                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
1681                           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') {  
1682                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
1683                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
1684                                   !!!cp ('t70');
1685                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
1686                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
1687                               } else {                               } else {
1688                                   !!!cp ('t71');
1689                                 $foster_parent_element                                 $foster_parent_element
1690                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
1691                               }                               }
# Line 2673  sub _tree_construction_main ($) { Line 1696  sub _tree_construction_main ($) {
1696                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
1697                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
1698                             ($child, $next_sibling);                             ($child, $next_sibling);
1699                         } else {        $open_tables->[-1]->[1] = 1; # tainted
1700                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
1701                         }        !!!cp ('t72');
1702          $self->{open_elements}->[-1]->[0]->append_child ($child);
1703        }
1704    }; # $insert_to_foster    }; # $insert_to_foster
1705    
1706    my $insert;    ## NOTE: Insert a character (MUST): When a character is inserted, if
1707      ## the last node that was inserted by the parser is a Text node and
1708      ## the character has to be inserted after that node, then the
1709      ## character is appended to the Text node.  However, if any other
1710      ## node is inserted by the parser, then a new Text node is created
1711      ## and the character is appended as that Text node.  If I'm not
1712      ## wrong, for a parser with scripting disabled, there are only two
1713      ## cases where this occurs.  One is the case where an element node
1714      ## is inserted to the |head| element.  This is covered by using the
1715      ## |$self->{head_element_inserted}| flag.  Another is the case where
1716      ## an element or comment is inserted into the |table| subtree while
1717      ## foster parenting happens.  This is covered by using the [2] flag
1718      ## of the |$open_tables| structure.  All other cases are handled
1719      ## simply by calling |manakai_append_text| method.
1720    
1721      ## TODO: |<body><script>document.write("a<br>");
1722      ## document.body.removeChild (document.body.lastChild);
1723      ## document.write ("b")</script>|
1724    
1725    B: {    B: while (1) {
1726      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
1727        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
1728          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
1729        ## Ignore the token        ## Ignore the token
1730        ## Stay in the phase        ## Stay in the phase
1731        !!!next-token;        !!!next-token;
1732        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
1733      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
1734               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
1735        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
1736          ## Turn into the main phase          !!!cp ('t79');
1737          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
1738          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
1739        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
1740          ## Turn into the main phase          !!!cp ('t80');
1741          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
1742          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
1743          } else {
1744            !!!cp ('t81');
1745        }        }
1746    
1747  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
1748  ## 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');  
       }  
1749        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
1750        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
1751          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
1752              !!!cp ('t84');
1753            $top_el->set_attribute_ns            $top_el->set_attribute_ns
1754              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
1755               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
1756          }          }
1757        }        }
1758          !!!nack ('t84.1');
1759        !!!next-token;        !!!next-token;
1760        redo B;        next B;
1761      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
1762        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
1763        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
1764            !!!cp ('t85');
1765          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
1766        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
1767            !!!cp ('t86');
1768          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
1769        } else {        } else {
1770            !!!cp ('t87');
1771          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
1772            $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
1773        }        }
1774        !!!next-token;        !!!next-token;
1775        redo B;        next B;
1776      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_CDATA_RCDATA_IM) {
1777        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
1778          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          $token->{data} =~ s/^\x0A// if $self->{ignore_newline};
1779            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          delete $self->{ignore_newline};
1780    
1781            if (length $token->{data}) {
1782              !!!cp ('t43');
1783              $self->{open_elements}->[-1]->[0]->manakai_append_text
1784                  ($token->{data});
1785            } else {
1786              !!!cp ('t43.1');
1787            }
1788            !!!next-token;
1789            next B;
1790          } elsif ($token->{type} == END_TAG_TOKEN) {
1791            delete $self->{ignore_newline};
1792    
1793            if ($token->{tag_name} eq 'script') {
1794              !!!cp ('t50');
1795              
1796              ## Para 1-2
1797              my $script = pop @{$self->{open_elements}};
1798              
1799              ## Para 3
1800              $self->{insertion_mode} &= ~ IN_CDATA_RCDATA_IM;
1801    
1802              ## Para 4
1803              ## TODO: $old_insertion_point = $current_insertion_point;
1804              ## TODO: $current_insertion_point = just before $self->{nc};
1805    
1806              ## Para 5
1807              ## TODO: Run the $script->[0].
1808    
1809              ## Para 6
1810              ## TODO: $current_insertion_point = $old_insertion_point;
1811    
1812              ## Para 7
1813              ## TODO: if ($pending_external_script) {
1814                ## TODO: ...
1815              ## TODO: }
1816    
1817              !!!next-token;
1818              next B;
1819            } else {
1820              !!!cp ('t42');
1821    
1822              pop @{$self->{open_elements}};
1823    
1824              $self->{insertion_mode} &= ~ IN_CDATA_RCDATA_IM;
1825              !!!next-token;
1826              next B;
1827            }
1828          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
1829            delete $self->{ignore_newline};
1830    
1831            !!!cp ('t44');
1832            !!!parse-error (type => 'not closed',
1833                            text => $self->{open_elements}->[-1]->[0]
1834                                ->manakai_local_name,
1835                            token => $token);
1836    
1837            #if ($self->{open_elements}->[-1]->[1] == SCRIPT_EL) {
1838            #  ## TODO: Mark as "already executed"
1839            #}
1840    
1841            pop @{$self->{open_elements}};
1842    
1843            $self->{insertion_mode} &= ~ IN_CDATA_RCDATA_IM;
1844            ## Reprocess.
1845            next B;
1846          } else {
1847            die "$0: $token->{type}: In CDATA/RCDATA: Unknown token type";        
1848          }
1849        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
1850          if ($token->{type} == CHARACTER_TOKEN) {
1851            !!!cp ('t87.1');
1852            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
1853            !!!next-token;
1854            next B;
1855          } elsif ($token->{type} == START_TAG_TOKEN) {
1856            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
1857                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
1858                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
1859                ($token->{tag_name} eq 'svg' and
1860                 $self->{open_elements}->[-1]->[1] == MML_AXML_EL)) {
1861              ## NOTE: "using the rules for secondary insertion mode"then"continue"
1862              !!!cp ('t87.2');
1863              #
1864            } elsif ({
1865                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
1866                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
1867                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
1868                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
1869                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
1870                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
1871                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
1872                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
1873                     }->{$token->{tag_name}}) {
1874              !!!cp ('t87.2');
1875              !!!parse-error (type => 'not closed',
1876                              text => $self->{open_elements}->[-1]->[0]
1877                                  ->manakai_local_name,
1878                              token => $token);
1879    
1880              pop @{$self->{open_elements}}
1881                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
1882    
1883              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
1884              ## Reprocess.
1885              next B;
1886            } else {
1887              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
1888              my $tag_name = $token->{tag_name};
1889              if ($nsuri eq $SVG_NS) {
1890                $tag_name = {
1891                   altglyph => 'altGlyph',
1892                   altglyphdef => 'altGlyphDef',
1893                   altglyphitem => 'altGlyphItem',
1894                   animatecolor => 'animateColor',
1895                   animatemotion => 'animateMotion',
1896                   animatetransform => 'animateTransform',
1897                   clippath => 'clipPath',
1898                   feblend => 'feBlend',
1899                   fecolormatrix => 'feColorMatrix',
1900                   fecomponenttransfer => 'feComponentTransfer',
1901                   fecomposite => 'feComposite',
1902                   feconvolvematrix => 'feConvolveMatrix',
1903                   fediffuselighting => 'feDiffuseLighting',
1904                   fedisplacementmap => 'feDisplacementMap',
1905                   fedistantlight => 'feDistantLight',
1906                   feflood => 'feFlood',
1907                   fefunca => 'feFuncA',
1908                   fefuncb => 'feFuncB',
1909                   fefuncg => 'feFuncG',
1910                   fefuncr => 'feFuncR',
1911                   fegaussianblur => 'feGaussianBlur',
1912                   feimage => 'feImage',
1913                   femerge => 'feMerge',
1914                   femergenode => 'feMergeNode',
1915                   femorphology => 'feMorphology',
1916                   feoffset => 'feOffset',
1917                   fepointlight => 'fePointLight',
1918                   fespecularlighting => 'feSpecularLighting',
1919                   fespotlight => 'feSpotLight',
1920                   fetile => 'feTile',
1921                   feturbulence => 'feTurbulence',
1922                   foreignobject => 'foreignObject',
1923                   glyphref => 'glyphRef',
1924                   lineargradient => 'linearGradient',
1925                   radialgradient => 'radialGradient',
1926                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
1927                   textpath => 'textPath',  
1928                }->{$tag_name} || $tag_name;
1929              }
1930    
1931              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
1932    
1933              ## "adjust foreign attributes" - done in insert-element-f
1934    
1935              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
1936    
1937              if ($self->{self_closing}) {
1938                pop @{$self->{open_elements}};
1939                !!!ack ('t87.3');
1940              } else {
1941                !!!cp ('t87.4');
1942              }
1943    
1944              !!!next-token;
1945              next B;
1946            }
1947          } elsif ($token->{type} == END_TAG_TOKEN) {
1948            ## NOTE: "using the rules for secondary insertion mode" then "continue"
1949            !!!cp ('t87.5');
1950            #
1951          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
1952            !!!cp ('t87.6');
1953            !!!parse-error (type => 'not closed',
1954                            text => $self->{open_elements}->[-1]->[0]
1955                                ->manakai_local_name,
1956                            token => $token);
1957    
1958            pop @{$self->{open_elements}}
1959                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
1960    
1961            ## NOTE: |<span><svg>| ... two parse errors, |<svg>| ... a parse error.
1962    
1963            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
1964            ## Reprocess.
1965            next B;
1966          } else {
1967            die "$0: $token->{type}: Unknown token type";        
1968          }
1969        }
1970    
1971        if ($self->{insertion_mode} & HEAD_IMS) {
1972          if ($token->{type} == CHARACTER_TOKEN) {
1973            if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
1974              unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
1975                if ($self->{head_element_inserted}) {
1976                  !!!cp ('t88.3');
1977                  $self->{open_elements}->[-1]->[0]->append_child
1978                    ($self->{document}->create_text_node ($1));
1979                  delete $self->{head_element_inserted};
1980                  ## NOTE: |</head> <link> |
1981                  #
1982                } else {
1983                  !!!cp ('t88.2');
1984                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
1985                  ## NOTE: |</head> &#x20;|
1986                  #
1987                }
1988              } else {
1989                !!!cp ('t88.1');
1990                ## Ignore the token.
1991                #
1992              }
1993            unless (length $token->{data}) {            unless (length $token->{data}) {
1994                !!!cp ('t88');
1995              !!!next-token;              !!!next-token;
1996              redo B;              next B;
1997            }            }
1998    ## TODO: set $token->{column} appropriately
1999          }          }
2000    
2001          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2002              !!!cp ('t89');
2003            ## As if <head>            ## As if <head>
2004            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2005            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2006            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
2007                  [$self->{head_element}, $el_category->{head}];
2008    
2009            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
2010            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2011    
2012            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
2013          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2014              !!!cp ('t90');
2015            ## As if </noscript>            ## As if </noscript>
2016            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2017            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
2018                        
2019            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
2020            ## As if </head>            ## As if </head>
# Line 2784  sub _tree_construction_main ($) { Line 2022  sub _tree_construction_main ($) {
2022    
2023            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
2024          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2025              !!!cp ('t91');
2026            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2027    
2028            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
2029            } else {
2030              !!!cp ('t92');
2031          }          }
2032    
2033              ## "after head" insertion mode          ## "after head" insertion mode
2034              ## As if <body>          ## As if <body>
2035              !!!insert-element ('body');          !!!insert-element ('body',, $token);
2036              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
2037              ## reprocess          ## reprocess
2038              redo B;          next B;
2039            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
2040              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
2041                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2042                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
2043                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
2044                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
2045                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
2046                  !!!next-token;              push @{$self->{open_elements}},
2047                  redo B;                  [$self->{head_element}, $el_category->{head}];
2048                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
2049                  #              !!!nack ('t93.1');
2050                } else {              !!!next-token;
2051                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
2052                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2053                  !!!next-token;              !!!cp ('t93.2');
2054                  redo B;              !!!parse-error (type => 'after head', text => 'head',
2055                }                              token => $token);
2056              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              ## Ignore the token
2057                ## As if <head>              !!!nack ('t93.3');
2058                !!!create-element ($self->{head_element}, 'head');              !!!next-token;
2059                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              next B;
2060                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            } else {
2061                !!!cp ('t95');
2062                !!!parse-error (type => 'in head:head',
2063                                token => $token); # or in head noscript
2064                ## Ignore the token
2065                !!!nack ('t95.1');
2066                !!!next-token;
2067                next B;
2068              }
2069            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2070              !!!cp ('t96');
2071              ## As if <head>
2072              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2073              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2074              push @{$self->{open_elements}},
2075                  [$self->{head_element}, $el_category->{head}];
2076    
2077                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
2078                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
2079              }          } else {
2080              !!!cp ('t97');
2081            }
2082    
2083              if ($token->{tag_name} eq 'base') {          if ($token->{tag_name} eq 'base') {
2084                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {            if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2085                  ## As if </noscript>              !!!cp ('t98');
2086                  pop @{$self->{open_elements}};              ## As if </noscript>
2087                  !!!parse-error (type => 'in noscript:base');              pop @{$self->{open_elements}};
2088                              !!!parse-error (type => 'in noscript', text => 'base',
2089                  $self->{insertion_mode} = IN_HEAD_IM;                              token => $token);
2090                  ## Reprocess in the "in head" insertion mode...            
2091                }              $self->{insertion_mode} = IN_HEAD_IM;
2092                ## Reprocess in the "in head" insertion mode...
2093              } else {
2094                !!!cp ('t99');
2095              }
2096    
2097                ## NOTE: There is a "as if in head" code clone.            ## NOTE: There is a "as if in head" code clone.
2098                if ($self->{insertion_mode} == AFTER_HEAD_IM) {            if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2099                  !!!parse-error (type => 'after head:'.$token->{tag_name});              !!!cp ('t100');
2100                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!parse-error (type => 'after head',
2101                }                              text => $token->{tag_name}, token => $token);
2102                !!!insert-element ($token->{tag_name}, $token->{attributes});              push @{$self->{open_elements}},
2103                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                  [$self->{head_element}, $el_category->{head}];
2104                pop @{$self->{open_elements}}              $self->{head_element_inserted} = 1;
2105                    if $self->{insertion_mode} == AFTER_HEAD_IM;            } else {
2106                !!!next-token;              !!!cp ('t101');
2107                redo B;            }
2108              } elsif ($token->{tag_name} eq 'link') {            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2109                ## NOTE: There is a "as if in head" code clone.            pop @{$self->{open_elements}};
2110                if ($self->{insertion_mode} == AFTER_HEAD_IM) {            pop @{$self->{open_elements}} # <head>
2111                  !!!parse-error (type => 'after head:'.$token->{tag_name});                if $self->{insertion_mode} == AFTER_HEAD_IM;
2112                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!nack ('t101.1');
2113                }            !!!next-token;
2114                !!!insert-element ($token->{tag_name}, $token->{attributes});            next B;
2115                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          } elsif ($token->{tag_name} eq 'link') {
2116                pop @{$self->{open_elements}}            ## NOTE: There is a "as if in head" code clone.
2117                    if $self->{insertion_mode} == AFTER_HEAD_IM;            if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2118                !!!next-token;              !!!cp ('t102');
2119                redo B;              !!!parse-error (type => 'after head',
2120              } elsif ($token->{tag_name} eq 'meta') {                              text => $token->{tag_name}, token => $token);
2121                ## NOTE: There is a "as if in head" code clone.              push @{$self->{open_elements}},
2122                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  [$self->{head_element}, $el_category->{head}];
2123                  !!!parse-error (type => 'after head:'.$token->{tag_name});              $self->{head_element_inserted} = 1;
2124                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            } else {
2125                }              !!!cp ('t103');
2126                !!!insert-element ($token->{tag_name}, $token->{attributes});            }
2127                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2128              pop @{$self->{open_elements}};
2129              pop @{$self->{open_elements}} # <head>
2130                  if $self->{insertion_mode} == AFTER_HEAD_IM;
2131              !!!ack ('t103.1');
2132              !!!next-token;
2133              next B;
2134            } elsif ($token->{tag_name} eq 'command' or
2135                     $token->{tag_name} eq 'eventsource') {
2136              if ($self->{insertion_mode} == IN_HEAD_IM) {
2137                ## NOTE: If the insertion mode at the time of the emission
2138                ## of the token was "before head", $self->{insertion_mode}
2139                ## is already changed to |IN_HEAD_IM|.
2140    
2141                ## NOTE: There is a "as if in head" code clone.
2142                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2143                pop @{$self->{open_elements}};
2144                pop @{$self->{open_elements}} # <head>
2145                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2146                !!!ack ('t103.2');
2147                !!!next-token;
2148                next B;
2149              } else {
2150                ## NOTE: "in head noscript" or "after head" insertion mode
2151                ## - in these cases, these tags are treated as same as
2152                ## normal in-body tags.
2153                !!!cp ('t103.3');
2154                #
2155              }
2156            } elsif ($token->{tag_name} eq 'meta') {
2157              ## NOTE: There is a "as if in head" code clone.
2158              if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2159                !!!cp ('t104');
2160                !!!parse-error (type => 'after head',
2161                                text => $token->{tag_name}, token => $token);
2162                push @{$self->{open_elements}},
2163                    [$self->{head_element}, $el_category->{head}];
2164                $self->{head_element_inserted} = 1;
2165              } else {
2166                !!!cp ('t105');
2167              }
2168              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2169              my $meta_el = pop @{$self->{open_elements}};
2170    
2171                unless ($self->{confident}) {                unless ($self->{confident}) {
2172                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
2173                      !!!cp ('t106');
2174                      ## NOTE: Whether the encoding is supported or not is handled
2175                      ## in the {change_encoding} callback.
2176                    $self->{change_encoding}                    $self->{change_encoding}
2177                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
2178                             $token);
2179                      
2180                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2181                          ->set_user_data (manakai_has_reference =>
2182                                               $token->{attributes}->{charset}
2183                                                   ->{has_reference});
2184                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
2185                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
2186                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
2187                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09\x0A\x0C\x0D\x20]*=
2188                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2189                              ([^"'\x09\x0A\x0C\x0D\x20]
2190                               [^\x09\x0A\x0C\x0D\x20\x3B]*))/x) {
2191                        !!!cp ('t107');
2192                        ## NOTE: Whether the encoding is supported or not is handled
2193                        ## in the {change_encoding} callback.
2194                      $self->{change_encoding}                      $self->{change_encoding}
2195                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
2196                               $token);
2197                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2198                            ->set_user_data (manakai_has_reference =>
2199                                                 $token->{attributes}->{content}
2200                                                       ->{has_reference});
2201                      } else {
2202                        !!!cp ('t108');
2203                    }                    }
2204                  }                  }
2205                  } else {
2206                    if ($token->{attributes}->{charset}) {
2207                      !!!cp ('t109');
2208                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2209                          ->set_user_data (manakai_has_reference =>
2210                                               $token->{attributes}->{charset}
2211                                                   ->{has_reference});
2212                    }
2213                    if ($token->{attributes}->{content}) {
2214                      !!!cp ('t110');
2215                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2216                          ->set_user_data (manakai_has_reference =>
2217                                               $token->{attributes}->{content}
2218                                                   ->{has_reference});
2219                    }
2220                }                }
2221    
2222                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
2223                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2224                  !!!ack ('t110.1');
2225                !!!next-token;                !!!next-token;
2226                redo B;                next B;
2227              } elsif ($token->{tag_name} eq 'title') {          } elsif ($token->{tag_name} eq 'title') {
2228                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {            if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2229                  ## As if </noscript>              !!!cp ('t111');
2230                  pop @{$self->{open_elements}};              ## As if </noscript>
2231                  !!!parse-error (type => 'in noscript:title');              pop @{$self->{open_elements}};
2232                              !!!parse-error (type => 'in noscript', text => 'title',
2233                  $self->{insertion_mode} = IN_HEAD_IM;                              token => $token);
2234                  ## Reprocess in the "in head" insertion mode...            
2235                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
2236                  !!!parse-error (type => 'after head:'.$token->{tag_name});              ## Reprocess in the "in head" insertion mode...
2237                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2238                }              !!!cp ('t112');
2239                !!!parse-error (type => 'after head',
2240                                text => $token->{tag_name}, token => $token);
2241                push @{$self->{open_elements}},
2242                    [$self->{head_element}, $el_category->{head}];
2243                $self->{head_element_inserted} = 1;
2244              } else {
2245                !!!cp ('t113');
2246              }
2247    
2248                ## NOTE: There is a "as if in head" code clone.            ## NOTE: There is a "as if in head" code clone.
2249                my $parent = defined $self->{head_element} ? $self->{head_element}            $parse_rcdata->(RCDATA_CONTENT_MODEL);
2250                    : $self->{open_elements}->[-1]->[0];            ## ISSUE: A spec bug [Bug 6038]
2251                $parse_rcdata->(RCDATA_CONTENT_MODEL,            splice @{$self->{open_elements}}, -2, 1, () # <head>
2252                                sub { $parent->append_child ($_[0]) });                if ($self->{insertion_mode} & IM_MASK) == AFTER_HEAD_IM;
2253                pop @{$self->{open_elements}}            next B;
2254                    if $self->{insertion_mode} == AFTER_HEAD_IM;          } elsif ($token->{tag_name} eq 'style' or
2255                redo B;                   $token->{tag_name} eq 'noframes') {
2256              } elsif ($token->{tag_name} eq 'style') {            ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2257                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and            ## insertion mode IN_HEAD_IM)
2258                ## insertion mode IN_HEAD_IM)            ## NOTE: There is a "as if in head" code clone.
2259                ## NOTE: There is a "as if in head" code clone.            if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2260                if ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!cp ('t114');
2261                  !!!parse-error (type => 'after head:'.$token->{tag_name});              !!!parse-error (type => 'after head',
2262                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                              text => $token->{tag_name}, token => $token);
2263                }              push @{$self->{open_elements}},
2264                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                  [$self->{head_element}, $el_category->{head}];
2265                pop @{$self->{open_elements}}              $self->{head_element_inserted} = 1;
2266                    if $self->{insertion_mode} == AFTER_HEAD_IM;            } else {
2267                redo B;              !!!cp ('t115');
2268              } elsif ($token->{tag_name} eq 'noscript') {            }
2269              $parse_rcdata->(CDATA_CONTENT_MODEL);
2270              ## ISSUE: A spec bug [Bug 6038]
2271              splice @{$self->{open_elements}}, -2, 1, () # <head>
2272                  if ($self->{insertion_mode} & IM_MASK) == AFTER_HEAD_IM;
2273              next B;
2274            } elsif ($token->{tag_name} eq 'noscript') {
2275                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
2276                    !!!cp ('t116');
2277                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
2278                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2279                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2280                    !!!nack ('t116.1');
2281                  !!!next-token;                  !!!next-token;
2282                  redo B;                  next B;
2283                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2284                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
2285                    !!!parse-error (type => 'in noscript', text => 'noscript',
2286                                    token => $token);
2287                  ## Ignore the token                  ## Ignore the token
2288                    !!!nack ('t117.1');
2289                  !!!next-token;                  !!!next-token;
2290                  redo B;                  next B;
2291                } else {                } else {
2292                    !!!cp ('t118');
2293                  #                  #
2294                }                }
2295              } elsif ($token->{tag_name} eq 'script') {          } elsif ($token->{tag_name} eq 'script') {
2296                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {            if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2297                  ## As if </noscript>              !!!cp ('t119');
2298                  pop @{$self->{open_elements}};              ## As if </noscript>
2299                  !!!parse-error (type => 'in noscript:script');              pop @{$self->{open_elements}};
2300                              !!!parse-error (type => 'in noscript', text => 'script',
2301                  $self->{insertion_mode} = IN_HEAD_IM;                              token => $token);
2302                  ## Reprocess in the "in head" insertion mode...            
2303                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
2304                  !!!parse-error (type => 'after head:'.$token->{tag_name});              ## Reprocess in the "in head" insertion mode...
2305                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2306                }              !!!cp ('t120');
2307                !!!parse-error (type => 'after head',
2308                                text => $token->{tag_name}, token => $token);
2309                push @{$self->{open_elements}},
2310                    [$self->{head_element}, $el_category->{head}];
2311                $self->{head_element_inserted} = 1;
2312              } else {
2313                !!!cp ('t121');
2314              }
2315    
2316                ## NOTE: There is a "as if in head" code clone.            ## NOTE: There is a "as if in head" code clone.
2317                $script_start_tag->($insert_to_current);            $script_start_tag->();
2318                pop @{$self->{open_elements}}            ## ISSUE: A spec bug  [Bug 6038]
2319                    if $self->{insertion_mode} == AFTER_HEAD_IM;            splice @{$self->{open_elements}}, -2, 1 # <head>
2320                redo B;                if ($self->{insertion_mode} & IM_MASK) == AFTER_HEAD_IM;
2321              } elsif ($token->{tag_name} eq 'body' or            next B;
2322                       $token->{tag_name} eq 'frameset') {          } elsif ($token->{tag_name} eq 'body' or
2323                     $token->{tag_name} eq 'frameset') {
2324                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2325                    !!!cp ('t122');
2326                  ## As if </noscript>                  ## As if </noscript>
2327                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2328                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
2329                                    text => $token->{tag_name}, token => $token);
2330                                    
2331                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2332                  ## As if </head>                  ## As if </head>
# Line 2963  sub _tree_construction_main ($) { Line 2334  sub _tree_construction_main ($) {
2334                                    
2335                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
2336                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2337                    !!!cp ('t124');
2338                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2339                                    
2340                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
2341                  } else {
2342                    !!!cp ('t125');
2343                }                }
2344    
2345                ## "after head" insertion mode                ## "after head" insertion mode
2346                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2347                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
2348                    !!!cp ('t126');
2349                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
2350                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
2351                    !!!cp ('t127');
2352                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
2353                } else {                } else {
2354                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
2355                }                }
2356                  !!!nack ('t127.1');
2357                !!!next-token;                !!!next-token;
2358                redo B;                next B;
2359              } else {              } else {
2360                  !!!cp ('t128');
2361                #                #
2362              }              }
2363    
2364              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2365                  !!!cp ('t129');
2366                ## As if </noscript>                ## As if </noscript>
2367                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2368                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
2369                                  text => $token->{tag_name}, token => $token);
2370                                
2371                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
2372                ## As if </head>                ## As if </head>
# Line 2994  sub _tree_construction_main ($) { Line 2374  sub _tree_construction_main ($) {
2374    
2375                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
2376              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2377                  !!!cp ('t130');
2378                ## As if </head>                ## As if </head>
2379                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2380    
2381                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
2382                } else {
2383                  !!!cp ('t131');
2384              }              }
2385    
2386              ## "after head" insertion mode              ## "after head" insertion mode
2387              ## As if <body>              ## As if <body>
2388              !!!insert-element ('body');              !!!insert-element ('body',, $token);
2389              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
2390              ## reprocess              ## reprocess
2391              redo B;              !!!ack-later;
2392                next B;
2393            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
2394              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2395                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2396                    !!!cp ('t132');
2397                  ## As if <head>                  ## As if <head>
2398                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2399                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2400                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
2401                        [$self->{head_element}, $el_category->{head}];
2402    
2403                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2404                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2405                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
2406                  !!!next-token;                  !!!next-token;
2407                  redo B;                  next B;
2408                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2409                    !!!cp ('t133');
2410                  ## As if </noscript>                  ## As if </noscript>
2411                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2412                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/',
2413                                    text => 'head', token => $token);
2414                                    
2415                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2416                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2417                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
2418                  !!!next-token;                  !!!next-token;
2419                  redo B;                  next B;
2420                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2421                    !!!cp ('t134');
2422                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2423                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
2424                  !!!next-token;                  !!!next-token;
2425                  redo B;                  next B;
2426                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2427                    !!!cp ('t134.1');
2428                    !!!parse-error (type => 'unmatched end tag', text => 'head',
2429                                    token => $token);
2430                    ## Ignore the token
2431                    !!!next-token;
2432                    next B;
2433                } else {                } else {
2434                  #                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
2435                }                }
2436              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
2437                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2438                    !!!cp ('t136');
2439                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2440                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
2441                  !!!next-token;                  !!!next-token;
2442                  redo B;                  next B;
2443                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
2444                  !!!parse-error (type => 'unmatched end tag:noscript');                         $self->{insertion_mode} == AFTER_HEAD_IM) {
2445                    !!!cp ('t137');
2446                    !!!parse-error (type => 'unmatched end tag',
2447                                    text => 'noscript', token => $token);
2448                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
2449                  !!!next-token;                  !!!next-token;
2450                  redo B;                  next B;
2451                } else {                } else {
2452                    !!!cp ('t138');
2453                  #                  #
2454                }                }
2455              } elsif ({              } elsif ({
2456                        body => 1, html => 1,                        body => 1, html => 1,
2457                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2458                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                ## TODO: This branch is entirely redundant.
2459                  ## As if <head>                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
2460                  !!!create-element ($self->{head_element}, 'head');                    $self->{insertion_mode} == IN_HEAD_IM or
2461                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2462                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!cp ('t140');
2463                    !!!parse-error (type => 'unmatched end tag',
2464                  $self->{insertion_mode} = IN_HEAD_IM;                                  text => $token->{tag_name}, token => $token);
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
2465                  ## Ignore the token                  ## Ignore the token
2466                  !!!next-token;                  !!!next-token;
2467                  redo B;                  next B;
2468                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2469                    !!!cp ('t140.1');
2470                    !!!parse-error (type => 'unmatched end tag',
2471                                    text => $token->{tag_name}, token => $token);
2472                    ## Ignore the token
2473                    !!!next-token;
2474                    next B;
2475                  } else {
2476                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
2477                }                }
2478                              } elsif ($token->{tag_name} eq 'p') {
2479                #                !!!cp ('t142');
2480              } elsif ({                !!!parse-error (type => 'unmatched end tag',
2481                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
2482                       }->{$token->{tag_name}}) {                ## Ignore the token
2483                  !!!next-token;
2484                  next B;
2485                } elsif ($token->{tag_name} eq 'br') {
2486                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2487                  ## As if <head>                  !!!cp ('t142.2');
2488                  !!!create-element ($self->{head_element}, 'head');                  ## (before head) as if <head>, (in head) as if </head>
2489                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2490                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2491                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
2492      
2493                    ## Reprocess in the "after head" insertion mode...
2494                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2495                    !!!cp ('t143.2');
2496                    ## As if </head>
2497                    pop @{$self->{open_elements}};
2498                    $self->{insertion_mode} = AFTER_HEAD_IM;
2499      
2500                    ## Reprocess in the "after head" insertion mode...
2501                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2502                    !!!cp ('t143.3');
2503                    ## ISSUE: Two parse errors for <head><noscript></br>
2504                    !!!parse-error (type => 'unmatched end tag',
2505                                    text => 'br', token => $token);
2506                    ## As if </noscript>
2507                    pop @{$self->{open_elements}};
2508                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
2509    
2510                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2511                }                  ## As if </head>
2512                    pop @{$self->{open_elements}};
2513                    $self->{insertion_mode} = AFTER_HEAD_IM;
2514    
2515                #                  ## Reprocess in the "after head" insertion mode...
2516              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2517                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
2518                  #                  #
2519                } else {                } else {
2520                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
2521                }                }
2522    
2523                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
2524                  !!!parse-error (type => 'unmatched end tag',
2525                                  text => 'br', token => $token);
2526                  ## Ignore the token
2527                  !!!next-token;
2528                  next B;
2529                } else {
2530                  !!!cp ('t145');
2531                  !!!parse-error (type => 'unmatched end tag',
2532                                  text => $token->{tag_name}, token => $token);
2533                  ## Ignore the token
2534                  !!!next-token;
2535                  next B;
2536              }              }
2537    
2538              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2539                  !!!cp ('t146');
2540                ## As if </noscript>                ## As if </noscript>
2541                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2542                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
2543                                  text => $token->{tag_name}, token => $token);
2544                                
2545                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
2546                ## As if </head>                ## As if </head>
# Line 3106  sub _tree_construction_main ($) { Line 2548  sub _tree_construction_main ($) {
2548    
2549                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
2550              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2551                  !!!cp ('t147');
2552                ## As if </head>                ## As if </head>
2553                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2554    
2555                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
2556              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2557                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
2558                  !!!cp ('t148');
2559                  !!!parse-error (type => 'unmatched end tag',
2560                                  text => $token->{tag_name}, token => $token);
2561                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
2562                !!!next-token;                !!!next-token;
2563                redo B;                next B;
2564                } else {
2565                  !!!cp ('t149');
2566              }              }
2567    
2568              ## "after head" insertion mode              ## "after head" insertion mode
2569              ## As if <body>              ## As if <body>
2570              !!!insert-element ('body');              !!!insert-element ('body',, $token);
2571              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
2572              ## reprocess              ## reprocess
2573              redo B;              next B;
2574            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2575              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2576            }            !!!cp ('t149.1');
2577    
2578              ## NOTE: As if <head>
2579              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2580              $self->{open_elements}->[-1]->[0]->append_child
2581                  ($self->{head_element});
2582              #push @{$self->{open_elements}},
2583              #    [$self->{head_element}, $el_category->{head}];
2584              #$self->{insertion_mode} = IN_HEAD_IM;
2585              ## NOTE: Reprocess.
2586    
2587              ## NOTE: As if </head>
2588              #pop @{$self->{open_elements}};
2589              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
2590              ## NOTE: Reprocess.
2591              
2592              #
2593            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2594              !!!cp ('t149.2');
2595    
2596              ## NOTE: As if </head>
2597              pop @{$self->{open_elements}};
2598              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
2599              ## NOTE: Reprocess.
2600    
2601            ## ISSUE: An issue in the spec.            #
2602            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2603              !!!cp ('t149.3');
2604    
2605              !!!parse-error (type => 'in noscript:#eof', token => $token);
2606    
2607              ## As if </noscript>
2608              pop @{$self->{open_elements}};
2609              #$self->{insertion_mode} = IN_HEAD_IM;
2610              ## NOTE: Reprocess.
2611    
2612              ## NOTE: As if </head>
2613              pop @{$self->{open_elements}};
2614              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
2615              ## NOTE: Reprocess.
2616    
2617              #
2618            } else {
2619              !!!cp ('t149.4');
2620              #
2621            }
2622    
2623            ## NOTE: As if <body>
2624            !!!insert-element ('body',, $token);
2625            $self->{insertion_mode} = IN_BODY_IM;
2626            ## NOTE: Reprocess.
2627            next B;
2628          } else {
2629            die "$0: $token->{type}: Unknown token type";
2630          }
2631      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
2632            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
2633                !!!cp ('t150');
2634              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
2635              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
2636                            
2637              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
2638    
2639              !!!next-token;              !!!next-token;
2640              redo B;              next B;
2641            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
2642              if ({              if ({
2643                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
2644                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
2645                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
2646                if ($self->{insertion_mode} == IN_CELL_IM) {                if (($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2647                  ## have an element in table scope                  ## have an element in table scope
2648                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
2649                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
2650                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] == TABLE_CELL_EL) {
2651                      $tn = $node->[1];                      !!!cp ('t151');
2652                      last INSCOPE;  
2653                    } elsif ({                      ## Close the cell
2654                              table => 1, html => 1,                      !!!back-token; # <x>
2655                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
2656                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
2657                    }                                line => $token->{line},
2658                  } # INSCOPE                                column => $token->{column}};
2659                    unless (defined $tn) {                      next B;
2660                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
2661                      ## Ignore the token                      !!!cp ('t152');
2662                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
2663                      redo B;                      last;
2664                    }                    }
2665                    }
2666    
2667                    !!!cp ('t153');
2668                    !!!parse-error (type => 'start tag not allowed',
2669                        text => $token->{tag_name}, token => $token);
2670                    ## Ignore the token
2671                    !!!nack ('t153.1');
2672                    !!!next-token;
2673                    next B;
2674                  } elsif (($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2675                    !!!parse-error (type => 'not closed', text => 'caption',
2676                                    token => $token);
2677                                    
2678                  ## Close the cell                  ## NOTE: As if </caption>.
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
                 redo B;  
               } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {  
                 !!!parse-error (type => 'not closed:caption');  
                   
                 ## As if </caption>  
2679                  ## have a table element in table scope                  ## have a table element in table scope
2680                  my $i;                  my $i;
2681                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
2682                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
2683                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
2684                      $i = $_;                      if ($node->[1] == CAPTION_EL) {
2685                      last INSCOPE;                        !!!cp ('t155');
2686                    } elsif ({                        $i = $_;
2687                              table => 1, html => 1,                        last INSCOPE;
2688                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
2689                      last INSCOPE;                        !!!cp ('t156');
2690                          last;
2691                        }
2692                    }                    }
2693    
2694                      !!!cp ('t157');
2695                      !!!parse-error (type => 'start tag not allowed',
2696                                      text => $token->{tag_name}, token => $token);
2697                      ## Ignore the token
2698                      !!!nack ('t157.1');
2699                      !!!next-token;
2700                      next B;
2701                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
2702                                    
2703                  ## generate implied end tags                  ## generate implied end tags
2704                  if ({                  while ($self->{open_elements}->[-1]->[1]
2705                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
2706                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
2707                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token; # <?>  
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
2708                  }                  }
2709    
2710                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] == CAPTION_EL) {
2711                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
2712                      !!!parse-error (type => 'not closed',
2713                                      text => $self->{open_elements}->[-1]->[0]
2714                                          ->manakai_local_name,
2715                                      token => $token);
2716                    } else {
2717                      !!!cp ('t160');
2718                  }                  }
2719                                    
2720                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3216  sub _tree_construction_main ($) { Line 2724  sub _tree_construction_main ($) {
2724                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
2725                                    
2726                  ## reprocess                  ## reprocess
2727                  redo B;                  !!!ack-later;
2728                    next B;
2729                } else {                } else {
2730                    !!!cp ('t161');
2731                  #                  #
2732                }                }
2733              } else {              } else {
2734                  !!!cp ('t162');
2735                #                #
2736              }              }
2737            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
2738              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
2739                if ($self->{insertion_mode} == IN_CELL_IM) {                if (($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2740                  ## have an element in table scope                  ## have an element in table scope
2741                  my $i;                  my $i;
2742                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2743                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
2744                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
2745                        !!!cp ('t163');
2746                      $i = $_;                      $i = $_;
2747                      last INSCOPE;                      last INSCOPE;
2748                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
2749                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
2750                      last INSCOPE;                      last INSCOPE;
2751                    }                    }
2752                  } # INSCOPE                  } # INSCOPE
2753                    unless (defined $i) {                    unless (defined $i) {
2754                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
2755                        !!!parse-error (type => 'unmatched end tag',
2756                                        text => $token->{tag_name},
2757                                        token => $token);
2758                      ## Ignore the token                      ## Ignore the token
2759                      !!!next-token;                      !!!next-token;
2760                      redo B;                      next B;
2761                    }                    }
2762                                    
2763                  ## generate implied end tags                  ## generate implied end tags
2764                  if ({                  while ($self->{open_elements}->[-1]->[1]
2765                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
2766                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
2767                       th => ($token->{tag_name} eq 'td'),                    pop @{$self->{open_elements}};
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
2768                  }                  }
2769                    
2770                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
2771                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
2772                      !!!cp ('t167');
2773                      !!!parse-error (type => 'not closed',
2774                                      text => $self->{open_elements}->[-1]->[0]
2775                                          ->manakai_local_name,
2776                                      token => $token);
2777                    } else {
2778                      !!!cp ('t168');
2779                  }                  }
2780                                    
2781                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3271  sub _tree_construction_main ($) { Line 2785  sub _tree_construction_main ($) {
2785                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
2786                                    
2787                  !!!next-token;                  !!!next-token;
2788                  redo B;                  next B;
2789                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif (($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2790                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
2791                    !!!parse-error (type => 'unmatched end tag',
2792                                    text => $token->{tag_name}, token => $token);
2793                  ## Ignore the token                  ## Ignore the token
2794                  !!!next-token;                  !!!next-token;
2795                  redo B;                  next B;
2796                } else {                } else {
2797                    !!!cp ('t170');
2798                  #                  #
2799                }                }
2800              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
2801                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if (($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2802                  ## have a table element in table scope                  ## have a table element in table scope
2803                  my $i;                  my $i;
2804                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
2805                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
2806                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
2807                      $i = $_;                      if ($node->[1] == CAPTION_EL) {
2808                      last INSCOPE;                        !!!cp ('t171');
2809                    } elsif ({                        $i = $_;
2810                              table => 1, html => 1,                        last INSCOPE;
2811                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
2812                      last INSCOPE;                        !!!cp ('t172');
2813                          last;
2814                        }
2815                    }                    }
2816    
2817                      !!!cp ('t173');
2818                      !!!parse-error (type => 'unmatched end tag',
2819                                      text => $token->{tag_name}, token => $token);
2820                      ## Ignore the token
2821                      !!!next-token;
2822                      next B;
2823                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
2824                                    
2825                  ## generate implied end tags                  ## generate implied end tags
2826                  if ({                  while ($self->{open_elements}->[-1]->[1]
2827                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
2828                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
2829                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
2830                  }                  }
2831                                    
2832                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] == CAPTION_EL) {
2833                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
2834                      !!!parse-error (type => 'not closed',
2835                                      text => $self->{open_elements}->[-1]->[0]
2836                                          ->manakai_local_name,
2837                                      token => $token);
2838                    } else {
2839                      !!!cp ('t176');
2840                  }                  }
2841                                    
2842                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3325  sub _tree_construction_main ($) { Line 2846  sub _tree_construction_main ($) {
2846                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
2847                                    
2848                  !!!next-token;                  !!!next-token;
2849                  redo B;                  next B;
2850                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif (($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2851                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
2852                    !!!parse-error (type => 'unmatched end tag',
2853                                    text => $token->{tag_name}, token => $token);
2854                  ## Ignore the token                  ## Ignore the token
2855                  !!!next-token;                  !!!next-token;
2856                  redo B;                  next B;
2857                } else {                } else {
2858                    !!!cp ('t178');
2859                  #                  #
2860                }                }
2861              } elsif ({              } elsif ({
2862                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
2863                        thead => 1, tr => 1,                        thead => 1, tr => 1,
2864                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
2865                       $self->{insertion_mode} == IN_CELL_IM) {                       ($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2866                ## have an element in table scope                ## have an element in table scope
2867                my $i;                my $i;
2868                my $tn;                my $tn;
2869                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
2870                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
2871                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
2872                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
2873                    last INSCOPE;                      !!!cp ('t179');
2874                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
2875                    $tn = $node->[1];  
2876                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
2877                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
2878                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
2879                            table => 1, html => 1,                                line => $token->{line},
2880                           }->{$node->[1]}) {                                column => $token->{column}};
2881                    last INSCOPE;                      next B;
2882                      } elsif ($node->[1] == TABLE_CELL_EL) {
2883                        !!!cp ('t180');
2884                        $tn = $node->[0]->manakai_local_name;
2885                        ## NOTE: There is exactly one |td| or |th| element
2886                        ## in scope in the stack of open elements by definition.
2887                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
2888                        ## ISSUE: Can this be reached?
2889                        !!!cp ('t181');
2890                        last;
2891                      }
2892                  }                  }
2893                } # INSCOPE  
2894                unless (defined $i) {                  !!!cp ('t182');
2895                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
2896                        text => $token->{tag_name}, token => $token);
2897                  ## Ignore the token                  ## Ignore the token
2898                  !!!next-token;                  !!!next-token;
2899                  redo B;                  next B;
2900                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
2901              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
2902                       $self->{insertion_mode} == IN_CAPTION_IM) {                       ($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2903                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
2904                                  token => $token);
2905    
2906                ## As if </caption>                ## As if </caption>
2907                ## have a table element in table scope                ## have a table element in table scope
2908                my $i;                my $i;
2909                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2910                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
2911                  if ($node->[1] eq 'caption') {                  if ($node->[1] == CAPTION_EL) {
2912                      !!!cp ('t184');
2913                    $i = $_;                    $i = $_;
2914                    last INSCOPE;                    last INSCOPE;
2915                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
2916                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
2917                    last INSCOPE;                    last INSCOPE;
2918                  }                  }
2919                } # INSCOPE                } # INSCOPE
2920                unless (defined $i) {                unless (defined $i) {
2921                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
2922            ## TODO: Wrong error type?
2923                    !!!parse-error (type => 'unmatched end tag',
2924                                    text => 'caption', token => $token);
2925                  ## Ignore the token                  ## Ignore the token
2926                  !!!next-token;                  !!!next-token;
2927                  redo B;                  next B;
2928                }                }
2929                                
2930                ## generate implied end tags                ## generate implied end tags
2931                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
2932                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
2933                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
2934                }                }
2935    
2936                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] == CAPTION_EL) {
2937                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
2938                    !!!parse-error (type => 'not closed',
2939                                    text => $self->{open_elements}->[-1]->[0]
2940                                        ->manakai_local_name,
2941                                    token => $token);
2942                  } else {
2943                    !!!cp ('t189');
2944                }                }
2945    
2946                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3418  sub _tree_construction_main ($) { Line 2950  sub _tree_construction_main ($) {
2950                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
2951    
2952                ## reprocess                ## reprocess
2953                redo B;                next B;
2954              } elsif ({              } elsif ({
2955                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
2956                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2957                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
2958                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
2959                    !!!parse-error (type => 'unmatched end tag',
2960                                    text => $token->{tag_name}, token => $token);
2961                  ## Ignore the token                  ## Ignore the token
2962                  !!!next-token;                  !!!next-token;
2963                  redo B;                  next B;
2964                } else {                } else {
2965                    !!!cp ('t191');
2966                  #                  #
2967                }                }
2968              } elsif ({          } elsif ({
2969                        tbody => 1, tfoot => 1,                    tbody => 1, tfoot => 1,
2970                        thead => 1, tr => 1,                    thead => 1, tr => 1,
2971                       }->{$token->{tag_name}} and                   }->{$token->{tag_name}} and
2972                       $self->{insertion_mode} == IN_CAPTION_IM) {                   ($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2973                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t192');
2974                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
2975                !!!next-token;                            text => $token->{tag_name}, token => $token);
2976                redo B;            ## Ignore the token
2977              } else {            !!!next-token;
2978                #            next B;
2979              }          } else {
2980              !!!cp ('t193');
2981              #
2982            }
2983          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2984            for my $entry (@{$self->{open_elements}}) {
2985              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
2986                !!!cp ('t75');
2987                !!!parse-error (type => 'in body:#eof', token => $token);
2988                last;
2989              }
2990            }
2991    
2992            ## Stop parsing.
2993            last B;
2994        } else {        } else {
2995          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2996        }        }
# Line 3450  sub _tree_construction_main ($) { Line 2999  sub _tree_construction_main ($) {
2999        #        #
3000      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3001        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3002              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
3003                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3004              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3005                                
3006                unless (length $token->{data}) {            unless (length $token->{data}) {
3007                  !!!next-token;              !!!cp ('t194');
3008                  redo B;              !!!next-token;
3009                }              next B;
3010              }            } else {
3011                !!!cp ('t195');
3012              }
3013            }
3014    
3015              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
3016    
3017              ## As if in body, but insert into foster parent element          ## NOTE: As if in body, but insert into the foster parent element.
3018              ## ISSUE: Spec says that "whenever a node would be inserted          $reconstruct_active_formatting_elements->($insert_to_foster);
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
3019                            
3020              if ({          if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3021                   table => 1, tbody => 1, tfoot => 1,            # MUST
3022                   thead => 1, tr => 1,            my $foster_parent_element;
3023                  }->{$self->{open_elements}->[-1]->[1]}) {            my $next_sibling;
3024                # MUST            my $prev_sibling;
3025                my $foster_parent_element;            OE: for (reverse 0..$#{$self->{open_elements}}) {
3026                my $next_sibling;              if ($self->{open_elements}->[$_]->[1] == TABLE_EL) {
3027                my $prev_sibling;                my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3028                OE: for (reverse 0..$#{$self->{open_elements}}) {                if (defined $parent and $parent->node_type == 1) {
3029                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $foster_parent_element = $parent;
3030                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  !!!cp ('t196');
3031                    if (defined $parent and $parent->node_type == 1) {                  $next_sibling = $self->{open_elements}->[$_]->[0];
3032                      $foster_parent_element = $parent;                  $prev_sibling = $next_sibling->previous_sibling;
3033                      $next_sibling = $self->{open_elements}->[$_]->[0];                  #
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
3034                } else {                } else {
3035                  $foster_parent_element->insert_before                  !!!cp ('t197');
3036                    ($self->{document}->create_text_node ($token->{data}),                  $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3037                     $next_sibling);                  $prev_sibling = $foster_parent_element->last_child;
3038                    #
3039                }                }
3040              } else {                last OE;
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
3041              }              }
3042              } # OE
3043              $foster_parent_element = $self->{open_elements}->[0]->[0] and
3044              $prev_sibling = $foster_parent_element->last_child
3045                  unless defined $foster_parent_element;
3046              undef $prev_sibling unless $open_tables->[-1]->[2]; # ~node inserted
3047              if (defined $prev_sibling and
3048                  $prev_sibling->node_type == 3) {
3049                !!!cp ('t198');
3050                $prev_sibling->manakai_append_text ($token->{data});
3051              } else {
3052                !!!cp ('t199');
3053                $foster_parent_element->insert_before
3054                    ($self->{document}->create_text_node ($token->{data}),
3055                     $next_sibling);
3056              }
3057              $open_tables->[-1]->[1] = 1; # tainted
3058              $open_tables->[-1]->[2] = 1; # ~node inserted
3059            } else {
3060              ## NOTE: Fragment case or in a foster parent'ed element
3061              ## (e.g. |<table><span>a|).  In fragment case, whether the
3062              ## character is appended to existing node or a new node is
3063              ## created is irrelevant, since the foster parent'ed nodes
3064              ## are discarded and fragment parsing does not invoke any
3065              ## script.
3066              !!!cp ('t200');
3067              $self->{open_elements}->[-1]->[0]->manakai_append_text
3068                  ($token->{data});
3069            }
3070                            
3071              !!!next-token;          !!!next-token;
3072              redo B;          next B;
3073        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3074              if ({          if ({
3075                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => (($self->{insertion_mode} & IM_MASK) != IN_ROW_IM),
3076                   th => 1, td => 1,               th => 1, td => 1,
3077                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
3078                if ($self->{insertion_mode} == IN_TABLE_IM) {            if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_IM) {
3079                  ## Clear back to table context              ## Clear back to table context
3080                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
3081                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
3082                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!cp ('t201');
3083                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3084                  }              }
3085                                
3086                  !!!insert-element ('tbody');              !!!insert-element ('tbody',, $token);
3087                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
3088                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
3089                }            }
3090              
3091                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_BODY_IM) {
3092                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
3093                    !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t202');
3094                  }                !!!parse-error (type => 'missing start tag:tr', token => $token);
3095                }
3096                                    
3097                  ## Clear back to table body context              ## Clear back to table body context
3098                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
3099                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
3100                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
3101                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                ## ISSUE: Can this case be reached?
3102                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3103                  }              }
3104                                    
3105                  $self->{insertion_mode} = IN_ROW_IM;              $self->{insertion_mode} = IN_ROW_IM;
3106                  if ($token->{tag_name} eq 'tr') {              if ($token->{tag_name} eq 'tr') {
3107                    !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t204');
3108                    !!!next-token;                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3109                    redo B;                $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3110                  } else {                !!!nack ('t204');
3111                    !!!insert-element ('tr');                !!!next-token;
3112                    ## reprocess in the "in row" insertion mode                next B;
3113                  }              } else {
3114                }                !!!cp ('t205');
3115                  !!!insert-element ('tr',, $token);
3116                  ## reprocess in the "in row" insertion mode
3117                }
3118              } else {
3119                !!!cp ('t206');
3120              }
3121    
3122                ## Clear back to table row context                ## Clear back to table row context
3123                while (not {                while (not ($self->{open_elements}->[-1]->[1]
3124                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
3125                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3126                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3127                }                }
3128                                
3129                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3130                $self->{insertion_mode} = IN_CELL_IM;            $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3131              $self->{insertion_mode} = IN_CELL_IM;
3132    
3133                push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
3134                                
3135              !!!nack ('t207.1');
3136              !!!next-token;
3137              next B;
3138            } elsif ({
3139                      caption => 1, col => 1, colgroup => 1,
3140                      tbody => 1, tfoot => 1, thead => 1,
3141                      tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3142                     }->{$token->{tag_name}}) {
3143              if (($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3144                ## As if </tr>
3145                ## have an element in table scope
3146                my $i;
3147                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3148                  my $node = $self->{open_elements}->[$_];
3149                  if ($node->[1] == TABLE_ROW_EL) {
3150                    !!!cp ('t208');
3151                    $i = $_;
3152                    last INSCOPE;
3153                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
3154                    !!!cp ('t209');
3155                    last INSCOPE;
3156                  }
3157                } # INSCOPE
3158                unless (defined $i) {
3159                  !!!cp ('t210');
3160                  ## TODO: This type is wrong.
3161                  !!!parse-error (type => 'unmacthed end tag',
3162                                  text => $token->{tag_name}, token => $token);
3163                  ## Ignore the token
3164                  !!!nack ('t210.1');
3165                !!!next-token;                !!!next-token;
3166                redo B;                next B;
3167              } elsif ({              }
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1,  
                       tr => 1, # $self->{insertion_mode} == IN_ROW_IM  
                      }->{$token->{tag_name}}) {  
               if ($self->{insertion_mode} == IN_ROW_IM) {  
                 ## As if </tr>  
                 ## have an element in table scope  
                 my $i;  
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                   my $node = $self->{open_elements}->[$_];  
                   if ($node->[1] eq 'tr') {  
                     $i = $_;  
                     last INSCOPE;  
                   } elsif ({  
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
                     last INSCOPE;  
                   }  
                 } # INSCOPE  
                 unless (defined $i) {  
                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  
                   ## Ignore the token  
                   !!!next-token;  
                   redo B;  
                 }  
3168                                    
3169                  ## Clear back to table row context                  ## Clear back to table row context
3170                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
3171                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
3172                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
3173                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
3174                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3175                  }                  }
3176                                    
3177                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3178                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3179                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3180                      !!!cp ('t212');
3181                    ## reprocess                    ## reprocess
3182                    redo B;                    !!!ack-later;
3183                      next B;
3184                  } else {                  } else {
3185                      !!!cp ('t213');
3186                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
3187                  }                  }
3188                }                }
3189    
3190                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_BODY_IM) {
3191                  ## have an element in table scope                  ## have an element in table scope
3192                  my $i;                  my $i;
3193                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3194                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3195                    if ({                    if ($node->[1] == TABLE_ROW_GROUP_EL) {
3196                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
3197                      $i = $_;                      $i = $_;
3198                      last INSCOPE;                      last INSCOPE;
3199                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
3200                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
3201                      last INSCOPE;                      last INSCOPE;
3202                    }                    }
3203                  } # INSCOPE                  } # INSCOPE
3204                  unless (defined $i) {                  unless (defined $i) {
3205                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
3206    ## TODO: This erorr type is wrong.
3207                      !!!parse-error (type => 'unmatched end tag',
3208                                      text => $token->{tag_name}, token => $token);
3209                    ## Ignore the token                    ## Ignore the token
3210                      !!!nack ('t216.1');
3211                    !!!next-token;                    !!!next-token;
3212                    redo B;                    next B;
3213                  }                  }
3214    
3215                  ## Clear back to table body context                  ## Clear back to table body context
3216                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
3217                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
3218                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
3219                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
3220                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3221                  }                  }
3222                                    
# Line 3649  sub _tree_construction_main ($) { Line 3230  sub _tree_construction_main ($) {
3230                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3231                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
3232                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
3233                  } else {
3234                    !!!cp ('t218');
3235                }                }
3236    
3237                if ($token->{tag_name} eq 'col') {            if ($token->{tag_name} eq 'col') {
3238                  ## Clear back to table context              ## Clear back to table context
3239                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
3240                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
3241                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!cp ('t219');
3242                    pop @{$self->{open_elements}};                ## ISSUE: Can this state be reached?
3243                  }                pop @{$self->{open_elements}};
3244                                }
3245                  !!!insert-element ('colgroup');              
3246                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;              !!!insert-element ('colgroup',, $token);
3247                  ## reprocess              $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3248                  redo B;              ## reprocess
3249                } elsif ({              $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3250                          caption => 1,              !!!ack-later;
3251                          colgroup => 1,              next B;
3252                          tbody => 1, tfoot => 1, thead => 1,            } elsif ({
3253                         }->{$token->{tag_name}}) {                      caption => 1,
3254                  ## Clear back to table context                      colgroup => 1,
3255                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                      tbody => 1, tfoot => 1, thead => 1,
3256                         $self->{open_elements}->[-1]->[1] ne 'html') {                     }->{$token->{tag_name}}) {
3257                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              ## Clear back to table context
3258                    while (not ($self->{open_elements}->[-1]->[1]
3259                                    & TABLE_SCOPING_EL)) {
3260                      !!!cp ('t220');
3261                      ## ISSUE: Can this state be reached?
3262                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3263                  }                  }
3264                                    
3265                  push @$active_formatting_elements, ['#marker', '']              push @$active_formatting_elements, ['#marker', '']
3266                      if $token->{tag_name} eq 'caption';                  if $token->{tag_name} eq 'caption';
3267                                    
3268                  !!!insert-element ($token->{tag_name}, $token->{attributes});              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3269                  $self->{insertion_mode} = {              $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3270                                             caption => IN_CAPTION_IM,              $self->{insertion_mode} = {
3271                                             colgroup => IN_COLUMN_GROUP_IM,                                         caption => IN_CAPTION_IM,
3272                                             tbody => IN_TABLE_BODY_IM,                                         colgroup => IN_COLUMN_GROUP_IM,
3273                                             tfoot => IN_TABLE_BODY_IM,                                         tbody => IN_TABLE_BODY_IM,
3274                                             thead => IN_TABLE_BODY_IM,                                         tfoot => IN_TABLE_BODY_IM,
3275                                            }->{$token->{tag_name}};                                         thead => IN_TABLE_BODY_IM,
3276                  !!!next-token;                                        }->{$token->{tag_name}};
3277                  redo B;              !!!next-token;
3278                } else {              !!!nack ('t220.1');
3279                  die "$0: in table: <>: $token->{tag_name}";              next B;
3280                }            } else {
3281                die "$0: in table: <>: $token->{tag_name}";
3282              }
3283              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3284                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
3285                                  text => $self->{open_elements}->[-1]->[0]
3286                                      ->manakai_local_name,
3287                                  token => $token);
3288    
3289                ## As if </table>                ## As if </table>
3290                ## have a table element in table scope                ## have a table element in table scope
3291                my $i;                my $i;
3292                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3293                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3294                  if ($node->[1] eq 'table') {                  if ($node->[1] == TABLE_EL) {
3295                      !!!cp ('t221');
3296                    $i = $_;                    $i = $_;
3297                    last INSCOPE;                    last INSCOPE;
3298                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
3299                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
3300                    last INSCOPE;                    last INSCOPE;
3301                  }                  }
3302                } # INSCOPE                } # INSCOPE
3303                unless (defined $i) {                unless (defined $i) {
3304                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
3305    ## TODO: The following is wrong, maybe.
3306                    !!!parse-error (type => 'unmatched end tag', text => 'table',
3307                                    token => $token);
3308                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
3309                    !!!nack ('t223.1');
3310                  !!!next-token;                  !!!next-token;
3311                  redo B;                  next B;
3312                }                }
3313                                
3314    ## TODO: Followings are removed from the latest spec.
3315                ## generate implied end tags                ## generate implied end tags
3316                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
3317                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
3318                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
3319                }                }
3320    
3321                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] == TABLE_EL) {
3322                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
3323                    ## NOTE: |<table><tr><table>|
3324                    !!!parse-error (type => 'not closed',
3325                                    text => $self->{open_elements}->[-1]->[0]
3326                                        ->manakai_local_name,
3327                                    token => $token);
3328                  } else {
3329                    !!!cp ('t226');
3330                }                }
3331    
3332                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3333                  pop @{$open_tables};
3334    
3335                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3336    
3337                ## reprocess            ## reprocess
3338                redo B;            !!!ack-later;
3339          } else {            next B;
3340            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
3341              if (not $open_tables->[-1]->[1]) { # tainted
3342                !!!cp ('t227.8');
3343                ## NOTE: This is a "as if in head" code clone.
3344                $parse_rcdata->(CDATA_CONTENT_MODEL);
3345                $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3346                next B;
3347              } else {
3348                !!!cp ('t227.7');
3349                #
3350              }
3351            } elsif ($token->{tag_name} eq 'script') {
3352              if (not $open_tables->[-1]->[1]) { # tainted
3353                !!!cp ('t227.6');
3354                ## NOTE: This is a "as if in head" code clone.
3355                $script_start_tag->();
3356                $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3357                next B;
3358              } else {
3359                !!!cp ('t227.5');
3360                #
3361              }
3362            } elsif ($token->{tag_name} eq 'input') {
3363              if (not $open_tables->[-1]->[1]) { # tainted
3364                if ($token->{attributes}->{type}) { ## TODO: case
3365                  my $type = lc $token->{attributes}->{type}->{value};
3366                  if ($type eq 'hidden') {
3367                    !!!cp ('t227.3');
3368                    !!!parse-error (type => 'in table',
3369                                    text => $token->{tag_name}, token => $token);
3370    
3371            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3372                    $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3373    
3374                    ## TODO: form element pointer
3375    
3376                    pop @{$self->{open_elements}};
3377    
3378                    !!!next-token;
3379                    !!!ack ('t227.2.1');
3380                    next B;
3381                  } else {
3382                    !!!cp ('t227.2');
3383                    #
3384                  }
3385                } else {
3386                  !!!cp ('t227.1');
3387                  #
3388                }
3389              } else {
3390                !!!cp ('t227.4');
3391                #
3392              }
3393            } else {
3394              !!!cp ('t227');
3395            #            #
3396          }          }
3397    
3398            !!!parse-error (type => 'in table', text => $token->{tag_name},
3399                            token => $token);
3400    
3401            $insert = $insert_to_foster;
3402            #
3403        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
3404              if ($token->{tag_name} eq 'tr' and          if ($token->{tag_name} eq 'tr' and
3405                  $self->{insertion_mode} == IN_ROW_IM) {              ($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3406                ## have an element in table scope            ## have an element in table scope
3407                my $i;                my $i;
3408                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3409                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3410                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] == TABLE_ROW_EL) {
3411                      !!!cp ('t228');
3412                    $i = $_;                    $i = $_;
3413                    last INSCOPE;                    last INSCOPE;
3414                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
3415                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
3416                    last INSCOPE;                    last INSCOPE;
3417                  }                  }
3418                } # INSCOPE                } # INSCOPE
3419                unless (defined $i) {                unless (defined $i) {
3420                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
3421                    !!!parse-error (type => 'unmatched end tag',
3422                                    text => $token->{tag_name}, token => $token);
3423                  ## Ignore the token                  ## Ignore the token
3424                    !!!nack ('t230.1');
3425                  !!!next-token;                  !!!next-token;
3426                  redo B;                  next B;
3427                  } else {
3428                    !!!cp ('t232');
3429                }                }
3430    
3431                ## Clear back to table row context                ## Clear back to table row context
3432                while (not {                while (not ($self->{open_elements}->[-1]->[1]
3433                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
3434                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
3435                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
3436                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3437                }                }
3438    
3439                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
3440                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3441                !!!next-token;                !!!next-token;
3442                redo B;                !!!nack ('t231.1');
3443                  next B;
3444              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3445                if ($self->{insertion_mode} == IN_ROW_IM) {                if (($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3446                  ## As if </tr>                  ## As if </tr>
3447                  ## have an element in table scope                  ## have an element in table scope
3448                  my $i;                  my $i;
3449                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3450                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3451                    if ($node->[1] eq 'tr') {                    if ($node->[1] == TABLE_ROW_EL) {
3452                        !!!cp ('t233');
3453                      $i = $_;                      $i = $_;
3454                      last INSCOPE;                      last INSCOPE;
3455                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
3456                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
3457                      last INSCOPE;                      last INSCOPE;
3458                    }                    }
3459                  } # INSCOPE                  } # INSCOPE
3460                  unless (defined $i) {                  unless (defined $i) {
3461                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
3462    ## TODO: The following is wrong.
3463                      !!!parse-error (type => 'unmatched end tag',
3464                                      text => $token->{type}, token => $token);
3465                    ## Ignore the token                    ## Ignore the token
3466                      !!!nack ('t236.1');
3467                    !!!next-token;                    !!!next-token;
3468                    redo B;                    next B;
3469                  }                  }
3470                                    
3471                  ## Clear back to table row context                  ## Clear back to table row context
3472                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
3473                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
3474                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
3475                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
3476                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3477                  }                  }
3478                                    
# Line 3816  sub _tree_construction_main ($) { Line 3481  sub _tree_construction_main ($) {
3481                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3482                }                }
3483    
3484                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_BODY_IM) {
3485                  ## have an element in table scope                  ## have an element in table scope
3486                  my $i;                  my $i;
3487                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3488                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3489                    if ({                    if ($node->[1] == TABLE_ROW_GROUP_EL) {
3490                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
3491                      $i = $_;                      $i = $_;
3492                      last INSCOPE;                      last INSCOPE;
3493                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
3494                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
3495                      last INSCOPE;                      last INSCOPE;
3496                    }                    }
3497                  } # INSCOPE                  } # INSCOPE
3498                  unless (defined $i) {                  unless (defined $i) {
3499                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
3500                      !!!parse-error (type => 'unmatched end tag',
3501                                      text => $token->{tag_name}, token => $token);
3502                    ## Ignore the token                    ## Ignore the token
3503                      !!!nack ('t239.1');
3504                    !!!next-token;                    !!!next-token;
3505                    redo B;                    next B;
3506                  }                  }
3507                                    
3508                  ## Clear back to table body context                  ## Clear back to table body context
3509                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
3510                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
3511                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3512                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3513                  }                  }
3514                                    
# Line 3859  sub _tree_construction_main ($) { Line 3524  sub _tree_construction_main ($) {
3524                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
3525                }                }
3526    
3527                  ## NOTE: </table> in the "in table" insertion mode.
3528                  ## When you edit the code fragment below, please ensure that
3529                  ## the code for <table> in the "in table" insertion mode
3530                  ## is synced with it.
3531    
3532                ## have a table element in table scope                ## have a table element in table scope
3533                my $i;                my $i;
3534                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3535                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3536                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] == TABLE_EL) {
3537                      !!!cp ('t241');
3538                    $i = $_;                    $i = $_;
3539                    last INSCOPE;                    last INSCOPE;
3540                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
3541                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
3542                    last INSCOPE;                    last INSCOPE;
3543                  }                  }
3544                } # INSCOPE                } # INSCOPE
3545                unless (defined $i) {                unless (defined $i) {
3546                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
3547                    !!!parse-error (type => 'unmatched end tag',
3548                                    text => $token->{tag_name}, token => $token);
3549                  ## Ignore the token                  ## Ignore the token
3550                    !!!nack ('t243.1');
3551                  !!!next-token;                  !!!next-token;
3552                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3553                }                }
3554                                    
3555                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3556                  pop @{$open_tables};
3557                                
3558                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3559                                
3560                !!!next-token;                !!!next-token;
3561                redo B;                next B;
3562              } elsif ({              } elsif ({
3563                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3564                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3565                       $self->{insertion_mode} & ROW_IMS) {                       $self->{insertion_mode} & ROW_IMS) {
3566                if ($self->{insertion_mode} == IN_ROW_IM) {                if (($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3567                  ## have an element in table scope                  ## have an element in table scope
3568                  my $i;                  my $i;
3569                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3570                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3571                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
3572                        !!!cp ('t247');
3573                      $i = $_;                      $i = $_;
3574                      last INSCOPE;                      last INSCOPE;
3575                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
3576                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
3577                      last INSCOPE;                      last INSCOPE;
3578                    }                    }
3579                  } # INSCOPE                  } # INSCOPE
3580                    unless (defined $i) {                    unless (defined $i) {
3581                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
3582                        !!!parse-error (type => 'unmatched end tag',
3583                                        text => $token->{tag_name}, token => $token);
3584                      ## Ignore the token                      ## Ignore the token
3585                        !!!nack ('t249.1');
3586                      !!!next-token;                      !!!next-token;
3587                      redo B;                      next B;
3588                    }                    }
3589                                    
3590                  ## As if </tr>                  ## As if </tr>
# Line 3931  sub _tree_construction_main ($) { Line 3592  sub _tree_construction_main ($) {
3592                  my $i;                  my $i;
3593                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3594                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3595                    if ($node->[1] eq 'tr') {                    if ($node->[1] == TABLE_ROW_EL) {
3596                        !!!cp ('t250');
3597                      $i = $_;                      $i = $_;
3598                      last INSCOPE;                      last INSCOPE;
3599                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
3600                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
3601                      last INSCOPE;                      last INSCOPE;
3602                    }                    }
3603                  } # INSCOPE                  } # INSCOPE
3604                    unless (defined $i) {                    unless (defined $i) {
3605                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
3606                        !!!parse-error (type => 'unmatched end tag',
3607                                        text => 'tr', token => $token);
3608                      ## Ignore the token                      ## Ignore the token
3609                        !!!nack ('t252.1');
3610                      !!!next-token;                      !!!next-token;
3611                      redo B;                      next B;
3612                    }                    }
3613                                    
3614                  ## Clear back to table row context                  ## Clear back to table row context
3615                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
3616                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
3617                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
3618                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
3619                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3620                  }                  }
3621                                    
# Line 3964  sub _tree_construction_main ($) { Line 3628  sub _tree_construction_main ($) {
3628                my $i;                my $i;
3629                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3630                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3631                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
3632                      !!!cp ('t254');
3633                    $i = $_;                    $i = $_;
3634                    last INSCOPE;                    last INSCOPE;
3635                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
3636                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
3637                    last INSCOPE;                    last INSCOPE;
3638                  }                  }
3639                } # INSCOPE                } # INSCOPE
3640                unless (defined $i) {                unless (defined $i) {
3641                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
3642                    !!!parse-error (type => 'unmatched end tag',
3643                                    text => $token->{tag_name}, token => $token);
3644                  ## Ignore the token                  ## Ignore the token
3645                    !!!nack ('t256.1');
3646                  !!!next-token;                  !!!next-token;
3647                  redo B;                  next B;
3648                }                }
3649    
3650                ## Clear back to table body context                ## Clear back to table body context
3651                while (not {                while (not ($self->{open_elements}->[-1]->[1]
3652                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
3653                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
3654                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
3655                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3656                }                }
3657    
3658                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3659                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
3660                  !!!nack ('t257.1');
3661                !!!next-token;                !!!next-token;
3662                redo B;                next B;
3663              } elsif ({              } elsif ({
3664                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3665                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
3666                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3667                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3668                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3669                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
3670                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
3671                !!!next-token;                            text => $token->{tag_name}, token => $token);
3672                redo B;            ## Ignore the token
3673          } else {            !!!nack ('t258.1');
3674            !!!parse-error (type => 'in table:/'.$token->{tag_name});             !!!next-token;
3675              next B;
3676            } else {
3677              !!!cp ('t259');
3678              !!!parse-error (type => 'in table:/',
3679                              text => $token->{tag_name}, token => $token);
3680    
3681            $insert = $insert_to_foster;            $insert = $insert_to_foster;
3682            #            #
3683          }          }
3684          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3685            unless ($self->{open_elements}->[-1]->[1] == HTML_EL and
3686                    @{$self->{open_elements}} == 1) { # redundant, maybe
3687              !!!parse-error (type => 'in body:#eof', token => $token);
3688              !!!cp ('t259.1');
3689              #
3690            } else {
3691              !!!cp ('t259.2');
3692              #
3693            }
3694    
3695            ## Stop parsing
3696            last B;
3697        } else {        } else {
3698          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3699        }        }
3700      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {      } elsif (($self->{insertion_mode} & IM_MASK) == IN_COLUMN_GROUP_IM) {
3701            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
3702              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3703                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3704                unless (length $token->{data}) {                unless (length $token->{data}) {
3705                    !!!cp ('t260');
3706                  !!!next-token;                  !!!next-token;
3707                  redo B;                  next B;
3708                }                }
3709              }              }
3710                            
3711                !!!cp ('t261');
3712              #              #
3713            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
3714              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
3715                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
3716                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3717                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3718                  !!!ack ('t262.1');
3719                !!!next-token;                !!!next-token;
3720                redo B;                next B;
3721              } else {              } else {
3722                  !!!cp ('t263');
3723                #                #
3724              }              }
3725            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
3726              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
3727                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] == HTML_EL) {
3728                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
3729                    !!!parse-error (type => 'unmatched end tag',
3730                                    text => 'colgroup', token => $token);
3731                  ## Ignore the token                  ## Ignore the token
3732                  !!!next-token;                  !!!next-token;
3733                  redo B;                  next B;
3734                } else {                } else {
3735                    !!!cp ('t265');
3736                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
3737                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
3738                  !!!next-token;                  !!!next-token;
3739                  redo B;                              next B;            
3740                }                }
3741              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
3742                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
3743                  !!!parse-error (type => 'unmatched end tag',
3744                                  text => 'col', token => $token);
3745                ## Ignore the token                ## Ignore the token
3746                !!!next-token;                !!!next-token;
3747                redo B;                next B;
3748              } else {              } else {
3749                  !!!cp ('t267');
3750                #                #
3751              }              }
3752            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3753              #          if ($self->{open_elements}->[-1]->[1] == HTML_EL and
3754            }              @{$self->{open_elements}} == 1) { # redundant, maybe
3755              !!!cp ('t270.2');
3756              ## Stop parsing.
3757              last B;
3758            } else {
3759              ## NOTE: As if </colgroup>.
3760              !!!cp ('t270.1');
3761              pop @{$self->{open_elements}}; # colgroup
3762              $self->{insertion_mode} = IN_TABLE_IM;
3763              ## Reprocess.
3764              next B;
3765            }
3766          } else {
3767            die "$0: $token->{type}: Unknown token type";
3768          }
3769    
3770            ## As if </colgroup>            ## As if </colgroup>
3771            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] == HTML_EL) {
3772              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
3773    ## TODO: Wrong error type?
3774                !!!parse-error (type => 'unmatched end tag',
3775                                text => 'colgroup', token => $token);
3776              ## Ignore the token              ## Ignore the token
3777                !!!nack ('t269.1');
3778              !!!next-token;              !!!next-token;
3779              redo B;              next B;
3780            } else {            } else {
3781                !!!cp ('t270');
3782              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
3783              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
3784                !!!ack-later;
3785              ## reprocess              ## reprocess
3786              redo B;              next B;
3787            }            }
3788      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
3789        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3790            !!!cp ('t271');
3791          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3792          !!!next-token;          !!!next-token;
3793          redo B;          next B;
3794        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3795              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
3796                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] == OPTION_EL) {
3797                  ## As if </option>              !!!cp ('t272');
3798                  pop @{$self->{open_elements}};              ## As if </option>
3799                }              pop @{$self->{open_elements}};
3800              } else {
3801                !!!cp ('t273');
3802              }
3803    
3804                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3805                !!!next-token;            !!!nack ('t273.1');
3806                redo B;            !!!next-token;
3807              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
3808                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
3809                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] == OPTION_EL) {
3810                  pop @{$self->{open_elements}};              !!!cp ('t274');
3811                }              ## As if </option>
3812                pop @{$self->{open_elements}};
3813              } else {
3814                !!!cp ('t275');
3815              }
3816    
3817                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] == OPTGROUP_EL) {
3818                  ## As if </optgroup>              !!!cp ('t276');
3819                  pop @{$self->{open_elements}};              ## As if </optgroup>
3820                }              pop @{$self->{open_elements}};
3821              } else {
3822                !!!cp ('t277');
3823              }
3824    
3825                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3826                !!!next-token;            !!!nack ('t277.1');
3827                redo B;            !!!next-token;
3828              } elsif ($token->{tag_name} eq 'select') {            next B;
3829                !!!parse-error (type => 'not closed:select');          } elsif ({
3830                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
3831                ## have an element in table scope                   }->{$token->{tag_name}} or
3832                my $i;                   (($self->{insertion_mode} & IM_MASK)
3833                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                        == IN_SELECT_IN_TABLE_IM and
3834                  my $node = $self->{open_elements}->[$_];                    {
3835                  if ($node->[1] eq $token->{tag_name}) {                     caption => 1, table => 1,
3836                    $i = $_;                     tbody => 1, tfoot => 1, thead => 1,
3837                    last INSCOPE;                     tr => 1, td => 1, th => 1,
3838                  } elsif ({                    }->{$token->{tag_name}})) {
3839                            table => 1, html => 1,            ## TODO: The type below is not good - <select> is replaced by </select>
3840                           }->{$node->[1]}) {            !!!parse-error (type => 'not closed', text => 'select',
3841                    last INSCOPE;                            token => $token);
3842                  }            ## NOTE: As if the token were </select> (<select> case) or
3843                } # INSCOPE            ## as if there were </select> (otherwise).
3844                unless (defined $i) {            ## have an element in table scope
3845                  !!!parse-error (type => 'unmatched end tag:select');            my $i;
3846                  ## Ignore the token            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3847                  !!!next-token;              my $node = $self->{open_elements}->[$_];
3848                  redo B;              if ($node->[1] == SELECT_EL) {
3849                }                !!!cp ('t278');
3850                  $i = $_;
3851                  last INSCOPE;
3852                } elsif ($node->[1] & TABLE_SCOPING_EL) {
3853                  !!!cp ('t279');
3854                  last INSCOPE;
3855                }
3856              } # INSCOPE
3857              unless (defined $i) {
3858                !!!cp ('t280');
3859                !!!parse-error (type => 'unmatched end tag',
3860                                text => 'select', token => $token);
3861                ## Ignore the token
3862                !!!nack ('t280.1');
3863                !!!next-token;
3864                next B;
3865              }
3866                                
3867                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
3868              splice @{$self->{open_elements}}, $i;
3869    
3870                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
3871    
3872                !!!next-token;            if ($token->{tag_name} eq 'select') {
3873                redo B;              !!!nack ('t281.2');
3874                !!!next-token;
3875                next B;
3876              } else {
3877                !!!cp ('t281.1');
3878                !!!ack-later;
3879                ## Reprocess the token.
3880                next B;
3881              }
3882          } else {          } else {
3883            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
3884              !!!parse-error (type => 'in select',
3885                              text => $token->{tag_name}, token => $token);
3886            ## Ignore the token            ## Ignore the token
3887              !!!nack ('t282.1');
3888            !!!next-token;            !!!next-token;
3889            redo B;            next B;
3890          }          }
3891        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
3892              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
3893                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] == OPTION_EL and
3894                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] == OPTGROUP_EL) {
3895                  ## As if </option>              !!!cp ('t283');
3896                  splice @{$self->{open_elements}}, -2;              ## As if </option>
3897                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
3898                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] == OPTGROUP_EL) {
3899                } else {              !!!cp ('t284');
3900                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
3901                  ## Ignore the token            } else {
3902                }              !!!cp ('t285');
3903                !!!next-token;              !!!parse-error (type => 'unmatched end tag',
3904                redo B;                              text => $token->{tag_name}, token => $token);
3905              } elsif ($token->{tag_name} eq 'option') {              ## Ignore the token
3906                if ($self->{open_elements}->[-1]->[1] eq 'option') {            }
3907                  pop @{$self->{open_elements}};            !!!nack ('t285.1');
3908                } else {            !!!next-token;
3909                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            next B;
3910                  ## Ignore the token          } elsif ($token->{tag_name} eq 'option') {
3911                }            if ($self->{open_elements}->[-1]->[1] == OPTION_EL) {
3912                !!!next-token;              !!!cp ('t286');
3913                redo B;              pop @{$self->{open_elements}};
3914              } elsif ($token->{tag_name} eq 'select') {            } else {
3915                ## have an element in table scope              !!!cp ('t287');
3916                my $i;              !!!parse-error (type => 'unmatched end tag',
3917                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                              text => $token->{tag_name}, token => $token);
3918                  my $node = $self->{open_elements}->[$_];              ## Ignore the token
3919                  if ($node->[1] eq $token->{tag_name}) {            }
3920                    $i = $_;            !!!nack ('t287.1');
3921                    last INSCOPE;            !!!next-token;
3922                  } elsif ({            next B;
3923                            table => 1, html => 1,          } elsif ($token->{tag_name} eq 'select') {
3924                           }->{$node->[1]}) {            ## have an element in table scope
3925                    last INSCOPE;            my $i;
3926                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3927                } # INSCOPE              my $node = $self->{open_elements}->[$_];
3928                unless (defined $i) {              if ($node->[1] == SELECT_EL) {
3929                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t288');
3930                  ## Ignore the token                $i = $_;
3931                  !!!next-token;                last INSCOPE;
3932                  redo B;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
3933                }                !!!cp ('t289');
3934                  last INSCOPE;
3935                }
3936              } # INSCOPE
3937              unless (defined $i) {
3938                !!!cp ('t290');
3939                !!!parse-error (type => 'unmatched end tag',
3940                                text => $token->{tag_name}, token => $token);
3941                ## Ignore the token
3942                !!!nack ('t290.1');
3943                !!!next-token;
3944                next B;
3945              }
3946                                
3947                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
3948              splice @{$self->{open_elements}}, $i;
3949    
3950                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
3951    
3952                !!!next-token;            !!!nack ('t291.1');
3953                redo B;            !!!next-token;
3954              } elsif ({            next B;
3955                        caption => 1, table => 1, tbody => 1,          } elsif (($self->{insertion_mode} & IM_MASK)
3956                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                       == IN_SELECT_IN_TABLE_IM and
3957                       }->{$token->{tag_name}}) {                   {
3958                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    caption => 1, table => 1, tbody => 1,
3959                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
3960                     }->{$token->{tag_name}}) {
3961    ## TODO: The following is wrong?
3962              !!!parse-error (type => 'unmatched end tag',
3963                              text => $token->{tag_name}, token => $token);
3964                                
3965                ## have an element in table scope            ## have an element in table scope
3966                my $i;            my $i;
3967                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3968                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
3969                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
3970                    $i = $_;                !!!cp ('t292');
3971                    last INSCOPE;                $i = $_;
3972                  } elsif ({                last INSCOPE;
3973                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
3974                           }->{$node->[1]}) {                !!!cp ('t293');
3975                    last INSCOPE;                last INSCOPE;
3976                  }              }
3977                } # INSCOPE            } # INSCOPE
3978                unless (defined $i) {            unless (defined $i) {
3979                  ## Ignore the token              !!!cp ('t294');
3980                  !!!next-token;              ## Ignore the token
3981                  redo B;              !!!nack ('t294.1');
3982                }              !!!next-token;
3983                next B;
3984              }
3985                                
3986                ## As if </select>            ## As if </select>
3987                ## have an element in table scope            ## have an element in table scope
3988                undef $i;            undef $i;
3989                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3990                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
3991                  if ($node->[1] eq 'select') {              if ($node->[1] == SELECT_EL) {
3992                    $i = $_;                !!!cp ('t295');
3993                    last INSCOPE;                $i = $_;
3994                  } elsif ({                last INSCOPE;
3995                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
3996                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
3997                    last INSCOPE;                !!!cp ('t296');
3998                  }                last INSCOPE;
3999                } # INSCOPE              }
4000                unless (defined $i) {            } # INSCOPE
4001                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
4002                  ## Ignore the </select> token              !!!cp ('t297');
4003                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
4004                  redo B;              !!!parse-error (type => 'unmatched end tag',
4005                }                              text => 'select', token => $token);
4006                ## Ignore the </select> token
4007                !!!nack ('t297.1');
4008                !!!next-token; ## TODO: ok?
4009                next B;
4010              }
4011                                
4012                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
4013              splice @{$self->{open_elements}}, $i;
4014    
4015                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
4016    
4017                ## reprocess            !!!ack-later;
4018                redo B;            ## reprocess
4019              next B;
4020          } else {          } else {
4021            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
4022              !!!parse-error (type => 'in select:/',
4023                              text => $token->{tag_name}, token => $token);
4024            ## Ignore the token            ## Ignore the token
4025              !!!nack ('t299.3');
4026            !!!next-token;            !!!next-token;
4027            redo B;            next B;
4028          }          }
4029          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4030            unless ($self->{open_elements}->[-1]->[1] == HTML_EL and
4031                    @{$self->{open_elements}} == 1) { # redundant, maybe
4032              !!!cp ('t299.1');
4033              !!!parse-error (type => 'in body:#eof', token => $token);
4034            } else {
4035              !!!cp ('t299.2');
4036            }
4037    
4038            ## Stop parsing.
4039            last B;
4040        } else {        } else {
4041          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4042        }        }
4043      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4044        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4045          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
4046            my $data = $1;            my $data = $1;
4047            ## As if in body            ## As if in body
4048            $reconstruct_active_formatting_elements->($insert_to_current);            $reconstruct_active_formatting_elements->($insert_to_current);
# Line 4253  sub _tree_construction_main ($) { Line 4050  sub _tree_construction_main ($) {
4050            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4051                        
4052            unless (length $token->{data}) {            unless (length $token->{data}) {
4053                !!!cp ('t300');
4054              !!!next-token;              !!!next-token;
4055              redo B;              next B;
4056            }            }
4057          }          }
4058                    
4059          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4060            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
4061              !!!parse-error (type => 'after html:#text', token => $token);
4062            ## Reprocess in the "main" phase, "after body" insertion mode...            #
4063            } else {
4064              !!!cp ('t302');
4065              ## "after body" insertion mode
4066              !!!parse-error (type => 'after body:#text', token => $token);
4067              #
4068          }          }
           
         ## "after body" insertion mode  
         !!!parse-error (type => 'after body:#character');  
4069    
4070          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4071          ## reprocess          ## reprocess
4072          redo B;          next B;
4073        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4074          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4075            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
4076                        !!!parse-error (type => 'after html',
4077            ## Reprocess in the "main" phase, "after body" insertion mode...                            text => $token->{tag_name}, token => $token);
4078              #
4079            } else {
4080              !!!cp ('t304');
4081              ## "after body" insertion mode
4082              !!!parse-error (type => 'after body',
4083                              text => $token->{tag_name}, token => $token);
4084              #
4085          }          }
4086    
         ## "after body" insertion mode  
         !!!parse-error (type => 'after body:'.$token->{tag_name});  
   
4087          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4088            !!!ack-later;
4089          ## reprocess          ## reprocess
4090          redo B;          next B;
4091        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4092          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4093            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
4094              !!!parse-error (type => 'after html:/',
4095                              text => $token->{tag_name}, token => $token);
4096                        
4097            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
4098            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess.
4099              next B;
4100            } else {
4101              !!!cp ('t306');
4102          }          }
4103    
4104          ## "after body" insertion mode          ## "after body" insertion mode
4105          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
4106            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
4107              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
4108                !!!parse-error (type => 'unmatched end tag',
4109                                text => 'html', token => $token);
4110              ## Ignore the token              ## Ignore the token
4111              !!!next-token;              !!!next-token;
4112              redo B;              next B;
4113            } else {            } else {
4114                !!!cp ('t308');
4115              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4116              !!!next-token;              !!!next-token;
4117              redo B;              next B;
4118            }            }
4119          } else {          } else {
4120            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
4121              !!!parse-error (type => 'after body:/',
4122                              text => $token->{tag_name}, token => $token);
4123    
4124            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
4125            ## reprocess            ## reprocess
4126            redo B;            next B;
4127          }          }
4128          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4129            !!!cp ('t309.2');
4130            ## Stop parsing
4131            last B;
4132        } else {        } else {
4133          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4134        }        }
4135      } elsif ($self->{insertion_mode} & FRAME_IMS) {      } elsif ($self->{insertion_mode} & FRAME_IMS) {
4136        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4137          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
4138            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4139                        
4140            unless (length $token->{data}) {            unless (length $token->{data}) {
4141                !!!cp ('t310');
4142              !!!next-token;              !!!next-token;
4143              redo B;              next B;
4144            }            }
4145          }          }
4146                    
4147          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0C\x20]+//) {
4148            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4149              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
4150                !!!parse-error (type => 'in frameset:#text', token => $token);
4151            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4152              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
4153            } else { # "after html frameset"              !!!parse-error (type => 'after frameset:#text', token => $token);
4154              !!!parse-error (type => 'after html:#character');            } else { # "after after frameset"
4155                !!!cp ('t313');
4156              $self->{insertion_mode} = AFTER_FRAMESET_IM;              !!!parse-error (type => 'after html:#text', token => $token);
             ## Reprocess in the "main" phase, "after frameset"...  
             !!!parse-error (type => 'after frameset:#character');  
4157            }            }
4158                        
4159            ## Ignore the token.            ## Ignore the token.
4160            if (length $token->{data}) {            if (length $token->{data}) {
4161                !!!cp ('t314');
4162              ## reprocess the rest of characters              ## reprocess the rest of characters
4163            } else {            } else {
4164                !!!cp ('t315');
4165              !!!next-token;              !!!next-token;
4166            }            }
4167            redo B;            next B;
4168          }          }
4169                    
4170          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
4171        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!parse-error (type => 'after html:'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "main" phase, "after frameset" insertion mode...  
         }  
   
4172          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
4173              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4174            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
4175              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4176              !!!nack ('t318.1');
4177            !!!next-token;            !!!next-token;
4178            redo B;            next B;
4179          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
4180                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
4181            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
4182              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4183            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4184              !!!ack ('t319.1');
4185            !!!next-token;            !!!next-token;
4186            redo B;            next B;
4187          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
4188            ## NOTE: As if in body.            !!!cp ('t320');
4189            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            ## NOTE: As if in head.
4190            redo B;            $parse_rcdata->(CDATA_CONTENT_MODEL);
4191              next B;
4192    
4193              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
4194              ## has no parse error.
4195          } else {          } else {
4196            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4197              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
4198            } else {              !!!parse-error (type => 'in frameset',
4199              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
4200              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4201                !!!cp ('t322');
4202                !!!parse-error (type => 'after frameset',
4203                                text => $token->{tag_name}, token => $token);
4204              } else { # "after after frameset"
4205                !!!cp ('t322.2');
4206                !!!parse-error (type => 'after after frameset',
4207                                text => $token->{tag_name}, token => $token);
4208            }            }
4209            ## Ignore the token            ## Ignore the token
4210              !!!nack ('t322.1');
4211            !!!next-token;            !!!next-token;
4212            redo B;            next B;
4213          }          }
4214        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!parse-error (type => 'after html:/'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "main" phase, "after frameset" insertion mode...  
         }  
   
4215          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
4216              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4217            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] == HTML_EL and
4218                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4219              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
4220                !!!parse-error (type => 'unmatched end tag',
4221                                text => $token->{tag_name}, token => $token);
4222              ## Ignore the token              ## Ignore the token
4223              !!!next-token;              !!!next-token;
4224            } else {            } else {
4225                !!!cp ('t326');
4226              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
4227              !!!next-token;              !!!next-token;
4228            }            }
4229    
4230            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4231                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] == FRAMESET_EL)) {
4232                !!!cp ('t327');
4233              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4234              } else {
4235                !!!cp ('t328');
4236            }            }
4237            redo B;            next B;
4238          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
4239                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4240              !!!cp ('t329');
4241            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4242            !!!next-token;            !!!next-token;
4243            redo B;            next B;
4244          } else {          } else {
4245            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4246              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
4247            } else {              !!!parse-error (type => 'in frameset:/',
4248              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
4249              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4250                !!!cp ('t330.1');
4251                !!!parse-error (type => 'after frameset:/',
4252                                text => $token->{tag_name}, token => $token);
4253              } else { # "after after html"
4254                !!!cp ('t331');
4255                !!!parse-error (type => 'after after frameset:/',
4256                                text => $token->{tag_name}, token => $token);
4257            }            }
4258            ## Ignore the token            ## Ignore the token
4259            !!!next-token;            !!!next-token;
4260            redo B;            next B;
4261            }
4262          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4263            unless ($self->{open_elements}->[-1]->[1] == HTML_EL and
4264                    @{$self->{open_elements}} == 1) { # redundant, maybe
4265              !!!cp ('t331.1');
4266              !!!parse-error (type => 'in body:#eof', token => $token);
4267            } else {
4268              !!!cp ('t331.2');
4269          }          }
4270            
4271            ## Stop parsing
4272            last B;
4273        } else {        } else {
4274          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4275        }        }
   
       ## ISSUE: An issue in spec here  
4276      } else {      } else {
4277        die "$0: $self->{insertion_mode}: Unknown insertion mode";        die "$0: $self->{insertion_mode}: Unknown insertion mode";
4278      }      }
# Line 4432  sub _tree_construction_main ($) { Line 4280  sub _tree_construction_main ($) {
4280      ## "in body" insertion mode      ## "in body" insertion mode
4281      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
4282        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
4283            !!!cp ('t332');
4284          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4285          $script_start_tag->($insert);          $script_start_tag->();
4286          redo B;          next B;
4287        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
4288            !!!cp ('t333');
4289          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4290          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
4291          redo B;          next B;
4292        } elsif ({        } elsif ({
4293                  base => 1, link => 1,                  base => 1, command => 1, eventsource => 1, link => 1,
4294                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
4295            !!!cp ('t334');
4296          ## 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
4297          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4298          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}};
4299            !!!ack ('t334.1');
4300          !!!next-token;          !!!next-token;
4301          redo B;          next B;
4302        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
4303          ## 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
4304          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4305          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}};
4306    
4307          unless ($self->{confident}) {          unless ($self->{confident}) {
4308            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
4309                !!!cp ('t335');
4310                ## NOTE: Whether the encoding is supported or not is handled
4311                ## in the {change_encoding} callback.
4312              $self->{change_encoding}              $self->{change_encoding}
4313                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
4314                
4315                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4316                    ->set_user_data (manakai_has_reference =>
4317                                         $token->{attributes}->{charset}
4318                                             ->{has_reference});
4319            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4320              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
4321                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4322                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09\x0A\x0C\x0D\x20]*=
4323                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4324                        ([^"'\x09\x0A\x0C\x0D\x20][^\x09\x0A\x0C\x0D\x20\x3B]*))
4325                       /x) {
4326                  !!!cp ('t336');
4327                  ## NOTE: Whether the encoding is supported or not is handled
4328                  ## in the {change_encoding} callback.
4329                $self->{change_encoding}                $self->{change_encoding}
4330                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
4331                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4332                      ->set_user_data (manakai_has_reference =>
4333                                           $token->{attributes}->{content}
4334                                                 ->{has_reference});
4335              }              }
4336            }            }
4337            } else {
4338              if ($token->{attributes}->{charset}) {
4339                !!!cp ('t337');
4340                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4341                    ->set_user_data (manakai_has_reference =>
4342                                         $token->{attributes}->{charset}
4343                                             ->{has_reference});
4344              }
4345              if ($token->{attributes}->{content}) {
4346                !!!cp ('t338');
4347                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4348                    ->set_user_data (manakai_has_reference =>
4349                                         $token->{attributes}->{content}
4350                                             ->{has_reference});
4351              }
4352          }          }
4353    
4354            !!!ack ('t338.1');
4355          !!!next-token;          !!!next-token;
4356          redo B;          next B;
4357        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
4358          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
4359          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4360          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
4361            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
4362        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
4363          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
4364                                
4365          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
4366              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] == BODY_EL)) {
4367              !!!cp ('t342');
4368            ## Ignore the token            ## Ignore the token
4369          } else {          } else {
4370            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
4371            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
4372              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4373                  !!!cp ('t343');
4374                $body_el->set_attribute_ns                $body_el->set_attribute_ns
4375                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
4376                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
4377              }              }
4378            }            }
4379          }          }
4380            !!!nack ('t343.1');
4381          !!!next-token;          !!!next-token;
4382          redo B;          next B;
4383        } elsif ({        } elsif ({
4384                  address => 1, blockquote => 1, center => 1, dir => 1,                  ## NOTE: Start tags for non-phrasing flow content elements
4385                  div => 1, dl => 1, fieldset => 1, listing => 1,  
4386                  menu => 1, ol => 1, p => 1, ul => 1,                  ## NOTE: The normal one
4387                  pre => 1,                  address => 1, article => 1, aside => 1, blockquote => 1,
4388                    center => 1, datagrid => 1, details => 1, dialog => 1,
4389                    dir => 1, div => 1, dl => 1, fieldset => 1, figure => 1,
4390                    footer => 1, h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1,
4391                    h6 => 1, header => 1, menu => 1, nav => 1, ol => 1, p => 1,
4392                    section => 1, ul => 1,
4393                    ## NOTE: As normal, but drops leading newline
4394                    pre => 1, listing => 1,
4395                    ## NOTE: As normal, but interacts with the form element pointer
4396                    form => 1,
4397                    
4398                    table => 1,
4399                    hr => 1,
4400                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
4401            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
4402              !!!cp ('t350');
4403              !!!parse-error (type => 'in form:form', token => $token);
4404              ## Ignore the token
4405              !!!nack ('t350.1');
4406              !!!next-token;
4407              next B;
4408            }
4409    
4410          ## has a p element in scope          ## has a p element in scope
4411          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4412            if ($_->[1] eq 'p') {            if ($_->[1] == P_EL) {
4413              !!!back-token;              !!!cp ('t344');
4414              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
4415              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
4416            } elsif ({                        line => $token->{line}, column => $token->{column}};
4417                      table => 1, caption => 1, td => 1, th => 1,              next B;
4418                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
4419                     }->{$_->[1]}) {              !!!cp ('t345');
4420              last INSCOPE;              last INSCOPE;
4421            }            }
4422          } # INSCOPE          } # INSCOPE
4423                        
4424          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4425          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
4426              !!!nack ('t346.1');
4427            !!!next-token;            !!!next-token;
4428            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4429              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
4430              unless (length $token->{data}) {              unless (length $token->{data}) {
4431                  !!!cp ('t346');
4432                !!!next-token;                !!!next-token;
4433                } else {
4434                  !!!cp ('t349');
4435              }              }
4436              } else {
4437                !!!cp ('t348');
4438            }            }
4439          } else {          } elsif ($token->{tag_name} eq 'form') {
4440              !!!cp ('t347.1');
4441              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4442    
4443              !!!nack ('t347.2');
4444            !!!next-token;            !!!next-token;
4445          }          } elsif ($token->{tag_name} eq 'table') {
4446          redo B;            !!!cp ('t382');
4447        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
4448          if (defined $self->{form_element}) {            
4449            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
4450            ## Ignore the token  
4451              !!!nack ('t382.1');
4452              !!!next-token;
4453            } elsif ($token->{tag_name} eq 'hr') {
4454              !!!cp ('t386');
4455              pop @{$self->{open_elements}};
4456            
4457              !!!nack ('t386.1');
4458            !!!next-token;            !!!next-token;
           redo B;  
4459          } else {          } else {
4460            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
4461            !!!next-token;            !!!next-token;
           redo B;  
4462          }          }
4463            next B;
4464        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{tag_name} eq 'li') {
4465          ## has a p element in scope          ## NOTE: As normal, but imply </li> when there's another <li> ...
4466          INSCOPE: for (reverse @{$self->{open_elements}}) {  
4467            if ($_->[1] eq 'p') {          ## NOTE: Special, Scope (<li><foo><li> == <li><foo><li/></foo></li>)
4468              !!!back-token;            ## Interpreted as <li><foo/></li><li/> (non-conforming)
4469              $token = {type => END_TAG_TOKEN, tag_name => 'p'};            ## blockquote (O9.27), center (O), dd (Fx3, O, S3.1.2, IE7),
4470              redo B;            ## dt (Fx, O, S, IE), dl (O), fieldset (O, S, IE), form (Fx, O, S),
4471            } elsif ({            ## hn (O), pre (O), applet (O, S), button (O, S), marquee (Fx, O, S),
4472                      table => 1, caption => 1, td => 1, th => 1,            ## object (Fx)
4473                      button => 1, marquee => 1, object => 1, html => 1,            ## Generate non-tree (non-conforming)
4474                     }->{$_->[1]}) {            ## basefont (IE7 (where basefont is non-void)), center (IE),
4475              last INSCOPE;            ## form (IE), hn (IE)
4476            }          ## address, div, p (<li><foo><li> == <li><foo/></li><li/>)
4477          } # INSCOPE            ## Interpreted as <li><foo><li/></foo></li> (non-conforming)
4478                        ## div (Fx, S)
4479          ## Step 1  
4480            my $non_optional;
4481          my $i = -1;          my $i = -1;
4482          my $node = $self->{open_elements}->[$i];  
4483          LI: {          ## 1.
4484            ## Step 2          for my $node (reverse @{$self->{open_elements}}) {
4485            if ($node->[1] eq 'li') {            if ($node->[1] == LI_EL) {
4486              if ($i != -1) {              ## 2. (a) As if </li>
4487                !!!parse-error (type => 'end tag missing:'.              {
4488                                $self->{open_elements}->[-1]->[1]);                ## If no </li> - not applied
4489                  #
4490    
4491                  ## Otherwise
4492    
4493                  ## 1. generate implied end tags, except for </li>
4494                  #
4495    
4496                  ## 2. If current node != "li", parse error
4497                  if ($non_optional) {
4498                    !!!parse-error (type => 'not closed',
4499                                    text => $non_optional->[0]->manakai_local_name,
4500                                    token => $token);
4501                    !!!cp ('t355');
4502                  } else {
4503                    !!!cp ('t356');
4504                  }
4505    
4506                  ## 3. Pop
4507                  splice @{$self->{open_elements}}, $i;
4508              }              }
4509              splice @{$self->{open_elements}}, $i;  
4510              last LI;              last; ## 2. (b) goto 5.
4511            }            } elsif (
4512                                 ## NOTE: not "formatting" and not "phrasing"
4513            ## Step 3                     ($node->[1] & SPECIAL_EL or
4514            if (not $formatting_category->{$node->[1]} and                      $node->[1] & SCOPING_EL) and
4515                #not $phrasing_category->{$node->[1]} and                     ## NOTE: "li", "dt", and "dd" are in |SPECIAL_EL|.
4516                ($special_category->{$node->[1]} or                     (not $node->[1] & ADDRESS_DIV_P_EL)
4517                 $scoping_category->{$node->[1]}) and                    ) {
4518                $node->[1] ne 'address' and $node->[1] ne 'div') {              ## 3.
4519              last LI;              !!!cp ('t357');
4520                last; ## goto 5.
4521              } elsif ($node->[1] & END_TAG_OPTIONAL_EL) {
4522                !!!cp ('t358');
4523                #
4524              } else {
4525                !!!cp ('t359');
4526                $non_optional ||= $node;
4527                #
4528            }            }
4529                        ## 4.
4530            ## Step 4            ## goto 2.
4531            $i--;            $i--;
4532            $node = $self->{open_elements}->[$i];          }
4533            redo LI;  
4534          } # LI          ## 5. (a) has a |p| element in scope
             
         !!!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  
4535          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4536            if ($_->[1] eq 'p') {            if ($_->[1] == P_EL) {
4537              !!!back-token;              !!!cp ('t353');
4538              $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
4539              redo B;              ## NOTE: |<p><li>|, for example.
4540            } elsif ({  
4541                      table => 1, caption => 1, td => 1, th => 1,              !!!back-token; # <x>
4542                      button => 1, marquee => 1, object => 1, html => 1,              $token = {type => END_TAG_TOKEN, tag_name => 'p',
4543                     }->{$_->[1]}) {                        line => $token->{line}, column => $token->{column}};
4544                next B;
4545              } elsif ($_->[1] & SCOPING_EL) {
4546                !!!cp ('t354');
4547              last INSCOPE;              last INSCOPE;
4548            }            }
4549          } # INSCOPE          } # INSCOPE
4550              
4551          ## Step 1          ## 5. (b) insert
4552            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4553            !!!nack ('t359.1');
4554            !!!next-token;
4555            next B;
4556          } elsif ($token->{tag_name} eq 'dt' or
4557                   $token->{tag_name} eq 'dd') {
4558            ## NOTE: As normal, but imply </dt> or </dd> when ...
4559    
4560            my $non_optional;
4561          my $i = -1;          my $i = -1;
4562          my $node = $self->{open_elements}->[$i];  
4563          LI: {          ## 1.
4564            ## Step 2          for my $node (reverse @{$self->{open_elements}}) {
4565            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($node->[1] == DTDD_EL) {
4566              if ($i != -1) {              ## 2. (a) As if </li>
4567                !!!parse-error (type => 'end tag missing:'.              {
4568                                $self->{open_elements}->[-1]->[1]);                ## If no </li> - not applied
4569                  #
4570    
4571                  ## Otherwise
4572    
4573                  ## 1. generate implied end tags, except for </dt> or </dd>
4574                  #
4575    
4576                  ## 2. If current node != "dt"|"dd", parse error
4577                  if ($non_optional) {
4578                    !!!parse-error (type => 'not closed',
4579                                    text => $non_optional->[0]->manakai_local_name,
4580                                    token => $token);
4581                    !!!cp ('t355.1');
4582                  } else {
4583                    !!!cp ('t356.1');
4584                  }
4585    
4586                  ## 3. Pop
4587                  splice @{$self->{open_elements}}, $i;
4588              }              }
4589              splice @{$self->{open_elements}}, $i;  
4590              last LI;              last; ## 2. (b) goto 5.
4591            }            } elsif (
4592                                 ## NOTE: not "formatting" and not "phrasing"
4593            ## Step 3                     ($node->[1] & SPECIAL_EL or
4594            if (not $formatting_category->{$node->[1]} and                      $node->[1] & SCOPING_EL) and
4595                #not $phrasing_category->{$node->[1]} and                     ## NOTE: "li", "dt", and "dd" are in |SPECIAL_EL|.
4596                ($special_category->{$node->[1]} or  
4597                 $scoping_category->{$node->[1]}) and                     (not $node->[1] & ADDRESS_DIV_P_EL)
4598                $node->[1] ne 'address' and $node->[1] ne 'div') {                    ) {
4599              last LI;              ## 3.
4600                !!!cp ('t357.1');
4601                last; ## goto 5.
4602              } elsif ($node->[1] & END_TAG_OPTIONAL_EL) {
4603                !!!cp ('t358.1');
4604                #
4605              } else {
4606                !!!cp ('t359.1');
4607                $non_optional ||= $node;
4608                #
4609            }            }
4610                        ## 4.
4611            ## Step 4            ## goto 2.
4612            $i--;            $i--;
4613            $node = $self->{open_elements}->[$i];          }
4614            redo LI;  
4615          } # LI          ## 5. (a) has a |p| element in scope
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
4616          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4617            if ($_->[1] eq 'p') {            if ($_->[1] == P_EL) {
4618              !!!back-token;              !!!cp ('t353.1');
4619              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
4620              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
4621            } elsif ({                        line => $token->{line}, column => $token->{column}};
4622                      table => 1, caption => 1, td => 1, th => 1,              next B;
4623                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
4624                     }->{$_->[1]}) {              !!!cp ('t354.1');
4625              last INSCOPE;              last INSCOPE;
4626            }            }
4627          } # INSCOPE          } # INSCOPE
4628              
4629          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          ## 5. (b) insert
4630                      !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4631          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          !!!nack ('t359.2');
             
4632          !!!next-token;          !!!next-token;
4633          redo B;          next B;
4634        } elsif ({        } elsif ($token->{tag_name} eq 'plaintext') {
4635                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,          ## NOTE: As normal, but effectively ends parsing
4636                 }->{$token->{tag_name}}) {  
4637          ## has a p element in scope          ## has a p element in scope
4638          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4639            my $node = $self->{open_elements}->[$_];            if ($_->[1] == P_EL) {
4640            if ($node->[1] eq 'p') {              !!!cp ('t367');
4641              !!!back-token;              !!!back-token; # <plaintext>
4642              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
4643              redo B;                        line => $token->{line}, column => $token->{column}};
4644            } elsif ({              next B;
4645                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
4646                      button => 1, marquee => 1, object => 1, html => 1,              !!!cp ('t368');
                    }->{$node->[1]}) {  
4647              last INSCOPE;              last INSCOPE;
4648            }            }
4649          } # INSCOPE          } # INSCOPE
4650                        
4651          ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
         ## 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;  
         #}  
4652                        
4653          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4654                        
4655            !!!nack ('t368.1');
4656          !!!next-token;          !!!next-token;
4657          redo B;          next B;
4658        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
4659          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4660            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
4661            if ($node->[1] eq 'a') {            if ($node->[1] == A_EL) {
4662              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
4663                !!!parse-error (type => 'in a:a', token => $token);
4664                            
4665              !!!back-token;              !!!back-token; # <a>
4666              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
4667              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
4668                $formatting_end_tag->($token);
4669                            
4670              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
4671                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4672                    !!!cp ('t372');
4673                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
4674                  last AFE2;                  last AFE2;
4675                }                }
4676              } # AFE2              } # AFE2
4677              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
4678                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4679                    !!!cp ('t373');
4680                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
4681                  last OE;                  last OE;
4682                }                }
4683              } # OE              } # OE
4684              last AFE;              last AFE;
4685            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
4686                !!!cp ('t374');
4687              last AFE;              last AFE;
4688            }            }
4689          } # AFE          } # AFE
4690                        
4691          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4692    
4693          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4694          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4695    
4696            !!!nack ('t374.1');
4697          !!!next-token;          !!!next-token;
4698          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;  
4699        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
4700          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4701    
4702          ## has a |nobr| element in scope          ## has a |nobr| element in scope
4703          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4704            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4705            if ($node->[1] eq 'nobr') {            if ($node->[1] == NOBR_EL) {
4706              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
4707              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
4708              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
4709              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
4710            } elsif ({                        line => $token->{line}, column => $token->{column}};
4711                      table => 1, caption => 1, td => 1, th => 1,              next B;
4712                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
4713                     }->{$node->[1]}) {              !!!cp ('t377');
4714              last INSCOPE;              last INSCOPE;
4715            }            }
4716          } # INSCOPE          } # INSCOPE
4717                    
4718          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4719          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4720                    
4721            !!!nack ('t377.1');
4722          !!!next-token;          !!!next-token;
4723          redo B;          next B;
4724        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
4725          ## has a button element in scope          ## has a button element in scope
4726          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4727            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4728            if ($node->[1] eq 'button') {            if ($node->[1] == BUTTON_EL) {
4729              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
4730              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
4731              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
4732              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
4733            } elsif ({                        line => $token->{line}, column => $token->{column}};
4734                      table => 1, caption => 1, td => 1, th => 1,              next B;
4735                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
4736                     }->{$node->[1]}) {              !!!cp ('t379');
4737              last INSCOPE;              last INSCOPE;
4738            }            }
4739          } # INSCOPE          } # INSCOPE
4740                        
4741          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4742                        
4743          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4744          push @$active_formatting_elements, ['#marker', ''];  
4745            ## TODO: associate with $self->{form_element} if defined
4746    
         !!!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});  
4747          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4748            
4749          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
4750          !!!next-token;          !!!next-token;
4751          redo B;          next B;
4752        } elsif ({        } elsif ({
4753                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
4754                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
4755                  image => 1,                  noembed => 1,
4756                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
4757                    noscript => 0, ## TODO: 1 if scripting is enabled
4758                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
4759          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
4760            !!!parse-error (type => 'image');            !!!cp ('t381');
4761            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
4762            } else {
4763              !!!cp ('t399');
4764          }          }
4765            ## NOTE: There is an "as if in body" code clone.
4766          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
4767          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
4768        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
4769          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
4770                    
4771          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4772              !!!cp ('t389');
4773            ## Ignore the token            ## Ignore the token
4774              !!!nack ('t389'); ## NOTE: Not acknowledged.
4775            !!!next-token;            !!!next-token;
4776            redo B;            next B;
4777          } else {          } else {
4778              !!!ack ('t391.1');
4779    
4780            my $at = $token->{attributes};            my $at = $token->{attributes};
4781            my $form_attrs;            my $form_attrs;
4782            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 4911  sub _tree_construction_main ($) { Line 4786  sub _tree_construction_main ($) {
4786            delete $at->{prompt};            delete $at->{prompt};
4787            my @tokens = (            my @tokens = (
4788                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
4789                           attributes => $form_attrs},                           attributes => $form_attrs,
4790                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
4791                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
4792                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
4793                            {type => START_TAG_TOKEN, tag_name => 'p',
4794                             line => $token->{line}, column => $token->{column}},
4795                            {type => START_TAG_TOKEN, tag_name => 'label',
4796                             line => $token->{line}, column => $token->{column}},
4797                         );                         );
4798            if ($prompt_attr) {            if ($prompt_attr) {
4799              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
4800                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
4801                               #line => $token->{line}, column => $token->{column},
4802                              };
4803            } else {            } else {
4804                !!!cp ('t391');
4805              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
4806                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
4807                               #line => $token->{line}, column => $token->{column},
4808                              }; # SHOULD
4809              ## TODO: make this configurable              ## TODO: make this configurable
4810            }            }
4811            push @tokens,            push @tokens,
4812                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
4813                             line => $token->{line}, column => $token->{column}},
4814                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4815                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
4816                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
4817                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
4818                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
4819            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
4820                             line => $token->{line}, column => $token->{column}},
4821                            {type => END_TAG_TOKEN, tag_name => 'form',
4822                             line => $token->{line}, column => $token->{column}};
4823            !!!back-token (@tokens);            !!!back-token (@tokens);
4824            redo B;            !!!next-token;
4825              next B;
4826          }          }
4827        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
4828          my $tag_name = $token->{tag_name};          ## Step 1
4829          my $el;          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
4830                    
4831            ## Step 2
4832          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
4833    
4834            ## Step 3
4835            $self->{ignore_newline} = 1;
4836    
4837            ## Step 4
4838            ## ISSUE: This step is wrong. (r2302 enbugged)
4839    
4840            ## Step 5
4841          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
4842          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
4843            
4844          $insert->($el);          ## Step 6-7
4845                    $self->{insertion_mode} |= IN_CDATA_RCDATA_IM;
4846          my $text = '';  
4847            !!!nack ('t392.1');
4848          !!!next-token;          !!!next-token;
4849          if ($token->{type} == CHARACTER_TOKEN) {          next B;
4850            $token->{data} =~ s/^\x0A//;        } elsif ($token->{tag_name} eq 'optgroup' or
4851            unless (length $token->{data}) {                 $token->{tag_name} eq 'option') {
4852              !!!next-token;          ## has an |option| element in scope
4853            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4854              my $node = $self->{open_elements}->[$_];
4855              if ($node->[1] == OPTION_EL) {
4856                !!!cp ('t397.1');
4857                ## NOTE: As if </option>
4858                !!!back-token; # <option> or <optgroup>
4859                $token = {type => END_TAG_TOKEN, tag_name => 'option',
4860                          line => $token->{line}, column => $token->{column}};
4861                next B;
4862              } elsif ($node->[1] & SCOPING_EL) {
4863                !!!cp ('t397.2');
4864                last INSCOPE;
4865            }            }
4866          }          } # INSCOPE
4867          while ($token->{type} == CHARACTER_TOKEN) {  
4868            $text .= $token->{data};          $reconstruct_active_formatting_elements->($insert_to_current);
4869            !!!next-token;  
4870          }          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4871          if (length $text) {  
4872            $el->manakai_append_text ($text);          !!!nack ('t397.3');
         }  
           
         $self->{content_model} = PCDATA_CONTENT_MODEL;  
           
         if ($token->{type} == END_TAG_TOKEN and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
         }  
4873          !!!next-token;          !!!next-token;
4874          redo B;          redo B;
4875        } elsif ({        } elsif ($token->{tag_name} eq 'rt' or
4876                  iframe => 1,                 $token->{tag_name} eq 'rp') {
4877                  noembed => 1,          ## has a |ruby| element in scope
4878                  noframes => 1,          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4879                  noscript => 0, ## TODO: 1 if scripting is enabled            my $node = $self->{open_elements}->[$_];
4880                 }->{$token->{tag_name}}) {            if ($node->[1] == RUBY_EL) {
4881          ## NOTE: There is an "as if in body" code clone.              !!!cp ('t398.1');
4882          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);              ## generate implied end tags
4883                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4884                  !!!cp ('t398.2');
4885                  pop @{$self->{open_elements}};
4886                }
4887                unless ($self->{open_elements}->[-1]->[1] == RUBY_EL) {
4888                  !!!cp ('t398.3');
4889                  !!!parse-error (type => 'not closed',
4890                                  text => $self->{open_elements}->[-1]->[0]
4891                                      ->manakai_local_name,
4892                                  token => $token);
4893                  pop @{$self->{open_elements}}
4894                      while not $self->{open_elements}->[-1]->[1] == RUBY_EL;
4895                }
4896                last INSCOPE;
4897              } elsif ($node->[1] & SCOPING_EL) {
4898                !!!cp ('t398.4');
4899                last INSCOPE;
4900              }
4901            } # INSCOPE
4902    
4903            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4904    
4905            !!!nack ('t398.5');
4906            !!!next-token;
4907          redo B;          redo B;
4908        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'math' or
4909                   $token->{tag_name} eq 'svg') {
4910          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4911    
4912            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
4913    
4914            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
4915    
4916            ## "adjust foreign attributes" - done in insert-element-f
4917                    
4918          !!!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);
4919                    
4920          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
4921              pop @{$self->{open_elements}};
4922              !!!ack ('t398.6');
4923            } else {
4924              !!!cp ('t398.7');
4925              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
4926              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
4927              ## mode, "in body" (not "in foreign content") secondary insertion
4928              ## mode, maybe.
4929            }
4930    
4931          !!!next-token;          !!!next-token;
4932          redo B;          next B;
4933        } elsif ({        } elsif ({
4934                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
4935                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1,
4936                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
4937                  thead => 1, tr => 1,                  thead => 1, tr => 1,
4938                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
4939          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
4940            !!!parse-error (type => 'in body',
4941                            text => $token->{tag_name}, token => $token);
4942          ## Ignore the token          ## Ignore the token
4943            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
4944            !!!next-token;
4945            next B;
4946          } elsif ($token->{tag_name} eq 'param' or
4947                   $token->{tag_name} eq 'source') {
4948            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4949            pop @{$self->{open_elements}};
4950    
4951            !!!ack ('t398.5');
4952          !!!next-token;          !!!next-token;
4953          redo B;          redo B;
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
4954        } else {        } else {
4955            if ($token->{tag_name} eq 'image') {
4956              !!!cp ('t384');
4957              !!!parse-error (type => 'image', token => $token);
4958              $token->{tag_name} = 'img';
4959            } else {
4960              !!!cp ('t385');
4961            }
4962    
4963            ## NOTE: There is an "as if <br>" code clone.
4964          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4965                    
4966          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4967    
4968            if ({
4969                 applet => 1, marquee => 1, object => 1,
4970                }->{$token->{tag_name}}) {
4971              !!!cp ('t380');
4972              push @$active_formatting_elements, ['#marker', ''];
4973              !!!nack ('t380.1');
4974            } elsif ({
4975                      b => 1, big => 1, em => 1, font => 1, i => 1,
4976                      s => 1, small => 1, strike => 1,
4977                      strong => 1, tt => 1, u => 1,
4978                     }->{$token->{tag_name}}) {
4979              !!!cp ('t375');
4980              push @$active_formatting_elements, $self->{open_elements}->[-1];
4981              !!!nack ('t375.1');
4982            } elsif ($token->{tag_name} eq 'input') {
4983              !!!cp ('t388');
4984              ## TODO: associate with $self->{form_element} if defined
4985              pop @{$self->{open_elements}};
4986              !!!ack ('t388.2');
4987            } elsif ({
4988                      area => 1, basefont => 1, bgsound => 1, br => 1,
4989                      embed => 1, img => 1, spacer => 1, wbr => 1,
4990                     }->{$token->{tag_name}}) {
4991              !!!cp ('t388.1');
4992              pop @{$self->{open_elements}};
4993              !!!ack ('t388.3');
4994            } elsif ($token->{tag_name} eq 'select') {
4995              ## TODO: associate with $self->{form_element} if defined
4996            
4997              if ($self->{insertion_mode} & TABLE_IMS or
4998                  $self->{insertion_mode} & BODY_TABLE_IMS or
4999                  ($self->{insertion_mode} & IM_MASK) == IN_COLUMN_GROUP_IM) {
5000                !!!cp ('t400.1');
5001                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
5002              } else {
5003                !!!cp ('t400.2');
5004                $self->{insertion_mode} = IN_SELECT_IM;
5005              }
5006              !!!nack ('t400.3');
5007            } else {
5008              !!!nack ('t402');
5009            }
5010                    
5011          !!!next-token;          !!!next-token;
5012          redo B;          next B;
5013        }        }
5014      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
5015        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
5016          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
5017              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
5018            for (@{$self->{open_elements}}) {          INSCOPE: {
5019              unless ({            for (reverse @{$self->{open_elements}}) {
5020                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] == BODY_EL) {
5021                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
5022                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
5023                      }->{$_->[1]}) {                last INSCOPE;
5024                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
5025                  !!!cp ('t405.1');
5026                  last;
5027              }              }
5028            }            }
5029    
5030            $self->{insertion_mode} = AFTER_BODY_IM;            ## NOTE: |<marquee></body>|, |<svg><foreignobject></body>|
5031            !!!next-token;  
5032            redo B;            !!!parse-error (type => 'unmatched end tag',
5033          } else {                            text => $token->{tag_name}, token => $token);
5034            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            ## NOTE: Ignore the token.
           ## Ignore the token  
5035            !!!next-token;            !!!next-token;
5036            redo B;            next B;
5037            } # INSCOPE
5038    
5039            for (@{$self->{open_elements}}) {
5040              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
5041                !!!cp ('t403');
5042                !!!parse-error (type => 'not closed',
5043                                text => $_->[0]->manakai_local_name,
5044                                token => $token);
5045                last;
5046              } else {
5047                !!!cp ('t404');
5048              }
5049          }          }
5050    
5051            $self->{insertion_mode} = AFTER_BODY_IM;
5052            !!!next-token;
5053            next B;
5054        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
5055          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
5056            ## ISSUE: There is an issue in the spec.          ## up-to-date, though it has same effect as speced.
5057            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if (@{$self->{open_elements}} > 1 and
5058              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              $self->{open_elements}->[1]->[1] == BODY_EL) {
5059              unless ($self->{open_elements}->[-1]->[1] == BODY_EL) {
5060                !!!cp ('t406');
5061                !!!parse-error (type => 'not closed',
5062                                text => $self->{open_elements}->[1]->[0]
5063                                    ->manakai_local_name,
5064                                token => $token);
5065              } else {
5066                !!!cp ('t407');
5067            }            }
5068            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5069            ## reprocess            ## reprocess
5070            redo B;            next B;
5071          } else {          } else {
5072            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
5073              !!!parse-error (type => 'unmatched end tag',
5074                              text => $token->{tag_name}, token => $token);
5075            ## Ignore the token            ## Ignore the token
5076            !!!next-token;            !!!next-token;
5077            redo B;            next B;
5078          }          }
5079        } elsif ({        } elsif ({
5080                  address => 1, blockquote => 1, center => 1, dir => 1,                  ## NOTE: End tags for non-phrasing flow content elements
5081                  div => 1, dl => 1, fieldset => 1, listing => 1,  
5082                  menu => 1, ol => 1, pre => 1, ul => 1,                  ## NOTE: The normal ones
5083                  p => 1,                  address => 1, article => 1, aside => 1, blockquote => 1,
5084                    center => 1, datagrid => 1, details => 1, dialog => 1,
5085                    dir => 1, div => 1, dl => 1, fieldset => 1, figure => 1,
5086                    footer => 1, header => 1, listing => 1, menu => 1, nav => 1,
5087                    ol => 1, pre => 1, section => 1, ul => 1,
5088    
5089                    ## NOTE: As normal, but ... optional tags
5090                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
5091                  button => 1, marquee => 1, object => 1,  
5092                    applet => 1, button => 1, marquee => 1, object => 1,
5093                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5094            ## NOTE: Code for <li> start tags includes "as if </li>" code.
5095            ## Code for <dt> or <dd> start tags includes "as if </dt> or
5096            ## </dd>" code.
5097    
5098          ## has an element in scope          ## has an element in scope
5099          my $i;          my $i;
5100          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5101            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5102            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5103              ## generate implied end tags              !!!cp ('t410');
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
5104              $i = $_;              $i = $_;
5105              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
5106            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
5107                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
5108              last INSCOPE;              last INSCOPE;
5109            }            }
5110          } # INSCOPE          } # INSCOPE
5111            
5112          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
5113            if (defined $i) {            !!!cp ('t413');
5114              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag',
5115                              text => $token->{tag_name}, token => $token);
5116              ## NOTE: Ignore the token.
5117            } else {
5118              ## Step 1. generate implied end tags
5119              while ({
5120                      ## END_TAG_OPTIONAL_EL
5121                      dd => ($token->{tag_name} ne 'dd'),
5122                      dt => ($token->{tag_name} ne 'dt'),
5123                      li => ($token->{tag_name} ne 'li'),
5124                      option => 1,
5125                      optgroup => 1,
5126                      p => 1,
5127                      rt => 1,
5128                      rp => 1,
5129                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
5130                !!!cp ('t409');
5131                pop @{$self->{open_elements}};
5132              }
5133    
5134              ## Step 2.
5135              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5136                      ne $token->{tag_name}) {
5137                !!!cp ('t412');
5138                !!!parse-error (type => 'not closed',
5139                                text => $self->{open_elements}->[-1]->[0]
5140                                    ->manakai_local_name,
5141                                token => $token);
5142            } else {            } else {
5143              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
5144            }            }
5145          }  
5146                      ## Step 3.
         if (defined $i) {  
5147            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5148          } elsif ($token->{tag_name} eq 'p') {  
5149            ## As if <p>, then reprocess the current token            ## Step 4.
5150            my $el;            $clear_up_to_marker->()
5151            !!!create-element ($el, 'p');                if {
5152            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
5153                  }->{$token->{tag_name}};
5154          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
5155          !!!next-token;          !!!next-token;
5156          redo B;          next B;
5157        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
5158            ## NOTE: As normal, but interacts with the form element pointer
5159    
5160            undef $self->{form_element};
5161    
5162          ## has an element in scope          ## has an element in scope
5163            my $i;
5164          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5165            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5166            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] == FORM_EL) {
5167              ## generate implied end tags              !!!cp ('t418');
5168              if ({              $i = $_;
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
5169              last INSCOPE;              last INSCOPE;
5170            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
5171                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
5172              last INSCOPE;              last INSCOPE;
5173            }            }
5174          } # INSCOPE          } # INSCOPE
5175            
5176          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
5177            pop @{$self->{open_elements}};            !!!cp ('t421');
5178          } else {            !!!parse-error (type => 'unmatched end tag',
5179            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                            text => $token->{tag_name}, token => $token);
5180              ## NOTE: Ignore the token.
5181            } else {
5182              ## Step 1. generate implied end tags
5183              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5184                !!!cp ('t417');
5185                pop @{$self->{open_elements}};
5186              }
5187              
5188              ## Step 2.
5189              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5190                      ne $token->{tag_name}) {
5191                !!!cp ('t417.1');
5192                !!!parse-error (type => 'not closed',
5193                                text => $self->{open_elements}->[-1]->[0]
5194                                    ->manakai_local_name,
5195                                token => $token);
5196              } else {
5197                !!!cp ('t420');
5198              }  
5199              
5200              ## Step 3.
5201              splice @{$self->{open_elements}}, $i;
5202          }          }
5203    
         undef $self->{form_element};  
5204          !!!next-token;          !!!next-token;
5205          redo B;          next B;
5206        } elsif ({        } elsif ({
5207                    ## NOTE: As normal, except acts as a closer for any ...
5208                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5209                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5210          ## has an element in scope          ## has an element in scope
5211          my $i;          my $i;
5212          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5213            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5214            if ({            if ($node->[1] == HEADING_EL) {
5215                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,              !!!cp ('t423');
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
5216              $i = $_;              $i = $_;
5217              last INSCOPE;              last INSCOPE;
5218            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
5219                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
5220              last INSCOPE;              last INSCOPE;
5221            }            }
5222          } # INSCOPE          } # INSCOPE
5223            
5224          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
5225            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
5226              !!!parse-error (type => 'unmatched end tag',
5227                              text => $token->{tag_name}, token => $token);
5228              ## NOTE: Ignore the token.
5229            } else {
5230              ## Step 1. generate implied end tags
5231              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5232                !!!cp ('t422');
5233                pop @{$self->{open_elements}};
5234              }
5235              
5236              ## Step 2.
5237              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5238                      ne $token->{tag_name}) {
5239                !!!cp ('t425');
5240                !!!parse-error (type => 'unmatched end tag',
5241                                text => $token->{tag_name}, token => $token);
5242              } else {
5243                !!!cp ('t426');
5244              }
5245    
5246              ## Step 3.
5247              splice @{$self->{open_elements}}, $i;
5248          }          }
5249                    
         splice @{$self->{open_elements}}, $i if defined $i;  
5250          !!!next-token;          !!!next-token;
5251          redo B;          next B;
5252          } elsif ($token->{tag_name} eq 'p') {
5253            ## NOTE: As normal, except </p> implies <p> and ...
5254    
5255            ## has an element in scope
5256            my $non_optional;
5257            my $i;
5258            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5259              my $node = $self->{open_elements}->[$_];
5260              if ($node->[1] == P_EL) {
5261                !!!cp ('t410.1');
5262                $i = $_;
5263                last INSCOPE;
5264              } elsif ($node->[1] & SCOPING_EL) {
5265                !!!cp ('t411.1');
5266                last INSCOPE;
5267              } elsif ($node->[1] & END_TAG_OPTIONAL_EL) {
5268                ## NOTE: |END_TAG_OPTIONAL_EL| includes "p"
5269                !!!cp ('t411.2');
5270                #
5271              } else {
5272                !!!cp ('t411.3');
5273                $non_optional ||= $node;
5274                #
5275              }
5276            } # INSCOPE
5277    
5278            if (defined $i) {
5279              ## 1. Generate implied end tags
5280              #
5281    
5282              ## 2. If current node != "p", parse error
5283              if ($non_optional) {
5284                !!!cp ('t412.1');
5285                !!!parse-error (type => 'not closed',
5286                                text => $non_optional->[0]->manakai_local_name,
5287                                token => $token);
5288              } else {
5289                !!!cp ('t414.1');
5290              }
5291    
5292              ## 3. Pop
5293              splice @{$self->{open_elements}}, $i;
5294            } else {
5295              !!!cp ('t413.1');
5296              !!!parse-error (type => 'unmatched end tag',
5297                              text => $token->{tag_name}, token => $token);
5298    
5299              !!!cp ('t415.1');
5300              ## As if <p>, then reprocess the current token
5301              my $el;
5302              !!!create-element ($el, $HTML_NS, 'p',, $token);
5303              $insert->($el);
5304              ## NOTE: Not inserted into |$self->{open_elements}|.
5305            }
5306    
5307            !!!next-token;
5308            next B;
5309        } elsif ({        } elsif ({
5310                  a => 1,                  a => 1,
5311                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
5312                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strike => 1,
5313                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5314                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5315          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
5316          redo B;          $formatting_end_tag->($token);
5317            next B;
5318        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
5319          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
5320            !!!parse-error (type => 'unmatched end tag',
5321                            text => 'br', token => $token);
5322    
5323          ## As if <br>          ## As if <br>
5324          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5325                    
5326          my $el;          my $el;
5327          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
5328          $insert->($el);          $insert->($el);
5329                    
5330          ## Ignore the token.          ## Ignore the token.
5331          !!!next-token;          !!!next-token;
5332          redo B;          next B;
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
5333        } else {        } else {
5334            if ($token->{tag_name} eq 'sarcasm') {
5335              sleep 0.001; # take a deep breath
5336            }
5337    
5338          ## Step 1          ## Step 1
5339          my $node_i = -1;          my $node_i = -1;
5340          my $node = $self->{open_elements}->[$node_i];          my $node = $self->{open_elements}->[$node_i];
5341    
5342          ## Step 2          ## Step 2
5343          S2: {          S2: {
5344            if ($node->[1] eq $token->{tag_name}) {            my $node_tag_name = $node->[0]->manakai_local_name;
5345              $node_tag_name =~ tr/A-Z/a-z/; # for SVG camelCase tag names
5346              if ($node_tag_name eq $token->{tag_name}) {
5347              ## Step 1              ## Step 1
5348              ## generate implied end tags              ## generate implied end tags
5349              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5350                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
5351                   td => 1, th => 1, tr => 1,                ## NOTE: |<ruby><rt></ruby>|.
5352                   tbody => 1, tfoot => 1, thead => 1,                ## ISSUE: <ruby><rt></rt> will also take this code path,
5353                  }->{$self->{open_elements}->[-1]->[1]}) {                ## which seems wrong.
5354                !!!back-token;                pop @{$self->{open_elements}};
5355                $token = {type => END_TAG_TOKEN,                $node_i++;
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
5356              }              }
5357                    
5358              ## Step 2              ## Step 2
5359              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              my $current_tag_name
5360                    = $self->{open_elements}->[-1]->[0]->manakai_local_name;
5361                $current_tag_name =~ tr/A-Z/a-z/;
5362                if ($current_tag_name ne $token->{tag_name}) {
5363                  !!!cp ('t431');
5364                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
5365                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5366                                  text => $self->{open_elements}->[-1]->[0]
5367                                      ->manakai_local_name,
5368                                  token => $token);
5369                } else {
5370                  !!!cp ('t432');
5371              }              }
5372                            
5373              ## Step 3              ## Step 3
5374              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
5375    
5376              !!!next-token;              !!!next-token;
5377              last S2;              last S2;
5378            } else {            } else {
5379              ## Step 3              ## Step 3
5380              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
5381                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
5382                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
5383                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
5384                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
5385                  !!!parse-error (type => 'unmatched end tag',
5386                                  text => $token->{tag_name}, token => $token);
5387                ## Ignore the token                ## Ignore the token
5388                !!!next-token;                !!!next-token;
5389                last S2;                last S2;
5390    
5391                  ## NOTE: |<span><dd></span>a|: In Safari 3.1.2 and Opera
5392                  ## 9.27, "a" is a child of <dd> (conforming).  In
5393                  ## Firefox 3.0.2, "a" is a child of <body>.  In WinIE 7,
5394                  ## "a" is a child of both <body> and <dd>.
5395              }              }
5396                
5397                !!!cp ('t434');
5398            }            }
5399                        
5400            ## Step 4            ## Step 4
# Line 5269  sub _tree_construction_main ($) { Line 5404  sub _tree_construction_main ($) {
5404            ## Step 5;            ## Step 5;
5405            redo S2;            redo S2;
5406          } # S2          } # S2
5407          redo B;          next B;
5408        }        }
5409      }      }
5410      redo B;      next B;
5411      } continue { # B
5412        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
5413          ## NOTE: The code below is executed in cases where it does not have
5414          ## to be, but it it is harmless even in those cases.
5415          ## has an element in scope
5416          INSCOPE: {
5417            for (reverse 0..$#{$self->{open_elements}}) {
5418              my $node = $self->{open_elements}->[$_];
5419              if ($node->[1] & FOREIGN_EL) {
5420                last INSCOPE;
5421              } elsif ($node->[1] & SCOPING_EL) {
5422                last;
5423              }
5424            }
5425            
5426            ## NOTE: No foreign element in scope.
5427            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
5428          } # INSCOPE
5429        }
5430    } # B    } # B
5431    
   ## 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  
   
5432    ## Stop parsing # MUST    ## Stop parsing # MUST
5433        
5434    ## TODO: script stuffs    ## TODO: script stuffs
5435  } # _tree_construct_main  } # _tree_construct_main
5436    
5437  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
5438    my $class = shift;    my $class = shift;
5439    my $node = shift;    my $node = shift;
5440    my $s = \$_[0];    #my $s = \$_[0];
5441    my $onerror = $_[1];    my $onerror = $_[1];
5442      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
5443    
5444    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
5445    
# Line 5308  sub set_inner_html ($$$) { Line 5458  sub set_inner_html ($$$) {
5458      }      }
5459    
5460      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
5461      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
5462    } elsif ($nt == 1) {    } elsif ($nt == 1) {
5463      ## TODO: If non-html element      ## TODO: If non-html element
5464    
5465      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
5466    
5467    ## TODO: Support for $get_wrapper
5468    
5469      ## Step 1 # MUST      ## Step 1 # MUST
5470      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
5471      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 5321  sub set_inner_html ($$$) { Line 5473  sub set_inner_html ($$$) {
5473      my $p = $class->new;      my $p = $class->new;
5474      $p->{document} = $doc;      $p->{document} = $doc;
5475    
5476      ## Step 9 # MUST      ## Step 8 # MUST
5477      my $i = 0;      my $i = 0;
5478      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
5479      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
5480      $p->{set_next_input_character} = sub {      require Whatpm::Charset::DecodeHandle;
5481        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
5482        $input = $get_wrapper->($input);
5483        $p->{set_nc} = sub {
5484        my $self = shift;        my $self = shift;
5485    
5486        pop @{$self->{prev_input_character}};        my $char = '';
5487        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        if (defined $self->{next_nc}) {
5488            $char = $self->{next_nc};
5489            delete $self->{next_nc};
5490            $self->{nc} = ord $char;
5491          } else {
5492            $self->{char_buffer} = '';
5493            $self->{char_buffer_pos} = 0;
5494            
5495            my $count = $input->manakai_read_until
5496                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
5497                 $self->{char_buffer_pos});
5498            if ($count) {
5499              $self->{line_prev} = $self->{line};
5500              $self->{column_prev} = $self->{column};
5501              $self->{column}++;
5502              $self->{nc}
5503                  = ord substr ($self->{char_buffer},
5504                                $self->{char_buffer_pos}++, 1);
5505              return;
5506            }
5507            
5508            if ($input->read ($char, 1)) {
5509              $self->{nc} = ord $char;
5510            } else {
5511              $self->{nc} = -1;
5512              return;
5513            }
5514          }
5515    
5516        $self->{next_input_character} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
5517        $self->{next_input_character} = ord substr $$s, $i++, 1;        $p->{column}++;
5518        $column++;  
5519          if ($self->{nc} == 0x000A) { # LF
5520        if ($self->{next_input_character} == 0x000A) { # LF          $p->{line}++;
5521          $line++;          $p->{column} = 0;
5522          $column = 0;          !!!cp ('i1');
5523        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{nc} == 0x000D) { # CR
5524          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
5525          $self->{next_input_character} = 0x000A; # LF # MUST          my $next = '';
5526          $line++;          if ($input->read ($next, 1) and $next ne "\x0A") {
5527          $column = 0;            $self->{next_nc} = $next;
5528        } elsif ($self->{next_input_character} > 0x10FFFF) {          }
5529          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{nc} = 0x000A; # LF # MUST
5530        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $p->{line}++;
5531            $p->{column} = 0;
5532            !!!cp ('i2');
5533          } elsif ($self->{nc} == 0x0000) { # NULL
5534            !!!cp ('i4');
5535          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
5536          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5537        }        }
5538      };      };
5539      $p->{prev_input_character} = [-1, -1, -1];  
5540      $p->{next_input_character} = -1;      $p->{read_until} = sub {
5541              #my ($scalar, $specials_range, $offset) = @_;
5542          return 0 if defined $p->{next_nc};
5543    
5544          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
5545          my $offset = $_[2] || 0;
5546          
5547          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
5548            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
5549            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
5550              substr ($_[0], $offset)
5551                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
5552              my $count = $+[0] - $-[0];
5553              if ($count) {
5554                $p->{column} += $count;
5555                $p->{char_buffer_pos} += $count;
5556                $p->{line_prev} = $p->{line};
5557                $p->{column_prev} = $p->{column} - 1;
5558                $p->{nc} = -1;
5559              }
5560              return $count;
5561            } else {
5562              return 0;
5563            }
5564          } else {
5565            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
5566            if ($count) {
5567              $p->{column} += $count;
5568              $p->{column_prev} += $count;
5569              $p->{nc} = -1;
5570            }
5571            return $count;
5572          }
5573        }; # $p->{read_until}
5574    
5575      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
5576        my (%opt) = @_;        my (%opt) = @_;
5577        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
5578          my $column = $opt{column};
5579          if (defined $opt{token} and defined $opt{token}->{line}) {
5580            $line = $opt{token}->{line};
5581            $column = $opt{token}->{column};
5582          }
5583          warn "Parse error ($opt{type}) at line $line column $column\n";
5584      };      };
5585      $p->{parse_error} = sub {      $p->{parse_error} = sub {
5586        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
5587      };      };
5588            
5589        my $char_onerror = sub {
5590          my (undef, $type, %opt) = @_;
5591          $ponerror->(layer => 'encode',
5592                      line => $p->{line}, column => $p->{column} + 1,
5593                      %opt, type => $type);
5594        }; # $char_onerror
5595        $input->onerror ($char_onerror);
5596    
5597      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
5598      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
5599    
5600      ## Step 2      ## Step 2
5601      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
5602      $p->{content_model} = {      $p->{content_model} = {
5603        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
5604        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5382  sub set_inner_html ($$$) { Line 5615  sub set_inner_html ($$$) {
5615          unless defined $p->{content_model};          unless defined $p->{content_model};
5616          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
5617    
5618      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
5619          ## TODO: Foreign element OK?
5620    
5621      ## Step 4      ## Step 3
5622      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
5623        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
5624    
5625      ## Step 5 # MUST      ## Step 4 # MUST
5626      $doc->append_child ($root);      $doc->append_child ($root);
5627    
5628      ## Step 6 # MUST      ## Step 5 # MUST
5629      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
5630    
5631      undef $p->{head_element};      undef $p->{head_element};
5632        undef $p->{head_element_inserted};
5633    
5634      ## Step 7 # MUST      ## Step 6 # MUST
5635      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
5636    
5637      ## Step 8 # MUST      ## Step 7 # MUST
5638      my $anode = $node;      my $anode = $node;
5639      AN: while (defined $anode) {      AN: while (defined $anode) {
5640        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
5641          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
5642          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5643            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
5644                !!!cp ('i5');
5645              $p->{form_element} = $anode;              $p->{form_element} = $anode;
5646              last AN;              last AN;
5647            }            }
# Line 5414  sub set_inner_html ($$$) { Line 5650  sub set_inner_html ($$$) {
5650        $anode = $anode->parent_node;        $anode = $anode->parent_node;
5651      } # AN      } # AN
5652            
5653      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
5654      {      {
5655        my $self = $p;        my $self = $p;
5656        !!!next-token;        !!!next-token;
5657      }      }
5658      $p->_tree_construction_main;      $p->_tree_construction_main;
5659    
5660      ## Step 11 # MUST      ## Step 10 # MUST
5661      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
5662      for (@cn) {      for (@cn) {
5663        $node->remove_child ($_);        $node->remove_child ($_);
5664      }      }
5665      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
5666    
5667      ## Step 12 # MUST      ## Step 11 # MUST
5668      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
5669      for (@cn) {      for (@cn) {
5670        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5438  sub set_inner_html ($$$) { Line 5673  sub set_inner_html ($$$) {
5673      ## ISSUE: mutation events?      ## ISSUE: mutation events?
5674    
5675      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
5676    
5677        delete $p->{parse_error}; # delete loop
5678    } else {    } else {
5679      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";
5680    }    }

Legend:
Removed from v.1.64  
changed lines
  Added in v.1.210

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24