/[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.35 by wakaba, Mon Jul 16 03:21:04 2007 UTC revision 1.210 by wakaba, Tue Oct 14 13:24:52 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6    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;
 ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  
 ## is not yet clear.  
 ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  
 ## "{U+FEFF}..." in GB18030?  
   
 my $permitted_slash_tag_name = {  
   base => 1,  
   link => 1,  
   meta => 1,  
   hr => 1,  
   br => 1,  
   img=> 1,  
   embed => 1,  
   param => 1,  
   area => 1,  
   col => 1,  
   input => 1,  
 };  
25    
26  my $c1_entity_char = {  ## Namespace URLs
27    0x80 => 0x20AC,  
28    0x81 => 0xFFFD,  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
29    0x82 => 0x201A,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
30    0x83 => 0x0192,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
31    0x84 => 0x201E,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
32    0x85 => 0x2026,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
33    0x86 => 0x2020,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
34    0x87 => 0x2021,  
35    0x88 => 0x02C6,  ## Element categories
36    0x89 => 0x2030,  
37    0x8A => 0x0160,  ## Bits 12-15
38    0x8B => 0x2039,  sub SPECIAL_EL () { 0b1_000000000000000 }
39    0x8C => 0x0152,  sub SCOPING_EL () { 0b1_00000000000000 }
40    0x8D => 0xFFFD,  sub FORMATTING_EL () { 0b1_0000000000000 }
41    0x8E => 0x017D,  sub PHRASING_EL () { 0b1_000000000000 }
42    0x8F => 0xFFFD,  
43    0x90 => 0xFFFD,  ## Bits 10-11
44    0x91 => 0x2018,  #sub FOREIGN_EL () { 0b1_00000000000 } # see Whatpm::HTML::Tokenizer
45    0x92 => 0x2019,  sub FOREIGN_FLOW_CONTENT_EL () { 0b1_0000000000 }
46    0x93 => 0x201C,  
47    0x94 => 0x201D,  ## Bits 6-9
48    0x95 => 0x2022,  sub TABLE_SCOPING_EL () { 0b1_000000000 }
49    0x96 => 0x2013,  sub TABLE_ROWS_SCOPING_EL () { 0b1_00000000 }
50    0x97 => 0x2014,  sub TABLE_ROW_SCOPING_EL () { 0b1_0000000 }
51    0x98 => 0x02DC,  sub TABLE_ROWS_EL () { 0b1_000000 }
52    0x99 => 0x2122,  
53    0x9A => 0x0161,  ## Bit 5
54    0x9B => 0x203A,  sub ADDRESS_DIV_P_EL () { 0b1_00000 }
55    0x9C => 0x0153,  
56    0x9D => 0xFFFD,  ## NOTE: Used in </body> and EOF algorithms.
57    0x9E => 0x017E,  ## Bit 4
58    0x9F => 0x0178,  sub ALL_END_TAG_OPTIONAL_EL () { 0b1_0000 }
59  }; # $c1_entity_char  
60    ## NOTE: Used in "generate implied end tags" algorithm.
61  my $special_category = {  ## NOTE: There is a code where a modified version of
62    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  ## END_TAG_OPTIONAL_EL is used in "generate implied end tags"
63    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  ## implementation (search for the algorithm name).
64    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  ## Bit 3
65    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  sub END_TAG_OPTIONAL_EL () { 0b1_000 }
66    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
67    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  ## Bits 0-2
68    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
69    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  sub MISC_SPECIAL_EL () { SPECIAL_EL | 0b000 }
70    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  sub FORM_EL () { SPECIAL_EL | 0b001 }
71    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  sub FRAMESET_EL () { SPECIAL_EL | 0b010 }
72  };  sub HEADING_EL () { SPECIAL_EL | 0b011 }
73  my $scoping_category = {  sub SELECT_EL () { SPECIAL_EL | 0b100 }
74    button => 1, caption => 1, html => 1, marquee => 1, object => 1,  sub SCRIPT_EL () { SPECIAL_EL | 0b101 }
75    table => 1, td => 1, th => 1,  
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  my $formatting_category = {  
252    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  my $el_category_f = {
253    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,    $MML_NS => {
254        'annotation-xml' => MML_AXML_EL,
255        mi => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
256        mo => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
257        mn => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
258        ms => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
259        mtext => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
260      },
261      $SVG_NS => {
262        foreignObject => SCOPING_EL | FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
263        desc => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
264        title => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
265      },
266      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
267  };  };
 # $phrasing_category: all other elements  
268    
269  sub parse_string ($$$;$) {  my $svg_attr_name = {
270    my $self = shift->new;    attributename => 'attributeName',
271    my $s = \$_[0];    attributetype => 'attributeType',
272    $self->{document} = $_[1];    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    
334    ## NOTE: |set_inner_html| copies most of this method's code  my $foreign_attr_xname = {
335      '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    
349    my $i = 0;  ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
   my $line = 1;  
   my $column = 0;  
   $self->{set_next_input_character} = sub {  
     my $self = shift;  
350    
351      pop @{$self->{prev_input_character}};  ## TODO: Invoke the reset algorithm when a resettable element is
352      unshift @{$self->{prev_input_character}}, $self->{next_input_character};  ## created (cf. HTML5 revision 2259).
353    
354      $self->{next_input_character} = -1 and return if $i >= length $$s;  sub parse_byte_string ($$$$;$) {
355      $self->{next_input_character} = ord substr $$s, $i++, 1;    my $self = shift;
356      $column++;    my $charset_name = shift;
357          open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
358      if ($self->{next_input_character} == 0x000A) { # LF    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
359        $line++;  } # parse_byte_string
360        $column = 0;  
361      } elsif ($self->{next_input_character} == 0x000D) { # CR  sub parse_byte_stream ($$$$;$$) {
362        $i++ if substr ($$s, $i, 1) eq "\x0A";    # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
363        $self->{next_input_character} = 0x000A; # LF # MUST    my $self = ref $_[0] ? shift : shift->new;
364        $line++;    my $charset_name = shift;
365        $column = 0;    my $byte_stream = $_[0];
     } elsif ($self->{next_input_character} > 0x10FFFF) {  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
     } elsif ($self->{next_input_character} == 0x0000) { # NULL  
       !!!parse-error (type => 'NULL');  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
     }  
   };  
   $self->{prev_input_character} = [-1, -1, -1];  
   $self->{next_input_character} = -1;  
366    
367    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
368      my (%opt) = @_;      my (%opt) = @_;
369      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      warn "Parse error ($opt{type})\n";
   };  
   $self->{parse_error} = sub {  
     $onerror->(@_, line => $line, column => $column);  
370    };    };
371      $self->{parse_error} = $onerror; # updated later by parse_char_string
372    
373    $self->_initialize_tokenizer;    my $get_wrapper = $_[3] || sub ($) {
374    $self->_initialize_tree_constructor;      return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
   $self->_construct_tree;  
   $self->_terminate_tree_constructor;  
   
   return $self->{document};  
 } # parse_string  
   
 sub new ($) {  
   my $class = shift;  
   my $self = bless {}, $class;  
   $self->{set_next_input_character} = sub {  
     $self->{next_input_character} = -1;  
   };  
   $self->{parse_error} = sub {  
     #  
375    };    };
   return $self;  
 } # new  
   
 ## Implementations MUST act as if state machine in the spec  
   
 sub _initialize_tokenizer ($) {  
   my $self = shift;  
   $self->{state} = 'data'; # MUST  
   $self->{content_model_flag} = 'PCDATA'; # 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} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  
 ##       'character', or 'end-of-file'  
 ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  
 ##   ->{public_identifier} (DOCTYPE)  
 ##   ->{system_identifier} (DOCTYPE)  
 ##   ->{correct} == 1 or 0 (DOCTYPE)  
 ##   ->{attributes} isa HASH (start tag, end tag)  
 ##   ->{data} (comment, character)  
   
 ## 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.  
   
 sub _get_next_token ($) {  
   my $self = shift;  
   if (@{$self->{token}}) {  
     return shift @{$self->{token}};  
   }  
   
   A: {  
     if ($self->{state} eq 'data') {  
       if ($self->{next_input_character} == 0x0026) { # &  
         if ($self->{content_model_flag} eq 'PCDATA' or  
             $self->{content_model_flag} eq 'RCDATA') {  
           $self->{state} = 'entity data';  
           !!!next-input-character;  
           redo A;  
         } else {  
           #  
         }  
       } elsif ($self->{next_input_character} == 0x002D) { # -  
         if ($self->{content_model_flag} eq 'RCDATA' or  
             $self->{content_model_flag} eq '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_flag} eq 'PCDATA' or  
             (($self->{content_model_flag} eq 'CDATA' or  
               $self->{content_model_flag} eq 'RCDATA') and  
              not $self->{escape})) {  
           $self->{state} = 'tag open';  
           !!!next-input-character;  
           redo A;  
         } else {  
           #  
         }  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         if ($self->{escape} and  
             ($self->{content_model_flag} eq 'RCDATA' or  
              $self->{content_model_flag} eq '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'});  
         last A; ## TODO: ok?  
       }  
       # Anything else  
       my $token = {type => 'character',  
                    data => chr $self->{next_input_character}};  
       ## Stay in the data state  
       !!!next-input-character;  
   
       !!!emit ($token);  
   
       redo A;  
     } elsif ($self->{state} eq 'entity data') {  
       ## (cannot happen in CDATA state)  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity (0);  
   
       $self->{state} = 'data';  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!emit ({type => 'character', data => '&'});  
       } else {  
         !!!emit ($token);  
       }  
   
       redo A;  
     } elsif ($self->{state} eq 'tag open') {  
       if ($self->{content_model_flag} eq 'RCDATA' or  
           $self->{content_model_flag} eq 'CDATA') {  
         if ($self->{next_input_character} == 0x002F) { # /  
           !!!next-input-character;  
           $self->{state} = 'close tag open';  
           redo A;  
         } else {  
           ## reconsume  
           $self->{state} = 'data';  
   
           !!!emit ({type => 'character', data => '<'});  
   
           redo A;  
         }  
       } elsif ($self->{content_model_flag} eq 'PCDATA') {  
         if ($self->{next_input_character} == 0x0021) { # !  
           $self->{state} = 'markup declaration open';  
           !!!next-input-character;  
           redo A;  
         } elsif ($self->{next_input_character} == 0x002F) { # /  
           $self->{state} = 'close tag open';  
           !!!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',  
                tag_name => chr ($self->{next_input_character} + 0x0020)};  
           $self->{state} = 'tag name';  
           !!!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',  
                             tag_name => chr ($self->{next_input_character})};  
           $self->{state} = 'tag name';  
           !!!next-input-character;  
           redo A;  
         } elsif ($self->{next_input_character} == 0x003E) { # >  
           !!!parse-error (type => 'empty start tag');  
           $self->{state} = 'data';  
           !!!next-input-character;  
   
           !!!emit ({type => 'character', data => '<>'});  
   
           redo A;  
         } elsif ($self->{next_input_character} == 0x003F) { # ?  
           !!!parse-error (type => 'pio');  
           $self->{state} = 'bogus comment';  
           ## $self->{next_input_character} is intentionally left as is  
           redo A;  
         } else {  
           !!!parse-error (type => 'bare stago');  
           $self->{state} = 'data';  
           ## reconsume  
376    
377            !!!emit ({type => 'character', data => '<'});    ## 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            redo A;      ## 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 {        } else {
404          die "$0: $self->{content_model_flag}: Unknown content model flag";          !!!parse-error (type => 'charset:not supported',
405        }                          layer => 'encode',
406      } elsif ($self->{state} eq 'close tag open') {                          line => 1, column => 1,
407        if ($self->{content_model_flag} eq 'RCDATA' or                          value => $charset_name,
408            $self->{content_model_flag} eq 'CDATA') {                          level => $self->{level}->{uncertain});
         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';  
   
               !!!emit ({type => 'character', data => '</'});  
     
               redo A;  
             }  
           }  
           push @next_char, $self->{next_input_character};  
         
           unless ($self->{next_input_character} == 0x0009 or # HT  
                   $self->{next_input_character} == 0x000A or # LF  
                   $self->{next_input_character} == 0x000B or # VT  
                   $self->{next_input_character} == 0x000C or # FF  
                   $self->{next_input_character} == 0x0020 or # SP  
                   $self->{next_input_character} == 0x003E or # >  
                   $self->{next_input_character} == 0x002F or # /  
                   $self->{next_input_character} == -1) {  
             $self->{next_input_character} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = 'data';  
             !!!emit ({type => 'character', data => '</'});  
             redo A;  
           } else {  
             $self->{next_input_character} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
         } else {  
           ## No start tag token has ever been emitted  
           # next-input-character is already done  
           $self->{state} = 'data';  
           !!!emit ({type => 'character', data => '</'});  
           redo A;  
         }  
409        }        }
410              }
       if (0x0041 <= $self->{next_input_character} and  
           $self->{next_input_character} <= 0x005A) { # A..Z  
         $self->{current_token} = {type => 'end tag',  
                           tag_name => chr ($self->{next_input_character} + 0x0020)};  
         $self->{state} = 'tag name';  
         !!!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',  
                           tag_name => chr ($self->{next_input_character})};  
         $self->{state} = 'tag name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         !!!parse-error (type => 'empty end tag');  
         $self->{state} = 'data';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'bare etago');  
         $self->{state} = 'data';  
         # reconsume  
   
         !!!emit ({type => 'character', data => '</'});  
   
         redo A;  
       } else {  
         !!!parse-error (type => 'bogus end tag');  
         $self->{state} = 'bogus comment';  
         ## $self->{next_input_character} is intentionally left as is  
         redo A;  
       }  
     } elsif ($self->{state} eq 'tag name') {  
       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';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $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} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # 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';  
         !!!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} eq 'start tag') {  
           $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} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # 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';  
         # 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} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         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} eq 'before attribute name') {  
       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} eq 'start tag') {  
           $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} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # 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';  
         !!!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';  
         !!!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} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $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} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # 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';  
         # 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';  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'attribute name') {  
       my $before_leave = sub {  
         if (exists $self->{current_token}->{attributes} # start tag or end tag  
             ->{$self->{current_attribute}->{name}}) { # MUST  
           !!!parse-error (type => 'dupulicate attribute');  
           ## 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';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003D) { # =  
         $before_leave->();  
         $self->{state} = 'before attribute value';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         $before_leave->();  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $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} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # 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';  
         !!!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} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         $before_leave->();  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $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} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # 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';  
         # 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} eq 'after attribute name') {  
       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';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $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} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # 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';  
         !!!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';  
         !!!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} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $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} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # 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';  
         # 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';  
         !!!next-input-character;  
         redo A;          
       }  
     } elsif ($self->{state} eq 'before attribute value') {  
       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)';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0026) { # &  
         $self->{state} = 'attribute value (unquoted)';  
         ## reconsume  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0027) { # '  
         $self->{state} = 'attribute value (single-quoted)';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $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} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # 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';  
         !!!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} eq 'start tag') {  
           $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} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # 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';  
         ## 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)';  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'attribute value (double-quoted)') {  
       if ($self->{next_input_character} == 0x0022) { # "  
         $self->{state} = 'before attribute name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0026) { # &  
         $self->{last_attribute_value_state} = 'attribute value (double-quoted)';  
         $self->{state} = 'entity in attribute value';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed attribute value');  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $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} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # 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';  
         ## 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} eq 'attribute value (single-quoted)') {  
       if ($self->{next_input_character} == 0x0027) { # '  
         $self->{state} = 'before attribute name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0026) { # &  
         $self->{last_attribute_value_state} = 'attribute value (single-quoted)';  
         $self->{state} = 'entity in attribute value';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed attribute value');  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $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} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # 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';  
         ## reconsume  
411    
412          !!!emit ($self->{current_token}); # start tag or end tag      ## Step 2
413        my $byte_buffer = '';
414        for (1..1024) {
415          my $char = $byte_stream->getc;
416          last unless defined $char;
417          $byte_buffer .= $char;
418        } ## TODO: timeout
419    
420          redo A;      ## Step 3
421        } else {      if ($byte_buffer =~ /^\xFE\xFF/) {
422          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});        $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
423          ## Stay in the state        ($char_stream, $e_status) = $charset->get_decode_handle
424          !!!next-input-character;            ($byte_stream, allow_error_reporting => 1,
425          redo A;             allow_fallback => 1, byte_buffer => \$byte_buffer);
426        }        $self->{confident} = 1;
427      } elsif ($self->{state} eq 'attribute value (unquoted)') {        last SNIFFING;
428        if ($self->{next_input_character} == 0x0009 or # HT      } elsif ($byte_buffer =~ /^\xFF\xFE/) {
429            $self->{next_input_character} == 0x000A or # LF        $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
430            $self->{next_input_character} == 0x000B or # HT        ($char_stream, $e_status) = $charset->get_decode_handle
431            $self->{next_input_character} == 0x000C or # FF            ($byte_stream, allow_error_reporting => 1,
432            $self->{next_input_character} == 0x0020) { # SP             allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{state} = 'before attribute name';        $self->{confident} = 1;
434          !!!next-input-character;        last SNIFFING;
435          redo A;      } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
436        } elsif ($self->{next_input_character} == 0x0026) { # &        $charset = Message::Charset::Info->get_by_html_name ('utf-8');
437          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        ($char_stream, $e_status) = $charset->get_decode_handle
438          $self->{state} = 'entity in attribute value';            ($byte_stream, allow_error_reporting => 1,
439          !!!next-input-character;             allow_fallback => 1, byte_buffer => \$byte_buffer);
440          redo A;        $self->{confident} = 1;
441        } elsif ($self->{next_input_character} == 0x003E) { # >        last SNIFFING;
442          if ($self->{current_token}->{type} eq 'start tag') {      }
           $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} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # 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';  
         !!!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} eq 'start tag') {  
           $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} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # 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';  
         ## reconsume  
443    
444          !!!emit ($self->{current_token}); # start tag or end tag      ## Step 4
445        ## TODO: <meta charset>
446    
447          redo A;      ## Step 5
448        } else {      ## TODO: from history
         $self->{current_attribute}->{value} .= chr ($self->{next_input_character});  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'entity in attribute value') {  
       my $token = $self->_tokenize_attempt_to_consume_an_entity (1);  
449    
450        unless (defined $token) {      ## Step 6
451          $self->{current_attribute}->{value} .= '&';      require Whatpm::Charset::UniversalCharDet;
452        } else {      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
453          $self->{current_attribute}->{value} .= $token->{data};          ($byte_buffer);
454          ## ISSUE: spec says "append the returned character token to the current attribute's value"      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        $self->{state} = $self->{last_attribute_value_state};      ## Step 7: default
476        # next-input-character is already done      ## TODO: Make this configurable.
477        redo A;      $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
478      } elsif ($self->{state} eq 'bogus comment') {          ## NOTE: We choose |windows-1252| here, since |utf-8| should be
479        ## (only happen if PCDATA state)          ## detectable in the step 6.
480              require Whatpm::Charset::DecodeHandle;
481        my $token = {type => 'comment', data => ''};      $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
482            ($byte_stream);
483        BC: {      ($char_stream, $e_status)
484          if ($self->{next_input_character} == 0x003E) { # >          = $charset->get_decode_handle ($buffer,
485            $self->{state} = 'data';                                         allow_error_reporting => 1,
486            !!!next-input-character;                                         allow_fallback => 1,
487                                           byte_buffer => \$byte_buffer);
488            !!!emit ($token);      $buffer->{buffer} = $byte_buffer;
489        !!!parse-error (type => 'sniffing:default',
490            redo A;                      text => 'windows-1252',
491          } elsif ($self->{next_input_character} == -1) {                      level => $self->{level}->{info},
492            $self->{state} = 'data';                      line => 1, column => 1,
493            ## reconsume                      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            !!!emit ($token);    $self->{change_encoding} = sub {
517        my $self = shift;
518        $charset_name = shift;
519        my $token = shift;
520    
521            redo A;      $charset = Message::Charset::Info->get_by_html_name ($charset_name);
522          } else {      ($char_stream, $e_status) = $charset->get_decode_handle
523            $token->{data} .= chr ($self->{next_input_character});          ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
524            !!!next-input-character;           byte_buffer => \ $buffer->{buffer});
525            redo BC;      
526          }      if ($char_stream) { # if supported
527        } # BC        ## "Change the encoding" algorithm:
     } elsif ($self->{state} eq 'markup declaration open') {  
       ## (only happen if PCDATA state)  
528    
529        my @next_char;        ## Step 1    
530        push @next_char, $self->{next_input_character};        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        if ($self->{next_input_character} == 0x002D) { # -        ## Step 2
540          !!!next-input-character;        if (defined $self->{input_encoding} and
541          push @next_char, $self->{next_input_character};            $self->{input_encoding} eq $charset_name) {
542          if ($self->{next_input_character} == 0x002D) { # -          !!!parse-error (type => 'charset label:matching',
543            $self->{current_token} = {type => 'comment', data => ''};                          text => $charset_name,
544            $self->{state} = 'comment start';                          level => $self->{level}->{info});
545            !!!next-input-character;          $self->{confident} = 1;
546            redo A;          return;
         }  
       } 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';  
                     !!!next-input-character;  
                     redo A;  
                   }  
                 }  
               }  
             }  
           }  
         }  
547        }        }
548    
549        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'charset label detected',
550        $self->{next_input_character} = shift @next_char;                        text => $self->{input_encoding},
551        !!!back-next-input-character (@next_char);                        value => $charset_name,
552        $self->{state} = 'bogus comment';                        level => $self->{level}->{warn},
553        redo A;                        token => $token);
554                
555        ## ISSUE: typos in spec: chacacters, is is a parse error        ## Step 3
556        ## 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?        # if (can) {
557      } elsif ($self->{state} eq 'comment start') {          ## change the encoding on the fly.
558        if ($self->{next_input_character} == 0x002D) { # -          #$self->{confident} = 1;
559          $self->{state} = 'comment start dash';          #return;
560          !!!next-input-character;        # }
561          redo A;        
562        } elsif ($self->{next_input_character} == 0x003E) { # >        ## Step 4
563          !!!parse-error (type => 'bogus comment');        throw Whatpm::HTML::RestartParser ();
564          $self->{state} = 'data';      }
565          !!!next-input-character;    }; # $self->{change_encoding}
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = 'data';  
         ## reconsume  
566    
567          !!!emit ($self->{current_token}); # comment    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          redo A;    my $wrapped_char_stream = $get_wrapper->($char_stream);
578        } else {    $wrapped_char_stream->onerror ($char_onerror);
         $self->{current_token}->{data} # comment  
             .= chr ($self->{next_input_character});  
         $self->{state} = 'comment';  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'comment start dash') {  
       if ($self->{next_input_character} == 0x002D) { # -  
         $self->{state} = 'comment end';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         !!!parse-error (type => 'bogus comment');  
         $self->{state} = 'data';  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # comment  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = 'data';  
         ## reconsume  
579    
580          !!!emit ($self->{current_token}); # comment    my @args = ($_[1], $_[2]); # $doc, $onerror - $get_wrapper = undef;
581      my $return;
582      try {
583        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
584      } catch Whatpm::HTML::RestartParser with {
585        ## NOTE: Invoked after {change_encoding}.
586    
587        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;
606    
607          redo A;      $wrapped_char_stream = $get_wrapper->($char_stream);
608        } else {      $wrapped_char_stream->onerror ($char_onerror);
         $self->{current_token}->{data} # comment  
             .= '-' . chr ($self->{next_input_character});  
         $self->{state} = 'comment';  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'comment') {  
       if ($self->{next_input_character} == 0x002D) { # -  
         $self->{state} = 'comment end dash';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = 'data';  
         ## reconsume  
609    
610          !!!emit ($self->{current_token}); # comment      $return = $self->parse_char_stream ($wrapped_char_stream, @args);
611      };
612      return $return;
613    } # parse_byte_stream
614    
615          redo A;  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
616        } else {  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
617          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment  ## the encoding layer and the HTML layer does not ignore any U+FEFF,
618          ## Stay in the state  ## because the core part of our HTML parser expects a string of character,
619          !!!next-input-character;  ## not a string of bytes or code units or anything which might contain a BOM.
620          redo A;  ## 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      } elsif ($self->{state} eq 'comment end dash') {  ## strip the BOM and never strip any ZWNBSP.
       if ($self->{next_input_character} == 0x002D) { # -  
         $self->{state} = 'comment end';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = 'data';  
         ## reconsume  
623    
624          !!!emit ($self->{current_token}); # comment  sub parse_char_string ($$$;$$) {
625      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
626      my $self = shift;
627      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];
638      @{$self->{document}->child_nodes} = ();
639    
640          redo A;    ## NOTE: |set_inner_html| copies most of this method's code
       } else {  
         $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment  
         $self->{state} = 'comment';  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'comment end') {  
       if ($self->{next_input_character} == 0x003E) { # >  
         $self->{state} = 'data';  
         !!!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';  
         ## reconsume  
641    
642          !!!emit ($self->{current_token}); # comment    $self->{confident} = 1 unless exists $self->{confident};
643      $self->{document}->input_encoding ($self->{input_encoding})
644          if defined $self->{input_encoding};
645    ## TODO: |{input_encoding}| is needless?
646    
647      $self->{line_prev} = $self->{line} = 1;
648      $self->{column_prev} = -1;
649      $self->{column} = 0;
650      $self->{set_nc} = sub {
651        my $self = shift;
652    
653          redo A;      my $char = '';
654        } else {      if (defined $self->{next_nc}) {
655          !!!parse-error (type => 'dash in comment');        $char = $self->{next_nc};
656          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment        delete $self->{next_nc};
657          $self->{state} = 'comment';        $self->{nc} = ord $char;
658          !!!next-input-character;      } else {
659          redo A;        $self->{char_buffer} = '';
660        }        $self->{char_buffer_pos} = 0;
     } elsif ($self->{state} eq 'DOCTYPE') {  
       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';  
         !!!next-input-character;  
         redo A;  
       } else {  
         !!!parse-error (type => 'no space before DOCTYPE name');  
         $self->{state} = 'before DOCTYPE name';  
         ## reconsume  
         redo A;  
       }  
     } elsif ($self->{state} eq 'before DOCTYPE name') {  
       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';  
         !!!next-input-character;  
   
         !!!emit ({type => 'DOCTYPE'}); # incorrect  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'no DOCTYPE name');  
         $self->{state} = 'data';  
         ## reconsume  
661    
662          !!!emit ({type => 'DOCTYPE'}); # incorrect        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          redo A;        if ($input->read ($char, 1)) {
674            $self->{nc} = ord $char;
675        } else {        } else {
676          $self->{current_token}          $self->{nc} = -1;
677              = {type => 'DOCTYPE',          return;
678                 name => chr ($self->{next_input_character}),        }
679                 correct => 1};      }
 ## ISSUE: "Set the token's name name to the" in the spec  
         $self->{state} = 'DOCTYPE name';  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'DOCTYPE name') {  
 ## 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';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         $self->{state} = 'data';  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed DOCTYPE');  
         $self->{state} = 'data';  
         ## reconsume  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
680    
681          redo A;      ($self->{line_prev}, $self->{column_prev})
682        } else {          = ($self->{line}, $self->{column});
683          $self->{current_token}->{name}      $self->{column}++;
684            .= chr ($self->{next_input_character}); # DOCTYPE      
685          ## Stay in the state      if ($self->{nc} == 0x000A) { # LF
686          !!!next-input-character;        !!!cp ('j1');
687          redo A;        $self->{line}++;
688        }        $self->{column} = 0;
689      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{nc} == 0x000D) { # CR
690        if ($self->{next_input_character} == 0x0009 or # HT        !!!cp ('j2');
691            $self->{next_input_character} == 0x000A or # LF  ## TODO: support for abort/streaming
692            $self->{next_input_character} == 0x000B or # VT        my $next = '';
693            $self->{next_input_character} == 0x000C or # FF        if ($input->read ($next, 1) and $next ne "\x0A") {
694            $self->{next_input_character} == 0x0020) { # SP          $self->{next_nc} = $next;
695          ## Stay in the state        }
696          !!!next-input-character;        $self->{nc} = 0x000A; # LF # MUST
697          redo A;        $self->{line}++;
698        } elsif ($self->{next_input_character} == 0x003E) { # >        $self->{column} = 0;
699          $self->{state} = 'data';      } elsif ($self->{nc} == 0x0000) { # NULL
700          !!!next-input-character;        !!!cp ('j4');
701          !!!parse-error (type => 'NULL');
702          !!!emit ($self->{current_token}); # DOCTYPE        $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
703        }
704          redo A;    };
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed DOCTYPE');  
         $self->{state} = 'data';  
         ## 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';  
                   !!!next-input-character;  
                   redo A;  
                 }  
               }  
             }  
           }  
         }  
705    
706          #    $self->{read_until} = sub {
707        } elsif ($self->{next_input_character} == 0x0053 or # S      #my ($scalar, $specials_range, $offset) = @_;
708                 $self->{next_input_character} == 0x0073) { # s      return 0 if defined $self->{next_nc};
709          !!!next-input-character;  
710          if ($self->{next_input_character} == 0x0059 or # Y      my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
711              $self->{next_input_character} == 0x0079) { # y      my $offset = $_[2] || 0;
712            !!!next-input-character;  
713            if ($self->{next_input_character} == 0x0053 or # S      if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
714                $self->{next_input_character} == 0x0073) { # s        pos ($self->{char_buffer}) = $self->{char_buffer_pos};
715              !!!next-input-character;        if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
716              if ($self->{next_input_character} == 0x0054 or # T          substr ($_[0], $offset)
717                  $self->{next_input_character} == 0x0074) { # t              = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
718                !!!next-input-character;          my $count = $+[0] - $-[0];
719                if ($self->{next_input_character} == 0x0045 or # E          if ($count) {
720                    $self->{next_input_character} == 0x0065) { # e            $self->{column} += $count;
721                  !!!next-input-character;            $self->{char_buffer_pos} += $count;
722                  if ($self->{next_input_character} == 0x004D or # M            $self->{line_prev} = $self->{line};
723                      $self->{next_input_character} == 0x006D) { # m            $self->{column_prev} = $self->{column} - 1;
724                    $self->{state} = 'before DOCTYPE system identifier';            $self->{nc} = -1;
                   !!!next-input-character;  
                   redo A;  
                 }  
               }  
             }  
           }  
725          }          }
726            return $count;
         #  
727        } else {        } else {
728          !!!next-input-character;          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        !!!parse-error (type => 'string after DOCTYPE name');    my $onerror = $_[2] || sub {
743        $self->{state} = 'bogus DOCTYPE';      my (%opt) = @_;
744        # next-input-character is already done      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
745        redo A;      my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
746      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      warn "Parse error ($opt{type}) at line $line column $column\n";
747        if ({    };
748              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,    $self->{parse_error} = sub {
749              #0x000D => 1, # HT, LF, VT, FF, SP, CR      $onerror->(line => $self->{line}, column => $self->{column}, @_);
750            }->{$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)';  
         !!!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)';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} eq 0x003E) { # >  
         !!!parse-error (type => 'no PUBLIC literal');  
   
         $self->{state} = 'data';  
         !!!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';  
         ## reconsume  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!parse-error (type => 'string after PUBLIC');  
         $self->{state} = 'bogus DOCTYPE';  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {  
       if ($self->{next_input_character} == 0x0022) { # "  
         $self->{state} = 'after DOCTYPE public identifier';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed PUBLIC literal');  
   
         $self->{state} = 'data';  
         ## 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} eq 'DOCTYPE public identifier (single-quoted)') {  
       if ($self->{next_input_character} == 0x0027) { # '  
         $self->{state} = 'after DOCTYPE public identifier';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed PUBLIC literal');  
   
         $self->{state} = 'data';  
         ## 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} eq 'after DOCTYPE public identifier') {  
       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)';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0027) { # '  
         $self->{current_token}->{system_identifier} = ''; # DOCTYPE  
         $self->{state} = 'DOCTYPE system identifier (single-quoted)';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         $self->{state} = 'data';  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed DOCTYPE');  
   
         $self->{state} = 'data';  
         ## 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';  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'before DOCTYPE system identifier') {  
       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)';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0027) { # '  
         $self->{current_token}->{system_identifier} = ''; # DOCTYPE  
         $self->{state} = 'DOCTYPE system identifier (single-quoted)';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         !!!parse-error (type => 'no SYSTEM literal');  
         $self->{state} = 'data';  
         !!!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';  
         ## reconsume  
   
         delete $self->{current_token}->{correct};  
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } else {  
         !!!parse-error (type => 'string after SYSTEM');  
         $self->{state} = 'bogus DOCTYPE';  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {  
       if ($self->{next_input_character} == 0x0022) { # "  
         $self->{state} = 'after DOCTYPE system identifier';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed SYSTEM literal');  
   
         $self->{state} = 'data';  
         ## 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} eq 'DOCTYPE system identifier (single-quoted)') {  
       if ($self->{next_input_character} == 0x0027) { # '  
         $self->{state} = 'after DOCTYPE system identifier';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed SYSTEM literal');  
   
         $self->{state} = 'data';  
         ## 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} eq 'after DOCTYPE system identifier') {  
       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';  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed DOCTYPE');  
   
         $self->{state} = 'data';  
         ## 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';  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'bogus DOCTYPE') {  
       if ($self->{next_input_character} == 0x003E) { # >  
         $self->{state} = 'data';  
         !!!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';  
         ## reconsume  
751    
752          delete $self->{current_token}->{correct};    my $char_onerror = sub {
753          !!!emit ($self->{current_token}); # DOCTYPE      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          redo A;    $self->_initialize_tokenizer;
767        } else {    $self->_initialize_tree_constructor;
768          ## Stay in the state    $self->_construct_tree;
769          !!!next-input-character;    $self->_terminate_tree_constructor;
         redo A;  
       }  
     } else {  
       die "$0: $self->{state}: Unknown state";  
     }  
   } # A    
770    
771    die "$0: _get_next_token: unexpected case";    delete $self->{parse_error}; # remove loop
 } # _get_next_token  
772    
773  sub _tokenize_attempt_to_consume_an_entity ($$) {    return $self->{document};
774    my ($self, $in_attr) = @_;  } # parse_char_stream
775    
776    if ({  sub new ($) {
777         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,    my $class = shift;
778         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR    my $self = bless {
779        }->{$self->{next_input_character}}) {      level => {must => 'm',
780      ## Don't consume                should => 's',
781      ## No error                warn => 'w',
782      return undef;                info => 'i',
783    } elsif ($self->{next_input_character} == 0x0023) { # #                uncertain => 'u'},
784      !!!next-input-character;    }, $class;
785      if ($self->{next_input_character} == 0x0078 or # x    $self->{set_nc} = sub {
786          $self->{next_input_character} == 0x0058) { # X      $self->{nc} = -1;
787        my $code;    };
788        X: {    $self->{parse_error} = sub {
789          my $x_char = $self->{next_input_character};      #
790          !!!next-input-character;    };
791          if (0x0030 <= $self->{next_input_character} and    $self->{change_encoding} = sub {
792              $self->{next_input_character} <= 0x0039) { # 0..9      # if ($_[0] is a supported encoding) {
793            $code ||= 0;      #   run "change the encoding" algorithm;
794            $code *= 0x10;      #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
795            $code += $self->{next_input_character} - 0x0030;      # }
796            redo X;    };
797          } elsif (0x0061 <= $self->{next_input_character} and    $self->{application_cache_selection} = sub {
798                   $self->{next_input_character} <= 0x0066) { # a..f      #
799            $code ||= 0;    };
800            $code *= 0x10;    return $self;
801            $code += $self->{next_input_character} - 0x0060 + 9;  } # new
           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');  
           $self->{next_input_character} = 0x0023; # #  
           !!!back-next-input-character ($x_char);  
           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', 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;  
       }  
802    
803        if ($self->{next_input_character} == 0x003B) { # ;  ## Insertion modes
         !!!next-input-character;  
       } else {  
         !!!parse-error (type => 'no refc');  
       }  
804    
805        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {  sub AFTER_HTML_IMS () { 0b100 }
806          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);  sub HEAD_IMS ()       { 0b1000 }
807          $code = 0xFFFD;  sub BODY_IMS ()       { 0b10000 }
808        } elsif ($code > 0x10FFFF) {  sub BODY_TABLE_IMS () { 0b100000 }
809          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);  sub TABLE_IMS ()      { 0b1000000 }
810          $code = 0xFFFD;  sub ROW_IMS ()        { 0b10000000 }
811        } elsif ($code == 0x000D) {  sub BODY_AFTER_IMS () { 0b100000000 }
812          !!!parse-error (type => 'CR character reference');  sub FRAME_IMS ()      { 0b1000000000 }
813          $code = 0x000A;  sub SELECT_IMS ()     { 0b10000000000 }
814        } elsif (0x80 <= $code and $code <= 0x9F) {  #sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 } # see Whatpm::HTML::Tokenizer
815          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);      ## NOTE: "in foreign content" insertion mode is special; it is combined
816          $code = $c1_entity_char->{$code};      ## 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        return {type => 'character', data => chr $code};      ## NOTE: "in CDATA/RCDATA" insertion mode is also special; it is
820      } else {      ## combined with the original insertion mode.  In thie parser,
821        !!!parse-error (type => 'bare nero');      ## they are stored together in the bit-or'ed form.
822        !!!back-next-input-character ($self->{next_input_character});  
823        $self->{next_input_character} = 0x0023; # #  sub IM_MASK () { 0b11111111111 }
824        return undef;  
825      }  ## NOTE: "initial" and "before html" insertion modes have no constants.
826    } elsif ((0x0041 <= $self->{next_input_character} and  
827              $self->{next_input_character} <= 0x005A) or  ## NOTE: "after after body" insertion mode.
828             (0x0061 <= $self->{next_input_character} and  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
829              $self->{next_input_character} <= 0x007A)) {  
830      my $entity_name = chr $self->{next_input_character};  ## NOTE: "after after frameset" insertion mode.
831      !!!next-input-character;  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
832    
833      my $value = $entity_name;  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
834      my $match;  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
835      require Whatpm::_NamedEntityList;  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
836      our $EntityChar;  sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
837    sub IN_BODY_IM () { BODY_IMS }
838      while (length $entity_name < 10 and  sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
839             ## NOTE: Some number greater than the maximum length of entity name  sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
840             ((0x0041 <= $self->{next_input_character} and # a  sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
841               $self->{next_input_character} <= 0x005A) or # x  sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
842              (0x0061 <= $self->{next_input_character} and # a  sub IN_TABLE_IM () { TABLE_IMS }
843               $self->{next_input_character} <= 0x007A) or # z  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
844              (0x0030 <= $self->{next_input_character} and # 0  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
845               $self->{next_input_character} <= 0x0039) or # 9  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
846              $self->{next_input_character} == 0x003B)) { # ;  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
847        $entity_name .= chr $self->{next_input_character};  sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
848        if (defined $EntityChar->{$entity_name}) {  sub IN_COLUMN_GROUP_IM () { 0b10 }
         if ($self->{next_input_character} == 0x003B) { # ;  
           $value = $EntityChar->{$entity_name};  
           $match = 1;  
           !!!next-input-character;  
           last;  
         } elsif (not $in_attr) {  
           $value = $EntityChar->{$entity_name};  
           $match = -1;  
         } else {  
           $value .= chr $self->{next_input_character};  
         }  
       } else {  
         $value .= chr $self->{next_input_character};  
       }  
       !!!next-input-character;  
     }  
       
     if ($match > 0) {  
       return {type => 'character', data => $value};  
     } elsif ($match < 0) {  
       !!!parse-error (type => 'no refc');  
       return {type => 'character', data => $value};  
     } else {  
       !!!parse-error (type => 'bare ero');  
       ## NOTE: No characters are consumed in the spec.  
       return {type => 'character', data => '&'.$value};  
     }  
   } else {  
     ## no characters are consumed  
     !!!parse-error (type => 'bare ero');  
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
849    
850  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
851    my $self = shift;    my $self = shift;
# Line 1773  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 1792  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';  
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} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
906        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
907        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
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 ({
1056                'start tag' => 1,                START_TAG_TOKEN, 1,
1057                'end tag' => 1,                END_TAG_TOKEN, 1,
1058                'end-of-file' => 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} eq 'character') {      } 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} eq 'comment') {      } 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";        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} eq 'DOCTYPE') {        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} eq 'comment') {        } 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} eq 'character') {        } 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);
1141    
1142          #          #
1143          } elsif ($token->{type} == START_TAG_TOKEN) {
1144            if ($token->{tag_name} eq 'html') {
1145              my $root_element;
1146              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
1147              $self->{document}->append_child ($root_element);
1148              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 {
1169              !!!cp ('t25.1');
1170              #
1171            }
1172        } elsif ({        } elsif ({
1173                  'start tag' => 1,                  END_TAG_TOKEN, 1,
1174                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
1175                 }->{$token->{type}}) {                 }->{$token->{type}}) {
1176          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
1177          #          #
1178        } else {        } else {
1179          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
1180        }        }
1181        my $root_element; !!!create-element ($root_element, 'html');  
1182        $self->{document}->append_child ($root_element);      my $root_element;
1183        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
1184        ## reprocess      $self->{document}->append_child ($root_element);
1185        #redo B;      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
1186        return; ## Go to the main phase.  
1187        $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 2036  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                        select => 'in select',        if ($node->[1] & FOREIGN_EL) {
1222                        td => 'in cell',          !!!cp ('t28.1');
1223                        th => 'in cell',          ## NOTE: Strictly spaking, the line below only applies to MathML and
1224                        tr => 'in row',          ## SVG elements.  Currently the HTML syntax supports only MathML and
1225                        tbody => 'in table body',          ## SVG elements as foreigners.
1226                        thead => 'in table head',          $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
1227                        tfoot => 'in table foot',        } elsif ($node->[1] == TABLE_CELL_EL) {
1228                        caption => 'in caption',          if ($last) {
1229                        colgroup => 'in column group',            !!!cp ('t28.2');
1230                        table => 'in table',            #
1231                        head => 'in body', # not in head!          } else {
1232                        body => 'in body',            !!!cp ('t28.3');
1233                        frameset => 'in frameset',            $new_mode = IN_CELL_IM;
1234                       }->{$node->[1]};          }
1235          } else {
1236            !!!cp ('t28.4');
1237            $new_mode = {
1238                          select => IN_SELECT_IM,
1239                          ## NOTE: |option| and |optgroup| do not set
1240                          ## insertion mode to "in select" by themselves.
1241                          tr => IN_ROW_IM,
1242                          tbody => IN_TABLE_BODY_IM,
1243                          thead => IN_TABLE_BODY_IM,
1244                          tfoot => IN_TABLE_BODY_IM,
1245                          caption => IN_CAPTION_IM,
1246                          colgroup => IN_COLUMN_GROUP_IM,
1247                          table => IN_TABLE_IM,
1248                          head => IN_BODY_IM, # not in head!
1249                          body => IN_BODY_IM,
1250                          frameset => IN_FRAMESET_IM,
1251                         }->{$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            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
1259              $self->{insertion_mode} = BEFORE_HEAD_IM;
1260          } else {          } else {
1261            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
1262              !!!cp ('t30');
1263              $self->{insertion_mode} = AFTER_HEAD_IM;
1264          }          }
1265          return;          return;
1266          } else {
1267            !!!cp ('t31');
1268        }        }
1269                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
1270        ## Step 16        ## Step 16
1271          $self->{insertion_mode} = IN_BODY_IM and return if $last;
1272          
1273          ## 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 ($) {
1285    my $self = shift;    my $self = shift;
1286    
   my $previous_insertion_mode;  
   
1287    my $active_formatting_elements = [];    my $active_formatting_elements = [];
1288    
1289    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2114  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 2128  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 2162  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
1391      $insert->($el); # /context node/->append_child ($el)      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
   
     ## Step 3  
     $self->{content_model_flag} = $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} eq 'character') { # 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_flag} = 'PCDATA';  
1396    
1397      ## Step 7      !!!nack ('t40.1');
     if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {  
       ## Ignore the token  
     } else {  
       !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});  
     }  
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      $self->{content_model_flag} = 'CDATA';      ## 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;
1420      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
       
     my $text = '';  
     !!!next-token;  
     while ($token->{type} eq 'character') {  
       $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_flag} = 'PCDATA';  
1421    
1422      if ($token->{type} eq 'end tag' 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 2296  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 2326  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 2351  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 2373  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 2386  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 2408  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 2425  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 2437  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 2480  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 $in_body = sub {    ## NOTE: Insert a character (MUST): When a character is inserted, if
1707      my $insert = shift;    ## the last node that was inserted by the parser is a Text node and
1708      if ($token->{type} eq 'start tag') {    ## the character has to be inserted after that node, then the
1709        if ($token->{tag_name} eq 'script') {    ## character is appended to the Text node.  However, if any other
1710          ## NOTE: This is an "as if in head" code clone    ## node is inserted by the parser, then a new Text node is created
1711          $script_start_tag->($insert);    ## and the character is appended as that Text node.  If I'm not
1712          return;    ## wrong, for a parser with scripting disabled, there are only two
1713        } elsif ($token->{tag_name} eq 'style') {    ## cases where this occurs.  One is the case where an element node
1714          ## NOTE: This is an "as if in head" code clone    ## is inserted to the |head| element.  This is covered by using the
1715          $parse_rcdata->('CDATA', $insert);    ## |$self->{head_element_inserted}| flag.  Another is the case where
1716          return;    ## an element or comment is inserted into the |table| subtree while
1717        } elsif ({    ## foster parenting happens.  This is covered by using the [2] flag
1718                  base => 1, link => 1,    ## of the |$open_tables| structure.  All other cases are handled
1719                 }->{$token->{tag_name}}) {    ## simply by calling |manakai_append_text| method.
1720          ## NOTE: This is an "as if in head" code clone, only "-t" differs  
1721          !!!insert-element-t ($token->{tag_name}, $token->{attributes});    ## TODO: |<body><script>document.write("a<br>");
1722          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.    ## document.body.removeChild (document.body.lastChild);
1723          !!!next-token;    ## document.write ("b")</script>|
1724          return;  
1725        } elsif ($token->{tag_name} eq 'meta') {    B: while (1) {
1726          ## NOTE: This is an "as if in head" code clone, only "-t" differs      if ($token->{type} == DOCTYPE_TOKEN) {
1727          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!cp ('t73');
1728          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
1729          ## Ignore the token
1730          ## Stay in the phase
1731          !!!next-token;
1732          next B;
1733        } elsif ($token->{type} == START_TAG_TOKEN and
1734                 $token->{tag_name} eq 'html') {
1735          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
1736            !!!cp ('t79');
1737            !!!parse-error (type => 'after html', text => 'html', token => $token);
1738            $self->{insertion_mode} = AFTER_BODY_IM;
1739          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
1740            !!!cp ('t80');
1741            !!!parse-error (type => 'after html', text => 'html', token => $token);
1742            $self->{insertion_mode} = AFTER_FRAMESET_IM;
1743          } else {
1744            !!!cp ('t81');
1745          }
1746    
1747          unless ($self->{confident}) {        !!!cp ('t82');
1748            my $charset;        !!!parse-error (type => 'not first start tag', token => $token);
1749            if ($token->{attributes}->{charset}) { ## TODO: And if supported        my $top_el = $self->{open_elements}->[0]->[0];
1750              $charset = $token->{attributes}->{charset}->{value};        for my $attr_name (keys %{$token->{attributes}}) {
1751            }          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
1752            if ($token->{attributes}->{'http-equiv'}) {            !!!cp ('t84');
1753              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.            $top_el->set_attribute_ns
1754              if ($token->{attributes}->{'http-equiv'}->{value}              (undef, [undef, $attr_name],
1755                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=               $token->{attributes}->{$attr_name}->{value});
                     [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|  
                     ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {  
               $charset = defined $1 ? $1 : defined $2 ? $2 : $3;  
             } ## TODO: And if supported  
           }  
           ## TODO: Change the encoding  
1756          }          }
1757          }
1758          !!!next-token;        !!!nack ('t84.1');
1759          return;        !!!next-token;
1760        } elsif ($token->{tag_name} eq 'title') {        next B;
1761          !!!parse-error (type => 'in body:title');      } elsif ($token->{type} == COMMENT_TOKEN) {
1762          ## NOTE: This is an "as if in head" code clone        my $comment = $self->{document}->create_comment ($token->{data});
1763          $parse_rcdata->('RCDATA', sub {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
1764            if (defined $self->{head_element}) {          !!!cp ('t85');
1765              $self->{head_element}->append_child ($_[0]);          $self->{document}->append_child ($comment);
1766            } else {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
1767              $insert->($_[0]);          !!!cp ('t86');
1768            }          $self->{open_elements}->[0]->[0]->append_child ($comment);
1769          });        } else {
1770          return;          !!!cp ('t87');
1771        } elsif ($token->{tag_name} eq 'body') {          $self->{open_elements}->[-1]->[0]->append_child ($comment);
1772          !!!parse-error (type => 'in body:body');          $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
1773                        }
1774          if (@{$self->{open_elements}} == 1 or        !!!next-token;
1775              $self->{open_elements}->[1]->[1] ne 'body') {        next B;
1776            ## Ignore the token      } elsif ($self->{insertion_mode} & IN_CDATA_RCDATA_IM) {
1777          if ($token->{type} == CHARACTER_TOKEN) {
1778            $token->{data} =~ s/^\x0A// if $self->{ignore_newline};
1779            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 {          } else {
1786            my $body_el = $self->{open_elements}->[1]->[0];            !!!cp ('t43.1');
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
1787          }          }
1788          !!!next-token;          !!!next-token;
1789          return;          next B;
1790        } elsif ({        } elsif ($token->{type} == END_TAG_TOKEN) {
1791                  address => 1, blockquote => 1, center => 1, dir => 1,          delete $self->{ignore_newline};
1792                  div => 1, dl => 1, fieldset => 1, listing => 1,  
1793                  menu => 1, ol => 1, p => 1, ul => 1,          if ($token->{tag_name} eq 'script') {
1794                  pre => 1,            !!!cp ('t50');
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
1795                        
1796          !!!insert-element-t ($token->{tag_name}, $token->{attributes});            ## Para 1-2
1797          if ($token->{tag_name} eq 'pre') {            my $script = pop @{$self->{open_elements}};
1798            !!!next-token;            
1799            if ($token->{type} eq 'character') {            ## Para 3
1800              $token->{data} =~ s/^\x0A//;            $self->{insertion_mode} &= ~ IN_CDATA_RCDATA_IM;
1801              unless (length $token->{data}) {  
1802                !!!next-token;            ## Para 4
1803              }            ## TODO: $old_insertion_point = $current_insertion_point;
1804            }            ## TODO: $current_insertion_point = just before $self->{nc};
1805          } else {  
1806            !!!next-token;            ## Para 5
1807          }            ## TODO: Run the $script->[0].
1808          return;  
1809        } elsif ($token->{tag_name} eq 'form') {            ## Para 6
1810          if (defined $self->{form_element}) {            ## TODO: $current_insertion_point = $old_insertion_point;
1811            !!!parse-error (type => 'in form:form');  
1812            ## Ignore the token            ## Para 7
1813              ## TODO: if ($pending_external_script) {
1814                ## TODO: ...
1815              ## TODO: }
1816    
1817            !!!next-token;            !!!next-token;
1818            return;            next B;
1819          } else {          } else {
1820            ## has a p element in scope            !!!cp ('t42');
1821            INSCOPE: for (reverse @{$self->{open_elements}}) {  
1822              if ($_->[1] eq 'p') {            pop @{$self->{open_elements}};
1823                !!!back-token;  
1824                $token = {type => 'end tag', tag_name => 'p'};            $self->{insertion_mode} &= ~ IN_CDATA_RCDATA_IM;
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
1825            !!!next-token;            !!!next-token;
1826            return;            next B;
1827          }          }
1828        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
1829          ## has a p element in scope          delete $self->{ignore_newline};
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
1830    
1831          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t44');
1832          push @$active_formatting_elements, $self->{open_elements}->[-1];          !!!parse-error (type => 'not closed',
1833                            text => $self->{open_elements}->[-1]->[0]
1834                                ->manakai_local_name,
1835                            token => $token);
1836    
1837          !!!next-token;          #if ($self->{open_elements}->[-1]->[1] == SCRIPT_EL) {
1838          return;          #  ## TODO: Mark as "already executed"
1839        } elsif ({          #}
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'nobr') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
1840    
1841          ## has a |nobr| element in scope          pop @{$self->{open_elements}};
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'nobr') {  
             !!!parse-error (type => 'not closed:nobr');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'nobr'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
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;          !!!next-token;
1854          return;          next B;
1855        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{type} == START_TAG_TOKEN) {
1856                 $token->{tag_name} eq 'object') {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
1857          $reconstruct_active_formatting_elements->($insert_to_current);               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
1858                        not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
1859          !!!insert-element-t ($token->{tag_name}, $token->{attributes});              ($token->{tag_name} eq 'svg' and
1860          push @$active_formatting_elements, ['#marker', ''];               $self->{open_elements}->[-1]->[1] == MML_AXML_EL)) {
1861                      ## NOTE: "using the rules for secondary insertion mode"then"continue"
1862          !!!next-token;            !!!cp ('t87.2');
1863          return;            #
1864        } elsif ($token->{tag_name} eq 'xmp') {          } elsif ({
1865          $reconstruct_active_formatting_elements->($insert_to_current);                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
1866          $parse_rcdata->('CDATA', $insert);                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
1867          return;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
1868        } elsif ($token->{tag_name} eq 'table') {                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
1869          ## has a p element in scope                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
1870          INSCOPE: for (reverse @{$self->{open_elements}}) {                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
1871            if ($_->[1] eq 'p') {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
1872              !!!back-token;                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
1873              $token = {type => 'end tag', tag_name => 'p'};                   }->{$token->{tag_name}}) {
1874              return;            !!!cp ('t87.2');
1875            } elsif ({            !!!parse-error (type => 'not closed',
1876                      table => 1, caption => 1, td => 1, th => 1,                            text => $self->{open_elements}->[-1]->[0]
1877                      button => 1, marquee => 1, object => 1, html => 1,                                ->manakai_local_name,
1878                     }->{$_->[1]}) {                            token => $token);
1879              last INSCOPE;  
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            }            }
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
1930    
1931          ## NOTE: There is an "as if <br>" code clone.            ## "adjust SVG attributes" (SVG only) - done in insert-element-f
1932          $reconstruct_active_formatting_elements->($insert_to_current);  
1933                      ## "adjust foreign attributes" - done in insert-element-f
1934          !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
1935          pop @{$self->{open_elements}};            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
1936            
1937          !!!next-token;            if ($self->{self_closing}) {
1938          return;              pop @{$self->{open_elements}};
1939        } elsif ($token->{tag_name} eq 'hr') {              !!!ack ('t87.3');
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           my $form_attrs;  
           $form_attrs->{action} = $at->{action} if $at->{action};  
           my $prompt_attr = $at->{prompt};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           delete $at->{action};  
           delete $at->{prompt};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form',  
                          attributes => $form_attrs},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                        );  
           if ($prompt_attr) {  
             push @tokens, {type => 'character', data => $prompt_attr->{value}};  
1940            } else {            } else {
1941              push @tokens, {type => 'character',              !!!cp ('t87.4');
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
           }  
           push @tokens,  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'};  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'textarea') {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         ## TODO: $self->{form_element} if defined  
         $self->{content_model_flag} = 'RCDATA';  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         if ($token->{type} eq 'character') {  
           $token->{data} =~ s/^\x0A//;  
           unless (length $token->{data}) {  
             !!!next-token;  
1942            }            }
1943          }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
1944            !!!next-token;            !!!next-token;
1945              next B;
1946          }          }
1947          if (length $text) {        } elsif ($token->{type} == END_TAG_TOKEN) {
1948            $el->manakai_append_text ($text);          ## NOTE: "using the rules for secondary insertion mode" then "continue"
1949          }          !!!cp ('t87.5');
1950                    #
1951          $self->{content_model_flag} = 'PCDATA';        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
1952                    !!!cp ('t87.6');
1953          if ($token->{type} eq 'end tag' and          !!!parse-error (type => 'not closed',
1954              $token->{tag_name} eq $tag_name) {                          text => $self->{open_elements}->[-1]->[0]
1955            ## Ignore the token                              ->manakai_local_name,
1956          } else {                          token => $token);
1957            !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
1958          }          pop @{$self->{open_elements}}
1959          !!!next-token;              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
1960          return;  
1961        } elsif ({          ## NOTE: |<span><svg>| ... two parse errors, |<svg>| ... a parse error.
1962                  iframe => 1,  
1963                  noembed => 1,          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
1964                  noframes => 1,          ## Reprocess.
1965                  noscript => 0, ## TODO: 1 if scripting is enabled          next B;
                }->{$token->{tag_name}}) {  
         $parse_rcdata->('CDATA', $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
1966        } else {        } else {
1967          $reconstruct_active_formatting_elements->($insert_to_current);          die "$0: $token->{type}: Unknown token type";        
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
1968        }        }
1969      } elsif ($token->{type} eq 'end tag') {      }
       if ($token->{tag_name} eq 'body') {  
         if (@{$self->{open_elements}} > 1 and  
             $self->{open_elements}->[1]->[1] eq 'body') {  
           for (@{$self->{open_elements}}) {  
             unless ({  
                        dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                        th => 1, tr => 1, body => 1, html => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$_->[1]}) {  
               !!!parse-error (type => 'not closed:'.$_->[1]);  
             }  
           }  
1970    
1971            $self->{insertion_mode} = 'after body';      if ($self->{insertion_mode} & HEAD_IMS) {
1972            !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
1973            return;          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
1974          } else {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
1975            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              if ($self->{head_element_inserted}) {
1976            ## Ignore the token                !!!cp ('t88.3');
1977            !!!next-token;                $self->{open_elements}->[-1]->[0]->append_child
1978            return;                  ($self->{document}->create_text_node ($1));
1979          }                delete $self->{head_element_inserted};
1980        } elsif ($token->{tag_name} eq 'html') {                ## NOTE: |</head> <link> |
1981          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {                #
1982            ## ISSUE: There is an issue in the spec.              } else {
1983            if ($self->{open_elements}->[-1]->[1] ne 'body') {                !!!cp ('t88.2');
1984              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
1985            }                ## NOTE: |</head> &#x20;|
1986            $self->{insertion_mode} = 'after body';                #
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
1987              }              }
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           if (defined $i) {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
1988            } else {            } else {
1989              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.1');
1990                ## Ignore the token.
1991                #
1992            }            }
1993          }            unless (length $token->{data}) {
1994                        !!!cp ('t88');
1995          if (defined $i) {              !!!next-token;
1996            splice @{$self->{open_elements}}, $i;              next B;
         } elsif ($token->{tag_name} eq 'p') {  
           ## As if <p>, then reprocess the current token  
           my $el;  
           !!!create-element ($el, 'p');  
           $insert->($el);  
         }  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
1997            }            }
1998          } # INSCOPE  ## TODO: set $token->{column} appropriately
           
         if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {  
           pop @{$self->{open_elements}};  
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
1999          }          }
2000    
2001          undef $self->{form_element};          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2002          !!!next-token;            !!!cp ('t89');
2003          return;            ## As if <head>
2004        } elsif ({            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2005                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2006                 }->{$token->{tag_name}}) {            push @{$self->{open_elements}},
2007          ## has an element in scope                [$self->{head_element}, $el_category->{head}];
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ($token->{tag_name} eq 'br') {  
         !!!parse-error (type => 'unmatched end tag:br');  
2008    
2009          ## As if <br>            ## Reprocess in the "in head" insertion mode...
2010          $reconstruct_active_formatting_elements->($insert_to_current);            pop @{$self->{open_elements}};
           
         my $el;  
         !!!create-element ($el, 'br');  
         $insert->($el);  
           
         ## Ignore the token.  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
2011    
2012          ## Step 2            ## Reprocess in the "after head" insertion mode...
2013          S2: {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2014            if ($node->[1] eq $token->{tag_name}) {            !!!cp ('t90');
2015              ## Step 1            ## As if </noscript>
2016              ## generate implied end tags            pop @{$self->{open_elements}};
2017              if ({            !!!parse-error (type => 'in noscript:#text', token => $token);
2018                   dd => 1, dt => 1, li => 1, p => 1,            
2019                   td => 1, th => 1, tr => 1,            ## Reprocess in the "in head" insertion mode...
2020                   tbody => 1, tfoot=> 1, thead => 1,            ## As if </head>
2021                  }->{$self->{open_elements}->[-1]->[1]}) {            pop @{$self->{open_elements}};
2022                !!!back-token;  
2023                $token = {type => 'end tag',            ## Reprocess in the "after head" insertion mode...
2024                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2025                return;            !!!cp ('t91');
2026              }            pop @{$self->{open_elements}};
2027            
2028              ## Step 2            ## Reprocess in the "after head" insertion mode...
2029              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {          } else {
2030                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!cp ('t92');
2031              }          }
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
2032    
2033            ## "after head" insertion mode
2034            ## As if <body>
2035            !!!insert-element ('body',, $token);
2036            $self->{insertion_mode} = IN_BODY_IM;
2037            ## reprocess
2038            next B;
2039          } elsif ($token->{type} == START_TAG_TOKEN) {
2040            if ($token->{tag_name} eq 'head') {
2041              if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2042                !!!cp ('t93');
2043                !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
2044                $self->{open_elements}->[-1]->[0]->append_child
2045                    ($self->{head_element});
2046                push @{$self->{open_elements}},
2047                    [$self->{head_element}, $el_category->{head}];
2048                $self->{insertion_mode} = IN_HEAD_IM;
2049                !!!nack ('t93.1');
2050              !!!next-token;              !!!next-token;
2051              last S2;              next B;
2052              } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2053                !!!cp ('t93.2');
2054                !!!parse-error (type => 'after head', text => 'head',
2055                                token => $token);
2056                ## Ignore the token
2057                !!!nack ('t93.3');
2058                !!!next-token;
2059                next B;
2060            } else {            } else {
2061              ## Step 3              !!!cp ('t95');
2062              if (not $formatting_category->{$node->[1]} and              !!!parse-error (type => 'in head:head',
2063                  #not $phrasing_category->{$node->[1]} and                              token => $token); # or in head noscript
2064                  ($special_category->{$node->[1]} or              ## Ignore the token
2065                   $scoping_category->{$node->[1]})) {              !!!nack ('t95.1');
2066                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
2067                ## Ignore the token              next B;
               !!!next-token;  
               last S2;  
             }  
2068            }            }
2069                      } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2070            ## Step 4            !!!cp ('t96');
2071            $node_i--;            ## As if <head>
2072            $node = $self->{open_elements}->[$node_i];            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2073                        $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2074            ## Step 5;            push @{$self->{open_elements}},
2075            redo S2;                [$self->{head_element}, $el_category->{head}];
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
2076    
2077    B: {            $self->{insertion_mode} = IN_HEAD_IM;
2078      if ($self->{insertion_mode} ne 'trailing end') {            ## Reprocess in the "in head" insertion mode...
2079        if ($token->{type} eq 'DOCTYPE') {          } else {
2080          !!!parse-error (type => 'in html:#DOCTYPE');            !!!cp ('t97');
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
 ## ISSUE: "aa<html>" is not a parse error.  
 ## ISSUE: "<html>" in fragment is not a parse error.  
         unless ($token->{first_start_tag}) {  
           !!!parse-error (type => 'not first start tag');  
         }  
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
2081          }          }
2082    
2083          ## Stop parsing          if ($token->{tag_name} eq 'base') {
2084          last B;            if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2085                !!!cp ('t98');
2086                ## As if </noscript>
2087                pop @{$self->{open_elements}};
2088                !!!parse-error (type => 'in noscript', text => 'base',
2089                                token => $token);
2090              
2091                $self->{insertion_mode} = IN_HEAD_IM;
2092                ## Reprocess in the "in head" insertion mode...
2093              } else {
2094                !!!cp ('t99');
2095              }
2096    
2097          ## ISSUE: There is an issue in the spec.            ## NOTE: There is a "as if in head" code clone.
2098        } else {            if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2099          if ($self->{insertion_mode} eq 'before head') {              !!!cp ('t100');
2100            if ($token->{type} eq 'character') {              !!!parse-error (type => 'after head',
2101              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                              text => $token->{tag_name}, token => $token);
2102                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              push @{$self->{open_elements}},
2103                unless (length $token->{data}) {                  [$self->{head_element}, $el_category->{head}];
2104                  !!!next-token;              $self->{head_element_inserted} = 1;
                 redo B;  
               }  
             }  
             ## As if <head>  
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  head => 1, body => 1, html => 1,  
                  p => 1, br => 1,  
                 }->{$token->{tag_name}}) {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
2105            } else {            } else {
2106              die "$0: $token->{type}: Unknown type";              !!!cp ('t101');
2107            }            }
2108          } elsif ($self->{insertion_mode} eq 'in head' or            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2109                   $self->{insertion_mode} eq 'in head noscript' or            pop @{$self->{open_elements}};
2110                   $self->{insertion_mode} eq 'after head') {            pop @{$self->{open_elements}} # <head>
2111            if ($token->{type} eq 'character') {                if $self->{insertion_mode} == AFTER_HEAD_IM;
2112              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {            !!!nack ('t101.1');
2113                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            !!!next-token;
2114                unless (length $token->{data}) {            next B;
2115                  !!!next-token;          } elsif ($token->{tag_name} eq 'link') {
2116                  redo B;            ## NOTE: There is a "as if in head" code clone.
2117                }            if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2118              }              !!!cp ('t102');
2119                            !!!parse-error (type => 'after head',
2120              #                              text => $token->{tag_name}, token => $token);
2121            } elsif ($token->{type} eq 'comment') {              push @{$self->{open_elements}},
2122              my $comment = $self->{document}->create_comment ($token->{data});                  [$self->{head_element}, $el_category->{head}];
2123              $self->{open_elements}->[-1]->[0]->append_child ($comment);              $self->{head_element_inserted} = 1;
2124              } else {
2125                !!!cp ('t103');
2126              }
2127              !!!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;              !!!next-token;
2148              redo B;              next B;
2149            } elsif ($token->{type} eq 'start tag') {            } else {
2150              if ({base => ($self->{insertion_mode} eq 'in head' or              ## NOTE: "in head noscript" or "after head" insertion mode
2151                            $self->{insertion_mode} eq 'after head'),              ## - in these cases, these tags are treated as same as
2152                   link => 1}->{$token->{tag_name}}) {              ## normal in-body tags.
2153                ## NOTE: There is a "as if in head" code clone.              !!!cp ('t103.3');
2154                if ($self->{insertion_mode} eq 'after head') {              #
2155                  !!!parse-error (type => 'after head:'.$token->{tag_name});            }
2156                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];          } elsif ($token->{tag_name} eq 'meta') {
2157                }            ## NOTE: There is a "as if in head" code clone.
2158                !!!insert-element ($token->{tag_name}, $token->{attributes});            if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2159                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.              !!!cp ('t104');
2160                pop @{$self->{open_elements}}              !!!parse-error (type => 'after head',
2161                    if $self->{insertion_mode} eq 'after head';                              text => $token->{tag_name}, token => $token);
2162                !!!next-token;              push @{$self->{open_elements}},
2163                redo B;                  [$self->{head_element}, $el_category->{head}];
2164              } elsif ($token->{tag_name} eq 'meta') {              $self->{head_element_inserted} = 1;
2165                ## NOTE: There is a "as if in head" code clone.            } else {
2166                if ($self->{insertion_mode} eq 'after head') {              !!!cp ('t105');
2167                  !!!parse-error (type => 'after head:'.$token->{tag_name});            }
2168                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2169                }            my $meta_el = pop @{$self->{open_elements}};
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
2170    
2171                unless ($self->{confident}) {                unless ($self->{confident}) {
2172                  my $charset;                  if ($token->{attributes}->{charset}) {
2173                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                    !!!cp ('t106');
2174                    $charset = $token->{attributes}->{charset}->{value};                    ## NOTE: Whether the encoding is supported or not is handled
2175                      ## in the {change_encoding} callback.
2176                      $self->{change_encoding}
2177                          ->($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}) {
2185                      if ($token->{attributes}->{content}->{value}
2186                          =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
2187                              [\x09\x0A\x0C\x0D\x20]*=
2188                              [\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}
2195                            ->($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                  if ($token->{attributes}->{'http-equiv'}) {                } else {
2206                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                  if ($token->{attributes}->{charset}) {
2207                    if ($token->{attributes}->{'http-equiv'}->{value}                    !!!cp ('t109');
2208                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2209                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                        ->set_user_data (manakai_has_reference =>
2210                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                                             $token->{attributes}->{charset}
2211                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                                                 ->{has_reference});
2212                    } ## TODO: And if supported                  }
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                  }                  }
                 ## TODO: Change the encoding  
2220                }                }
2221    
2222                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
2223                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2224                    if $self->{insertion_mode} eq 'after head';                !!!ack ('t110.1');
2225                !!!next-token;                !!!next-token;
2226                redo B;                next B;
2227              } elsif ($token->{tag_name} eq 'title' and          } elsif ($token->{tag_name} eq 'title') {
2228                       $self->{insertion_mode} eq 'in head') {            if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2229                ## NOTE: There is a "as if in head" code clone.              !!!cp ('t111');
2230                if ($self->{insertion_mode} eq 'after head') {              ## As if </noscript>
2231                  !!!parse-error (type => 'after head:'.$token->{tag_name});              pop @{$self->{open_elements}};
2232                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!parse-error (type => 'in noscript', text => 'title',
2233                }                              token => $token);
2234                my $parent = defined $self->{head_element} ? $self->{head_element}            
2235                    : $self->{open_elements}->[-1]->[0];              $self->{insertion_mode} = IN_HEAD_IM;
2236                $parse_rcdata->('RCDATA', sub { $parent->append_child ($_[0]) });              ## Reprocess in the "in head" insertion mode...
2237                pop @{$self->{open_elements}}            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2238                    if $self->{insertion_mode} eq 'after head';              !!!cp ('t112');
2239                redo B;              !!!parse-error (type => 'after head',
2240              } elsif ($token->{tag_name} eq 'style') {                              text => $token->{tag_name}, token => $token);
2241                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and              push @{$self->{open_elements}},
2242                ## insertion mode 'in head')                  [$self->{head_element}, $el_category->{head}];
2243                ## NOTE: There is a "as if in head" code clone.              $self->{head_element_inserted} = 1;
2244                if ($self->{insertion_mode} eq 'after head') {            } else {
2245                  !!!parse-error (type => 'after head:'.$token->{tag_name});              !!!cp ('t113');
2246                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            }
2247                }  
2248                $parse_rcdata->('CDATA', $insert_to_current);            ## NOTE: There is a "as if in head" code clone.
2249                pop @{$self->{open_elements}}            $parse_rcdata->(RCDATA_CONTENT_MODEL);
2250                    if $self->{insertion_mode} eq 'after head';            ## ISSUE: A spec bug [Bug 6038]
2251                redo B;            splice @{$self->{open_elements}}, -2, 1, () # <head>
2252              } elsif ($token->{tag_name} eq 'noscript') {                if ($self->{insertion_mode} & IM_MASK) == AFTER_HEAD_IM;
2253                if ($self->{insertion_mode} eq 'in head') {            next B;
2254            } elsif ($token->{tag_name} eq 'style' or
2255                     $token->{tag_name} eq 'noframes') {
2256              ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2257              ## insertion mode IN_HEAD_IM)
2258              ## NOTE: There is a "as if in head" code clone.
2259              if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2260                !!!cp ('t114');
2261                !!!parse-error (type => 'after head',
2262                                text => $token->{tag_name}, token => $token);
2263                push @{$self->{open_elements}},
2264                    [$self->{head_element}, $el_category->{head}];
2265                $self->{head_element_inserted} = 1;
2266              } else {
2267                !!!cp ('t115');
2268              }
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) {
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';                  $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} eq 'in head noscript') {                } 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                  redo B;                  !!!nack ('t117.1');
2289                    !!!next-token;
2290                    next B;
2291                } else {                } else {
2292                    !!!cp ('t118');
2293                  #                  #
2294                }                }
2295              } elsif ($token->{tag_name} eq 'head' and          } elsif ($token->{tag_name} eq 'script') {
2296                       $self->{insertion_mode} ne 'after head') {            if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2297                !!!parse-error (type => 'in head:head'); # or in head noscript              !!!cp ('t119');
2298                ## Ignore the token              ## As if </noscript>
2299                !!!next-token;              pop @{$self->{open_elements}};
2300                redo B;              !!!parse-error (type => 'in noscript', text => 'script',
2301              } elsif ($self->{insertion_mode} ne 'in head noscript' and                              token => $token);
2302                       $token->{tag_name} eq 'script') {            
2303                if ($self->{insertion_mode} eq 'after head') {              $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.
2317              $script_start_tag->();
2318              ## ISSUE: A spec bug  [Bug 6038]
2319              splice @{$self->{open_elements}}, -2, 1 # <head>
2320                  if ($self->{insertion_mode} & IM_MASK) == AFTER_HEAD_IM;
2321              next B;
2322            } elsif ($token->{tag_name} eq 'body' or
2323                     $token->{tag_name} eq 'frameset') {
2324                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2325                    !!!cp ('t122');
2326                    ## As if </noscript>
2327                    pop @{$self->{open_elements}};
2328                    !!!parse-error (type => 'in noscript',
2329                                    text => $token->{tag_name}, token => $token);
2330                    
2331                    ## Reprocess in the "in head" insertion mode...
2332                    ## As if </head>
2333                    pop @{$self->{open_elements}};
2334                    
2335                    ## Reprocess in the "after head" insertion mode...
2336                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2337                    !!!cp ('t124');
2338                    pop @{$self->{open_elements}};
2339                    
2340                    ## Reprocess in the "after head" insertion mode...
2341                  } else {
2342                    !!!cp ('t125');
2343                }                }
2344                ## NOTE: There is a "as if in head" code clone.  
2345                $script_start_tag->($insert_to_current);                ## "after head" insertion mode
2346                pop @{$self->{open_elements}}                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2347                    if $self->{insertion_mode} eq 'after head';                if ($token->{tag_name} eq 'body') {
2348                redo B;                  !!!cp ('t126');
2349              } elsif ($self->{insertion_mode} eq 'after head' and                  $self->{insertion_mode} = IN_BODY_IM;
2350                       $token->{tag_name} eq 'body') {                } elsif ($token->{tag_name} eq 'frameset') {
2351                !!!insert-element ('body', $token->{attributes});                  !!!cp ('t127');
2352                $self->{insertion_mode} = 'in body';                  $self->{insertion_mode} = IN_FRAMESET_IM;
2353                !!!next-token;                } else {
2354                redo B;                  die "$0: tag name: $self->{tag_name}";
2355              } elsif ($self->{insertion_mode} eq 'after head' and                }
2356                       $token->{tag_name} eq 'frameset') {                !!!nack ('t127.1');
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
2357                !!!next-token;                !!!next-token;
2358                redo B;                next B;
2359              } else {              } else {
2360                  !!!cp ('t128');
2361                #                #
2362              }              }
2363            } elsif ($token->{type} eq 'end tag') {  
2364              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2365                  $token->{tag_name} eq 'head') {                !!!cp ('t129');
2366                  ## As if </noscript>
2367                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2368                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/',
2369                !!!next-token;                                text => $token->{tag_name}, token => $token);
2370                redo B;                
2371              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## Reprocess in the "in head" insertion mode...
2372                  $token->{tag_name} eq 'noscript') {                ## As if </head>
2373                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
               $self->{insertion_mode} = 'in head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head' and  
                      {  
                       body => 1, html => 1,  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                      {  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
2374    
2375            ## As if </head> or </noscript> or <body>                ## Reprocess in the "after head" insertion mode...
2376            if ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2377              pop @{$self->{open_elements}};                !!!cp ('t130');
2378              $self->{insertion_mode} = 'after head';                ## As if </head>
2379            } elsif ($self->{insertion_mode} eq 'in head noscript') {                pop @{$self->{open_elements}};
             pop @{$self->{open_elements}};  
             !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));  
             $self->{insertion_mode} = 'in head';  
           } else { # 'after head'  
             !!!insert-element ('body');  
             $self->{insertion_mode} = 'in body';  
           }  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
2380    
2381              !!!next-token;                ## Reprocess in the "after head" insertion mode...
2382              redo B;              } else {
2383            } elsif ($token->{type} eq 'comment') {                !!!cp ('t131');
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
2384              }              }
2385    
2386              !!!parse-error (type => 'in table:#character');              ## "after head" insertion mode
2387                ## As if <body>
2388                !!!insert-element ('body',, $token);
2389                $self->{insertion_mode} = IN_BODY_IM;
2390                ## reprocess
2391                !!!ack-later;
2392                next B;
2393              } elsif ($token->{type} == END_TAG_TOKEN) {
2394                if ($token->{tag_name} eq 'head') {
2395                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2396                    !!!cp ('t132');
2397                    ## As if <head>
2398                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2399                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2400                    push @{$self->{open_elements}},
2401                        [$self->{head_element}, $el_category->{head}];
2402    
2403              ## As if in body, but insert into foster parent element                  ## Reprocess in the "in head" insertion mode...
2404              ## ISSUE: Spec says that "whenever a node would be inserted                  pop @{$self->{open_elements}};
2405              ## into the current node" while characters might not be                  $self->{insertion_mode} = AFTER_HEAD_IM;
2406              ## result in a new Text node.                  !!!next-token;
2407              $reconstruct_active_formatting_elements->($insert_to_foster);                  next B;
2408                              } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2409              if ({                  !!!cp ('t133');
2410                   table => 1, tbody => 1, tfoot => 1,                  ## As if </noscript>
2411                   thead => 1, tr => 1,                  pop @{$self->{open_elements}};
2412                  }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'in noscript:/',
2413                # MUST                                  text => 'head', token => $token);
2414                my $foster_parent_element;                  
2415                my $next_sibling;                  ## Reprocess in the "in head" insertion mode...
2416                my $prev_sibling;                  pop @{$self->{open_elements}};
2417                OE: for (reverse 0..$#{$self->{open_elements}}) {                  $self->{insertion_mode} = AFTER_HEAD_IM;
2418                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  !!!next-token;
2419                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  next B;
2420                    if (defined $parent and $parent->node_type == 1) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2421                      $foster_parent_element = $parent;                  !!!cp ('t134');
2422                      $next_sibling = $self->{open_elements}->[$_]->[0];                  pop @{$self->{open_elements}};
2423                      $prev_sibling = $next_sibling->previous_sibling;                  $self->{insertion_mode} = AFTER_HEAD_IM;
2424                    } else {                  !!!next-token;
2425                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  next B;
2426                      $prev_sibling = $foster_parent_element->last_child;                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2427                    }                  !!!cp ('t134.1');
2428                    last OE;                  !!!parse-error (type => 'unmatched end tag', text => 'head',
2429                  }                                  token => $token);
2430                } # OE                  ## Ignore the token
2431                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  !!!next-token;
2432                $prev_sibling = $foster_parent_element->last_child                  next B;
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
2433                } else {                } else {
2434                  $foster_parent_element->insert_before                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
2435                }                }
2436              } else {              } elsif ($token->{tag_name} eq 'noscript') {
2437                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2438              }                  !!!cp ('t136');
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
2439                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2440                    $self->{insertion_mode} = IN_HEAD_IM;
2441                    !!!next-token;
2442                    next B;
2443                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
2444                           $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.
2449                    !!!next-token;
2450                    next B;
2451                  } else {
2452                    !!!cp ('t138');
2453                    #
2454                }                }
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
2455              } elsif ({              } elsif ({
2456                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
2457                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2458                ## Clear back to table context                ## TODO: This branch is entirely redundant.
2459                while ($self->{open_elements}->[-1]->[1] ne 'table' and                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
2460                       $self->{open_elements}->[-1]->[1] ne 'html') {                    $self->{insertion_mode} == IN_HEAD_IM or
2461                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2462                  pop @{$self->{open_elements}};                  !!!cp ('t140');
2463                    !!!parse-error (type => 'unmatched end tag',
2464                                    text => $token->{tag_name}, token => $token);
2465                    ## Ignore the token
2466                    !!!next-token;
2467                    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                  !!!parse-error (type => 'unmatched end tag',
2481                                  text => $token->{tag_name}, token => $token);
2482                  ## Ignore the token
2483                  !!!next-token;
2484                  next B;
2485                } elsif ($token->{tag_name} eq 'br') {
2486                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2487                    !!!cp ('t142.2');
2488                    ## (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});
2491                    $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;
2509    
2510                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                  ## Reprocess in the "in head" insertion mode...
2511                $self->{insertion_mode} = $token->{tag_name} eq 'col'                  ## As if </head>
2512                  ? 'in column group' : 'in table body';                  pop @{$self->{open_elements}};
2513                ## reprocess                  $self->{insertion_mode} = AFTER_HEAD_IM;
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
2514    
2515                ## As if </table>                  ## Reprocess in the "after head" insertion mode...
2516                ## have a table element in table scope                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2517                my $i;                  !!!cp ('t143.4');
2518                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  #
2519                  my $node = $self->{open_elements}->[$_];                } else {
2520                  if ($node->[1] eq 'table') {                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo 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; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
2521                }                }
2522    
2523                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
2524                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!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                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2539                  !!!cp ('t146');
2540                  ## As if </noscript>
2541                  pop @{$self->{open_elements}};
2542                  !!!parse-error (type => 'in noscript:/',
2543                                  text => $token->{tag_name}, token => $token);
2544                  
2545                  ## Reprocess in the "in head" insertion mode...
2546                  ## As if </head>
2547                  pop @{$self->{open_elements}};
2548    
2549                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
2550                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2551                  !!!cp ('t147');
2552                  ## As if </head>
2553                  pop @{$self->{open_elements}};
2554    
2555                ## reprocess                ## Reprocess in the "after head" insertion mode...
2556                redo B;              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2557    ## 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.
2562                  !!!next-token;
2563                  next B;
2564              } else {              } else {
2565                #                !!!cp ('t149');
2566              }              }
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
2567    
2568                if ($self->{open_elements}->[-1]->[1] ne 'table') {              ## "after head" insertion mode
2569                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              ## As if <body>
2570                }              !!!insert-element ('body',, $token);
2571                $self->{insertion_mode} = IN_BODY_IM;
2572                ## reprocess
2573                next B;
2574          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2575            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                splice @{$self->{open_elements}}, $i;            ## NOTE: As if </head>
2597              pop @{$self->{open_elements}};
2598              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
2599              ## NOTE: Reprocess.
2600    
2601                $self->_reset_insertion_mode;            #
2602            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2603              !!!cp ('t149.3');
2604    
2605                !!!next-token;            !!!parse-error (type => 'in noscript:#eof', token => $token);
2606                redo B;  
2607              } elsif ({            ## As if </noscript>
2608                        body => 1, caption => 1, col => 1, colgroup => 1,            pop @{$self->{open_elements}};
2609                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,            #$self->{insertion_mode} = IN_HEAD_IM;
2610                        thead => 1, tr => 1,            ## NOTE: Reprocess.
2611                       }->{$token->{tag_name}}) {  
2612                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            ## NOTE: As if </head>
2613                ## Ignore the token            pop @{$self->{open_elements}};
2614                !!!next-token;            #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
2615                redo B;            ## NOTE: Reprocess.
2616              } else {  
2617                #            #
2618              }          } else {
2619            } else {            !!!cp ('t149.4');
2620              #            #
2621            }          }
2622    
2623            !!!parse-error (type => 'in table:'.$token->{tag_name});          ## NOTE: As if <body>
2624            $in_body->($insert_to_foster);          !!!insert-element ('body',, $token);
2625            redo B;          $self->{insertion_mode} = IN_BODY_IM;
2626          } elsif ($self->{insertion_mode} eq 'in caption') {          ## NOTE: Reprocess.
2627            if ($token->{type} eq 'character') {          next B;
2628              ## NOTE: This is a code clone of "character in body".        } else {
2629            die "$0: $token->{type}: Unknown token type";
2630          }
2631        } elsif ($self->{insertion_mode} & BODY_IMS) {
2632              if ($token->{type} == CHARACTER_TOKEN) {
2633                !!!cp ('t150');
2634                ## 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} eq 'comment') {            } elsif ($token->{type} == START_TAG_TOKEN) {
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
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                !!!parse-error (type => 'not closed:caption');                if (($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2647                    ## have an element in table scope
2648                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
2649                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
2650                my $i;                    if ($node->[1] == TABLE_CELL_EL) {
2651                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
2652                  my $node = $self->{open_elements}->[$_];  
2653                  if ($node->[1] eq 'caption') {                      ## Close the cell
2654                    $i = $_;                      !!!back-token; # <x>
2655                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
2656                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
2657                            table => 1, html => 1,                                line => $token->{line},
2658                           }->{$node->[1]}) {                                column => $token->{column}};
2659                    last INSCOPE;                      next B;
2660                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
2661                        !!!cp ('t152');
2662                        ## ISSUE: This case can never be reached, maybe.
2663                        last;
2664                      }
2665                  }                  }
2666                } # INSCOPE  
2667                unless (defined $i) {                  !!!cp ('t153');
2668                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
2669                        text => $token->{tag_name}, token => $token);
2670                  ## Ignore the token                  ## Ignore the token
2671                    !!!nack ('t153.1');
2672                  !!!next-token;                  !!!next-token;
2673                  redo B;                  next B;
2674                }                } elsif (($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2675                                  !!!parse-error (type => 'not closed', text => 'caption',
2676                ## generate implied end tags                                  token => $token);
2677                if ({                  
2678                     dd => 1, dt => 1, li => 1, p => 1,                  ## NOTE: As if </caption>.
2679                     td => 1, th => 1, tr => 1,                  ## have a table element in table scope
2680                     tbody => 1, tfoot=> 1, thead => 1,                  my $i;
2681                    }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: {
2682                  !!!back-token; # <?>                    for (reverse 0..$#{$self->{open_elements}}) {
2683                  $token = {type => 'end tag', tag_name => 'caption'};                      my $node = $self->{open_elements}->[$_];
2684                  !!!back-token;                      if ($node->[1] == CAPTION_EL) {
2685                  $token = {type => 'end tag',                        !!!cp ('t155');
2686                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        $i = $_;
2687                  redo B;                        last INSCOPE;
2688                }                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
2689                          !!!cp ('t156');
2690                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        last;
2691                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      }
2692                }                    }
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
2693    
2694                $self->{insertion_mode} = 'in table';                    !!!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
2702                    
2703                    ## generate implied end tags
2704                    while ($self->{open_elements}->[-1]->[1]
2705                               & END_TAG_OPTIONAL_EL) {
2706                      !!!cp ('t158');
2707                      pop @{$self->{open_elements}};
2708                    }
2709    
2710                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] == CAPTION_EL) {
2711                redo B;                    !!!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;
2721                    
2722                    $clear_up_to_marker->();
2723                    
2724                    $self->{insertion_mode} = IN_TABLE_IM;
2725                    
2726                    ## reprocess
2727                    !!!ack-later;
2728                    next B;
2729                  } else {
2730                    !!!cp ('t161');
2731                    #
2732                  }
2733              } else {              } else {
2734                  !!!cp ('t162');
2735                #                #
2736              }              }
2737            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
2738              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
2739                ## have a table element in table scope                if (($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2740                my $i;                  ## have an element in table scope
2741                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
2742                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2743                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
2744                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
2745                    last INSCOPE;                      !!!cp ('t163');
2746                  } elsif ({                      $i = $_;
2747                            table => 1, html => 1,                      last INSCOPE;
2748                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
2749                    last INSCOPE;                      !!!cp ('t164');
2750                        last INSCOPE;
2751                      }
2752                    } # INSCOPE
2753                      unless (defined $i) {
2754                        !!!cp ('t165');
2755                        !!!parse-error (type => 'unmatched end tag',
2756                                        text => $token->{tag_name},
2757                                        token => $token);
2758                        ## Ignore the token
2759                        !!!next-token;
2760                        next B;
2761                      }
2762                    
2763                    ## generate implied end tags
2764                    while ($self->{open_elements}->[-1]->[1]
2765                               & END_TAG_OPTIONAL_EL) {
2766                      !!!cp ('t166');
2767                      pop @{$self->{open_elements}};
2768                  }                  }
2769                } # INSCOPE  
2770                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
2771                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          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;
2782                    
2783                    $clear_up_to_marker->();
2784                    
2785                    $self->{insertion_mode} = IN_ROW_IM;
2786                    
2787                    !!!next-token;
2788                    next B;
2789                  } elsif (($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2790                    !!!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 {
2797                                  !!!cp ('t170');
2798                ## generate implied end tags                  #
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
2799                }                }
2800                } elsif ($token->{tag_name} eq 'caption') {
2801                  if (($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2802                    ## have a table element in table scope
2803                    my $i;
2804                    INSCOPE: {
2805                      for (reverse 0..$#{$self->{open_elements}}) {
2806                        my $node = $self->{open_elements}->[$_];
2807                        if ($node->[1] == CAPTION_EL) {
2808                          !!!cp ('t171');
2809                          $i = $_;
2810                          last INSCOPE;
2811                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
2812                          !!!cp ('t172');
2813                          last;
2814                        }
2815                      }
2816    
2817                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
2818                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!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
2824                    
2825                    ## generate implied end tags
2826                    while ($self->{open_elements}->[-1]->[1]
2827                               & END_TAG_OPTIONAL_EL) {
2828                      !!!cp ('t174');
2829                      pop @{$self->{open_elements}};
2830                    }
2831                    
2832                    unless ($self->{open_elements}->[-1]->[1] == CAPTION_EL) {
2833                      !!!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;
2843                    
2844                    $clear_up_to_marker->();
2845                    
2846                    $self->{insertion_mode} = IN_TABLE_IM;
2847                    
2848                    !!!next-token;
2849                    next B;
2850                  } elsif (($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2851                    !!!cp ('t177');
2852                    !!!parse-error (type => 'unmatched end tag',
2853                                    text => $token->{tag_name}, token => $token);
2854                    ## Ignore the token
2855                    !!!next-token;
2856                    next B;
2857                  } else {
2858                    !!!cp ('t178');
2859                    #
2860                }                }
2861                } elsif ({
2862                          table => 1, tbody => 1, tfoot => 1,
2863                          thead => 1, tr => 1,
2864                         }->{$token->{tag_name}} and
2865                         ($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2866                  ## have an element in table scope
2867                  my $i;
2868                  my $tn;
2869                  INSCOPE: {
2870                    for (reverse 0..$#{$self->{open_elements}}) {
2871                      my $node = $self->{open_elements}->[$_];
2872                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
2873                        !!!cp ('t179');
2874                        $i = $_;
2875    
2876                        ## Close the cell
2877                        !!!back-token; # </x>
2878                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
2879                                  line => $token->{line},
2880                                  column => $token->{column}};
2881                        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    
2894                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
2895                    !!!parse-error (type => 'unmatched end tag',
2896                $clear_up_to_marker->();                      text => $token->{tag_name}, token => $token);
2897                    ## Ignore the token
2898                $self->{insertion_mode} = 'in table';                  !!!next-token;
2899                    next B;
2900                !!!next-token;                } # INSCOPE
2901                redo B;              } elsif ($token->{tag_name} eq 'table' and
2902              } elsif ($token->{tag_name} eq 'table') {                       ($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', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           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;
2947    
2948                $clear_up_to_marker->();                $clear_up_to_marker->();
2949    
2950                $self->{insertion_mode} = 'in table';                $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,                        body => 1, col => 1, colgroup => 1, html => 1,
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
2956                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2957                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
2958                ## Ignore the token                  !!!cp ('t190');
2959                redo B;                  !!!parse-error (type => 'unmatched end tag',
2960              } else {                                  text => $token->{tag_name}, token => $token);
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
2961                  ## Ignore the token                  ## Ignore the token
2962                  !!!next-token;                  !!!next-token;
2963                  redo B;                  next B;
2964                } else {                } else {
2965                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
2966                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
2967                }                }
2968              } elsif ($token->{tag_name} eq 'col') {          } elsif ({
2969                !!!parse-error (type => 'unmatched end tag:col');                    tbody => 1, tfoot => 1,
2970                ## Ignore the token                    thead => 1, tr => 1,
2971                !!!next-token;                   }->{$token->{tag_name}} and
2972                redo B;                   ($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2973              } else {            !!!cp ('t192');
2974                #            !!!parse-error (type => 'unmatched end tag',
2975              }                            text => $token->{tag_name}, token => $token);
2976            } else {            ## Ignore the token
2977              #            !!!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            ## As if </colgroup>          ## Stop parsing.
2993            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
2994              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
2995              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
2996          }
2997    
2998          $insert = $insert_to_current;
2999          #
3000        } elsif ($self->{insertion_mode} & TABLE_IMS) {
3001          if ($token->{type} == CHARACTER_TOKEN) {
3002            if (not $open_tables->[-1]->[1] and # tainted
3003                $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3004              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3005                  
3006              unless (length $token->{data}) {
3007                !!!cp ('t194');
3008              !!!next-token;              !!!next-token;
3009              redo B;              next B;
3010            } else {            } else {
3011              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
3012            }            }
3013          } elsif ($self->{insertion_mode} eq 'in table body') {          }
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
3014    
3015              ## As if in body, but insert into foster parent element          !!!parse-error (type => 'in table:#text', token => $token);
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
3016    
3017              if ({          ## NOTE: As if in body, but insert into the foster parent element.
3018                   table => 1, tbody => 1, tfoot => 1,          $reconstruct_active_formatting_elements->($insert_to_foster);
3019                   thead => 1, tr => 1,              
3020                  }->{$self->{open_elements}->[-1]->[1]}) {          if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3021                # MUST            # MUST
3022                my $foster_parent_element;            my $foster_parent_element;
3023                my $next_sibling;            my $next_sibling;
3024                my $prev_sibling;            my $prev_sibling;
3025                OE: for (reverse 0..$#{$self->{open_elements}}) {            OE: for (reverse 0..$#{$self->{open_elements}}) {
3026                  if ($self->{open_elements}->[$_]->[1] eq 'table') {              if ($self->{open_elements}->[$_]->[1] == TABLE_EL) {
3027                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3028                    if (defined $parent and $parent->node_type == 1) {                if (defined $parent and $parent->node_type == 1) {
3029                      $foster_parent_element = $parent;                  $foster_parent_element = $parent;
3030                      $next_sibling = $self->{open_elements}->[$_]->[0];                  !!!cp ('t196');
3031                      $prev_sibling = $next_sibling->previous_sibling;                  $next_sibling = $self->{open_elements}->[$_]->[0];
3032                    } else {                  $prev_sibling = $next_sibling->previous_sibling;
3033                      $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} eq 'comment') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3074              ## Copied from 'in table'          if ({
3075              my $comment = $self->{document}->create_comment ($token->{data});               tr => (($self->{insertion_mode} & IM_MASK) != IN_ROW_IM),
3076              $self->{open_elements}->[-1]->[0]->append_child ($comment);               th => 1, td => 1,
3077              !!!next-token;              }->{$token->{tag_name}}) {
3078              redo B;            if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_IM) {
3079            } elsif ($token->{type} eq 'start tag') {              ## Clear back to table context
3080              if ({              while (not ($self->{open_elements}->[-1]->[1]
3081                   tr => 1,                              & TABLE_SCOPING_EL)) {
3082                   th => 1, td => 1,                !!!cp ('t201');
3083                  }->{$token->{tag_name}}) {                pop @{$self->{open_elements}};
3084                unless ($token->{tag_name} eq 'tr') {              }
3085                  !!!parse-error (type => 'missing start tag:tr');              
3086                }              !!!insert-element ('tbody',, $token);
3087                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3088                ## reprocess in the "in table body" insertion mode...
3089              }
3090              
3091              if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_BODY_IM) {
3092                unless ($token->{tag_name} eq 'tr') {
3093                  !!!cp ('t202');
3094                  !!!parse-error (type => 'missing start tag:tr', token => $token);
3095                }
3096                    
3097                ## Clear back to table body context
3098                while (not ($self->{open_elements}->[-1]->[1]
3099                                & TABLE_ROWS_SCOPING_EL)) {
3100                  !!!cp ('t203');
3101                  ## ISSUE: Can this case be reached?
3102                  pop @{$self->{open_elements}};
3103                }
3104                    
3105                $self->{insertion_mode} = IN_ROW_IM;
3106                if ($token->{tag_name} eq 'tr') {
3107                  !!!cp ('t204');
3108                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3109                  $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3110                  !!!nack ('t204');
3111                  !!!next-token;
3112                  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 body context                ## Clear back to table row context
3123                while (not {                while (not ($self->{open_elements}->[-1]->[1]
3124                  tbody => 1, tfoot => 1, thead => 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                $self->{insertion_mode} = 'in row';            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3130                if ($token->{tag_name} eq 'tr') {            $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3131                  !!!insert-element ($token->{tag_name}, $token->{attributes});            $self->{insertion_mode} = IN_CELL_IM;
3132                  !!!next-token;  
3133                } else {            push @$active_formatting_elements, ['#marker', ''];
3134                  !!!insert-element ('tr');                
3135                  ## reprocess            !!!nack ('t207.1');
3136                }            !!!next-token;
3137                redo B;            next B;
3138              } elsif ({          } elsif ({
3139                        caption => 1, col => 1, colgroup => 1,                    caption => 1, col => 1, colgroup => 1,
3140                        tbody => 1, tfoot => 1, thead => 1,                    tbody => 1, tfoot => 1, thead => 1,
3141                       }->{$token->{tag_name}}) {                    tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3142                ## have an element in table scope                   }->{$token->{tag_name}}) {
3143                my $i;            if (($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3144                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              ## As if </tr>
3145                  my $node = $self->{open_elements}->[$_];              ## have an element in table scope
3146                  if ({              my $i;
3147                       tbody => 1, thead => 1, tfoot => 1,              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3148                      }->{$node->[1]}) {                my $node = $self->{open_elements}->[$_];
3149                    $i = $_;                if ($node->[1] == TABLE_ROW_EL) {
3150                    last INSCOPE;                  !!!cp ('t208');
3151                  } elsif ({                  $i = $_;
3152                            table => 1, html => 1,                  last INSCOPE;
3153                           }->{$node->[1]}) {                } elsif ($node->[1] & TABLE_SCOPING_EL) {
3154                    last INSCOPE;                  !!!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;
3166                  next B;
3167                }
3168                    
3169                    ## Clear back to table row context
3170                    while (not ($self->{open_elements}->[-1]->[1]
3171                                    & TABLE_ROW_SCOPING_EL)) {
3172                      !!!cp ('t211');
3173                      ## ISSUE: Can this case be reached?
3174                      pop @{$self->{open_elements}};
3175                    }
3176                    
3177                    pop @{$self->{open_elements}}; # tr
3178                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3179                    if ($token->{tag_name} eq 'tr') {
3180                      !!!cp ('t212');
3181                      ## reprocess
3182                      !!!ack-later;
3183                      next B;
3184                    } else {
3185                      !!!cp ('t213');
3186                      ## reprocess in the "in table body" insertion mode...
3187                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
3188                }                }
3189    
3190                ## Clear back to table body context                if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_BODY_IM) {
3191                while (not {                  ## have an element in table scope
3192                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3193                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3194                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
3195                      if ($node->[1] == TABLE_ROW_GROUP_EL) {
3196                        !!!cp ('t214');
3197                        $i = $_;
3198                        last INSCOPE;
3199                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
3200                        !!!cp ('t215');
3201                        last INSCOPE;
3202                      }
3203                    } # INSCOPE
3204                    unless (defined $i) {
3205                      !!!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
3210                      !!!nack ('t216.1');
3211                      !!!next-token;
3212                      next B;
3213                    }
3214    
3215                    ## Clear back to table body context
3216                    while (not ($self->{open_elements}->[-1]->[1]
3217                                    & TABLE_ROWS_SCOPING_EL)) {
3218                      !!!cp ('t217');
3219                      ## ISSUE: Can this state be reached?
3220                      pop @{$self->{open_elements}};
3221                    }
3222                    
3223                    ## As if <{current node}>
3224                    ## have an element in table scope
3225                    ## true by definition
3226                    
3227                    ## Clear back to table body context
3228                    ## nop by definition
3229                    
3230                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3231                    $self->{insertion_mode} = IN_TABLE_IM;
3232                    ## reprocess in "in table" insertion mode...
3233                  } else {
3234                    !!!cp ('t218');
3235                }                }
3236    
3237                ## As if <{current node}>            if ($token->{tag_name} eq 'col') {
3238                ## have an element in table scope              ## Clear back to table context
3239                ## true by definition              while (not ($self->{open_elements}->[-1]->[1]
3240                                & TABLE_SCOPING_EL)) {
3241                ## Clear back to table body context                !!!cp ('t219');
3242                ## nop by definition                ## ISSUE: Can this state be reached?
   
3243                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3244                $self->{insertion_mode} = 'in table';              }
3245                ## reprocess              
3246                redo B;              !!!insert-element ('colgroup',, $token);
3247                $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3248                ## reprocess
3249                $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3250                !!!ack-later;
3251                next B;
3252              } elsif ({
3253                        caption => 1,
3254                        colgroup => 1,
3255                        tbody => 1, tfoot => 1, thead => 1,
3256                       }->{$token->{tag_name}}) {
3257                ## 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}};
3263                    }
3264                    
3265                push @$active_formatting_elements, ['#marker', '']
3266                    if $token->{tag_name} eq 'caption';
3267                    
3268                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3269                $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3270                $self->{insertion_mode} = {
3271                                           caption => IN_CAPTION_IM,
3272                                           colgroup => IN_COLUMN_GROUP_IM,
3273                                           tbody => IN_TABLE_BODY_IM,
3274                                           tfoot => IN_TABLE_BODY_IM,
3275                                           thead => IN_TABLE_BODY_IM,
3276                                          }->{$token->{tag_name}};
3277                !!!next-token;
3278                !!!nack ('t220.1');
3279                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                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
3285                !!!parse-error (type => 'not closed:table');                                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', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           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                #          } elsif ($token->{tag_name} eq 'style') {
3341              }            if (not $open_tables->[-1]->[1]) { # tainted
3342            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
3343              if ({              ## NOTE: This is a "as if in head" code clone.
3344                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
3345                  }->{$token->{tag_name}}) {              $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3346                ## have an element in table scope              next B;
3347                my $i;            } else {
3348                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              !!!cp ('t227.7');
3349                  my $node = $self->{open_elements}->[$_];              #
3350                  if ($node->[1] eq $token->{tag_name}) {            }
3351                    $i = $_;          } elsif ($token->{tag_name} eq 'script') {
3352                    last INSCOPE;            if (not $open_tables->[-1]->[1]) { # tainted
3353                  } elsif ({              !!!cp ('t227.6');
3354                            table => 1, html => 1,              ## NOTE: This is a "as if in head" code clone.
3355                           }->{$node->[1]}) {              $script_start_tag->();
3356                    last INSCOPE;              $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3357                  }              next B;
3358                } # INSCOPE            } else {
3359                unless (defined $i) {              !!!cp ('t227.5');
3360                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              #
3361                  ## Ignore the token            }
3362                  !!!next-token;          } elsif ($token->{tag_name} eq 'input') {
3363                  redo B;            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                ## Clear back to table body context                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3372                while (not {                  $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
3373    
3374                pop @{$self->{open_elements}};                  ## TODO: form element pointer
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
3375    
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3376                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
3377    
3378                ## Clear back to table body context                  !!!next-token;
3379                ## nop by definition                  !!!ack ('t227.2.1');
3380                    next B;
3381                pop @{$self->{open_elements}};                } else {
3382                $self->{insertion_mode} = 'in table';                  !!!cp ('t227.2');
3383                ## reprocess                  #
3384                redo B;                }
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
3385              } else {              } else {
3386                  !!!cp ('t227.1');
3387                #                #
3388              }              }
3389            } else {            } else {
3390                !!!cp ('t227.4');
3391              #              #
3392            }            }
3393                      } else {
3394            ## As if in table            !!!cp ('t227');
3395            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
3396            $in_body->($insert_to_foster);          }
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
3397    
3398              ## As if in body, but insert into foster parent element          !!!parse-error (type => 'in table', text => $token->{tag_name},
3399              ## ISSUE: Spec says that "whenever a node would be inserted                          token => $token);
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
3400    
3401                push @$active_formatting_elements, ['#marker', ''];          $insert = $insert_to_foster;
3402                          #
3403                !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
3404                redo B;          if ($token->{tag_name} eq 'tr' and
3405              } elsif ({              ($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3406                        caption => 1, col => 1, colgroup => 1,            ## have an element in table scope
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## 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 'tr') {                  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 => 'unmacthed 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';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3441                ## reprocess                !!!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                ## NOTE: This is a code clone of "table in table"                if (($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3446                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
3447                    ## have an element in table scope
3448                ## As if </table>                  my $i;
3449                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3450                my $i;                    my $node = $self->{open_elements}->[$_];
3451                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] == TABLE_ROW_EL) {
3452                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
3453                  if ($node->[1] eq 'table') {                      $i = $_;
3454                    $i = $_;                      last INSCOPE;
3455                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
3456                  } elsif ({                      !!!cp ('t234');
3457                            table => 1, html => 1,                      last INSCOPE;
3458                           }->{$node->[1]}) {                    }
3459                    last INSCOPE;                  } # INSCOPE
3460                    unless (defined $i) {
3461                      !!!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
3466                      !!!nack ('t236.1');
3467                      !!!next-token;
3468                      next B;
3469                  }                  }
3470                } # INSCOPE                  
3471                unless (defined $i) {                  ## Clear back to table row context
3472                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
3473                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
3474                  !!!next-token;                    !!!cp ('t236');
3475                  redo B;  ## ISSUE: Can this state be reached?
3476                }                    pop @{$self->{open_elements}};
3477                                  }
3478                ## generate implied end tags                  
3479                if ({                  pop @{$self->{open_elements}}; # tr
3480                     dd => 1, dt => 1, li => 1, p => 1,                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3481                     td => 1, th => 1, tr => 1,                  ## reprocess in the "in table body" insertion mode...
3482                     tbody => 1, tfoot=> 1, thead => 1,                }
3483                    }->{$self->{open_elements}->[-1]->[1]}) {  
3484                  !!!back-token; # <table>                if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_BODY_IM) {
3485                  $token = {type => 'end tag', tag_name => 'table'};                  ## have an element in table scope
3486                  !!!back-token;                  my $i;
3487                  $token = {type => 'end tag',                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3488                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                    my $node = $self->{open_elements}->[$_];
3489                  redo B;                    if ($node->[1] == TABLE_ROW_GROUP_EL) {
3490                }                      !!!cp ('t237');
3491                        $i = $_;
3492                if ($self->{open_elements}->[-1]->[1] ne 'table') {                      last INSCOPE;
3493                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
3494                        !!!cp ('t238');
3495                        last INSCOPE;
3496                      }
3497                    } # INSCOPE
3498                    unless (defined $i) {
3499                      !!!cp ('t239');
3500                      !!!parse-error (type => 'unmatched end tag',
3501                                      text => $token->{tag_name}, token => $token);
3502                      ## Ignore the token
3503                      !!!nack ('t239.1');
3504                      !!!next-token;
3505                      next B;
3506                    }
3507                    
3508                    ## Clear back to table body context
3509                    while (not ($self->{open_elements}->[-1]->[1]
3510                                    & TABLE_ROWS_SCOPING_EL)) {
3511                      !!!cp ('t240');
3512                      pop @{$self->{open_elements}};
3513                    }
3514                    
3515                    ## As if <{current node}>
3516                    ## have an element in table scope
3517                    ## true by definition
3518                    
3519                    ## Clear back to table body context
3520                    ## nop by definition
3521                    
3522                    pop @{$self->{open_elements}};
3523                    $self->{insertion_mode} = IN_TABLE_IM;
3524                    ## reprocess in the "in table" insertion mode...
3525                }                }
3526    
3527                splice @{$self->{open_elements}}, $i;                ## NOTE: </table> in the "in table" insertion mode.
3528                  ## When you edit the code fragment below, please ensure that
3529                $self->_reset_insertion_mode;                ## the code for <table> in the "in table" insertion mode
3530                  ## is synced with it.
3531    
3532                ## reprocess                ## have a table element in table scope
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an 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;
3553                }                }
3554                    
3555                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
3556                while (not {                pop @{$open_tables};
3557                  tr => 1, html => 1,                
3558                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
3559                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
3560                  pop @{$self->{open_elements}};                !!!next-token;
3561                  next B;
3562                } elsif ({
3563                          tbody => 1, tfoot => 1, thead => 1,
3564                         }->{$token->{tag_name}} and
3565                         $self->{insertion_mode} & ROW_IMS) {
3566                  if (($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3567                    ## have an element in table scope
3568                    my $i;
3569                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3570                      my $node = $self->{open_elements}->[$_];
3571                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
3572                        !!!cp ('t247');
3573                        $i = $_;
3574                        last INSCOPE;
3575                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
3576                        !!!cp ('t248');
3577                        last INSCOPE;
3578                      }
3579                    } # INSCOPE
3580                      unless (defined $i) {
3581                        !!!cp ('t249');
3582                        !!!parse-error (type => 'unmatched end tag',
3583                                        text => $token->{tag_name}, token => $token);
3584                        ## Ignore the token
3585                        !!!nack ('t249.1');
3586                        !!!next-token;
3587                        next B;
3588                      }
3589                    
3590                    ## As if </tr>
3591                    ## have an element in table scope
3592                    my $i;
3593                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3594                      my $node = $self->{open_elements}->[$_];
3595                      if ($node->[1] == TABLE_ROW_EL) {
3596                        !!!cp ('t250');
3597                        $i = $_;
3598                        last INSCOPE;
3599                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
3600                        !!!cp ('t251');
3601                        last INSCOPE;
3602                      }
3603                    } # INSCOPE
3604                      unless (defined $i) {
3605                        !!!cp ('t252');
3606                        !!!parse-error (type => 'unmatched end tag',
3607                                        text => 'tr', token => $token);
3608                        ## Ignore the token
3609                        !!!nack ('t252.1');
3610                        !!!next-token;
3611                        next B;
3612                      }
3613                    
3614                    ## Clear back to table row context
3615                    while (not ($self->{open_elements}->[-1]->[1]
3616                                    & TABLE_ROW_SCOPING_EL)) {
3617                      !!!cp ('t253');
3618    ## ISSUE: Can this case be reached?
3619                      pop @{$self->{open_elements}};
3620                    }
3621                    
3622                    pop @{$self->{open_elements}}; # tr
3623                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3624                    ## reprocess in the "in table body" insertion mode...
3625                }                }
3626    
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
3627                ## have an element in table scope                ## have an element in table scope
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 'tr') {                  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->{type});                  !!!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 row context                ## Clear back to table body context
3651                while (not {                while (not ($self->{open_elements}->[-1]->[1]
3652                  tr => 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}}; # tr                pop @{$self->{open_elements}};
3659                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
3660                ## reprocess                !!!nack ('t257.1');
3661                redo B;                !!!next-token;
3662                  next B;
3663              } elsif ({              } elsif ({
3664                        tbody => 1, tfoot => 1, thead => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3665                          html => 1, td => 1, th => 1,
3666                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3667                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3668                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3669                ## have an element in table scope            !!!cp ('t258');
3670                my $i;            !!!parse-error (type => 'unmatched end tag',
3671                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                            text => $token->{tag_name}, token => $token);
3672                  my $node = $self->{open_elements}->[$_];            ## Ignore the token
3673                  if ($node->[1] eq $token->{tag_name}) {            !!!nack ('t258.1');
3674                    $i = $_;             !!!next-token;
3675                    last INSCOPE;            next B;
3676                  } elsif ({          } else {
3677                            table => 1, html => 1,            !!!cp ('t259');
3678                           }->{$node->[1]}) {            !!!parse-error (type => 'in table:/',
3679                    last INSCOPE;                            text => $token->{tag_name}, token => $token);
3680                  }  
3681                } # INSCOPE            $insert = $insert_to_foster;
3682                unless (defined $i) {            #
3683                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          }
3684                  ## Ignore the token        } 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 {
3698            die "$0: $token->{type}: Unknown token type";
3699          }
3700        } elsif (($self->{insertion_mode} & IM_MASK) == IN_COLUMN_GROUP_IM) {
3701              if ($token->{type} == CHARACTER_TOKEN) {
3702                if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3703                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3704                  unless (length $token->{data}) {
3705                    !!!cp ('t260');
3706                  !!!next-token;                  !!!next-token;
3707                  redo B;                  next B;
3708                }                }
3709                }
3710                ## As if </tr>              
3711                ## have an element in table scope              !!!cp ('t261');
3712                my $i;              #
3713                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            } elsif ($token->{type} == START_TAG_TOKEN) {
3714                  my $node = $self->{open_elements}->[$_];              if ($token->{tag_name} eq 'col') {
3715                  if ($node->[1] eq 'tr') {                !!!cp ('t262');
3716                    $i = $_;                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3717                    last INSCOPE;                pop @{$self->{open_elements}};
3718                  } elsif ({                !!!ack ('t262.1');
3719                            table => 1, html => 1,                !!!next-token;
3720                           }->{$node->[1]}) {                next B;
3721                    last INSCOPE;              } else {
3722                  }                !!!cp ('t263');
3723                } # INSCOPE                #
3724                unless (defined $i) {              }
3725                  !!!parse-error (type => 'unmatched end tag:tr');            } elsif ($token->{type} == END_TAG_TOKEN) {
3726                if ($token->{tag_name} eq 'colgroup') {
3727                  if ($self->{open_elements}->[-1]->[1] == HTML_EL) {
3728                    !!!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 {
3735                    !!!cp ('t265');
3736                ## Clear back to table row context                  pop @{$self->{open_elements}}; # colgroup
3737                while (not {                  $self->{insertion_mode} = IN_TABLE_IM;
3738                  tr => 1, html => 1,                  !!!next-token;
3739                }->{$self->{open_elements}->[-1]->[1]}) {                  next B;            
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
3740                }                }
3741                } elsif ($token->{tag_name} eq 'col') {
3742                pop @{$self->{open_elements}}; # tr                !!!cp ('t266');
3743                $self->{insertion_mode} = 'in table body';                !!!parse-error (type => 'unmatched end tag',
3744                ## reprocess                                text => 'col', token => $token);
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
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          } 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>
3771              if ($self->{open_elements}->[-1]->[1] == HTML_EL) {
3772                !!!cp ('t269');
3773    ## TODO: Wrong error type?
3774                !!!parse-error (type => 'unmatched end tag',
3775                                text => 'colgroup', token => $token);
3776                ## Ignore the token
3777                !!!nack ('t269.1');
3778                !!!next-token;
3779                next B;
3780            } else {            } else {
3781              #              !!!cp ('t270');
3782                pop @{$self->{open_elements}}; # colgroup
3783                $self->{insertion_mode} = IN_TABLE_IM;
3784                !!!ack-later;
3785                ## reprocess
3786                next B;
3787              }
3788        } elsif ($self->{insertion_mode} & SELECT_IMS) {
3789          if ($token->{type} == CHARACTER_TOKEN) {
3790            !!!cp ('t271');
3791            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3792            !!!next-token;
3793            next B;
3794          } elsif ($token->{type} == START_TAG_TOKEN) {
3795            if ($token->{tag_name} eq 'option') {
3796              if ($self->{open_elements}->[-1]->[1] == OPTION_EL) {
3797                !!!cp ('t272');
3798                ## As if </option>
3799                pop @{$self->{open_elements}};
3800              } else {
3801                !!!cp ('t273');
3802            }            }
3803    
3804            ## As if in table            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3805            !!!parse-error (type => 'in table:'.$token->{tag_name});            !!!nack ('t273.1');
3806            $in_body->($insert_to_foster);            !!!next-token;
3807            redo B;            next B;
3808          } elsif ($self->{insertion_mode} eq 'in cell') {          } elsif ($token->{tag_name} eq 'optgroup') {
3809            if ($token->{type} eq 'character') {            if ($self->{open_elements}->[-1]->[1] == OPTION_EL) {
3810              ## NOTE: This is a code clone of "character in body".              !!!cp ('t274');
3811              $reconstruct_active_formatting_elements->($insert_to_current);              ## As if </option>
3812                            pop @{$self->{open_elements}};
3813              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            } else {
3814                !!!cp ('t275');
3815              }
3816    
3817              if ($self->{open_elements}->[-1]->[1] == OPTGROUP_EL) {
3818                !!!cp ('t276');
3819                ## As if </optgroup>
3820                pop @{$self->{open_elements}};
3821              } else {
3822                !!!cp ('t277');
3823              }
3824    
3825              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3826              !!!nack ('t277.1');
3827              !!!next-token;
3828              next B;
3829            } elsif ({
3830                       select => 1, input => 1, textarea => 1,
3831                     }->{$token->{tag_name}} or
3832                     (($self->{insertion_mode} & IM_MASK)
3833                          == IN_SELECT_IN_TABLE_IM and
3834                      {
3835                       caption => 1, table => 1,
3836                       tbody => 1, tfoot => 1, thead => 1,
3837                       tr => 1, td => 1, th => 1,
3838                      }->{$token->{tag_name}})) {
3839              ## TODO: The type below is not good - <select> is replaced by </select>
3840              !!!parse-error (type => 'not closed', text => 'select',
3841                              token => $token);
3842              ## NOTE: As if the token were </select> (<select> case) or
3843              ## as if there were </select> (otherwise).
3844              ## have an element in table scope
3845              my $i;
3846              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3847                my $node = $self->{open_elements}->[$_];
3848                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;              !!!next-token;
3864              redo B;              next B;
3865            } elsif ($token->{type} eq 'comment') {            }
3866              ## NOTE: This is a code clone of "comment in body".                
3867              my $comment = $self->{document}->create_comment ($token->{data});            !!!cp ('t281');
3868              $self->{open_elements}->[-1]->[0]->append_child ($comment);            splice @{$self->{open_elements}}, $i;
3869    
3870              $self->_reset_insertion_mode;
3871    
3872              if ($token->{tag_name} eq 'select') {
3873                !!!nack ('t281.2');
3874              !!!next-token;              !!!next-token;
3875              redo B;              next B;
3876            } elsif ($token->{type} eq 'start tag') {            } else {
3877              if ({              !!!cp ('t281.1');
3878                   caption => 1, col => 1, colgroup => 1,              !!!ack-later;
3879                   tbody => 1, td => 1, tfoot => 1, th => 1,              ## Reprocess the token.
3880                   thead => 1, tr => 1,              next B;
3881                  }->{$token->{tag_name}}) {            }
3882                ## have an element in table scope          } else {
3883                my $tn;            !!!cp ('t282');
3884                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            !!!parse-error (type => 'in select',
3885                  my $node = $self->{open_elements}->[$_];                            text => $token->{tag_name}, token => $token);
3886                  if ($node->[1] eq 'td' or $node->[1] eq 'th') {            ## Ignore the token
3887                    $tn = $node->[1];            !!!nack ('t282.1');
3888                    last INSCOPE;            !!!next-token;
3889                  } elsif ({            next B;
3890                            table => 1, html => 1,          }
3891                           }->{$node->[1]}) {        } elsif ($token->{type} == END_TAG_TOKEN) {
3892                    last INSCOPE;          if ($token->{tag_name} eq 'optgroup') {
3893                  }            if ($self->{open_elements}->[-1]->[1] == OPTION_EL and
3894                } # INSCOPE                $self->{open_elements}->[-2]->[1] == OPTGROUP_EL) {
3895                unless (defined $tn) {              !!!cp ('t283');
3896                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              ## As if </option>
3897                  ## Ignore the token              splice @{$self->{open_elements}}, -2;
3898                  !!!next-token;            } elsif ($self->{open_elements}->[-1]->[1] == OPTGROUP_EL) {
3899                  redo B;              !!!cp ('t284');
3900                }              pop @{$self->{open_elements}};
3901              } else {
3902                !!!cp ('t285');
3903                !!!parse-error (type => 'unmatched end tag',
3904                                text => $token->{tag_name}, token => $token);
3905                ## Ignore the token
3906              }
3907              !!!nack ('t285.1');
3908              !!!next-token;
3909              next B;
3910            } elsif ($token->{tag_name} eq 'option') {
3911              if ($self->{open_elements}->[-1]->[1] == OPTION_EL) {
3912                !!!cp ('t286');
3913                pop @{$self->{open_elements}};
3914              } else {
3915                !!!cp ('t287');
3916                !!!parse-error (type => 'unmatched end tag',
3917                                text => $token->{tag_name}, token => $token);
3918                ## Ignore the token
3919              }
3920              !!!nack ('t287.1');
3921              !!!next-token;
3922              next B;
3923            } elsif ($token->{tag_name} eq 'select') {
3924              ## have an element in table scope
3925              my $i;
3926              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3927                my $node = $self->{open_elements}->[$_];
3928                if ($node->[1] == SELECT_EL) {
3929                  !!!cp ('t288');
3930                  $i = $_;
3931                  last INSCOPE;
3932                } 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              !!!cp ('t291');
3948              splice @{$self->{open_elements}}, $i;
3949    
3950                ## Close the cell            $self->_reset_insertion_mode;
3951                !!!back-token; # <?>  
3952                $token = {type => 'end tag', tag_name => $tn};            !!!nack ('t291.1');
3953                redo B;            !!!next-token;
3954              } else {            next B;
3955                #          } elsif (($self->{insertion_mode} & IM_MASK)
3956                         == IN_SELECT_IN_TABLE_IM and
3957                     {
3958                      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
3966              my $i;
3967              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3968                my $node = $self->{open_elements}->[$_];
3969                if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
3970                  !!!cp ('t292');
3971                  $i = $_;
3972                  last INSCOPE;
3973                } elsif ($node->[1] & TABLE_SCOPING_EL) {
3974                  !!!cp ('t293');
3975                  last INSCOPE;
3976              }              }
3977            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
3978              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {            unless (defined $i) {
3979                ## have an element in table scope              !!!cp ('t294');
3980                my $i;              ## Ignore the token
3981                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              !!!nack ('t294.1');
3982                  my $node = $self->{open_elements}->[$_];              !!!next-token;
3983                  if ($node->[1] eq $token->{tag_name}) {              next B;
3984                    $i = $_;            }
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
3985                                
3986                ## generate implied end tags            ## As if </select>
3987                if ({            ## have an element in table scope
3988                     dd => 1, dt => 1, li => 1, p => 1,            undef $i;
3989                     td => ($token->{tag_name} eq 'th'),            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3990                     th => ($token->{tag_name} eq 'td'),              my $node = $self->{open_elements}->[$_];
3991                     tr => 1,              if ($node->[1] == SELECT_EL) {
3992                     tbody => 1, tfoot=> 1, thead => 1,                !!!cp ('t295');
3993                    }->{$self->{open_elements}->[-1]->[1]}) {                $i = $_;
3994                  !!!back-token;                last INSCOPE;
3995                  $token = {type => 'end tag',              } elsif ($node->[1] & TABLE_SCOPING_EL) {
3996                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  ## ISSUE: Can this state be reached?
3997                  redo B;                !!!cp ('t296');
3998                }                last INSCOPE;
3999                }
4000              } # INSCOPE
4001              unless (defined $i) {
4002                !!!cp ('t297');
4003    ## TODO: The following error type is correct?
4004                !!!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              !!!cp ('t298');
4013              splice @{$self->{open_elements}}, $i;
4014    
4015                if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            $self->_reset_insertion_mode;
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
4016    
4017                splice @{$self->{open_elements}}, $i;            !!!ack-later;
4018              ## reprocess
4019              next B;
4020            } else {
4021              !!!cp ('t299');
4022              !!!parse-error (type => 'in select:/',
4023                              text => $token->{tag_name}, token => $token);
4024              ## Ignore the token
4025              !!!nack ('t299.3');
4026              !!!next-token;
4027              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                $clear_up_to_marker->();          ## Stop parsing.
4039            last B;
4040          } else {
4041            die "$0: $token->{type}: Unknown token type";
4042          }
4043        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4044          if ($token->{type} == CHARACTER_TOKEN) {
4045            if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
4046              my $data = $1;
4047              ## As if in body
4048              $reconstruct_active_formatting_elements->($insert_to_current);
4049                  
4050              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4051              
4052              unless (length $token->{data}) {
4053                !!!cp ('t300');
4054                !!!next-token;
4055                next B;
4056              }
4057            }
4058            
4059            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4060              !!!cp ('t301');
4061              !!!parse-error (type => 'after html:#text', token => $token);
4062              #
4063            } else {
4064              !!!cp ('t302');
4065              ## "after body" insertion mode
4066              !!!parse-error (type => 'after body:#text', token => $token);
4067              #
4068            }
4069    
4070                $self->{insertion_mode} = 'in row';          $self->{insertion_mode} = IN_BODY_IM;
4071            ## reprocess
4072            next B;
4073          } elsif ($token->{type} == START_TAG_TOKEN) {
4074            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4075              !!!cp ('t303');
4076              !!!parse-error (type => 'after html',
4077                              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    
4087                !!!next-token;          $self->{insertion_mode} = IN_BODY_IM;
4088                redo B;          !!!ack-later;
4089              } elsif ({          ## reprocess
4090                        body => 1, caption => 1, col => 1,          next B;
4091                        colgroup => 1, html => 1,        } elsif ($token->{type} == END_TAG_TOKEN) {
4092                       }->{$token->{tag_name}}) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4093                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t305');
4094                ## Ignore the token            !!!parse-error (type => 'after html:/',
4095                !!!next-token;                            text => $token->{tag_name}, token => $token);
4096                redo B;            
4097              } elsif ({            $self->{insertion_mode} = IN_BODY_IM;
4098                        table => 1, tbody => 1, tfoot => 1,            ## Reprocess.
4099                        thead => 1, tr => 1,            next B;
4100                       }->{$token->{tag_name}}) {          } else {
4101                ## have an element in table scope            !!!cp ('t306');
4102                my $i;          }
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
4103    
4104                ## Close the cell          ## "after body" insertion mode
4105                !!!back-token; # </?>          if ($token->{tag_name} eq 'html') {
4106                $token = {type => 'end tag', tag_name => $tn};            if (defined $self->{inner_html_node}) {
4107                redo B;              !!!cp ('t307');
4108              } else {              !!!parse-error (type => 'unmatched end tag',
4109                #                              text => 'html', token => $token);
4110              }              ## Ignore the token
4111                !!!next-token;
4112                next B;
4113            } else {            } else {
4114              #              !!!cp ('t308');
4115                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4116                !!!next-token;
4117                next B;
4118            }            }
4119            } else {
4120              !!!cp ('t309');
4121              !!!parse-error (type => 'after body:/',
4122                              text => $token->{tag_name}, token => $token);
4123    
4124              $self->{insertion_mode} = IN_BODY_IM;
4125              ## reprocess
4126              next B;
4127            }
4128          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4129            !!!cp ('t309.2');
4130            ## Stop parsing
4131            last B;
4132          } else {
4133            die "$0: $token->{type}: Unknown token type";
4134          }
4135        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4136          if ($token->{type} == CHARACTER_TOKEN) {
4137            if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
4138              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4139                        
4140            $in_body->($insert_to_current);            unless (length $token->{data}) {
4141            redo B;              !!!cp ('t310');
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4142              !!!next-token;              !!!next-token;
4143              redo B;              next B;
4144            } elsif ($token->{type} eq 'comment') {            }
4145              my $comment = $self->{document}->create_comment ($token->{data});          }
4146              $self->{open_elements}->[-1]->[0]->append_child ($comment);          
4147            if ($token->{data} =~ s/^[^\x09\x0A\x0C\x20]+//) {
4148              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4149                !!!cp ('t311');
4150                !!!parse-error (type => 'in frameset:#text', token => $token);
4151              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4152                !!!cp ('t312');
4153                !!!parse-error (type => 'after frameset:#text', token => $token);
4154              } else { # "after after frameset"
4155                !!!cp ('t313');
4156                !!!parse-error (type => 'after html:#text', token => $token);
4157              }
4158              
4159              ## Ignore the token.
4160              if (length $token->{data}) {
4161                !!!cp ('t314');
4162                ## reprocess the rest of characters
4163              } else {
4164                !!!cp ('t315');
4165              !!!next-token;              !!!next-token;
4166              redo B;            }
4167            } elsif ($token->{type} eq 'start tag') {            next B;
4168              if ($token->{tag_name} eq 'option') {          }
4169                if ($self->{open_elements}->[-1]->[1] eq 'option') {          
4170                  ## As if </option>          die qq[$0: Character "$token->{data}"];
4171                  pop @{$self->{open_elements}};        } elsif ($token->{type} == START_TAG_TOKEN) {
4172                }          if ($token->{tag_name} eq 'frameset' and
4173                $self->{insertion_mode} == IN_FRAMESET_IM) {
4174                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
4175                !!!next-token;            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4176                redo B;            !!!nack ('t318.1');
4177              } elsif ($token->{tag_name} eq 'optgroup') {            !!!next-token;
4178                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
4179                  ## As if </option>          } elsif ($token->{tag_name} eq 'frame' and
4180                  pop @{$self->{open_elements}};                   $self->{insertion_mode} == IN_FRAMESET_IM) {
4181                }            !!!cp ('t319');
4182              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4183              pop @{$self->{open_elements}};
4184              !!!ack ('t319.1');
4185              !!!next-token;
4186              next B;
4187            } elsif ($token->{tag_name} eq 'noframes') {
4188              !!!cp ('t320');
4189              ## NOTE: As if in head.
4190              $parse_rcdata->(CDATA_CONTENT_MODEL);
4191              next B;
4192    
4193                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
4194                  ## As if </optgroup>            ## has no parse error.
4195                  pop @{$self->{open_elements}};          } else {
4196                }            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4197                !!!cp ('t321');
4198                !!!parse-error (type => 'in frameset',
4199                                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
4210              !!!nack ('t322.1');
4211              !!!next-token;
4212              next B;
4213            }
4214          } elsif ($token->{type} == END_TAG_TOKEN) {
4215            if ($token->{tag_name} eq 'frameset' and
4216                $self->{insertion_mode} == IN_FRAMESET_IM) {
4217              if ($self->{open_elements}->[-1]->[1] == HTML_EL and
4218                  @{$self->{open_elements}} == 1) {
4219                !!!cp ('t325');
4220                !!!parse-error (type => 'unmatched end tag',
4221                                text => $token->{tag_name}, token => $token);
4222                ## Ignore the token
4223                !!!next-token;
4224              } else {
4225                !!!cp ('t326');
4226                pop @{$self->{open_elements}};
4227                !!!next-token;
4228              }
4229    
4230                !!!insert-element ($token->{tag_name}, $token->{attributes});            if (not defined $self->{inner_html_node} and
4231                !!!next-token;                not ($self->{open_elements}->[-1]->[1] == FRAMESET_EL)) {
4232                redo B;              !!!cp ('t327');
4233              } elsif ($token->{tag_name} eq 'select') {              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4234                !!!parse-error (type => 'not closed:select');            } else {
4235                ## As if </select> instead              !!!cp ('t328');
4236                ## have an element in table scope            }
4237                my $i;            next B;
4238                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          } elsif ($token->{tag_name} eq 'html' and
4239                  my $node = $self->{open_elements}->[$_];                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4240                  if ($node->[1] eq $token->{tag_name}) {            !!!cp ('t329');
4241                    $i = $_;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4242                    last INSCOPE;            !!!next-token;
4243                  } elsif ({            next B;
4244                            table => 1, html => 1,          } else {
4245                           }->{$node->[1]}) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4246                    last INSCOPE;              !!!cp ('t330');
4247                  }              !!!parse-error (type => 'in frameset:/',
4248                } # INSCOPE                              text => $token->{tag_name}, token => $token);
4249                unless (defined $i) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4250                  !!!parse-error (type => 'unmatched end tag:select');              !!!cp ('t330.1');
4251                  ## Ignore the token              !!!parse-error (type => 'after frameset:/',
4252                  !!!next-token;                              text => $token->{tag_name}, token => $token);
4253                  redo B;            } else { # "after after html"
4254                }              !!!cp ('t331');
4255                              !!!parse-error (type => 'after after frameset:/',
4256                splice @{$self->{open_elements}}, $i;                              text => $token->{tag_name}, token => $token);
4257              }
4258              ## Ignore the token
4259              !!!next-token;
4260              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 {
4274            die "$0: $token->{type}: Unknown token type";
4275          }
4276        } else {
4277          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4278        }
4279    
4280                $self->_reset_insertion_mode;      ## "in body" insertion mode
4281        if ($token->{type} == START_TAG_TOKEN) {
4282          if ($token->{tag_name} eq 'script') {
4283            !!!cp ('t332');
4284            ## NOTE: This is an "as if in head" code clone
4285            $script_start_tag->();
4286            next B;
4287          } elsif ($token->{tag_name} eq 'style') {
4288            !!!cp ('t333');
4289            ## NOTE: This is an "as if in head" code clone
4290            $parse_rcdata->(CDATA_CONTENT_MODEL);
4291            next B;
4292          } elsif ({
4293                    base => 1, command => 1, eventsource => 1, link => 1,
4294                   }->{$token->{tag_name}}) {
4295            !!!cp ('t334');
4296            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4297            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4298            pop @{$self->{open_elements}};
4299            !!!ack ('t334.1');
4300            !!!next-token;
4301            next B;
4302          } elsif ($token->{tag_name} eq 'meta') {
4303            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4304            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4305            my $meta_el = pop @{$self->{open_elements}};
4306    
4307                !!!next-token;          unless ($self->{confident}) {
4308                redo B;            if ($token->{attributes}->{charset}) {
4309              } else {              !!!cp ('t335');
4310                #              ## NOTE: Whether the encoding is supported or not is handled
4311                ## in the {change_encoding} callback.
4312                $self->{change_encoding}
4313                    ->($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}) {
4320                if ($token->{attributes}->{content}->{value}
4321                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4322                        [\x09\x0A\x0C\x0D\x20]*=
4323                        [\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}
4330                      ->($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            } elsif ($token->{type} eq 'end tag') {            }
4337              if ($token->{tag_name} eq 'optgroup') {          } else {
4338                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($token->{attributes}->{charset}) {
4339                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!cp ('t337');
4340                  ## As if </option>              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4341                  splice @{$self->{open_elements}}, -2;                  ->set_user_data (manakai_has_reference =>
4342                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                                       $token->{attributes}->{charset}
4343                  pop @{$self->{open_elements}};                                           ->{has_reference});
4344                } else {            }
4345                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            if ($token->{attributes}->{content}) {
4346                  ## Ignore the token              !!!cp ('t338');
4347                }              $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4348                !!!next-token;                  ->set_user_data (manakai_has_reference =>
4349                redo B;                                       $token->{attributes}->{content}
4350              } elsif ($token->{tag_name} eq 'option') {                                           ->{has_reference});
4351                if ($self->{open_elements}->[-1]->[1] eq 'option') {            }
4352                  pop @{$self->{open_elements}};          }
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
4353    
4354                !!!next-token;          !!!ack ('t338.1');
4355                redo B;          !!!next-token;
4356              } elsif ({          next B;
4357                        caption => 1, table => 1, tbody => 1,        } elsif ($token->{tag_name} eq 'title') {
4358                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,          !!!cp ('t341');
4359                       }->{$token->{tag_name}}) {          ## NOTE: This is an "as if in head" code clone
4360                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          $parse_rcdata->(RCDATA_CONTENT_MODEL);
4361                          next B;
4362                ## have an element in table scope        } elsif ($token->{tag_name} eq 'body') {
4363                my $i;          !!!parse-error (type => 'in body', text => 'body', token => $token);
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## As if </select>  
               ## have an element in table scope  
               undef $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'select') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the </select> token  
                 !!!next-token; ## TODO: ok?  
                 redo B;  
               }  
4364                                
4365                splice @{$self->{open_elements}}, $i;          if (@{$self->{open_elements}} == 1 or
4366                not ($self->{open_elements}->[1]->[1] == BODY_EL)) {
4367              !!!cp ('t342');
4368              ## Ignore the token
4369            } else {
4370              my $body_el = $self->{open_elements}->[1]->[0];
4371              for my $attr_name (keys %{$token->{attributes}}) {
4372                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4373                  !!!cp ('t343');
4374                  $body_el->set_attribute_ns
4375                    (undef, [undef, $attr_name],
4376                     $token->{attributes}->{$attr_name}->{value});
4377                }
4378              }
4379            }
4380            !!!nack ('t343.1');
4381            !!!next-token;
4382            next B;
4383          } elsif ({
4384                    ## NOTE: Start tags for non-phrasing flow content elements
4385    
4386                $self->_reset_insertion_mode;                  ## NOTE: The normal one
4387                    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}}) {
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                ## reprocess          ## has a p element in scope
4411                redo B;          INSCOPE: for (reverse @{$self->{open_elements}}) {
4412              if ($_->[1] == P_EL) {
4413                !!!cp ('t344');
4414                !!!back-token; # <form>
4415                $token = {type => END_TAG_TOKEN, tag_name => 'p',
4416                          line => $token->{line}, column => $token->{column}};
4417                next B;
4418              } elsif ($_->[1] & SCOPING_EL) {
4419                !!!cp ('t345');
4420                last INSCOPE;
4421              }
4422            } # INSCOPE
4423              
4424            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4425            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
4426              !!!nack ('t346.1');
4427              !!!next-token;
4428              if ($token->{type} == CHARACTER_TOKEN) {
4429                $token->{data} =~ s/^\x0A//;
4430                unless (length $token->{data}) {
4431                  !!!cp ('t346');
4432                  !!!next-token;
4433              } else {              } else {
4434                #                !!!cp ('t349');
4435              }              }
4436            } else {            } else {
4437              #              !!!cp ('t348');
4438            }            }
4439            } elsif ($token->{tag_name} eq 'form') {
4440              !!!cp ('t347.1');
4441              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4442    
4443            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!nack ('t347.2');
           ## Ignore the token  
4444            !!!next-token;            !!!next-token;
4445            redo B;          } elsif ($token->{tag_name} eq 'table') {
4446          } elsif ($self->{insertion_mode} eq 'after body') {            !!!cp ('t382');
4447            if ($token->{type} eq 'character') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
4448              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {            
4449                my $data = $1;            $self->{insertion_mode} = IN_TABLE_IM;
               ## As if in body  
               $reconstruct_active_formatting_elements->($insert_to_current);  
                 
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
4450    
4451                unless (length $token->{data}) {            !!!nack ('t382.1');
4452                  !!!next-token;            !!!next-token;
4453                  redo B;          } elsif ($token->{tag_name} eq 'hr') {
4454                }            !!!cp ('t386');
4455              }            pop @{$self->{open_elements}};
4456                        
4457              #            !!!nack ('t386.1');
4458              !!!parse-error (type => 'after body:#'.$token->{type});            !!!next-token;
4459            } elsif ($token->{type} eq 'comment') {          } else {
4460              my $comment = $self->{document}->create_comment ($token->{data});            !!!nack ('t347.1');
4461              $self->{open_elements}->[0]->[0]->append_child ($comment);            !!!next-token;
4462              !!!next-token;          }
4463              redo B;          next B;
4464            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{tag_name} eq 'li') {
4465              !!!parse-error (type => 'after body:'.$token->{tag_name});          ## NOTE: As normal, but imply </li> when there's another <li> ...
4466              #  
4467            } elsif ($token->{type} eq 'end tag') {          ## NOTE: Special, Scope (<li><foo><li> == <li><foo><li/></foo></li>)
4468              if ($token->{tag_name} eq 'html') {            ## Interpreted as <li><foo/></li><li/> (non-conforming)
4469                if (defined $self->{inner_html_node}) {            ## blockquote (O9.27), center (O), dd (Fx3, O, S3.1.2, IE7),
4470                  !!!parse-error (type => 'unmatched end tag:html');            ## dt (Fx, O, S, IE), dl (O), fieldset (O, S, IE), form (Fx, O, S),
4471                  ## Ignore the token            ## hn (O), pre (O), applet (O, S), button (O, S), marquee (Fx, O, S),
4472                  !!!next-token;            ## object (Fx)
4473                  redo B;            ## Generate non-tree (non-conforming)
4474              ## basefont (IE7 (where basefont is non-void)), center (IE),
4475              ## form (IE), hn (IE)
4476            ## address, div, p (<li><foo><li> == <li><foo/></li><li/>)
4477              ## Interpreted as <li><foo><li/></foo></li> (non-conforming)
4478              ## div (Fx, S)
4479    
4480            my $non_optional;
4481            my $i = -1;
4482    
4483            ## 1.
4484            for my $node (reverse @{$self->{open_elements}}) {
4485              if ($node->[1] == LI_EL) {
4486                ## 2. (a) As if </li>
4487                {
4488                  ## 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 {                } else {
4503                  $previous_insertion_mode = $self->{insertion_mode};                  !!!cp ('t356');
                 $self->{insertion_mode} = 'trailing end';  
                 !!!next-token;  
                 redo B;  
4504                }                }
4505              } else {  
4506                !!!parse-error (type => 'after body:/'.$token->{tag_name});                ## 3. Pop
4507                  splice @{$self->{open_elements}}, $i;
4508              }              }
4509    
4510                last; ## 2. (b) goto 5.
4511              } elsif (
4512                       ## NOTE: not "formatting" and not "phrasing"
4513                       ($node->[1] & SPECIAL_EL or
4514                        $node->[1] & SCOPING_EL) and
4515                       ## NOTE: "li", "dt", and "dd" are in |SPECIAL_EL|.
4516                       (not $node->[1] & ADDRESS_DIV_P_EL)
4517                      ) {
4518                ## 3.
4519                !!!cp ('t357');
4520                last; ## goto 5.
4521              } elsif ($node->[1] & END_TAG_OPTIONAL_EL) {
4522                !!!cp ('t358');
4523                #
4524            } else {            } else {
4525              !!!parse-error (type => 'after body:#'.$token->{type});              !!!cp ('t359');
4526                $non_optional ||= $node;
4527                #
4528            }            }
4529              ## 4.
4530              ## goto 2.
4531              $i--;
4532            }
4533    
4534            $self->{insertion_mode} = 'in body';          ## 5. (a) has a |p| element in scope
4535            ## reprocess          INSCOPE: for (reverse @{$self->{open_elements}}) {
4536            redo B;            if ($_->[1] == P_EL) {
4537          } elsif ($self->{insertion_mode} eq 'in frameset') {              !!!cp ('t353');
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
4538    
4539                unless (length $token->{data}) {              ## NOTE: |<p><li>|, for example.
                 !!!next-token;  
                 redo B;  
               }  
             }  
4540    
4541              #              !!!back-token; # <x>
4542            } elsif ($token->{type} eq 'comment') {              $token = {type => END_TAG_TOKEN, tag_name => 'p',
4543              my $comment = $self->{document}->create_comment ($token->{data});                        line => $token->{line}, column => $token->{column}};
4544              $self->{open_elements}->[-1]->[0]->append_child ($comment);              next B;
4545              !!!next-token;            } elsif ($_->[1] & SCOPING_EL) {
4546              redo B;              !!!cp ('t354');
4547            } elsif ($token->{type} eq 'start tag') {              last INSCOPE;
4548              if ($token->{tag_name} eq 'frameset') {            }
4549                !!!insert-element ($token->{tag_name}, $token->{attributes});          } # INSCOPE
4550                !!!next-token;  
4551                redo B;          ## 5. (b) insert
4552              } elsif ($token->{tag_name} eq 'frame') {          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4553                !!!insert-element ($token->{tag_name}, $token->{attributes});          !!!nack ('t359.1');
4554                pop @{$self->{open_elements}};          !!!next-token;
4555                !!!next-token;          next B;
4556                redo B;        } elsif ($token->{tag_name} eq 'dt' or
4557              } elsif ($token->{tag_name} eq 'noframes') {                 $token->{tag_name} eq 'dd') {
4558                $in_body->($insert_to_current);          ## NOTE: As normal, but imply </dt> or </dd> when ...
4559                redo B;  
4560              } else {          my $non_optional;
4561            my $i = -1;
4562    
4563            ## 1.
4564            for my $node (reverse @{$self->{open_elements}}) {
4565              if ($node->[1] == DTDD_EL) {
4566                ## 2. (a) As if </li>
4567                {
4568                  ## If no </li> - not applied
4569                #                #
4570              }  
4571            } elsif ($token->{type} eq 'end tag') {                ## Otherwise
4572              if ($token->{tag_name} eq 'frameset') {  
4573                if ($self->{open_elements}->[-1]->[1] eq 'html' and                ## 1. generate implied end tags, except for </dt> or </dd>
4574                    @{$self->{open_elements}} == 1) {                #
4575                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4576                  ## Ignore the token                ## 2. If current node != "dt"|"dd", parse error
4577                  !!!next-token;                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 {                } else {
4583                  pop @{$self->{open_elements}};                  !!!cp ('t356.1');
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
4584                }                }
4585                redo B;  
4586              } else {                ## 3. Pop
4587                #                splice @{$self->{open_elements}}, $i;
4588              }              }
4589    
4590                last; ## 2. (b) goto 5.
4591              } elsif (
4592                       ## NOTE: not "formatting" and not "phrasing"
4593                       ($node->[1] & SPECIAL_EL or
4594                        $node->[1] & SCOPING_EL) and
4595                       ## NOTE: "li", "dt", and "dd" are in |SPECIAL_EL|.
4596    
4597                       (not $node->[1] & ADDRESS_DIV_P_EL)
4598                      ) {
4599                ## 3.
4600                !!!cp ('t357.1');
4601                last; ## goto 5.
4602              } elsif ($node->[1] & END_TAG_OPTIONAL_EL) {
4603                !!!cp ('t358.1');
4604                #
4605            } else {            } else {
4606                !!!cp ('t359.1');
4607                $non_optional ||= $node;
4608              #              #
4609            }            }
4610              ## 4.
4611              ## goto 2.
4612              $i--;
4613            }
4614    
4615            ## 5. (a) has a |p| element in scope
4616            INSCOPE: for (reverse @{$self->{open_elements}}) {
4617              if ($_->[1] == P_EL) {
4618                !!!cp ('t353.1');
4619                !!!back-token; # <x>
4620                $token = {type => END_TAG_TOKEN, tag_name => 'p',
4621                          line => $token->{line}, column => $token->{column}};
4622                next B;
4623              } elsif ($_->[1] & SCOPING_EL) {
4624                !!!cp ('t354.1');
4625                last INSCOPE;
4626              }
4627            } # INSCOPE
4628    
4629            ## 5. (b) insert
4630            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4631            !!!nack ('t359.2');
4632            !!!next-token;
4633            next B;
4634          } elsif ($token->{tag_name} eq 'plaintext') {
4635            ## NOTE: As normal, but effectively ends parsing
4636    
4637            ## has a p element in scope
4638            INSCOPE: for (reverse @{$self->{open_elements}}) {
4639              if ($_->[1] == P_EL) {
4640                !!!cp ('t367');
4641                !!!back-token; # <plaintext>
4642                $token = {type => END_TAG_TOKEN, tag_name => 'p',
4643                          line => $token->{line}, column => $token->{column}};
4644                next B;
4645              } elsif ($_->[1] & SCOPING_EL) {
4646                !!!cp ('t368');
4647                last INSCOPE;
4648              }
4649            } # INSCOPE
4650                        
4651            if (defined $token->{tag_name}) {          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4652              !!!parse-error (type => 'in frameset:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});            
4653            } else {          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4654              !!!parse-error (type => 'in frameset:#'.$token->{type});            
4655            !!!nack ('t368.1');
4656            !!!next-token;
4657            next B;
4658          } elsif ($token->{tag_name} eq 'a') {
4659            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4660              my $node = $active_formatting_elements->[$i];
4661              if ($node->[1] == A_EL) {
4662                !!!cp ('t371');
4663                !!!parse-error (type => 'in a:a', token => $token);
4664                
4665                !!!back-token; # <a>
4666                $token = {type => END_TAG_TOKEN, tag_name => 'a',
4667                          line => $token->{line}, column => $token->{column}};
4668                $formatting_end_tag->($token);
4669                
4670                AFE2: for (reverse 0..$#$active_formatting_elements) {
4671                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4672                    !!!cp ('t372');
4673                    splice @$active_formatting_elements, $_, 1;
4674                    last AFE2;
4675                  }
4676                } # AFE2
4677                OE: for (reverse 0..$#{$self->{open_elements}}) {
4678                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4679                    !!!cp ('t373');
4680                    splice @{$self->{open_elements}}, $_, 1;
4681                    last OE;
4682                  }
4683                } # OE
4684                last AFE;
4685              } elsif ($node->[0] eq '#marker') {
4686                !!!cp ('t374');
4687                last AFE;
4688              }
4689            } # AFE
4690              
4691            $reconstruct_active_formatting_elements->($insert_to_current);
4692    
4693            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4694            push @$active_formatting_elements, $self->{open_elements}->[-1];
4695    
4696            !!!nack ('t374.1');
4697            !!!next-token;
4698            next B;
4699          } elsif ($token->{tag_name} eq 'nobr') {
4700            $reconstruct_active_formatting_elements->($insert_to_current);
4701    
4702            ## has a |nobr| element in scope
4703            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4704              my $node = $self->{open_elements}->[$_];
4705              if ($node->[1] == NOBR_EL) {
4706                !!!cp ('t376');
4707                !!!parse-error (type => 'in nobr:nobr', token => $token);
4708                !!!back-token; # <nobr>
4709                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
4710                          line => $token->{line}, column => $token->{column}};
4711                next B;
4712              } elsif ($node->[1] & SCOPING_EL) {
4713                !!!cp ('t377');
4714                last INSCOPE;
4715            }            }
4716            } # INSCOPE
4717            
4718            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4719            push @$active_formatting_elements, $self->{open_elements}->[-1];
4720            
4721            !!!nack ('t377.1');
4722            !!!next-token;
4723            next B;
4724          } elsif ($token->{tag_name} eq 'button') {
4725            ## has a button element in scope
4726            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4727              my $node = $self->{open_elements}->[$_];
4728              if ($node->[1] == BUTTON_EL) {
4729                !!!cp ('t378');
4730                !!!parse-error (type => 'in button:button', token => $token);
4731                !!!back-token; # <button>
4732                $token = {type => END_TAG_TOKEN, tag_name => 'button',
4733                          line => $token->{line}, column => $token->{column}};
4734                next B;
4735              } elsif ($node->[1] & SCOPING_EL) {
4736                !!!cp ('t379');
4737                last INSCOPE;
4738              }
4739            } # INSCOPE
4740              
4741            $reconstruct_active_formatting_elements->($insert_to_current);
4742              
4743            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4744    
4745            ## TODO: associate with $self->{form_element} if defined
4746    
4747            push @$active_formatting_elements, ['#marker', ''];
4748    
4749            !!!nack ('t379.1');
4750            !!!next-token;
4751            next B;
4752          } elsif ({
4753                    xmp => 1,
4754                    iframe => 1,
4755                    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}}) {
4759            if ($token->{tag_name} eq 'xmp') {
4760              !!!cp ('t381');
4761              $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            $parse_rcdata->(CDATA_CONTENT_MODEL);
4767            next B;
4768          } elsif ($token->{tag_name} eq 'isindex') {
4769            !!!parse-error (type => 'isindex', token => $token);
4770            
4771            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          } elsif ($self->{insertion_mode} eq 'after frameset') {          } else {
4778            if ($token->{type} eq 'character') {            !!!ack ('t391.1');
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
4779    
4780                unless (length $token->{data}) {            my $at = $token->{attributes};
4781                  !!!next-token;            my $form_attrs;
4782                  redo B;            $form_attrs->{action} = $at->{action} if $at->{action};
4783                }            my $prompt_attr = $at->{prompt};
4784              }            $at->{name} = {name => 'name', value => 'isindex'};
4785              delete $at->{action};
4786              delete $at->{prompt};
4787              my @tokens = (
4788                            {type => START_TAG_TOKEN, tag_name => 'form',
4789                             attributes => $form_attrs,
4790                             line => $token->{line}, column => $token->{column}},
4791                            {type => START_TAG_TOKEN, tag_name => 'hr',
4792                             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) {
4799                !!!cp ('t390');
4800                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
4801                               #line => $token->{line}, column => $token->{column},
4802                              };
4803              } else {
4804                !!!cp ('t391');
4805                push @tokens, {type => CHARACTER_TOKEN,
4806                               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
4810              }
4811              push @tokens,
4812                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
4813                             line => $token->{line}, column => $token->{column}},
4814                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4815                            {type => END_TAG_TOKEN, tag_name => 'label',
4816                             line => $token->{line}, column => $token->{column}},
4817                            {type => END_TAG_TOKEN, tag_name => 'p',
4818                             line => $token->{line}, column => $token->{column}},
4819                            {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);
4824              !!!next-token;
4825              next B;
4826            }
4827          } elsif ($token->{tag_name} eq 'textarea') {
4828            ## Step 1
4829            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4830            
4831            ## Step 2
4832            ## TODO: $self->{form_element} if defined
4833    
4834              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          ## Step 3
4835                !!!parse-error (type => 'after frameset:#character');          $self->{ignore_newline} = 1;
4836    
4837                ## Ignore the token.          ## Step 4
4838                if (length $token->{data}) {          ## ISSUE: This step is wrong. (r2302 enbugged)
4839                  ## reprocess the rest of characters  
4840                } else {          ## Step 5
4841                  !!!next-token;          $self->{content_model} = RCDATA_CONTENT_MODEL;
4842                }          delete $self->{escape}; # MUST
4843                redo B;  
4844            ## Step 6-7
4845            $self->{insertion_mode} |= IN_CDATA_RCDATA_IM;
4846    
4847            !!!nack ('t392.1');
4848            !!!next-token;
4849            next B;
4850          } elsif ($token->{tag_name} eq 'optgroup' or
4851                   $token->{tag_name} eq 'option') {
4852            ## 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    
4868            $reconstruct_active_formatting_elements->($insert_to_current);
4869    
4870            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4871    
4872            !!!nack ('t397.3');
4873            !!!next-token;
4874            redo B;
4875          } elsif ($token->{tag_name} eq 'rt' or
4876                   $token->{tag_name} eq 'rp') {
4877            ## has a |ruby| element in scope
4878            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4879              my $node = $self->{open_elements}->[$_];
4880              if ($node->[1] == RUBY_EL) {
4881                !!!cp ('t398.1');
4882                ## 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            } elsif ($token->{type} eq 'comment') {              unless ($self->{open_elements}->[-1]->[1] == RUBY_EL) {
4888              my $comment = $self->{document}->create_comment ($token->{data});                !!!cp ('t398.3');
4889              $self->{open_elements}->[-1]->[0]->append_child ($comment);                !!!parse-error (type => 'not closed',
4890              !!!next-token;                                text => $self->{open_elements}->[-1]->[0]
4891              redo B;                                    ->manakai_local_name,
4892            } elsif ($token->{type} eq 'start tag') {                                token => $token);
4893              if ($token->{tag_name} eq 'noframes') {                pop @{$self->{open_elements}}
4894                $in_body->($insert_to_current);                    while not $self->{open_elements}->[-1]->[1] == RUBY_EL;
               redo B;  
             } else {  
               #  
4895              }              }
4896            } elsif ($token->{type} eq 'end tag') {              last INSCOPE;
4897              if ($token->{tag_name} eq 'html') {            } elsif ($node->[1] & SCOPING_EL) {
4898                $previous_insertion_mode = $self->{insertion_mode};              !!!cp ('t398.4');
4899                $self->{insertion_mode} = 'trailing end';              last INSCOPE;
4900                !!!next-token;            }
4901                redo B;          } # INSCOPE
4902              } else {  
4903                #          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4904    
4905            !!!nack ('t398.5');
4906            !!!next-token;
4907            redo B;
4908          } elsif ($token->{tag_name} eq 'math' or
4909                   $token->{tag_name} eq 'svg') {
4910            $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-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
4919            
4920            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;
4932            next B;
4933          } elsif ({
4934                    caption => 1, col => 1, colgroup => 1, frame => 1,
4935                    frameset => 1, head => 1,
4936                    tbody => 1, td => 1, tfoot => 1, th => 1,
4937                    thead => 1, tr => 1,
4938                   }->{$token->{tag_name}}) {
4939            !!!cp ('t401');
4940            !!!parse-error (type => 'in body',
4941                            text => $token->{tag_name}, token => $token);
4942            ## 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;
4953            redo B;
4954          } 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);
4965            
4966            !!!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;
5012            next B;
5013          }
5014        } elsif ($token->{type} == END_TAG_TOKEN) {
5015          if ($token->{tag_name} eq 'body') {
5016            ## has a |body| element in scope
5017            my $i;
5018            INSCOPE: {
5019              for (reverse @{$self->{open_elements}}) {
5020                if ($_->[1] == BODY_EL) {
5021                  !!!cp ('t405');
5022                  $i = $_;
5023                  last INSCOPE;
5024                } elsif ($_->[1] & SCOPING_EL) {
5025                  !!!cp ('t405.1');
5026                  last;
5027              }              }
5028              }
5029    
5030              ## NOTE: |<marquee></body>|, |<svg><foreignobject></body>|
5031    
5032              !!!parse-error (type => 'unmatched end tag',
5033                              text => $token->{tag_name}, token => $token);
5034              ## NOTE: Ignore the token.
5035              !!!next-token;
5036              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 {            } else {
5047              die "$0: $token->{type}: Unknown token type";              !!!cp ('t404');
5048            }            }
5049                      }
5050            !!!parse-error (type => 'after frameset:'.($token->{tag_name} eq 'end tag' ? '/' : '').$token->{tag_name});  
5051            $self->{insertion_mode} = AFTER_BODY_IM;
5052            !!!next-token;
5053            next B;
5054          } elsif ($token->{tag_name} eq 'html') {
5055            ## TODO: Update this code.  It seems that the code below is not
5056            ## up-to-date, though it has same effect as speced.
5057            if (@{$self->{open_elements}} > 1 and
5058                $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;
5069              ## reprocess
5070              next B;
5071            } else {
5072              !!!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 ({
5080                    ## NOTE: End tags for non-phrasing flow content elements
5081    
5082                    ## NOTE: The normal ones
5083                    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,
5091    
5092                    applet => 1, button => 1, marquee => 1, object => 1,
5093                   }->{$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
5099            my $i;
5100            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5101              my $node = $self->{open_elements}->[$_];
5102              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5103                !!!cp ('t410');
5104                $i = $_;
5105                last INSCOPE;
5106              } elsif ($node->[1] & SCOPING_EL) {
5107                !!!cp ('t411');
5108                last INSCOPE;
5109              }
5110            } # INSCOPE
5111    
5112            ## ISSUE: An issue in spec there          unless (defined $i) { # has an element in scope
5113              !!!cp ('t413');
5114              !!!parse-error (type => 'unmatched end tag',
5115                              text => $token->{tag_name}, token => $token);
5116              ## NOTE: Ignore the token.
5117          } else {          } else {
5118            die "$0: $self->{insertion_mode}: Unknown insertion mode";            ## 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 {
5143                !!!cp ('t414');
5144              }
5145    
5146              ## Step 3.
5147              splice @{$self->{open_elements}}, $i;
5148    
5149              ## Step 4.
5150              $clear_up_to_marker->()
5151                  if {
5152                    applet => 1, button => 1, marquee => 1, object => 1,
5153                  }->{$token->{tag_name}};
5154          }          }
       }  
     } elsif ($self->{insertion_mode} eq 'trailing end') {  
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
5155          !!!next-token;          !!!next-token;
5156          redo B;          next B;
5157        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'form') {
5158          my $comment = $self->{document}->create_comment ($token->{data});          ## NOTE: As normal, but interacts with the form element pointer
5159          $self->{document}->append_child ($comment);  
5160          !!!next-token;          undef $self->{form_element};
5161          redo B;  
5162        } elsif ($token->{type} eq 'character') {          ## has an element in scope
5163          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          my $i;
5164            my $data = $1;          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5165            ## As if in the main phase.            my $node = $self->{open_elements}->[$_];
5166            ## NOTE: The insertion mode in the main phase            if ($node->[1] == FORM_EL) {
5167            ## just before the phase has been changed to the trailing              !!!cp ('t418');
5168            ## end phase is either "after body" or "after frameset".              $i = $_;
5169            $reconstruct_active_formatting_elements->($insert_to_current);              last INSCOPE;
5170              } elsif ($node->[1] & SCOPING_EL) {
5171                !!!cp ('t419');
5172                last INSCOPE;
5173              }
5174            } # INSCOPE
5175    
5176            unless (defined $i) { # has an element in scope
5177              !!!cp ('t421');
5178              !!!parse-error (type => 'unmatched end tag',
5179                              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            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## 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            unless (length $token->{data}) {            ## Step 3.
5201              !!!next-token;            splice @{$self->{open_elements}}, $i;
5202              redo B;          }
5203    
5204            !!!next-token;
5205            next B;
5206          } 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,
5209                   }->{$token->{tag_name}}) {
5210            ## has an element in scope
5211            my $i;
5212            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5213              my $node = $self->{open_elements}->[$_];
5214              if ($node->[1] == HEADING_EL) {
5215                !!!cp ('t423');
5216                $i = $_;
5217                last INSCOPE;
5218              } elsif ($node->[1] & SCOPING_EL) {
5219                !!!cp ('t424');
5220                last INSCOPE;
5221            }            }
5222            } # INSCOPE
5223    
5224            unless (defined $i) { # has an element in scope
5225              !!!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            
5250            !!!next-token;
5251            next B;
5252          } elsif ($token->{tag_name} eq 'p') {
5253            ## NOTE: As normal, except </p> implies <p> and ...
5254    
5255          !!!parse-error (type => 'after html:#character');          ## has an element in scope
5256          $self->{insertion_mode} = $previous_insertion_mode;          my $non_optional;
5257          ## reprocess          my $i;
5258          redo B;          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5259        } elsif ($token->{type} eq 'start tag' or            my $node = $self->{open_elements}->[$_];
5260                 $token->{type} eq 'end tag') {            if ($node->[1] == P_EL) {
5261          !!!parse-error (type => 'after html:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});              !!!cp ('t410.1');
5262          $self->{insertion_mode} = $previous_insertion_mode;              $i = $_;
5263          ## reprocess              last INSCOPE;
5264          redo B;            } elsif ($node->[1] & SCOPING_EL) {
5265        } elsif ($token->{type} eq 'end-of-file') {              !!!cp ('t411.1');
5266          ## Stop parsing              last INSCOPE;
5267          last B;            } 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 ({
5310                    a => 1,
5311                    b => 1, big => 1, em => 1, font => 1, i => 1,
5312                    nobr => 1, s => 1, small => 1, strike => 1,
5313                    strong => 1, tt => 1, u => 1,
5314                   }->{$token->{tag_name}}) {
5315            !!!cp ('t427');
5316            $formatting_end_tag->($token);
5317            next B;
5318          } elsif ($token->{tag_name} eq 'br') {
5319            !!!cp ('t428');
5320            !!!parse-error (type => 'unmatched end tag',
5321                            text => 'br', token => $token);
5322    
5323            ## As if <br>
5324            $reconstruct_active_formatting_elements->($insert_to_current);
5325            
5326            my $el;
5327            !!!create-element ($el, $HTML_NS, 'br',, $token);
5328            $insert->($el);
5329            
5330            ## Ignore the token.
5331            !!!next-token;
5332            next B;
5333        } else {        } else {
5334          die "$0: $token->{type}: Unknown token";          if ($token->{tag_name} eq 'sarcasm') {
5335              sleep 0.001; # take a deep breath
5336            }
5337    
5338            ## Step 1
5339            my $node_i = -1;
5340            my $node = $self->{open_elements}->[$node_i];
5341    
5342            ## Step 2
5343            S2: {
5344              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
5348                ## generate implied end tags
5349                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5350                  !!!cp ('t430');
5351                  ## NOTE: |<ruby><rt></ruby>|.
5352                  ## ISSUE: <ruby><rt></rt> will also take this code path,
5353                  ## which seems wrong.
5354                  pop @{$self->{open_elements}};
5355                  $node_i++;
5356                }
5357            
5358                ## Step 2
5359                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>
5365                  !!!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
5374                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
5375    
5376                !!!next-token;
5377                last S2;
5378              } else {
5379                ## Step 3
5380                if (not ($node->[1] & FORMATTING_EL) and
5381                    #not $phrasing_category->{$node->[1]} and
5382                    ($node->[1] & SPECIAL_EL or
5383                     $node->[1] & SCOPING_EL)) {
5384                  !!!cp ('t433');
5385                  !!!parse-error (type => 'unmatched end tag',
5386                                  text => $token->{tag_name}, token => $token);
5387                  ## Ignore the token
5388                  !!!next-token;
5389                  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
5401              $node_i--;
5402              $node = $self->{open_elements}->[$node_i];
5403              
5404              ## Step 5;
5405              redo S2;
5406            } # S2
5407            next B;
5408        }        }
5409      }      }
5410        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    
5432    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 5262  sub _tree_construction_main ($) { Line 5434  sub _tree_construction_main ($) {
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?
5445    
5446    my $nt = $node->node_type;    my $nt = $node->node_type;
5447    if ($nt == 9) {    if ($nt == 9) {
# Line 5283  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 5296  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          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
5517          $p->{column}++;
5518    
5519        $self->{next_input_character} = -1 and return if $i >= length $$s;        if ($self->{nc} == 0x000A) { # LF
5520        $self->{next_input_character} = ord substr $$s, $i++, 1;          $p->{line}++;
5521        $column++;          $p->{column} = 0;
5522            !!!cp ('i1');
5523        if ($self->{next_input_character} == 0x000A) { # LF        } elsif ($self->{nc} == 0x000D) { # CR
5524          $line++;  ## TODO: support for abort/streaming
5525          $column = 0;          my $next = '';
5526        } elsif ($self->{next_input_character} == 0x000D) { # CR          if ($input->read ($next, 1) and $next ne "\x0A") {
5527          $i++ if substr ($$s, $i, 1) eq "\x0A";            $self->{next_nc} = $next;
5528          $self->{next_input_character} = 0x000A; # LF # MUST          }
5529          $line++;          $self->{nc} = 0x000A; # LF # MUST
5530          $column = 0;          $p->{line}++;
5531        } elsif ($self->{next_input_character} > 0x10FFFF) {          $p->{column} = 0;
5532          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          !!!cp ('i2');
5533        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } 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_flag} = {      $p->{content_model} = {
5603        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5604        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5605        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5606        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5607        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5608        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5609        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5610        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5611        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5612        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5613      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5614         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5615            unless defined $p->{content_model};
5616            ## 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 5387  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 5411  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    }    }
# Line 5418  sub set_inner_html ($$$) { Line 5682  sub set_inner_html ($$$) {
5682    
5683  } # tree construction stage  } # tree construction stage
5684    
5685  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5686    my (undef, $node, $on_error) = @_;  push our @ISA, 'Error';
   
   ## Step 1  
   my $s = '';  
   
   my $in_cdata;  
   my $parent = $node;  
   while (defined $parent) {  
     if ($parent->node_type == 1 and  
         $parent->namespace_uri eq 'http://www.w3.org/1999/xhtml' and  
         {  
           style => 1, script => 1, xmp => 1, iframe => 1,  
           noembed => 1, noframes => 1, noscript => 1,  
         }->{$parent->local_name}) { ## TODO: case thingy  
       $in_cdata = 1;  
     }  
     $parent = $parent->parent_node;  
   }  
   
   ## Step 2  
   my @node = @{$node->child_nodes};  
   C: while (@node) {  
     my $child = shift @node;  
     unless (ref $child) {  
       if ($child eq 'cdata-out') {  
         $in_cdata = 0;  
       } else {  
         $s .= $child; # end tag  
       }  
       next C;  
     }  
       
     my $nt = $child->node_type;  
     if ($nt == 1) { # Element  
       my $tag_name = $child->tag_name; ## TODO: manakai_tag_name  
       $s .= '<' . $tag_name;  
       ## NOTE: Non-HTML case:  
       ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = $attr->name; ## TODO: manakai_name  
         $s .= ' ' . $attr_name . '="';  
         my $attr_value = $attr->value;  
         ## escape  
         $attr_value =~ s/&/&amp;/g;  
         $attr_value =~ s/</&lt;/g;  
         $attr_value =~ s/>/&gt;/g;  
         $attr_value =~ s/"/&quot;/g;  
         $s .= $attr_value . '"';  
       }  
       $s .= '>';  
         
       next C if {  
         area => 1, base => 1, basefont => 1, bgsound => 1,  
         br => 1, col => 1, embed => 1, frame => 1, hr => 1,  
         img => 1, input => 1, link => 1, meta => 1, param => 1,  
         spacer => 1, wbr => 1,  
       }->{$tag_name};  
   
       $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 1,  
         plaintext => 1,  
       }->{$tag_name}) {  
         unshift @node, 'cdata-out';  
         $in_cdata = 1;  
       }  
   
       unshift @node, @{$child->child_nodes}, '</' . $tag_name . '>';  
     } elsif ($nt == 3 or $nt == 4) {  
       if ($in_cdata) {  
         $s .= $child->data;  
       } else {  
         my $value = $child->data;  
         $value =~ s/&/&amp;/g;  
         $value =~ s/</&lt;/g;  
         $value =~ s/>/&gt;/g;  
         $value =~ s/"/&quot;/g;  
         $s .= $value;  
       }  
     } elsif ($nt == 8) {  
       $s .= '<!--' . $child->data . '-->';  
     } elsif ($nt == 10) {  
       $s .= '<!DOCTYPE ' . $child->name . '>';  
     } elsif ($nt == 5) { # entrefs  
       push @node, @{$child->child_nodes};  
     } else {  
       $on_error->($child) if defined $on_error;  
     }  
     ## ISSUE: This code does not support PIs.  
   } # C  
     
   ## Step 3  
   return \$s;  
 } # get_inner_html  
5687    
5688  1;  1;
5689  # $Date$  # $Date$

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24