/[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.65 by wakaba, Mon Nov 19 12:18:26 2007 UTC revision 1.131 by wakaba, Sun Apr 13 05:54:28 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  ## TODO: 1252 parse error (revision 1264)
13  ## is not yet clear.  ## TODO: 8859-11 = 874 (revision 1271)
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  
15  ## "{U+FEFF}..." in GB18030?  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
16    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
17  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
18    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
19    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
20    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
21    hr => 1,  
22    br => 1,  sub A_EL () { 0b1 }
23    img=> 1,  sub ADDRESS_EL () { 0b10 }
24    embed => 1,  sub BODY_EL () { 0b100 }
25    param => 1,  sub BUTTON_EL () { 0b1000 }
26    area => 1,  sub CAPTION_EL () { 0b10000 }
27    col => 1,  sub DD_EL () { 0b100000 }
28    input => 1,  sub DIV_EL () { 0b1000000 }
29    sub DT_EL () { 0b10000000 }
30    sub FORM_EL () { 0b100000000 }
31    sub FORMATTING_EL () { 0b1000000000 }
32    sub FRAMESET_EL () { 0b10000000000 }
33    sub HEADING_EL () { 0b100000000000 }
34    sub HTML_EL () { 0b1000000000000 }
35    sub LI_EL () { 0b10000000000000 }
36    sub NOBR_EL () { 0b100000000000000 }
37    sub OPTION_EL () { 0b1000000000000000 }
38    sub OPTGROUP_EL () { 0b10000000000000000 }
39    sub P_EL () { 0b100000000000000000 }
40    sub SELECT_EL () { 0b1000000000000000000 }
41    sub TABLE_EL () { 0b10000000000000000000 }
42    sub TABLE_CELL_EL () { 0b100000000000000000000 }
43    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
44    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
45    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
46    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
47    sub FOREIGN_EL () { 0b10000000000000000000000000 }
48    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
49    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    sub END_TAG_OPTIONAL_EL () {
58      DD_EL |
59      DT_EL |
60      LI_EL |
61      P_EL
62    }
63    
64    sub ALL_END_TAG_OPTIONAL_EL () {
65      END_TAG_OPTIONAL_EL |
66      BODY_EL |
67      HTML_EL |
68      TABLE_CELL_EL |
69      TABLE_ROW_EL |
70      TABLE_ROW_GROUP_EL
71    }
72    
73    sub SCOPING_EL () {
74      BUTTON_EL |
75      CAPTION_EL |
76      HTML_EL |
77      TABLE_EL |
78      TABLE_CELL_EL |
79      MISC_SCOPING_EL
80    }
81    
82    sub TABLE_SCOPING_EL () {
83      HTML_EL |
84      TABLE_EL
85    }
86    
87    sub TABLE_ROWS_SCOPING_EL () {
88      HTML_EL |
89      TABLE_ROW_GROUP_EL
90    }
91    
92    sub TABLE_ROW_SCOPING_EL () {
93      HTML_EL |
94      TABLE_ROW_EL
95    }
96    
97    sub SPECIAL_EL () {
98      ADDRESS_EL |
99      BODY_EL |
100      DIV_EL |
101      END_TAG_OPTIONAL_EL |
102      FORM_EL |
103      FRAMESET_EL |
104      HEADING_EL |
105      OPTION_EL |
106      OPTGROUP_EL |
107      SELECT_EL |
108      TABLE_ROW_EL |
109      TABLE_ROW_GROUP_EL |
110      MISC_SPECIAL_EL
111    }
112    
113    my $el_category = {
114      a => A_EL | FORMATTING_EL,
115      address => ADDRESS_EL,
116      applet => MISC_SCOPING_EL,
117      area => MISC_SPECIAL_EL,
118      b => FORMATTING_EL,
119      base => MISC_SPECIAL_EL,
120      basefont => MISC_SPECIAL_EL,
121      bgsound => MISC_SPECIAL_EL,
122      big => FORMATTING_EL,
123      blockquote => MISC_SPECIAL_EL,
124      body => BODY_EL,
125      br => MISC_SPECIAL_EL,
126      button => BUTTON_EL,
127      caption => CAPTION_EL,
128      center => MISC_SPECIAL_EL,
129      col => MISC_SPECIAL_EL,
130      colgroup => MISC_SPECIAL_EL,
131      dd => DD_EL,
132      dir => MISC_SPECIAL_EL,
133      div => DIV_EL,
134      dl => MISC_SPECIAL_EL,
135      dt => DT_EL,
136      em => FORMATTING_EL,
137      embed => MISC_SPECIAL_EL,
138      fieldset => MISC_SPECIAL_EL,
139      font => FORMATTING_EL,
140      form => FORM_EL,
141      frame => MISC_SPECIAL_EL,
142      frameset => FRAMESET_EL,
143      h1 => HEADING_EL,
144      h2 => HEADING_EL,
145      h3 => HEADING_EL,
146      h4 => HEADING_EL,
147      h5 => HEADING_EL,
148      h6 => HEADING_EL,
149      head => MISC_SPECIAL_EL,
150      hr => MISC_SPECIAL_EL,
151      html => HTML_EL,
152      i => FORMATTING_EL,
153      iframe => MISC_SPECIAL_EL,
154      img => MISC_SPECIAL_EL,
155      input => MISC_SPECIAL_EL,
156      isindex => MISC_SPECIAL_EL,
157      li => LI_EL,
158      link => MISC_SPECIAL_EL,
159      listing => MISC_SPECIAL_EL,
160      marquee => MISC_SCOPING_EL,
161      menu => MISC_SPECIAL_EL,
162      meta => MISC_SPECIAL_EL,
163      nobr => NOBR_EL | FORMATTING_EL,
164      noembed => MISC_SPECIAL_EL,
165      noframes => MISC_SPECIAL_EL,
166      noscript => MISC_SPECIAL_EL,
167      object => MISC_SCOPING_EL,
168      ol => MISC_SPECIAL_EL,
169      optgroup => OPTGROUP_EL,
170      option => OPTION_EL,
171      p => P_EL,
172      param => MISC_SPECIAL_EL,
173      plaintext => MISC_SPECIAL_EL,
174      pre => MISC_SPECIAL_EL,
175      s => FORMATTING_EL,
176      script => MISC_SPECIAL_EL,
177      select => SELECT_EL,
178      small => FORMATTING_EL,
179      spacer => MISC_SPECIAL_EL,
180      strike => FORMATTING_EL,
181      strong => FORMATTING_EL,
182      style => MISC_SPECIAL_EL,
183      table => TABLE_EL,
184      tbody => TABLE_ROW_GROUP_EL,
185      td => TABLE_CELL_EL,
186      textarea => MISC_SPECIAL_EL,
187      tfoot => TABLE_ROW_GROUP_EL,
188      th => TABLE_CELL_EL,
189      thead => TABLE_ROW_GROUP_EL,
190      title => MISC_SPECIAL_EL,
191      tr => TABLE_ROW_EL,
192      tt => FORMATTING_EL,
193      u => FORMATTING_EL,
194      ul => MISC_SPECIAL_EL,
195      wbr => MISC_SPECIAL_EL,
196  };  };
197    
198    my $el_category_f = {
199      $MML_NS => {
200        'annotation-xml' => MML_AXML_EL,
201        mi => FOREIGN_FLOW_CONTENT_EL,
202        mo => FOREIGN_FLOW_CONTENT_EL,
203        mn => FOREIGN_FLOW_CONTENT_EL,
204        ms => FOREIGN_FLOW_CONTENT_EL,
205        mtext => FOREIGN_FLOW_CONTENT_EL,
206      },
207      $SVG_NS => {
208        foreignObject => FOREIGN_FLOW_CONTENT_EL,
209        desc => FOREIGN_FLOW_CONTENT_EL,
210        title => FOREIGN_FLOW_CONTENT_EL,
211      },
212      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
213    };
214    
215    my $svg_attr_name = {
216      attributetype => 'attributeType',
217      basefrequency => 'baseFrequency',
218      baseprofile => 'baseProfile',
219      calcmode => 'calcMode',
220      clippathunits => 'clipPathUnits',
221      contentscripttype => 'contentScriptType',
222      contentstyletype => 'contentStyleType',
223      diffuseconstant => 'diffuseConstant',
224      edgemode => 'edgeMode',
225      externalresourcesrequired => 'externalResourcesRequired',
226      fecolormatrix => 'feColorMatrix',
227      fecomposite => 'feComposite',
228      fegaussianblur => 'feGaussianBlur',
229      femorphology => 'feMorphology',
230      fetile => 'feTile',
231      filterres => 'filterRes',
232      filterunits => 'filterUnits',
233      glyphref => 'glyphRef',
234      gradienttransform => 'gradientTransform',
235      gradientunits => 'gradientUnits',
236      kernelmatrix => 'kernelMatrix',
237      kernelunitlength => 'kernelUnitLength',
238      keypoints => 'keyPoints',
239      keysplines => 'keySplines',
240      keytimes => 'keyTimes',
241      lengthadjust => 'lengthAdjust',
242      limitingconeangle => 'limitingConeAngle',
243      markerheight => 'markerHeight',
244      markerunits => 'markerUnits',
245      markerwidth => 'markerWidth',
246      maskcontentunits => 'maskContentUnits',
247      maskunits => 'maskUnits',
248      numoctaves => 'numOctaves',
249      pathlength => 'pathLength',
250      patterncontentunits => 'patternContentUnits',
251      patterntransform => 'patternTransform',
252      patternunits => 'patternUnits',
253      pointsatx => 'pointsAtX',
254      pointsaty => 'pointsAtY',
255      pointsatz => 'pointsAtZ',
256      preservealpha => 'preserveAlpha',
257      preserveaspectratio => 'preserveAspectRatio',
258      primitiveunits => 'primitiveUnits',
259      refx => 'refX',
260      refy => 'refY',
261      repeatcount => 'repeatCount',
262      repeatdur => 'repeatDur',
263      requiredextensions => 'requiredExtensions',
264      specularconstant => 'specularConstant',
265      specularexponent => 'specularExponent',
266      spreadmethod => 'spreadMethod',
267      startoffset => 'startOffset',
268      stddeviation => 'stdDeviation',
269      stitchtiles => 'stitchTiles',
270      surfacescale => 'surfaceScale',
271      systemlanguage => 'systemLanguage',
272      tablevalues => 'tableValues',
273      targetx => 'targetX',
274      targety => 'targetY',
275      textlength => 'textLength',
276      viewbox => 'viewBox',
277      viewtarget => 'viewTarget',
278      xchannelselector => 'xChannelSelector',
279      ychannelselector => 'yChannelSelector',
280      zoomandpan => 'zoomAndPan',
281    };
282    
283    my $foreign_attr_xname = {
284      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
285      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
286      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
287      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
288      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
289      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
290      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
291      'xml:base' => [$XML_NS, ['xml', 'base']],
292      'xml:lang' => [$XML_NS, ['xml', 'lang']],
293      'xml:space' => [$XML_NS, ['xml', 'space']],
294      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
295      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
296    };
297    
298    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
299    
300  my $c1_entity_char = {  my $c1_entity_char = {
301    0x80 => 0x20AC,    0x80 => 0x20AC,
302    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 63  my $c1_entity_char = { Line 332  my $c1_entity_char = {
332    0x9F => 0x0178,    0x9F => 0x0178,
333  }; # $c1_entity_char  }; # $c1_entity_char
334    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
335  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
336    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
337    my $charset = shift;    my $charset = shift;
# Line 110  sub parse_byte_string ($$$$;$) { Line 357  sub parse_byte_string ($$$$;$) {
357    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
358      my $self = shift;      my $self = shift;
359      my $charset = lc shift;      my $charset = lc shift;
360        my $token = shift;
361      ## TODO: if $charset is supported      ## TODO: if $charset is supported
362      ## TODO: normalize charset name      ## TODO: normalize charset name
363    
# Line 128  sub parse_byte_string ($$$$;$) { Line 376  sub parse_byte_string ($$$$;$) {
376      }      }
377    
378      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
379          ':'.$charset, level => 'w');          ':'.$charset, level => 'w', token => $token);
380    
381      ## Step 3      ## Step 3
382      # if (can) {      # if (can) {
# Line 155  sub parse_byte_string ($$$$;$) { Line 403  sub parse_byte_string ($$$$;$) {
403    return $return;    return $return;
404  } # parse_byte_string  } # parse_byte_string
405    
406    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
407    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
408    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
409    ## because the core part of our HTML parser expects a string of character,
410    ## not a string of bytes or code units or anything which might contain a BOM.
411    ## Therefore, any parser interface that accepts a string of bytes,
412    ## such as |parse_byte_string| in this module, must ensure that it does
413    ## strip the BOM and never strip any ZWNBSP.
414    
415  *parse_char_string = \&parse_string;  *parse_char_string = \&parse_string;
416    
417  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
# Line 170  sub parse_string ($$$;$) { Line 427  sub parse_string ($$$;$) {
427        if defined $self->{input_encoding};        if defined $self->{input_encoding};
428    
429    my $i = 0;    my $i = 0;
430    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
431    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
432    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
433      my $self = shift;      my $self = shift;
434    
435      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
436      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
437    
438        $self->{next_char} = -1 and return if $i >= length $$s;
439        $self->{next_char} = ord substr $$s, $i++, 1;
440    
441      $self->{next_input_character} = -1 and return if $i >= length $$s;      ($self->{line_prev}, $self->{column_prev})
442      $self->{next_input_character} = ord substr $$s, $i++, 1;          = ($self->{line}, $self->{column});
443      $column++;      $self->{column}++;
444            
445      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
446        $line++;        $self->{line}++;
447        $column = 0;        $self->{column} = 0;
448      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
449        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
450        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
451        $line++;        $self->{line}++;
452        $column = 0;        $self->{column} = 0;
453      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
454        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
455      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
456        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
457        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
458      }      }
459    };    };
460    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
461    $self->{next_input_character} = -1;    $self->{next_char} = -1;
462    
463    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
464      my (%opt) = @_;      my (%opt) = @_;
465      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
466        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
467        warn "Parse error ($opt{type}) at line $line column $column\n";
468    };    };
469    $self->{parse_error} = sub {    $self->{parse_error} = sub {
470      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
471    };    };
472    
473    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 213  sub parse_string ($$$;$) { Line 475  sub parse_string ($$$;$) {
475    $self->_construct_tree;    $self->_construct_tree;
476    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
477    
478      delete $self->{parse_error}; # remove loop
479    
480    return $self->{document};    return $self->{document};
481  } # parse_string  } # parse_string
482    
483  sub new ($) {  sub new ($) {
484    my $class = shift;    my $class = shift;
485    my $self = bless {}, $class;    my $self = bless {}, $class;
486    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
487      $self->{next_input_character} = -1;      $self->{next_char} = -1;
488    };    };
489    $self->{parse_error} = sub {    $self->{parse_error} = sub {
490      #      #
# Line 279  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO Line 543  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO
543  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
544  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
545  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
546    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
547    sub SELF_CLOSING_START_TAG_STATE () { 34 }
548    sub CDATA_BLOCK_STATE () { 35 }
549    
550  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
551  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 295  sub TABLE_IMS ()      { 0b1000000 } Line 562  sub TABLE_IMS ()      { 0b1000000 }
562  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
563  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
564  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
565    sub SELECT_IMS ()     { 0b10000000000 }
566    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
567        ## NOTE: "in foreign content" insertion mode is special; it is combined
568        ## with the secondary insertion mode.  In this parser, they are stored
569        ## together in the bit-or'ed form.
570    
571    ## NOTE: "initial" and "before html" insertion modes have no constants.
572    
573    ## NOTE: "after after body" insertion mode.
574  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
575    
576    ## NOTE: "after after frameset" insertion mode.
577  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
578    
579  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
580  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
581  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 311  sub IN_TABLE_IM () { TABLE_IMS } Line 589  sub IN_TABLE_IM () { TABLE_IMS }
589  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
590  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
591  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
592  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
593    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
594  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
595    
596  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 324  sub _initialize_tokenizer ($) { Line 603  sub _initialize_tokenizer ($) {
603    undef $self->{current_attribute};    undef $self->{current_attribute};
604    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
605    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
606      delete $self->{self_closing};
607    $self->{char} = [];    $self->{char} = [];
608    # $self->{next_input_character}    # $self->{next_char}
609    !!!next-input-character;    !!!next-input-character;
610    $self->{token} = [];    $self->{token} = [];
611    # $self->{escape}    # $self->{escape}
# Line 338  sub _initialize_tokenizer ($) { Line 618  sub _initialize_tokenizer ($) {
618  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
619  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
620  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
621  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
622  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
623    ##        ->{name}
624    ##        ->{value}
625    ##        ->{has_reference} == 1 or 0
626  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
627    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
628    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
629    ##     while the token is pushed back to the stack.
630    
631    ## ISSUE: "When a DOCTYPE token is created, its
632    ## <i>self-closing flag</i> must be unset (its other state is that it
633    ## be set), and its attributes list must be empty.": Wrong subject?
634    
635  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
636    
# Line 367  sub _initialize_tokenizer ($) { Line 657  sub _initialize_tokenizer ($) {
657    
658  sub _get_next_token ($) {  sub _get_next_token ($) {
659    my $self = shift;    my $self = shift;
660    
661      if ($self->{self_closing}) {
662        !!!parse-error (type => 'nestc', token => $self->{current_token});
663        ## NOTE: The |self_closing| flag is only set by start tag token.
664        ## In addition, when a start tag token is emitted, it is always set to
665        ## |current_token|.
666        delete $self->{self_closing};
667      }
668    
669    if (@{$self->{token}}) {    if (@{$self->{token}}) {
670        $self->{self_closing} = $self->{token}->[0]->{self_closing};
671      return shift @{$self->{token}};      return shift @{$self->{token}};
672    }    }
673    
674    A: {    A: {
675      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
676        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
677          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
678                not $self->{escape}) {
679              !!!cp (1);
680            $self->{state} = ENTITY_DATA_STATE;            $self->{state} = ENTITY_DATA_STATE;
681            !!!next-input-character;            !!!next-input-character;
682            redo A;            redo A;
683          } else {          } else {
684              !!!cp (2);
685            #            #
686          }          }
687        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
688          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
689            unless ($self->{escape}) {            unless ($self->{escape}) {
690              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
691                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
692                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
693                  !!!cp (3);
694                $self->{escape} = 1;                $self->{escape} = 1;
695                } else {
696                  !!!cp (4);
697              }              }
698              } else {
699                !!!cp (5);
700            }            }
701          }          }
702                    
703          #          #
704        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
705          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
706              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
707               not $self->{escape})) {               not $self->{escape})) {
708              !!!cp (6);
709            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
710            !!!next-input-character;            !!!next-input-character;
711            redo A;            redo A;
712          } else {          } else {
713              !!!cp (7);
714            #            #
715          }          }
716        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
717          if ($self->{escape} and          if ($self->{escape} and
718              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
719            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
720                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
721                !!!cp (8);
722              delete $self->{escape};              delete $self->{escape};
723              } else {
724                !!!cp (9);
725            }            }
726            } else {
727              !!!cp (10);
728          }          }
729                    
730          #          #
731        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
732          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
733            !!!emit ({type => END_OF_FILE_TOKEN,
734                      line => $self->{line}, column => $self->{column}});
735          last A; ## TODO: ok?          last A; ## TODO: ok?
736          } else {
737            !!!cp (12);
738        }        }
739        # Anything else        # Anything else
740        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
741                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
742                       line => $self->{line}, column => $self->{column},
743                      };
744        ## Stay in the data state        ## Stay in the data state
745        !!!next-input-character;        !!!next-input-character;
746    
# Line 428  sub _get_next_token ($) { Line 749  sub _get_next_token ($) {
749        redo A;        redo A;
750      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
751        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
752    
753          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
754                
755        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
756    
757        $self->{state} = DATA_STATE;        $self->{state} = DATA_STATE;
758        # next-input-character is already done        # next-input-character is already done
759    
760        unless (defined $token) {        unless (defined $token) {
761          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!cp (13);
762            !!!emit ({type => CHARACTER_TOKEN, data => '&',
763                      line => $l, column => $c,
764                     });
765        } else {        } else {
766            !!!cp (14);
767          !!!emit ($token);          !!!emit ($token);
768        }        }
769    
770        redo A;        redo A;
771      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
772        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
773          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
774              !!!cp (15);
775            !!!next-input-character;            !!!next-input-character;
776            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
777            redo A;            redo A;
778          } else {          } else {
779              !!!cp (16);
780            ## reconsume            ## reconsume
781            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
782    
783            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
784                        line => $self->{line_prev},
785                        column => $self->{column_prev},
786                       });
787    
788            redo A;            redo A;
789          }          }
790        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
791          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
792              !!!cp (17);
793            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
794            !!!next-input-character;            !!!next-input-character;
795            redo A;            redo A;
796          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
797              !!!cp (18);
798            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
799            !!!next-input-character;            !!!next-input-character;
800            redo A;            redo A;
801          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
802                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
803              !!!cp (19);
804            $self->{current_token}            $self->{current_token}
805              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
806                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
807                   line => $self->{line_prev},
808                   column => $self->{column_prev}};
809            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
810            !!!next-input-character;            !!!next-input-character;
811            redo A;            redo A;
812          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
813                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
814              !!!cp (20);
815            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
816                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
817                                        line => $self->{line_prev},
818                                        column => $self->{column_prev}};
819            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
820            !!!next-input-character;            !!!next-input-character;
821            redo A;            redo A;
822          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
823            !!!parse-error (type => 'empty start tag');            !!!cp (21);
824              !!!parse-error (type => 'empty start tag',
825                              line => $self->{line_prev},
826                              column => $self->{column_prev});
827            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
828            !!!next-input-character;            !!!next-input-character;
829    
830            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
831                        line => $self->{line_prev},
832                        column => $self->{column_prev},
833                       });
834    
835            redo A;            redo A;
836          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
837            !!!parse-error (type => 'pio');            !!!cp (22);
838              !!!parse-error (type => 'pio',
839                              line => $self->{line_prev},
840                              column => $self->{column_prev});
841            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
842            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
843                                        line => $self->{line_prev},
844                                        column => $self->{column_prev},
845                                       };
846              ## $self->{next_char} is intentionally left as is
847            redo A;            redo A;
848          } else {          } else {
849              !!!cp (23);
850            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
851            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
852            ## reconsume            ## reconsume
853    
854            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
855                        line => $self->{line_prev},
856                        column => $self->{column_prev},
857                       });
858    
859            redo A;            redo A;
860          }          }
# Line 505  sub _get_next_token ($) { Line 862  sub _get_next_token ($) {
862          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
863        }        }
864      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
865          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
866        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
867          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
868    
869            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
870            my @next_char;            my @next_char;
871            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
872              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
873              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
874              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
875              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
876                  !!!cp (24);
877                !!!next-input-character;                !!!next-input-character;
878                next TAGNAME;                next TAGNAME;
879              } else {              } else {
880                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
881                  $self->{next_char} = shift @next_char; # reconsume
882                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
883                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
884    
885                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
886                            line => $l, column => $c,
887                           });
888        
889                redo A;                redo A;
890              }              }
891            }            }
892            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
893                
894            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
895                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
896                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
897                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
898                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
899                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
900                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
901                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
902              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
903                $self->{next_char} = shift @next_char; # reconsume
904              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
905              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
906              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
907                          line => $l, column => $c,
908                         });
909              redo A;              redo A;
910            } else {            } else {
911              $self->{next_input_character} = shift @next_char;              !!!cp (27);
912                $self->{next_char} = shift @next_char;
913              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
914              # and consume...              # and consume...
915            }            }
916          } else {          } else {
917            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
918              !!!cp (28);
919            # next-input-character is already done            # next-input-character is already done
920            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
921            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
922                        line => $l, column => $c,
923                       });
924            redo A;            redo A;
925          }          }
926        }        }
927                
928        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
929            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
930          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
931                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
932                = {type => END_TAG_TOKEN,
933                   tag_name => chr ($self->{next_char} + 0x0020),
934                   line => $l, column => $c};
935          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
936          !!!next-input-character;          !!!next-input-character;
937          redo A;          redo A;
938        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
939                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
940            !!!cp (30);
941          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
942                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
943                                      line => $l, column => $c};
944          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
945          !!!next-input-character;          !!!next-input-character;
946          redo A;          redo A;
947        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
948          !!!parse-error (type => 'empty end tag');          !!!cp (31);
949            !!!parse-error (type => 'empty end tag',
950                            line => $self->{line_prev}, ## "<" in "</>"
951                            column => $self->{column_prev} - 1);
952          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
953          !!!next-input-character;          !!!next-input-character;
954          redo A;          redo A;
955        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
956            !!!cp (32);
957          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
958          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
959          # reconsume          # reconsume
960    
961          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
962                      line => $l, column => $c,
963                     });
964    
965          redo A;          redo A;
966        } else {        } else {
967            !!!cp (33);
968          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
969          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
970          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
971                                      line => $self->{line_prev}, # "<" of "</"
972                                      column => $self->{column_prev} - 1,
973                                     };
974            ## $self->{next_char} is intentionally left as is
975          redo A;          redo A;
976        }        }
977      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
978        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
979            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
980            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
981            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
982            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
983            !!!cp (34);
984          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
985          !!!next-input-character;          !!!next-input-character;
986          redo A;          redo A;
987        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
988          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
989            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
990            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
991          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
992            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
993            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
994              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
995            }            #  !!! cp (36);
996              #  !!! parse-error (type => 'end tag attribute');
997              #} else {
998                !!!cp (37);
999              #}
1000          } else {          } else {
1001            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1002          }          }
# Line 616  sub _get_next_token ($) { Line 1006  sub _get_next_token ($) {
1006          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1007    
1008          redo A;          redo A;
1009        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1010                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1011          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1012            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1013            # start tag or end tag            # start tag or end tag
1014          ## Stay in this state          ## Stay in this state
1015          !!!next-input-character;          !!!next-input-character;
1016          redo A;          redo A;
1017        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1018          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1019          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1020            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1021            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1022          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1023            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1024            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1025              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1026            }            #  !!! cp (40);
1027              #  !!! parse-error (type => 'end tag attribute');
1028              #} else {
1029                !!!cp (41);
1030              #}
1031          } else {          } else {
1032            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1033          }          }
# Line 643  sub _get_next_token ($) { Line 1037  sub _get_next_token ($) {
1037          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1038    
1039          redo A;          redo A;
1040        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1041            !!!cp (42);
1042            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1043          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1044          redo A;          redo A;
1045        } else {        } else {
1046          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1047            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1048            # start tag or end tag            # start tag or end tag
1049          ## Stay in the state          ## Stay in the state
1050          !!!next-input-character;          !!!next-input-character;
1051          redo A;          redo A;
1052        }        }
1053      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1054        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1055            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1056            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1057            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1058            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1059            !!!cp (45);
1060          ## Stay in the state          ## Stay in the state
1061          !!!next-input-character;          !!!next-input-character;
1062          redo A;          redo A;
1063        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1064          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1065            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1066            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1067          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1068            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1069            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1070                !!!cp (47);
1071              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1072              } else {
1073                !!!cp (48);
1074            }            }
1075          } else {          } else {
1076            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 691  sub _get_next_token ($) { Line 1081  sub _get_next_token ($) {
1081          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1082    
1083          redo A;          redo A;
1084        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1085                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1086          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1087                                value => ''};          $self->{current_attribute}
1088                = {name => chr ($self->{next_char} + 0x0020),
1089                   value => '',
1090                   line => $self->{line}, column => $self->{column}};
1091          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1092          !!!next-input-character;          !!!next-input-character;
1093          redo A;          redo A;
1094        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1095            !!!cp (50);
1096            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1097          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
1098          redo A;          redo A;
1099        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1100          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1101          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1102            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1103            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1104          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1105            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1106            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1107                !!!cp (53);
1108              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1109              } else {
1110                !!!cp (54);
1111            }            }
1112          } else {          } else {
1113            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 732  sub _get_next_token ($) { Line 1119  sub _get_next_token ($) {
1119    
1120          redo A;          redo A;
1121        } else {        } else {
1122          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1123                                value => ''};               0x0022 => 1, # "
1124                 0x0027 => 1, # '
1125                 0x003D => 1, # =
1126                }->{$self->{next_char}}) {
1127              !!!cp (55);
1128              !!!parse-error (type => 'bad attribute name');
1129            } else {
1130              !!!cp (56);
1131            }
1132            $self->{current_attribute}
1133                = {name => chr ($self->{next_char}),
1134                   value => '',
1135                   line => $self->{line}, column => $self->{column}};
1136          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1137          !!!next-input-character;          !!!next-input-character;
1138          redo A;          redo A;
# Line 742  sub _get_next_token ($) { Line 1141  sub _get_next_token ($) {
1141        my $before_leave = sub {        my $before_leave = sub {
1142          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1143              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1144            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1145              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1146            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1147          } else {          } else {
1148              !!!cp (58);
1149            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1150              = $self->{current_attribute};              = $self->{current_attribute};
1151          }          }
1152        }; # $before_leave        }; # $before_leave
1153    
1154        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1155            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1156            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1157            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1158            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1159            !!!cp (59);
1160          $before_leave->();          $before_leave->();
1161          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1162          !!!next-input-character;          !!!next-input-character;
1163          redo A;          redo A;
1164        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1165            !!!cp (60);
1166          $before_leave->();          $before_leave->();
1167          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1168          !!!next-input-character;          !!!next-input-character;
1169          redo A;          redo A;
1170        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1171          $before_leave->();          $before_leave->();
1172          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1173            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1174            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1175          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1176              !!!cp (62);
1177            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1178            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1179              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 784  sub _get_next_token ($) { Line 1187  sub _get_next_token ($) {
1187          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1188    
1189          redo A;          redo A;
1190        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1191                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1192          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1193            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1194          ## Stay in the state          ## Stay in the state
1195          !!!next-input-character;          !!!next-input-character;
1196          redo A;          redo A;
1197        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1198            !!!cp (64);
1199          $before_leave->();          $before_leave->();
1200            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1201          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1202          redo A;          redo A;
1203        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1204          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1205          $before_leave->();          $before_leave->();
1206          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1207            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1208            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1209          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1210            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1211            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1212                !!!cp (67);
1213              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1214              } else {
1215                ## NOTE: This state should never be reached.
1216                !!!cp (68);
1217            }            }
1218          } else {          } else {
1219            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 826  sub _get_next_token ($) { Line 1225  sub _get_next_token ($) {
1225    
1226          redo A;          redo A;
1227        } else {        } else {
1228          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1229                $self->{next_char} == 0x0027) { # '
1230              !!!cp (69);
1231              !!!parse-error (type => 'bad attribute name');
1232            } else {
1233              !!!cp (70);
1234            }
1235            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1236          ## Stay in the state          ## Stay in the state
1237          !!!next-input-character;          !!!next-input-character;
1238          redo A;          redo A;
1239        }        }
1240      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1241        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1242            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1243            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1244            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1245            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1246            !!!cp (71);
1247          ## Stay in the state          ## Stay in the state
1248          !!!next-input-character;          !!!next-input-character;
1249          redo A;          redo A;
1250        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1251            !!!cp (72);
1252          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1253          !!!next-input-character;          !!!next-input-character;
1254          redo A;          redo A;
1255        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1256          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1257            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1258            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1259          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1260            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1261            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1262                !!!cp (74);
1263              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1264              } else {
1265                ## NOTE: This state should never be reached.
1266                !!!cp (75);
1267            }            }
1268          } else {          } else {
1269            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 863  sub _get_next_token ($) { Line 1274  sub _get_next_token ($) {
1274          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1275    
1276          redo A;          redo A;
1277        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1278                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1279          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1280                                value => ''};          $self->{current_attribute}
1281                = {name => chr ($self->{next_char} + 0x0020),
1282                   value => '',
1283                   line => $self->{line}, column => $self->{column}};
1284          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1285          !!!next-input-character;          !!!next-input-character;
1286          redo A;          redo A;
1287        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1288            !!!cp (77);
1289            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1290          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1291          redo A;          redo A;
1292        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1293          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1294          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1295            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1296            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1297          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1298            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1299            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1300                !!!cp (80);
1301              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1302              } else {
1303                ## NOTE: This state should never be reached.
1304                !!!cp (81);
1305            }            }
1306          } else {          } else {
1307            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 905  sub _get_next_token ($) { Line 1313  sub _get_next_token ($) {
1313    
1314          redo A;          redo A;
1315        } else {        } else {
1316          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1317                                value => ''};          $self->{current_attribute}
1318                = {name => chr ($self->{next_char}),
1319                   value => '',
1320                   line => $self->{line}, column => $self->{column}};
1321          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1322          !!!next-input-character;          !!!next-input-character;
1323          redo A;                  redo A;        
1324        }        }
1325      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1326        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1327            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1328            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1329            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1330            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1331            !!!cp (83);
1332          ## Stay in the state          ## Stay in the state
1333          !!!next-input-character;          !!!next-input-character;
1334          redo A;          redo A;
1335        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1336            !!!cp (84);
1337          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1338          !!!next-input-character;          !!!next-input-character;
1339          redo A;          redo A;
1340        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1341            !!!cp (85);
1342          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1343          ## reconsume          ## reconsume
1344          redo A;          redo A;
1345        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1346            !!!cp (86);
1347          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1348          !!!next-input-character;          !!!next-input-character;
1349          redo A;          redo A;
1350        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1351          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1352            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1353            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1354          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1355            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1356            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1357                !!!cp (88);
1358              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1359              } else {
1360                ## NOTE: This state should never be reached.
1361                !!!cp (89);
1362            }            }
1363          } else {          } else {
1364            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 951  sub _get_next_token ($) { Line 1369  sub _get_next_token ($) {
1369          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1370    
1371          redo A;          redo A;
1372        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1373          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1374          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1375            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1376            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1377          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1378            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1379            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1380                !!!cp (91);
1381              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1382              } else {
1383                ## NOTE: This state should never be reached.
1384                !!!cp (92);
1385            }            }
1386          } else {          } else {
1387            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 972  sub _get_next_token ($) { Line 1393  sub _get_next_token ($) {
1393    
1394          redo A;          redo A;
1395        } else {        } else {
1396          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1397              !!!cp (93);
1398              !!!parse-error (type => 'bad attribute value');
1399            } else {
1400              !!!cp (94);
1401            }
1402            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1403          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1404          !!!next-input-character;          !!!next-input-character;
1405          redo A;          redo A;
1406        }        }
1407      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1408        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1409          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (95);
1410            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1411          !!!next-input-character;          !!!next-input-character;
1412          redo A;          redo A;
1413        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1414            !!!cp (96);
1415          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1416          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1417          !!!next-input-character;          !!!next-input-character;
1418          redo A;          redo A;
1419        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1420          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1421          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1422            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1423            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1424          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1425            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1426            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1427                !!!cp (98);
1428              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1429              } else {
1430                ## NOTE: This state should never be reached.
1431                !!!cp (99);
1432            }            }
1433          } else {          } else {
1434            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1008  sub _get_next_token ($) { Line 1440  sub _get_next_token ($) {
1440    
1441          redo A;          redo A;
1442        } else {        } else {
1443          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1444            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1445          ## Stay in the state          ## Stay in the state
1446          !!!next-input-character;          !!!next-input-character;
1447          redo A;          redo A;
1448        }        }
1449      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1450        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1451          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (101);
1452            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1453          !!!next-input-character;          !!!next-input-character;
1454          redo A;          redo A;
1455        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1456            !!!cp (102);
1457          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1458          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1459          !!!next-input-character;          !!!next-input-character;
1460          redo A;          redo A;
1461        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1462          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1463          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1464            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1465            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1466          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1467            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1468            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1469                !!!cp (104);
1470              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1471              } else {
1472                ## NOTE: This state should never be reached.
1473                !!!cp (105);
1474            }            }
1475          } else {          } else {
1476            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1044  sub _get_next_token ($) { Line 1482  sub _get_next_token ($) {
1482    
1483          redo A;          redo A;
1484        } else {        } else {
1485          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1486            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1487          ## Stay in the state          ## Stay in the state
1488          !!!next-input-character;          !!!next-input-character;
1489          redo A;          redo A;
1490        }        }
1491      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1492        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1493            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1494            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1495            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1496            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1497            !!!cp (107);
1498          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1499          !!!next-input-character;          !!!next-input-character;
1500          redo A;          redo A;
1501        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1502            !!!cp (108);
1503          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1504          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1505          !!!next-input-character;          !!!next-input-character;
1506          redo A;          redo A;
1507        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1508          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1509            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1510            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1511          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1512            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1513            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1514                !!!cp (110);
1515              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1516              } else {
1517                ## NOTE: This state should never be reached.
1518                !!!cp (111);
1519            }            }
1520          } else {          } else {
1521            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1082  sub _get_next_token ($) { Line 1526  sub _get_next_token ($) {
1526          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1527    
1528          redo A;          redo A;
1529        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1530          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1531          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1532            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1533            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1534          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1535            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1536            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1537                !!!cp (113);
1538              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1539              } else {
1540                ## NOTE: This state should never be reached.
1541                !!!cp (114);
1542            }            }
1543          } else {          } else {
1544            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1103  sub _get_next_token ($) { Line 1550  sub _get_next_token ($) {
1550    
1551          redo A;          redo A;
1552        } else {        } else {
1553          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1554                 0x0022 => 1, # "
1555                 0x0027 => 1, # '
1556                 0x003D => 1, # =
1557                }->{$self->{next_char}}) {
1558              !!!cp (115);
1559              !!!parse-error (type => 'bad attribute value');
1560            } else {
1561              !!!cp (116);
1562            }
1563            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1564          ## Stay in the state          ## Stay in the state
1565          !!!next-input-character;          !!!next-input-character;
1566          redo A;          redo A;
1567        }        }
1568      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1569        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1570              (1,
1571               $self->{last_attribute_value_state}
1572                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1573               $self->{last_attribute_value_state}
1574                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1575               -1);
1576    
1577        unless (defined $token) {        unless (defined $token) {
1578            !!!cp (117);
1579          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1580        } else {        } else {
1581            !!!cp (118);
1582          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1583            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1584          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1585        }        }
1586    
1587        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1588        # next-input-character is already done        # next-input-character is already done
1589        redo A;        redo A;
1590        } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1591          if ($self->{next_char} == 0x0009 or # HT
1592              $self->{next_char} == 0x000A or # LF
1593              $self->{next_char} == 0x000B or # VT
1594              $self->{next_char} == 0x000C or # FF
1595              $self->{next_char} == 0x0020) { # SP
1596            !!!cp (118);
1597            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1598            !!!next-input-character;
1599            redo A;
1600          } elsif ($self->{next_char} == 0x003E) { # >
1601            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1602              !!!cp (119);
1603              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1604            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1605              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1606              if ($self->{current_token}->{attributes}) {
1607                !!!cp (120);
1608                !!!parse-error (type => 'end tag attribute');
1609              } else {
1610                ## NOTE: This state should never be reached.
1611                !!!cp (121);
1612              }
1613            } else {
1614              die "$0: $self->{current_token}->{type}: Unknown token type";
1615            }
1616            $self->{state} = DATA_STATE;
1617            !!!next-input-character;
1618    
1619            !!!emit ($self->{current_token}); # start tag or end tag
1620    
1621            redo A;
1622          } elsif ($self->{next_char} == 0x002F) { # /
1623            !!!cp (122);
1624            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1625            !!!next-input-character;
1626            redo A;
1627          } else {
1628            !!!cp ('124.1');
1629            !!!parse-error (type => 'no space between attributes');
1630            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1631            ## reconsume
1632            redo A;
1633          }
1634        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1635          if ($self->{next_char} == 0x003E) { # >
1636            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1637              !!!cp ('124.2');
1638              !!!parse-error (type => 'nestc', token => $self->{current_token});
1639              ## TODO: Different type than slash in start tag
1640              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1641              if ($self->{current_token}->{attributes}) {
1642                !!!cp ('124.4');
1643                !!!parse-error (type => 'end tag attribute');
1644              } else {
1645                !!!cp ('124.5');
1646              }
1647              ## TODO: Test |<title></title/>|
1648            } else {
1649              !!!cp ('124.3');
1650              $self->{self_closing} = 1;
1651            }
1652    
1653            $self->{state} = DATA_STATE;
1654            !!!next-input-character;
1655    
1656            !!!emit ($self->{current_token}); # start tag or end tag
1657    
1658            redo A;
1659          } else {
1660            !!!cp ('124.4');
1661            !!!parse-error (type => 'nestc');
1662            ## TODO: This error type is wrong.
1663            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1664            ## Reconsume.
1665            redo A;
1666          }
1667      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1668        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1669                
1670        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1671          #my $token = {type => COMMENT_TOKEN, data => ''};
1672    
1673        BC: {        BC: {
1674          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1675              !!!cp (124);
1676            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1677            !!!next-input-character;            !!!next-input-character;
1678    
1679            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1680    
1681            redo A;            redo A;
1682          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1683              !!!cp (125);
1684            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1685            ## reconsume            ## reconsume
1686    
1687            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1688    
1689            redo A;            redo A;
1690          } else {          } else {
1691            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1692              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1693            !!!next-input-character;            !!!next-input-character;
1694            redo BC;            redo BC;
1695          }          }
1696        } # BC        } # BC
1697    
1698          die "$0: _get_next_token: unexpected case [BC]";
1699      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1700        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1701    
1702          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1703    
1704        my @next_char;        my @next_char;
1705        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1706                
1707        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1708          !!!next-input-character;          !!!next-input-character;
1709          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1710          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1711            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            !!!cp (127);
1712              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1713                                        line => $l, column => $c,
1714                                       };
1715            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1716            !!!next-input-character;            !!!next-input-character;
1717            redo A;            redo A;
1718            } else {
1719              !!!cp (128);
1720          }          }
1721        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1722                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1723          !!!next-input-character;          !!!next-input-character;
1724          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1725          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1726              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1727            !!!next-input-character;            !!!next-input-character;
1728            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1729            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1730                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1731              !!!next-input-character;              !!!next-input-character;
1732              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1733              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1734                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1735                !!!next-input-character;                !!!next-input-character;
1736                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1737                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1738                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1739                  !!!next-input-character;                  !!!next-input-character;
1740                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1741                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1742                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1743                    !!!next-input-character;                    !!!next-input-character;
1744                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1745                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1746                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1747                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1748                        ## TODO: What a stupid code this is!
1749                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1750                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1751                                                  quirks => 1,
1752                                                  line => $l, column => $c,
1753                                                 };
1754                        !!!next-input-character;
1755                        redo A;
1756                      } else {
1757                        !!!cp (130);
1758                      }
1759                    } else {
1760                      !!!cp (131);
1761                    }
1762                  } else {
1763                    !!!cp (132);
1764                  }
1765                } else {
1766                  !!!cp (133);
1767                }
1768              } else {
1769                !!!cp (134);
1770              }
1771            } else {
1772              !!!cp (135);
1773            }
1774          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1775                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1776                   $self->{next_char} == 0x005B) { # [
1777            !!!next-input-character;
1778            push @next_char, $self->{next_char};
1779            if ($self->{next_char} == 0x0043) { # C
1780              !!!next-input-character;
1781              push @next_char, $self->{next_char};
1782              if ($self->{next_char} == 0x0044) { # D
1783                !!!next-input-character;
1784                push @next_char, $self->{next_char};
1785                if ($self->{next_char} == 0x0041) { # A
1786                  !!!next-input-character;
1787                  push @next_char, $self->{next_char};
1788                  if ($self->{next_char} == 0x0054) { # T
1789                    !!!next-input-character;
1790                    push @next_char, $self->{next_char};
1791                    if ($self->{next_char} == 0x0041) { # A
1792                      !!!next-input-character;
1793                      push @next_char, $self->{next_char};
1794                      if ($self->{next_char} == 0x005B) { # [
1795                        !!!cp (135.1);
1796                        $self->{state} = CDATA_BLOCK_STATE;
1797                      !!!next-input-character;                      !!!next-input-character;
1798                      redo A;                      redo A;
1799                      } else {
1800                        !!!cp (135.2);
1801                    }                    }
1802                    } else {
1803                      !!!cp (135.3);
1804                  }                  }
1805                  } else {
1806                    !!!cp (135.4);                
1807                }                }
1808                } else {
1809                  !!!cp (135.5);
1810              }              }
1811              } else {
1812                !!!cp (135.6);
1813            }            }
1814            } else {
1815              !!!cp (135.7);
1816          }          }
1817          } else {
1818            !!!cp (136);
1819        }        }
1820    
1821        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1822        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1823        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1824        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
1825          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1826                                    line => $l, column => $c,
1827                                   };
1828        redo A;        redo A;
1829                
1830        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1831        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
1832      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
1833        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1834            !!!cp (137);
1835          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
1836          !!!next-input-character;          !!!next-input-character;
1837          redo A;          redo A;
1838        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1839            !!!cp (138);
1840          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1841          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1842          !!!next-input-character;          !!!next-input-character;
# Line 1221  sub _get_next_token ($) { Line 1844  sub _get_next_token ($) {
1844          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1845    
1846          redo A;          redo A;
1847        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1848            !!!cp (139);
1849          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1850          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1851          ## reconsume          ## reconsume
# Line 1230  sub _get_next_token ($) { Line 1854  sub _get_next_token ($) {
1854    
1855          redo A;          redo A;
1856        } else {        } else {
1857            !!!cp (140);
1858          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1859              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
1860          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1861          !!!next-input-character;          !!!next-input-character;
1862          redo A;          redo A;
1863        }        }
1864      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1865        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1866            !!!cp (141);
1867          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
1868          !!!next-input-character;          !!!next-input-character;
1869          redo A;          redo A;
1870        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1871            !!!cp (142);
1872          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1873          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1874          !!!next-input-character;          !!!next-input-character;
# Line 1249  sub _get_next_token ($) { Line 1876  sub _get_next_token ($) {
1876          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1877    
1878          redo A;          redo A;
1879        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1880            !!!cp (143);
1881          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1882          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1883          ## reconsume          ## reconsume
# Line 1258  sub _get_next_token ($) { Line 1886  sub _get_next_token ($) {
1886    
1887          redo A;          redo A;
1888        } else {        } else {
1889            !!!cp (144);
1890          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1891              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
1892          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1893          !!!next-input-character;          !!!next-input-character;
1894          redo A;          redo A;
1895        }        }
1896      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
1897        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1898            !!!cp (145);
1899          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
1900          !!!next-input-character;          !!!next-input-character;
1901          redo A;          redo A;
1902        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1903            !!!cp (146);
1904          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1905          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1906          ## reconsume          ## reconsume
# Line 1278  sub _get_next_token ($) { Line 1909  sub _get_next_token ($) {
1909    
1910          redo A;          redo A;
1911        } else {        } else {
1912          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
1913            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1914          ## Stay in the state          ## Stay in the state
1915          !!!next-input-character;          !!!next-input-character;
1916          redo A;          redo A;
1917        }        }
1918      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1919        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1920            !!!cp (148);
1921          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
1922          !!!next-input-character;          !!!next-input-character;
1923          redo A;          redo A;
1924        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1925            !!!cp (149);
1926          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1927          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1928          ## reconsume          ## reconsume
# Line 1297  sub _get_next_token ($) { Line 1931  sub _get_next_token ($) {
1931    
1932          redo A;          redo A;
1933        } else {        } else {
1934          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1935            $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1936          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1937          !!!next-input-character;          !!!next-input-character;
1938          redo A;          redo A;
1939        }        }
1940      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
1941        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1942            !!!cp (151);
1943          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1944          !!!next-input-character;          !!!next-input-character;
1945    
1946          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1947    
1948          redo A;          redo A;
1949        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1950          !!!parse-error (type => 'dash in comment');          !!!cp (152);
1951            !!!parse-error (type => 'dash in comment',
1952                            line => $self->{line_prev},
1953                            column => $self->{column_prev});
1954          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1955          ## Stay in the state          ## Stay in the state
1956          !!!next-input-character;          !!!next-input-character;
1957          redo A;          redo A;
1958        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1959            !!!cp (153);
1960          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1961          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1962          ## reconsume          ## reconsume
# Line 1325  sub _get_next_token ($) { Line 1965  sub _get_next_token ($) {
1965    
1966          redo A;          redo A;
1967        } else {        } else {
1968          !!!parse-error (type => 'dash in comment');          !!!cp (154);
1969          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
1970                            line => $self->{line_prev},
1971                            column => $self->{column_prev});
1972            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1973          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1974          !!!next-input-character;          !!!next-input-character;
1975          redo A;          redo A;
1976        }        }
1977      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
1978        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1979            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1980            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1981            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1982            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1983            !!!cp (155);
1984          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1985          !!!next-input-character;          !!!next-input-character;
1986          redo A;          redo A;
1987        } else {        } else {
1988            !!!cp (156);
1989          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1990          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1991          ## reconsume          ## reconsume
1992          redo A;          redo A;
1993        }        }
1994      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1995        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1996            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1997            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1998            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1999            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2000            !!!cp (157);
2001          ## Stay in the state          ## Stay in the state
2002          !!!next-input-character;          !!!next-input-character;
2003          redo A;          redo A;
2004        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2005            !!!cp (158);
2006          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2007          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2008          !!!next-input-character;          !!!next-input-character;
2009    
2010          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2011    
2012          redo A;          redo A;
2013        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2014            !!!cp (159);
2015          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2016          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2017          ## reconsume          ## reconsume
2018    
2019          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2020    
2021          redo A;          redo A;
2022        } else {        } else {
2023          $self->{current_token}          !!!cp (160);
2024              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
2025                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2026  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2027          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2028          !!!next-input-character;          !!!next-input-character;
# Line 1383  sub _get_next_token ($) { Line 2030  sub _get_next_token ($) {
2030        }        }
2031      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2032  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2033        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2034            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2035            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2036            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2037            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2038            !!!cp (161);
2039          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2040          !!!next-input-character;          !!!next-input-character;
2041          redo A;          redo A;
2042        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2043            !!!cp (162);
2044          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2045          !!!next-input-character;          !!!next-input-character;
2046    
2047          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2048    
2049          redo A;          redo A;
2050        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2051            !!!cp (163);
2052          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2053          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2054          ## reconsume          ## reconsume
2055    
2056          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2057          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2058    
2059          redo A;          redo A;
2060        } else {        } else {
2061            !!!cp (164);
2062          $self->{current_token}->{name}          $self->{current_token}->{name}
2063            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2064          ## Stay in the state          ## Stay in the state
2065          !!!next-input-character;          !!!next-input-character;
2066          redo A;          redo A;
2067        }        }
2068      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2069        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2070            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2071            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2072            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2073            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2074            !!!cp (165);
2075          ## Stay in the state          ## Stay in the state
2076          !!!next-input-character;          !!!next-input-character;
2077          redo A;          redo A;
2078        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2079            !!!cp (166);
2080          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2081          !!!next-input-character;          !!!next-input-character;
2082    
2083          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2084    
2085          redo A;          redo A;
2086        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2087            !!!cp (167);
2088          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2089          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2090          ## reconsume          ## reconsume
2091    
2092          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2093          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2094    
2095          redo A;          redo A;
2096        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2097                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2098          !!!next-input-character;          !!!next-input-character;
2099          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2100              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2101            !!!next-input-character;            !!!next-input-character;
2102            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2103                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2104              !!!next-input-character;              !!!next-input-character;
2105              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2106                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2107                !!!next-input-character;                !!!next-input-character;
2108                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2109                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2110                  !!!next-input-character;                  !!!next-input-character;
2111                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2112                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2113                      !!!cp (168);
2114                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2115                    !!!next-input-character;                    !!!next-input-character;
2116                    redo A;                    redo A;
2117                    } else {
2118                      !!!cp (169);
2119                  }                  }
2120                  } else {
2121                    !!!cp (170);
2122                }                }
2123                } else {
2124                  !!!cp (171);
2125              }              }
2126              } else {
2127                !!!cp (172);
2128            }            }
2129            } else {
2130              !!!cp (173);
2131          }          }
2132    
2133          #          #
2134        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2135                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2136          !!!next-input-character;          !!!next-input-character;
2137          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2138              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2139            !!!next-input-character;            !!!next-input-character;
2140            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2141                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2142              !!!next-input-character;              !!!next-input-character;
2143              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2144                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2145                !!!next-input-character;                !!!next-input-character;
2146                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2147                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2148                  !!!next-input-character;                  !!!next-input-character;
2149                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2150                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2151                      !!!cp (174);
2152                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2153                    !!!next-input-character;                    !!!next-input-character;
2154                    redo A;                    redo A;
2155                    } else {
2156                      !!!cp (175);
2157                  }                  }
2158                  } else {
2159                    !!!cp (176);
2160                }                }
2161                } else {
2162                  !!!cp (177);
2163              }              }
2164              } else {
2165                !!!cp (178);
2166            }            }
2167            } else {
2168              !!!cp (179);
2169          }          }
2170    
2171          #          #
2172        } else {        } else {
2173            !!!cp (180);
2174          !!!next-input-character;          !!!next-input-character;
2175          #          #
2176        }        }
2177    
2178        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2179          $self->{current_token}->{quirks} = 1;
2180    
2181        $self->{state} = BOGUS_DOCTYPE_STATE;        $self->{state} = BOGUS_DOCTYPE_STATE;
2182        # next-input-character is already done        # next-input-character is already done
2183        redo A;        redo A;
# Line 1506  sub _get_next_token ($) { Line 2185  sub _get_next_token ($) {
2185        if ({        if ({
2186              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2187              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2188            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2189            !!!cp (181);
2190          ## Stay in the state          ## Stay in the state
2191          !!!next-input-character;          !!!next-input-character;
2192          redo A;          redo A;
2193        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2194            !!!cp (182);
2195          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2196          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2197          !!!next-input-character;          !!!next-input-character;
2198          redo A;          redo A;
2199        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2200            !!!cp (183);
2201          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2202          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2203          !!!next-input-character;          !!!next-input-character;
2204          redo A;          redo A;
2205        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2206            !!!cp (184);
2207          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2208    
2209          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2210          !!!next-input-character;          !!!next-input-character;
2211    
2212          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2213          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2214    
2215          redo A;          redo A;
2216        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2217            !!!cp (185);
2218          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2219    
2220          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2221          ## reconsume          ## reconsume
2222    
2223          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2224          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2225    
2226          redo A;          redo A;
2227        } else {        } else {
2228            !!!cp (186);
2229          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2230            $self->{current_token}->{quirks} = 1;
2231    
2232          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2233          !!!next-input-character;          !!!next-input-character;
2234          redo A;          redo A;
2235        }        }
2236      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2237        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2238            !!!cp (187);
2239          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2240          !!!next-input-character;          !!!next-input-character;
2241          redo A;          redo A;
2242        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2243            !!!cp (188);
2244            !!!parse-error (type => 'unclosed PUBLIC literal');
2245    
2246            $self->{state} = DATA_STATE;
2247            !!!next-input-character;
2248    
2249            $self->{current_token}->{quirks} = 1;
2250            !!!emit ($self->{current_token}); # DOCTYPE
2251    
2252            redo A;
2253          } elsif ($self->{next_char} == -1) {
2254            !!!cp (189);
2255          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2256    
2257          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2258          ## reconsume          ## reconsume
2259    
2260          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2261          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2262    
2263          redo A;          redo A;
2264        } else {        } else {
2265            !!!cp (190);
2266          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2267              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2268          ## Stay in the state          ## Stay in the state
2269          !!!next-input-character;          !!!next-input-character;
2270          redo A;          redo A;
2271        }        }
2272      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2273        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2274            !!!cp (191);
2275          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2276          !!!next-input-character;          !!!next-input-character;
2277          redo A;          redo A;
2278        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2279            !!!cp (192);
2280            !!!parse-error (type => 'unclosed PUBLIC literal');
2281    
2282            $self->{state} = DATA_STATE;
2283            !!!next-input-character;
2284    
2285            $self->{current_token}->{quirks} = 1;
2286            !!!emit ($self->{current_token}); # DOCTYPE
2287    
2288            redo A;
2289          } elsif ($self->{next_char} == -1) {
2290            !!!cp (193);
2291          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2292    
2293          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2294          ## reconsume          ## reconsume
2295    
2296          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2297          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2298    
2299          redo A;          redo A;
2300        } else {        } else {
2301            !!!cp (194);
2302          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2303              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2304          ## Stay in the state          ## Stay in the state
2305          !!!next-input-character;          !!!next-input-character;
2306          redo A;          redo A;
# Line 1594  sub _get_next_token ($) { Line 2309  sub _get_next_token ($) {
2309        if ({        if ({
2310              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2311              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2312            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2313            !!!cp (195);
2314          ## Stay in the state          ## Stay in the state
2315          !!!next-input-character;          !!!next-input-character;
2316          redo A;          redo A;
2317        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2318            !!!cp (196);
2319          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2320          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2321          !!!next-input-character;          !!!next-input-character;
2322          redo A;          redo A;
2323        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2324            !!!cp (197);
2325          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2326          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2327          !!!next-input-character;          !!!next-input-character;
2328          redo A;          redo A;
2329        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2330            !!!cp (198);
2331          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2332          !!!next-input-character;          !!!next-input-character;
2333    
2334          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2335    
2336          redo A;          redo A;
2337        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2338            !!!cp (199);
2339          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2340    
2341          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2342          ## reconsume          ## reconsume
2343    
2344          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2345          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2346    
2347          redo A;          redo A;
2348        } else {        } else {
2349            !!!cp (200);
2350          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2351            $self->{current_token}->{quirks} = 1;
2352    
2353          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2354          !!!next-input-character;          !!!next-input-character;
2355          redo A;          redo A;
# Line 1635  sub _get_next_token ($) { Line 2358  sub _get_next_token ($) {
2358        if ({        if ({
2359              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2360              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2361            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2362            !!!cp (201);
2363          ## Stay in the state          ## Stay in the state
2364          !!!next-input-character;          !!!next-input-character;
2365          redo A;          redo A;
2366        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2367            !!!cp (202);
2368          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2369          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2370          !!!next-input-character;          !!!next-input-character;
2371          redo A;          redo A;
2372        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2373            !!!cp (203);
2374          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2375          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2376          !!!next-input-character;          !!!next-input-character;
2377          redo A;          redo A;
2378        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2379            !!!cp (204);
2380          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2381          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2382          !!!next-input-character;          !!!next-input-character;
2383    
2384          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2385          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2386    
2387          redo A;          redo A;
2388        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2389            !!!cp (205);
2390          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2391    
2392          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2393          ## reconsume          ## reconsume
2394    
2395          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2396          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2397    
2398          redo A;          redo A;
2399        } else {        } else {
2400            !!!cp (206);
2401          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2402            $self->{current_token}->{quirks} = 1;
2403    
2404          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2405          !!!next-input-character;          !!!next-input-character;
2406          redo A;          redo A;
2407        }        }
2408      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2409        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2410            !!!cp (207);
2411          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2412          !!!next-input-character;          !!!next-input-character;
2413          redo A;          redo A;
2414        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2415            !!!cp (208);
2416            !!!parse-error (type => 'unclosed PUBLIC literal');
2417    
2418            $self->{state} = DATA_STATE;
2419            !!!next-input-character;
2420    
2421            $self->{current_token}->{quirks} = 1;
2422            !!!emit ($self->{current_token}); # DOCTYPE
2423    
2424            redo A;
2425          } elsif ($self->{next_char} == -1) {
2426            !!!cp (209);
2427          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2428    
2429          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2430          ## reconsume          ## reconsume
2431    
2432          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2433          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2434    
2435          redo A;          redo A;
2436        } else {        } else {
2437            !!!cp (210);
2438          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2439              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2440          ## Stay in the state          ## Stay in the state
2441          !!!next-input-character;          !!!next-input-character;
2442          redo A;          redo A;
2443        }        }
2444      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2445        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2446            !!!cp (211);
2447          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2448          !!!next-input-character;          !!!next-input-character;
2449          redo A;          redo A;
2450        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2451            !!!cp (212);
2452            !!!parse-error (type => 'unclosed PUBLIC literal');
2453    
2454            $self->{state} = DATA_STATE;
2455            !!!next-input-character;
2456    
2457            $self->{current_token}->{quirks} = 1;
2458            !!!emit ($self->{current_token}); # DOCTYPE
2459    
2460            redo A;
2461          } elsif ($self->{next_char} == -1) {
2462            !!!cp (213);
2463          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2464    
2465          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2466          ## reconsume          ## reconsume
2467    
2468          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2469          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2470    
2471          redo A;          redo A;
2472        } else {        } else {
2473            !!!cp (214);
2474          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2475              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2476          ## Stay in the state          ## Stay in the state
2477          !!!next-input-character;          !!!next-input-character;
2478          redo A;          redo A;
# Line 1722  sub _get_next_token ($) { Line 2481  sub _get_next_token ($) {
2481        if ({        if ({
2482              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2483              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2484            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2485            !!!cp (215);
2486          ## Stay in the state          ## Stay in the state
2487          !!!next-input-character;          !!!next-input-character;
2488          redo A;          redo A;
2489        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2490            !!!cp (216);
2491          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2492          !!!next-input-character;          !!!next-input-character;
2493    
2494          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2495    
2496          redo A;          redo A;
2497        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2498            !!!cp (217);
2499          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2500    
2501          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2502          ## reconsume          ## reconsume
2503    
2504          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2505          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2506    
2507          redo A;          redo A;
2508        } else {        } else {
2509            !!!cp (218);
2510          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2511            #$self->{current_token}->{quirks} = 1;
2512    
2513          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2514          !!!next-input-character;          !!!next-input-character;
2515          redo A;          redo A;
2516        }        }
2517      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2518        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2519            !!!cp (219);
2520          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2521          !!!next-input-character;          !!!next-input-character;
2522    
         delete $self->{current_token}->{correct};  
2523          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2524    
2525          redo A;          redo A;
2526        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2527            !!!cp (220);
2528          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2529          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2530          ## reconsume          ## reconsume
2531    
         delete $self->{current_token}->{correct};  
2532          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2533    
2534          redo A;          redo A;
2535        } else {        } else {
2536            !!!cp (221);
2537          ## Stay in the state          ## Stay in the state
2538          !!!next-input-character;          !!!next-input-character;
2539          redo A;          redo A;
2540        }        }
2541        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2542          my $s = '';
2543          
2544          my ($l, $c) = ($self->{line}, $self->{column});
2545    
2546          CS: while ($self->{next_char} != -1) {
2547            if ($self->{next_char} == 0x005D) { # ]
2548              !!!next-input-character;
2549              if ($self->{next_char} == 0x005D) { # ]
2550                !!!next-input-character;
2551                MDC: {
2552                  if ($self->{next_char} == 0x003E) { # >
2553                    !!!cp (221.1);
2554                    !!!next-input-character;
2555                    last CS;
2556                  } elsif ($self->{next_char} == 0x005D) { # ]
2557                    !!!cp (221.2);
2558                    $s .= ']';
2559                    !!!next-input-character;
2560                    redo MDC;
2561                  } else {
2562                    !!!cp (221.3);
2563                    $s .= ']]';
2564                    #
2565                  }
2566                } # MDC
2567              } else {
2568                !!!cp (221.4);
2569                $s .= ']';
2570                #
2571              }
2572            } else {
2573              !!!cp (221.5);
2574              #
2575            }
2576            $s .= chr $self->{next_char};
2577            !!!next-input-character;
2578          } # CS
2579    
2580          $self->{state} = DATA_STATE;
2581          ## next-input-character done or EOF, which is reconsumed.
2582    
2583          if (length $s) {
2584            !!!cp (221.6);
2585            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2586                      line => $l, column => $c});
2587          } else {
2588            !!!cp (221.7);
2589          }
2590    
2591          redo A;
2592    
2593          ## ISSUE: "text tokens" in spec.
2594          ## TODO: Streaming support
2595      } else {      } else {
2596        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2597      }      }
# Line 1780  sub _get_next_token ($) { Line 2600  sub _get_next_token ($) {
2600    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2601  } # _get_next_token  } # _get_next_token
2602    
2603  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2604    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2605    
2606      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2607    
2608    if ({    if ({
2609         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2610         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2611        }->{$self->{next_input_character}}) {         $additional => 1,
2612          }->{$self->{next_char}}) {
2613        !!!cp (1001);
2614      ## Don't consume      ## Don't consume
2615      ## No error      ## No error
2616      return undef;      return undef;
2617    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2618      !!!next-input-character;      !!!next-input-character;
2619      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2620          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2621        my $code;        my $code;
2622        X: {        X: {
2623          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2624          !!!next-input-character;          !!!next-input-character;
2625          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2626              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2627              !!!cp (1002);
2628            $code ||= 0;            $code ||= 0;
2629            $code *= 0x10;            $code *= 0x10;
2630            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2631            redo X;            redo X;
2632          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2633                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2634              !!!cp (1003);
2635            $code ||= 0;            $code ||= 0;
2636            $code *= 0x10;            $code *= 0x10;
2637            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2638            redo X;            redo X;
2639          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2640                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2641              !!!cp (1004);
2642            $code ||= 0;            $code ||= 0;
2643            $code *= 0x10;            $code *= 0x10;
2644            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2645            redo X;            redo X;
2646          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2647            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2648            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2649            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2650              $self->{next_char} = 0x0023; # #
2651            return undef;            return undef;
2652          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2653              !!!cp (1006);
2654            !!!next-input-character;            !!!next-input-character;
2655          } else {          } else {
2656            !!!parse-error (type => 'no refc');            !!!cp (1007);
2657              !!!parse-error (type => 'no refc', line => $l, column => $c);
2658          }          }
2659    
2660          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2661            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2662              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2663            $code = 0xFFFD;            $code = 0xFFFD;
2664          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2665            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2666              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2667            $code = 0xFFFD;            $code = 0xFFFD;
2668          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2669            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2670              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2671            $code = 0x000A;            $code = 0x000A;
2672          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2673            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2674              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2675            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2676          }          }
2677    
2678          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2679                    has_reference => 1,
2680                    line => $l, column => $c,
2681                   };
2682        } # X        } # X
2683      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2684               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2685        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2686        !!!next-input-character;        !!!next-input-character;
2687                
2688        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2689                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2690            !!!cp (1012);
2691          $code *= 10;          $code *= 10;
2692          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2693                    
2694          !!!next-input-character;          !!!next-input-character;
2695        }        }
2696    
2697        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2698            !!!cp (1013);
2699          !!!next-input-character;          !!!next-input-character;
2700        } else {        } else {
2701          !!!parse-error (type => 'no refc');          !!!cp (1014);
2702            !!!parse-error (type => 'no refc', line => $l, column => $c);
2703        }        }
2704    
2705        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2706          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2707            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2708          $code = 0xFFFD;          $code = 0xFFFD;
2709        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2710          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2711            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2712          $code = 0xFFFD;          $code = 0xFFFD;
2713        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2714          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2715            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2716          $code = 0x000A;          $code = 0x000A;
2717        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2718          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2719            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2720          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2721        }        }
2722                
2723        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2724                  line => $l, column => $c,
2725                 };
2726      } else {      } else {
2727        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2728        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2729        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2730          $self->{next_char} = 0x0023; # #
2731        return undef;        return undef;
2732      }      }
2733    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2734              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2735             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2736              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2737      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2738      !!!next-input-character;      !!!next-input-character;
2739    
2740      my $value = $entity_name;      my $value = $entity_name;
# Line 1895  sub _tokenize_attempt_to_consume_an_enti Line 2742  sub _tokenize_attempt_to_consume_an_enti
2742      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2743      our $EntityChar;      our $EntityChar;
2744    
2745      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2746             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2747             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2748               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2749              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2750               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2751              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2752               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2753              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2754        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2755        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2756          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2757              !!!cp (1020);
2758            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2759            $match = 1;            $match = 1;
2760            !!!next-input-character;            !!!next-input-character;
2761            last;            last;
2762          } else {          } else {
2763              !!!cp (1021);
2764            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2765            $match = -1;            $match = -1;
2766            !!!next-input-character;            !!!next-input-character;
2767          }          }
2768        } else {        } else {
2769          $value .= chr $self->{next_input_character};          !!!cp (1022);
2770            $value .= chr $self->{next_char};
2771          $match *= 2;          $match *= 2;
2772          !!!next-input-character;          !!!next-input-character;
2773        }        }
2774      }      }
2775            
2776      if ($match > 0) {      if ($match > 0) {
2777        return {type => CHARACTER_TOKEN, data => $value};        !!!cp (1023);
2778          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2779                  line => $l, column => $c,
2780                 };
2781      } elsif ($match < 0) {      } elsif ($match < 0) {
2782        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2783        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2784          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          !!!cp (1024);
2785        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2786          return {type => CHARACTER_TOKEN, data => $value};                  line => $l, column => $c,
2787                   };
2788          } else {
2789            !!!cp (1025);
2790            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2791                    line => $l, column => $c,
2792                   };
2793        }        }
2794      } else {      } else {
2795        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2796        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2797        return {type => CHARACTER_TOKEN, data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
2798          return {type => CHARACTER_TOKEN, data => '&'.$value,
2799                  line => $l, column => $c,
2800                 };
2801      }      }
2802    } else {    } else {
2803        !!!cp (1027);
2804      ## no characters are consumed      ## no characters are consumed
2805      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2806      return undef;      return undef;
2807    }    }
2808  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1977  sub _construct_tree ($) { Line 2840  sub _construct_tree ($) {
2840        
2841    !!!next-token;    !!!next-token;
2842    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
2843    undef $self->{form_element};    undef $self->{form_element};
2844    undef $self->{head_element};    undef $self->{head_element};
2845    $self->{open_elements} = [];    $self->{open_elements} = [];
2846    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2847    
2848      ## NOTE: The "initial" insertion mode.
2849    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2850    
2851      ## NOTE: The "before html" insertion mode.
2852    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2853      $self->{insertion_mode} = BEFORE_HEAD_IM;
2854    
2855      ## NOTE: The "before head" insertion mode and so on.
2856    $self->_tree_construction_main;    $self->_tree_construction_main;
2857  } # _construct_tree  } # _construct_tree
2858    
2859  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2860    my $self = shift;    my $self = shift;
2861    
2862      ## NOTE: "initial" insertion mode
2863    
2864    INITIAL: {    INITIAL: {
2865      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
2866        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2001  sub _tree_construction_initial ($) { Line 2872  sub _tree_construction_initial ($) {
2872        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
2873            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2874            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2875          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
2876            !!!parse-error (type => 'not HTML5', token => $token);
2877        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2878            !!!cp ('t2');
2879          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2880          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2881          } else {
2882            !!!cp ('t3');
2883        }        }
2884                
2885        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
2886          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2887          ## NOTE: Default value for both |public_id| and |system_id| attributes
2888          ## are empty strings, so that we don't set any value in missing cases.
2889        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
2890            if defined $token->{public_identifier};            if defined $token->{public_identifier};
2891        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2017  sub _tree_construction_initial ($) { Line 2894  sub _tree_construction_initial ($) {
2894        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
2895        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2896                
2897        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2898            !!!cp ('t4');
2899          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2900        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2901          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 2071  sub _tree_construction_initial ($) { Line 2949  sub _tree_construction_initial ($) {
2949            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2950            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2951            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2952              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2953              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2954              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2955            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2956            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2957            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 2093  sub _tree_construction_initial ($) { Line 2974  sub _tree_construction_initial ($) {
2974            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2975            "HTML" => 1,            "HTML" => 1,
2976          }->{$pubid}) {          }->{$pubid}) {
2977              !!!cp ('t5');
2978            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2979          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2980                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2981            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
2982                !!!cp ('t6');
2983              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
2984            } else {            } else {
2985                !!!cp ('t7');
2986              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
2987            }            }
2988          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2989                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2990              !!!cp ('t8');
2991            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
2992            } else {
2993              !!!cp ('t9');
2994          }          }
2995          } else {
2996            !!!cp ('t10');
2997        }        }
2998        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
2999          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3000          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3001          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") {
3002              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
3003            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3004              !!!cp ('t11');
3005            } else {
3006              !!!cp ('t12');
3007          }          }
3008          } else {
3009            !!!cp ('t13');
3010        }        }
3011                
3012        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3013        !!!next-token;        !!!next-token;
3014        return;        return;
3015      } elsif ({      } elsif ({
# Line 2122  sub _tree_construction_initial ($) { Line 3017  sub _tree_construction_initial ($) {
3017                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3018                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3019               }->{$token->{type}}) {               }->{$token->{type}}) {
3020        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3021          !!!parse-error (type => 'no DOCTYPE', token => $token);
3022        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3023        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3024        ## reprocess        ## reprocess
3025          !!!ack-later;
3026        return;        return;
3027      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3028        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3029          ## Ignore the token          ## Ignore the token
3030    
3031          unless (length $token->{data}) {          unless (length $token->{data}) {
3032            ## Stay in the phase            !!!cp ('t15');
3033              ## Stay in the insertion mode.
3034            !!!next-token;            !!!next-token;
3035            redo INITIAL;            redo INITIAL;
3036            } else {
3037              !!!cp ('t16');
3038          }          }
3039          } else {
3040            !!!cp ('t17');
3041        }        }
3042    
3043        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3044        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3045        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3046        ## reprocess        ## reprocess
3047        return;        return;
3048      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3049          !!!cp ('t18');
3050        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3051        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3052                
3053        ## Stay in the phase.        ## Stay in the insertion mode.
3054        !!!next-token;        !!!next-token;
3055        redo INITIAL;        redo INITIAL;
3056      } else {      } else {
3057        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3058      }      }
3059    } # INITIAL    } # INITIAL
3060    
3061      die "$0: _tree_construction_initial: This should be never reached";
3062  } # _tree_construction_initial  } # _tree_construction_initial
3063    
3064  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3065    my $self = shift;    my $self = shift;
3066    
3067      ## NOTE: "before html" insertion mode.
3068        
3069    B: {    B: {
3070        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3071          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3072            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3073          ## Ignore the token          ## Ignore the token
3074          ## Stay in the phase          ## Stay in the insertion mode.
3075          !!!next-token;          !!!next-token;
3076          redo B;          redo B;
3077        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3078            !!!cp ('t20');
3079          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3080          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3081          ## Stay in the phase          ## Stay in the insertion mode.
3082          !!!next-token;          !!!next-token;
3083          redo B;          redo B;
3084        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2177  sub _tree_construction_root_element ($) Line 3086  sub _tree_construction_root_element ($)
3086            ## Ignore the token.            ## Ignore the token.
3087    
3088            unless (length $token->{data}) {            unless (length $token->{data}) {
3089              ## Stay in the phase              !!!cp ('t21');
3090                ## Stay in the insertion mode.
3091              !!!next-token;              !!!next-token;
3092              redo B;              redo B;
3093              } else {
3094                !!!cp ('t22');
3095            }            }
3096            } else {
3097              !!!cp ('t23');
3098          }          }
3099    
3100          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
3101    
3102          #          #
3103        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3104          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3105              $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"            my $root_element;
3106            $self->{application_cache_selection}            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3107                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
3108            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
3109                  [$root_element, $el_category->{html}];
3110    
3111              if ($token->{attributes}->{manifest}) {
3112                !!!cp ('t24');
3113                $self->{application_cache_selection}
3114                    ->($token->{attributes}->{manifest}->{value});
3115                ## ISSUE: Spec is unclear on relative references.
3116                ## According to Hixie (#whatwg 2008-03-19), it should be
3117                ## resolved against the base URI of the document in HTML
3118                ## or xml:base of the element in XHTML.
3119              } else {
3120                !!!cp ('t25');
3121                $self->{application_cache_selection}->(undef);
3122              }
3123    
3124              !!!nack ('t25c');
3125    
3126              !!!next-token;
3127              return; ## Go to the "before head" insertion mode.
3128          } else {          } else {
3129            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
3130              #
3131          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3132        } elsif ({        } elsif ({
3133                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3134                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3135                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3136          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
3137          #          #
3138        } else {        } else {
3139          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3140        }        }
3141    
3142        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3143        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3144        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3145        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3146        #redo B;  
3147        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3148    
3149        ## NOTE: Reprocess the token.
3150        !!!ack-later;
3151        return; ## Go to the "before head" insertion mode.
3152    
3153        ## ISSUE: There is an issue in the spec
3154    } # B    } # B
3155    
3156      die "$0: _tree_construction_root_element: This should never be reached";
3157  } # _tree_construction_root_element  } # _tree_construction_root_element
3158    
3159  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2231  sub _reset_insertion_mode ($) { Line 3168  sub _reset_insertion_mode ($) {
3168            
3169      ## Step 3      ## Step 3
3170      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"!?  
3171        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3172          $last = 1;          $last = 1;
3173          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3174            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
3175                $self->{inner_html_node}->[1] eq 'th') {              !!!cp ('t27');
3176              #              #
3177            } else {            } else {
3178                !!!cp ('t28');
3179              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
3180            }            }
3181          }          }
3182        }        }
3183            
3184        ## Step 4..13      ## Step 4..14
3185        my $new_mode = {      my $new_mode;
3186        if ($node->[1] & FOREIGN_EL) {
3187          ## NOTE: Strictly spaking, the line below only applies to MathML and
3188          ## SVG elements.  Currently the HTML syntax supports only MathML and
3189          ## SVG elements as foreigners.
3190          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3191          ## ISSUE: What is set as the secondary insertion mode?
3192        } else {
3193          $new_mode = {
3194                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3195                          ## NOTE: |option| and |optgroup| do not set
3196                          ## insertion mode to "in select" by themselves.
3197                        td => IN_CELL_IM,                        td => IN_CELL_IM,
3198                        th => IN_CELL_IM,                        th => IN_CELL_IM,
3199                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2263  sub _reset_insertion_mode ($) { Line 3206  sub _reset_insertion_mode ($) {
3206                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3207                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3208                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3209                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3210        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3211        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3212                
3213        ## Step 14        ## Step 15
3214        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3215          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3216              !!!cp ('t29');
3217            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3218          } else {          } else {
3219              ## ISSUE: Can this state be reached?
3220              !!!cp ('t30');
3221            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3222          }          }
3223          return;          return;
3224          } else {
3225            !!!cp ('t31');
3226        }        }
3227                
3228        ## Step 15        ## Step 16
3229        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3230                
3231        ## Step 16        ## Step 17
3232        $i--;        $i--;
3233        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3234                
3235        ## Step 17        ## Step 18
3236        redo S3;        redo S3;
3237      } # S3      } # S3
3238    
3239      die "$0: _reset_insertion_mode: This line should never be reached";
3240  } # _reset_insertion_mode  } # _reset_insertion_mode
3241    
3242  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2307  sub _tree_construction_main ($) { Line 3258  sub _tree_construction_main ($) {
3258      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3259      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3260        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3261            !!!cp ('t32');
3262          return;          return;
3263        }        }
3264      }      }
# Line 2321  sub _tree_construction_main ($) { Line 3273  sub _tree_construction_main ($) {
3273    
3274        ## Step 6        ## Step 6
3275        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3276            !!!cp ('t33_1');
3277          #          #
3278        } else {        } else {
3279          my $in_open_elements;          my $in_open_elements;
3280          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3281            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3282                !!!cp ('t33');
3283              $in_open_elements = 1;              $in_open_elements = 1;
3284              last OE;              last OE;
3285            }            }
3286          }          }
3287          if ($in_open_elements) {          if ($in_open_elements) {
3288              !!!cp ('t34');
3289            #            #
3290          } else {          } else {
3291              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3292              !!!cp ('t35');
3293            redo S4;            redo S4;
3294          }          }
3295        }        }
# Line 2355  sub _tree_construction_main ($) { Line 3312  sub _tree_construction_main ($) {
3312    
3313        ## Step 11        ## Step 11
3314        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3315            !!!cp ('t36');
3316          ## Step 7'          ## Step 7'
3317          $i++;          $i++;
3318          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3319                    
3320          redo S7;          redo S7;
3321        }        }
3322    
3323          !!!cp ('t37');
3324      } # S7      } # S7
3325    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3326    
3327    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3328      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3329        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3330            !!!cp ('t38');
3331          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3332          return;          return;
3333        }        }
3334      }      }
3335    
3336        !!!cp ('t39');
3337    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3338    
3339    my $parse_rcdata = sub ($$) {    my $insert;
3340      my ($content_model_flag, $insert) = @_;  
3341      my $parse_rcdata = sub ($) {
3342        my ($content_model_flag) = @_;
3343    
3344      ## Step 1      ## Step 1
3345      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3346      my $el;      my $el;
3347      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3348    
3349      ## Step 2      ## Step 2
3350      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3351    
3352      ## Step 3      ## Step 3
3353      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2390  sub _tree_construction_main ($) { Line 3355  sub _tree_construction_main ($) {
3355    
3356      ## Step 4      ## Step 4
3357      my $text = '';      my $text = '';
3358        !!!nack ('t40.1');
3359      !!!next-token;      !!!next-token;
3360      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3361          !!!cp ('t40');
3362        $text .= $token->{data};        $text .= $token->{data};
3363        !!!next-token;        !!!next-token;
3364      }      }
3365    
3366      ## Step 5      ## Step 5
3367      if (length $text) {      if (length $text) {
3368          !!!cp ('t41');
3369        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3370        $el->append_child ($text);        $el->append_child ($text);
3371      }      }
# Line 2406  sub _tree_construction_main ($) { Line 3374  sub _tree_construction_main ($) {
3374      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3375    
3376      ## Step 7      ## Step 7
3377      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3378            $token->{tag_name} eq $start_tag_name) {
3379          !!!cp ('t42');
3380        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3381      } else {      } else {
3382        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3383          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3384            !!!cp ('t43');
3385            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3386          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3387            !!!cp ('t44');
3388            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3389          } else {
3390            die "$0: $content_model_flag in parse_rcdata";
3391          }
3392      }      }
3393      !!!next-token;      !!!next-token;
3394    }; # $parse_rcdata    }; # $parse_rcdata
3395    
3396    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3397      my $script_el;      my $script_el;
3398      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3399      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3400    
3401      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3402      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3403            
3404      my $text = '';      my $text = '';
3405        !!!nack ('t45.1');
3406      !!!next-token;      !!!next-token;
3407      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3408          !!!cp ('t45');
3409        $text .= $token->{data};        $text .= $token->{data};
3410        !!!next-token;        !!!next-token;
3411      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3412      if (length $text) {      if (length $text) {
3413          !!!cp ('t46');
3414        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3415      }      }
3416                                
# Line 2441  sub _tree_construction_main ($) { Line 3418  sub _tree_construction_main ($) {
3418    
3419      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3420          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3421          !!!cp ('t47');
3422        ## Ignore the token        ## Ignore the token
3423      } else {      } else {
3424        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3425          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3426        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3427        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3428      }      }
3429            
3430      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3431          !!!cp ('t49');
3432        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3433      } else {      } else {
3434          !!!cp ('t50');
3435        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3436        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3437    
# Line 2464  sub _tree_construction_main ($) { Line 3445  sub _tree_construction_main ($) {
3445      !!!next-token;      !!!next-token;
3446    }; # $script_start_tag    }; # $script_start_tag
3447    
3448      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3449      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3450      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3451    
3452    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3453      my $tag_name = shift;      my $end_tag_token = shift;
3454        my $tag_name = $end_tag_token->{tag_name};
3455    
3456        ## NOTE: The adoption agency algorithm (AAA).
3457    
3458      FET: {      FET: {
3459        ## Step 1        ## Step 1
3460        my $formatting_element;        my $formatting_element;
3461        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3462        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3463          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3464              !!!cp ('t52');
3465              last AFE;
3466            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3467                         eq $tag_name) {
3468              !!!cp ('t51');
3469            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3470            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3471            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3472          }          }
3473        } # AFE        } # AFE
3474        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3475          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3476            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3477          ## Ignore the token          ## Ignore the token
3478          !!!next-token;          !!!next-token;
3479          return;          return;
# Line 2493  sub _tree_construction_main ($) { Line 3485  sub _tree_construction_main ($) {
3485          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3486          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3487            if ($in_scope) {            if ($in_scope) {
3488                !!!cp ('t54');
3489              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3490              last INSCOPE;              last INSCOPE;
3491            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3492              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3493                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3494                                token => $end_tag_token);
3495              ## Ignore the token              ## Ignore the token
3496              !!!next-token;              !!!next-token;
3497              return;              return;
3498            }            }
3499          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3500                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3501            $in_scope = 0;            $in_scope = 0;
3502          }          }
3503        } # INSCOPE        } # INSCOPE
3504        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3505          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3506            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3507                            token => $end_tag_token);
3508          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3509          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3510          return;          return;
3511        }        }
3512        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3513          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3514            !!!parse-error (type => 'not closed',
3515                            value => $self->{open_elements}->[-1]->[0]
3516                                ->manakai_local_name,
3517                            token => $end_tag_token);
3518        }        }
3519                
3520        ## Step 2        ## Step 2
# Line 2523  sub _tree_construction_main ($) { Line 3522  sub _tree_construction_main ($) {
3522        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3523        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3524          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3525          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3526              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3527              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3528               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3529              !!!cp ('t59');
3530            $furthest_block = $node;            $furthest_block = $node;
3531            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3532          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3533              !!!cp ('t60');
3534            last OE;            last OE;
3535          }          }
3536        } # OE        } # OE
3537                
3538        ## Step 3        ## Step 3
3539        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3540            !!!cp ('t61');
3541          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3542          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3543          !!!next-token;          !!!next-token;
# Line 2548  sub _tree_construction_main ($) { Line 3550  sub _tree_construction_main ($) {
3550        ## Step 5        ## Step 5
3551        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3552        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3553            !!!cp ('t62');
3554          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3555        }        }
3556                
# Line 2570  sub _tree_construction_main ($) { Line 3573  sub _tree_construction_main ($) {
3573          S7S2: {          S7S2: {
3574            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3575              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3576                  !!!cp ('t63');
3577                $node_i_in_active = $_;                $node_i_in_active = $_;
3578                last S7S2;                last S7S2;
3579              }              }
# Line 2583  sub _tree_construction_main ($) { Line 3587  sub _tree_construction_main ($) {
3587                    
3588          ## Step 4          ## Step 4
3589          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3590              !!!cp ('t64');
3591            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3592          }          }
3593                    
3594          ## Step 5          ## Step 5
3595          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3596              !!!cp ('t65');
3597            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3598            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3599            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2605  sub _tree_construction_main ($) { Line 3611  sub _tree_construction_main ($) {
3611        } # S7          } # S7  
3612                
3613        ## Step 8        ## Step 8
3614        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3615            my $foster_parent_element;
3616            my $next_sibling;
3617            OE: for (reverse 0..$#{$self->{open_elements}}) {
3618              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3619                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3620                                 if (defined $parent and $parent->node_type == 1) {
3621                                   !!!cp ('t65.1');
3622                                   $foster_parent_element = $parent;
3623                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3624                                 } else {
3625                                   !!!cp ('t65.2');
3626                                   $foster_parent_element
3627                                     = $self->{open_elements}->[$_ - 1]->[0];
3628                                 }
3629                                 last OE;
3630                               }
3631                             } # OE
3632                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3633                               unless defined $foster_parent_element;
3634            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3635            $open_tables->[-1]->[1] = 1; # tainted
3636          } else {
3637            !!!cp ('t65.3');
3638            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3639          }
3640                
3641        ## Step 9        ## Step 9
3642        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2622  sub _tree_construction_main ($) { Line 3653  sub _tree_construction_main ($) {
3653        my $i;        my $i;
3654        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3655          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3656              !!!cp ('t66');
3657            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3658            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3659          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3660              !!!cp ('t67');
3661            $i = $_;            $i = $_;
3662          }          }
3663        } # AFE        } # AFE
# Line 2634  sub _tree_construction_main ($) { Line 3667  sub _tree_construction_main ($) {
3667        undef $i;        undef $i;
3668        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3669          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3670              !!!cp ('t68');
3671            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3672            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3673          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3674              !!!cp ('t69');
3675            $i = $_;            $i = $_;
3676          }          }
3677        } # OE        } # OE
# Line 2647  sub _tree_construction_main ($) { Line 3682  sub _tree_construction_main ($) {
3682      } # FET      } # FET
3683    }; # $formatting_end_tag    }; # $formatting_end_tag
3684    
3685    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3686      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3687    }; # $insert_to_current    }; # $insert_to_current
3688    
3689    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3690                         my $child = shift;      my $child = shift;
3691                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3692                              table => 1, tbody => 1, tfoot => 1,        # MUST
3693                              thead => 1, tr => 1,        my $foster_parent_element;
3694                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3695                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3696                           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') {  
3697                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3698                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3699                                   !!!cp ('t70');
3700                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3701                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3702                               } else {                               } else {
3703                                   !!!cp ('t71');
3704                                 $foster_parent_element                                 $foster_parent_element
3705                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3706                               }                               }
# Line 2677  sub _tree_construction_main ($) { Line 3711  sub _tree_construction_main ($) {
3711                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3712                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3713                             ($child, $next_sibling);                             ($child, $next_sibling);
3714                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3715                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3716                         }        !!!cp ('t72');
3717          $self->{open_elements}->[-1]->[0]->append_child ($child);
3718        }
3719    }; # $insert_to_foster    }; # $insert_to_foster
3720    
3721    my $insert;    B: while (1) {
   
   B: {  
3722      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3723        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
3724          !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3725        ## Ignore the token        ## Ignore the token
3726        ## Stay in the phase        ## Stay in the phase
3727        !!!next-token;        !!!next-token;
3728        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3729      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3730               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3731        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3732          ## Turn into the main phase          !!!cp ('t79');
3733          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3734          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3735        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3736          ## Turn into the main phase          !!!cp ('t80');
3737          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3738          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3739          } else {
3740            !!!cp ('t81');
3741        }        }
3742    
3743  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3744  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag', token => $token);
       unless ($token->{first_start_tag}) {  
         !!!parse-error (type => 'not first start tag');  
       }  
3745        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3746        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3747          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3748              !!!cp ('t84');
3749            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3750              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3751               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3752          }          }
3753        }        }
3754          !!!nack ('t84.1');
3755        !!!next-token;        !!!next-token;
3756        redo B;        next B;
3757      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3758        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3759        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3760            !!!cp ('t85');
3761          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3762        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3763            !!!cp ('t86');
3764          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3765        } else {        } else {
3766            !!!cp ('t87');
3767          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3768        }        }
3769        !!!next-token;        !!!next-token;
3770        redo B;        next B;
3771      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3772          if ($token->{type} == CHARACTER_TOKEN) {
3773            !!!cp ('t87.1');
3774            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3775            !!!next-token;
3776            next B;
3777          } elsif ($token->{type} == START_TAG_TOKEN) {
3778            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3779                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3780                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3781                ($token->{tag_name} eq 'svg' and
3782                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3783              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3784              !!!cp ('t87.2');
3785              #
3786            } elsif ({
3787                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3788                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3789                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3790                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3791                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3792                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3793                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3794                      var => 1,
3795                     }->{$token->{tag_name}}) {
3796              !!!cp ('t87.2');
3797              !!!parse-error (type => 'not closed',
3798                              value => $self->{open_elements}->[-1]->[0]
3799                                  ->manakai_local_name,
3800                              token => $token);
3801    
3802              pop @{$self->{open_elements}}
3803                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3804    
3805              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3806              ## Reprocess.
3807              next B;
3808            } else {
3809              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3810              my $tag_name = $token->{tag_name};
3811              if ($nsuri eq $SVG_NS) {
3812                $tag_name = {
3813                   altglyph => 'altGlyph',
3814                   altglyphdef => 'altGlyphDef',
3815                   altglyphitem => 'altGlyphItem',
3816                   animatecolor => 'animateColor',
3817                   animatemotion => 'animateMotion',
3818                   animatetransform => 'animateTransform',
3819                   clippath => 'clipPath',
3820                   feblend => 'feBlend',
3821                   fecolormatrix => 'feColorMatrix',
3822                   fecomponenttransfer => 'feComponentTransfer',
3823                   fecomposite => 'feComposite',
3824                   feconvolvematrix => 'feConvolveMatrix',
3825                   fediffuselighting => 'feDiffuseLighting',
3826                   fedisplacementmap => 'feDisplacementMap',
3827                   fedistantlight => 'feDistantLight',
3828                   feflood => 'feFlood',
3829                   fefunca => 'feFuncA',
3830                   fefuncb => 'feFuncB',
3831                   fefuncg => 'feFuncG',
3832                   fefuncr => 'feFuncR',
3833                   fegaussianblur => 'feGaussianBlur',
3834                   feimage => 'feImage',
3835                   femerge => 'feMerge',
3836                   femergenode => 'feMergeNode',
3837                   femorphology => 'feMorphology',
3838                   feoffset => 'feOffset',
3839                   fepointlight => 'fePointLight',
3840                   fespecularlighting => 'feSpecularLighting',
3841                   fespotlight => 'feSpotLight',
3842                   fetile => 'feTile',
3843                   feturbulence => 'feTurbulence',
3844                   foreignobject => 'foreignObject',
3845                   glyphref => 'glyphRef',
3846                   lineargradient => 'linearGradient',
3847                   radialgradient => 'radialGradient',
3848                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
3849                   textpath => 'textPath',  
3850                }->{$tag_name} || $tag_name;
3851              }
3852    
3853              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
3854    
3855              ## "adjust foreign attributes" - done in insert-element-f
3856    
3857              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
3858    
3859              if ($self->{self_closing}) {
3860                pop @{$self->{open_elements}};
3861                !!!ack ('t87.3');
3862              } else {
3863                !!!cp ('t87.4');
3864              }
3865    
3866              !!!next-token;
3867              next B;
3868            }
3869          } elsif ($token->{type} == END_TAG_TOKEN) {
3870            ## NOTE: "using the rules for secondary insertion mode" then "continue"
3871            !!!cp ('t87.5');
3872            #
3873          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3874            ## NOTE: "using the rules for secondary insertion mode" then "continue"
3875            !!!cp ('t87.6');
3876            #
3877            ## TODO: ...
3878          } else {
3879            die "$0: $token->{type}: Unknown token type";        
3880          }
3881        }
3882    
3883        if ($self->{insertion_mode} & HEAD_IMS) {
3884        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3885          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3886            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3887                !!!cp ('t88.2');
3888                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3889              } else {
3890                !!!cp ('t88.1');
3891                ## Ignore the token.
3892                !!!next-token;
3893                next B;
3894              }
3895            unless (length $token->{data}) {            unless (length $token->{data}) {
3896                !!!cp ('t88');
3897              !!!next-token;              !!!next-token;
3898              redo B;              next B;
3899            }            }
3900          }          }
3901    
3902          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3903              !!!cp ('t89');
3904            ## As if <head>            ## As if <head>
3905            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
3906            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3907            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
3908                  [$self->{head_element}, $el_category->{head}];
3909    
3910            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3911            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3912    
3913            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3914          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3915              !!!cp ('t90');
3916            ## As if </noscript>            ## As if </noscript>
3917            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3918            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
3919                        
3920            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3921            ## As if </head>            ## As if </head>
# Line 2788  sub _tree_construction_main ($) { Line 3923  sub _tree_construction_main ($) {
3923    
3924            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3925          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3926              !!!cp ('t91');
3927            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3928    
3929            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3930            } else {
3931              !!!cp ('t92');
3932          }          }
3933    
3934              ## "after head" insertion mode          ## "after head" insertion mode
3935              ## As if <body>          ## As if <body>
3936              !!!insert-element ('body');          !!!insert-element ('body',, $token);
3937              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
3938              ## reprocess          ## reprocess
3939              redo B;          next B;
3940            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3941              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
3942                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3943                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
3944                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3945                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
3946                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
3947                  !!!next-token;              push @{$self->{open_elements}},
3948                  redo B;                  [$self->{head_element}, $el_category->{head}];
3949                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
3950                  #              !!!nack ('t93.1');
3951                } else {              !!!next-token;
3952                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
3953                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3954                  !!!next-token;              !!!cp ('t94');
3955                  redo B;              #
3956                }            } else {
3957              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!cp ('t95');
3958                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3959                !!!create-element ($self->{head_element}, 'head');              ## Ignore the token
3960                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
3961                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
3962                next B;
3963              }
3964            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3965              !!!cp ('t96');
3966              ## As if <head>
3967              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
3968              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3969              push @{$self->{open_elements}},
3970                  [$self->{head_element}, $el_category->{head}];
3971    
3972                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
3973                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3974              }          } else {
3975              !!!cp ('t97');
3976            }
3977    
3978              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
3979                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3980                    !!!cp ('t98');
3981                  ## As if </noscript>                  ## As if </noscript>
3982                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3983                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
3984                                
3985                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3986                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3987                  } else {
3988                    !!!cp ('t99');
3989                }                }
3990    
3991                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3992                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3993                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
3994                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3995                    push @{$self->{open_elements}},
3996                        [$self->{head_element}, $el_category->{head}];
3997                  } else {
3998                    !!!cp ('t101');
3999                }                }
4000                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4001                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4002                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4003                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4004                  !!!nack ('t101.1');
4005                !!!next-token;                !!!next-token;
4006                redo B;                next B;
4007              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4008                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4009                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4010                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4011                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4012                    push @{$self->{open_elements}},
4013                        [$self->{head_element}, $el_category->{head}];
4014                  } else {
4015                    !!!cp ('t103');
4016                }                }
4017                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4018                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4019                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4020                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4021                  !!!ack ('t103.1');
4022                !!!next-token;                !!!next-token;
4023                redo B;                next B;
4024              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4025                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4026                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4027                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4028                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4029                    push @{$self->{open_elements}},
4030                        [$self->{head_element}, $el_category->{head}];
4031                  } else {
4032                    !!!cp ('t105');
4033                }                }
4034                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4035                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4036    
4037                unless ($self->{confident}) {                unless ($self->{confident}) {
4038                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
4039                      !!!cp ('t106');
4040                    $self->{change_encoding}                    $self->{change_encoding}
4041                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4042                             $token);
4043                      
4044                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4045                          ->set_user_data (manakai_has_reference =>
4046                                               $token->{attributes}->{charset}
4047                                                   ->{has_reference});
4048                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
4049                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4050                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4051                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4052                              [\x09-\x0D\x20]*=
4053                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4054                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4055                        !!!cp ('t107');
4056                      $self->{change_encoding}                      $self->{change_encoding}
4057                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4058                               $token);
4059                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4060                            ->set_user_data (manakai_has_reference =>
4061                                                 $token->{attributes}->{content}
4062                                                       ->{has_reference});
4063                      } else {
4064                        !!!cp ('t108');
4065                    }                    }
4066                  }                  }
4067                  } else {
4068                    if ($token->{attributes}->{charset}) {
4069                      !!!cp ('t109');
4070                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4071                          ->set_user_data (manakai_has_reference =>
4072                                               $token->{attributes}->{charset}
4073                                                   ->{has_reference});
4074                    }
4075                    if ($token->{attributes}->{content}) {
4076                      !!!cp ('t110');
4077                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4078                          ->set_user_data (manakai_has_reference =>
4079                                               $token->{attributes}->{content}
4080                                                   ->{has_reference});
4081                    }
4082                }                }
4083    
4084                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4085                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4086                  !!!ack ('t110.1');
4087                !!!next-token;                !!!next-token;
4088                redo B;                next B;
4089              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4090                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4091                    !!!cp ('t111');
4092                  ## As if </noscript>                  ## As if </noscript>
4093                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4094                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4095                                
4096                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4097                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4098                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4099                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4100                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4101                    push @{$self->{open_elements}},
4102                        [$self->{head_element}, $el_category->{head}];
4103                  } else {
4104                    !!!cp ('t113');
4105                }                }
4106    
4107                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4108                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4109                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4110                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4111                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4112                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4113                redo B;                next B;
4114              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4115                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4116                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4117                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4118                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4119                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4120                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4121                    push @{$self->{open_elements}},
4122                        [$self->{head_element}, $el_category->{head}];
4123                  } else {
4124                    !!!cp ('t115');
4125                }                }
4126                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4127                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4128                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4129                redo B;                next B;
4130              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4131                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4132                    !!!cp ('t116');
4133                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4134                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4135                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4136                    !!!nack ('t116.1');
4137                  !!!next-token;                  !!!next-token;
4138                  redo B;                  next B;
4139                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4140                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4141                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4142                  ## Ignore the token                  ## Ignore the token
4143                    !!!nack ('t117.1');
4144                  !!!next-token;                  !!!next-token;
4145                  redo B;                  next B;
4146                } else {                } else {
4147                    !!!cp ('t118');
4148                  #                  #
4149                }                }
4150              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4151                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4152                    !!!cp ('t119');
4153                  ## As if </noscript>                  ## As if </noscript>
4154                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4155                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4156                                
4157                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4158                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4159                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4160                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4161                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4162                    push @{$self->{open_elements}},
4163                        [$self->{head_element}, $el_category->{head}];
4164                  } else {
4165                    !!!cp ('t121');
4166                }                }
4167    
4168                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4169                $script_start_tag->($insert_to_current);                $script_start_tag->();
4170                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4171                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4172                redo B;                next B;
4173              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4174                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4175                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4176                    !!!cp ('t122');
4177                  ## As if </noscript>                  ## As if </noscript>
4178                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4179                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4180                                    
4181                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4182                  ## As if </head>                  ## As if </head>
# Line 2967  sub _tree_construction_main ($) { Line 4184  sub _tree_construction_main ($) {
4184                                    
4185                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4186                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4187                    !!!cp ('t124');
4188                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4189                                    
4190                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4191                  } else {
4192                    !!!cp ('t125');
4193                }                }
4194    
4195                ## "after head" insertion mode                ## "after head" insertion mode
4196                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4197                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4198                    !!!cp ('t126');
4199                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4200                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4201                    !!!cp ('t127');
4202                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4203                } else {                } else {
4204                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4205                }                }
4206                  !!!nack ('t127.1');
4207                !!!next-token;                !!!next-token;
4208                redo B;                next B;
4209              } else {              } else {
4210                  !!!cp ('t128');
4211                #                #
4212              }              }
4213    
4214              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4215                  !!!cp ('t129');
4216                ## As if </noscript>                ## As if </noscript>
4217                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4218                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4219                                
4220                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4221                ## As if </head>                ## As if </head>
# Line 2998  sub _tree_construction_main ($) { Line 4223  sub _tree_construction_main ($) {
4223    
4224                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4225              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4226                  !!!cp ('t130');
4227                ## As if </head>                ## As if </head>
4228                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4229    
4230                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4231                } else {
4232                  !!!cp ('t131');
4233              }              }
4234    
4235              ## "after head" insertion mode              ## "after head" insertion mode
4236              ## As if <body>              ## As if <body>
4237              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4238              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4239              ## reprocess              ## reprocess
4240              redo B;              !!!ack-later;
4241                next B;
4242            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4243              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4244                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4245                    !!!cp ('t132');
4246                  ## As if <head>                  ## As if <head>
4247                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4248                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4249                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4250                        [$self->{head_element}, $el_category->{head}];
4251    
4252                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4253                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4254                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4255                  !!!next-token;                  !!!next-token;
4256                  redo B;                  next B;
4257                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4258                    !!!cp ('t133');
4259                  ## As if </noscript>                  ## As if </noscript>
4260                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4261                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4262                                    
4263                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4264                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4265                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4266                  !!!next-token;                  !!!next-token;
4267                  redo B;                  next B;
4268                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4269                    !!!cp ('t134');
4270                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4271                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4272                  !!!next-token;                  !!!next-token;
4273                  redo B;                  next B;
4274                } else {                } else {
4275                    !!!cp ('t135');
4276                  #                  #
4277                }                }
4278              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4279                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4280                    !!!cp ('t136');
4281                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4282                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4283                  !!!next-token;                  !!!next-token;
4284                  redo B;                  next B;
4285                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4286                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!cp ('t137');
4287                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4288                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4289                  !!!next-token;                  !!!next-token;
4290                  redo B;                  next B;
4291                } else {                } else {
4292                    !!!cp ('t138');
4293                  #                  #
4294                }                }
4295              } elsif ({              } elsif ({
4296                        body => 1, html => 1,                        body => 1, html => 1,
4297                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4298                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4299                    !!!cp ('t139');
4300                  ## As if <head>                  ## As if <head>
4301                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4302                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4303                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4304                        [$self->{head_element}, $el_category->{head}];
4305    
4306                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4307                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4308                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4309                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t140');
4310                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4311                  ## Ignore the token                  ## Ignore the token
4312                  !!!next-token;                  !!!next-token;
4313                  redo B;                  next B;
4314                  } else {
4315                    !!!cp ('t141');
4316                }                }
4317                                
4318                #                #
# Line 3078  sub _tree_construction_main ($) { Line 4320  sub _tree_construction_main ($) {
4320                        p => 1, br => 1,                        p => 1, br => 1,
4321                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4322                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4323                    !!!cp ('t142');
4324                  ## As if <head>                  ## As if <head>
4325                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4326                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4327                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4328                        [$self->{head_element}, $el_category->{head}];
4329    
4330                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4331                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4332                  } else {
4333                    !!!cp ('t143');
4334                }                }
4335    
4336                #                #
4337              } else {              } else {
4338                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4339                    !!!cp ('t144');
4340                  #                  #
4341                } else {                } else {
4342                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t145');
4343                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4344                  ## Ignore the token                  ## Ignore the token
4345                  !!!next-token;                  !!!next-token;
4346                  redo B;                  next B;
4347                }                }
4348              }              }
4349    
4350              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4351                  !!!cp ('t146');
4352                ## As if </noscript>                ## As if </noscript>
4353                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4354                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4355                                
4356                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4357                ## As if </head>                ## As if </head>
# Line 3110  sub _tree_construction_main ($) { Line 4359  sub _tree_construction_main ($) {
4359    
4360                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4361              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4362                  !!!cp ('t147');
4363                ## As if </head>                ## As if </head>
4364                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4365    
4366                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4367              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4368                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4369                  !!!cp ('t148');
4370                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4371                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4372                !!!next-token;                !!!next-token;
4373                redo B;                next B;
4374                } else {
4375                  !!!cp ('t149');
4376              }              }
4377    
4378              ## "after head" insertion mode              ## "after head" insertion mode
4379              ## As if <body>              ## As if <body>
4380              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4381              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4382              ## reprocess              ## reprocess
4383              redo B;              next B;
4384            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4385              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4386            }            !!!cp ('t149.1');
4387    
4388              ## NOTE: As if <head>
4389              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4390              $self->{open_elements}->[-1]->[0]->append_child
4391                  ($self->{head_element});
4392              #push @{$self->{open_elements}},
4393              #    [$self->{head_element}, $el_category->{head}];
4394              #$self->{insertion_mode} = IN_HEAD_IM;
4395              ## NOTE: Reprocess.
4396    
4397              ## NOTE: As if </head>
4398              #pop @{$self->{open_elements}};
4399              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4400              ## NOTE: Reprocess.
4401              
4402              #
4403            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4404              !!!cp ('t149.2');
4405    
4406              ## NOTE: As if </head>
4407              pop @{$self->{open_elements}};
4408              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4409              ## NOTE: Reprocess.
4410    
4411              #
4412            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4413              !!!cp ('t149.3');
4414    
4415              !!!parse-error (type => 'in noscript:#eof', token => $token);
4416    
4417              ## As if </noscript>
4418              pop @{$self->{open_elements}};
4419              #$self->{insertion_mode} = IN_HEAD_IM;
4420              ## NOTE: Reprocess.
4421    
4422              ## NOTE: As if </head>
4423              pop @{$self->{open_elements}};
4424              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4425              ## NOTE: Reprocess.
4426    
4427              #
4428            } else {
4429              !!!cp ('t149.4');
4430              #
4431            }
4432    
4433            ## NOTE: As if <body>
4434            !!!insert-element ('body',, $token);
4435            $self->{insertion_mode} = IN_BODY_IM;
4436            ## NOTE: Reprocess.
4437            next B;
4438          } else {
4439            die "$0: $token->{type}: Unknown token type";
4440          }
4441    
4442            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4443      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
4444            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4445                !!!cp ('t150');
4446              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4447              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4448                            
4449              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4450    
4451              !!!next-token;              !!!next-token;
4452              redo B;              next B;
4453            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4454              if ({              if ({
4455                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3148  sub _tree_construction_main ($) { Line 4457  sub _tree_construction_main ($) {
4457                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4458                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4459                  ## have an element in table scope                  ## have an element in table scope
4460                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4461                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4462                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4463                      $tn = $node->[1];                      !!!cp ('t151');
4464                      last INSCOPE;  
4465                    } elsif ({                      ## Close the cell
4466                              table => 1, html => 1,                      !!!back-token; # <x>
4467                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4468                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4469                    }                                line => $token->{line},
4470                  } # INSCOPE                                column => $token->{column}};
4471                    unless (defined $tn) {                      next B;
4472                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4473                      ## Ignore the token                      !!!cp ('t152');
4474                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4475                      redo B;                      last;
4476                    }                    }
4477                                    }
4478                  ## Close the cell  
4479                  !!!back-token; # <?>                  !!!cp ('t153');
4480                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
4481                  redo B;                      value => $token->{tag_name}, token => $token);
4482                    ## Ignore the token
4483                    !!!nack ('t153.1');
4484                    !!!next-token;
4485                    next B;
4486                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4487                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4488                                    
4489                  ## As if </caption>                  ## NOTE: As if </caption>.
4490                  ## have a table element in table scope                  ## have a table element in table scope
4491                  my $i;                  my $i;
4492                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4493                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4494                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4495                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4496                      last INSCOPE;                        !!!cp ('t155');
4497                    } elsif ({                        $i = $_;
4498                              table => 1, html => 1,                        last INSCOPE;
4499                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4500                      last INSCOPE;                        !!!cp ('t156');
4501                          last;
4502                        }
4503                    }                    }
4504    
4505                      !!!cp ('t157');
4506                      !!!parse-error (type => 'start tag not allowed',
4507                                      value => $token->{tag_name}, token => $token);
4508                      ## Ignore the token
4509                      !!!nack ('t157.1');
4510                      !!!next-token;
4511                      next B;
4512                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4513                                    
4514                  ## generate implied end tags                  ## generate implied end tags
4515                  if ({                  while ($self->{open_elements}->[-1]->[1]
4516                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4517                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4518                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token; # <?>  
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4519                  }                  }
4520    
4521                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4522                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4523                      !!!parse-error (type => 'not closed',
4524                                      value => $self->{open_elements}->[-1]->[0]
4525                                          ->manakai_local_name,
4526                                      token => $token);
4527                    } else {
4528                      !!!cp ('t160');
4529                  }                  }
4530                                    
4531                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3220  sub _tree_construction_main ($) { Line 4535  sub _tree_construction_main ($) {
4535                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4536                                    
4537                  ## reprocess                  ## reprocess
4538                  redo B;                  !!!ack-later;
4539                    next B;
4540                } else {                } else {
4541                    !!!cp ('t161');
4542                  #                  #
4543                }                }
4544              } else {              } else {
4545                  !!!cp ('t162');
4546                #                #
4547              }              }
4548            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3234  sub _tree_construction_main ($) { Line 4552  sub _tree_construction_main ($) {
4552                  my $i;                  my $i;
4553                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4554                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4555                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4556                        !!!cp ('t163');
4557                      $i = $_;                      $i = $_;
4558                      last INSCOPE;                      last INSCOPE;
4559                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4560                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4561                      last INSCOPE;                      last INSCOPE;
4562                    }                    }
4563                  } # INSCOPE                  } # INSCOPE
4564                    unless (defined $i) {                    unless (defined $i) {
4565                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4566                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4567                      ## Ignore the token                      ## Ignore the token
4568                      !!!next-token;                      !!!next-token;
4569                      redo B;                      next B;
4570                    }                    }
4571                                    
4572                  ## generate implied end tags                  ## generate implied end tags
4573                  if ({                  while ($self->{open_elements}->[-1]->[1]
4574                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4575                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4576                       th => ($token->{tag_name} eq 'td'),                    pop @{$self->{open_elements}};
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4577                  }                  }
4578                    
4579                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4580                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4581                      !!!cp ('t167');
4582                      !!!parse-error (type => 'not closed',
4583                                      value => $self->{open_elements}->[-1]->[0]
4584                                          ->manakai_local_name,
4585                                      token => $token);
4586                    } else {
4587                      !!!cp ('t168');
4588                  }                  }
4589                                    
4590                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3275  sub _tree_construction_main ($) { Line 4594  sub _tree_construction_main ($) {
4594                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4595                                    
4596                  !!!next-token;                  !!!next-token;
4597                  redo B;                  next B;
4598                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4599                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4600                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4601                  ## Ignore the token                  ## Ignore the token
4602                  !!!next-token;                  !!!next-token;
4603                  redo B;                  next B;
4604                } else {                } else {
4605                    !!!cp ('t170');
4606                  #                  #
4607                }                }
4608              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4609                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4610                  ## have a table element in table scope                  ## have a table element in table scope
4611                  my $i;                  my $i;
4612                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4613                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4614                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4615                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4616                      last INSCOPE;                        !!!cp ('t171');
4617                    } elsif ({                        $i = $_;
4618                              table => 1, html => 1,                        last INSCOPE;
4619                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4620                      last INSCOPE;                        !!!cp ('t172');
4621                          last;
4622                        }
4623                    }                    }
4624    
4625                      !!!cp ('t173');
4626                      !!!parse-error (type => 'unmatched end tag',
4627                                      value => $token->{tag_name}, token => $token);
4628                      ## Ignore the token
4629                      !!!next-token;
4630                      next B;
4631                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4632                                    
4633                  ## generate implied end tags                  ## generate implied end tags
4634                  if ({                  while ($self->{open_elements}->[-1]->[1]
4635                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4636                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
4637                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4638                  }                  }
4639                                    
4640                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4641                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4642                      !!!parse-error (type => 'not closed',
4643                                      value => $self->{open_elements}->[-1]->[0]
4644                                          ->manakai_local_name,
4645                                      token => $token);
4646                    } else {
4647                      !!!cp ('t176');
4648                  }                  }
4649                                    
4650                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3329  sub _tree_construction_main ($) { Line 4654  sub _tree_construction_main ($) {
4654                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4655                                    
4656                  !!!next-token;                  !!!next-token;
4657                  redo B;                  next B;
4658                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4659                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4660                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4661                  ## Ignore the token                  ## Ignore the token
4662                  !!!next-token;                  !!!next-token;
4663                  redo B;                  next B;
4664                } else {                } else {
4665                    !!!cp ('t178');
4666                  #                  #
4667                }                }
4668              } elsif ({              } elsif ({
# Line 3346  sub _tree_construction_main ($) { Line 4673  sub _tree_construction_main ($) {
4673                ## have an element in table scope                ## have an element in table scope
4674                my $i;                my $i;
4675                my $tn;                my $tn;
4676                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4677                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4678                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4679                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4680                    last INSCOPE;                      !!!cp ('t179');
4681                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4682                    $tn = $node->[1];  
4683                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4684                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
4685                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4686                            table => 1, html => 1,                                line => $token->{line},
4687                           }->{$node->[1]}) {                                column => $token->{column}};
4688                    last INSCOPE;                      next B;
4689                      } elsif ($node->[1] & TABLE_CELL_EL) {
4690                        !!!cp ('t180');
4691                        $tn = $node->[0]->manakai_local_name;
4692                        ## NOTE: There is exactly one |td| or |th| element
4693                        ## in scope in the stack of open elements by definition.
4694                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4695                        ## ISSUE: Can this be reached?
4696                        !!!cp ('t181');
4697                        last;
4698                      }
4699                  }                  }
4700                } # INSCOPE  
4701                unless (defined $i) {                  !!!cp ('t182');
4702                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4703                        value => $token->{tag_name}, token => $token);
4704                  ## Ignore the token                  ## Ignore the token
4705                  !!!next-token;                  !!!next-token;
4706                  redo B;                  next B;
4707                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4708              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4709                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4710                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4711    
4712                ## As if </caption>                ## As if </caption>
4713                ## have a table element in table scope                ## have a table element in table scope
4714                my $i;                my $i;
4715                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4716                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4717                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4718                      !!!cp ('t184');
4719                    $i = $_;                    $i = $_;
4720                    last INSCOPE;                    last INSCOPE;
4721                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4722                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4723                    last INSCOPE;                    last INSCOPE;
4724                  }                  }
4725                } # INSCOPE                } # INSCOPE
4726                unless (defined $i) {                unless (defined $i) {
4727                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4728                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4729                  ## Ignore the token                  ## Ignore the token
4730                  !!!next-token;                  !!!next-token;
4731                  redo B;                  next B;
4732                }                }
4733                                
4734                ## generate implied end tags                ## generate implied end tags
4735                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4736                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4737                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4738                }                }
4739    
4740                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4741                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4742                    !!!parse-error (type => 'not closed',
4743                                    value => $self->{open_elements}->[-1]->[0]
4744                                        ->manakai_local_name,
4745                                    token => $token);
4746                  } else {
4747                    !!!cp ('t189');
4748                }                }
4749    
4750                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3422  sub _tree_construction_main ($) { Line 4754  sub _tree_construction_main ($) {
4754                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4755    
4756                ## reprocess                ## reprocess
4757                redo B;                next B;
4758              } elsif ({              } elsif ({
4759                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4760                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4761                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4762                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
4763                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4764                  ## Ignore the token                  ## Ignore the token
4765                  !!!next-token;                  !!!next-token;
4766                  redo B;                  next B;
4767                } else {                } else {
4768                    !!!cp ('t191');
4769                  #                  #
4770                }                }
4771              } elsif ({              } elsif ({
# Line 3439  sub _tree_construction_main ($) { Line 4773  sub _tree_construction_main ($) {
4773                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4774                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4775                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4776                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
4777                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4778                ## Ignore the token                ## Ignore the token
4779                !!!next-token;                !!!next-token;
4780                redo B;                next B;
4781              } else {              } else {
4782                  !!!cp ('t193');
4783                #                #
4784              }              }
4785          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4786            for my $entry (@{$self->{open_elements}}) {
4787              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4788                !!!cp ('t75');
4789                !!!parse-error (type => 'in body:#eof', token => $token);
4790                last;
4791              }
4792            }
4793    
4794            ## Stop parsing.
4795            last B;
4796        } else {        } else {
4797          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4798        }        }
# Line 3454  sub _tree_construction_main ($) { Line 4801  sub _tree_construction_main ($) {
4801        #        #
4802      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4803        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4804              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4805                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4806              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4807                                
4808                unless (length $token->{data}) {            unless (length $token->{data}) {
4809                  !!!next-token;              !!!cp ('t194');
4810                  redo B;              !!!next-token;
4811                }              next B;
4812              }            } else {
4813                !!!cp ('t195');
4814              }
4815            }
4816    
4817              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4818    
4819              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4820              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3471  sub _tree_construction_main ($) { Line 4822  sub _tree_construction_main ($) {
4822              ## result in a new Text node.              ## result in a new Text node.
4823              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4824                            
4825              if ({              if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4826                # MUST                # MUST
4827                my $foster_parent_element;                my $foster_parent_element;
4828                my $next_sibling;                my $next_sibling;
4829                my $prev_sibling;                my $prev_sibling;
4830                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4831                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4832                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4833                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4834                        !!!cp ('t196');
4835                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4836                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4837                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4838                    } else {                    } else {
4839                        !!!cp ('t197');
4840                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4841                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4842                    }                    }
# Line 3498  sub _tree_construction_main ($) { Line 4848  sub _tree_construction_main ($) {
4848                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4849                if (defined $prev_sibling and                if (defined $prev_sibling and
4850                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4851                    !!!cp ('t198');
4852                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4853                } else {                } else {
4854                    !!!cp ('t199');
4855                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4856                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4857                     $next_sibling);                     $next_sibling);
4858                }                }
4859              } else {            $open_tables->[-1]->[1] = 1; # tainted
4860                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4861              }            !!!cp ('t200');
4862              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4863            }
4864                            
4865              !!!next-token;          !!!next-token;
4866              redo B;          next B;
4867        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4868              if ({              if ({
4869                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 3517  sub _tree_construction_main ($) { Line 4871  sub _tree_construction_main ($) {
4871                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4872                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4873                  ## Clear back to table context                  ## Clear back to table context
4874                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4875                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4876                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
4877                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4878                  }                  }
4879                                    
4880                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
4881                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4882                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4883                }                }
4884    
4885                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4886                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4887                    !!!parse-error (type => 'missing start tag:tr');                    !!!cp ('t202');
4888                      !!!parse-error (type => 'missing start tag:tr', token => $token);
4889                  }                  }
4890                                    
4891                  ## Clear back to table body context                  ## Clear back to table body context
4892                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4893                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
4894                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t203');
4895                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
4896                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4897                  }                  }
4898                                    
4899                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4900                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4901                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
4902                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4903                      !!!nack ('t204');
4904                    !!!next-token;                    !!!next-token;
4905                    redo B;                    next B;
4906                  } else {                  } else {
4907                    !!!insert-element ('tr');                    !!!cp ('t205');
4908                      !!!insert-element ('tr',, $token);
4909                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4910                  }                  }
4911                  } else {
4912                    !!!cp ('t206');
4913                }                }
4914    
4915                ## Clear back to table row context                ## Clear back to table row context
4916                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4917                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
4918                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4919                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4920                }                }
4921                                
4922                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4923                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
4924    
4925                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
4926                                
4927                  !!!nack ('t207.1');
4928                !!!next-token;                !!!next-token;
4929                redo B;                next B;
4930              } elsif ({              } elsif ({
4931                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4932                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 3578  sub _tree_construction_main ($) { Line 4938  sub _tree_construction_main ($) {
4938                  my $i;                  my $i;
4939                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4940                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4941                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
4942                        !!!cp ('t208');
4943                      $i = $_;                      $i = $_;
4944                      last INSCOPE;                      last INSCOPE;
4945                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4946                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
4947                      last INSCOPE;                      last INSCOPE;
4948                    }                    }
4949                  } # INSCOPE                  } # INSCOPE
4950                  unless (defined $i) {                  unless (defined $i) {
4951                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
4952    ## TODO: This type is wrong.
4953                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4954                    ## Ignore the token                    ## Ignore the token
4955                      !!!nack ('t210.1');
4956                    !!!next-token;                    !!!next-token;
4957                    redo B;                    next B;
4958                  }                  }
4959                                    
4960                  ## Clear back to table row context                  ## Clear back to table row context
4961                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4962                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
4963                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
4964                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
4965                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4966                  }                  }
4967                                    
4968                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
4969                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4970                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4971                      !!!cp ('t212');
4972                    ## reprocess                    ## reprocess
4973                    redo B;                    !!!ack-later;
4974                      next B;
4975                  } else {                  } else {
4976                      !!!cp ('t213');
4977                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
4978                  }                  }
4979                }                }
# Line 3617  sub _tree_construction_main ($) { Line 4983  sub _tree_construction_main ($) {
4983                  my $i;                  my $i;
4984                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4985                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4986                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
4987                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
4988                      $i = $_;                      $i = $_;
4989                      last INSCOPE;                      last INSCOPE;
4990                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4991                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
4992                      last INSCOPE;                      last INSCOPE;
4993                    }                    }
4994                  } # INSCOPE                  } # INSCOPE
4995                  unless (defined $i) {                  unless (defined $i) {
4996                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
4997    ## TODO: This erorr type ios wrong.
4998                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4999                    ## Ignore the token                    ## Ignore the token
5000                      !!!nack ('t216.1');
5001                    !!!next-token;                    !!!next-token;
5002                    redo B;                    next B;
5003                  }                  }
5004    
5005                  ## Clear back to table body context                  ## Clear back to table body context
5006                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5007                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5008                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5009                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5010                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5011                  }                  }
5012                                    
# Line 3653  sub _tree_construction_main ($) { Line 5020  sub _tree_construction_main ($) {
5020                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5021                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5022                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5023                  } else {
5024                    !!!cp ('t218');
5025                }                }
5026    
5027                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5028                  ## Clear back to table context                  ## Clear back to table context
5029                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5030                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5031                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5032                      ## ISSUE: Can this state be reached?
5033                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5034                  }                  }
5035                                    
5036                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5037                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5038                  ## reprocess                  ## reprocess
5039                  redo B;                  !!!ack-later;
5040                    next B;
5041                } elsif ({                } elsif ({
5042                          caption => 1,                          caption => 1,
5043                          colgroup => 1,                          colgroup => 1,
5044                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5045                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5046                  ## Clear back to table context                  ## Clear back to table context
5047                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5048                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5049                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5050                      ## ISSUE: Can this state be reached?
5051                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5052                  }                  }
5053                                    
5054                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5055                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5056                                    
5057                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5058                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5059                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5060                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3691  sub _tree_construction_main ($) { Line 5063  sub _tree_construction_main ($) {
5063                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5064                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5065                  !!!next-token;                  !!!next-token;
5066                  redo B;                  !!!nack ('t220.1');
5067                    next B;
5068                } else {                } else {
5069                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5070                }                }
5071              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5072                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5073                                  value => $self->{open_elements}->[-1]->[0]
5074                                      ->manakai_local_name,
5075                                  token => $token);
5076    
5077                ## As if </table>                ## As if </table>
5078                ## have a table element in table scope                ## have a table element in table scope
5079                my $i;                my $i;
5080                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5081                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5082                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5083                      !!!cp ('t221');
5084                    $i = $_;                    $i = $_;
5085                    last INSCOPE;                    last INSCOPE;
5086                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5087                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5088                    last INSCOPE;                    last INSCOPE;
5089                  }                  }
5090                } # INSCOPE                } # INSCOPE
5091                unless (defined $i) {                unless (defined $i) {
5092                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5093    ## TODO: The following is wrong, maybe.
5094                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5095                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5096                    !!!nack ('t223.1');
5097                  !!!next-token;                  !!!next-token;
5098                  redo B;                  next B;
5099                }                }
5100                                
5101    ## TODO: Followings are removed from the latest spec.
5102                ## generate implied end tags                ## generate implied end tags
5103                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5104                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5105                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5106                }                }
5107    
5108                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5109                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5110                    ## NOTE: |<table><tr><table>|
5111                    !!!parse-error (type => 'not closed',
5112                                    value => $self->{open_elements}->[-1]->[0]
5113                                        ->manakai_local_name,
5114                                    token => $token);
5115                  } else {
5116                    !!!cp ('t226');
5117                }                }
5118    
5119                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5120                  pop @{$open_tables};
5121    
5122                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5123    
5124                ## reprocess            ## reprocess
5125                redo B;            !!!ack-later;
5126          } else {            next B;
5127            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
5128              if (not $open_tables->[-1]->[1]) { # tainted
5129                !!!cp ('t227.8');
5130                ## NOTE: This is a "as if in head" code clone.
5131                $parse_rcdata->(CDATA_CONTENT_MODEL);
5132                next B;
5133              } else {
5134                !!!cp ('t227.7');
5135                #
5136              }
5137            } elsif ($token->{tag_name} eq 'script') {
5138              if (not $open_tables->[-1]->[1]) { # tainted
5139                !!!cp ('t227.6');
5140                ## NOTE: This is a "as if in head" code clone.
5141                $script_start_tag->();
5142                next B;
5143              } else {
5144                !!!cp ('t227.5');
5145                #
5146              }
5147            } elsif ($token->{tag_name} eq 'input') {
5148              if (not $open_tables->[-1]->[1]) { # tainted
5149                if ($token->{attributes}->{type}) { ## TODO: case
5150                  my $type = lc $token->{attributes}->{type}->{value};
5151                  if ($type eq 'hidden') {
5152                    !!!cp ('t227.3');
5153                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5154    
5155            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5156    
5157                    ## TODO: form element pointer
5158    
5159                    pop @{$self->{open_elements}};
5160    
5161                    !!!next-token;
5162                    !!!ack ('t227.2.1');
5163                    next B;
5164                  } else {
5165                    !!!cp ('t227.2');
5166                    #
5167                  }
5168                } else {
5169                  !!!cp ('t227.1');
5170                  #
5171                }
5172              } else {
5173                !!!cp ('t227.4');
5174                #
5175              }
5176            } else {
5177              !!!cp ('t227');
5178            #            #
5179          }          }
5180    
5181            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5182    
5183            $insert = $insert_to_foster;
5184            #
5185        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5186              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5187                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3756  sub _tree_construction_main ($) { Line 5189  sub _tree_construction_main ($) {
5189                my $i;                my $i;
5190                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5191                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5192                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5193                      !!!cp ('t228');
5194                    $i = $_;                    $i = $_;
5195                    last INSCOPE;                    last INSCOPE;
5196                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5197                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5198                    last INSCOPE;                    last INSCOPE;
5199                  }                  }
5200                } # INSCOPE                } # INSCOPE
5201                unless (defined $i) {                unless (defined $i) {
5202                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5203                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5204                  ## Ignore the token                  ## Ignore the token
5205                    !!!nack ('t230.1');
5206                  !!!next-token;                  !!!next-token;
5207                  redo B;                  next B;
5208                  } else {
5209                    !!!cp ('t232');
5210                }                }
5211    
5212                ## Clear back to table row context                ## Clear back to table row context
5213                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5214                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5215                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5216                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5217                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5218                }                }
5219    
5220                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5221                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5222                !!!next-token;                !!!next-token;
5223                redo B;                !!!nack ('t231.1');
5224                  next B;
5225              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5226                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5227                  ## As if </tr>                  ## As if </tr>
# Line 3791  sub _tree_construction_main ($) { Line 5229  sub _tree_construction_main ($) {
5229                  my $i;                  my $i;
5230                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5231                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5232                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5233                        !!!cp ('t233');
5234                      $i = $_;                      $i = $_;
5235                      last INSCOPE;                      last INSCOPE;
5236                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5237                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5238                      last INSCOPE;                      last INSCOPE;
5239                    }                    }
5240                  } # INSCOPE                  } # INSCOPE
5241                  unless (defined $i) {                  unless (defined $i) {
5242                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5243    ## TODO: The following is wrong.
5244                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5245                    ## Ignore the token                    ## Ignore the token
5246                      !!!nack ('t236.1');
5247                    !!!next-token;                    !!!next-token;
5248                    redo B;                    next B;
5249                  }                  }
5250                                    
5251                  ## Clear back to table row context                  ## Clear back to table row context
5252                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5253                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5254                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5255                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5256                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5257                  }                  }
5258                                    
# Line 3825  sub _tree_construction_main ($) { Line 5266  sub _tree_construction_main ($) {
5266                  my $i;                  my $i;
5267                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5268                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5269                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5270                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5271                      $i = $_;                      $i = $_;
5272                      last INSCOPE;                      last INSCOPE;
5273                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5274                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5275                      last INSCOPE;                      last INSCOPE;
5276                    }                    }
5277                  } # INSCOPE                  } # INSCOPE
5278                  unless (defined $i) {                  unless (defined $i) {
5279                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5280                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5281                    ## Ignore the token                    ## Ignore the token
5282                      !!!nack ('t239.1');
5283                    !!!next-token;                    !!!next-token;
5284                    redo B;                    next B;
5285                  }                  }
5286                                    
5287                  ## Clear back to table body context                  ## Clear back to table body context
5288                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5289                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5290                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5291                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5292                  }                  }
5293                                    
# Line 3863  sub _tree_construction_main ($) { Line 5303  sub _tree_construction_main ($) {
5303                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5304                }                }
5305    
5306                  ## NOTE: </table> in the "in table" insertion mode.
5307                  ## When you edit the code fragment below, please ensure that
5308                  ## the code for <table> in the "in table" insertion mode
5309                  ## is synced with it.
5310    
5311                ## have a table element in table scope                ## have a table element in table scope
5312                my $i;                my $i;
5313                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5314                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5315                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5316                      !!!cp ('t241');
5317                    $i = $_;                    $i = $_;
5318                    last INSCOPE;                    last INSCOPE;
5319                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5320                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5321                    last INSCOPE;                    last INSCOPE;
5322                  }                  }
5323                } # INSCOPE                } # INSCOPE
5324                unless (defined $i) {                unless (defined $i) {
5325                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5326                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5327                  ## Ignore the token                  ## Ignore the token
5328                    !!!nack ('t243.1');
5329                  !!!next-token;                  !!!next-token;
5330                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5331                }                }
5332                                    
5333                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5334                  pop @{$open_tables};
5335                                
5336                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5337                                
5338                !!!next-token;                !!!next-token;
5339                redo B;                next B;
5340              } elsif ({              } elsif ({
5341                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5342                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 3914  sub _tree_construction_main ($) { Line 5346  sub _tree_construction_main ($) {
5346                  my $i;                  my $i;
5347                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5348                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5349                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5350                        !!!cp ('t247');
5351                      $i = $_;                      $i = $_;
5352                      last INSCOPE;                      last INSCOPE;
5353                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5354                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
5355                      last INSCOPE;                      last INSCOPE;
5356                    }                    }
5357                  } # INSCOPE                  } # INSCOPE
5358                    unless (defined $i) {                    unless (defined $i) {
5359                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
5360                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5361                      ## Ignore the token                      ## Ignore the token
5362                        !!!nack ('t249.1');
5363                      !!!next-token;                      !!!next-token;
5364                      redo B;                      next B;
5365                    }                    }
5366                                    
5367                  ## As if </tr>                  ## As if </tr>
# Line 3935  sub _tree_construction_main ($) { Line 5369  sub _tree_construction_main ($) {
5369                  my $i;                  my $i;
5370                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5371                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5372                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5373                        !!!cp ('t250');
5374                      $i = $_;                      $i = $_;
5375                      last INSCOPE;                      last INSCOPE;
5376                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5377                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
5378                      last INSCOPE;                      last INSCOPE;
5379                    }                    }
5380                  } # INSCOPE                  } # INSCOPE
5381                    unless (defined $i) {                    unless (defined $i) {
5382                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
5383                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5384                      ## Ignore the token                      ## Ignore the token
5385                        !!!nack ('t252.1');
5386                      !!!next-token;                      !!!next-token;
5387                      redo B;                      next B;
5388                    }                    }
5389                                    
5390                  ## Clear back to table row context                  ## Clear back to table row context
5391                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5392                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5393                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
5394                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5395                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5396                  }                  }
5397                                    
# Line 3968  sub _tree_construction_main ($) { Line 5404  sub _tree_construction_main ($) {
5404                my $i;                my $i;
5405                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5406                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5407                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5408                      !!!cp ('t254');
5409                    $i = $_;                    $i = $_;
5410                    last INSCOPE;                    last INSCOPE;
5411                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5412                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5413                    last INSCOPE;                    last INSCOPE;
5414                  }                  }
5415                } # INSCOPE                } # INSCOPE
5416                unless (defined $i) {                unless (defined $i) {
5417                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5418                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5419                  ## Ignore the token                  ## Ignore the token
5420                    !!!nack ('t256.1');
5421                  !!!next-token;                  !!!next-token;
5422                  redo B;                  next B;
5423                }                }
5424    
5425                ## Clear back to table body context                ## Clear back to table body context
5426                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5427                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5428                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5429                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5430                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5431                }                }
5432    
5433                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5434                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5435                  !!!nack ('t257.1');
5436                !!!next-token;                !!!next-token;
5437                redo B;                next B;
5438              } elsif ({              } elsif ({
5439                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5440                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5441                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5442                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5443                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5444                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5445                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5446                !!!next-token;            ## Ignore the token
5447                redo B;            !!!nack ('t258.1');
5448               !!!next-token;
5449              next B;
5450          } else {          } else {
5451            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!cp ('t259');
5452              !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5453    
5454            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5455            #            #
5456          }          }
5457          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5458            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5459                    @{$self->{open_elements}} == 1) { # redundant, maybe
5460              !!!parse-error (type => 'in body:#eof', token => $token);
5461              !!!cp ('t259.1');
5462              #
5463            } else {
5464              !!!cp ('t259.2');
5465              #
5466            }
5467    
5468            ## Stop parsing
5469            last B;
5470        } else {        } else {
5471          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5472        }        }
# Line 4020  sub _tree_construction_main ($) { Line 5475  sub _tree_construction_main ($) {
5475              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5476                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5477                unless (length $token->{data}) {                unless (length $token->{data}) {
5478                    !!!cp ('t260');
5479                  !!!next-token;                  !!!next-token;
5480                  redo B;                  next B;
5481                }                }
5482              }              }
5483                            
5484                !!!cp ('t261');
5485              #              #
5486            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5487              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5488                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
5489                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5490                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5491                  !!!ack ('t262.1');
5492                !!!next-token;                !!!next-token;
5493                redo B;                next B;
5494              } else {              } else {
5495                  !!!cp ('t263');
5496                #                #
5497              }              }
5498            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5499              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5500                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5501                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5502                    !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5503                  ## Ignore the token                  ## Ignore the token
5504                  !!!next-token;                  !!!next-token;
5505                  redo B;                  next B;
5506                } else {                } else {
5507                    !!!cp ('t265');
5508                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5509                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5510                  !!!next-token;                  !!!next-token;
5511                  redo B;                              next B;            
5512                }                }
5513              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5514                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5515                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
5516                ## Ignore the token                ## Ignore the token
5517                !!!next-token;                !!!next-token;
5518                redo B;                next B;
5519              } else {              } else {
5520                  !!!cp ('t267');
5521                #                #
5522              }              }
5523            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5524              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5525            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5526              !!!cp ('t270.2');
5527              ## Stop parsing.
5528              last B;
5529            } else {
5530              ## NOTE: As if </colgroup>.
5531              !!!cp ('t270.1');
5532              pop @{$self->{open_elements}}; # colgroup
5533              $self->{insertion_mode} = IN_TABLE_IM;
5534              ## Reprocess.
5535              next B;
5536            }
5537          } else {
5538            die "$0: $token->{type}: Unknown token type";
5539          }
5540    
5541            ## As if </colgroup>            ## As if </colgroup>
5542            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5543              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5544    ## TODO: Wrong error type?
5545                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5546              ## Ignore the token              ## Ignore the token
5547                !!!nack ('t269.1');
5548              !!!next-token;              !!!next-token;
5549              redo B;              next B;
5550            } else {            } else {
5551                !!!cp ('t270');
5552              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5553              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5554                !!!ack-later;
5555              ## reprocess              ## reprocess
5556              redo B;              next B;
5557            }            }
5558      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5559        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5560            !!!cp ('t271');
5561          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5562          !!!next-token;          !!!next-token;
5563          redo B;          next B;
5564        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5565              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5566                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5567                  ## As if </option>              !!!cp ('t272');
5568                  pop @{$self->{open_elements}};              ## As if </option>
5569                }              pop @{$self->{open_elements}};
5570              } else {
5571                !!!cp ('t273');
5572              }
5573    
5574                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5575                !!!next-token;            !!!nack ('t273.1');
5576                redo B;            !!!next-token;
5577              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5578                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5579                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5580                  pop @{$self->{open_elements}};              !!!cp ('t274');
5581                }              ## As if </option>
5582                pop @{$self->{open_elements}};
5583              } else {
5584                !!!cp ('t275');
5585              }
5586    
5587                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5588                  ## As if </optgroup>              !!!cp ('t276');
5589                  pop @{$self->{open_elements}};              ## As if </optgroup>
5590                }              pop @{$self->{open_elements}};
5591              } else {
5592                !!!cp ('t277');
5593              }
5594    
5595                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5596                !!!next-token;            !!!nack ('t277.1');
5597                redo B;            !!!next-token;
5598              } elsif ($token->{tag_name} eq 'select') {            next B;
5599                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5600                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5601                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5602                my $i;                    {
5603                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5604                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5605                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5606                    $i = $_;                    }->{$token->{tag_name}})) {
5607                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5608                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5609                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5610                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5611                    last INSCOPE;            ## have an element in table scope
5612                  }            my $i;
5613                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5614                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5615                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5616                  ## Ignore the token                !!!cp ('t278');
5617                  !!!next-token;                $i = $_;
5618                  redo B;                last INSCOPE;
5619                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5620                  !!!cp ('t279');
5621                  last INSCOPE;
5622                }
5623              } # INSCOPE
5624              unless (defined $i) {
5625                !!!cp ('t280');
5626                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5627                ## Ignore the token
5628                !!!nack ('t280.1');
5629                !!!next-token;
5630                next B;
5631              }
5632                                
5633                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5634              splice @{$self->{open_elements}}, $i;
5635    
5636                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5637    
5638                !!!next-token;            if ($token->{tag_name} eq 'select') {
5639                redo B;              !!!nack ('t281.2');
5640                !!!next-token;
5641                next B;
5642              } else {
5643                !!!cp ('t281.1');
5644                !!!ack-later;
5645                ## Reprocess the token.
5646                next B;
5647              }
5648          } else {          } else {
5649            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
5650              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5651            ## Ignore the token            ## Ignore the token
5652              !!!nack ('t282.1');
5653            !!!next-token;            !!!next-token;
5654            redo B;            next B;
5655          }          }
5656        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5657              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5658                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5659                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5660                  ## As if </option>              !!!cp ('t283');
5661                  splice @{$self->{open_elements}}, -2;              ## As if </option>
5662                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
5663                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5664                } else {              !!!cp ('t284');
5665                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
5666                  ## Ignore the token            } else {
5667                }              !!!cp ('t285');
5668                !!!next-token;              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5669                redo B;              ## Ignore the token
5670              } elsif ($token->{tag_name} eq 'option') {            }
5671                if ($self->{open_elements}->[-1]->[1] eq 'option') {            !!!nack ('t285.1');
5672                  pop @{$self->{open_elements}};            !!!next-token;
5673                } else {            next B;
5674                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'option') {
5675                  ## Ignore the token            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5676                }              !!!cp ('t286');
5677                !!!next-token;              pop @{$self->{open_elements}};
5678                redo B;            } else {
5679              } elsif ($token->{tag_name} eq 'select') {              !!!cp ('t287');
5680                ## have an element in table scope              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5681                my $i;              ## Ignore the token
5682                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            }
5683                  my $node = $self->{open_elements}->[$_];            !!!nack ('t287.1');
5684                  if ($node->[1] eq $token->{tag_name}) {            !!!next-token;
5685                    $i = $_;            next B;
5686                    last INSCOPE;          } elsif ($token->{tag_name} eq 'select') {
5687                  } elsif ({            ## have an element in table scope
5688                            table => 1, html => 1,            my $i;
5689                           }->{$node->[1]}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5690                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5691                  }              if ($node->[1] & SELECT_EL) {
5692                } # INSCOPE                !!!cp ('t288');
5693                unless (defined $i) {                $i = $_;
5694                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                last INSCOPE;
5695                  ## Ignore the token              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5696                  !!!next-token;                !!!cp ('t289');
5697                  redo B;                last INSCOPE;
5698                }              }
5699              } # INSCOPE
5700              unless (defined $i) {
5701                !!!cp ('t290');
5702                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5703                ## Ignore the token
5704                !!!nack ('t290.1');
5705                !!!next-token;
5706                next B;
5707              }
5708                                
5709                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5710              splice @{$self->{open_elements}}, $i;
5711    
5712                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5713    
5714                !!!next-token;            !!!nack ('t291.1');
5715                redo B;            !!!next-token;
5716              } elsif ({            next B;
5717                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5718                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5719                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5720                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5721                     }->{$token->{tag_name}}) {
5722    ## TODO: The following is wrong?
5723              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5724                                
5725                ## have an element in table scope            ## have an element in table scope
5726                my $i;            my $i;
5727                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5728                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5729                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5730                    $i = $_;                !!!cp ('t292');
5731                    last INSCOPE;                $i = $_;
5732                  } elsif ({                last INSCOPE;
5733                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5734                           }->{$node->[1]}) {                !!!cp ('t293');
5735                    last INSCOPE;                last INSCOPE;
5736                  }              }
5737                } # INSCOPE            } # INSCOPE
5738                unless (defined $i) {            unless (defined $i) {
5739                  ## Ignore the token              !!!cp ('t294');
5740                  !!!next-token;              ## Ignore the token
5741                  redo B;              !!!nack ('t294.1');
5742                }              !!!next-token;
5743                next B;
5744              }
5745                                
5746                ## As if </select>            ## As if </select>
5747                ## have an element in table scope            ## have an element in table scope
5748                undef $i;            undef $i;
5749                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5750                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5751                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5752                    $i = $_;                !!!cp ('t295');
5753                    last INSCOPE;                $i = $_;
5754                  } elsif ({                last INSCOPE;
5755                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5756                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
5757                    last INSCOPE;                !!!cp ('t296');
5758                  }                last INSCOPE;
5759                } # INSCOPE              }
5760                unless (defined $i) {            } # INSCOPE
5761                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
5762                  ## Ignore the </select> token              !!!cp ('t297');
5763                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
5764                  redo B;              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5765                }              ## Ignore the </select> token
5766                !!!nack ('t297.1');
5767                !!!next-token; ## TODO: ok?
5768                next B;
5769              }
5770                                
5771                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
5772              splice @{$self->{open_elements}}, $i;
5773    
5774                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5775    
5776                ## reprocess            !!!ack-later;
5777                redo B;            ## reprocess
5778              next B;
5779          } else {          } else {
5780            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
5781              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5782            ## Ignore the token            ## Ignore the token
5783              !!!nack ('t299.3');
5784            !!!next-token;            !!!next-token;
5785            redo B;            next B;
5786            }
5787          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5788            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5789                    @{$self->{open_elements}} == 1) { # redundant, maybe
5790              !!!cp ('t299.1');
5791              !!!parse-error (type => 'in body:#eof', token => $token);
5792            } else {
5793              !!!cp ('t299.2');
5794          }          }
5795    
5796            ## Stop parsing.
5797            last B;
5798        } else {        } else {
5799          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5800        }        }
# Line 4257  sub _tree_construction_main ($) { Line 5808  sub _tree_construction_main ($) {
5808            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5809                        
5810            unless (length $token->{data}) {            unless (length $token->{data}) {
5811                !!!cp ('t300');
5812              !!!next-token;              !!!next-token;
5813              redo B;              next B;
5814            }            }
5815          }          }
5816                    
5817          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5818            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
5819              !!!parse-error (type => 'after html:#character', token => $token);
5820    
5821            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5822            } else {
5823              !!!cp ('t302');
5824          }          }
5825                    
5826          ## "after body" insertion mode          ## "after body" insertion mode
5827          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5828    
5829          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5830          ## reprocess          ## reprocess
5831          redo B;          next B;
5832        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5833          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5834            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
5835              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5836                        
5837            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5838            } else {
5839              !!!cp ('t304');
5840          }          }
5841    
5842          ## "after body" insertion mode          ## "after body" insertion mode
5843          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5844    
5845          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5846            !!!ack-later;
5847          ## reprocess          ## reprocess
5848          redo B;          next B;
5849        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5850          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5851            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
5852              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5853                        
5854            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5855            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5856            } else {
5857              !!!cp ('t306');
5858          }          }
5859    
5860          ## "after body" insertion mode          ## "after body" insertion mode
5861          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5862            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5863              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
5864                !!!parse-error (type => 'unmatched end tag:html', token => $token);
5865              ## Ignore the token              ## Ignore the token
5866              !!!next-token;              !!!next-token;
5867              redo B;              next B;
5868            } else {            } else {
5869                !!!cp ('t308');
5870              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5871              !!!next-token;              !!!next-token;
5872              redo B;              next B;
5873            }            }
5874          } else {          } else {
5875            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
5876              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5877    
5878            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5879            ## reprocess            ## reprocess
5880            redo B;            next B;
5881          }          }
5882          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5883            !!!cp ('t309.2');
5884            ## Stop parsing
5885            last B;
5886        } else {        } else {
5887          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5888        }        }
# Line 4323  sub _tree_construction_main ($) { Line 5892  sub _tree_construction_main ($) {
5892            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5893                        
5894            unless (length $token->{data}) {            unless (length $token->{data}) {
5895                !!!cp ('t310');
5896              !!!next-token;              !!!next-token;
5897              redo B;              next B;
5898            }            }
5899          }          }
5900                    
5901          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5902            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5903              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
5904                !!!parse-error (type => 'in frameset:#character', token => $token);
5905            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5906              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
5907                !!!parse-error (type => 'after frameset:#character', token => $token);
5908            } else { # "after html frameset"            } else { # "after html frameset"
5909              !!!parse-error (type => 'after html:#character');              !!!cp ('t313');
5910                !!!parse-error (type => 'after html:#character', token => $token);
5911    
5912              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5913              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
5914              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5915            }            }
5916                        
5917            ## Ignore the token.            ## Ignore the token.
5918            if (length $token->{data}) {            if (length $token->{data}) {
5919                !!!cp ('t314');
5920              ## reprocess the rest of characters              ## reprocess the rest of characters
5921            } else {            } else {
5922                !!!cp ('t315');
5923              !!!next-token;              !!!next-token;
5924            }            }
5925            redo B;            next B;
5926          }          }
5927                    
5928          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
5929        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5930          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5931            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t316');
5932              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5933    
5934            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5935            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5936          }          } else {
5937              !!!cp ('t317');
5938            }
5939    
5940          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5941              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5942            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
5943              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5944              !!!nack ('t318.1');
5945            !!!next-token;            !!!next-token;
5946            redo B;            next B;
5947          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5948                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5949            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
5950              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5951            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5952              !!!ack ('t319.1');
5953            !!!next-token;            !!!next-token;
5954            redo B;            next B;
5955          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5956              !!!cp ('t320');
5957            ## NOTE: As if in body.            ## NOTE: As if in body.
5958            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5959            redo B;            next B;
5960          } else {          } else {
5961            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5962              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
5963                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5964            } else {            } else {
5965              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!cp ('t322');
5966                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5967            }            }
5968            ## Ignore the token            ## Ignore the token
5969              !!!nack ('t322.1');
5970            !!!next-token;            !!!next-token;
5971            redo B;            next B;
5972          }          }
5973        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5974          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5975            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t323');
5976              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5977    
5978            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5979            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5980            } else {
5981              !!!cp ('t324');
5982          }          }
5983    
5984          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5985              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5986            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5987                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5988              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
5989                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5990              ## Ignore the token              ## Ignore the token
5991              !!!next-token;              !!!next-token;
5992            } else {            } else {
5993                !!!cp ('t326');
5994              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5995              !!!next-token;              !!!next-token;
5996            }            }
5997    
5998            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5999                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6000                !!!cp ('t327');
6001              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6002              } else {
6003                !!!cp ('t328');
6004            }            }
6005            redo B;            next B;
6006          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6007                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6008              !!!cp ('t329');
6009            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6010            !!!next-token;            !!!next-token;
6011            redo B;            next B;
6012          } else {          } else {
6013            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6014              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6015                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6016            } else {            } else {
6017              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!cp ('t331');
6018                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6019            }            }
6020            ## Ignore the token            ## Ignore the token
6021            !!!next-token;            !!!next-token;
6022            redo B;            next B;
6023            }
6024          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6025            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6026                    @{$self->{open_elements}} == 1) { # redundant, maybe
6027              !!!cp ('t331.1');
6028              !!!parse-error (type => 'in body:#eof', token => $token);
6029            } else {
6030              !!!cp ('t331.2');
6031          }          }
6032            
6033            ## Stop parsing
6034            last B;
6035        } else {        } else {
6036          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6037        }        }
# Line 4436  sub _tree_construction_main ($) { Line 6044  sub _tree_construction_main ($) {
6044      ## "in body" insertion mode      ## "in body" insertion mode
6045      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
6046        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6047            !!!cp ('t332');
6048          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6049          $script_start_tag->($insert);          $script_start_tag->();
6050          redo B;          next B;
6051        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6052            !!!cp ('t333');
6053          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6054          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6055          redo B;          next B;
6056        } elsif ({        } elsif ({
6057                  base => 1, link => 1,                  base => 1, link => 1,
6058                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6059            !!!cp ('t334');
6060          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
6061          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6062          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6063            !!!ack ('t334.1');
6064          !!!next-token;          !!!next-token;
6065          redo B;          next B;
6066        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6067          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
6068          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6069          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6070    
6071          unless ($self->{confident}) {          unless ($self->{confident}) {
6072            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
6073                !!!cp ('t335');
6074              $self->{change_encoding}              $self->{change_encoding}
6075                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6076                
6077                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6078                    ->set_user_data (manakai_has_reference =>
6079                                         $token->{attributes}->{charset}
6080                                             ->{has_reference});
6081            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
6082              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
6083              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6084                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6085                        [\x09-\x0D\x20]*=
6086                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6087                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6088                  !!!cp ('t336');
6089                $self->{change_encoding}                $self->{change_encoding}
6090                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6091                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6092                      ->set_user_data (manakai_has_reference =>
6093                                           $token->{attributes}->{content}
6094                                                 ->{has_reference});
6095              }              }
6096            }            }
6097            } else {
6098              if ($token->{attributes}->{charset}) {
6099                !!!cp ('t337');
6100                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6101                    ->set_user_data (manakai_has_reference =>
6102                                         $token->{attributes}->{charset}
6103                                             ->{has_reference});
6104              }
6105              if ($token->{attributes}->{content}) {
6106                !!!cp ('t338');
6107                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6108                    ->set_user_data (manakai_has_reference =>
6109                                         $token->{attributes}->{content}
6110                                             ->{has_reference});
6111              }
6112          }          }
6113    
6114            !!!ack ('t338.1');
6115          !!!next-token;          !!!next-token;
6116          redo B;          next B;
6117        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6118          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6119          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6120          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6121            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6122        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6123          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6124                                
6125          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6126              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6127              !!!cp ('t342');
6128            ## Ignore the token            ## Ignore the token
6129          } else {          } else {
6130            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6131            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6132              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6133                  !!!cp ('t343');
6134                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6135                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6136                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6137              }              }
6138            }            }
6139          }          }
6140            !!!nack ('t343.1');
6141          !!!next-token;          !!!next-token;
6142          redo B;          next B;
6143        } elsif ({        } elsif ({
6144                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6145                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6146                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6147                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6148                  pre => 1,                  pre => 1, listing => 1,
6149                    form => 1,
6150                    table => 1,
6151                    hr => 1,
6152                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6153            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6154              !!!cp ('t350');
6155              !!!parse-error (type => 'in form:form', token => $token);
6156              ## Ignore the token
6157              !!!nack ('t350.1');
6158              !!!next-token;
6159              next B;
6160            }
6161    
6162          ## has a p element in scope          ## has a p element in scope
6163          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6164            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6165              !!!back-token;              !!!cp ('t344');
6166              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
6167              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6168            } elsif ({                        line => $token->{line}, column => $token->{column}};
6169                      table => 1, caption => 1, td => 1, th => 1,              next B;
6170                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6171                     }->{$_->[1]}) {              !!!cp ('t345');
6172              last INSCOPE;              last INSCOPE;
6173            }            }
6174          } # INSCOPE          } # INSCOPE
6175                        
6176          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6177          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6178              !!!nack ('t346.1');
6179            !!!next-token;            !!!next-token;
6180            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6181              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6182              unless (length $token->{data}) {              unless (length $token->{data}) {
6183                  !!!cp ('t346');
6184                !!!next-token;                !!!next-token;
6185                } else {
6186                  !!!cp ('t349');
6187              }              }
6188              } else {
6189                !!!cp ('t348');
6190            }            }
6191          } else {          } elsif ($token->{tag_name} eq 'form') {
6192              !!!cp ('t347.1');
6193              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6194    
6195              !!!nack ('t347.2');
6196            !!!next-token;            !!!next-token;
6197          }          } elsif ($token->{tag_name} eq 'table') {
6198          redo B;            !!!cp ('t382');
6199        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6200          if (defined $self->{form_element}) {            
6201            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6202            ## Ignore the token  
6203              !!!nack ('t382.1');
6204              !!!next-token;
6205            } elsif ($token->{tag_name} eq 'hr') {
6206              !!!cp ('t386');
6207              pop @{$self->{open_elements}};
6208            
6209              !!!nack ('t386.1');
6210            !!!next-token;            !!!next-token;
           redo B;  
6211          } else {          } else {
6212            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6213            !!!next-token;            !!!next-token;
           redo B;  
6214          }          }
6215        } elsif ($token->{tag_name} eq 'li') {          next B;
6216          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6217          ## has a p element in scope          ## has a p element in scope
6218          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6219            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6220              !!!back-token;              !!!cp ('t353');
6221              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
6222              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6223            } elsif ({                        line => $token->{line}, column => $token->{column}};
6224                      table => 1, caption => 1, td => 1, th => 1,              next B;
6225                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6226                     }->{$_->[1]}) {              !!!cp ('t354');
6227              last INSCOPE;              last INSCOPE;
6228            }            }
6229          } # INSCOPE          } # INSCOPE
# Line 4580  sub _tree_construction_main ($) { Line 6231  sub _tree_construction_main ($) {
6231          ## Step 1          ## Step 1
6232          my $i = -1;          my $i = -1;
6233          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6234            my $li_or_dtdd = {li => {li => 1},
6235                              dt => {dt => 1, dd => 1},
6236                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6237          LI: {          LI: {
6238            ## Step 2            ## Step 2
6239            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6240              if ($i != -1) {              if ($i != -1) {
6241                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6242                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6243              }                                value => $self->{open_elements}->[-1]->[0]
6244              splice @{$self->{open_elements}}, $i;                                    ->manakai_local_name,
6245              last LI;                                token => $token);
6246            }              } else {
6247                            !!!cp ('t356');
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
6248              }              }
6249              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6250              last LI;              last LI;
6251              } else {
6252                !!!cp ('t357');
6253            }            }
6254                        
6255            ## Step 3            ## Step 3
6256            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6257                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6258                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6259                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6260                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6261                  not ($node->[1] & DIV_EL)) {
6262                !!!cp ('t358');
6263              last LI;              last LI;
6264            }            }
6265                        
6266              !!!cp ('t359');
6267            ## Step 4            ## Step 4
6268            $i--;            $i--;
6269            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
6270            redo LI;            redo LI;
6271          } # LI          } # LI
6272                        
6273          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6274            !!!nack ('t359.1');
6275          !!!next-token;          !!!next-token;
6276          redo B;          next B;
6277        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6278          ## has a p element in scope          ## has a p element in scope
6279          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6280            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6281              !!!back-token;              !!!cp ('t367');
6282              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
6283              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6284            } elsif ({                        line => $token->{line}, column => $token->{column}};
6285                      table => 1, caption => 1, td => 1, th => 1,              next B;
6286                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6287                     }->{$_->[1]}) {              !!!cp ('t368');
6288              last INSCOPE;              last INSCOPE;
6289            }            }
6290          } # INSCOPE          } # INSCOPE
6291                        
6292          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6293                        
6294          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6295                        
6296            !!!nack ('t368.1');
6297          !!!next-token;          !!!next-token;
6298          redo B;          next B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
6299        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6300          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6301            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6302            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6303              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
6304                !!!parse-error (type => 'in a:a', token => $token);
6305                            
6306              !!!back-token;              !!!back-token; # <a>
6307              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6308              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6309                $formatting_end_tag->($token);
6310                            
6311              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6312                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6313                    !!!cp ('t372');
6314                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
6315                  last AFE2;                  last AFE2;
6316                }                }
6317              } # AFE2              } # AFE2
6318              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
6319                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6320                    !!!cp ('t373');
6321                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
6322                  last OE;                  last OE;
6323                }                }
6324              } # OE              } # OE
6325              last AFE;              last AFE;
6326            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
6327                !!!cp ('t374');
6328              last AFE;              last AFE;
6329            }            }
6330          } # AFE          } # AFE
6331                        
6332          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6333    
6334          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6335          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6336    
6337            !!!nack ('t374.1');
6338          !!!next-token;          !!!next-token;
6339          redo B;          next B;
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         redo B;  
6340        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6341          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6342    
6343          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6344          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6345            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6346            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6347              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
6348              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
6349              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
6350              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6351            } elsif ({                        line => $token->{line}, column => $token->{column}};
6352                      table => 1, caption => 1, td => 1, th => 1,              next B;
6353                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6354                     }->{$node->[1]}) {              !!!cp ('t377');
6355              last INSCOPE;              last INSCOPE;
6356            }            }
6357          } # INSCOPE          } # INSCOPE
6358                    
6359          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6360          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6361                    
6362            !!!nack ('t377.1');
6363          !!!next-token;          !!!next-token;
6364          redo B;          next B;
6365        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6366          ## has a button element in scope          ## has a button element in scope
6367          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6368            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6369            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6370              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
6371              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
6372              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
6373              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6374            } elsif ({                        line => $token->{line}, column => $token->{column}};
6375                      table => 1, caption => 1, td => 1, th => 1,              next B;
6376                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6377                     }->{$node->[1]}) {              !!!cp ('t379');
6378              last INSCOPE;              last INSCOPE;
6379            }            }
6380          } # INSCOPE          } # INSCOPE
6381                        
6382          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6383                        
6384          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6385          push @$active_formatting_elements, ['#marker', ''];  
6386            ## TODO: associate with $self->{form_element} if defined
6387    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6388          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6389            
6390          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
6391          !!!next-token;          !!!next-token;
6392          redo B;          next B;
6393        } elsif ({        } elsif ({
6394                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6395                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6396                  image => 1,                  noembed => 1,
6397                    noframes => 1,
6398                    noscript => 0, ## TODO: 1 if scripting is enabled
6399                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6400          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6401            !!!parse-error (type => 'image');            !!!cp ('t381');
6402            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
6403            } else {
6404              !!!cp ('t399');
6405          }          }
6406            ## NOTE: There is an "as if in body" code clone.
6407          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6408          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
6409        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6410          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6411                    
6412          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6413              !!!cp ('t389');
6414            ## Ignore the token            ## Ignore the token
6415              !!!nack ('t389'); ## NOTE: Not acknowledged.
6416            !!!next-token;            !!!next-token;
6417            redo B;            next B;
6418          } else {          } else {
6419            my $at = $token->{attributes};            my $at = $token->{attributes};
6420            my $form_attrs;            my $form_attrs;
# Line 4915  sub _tree_construction_main ($) { Line 6425  sub _tree_construction_main ($) {
6425            delete $at->{prompt};            delete $at->{prompt};
6426            my @tokens = (            my @tokens = (
6427                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6428                           attributes => $form_attrs},                           attributes => $form_attrs,
6429                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6430                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6431                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6432                            {type => START_TAG_TOKEN, tag_name => 'p',
6433                             line => $token->{line}, column => $token->{column}},
6434                            {type => START_TAG_TOKEN, tag_name => 'label',
6435                             line => $token->{line}, column => $token->{column}},
6436                         );                         );
6437            if ($prompt_attr) {            if ($prompt_attr) {
6438              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
6439                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6440                               #line => $token->{line}, column => $token->{column},
6441                              };
6442            } else {            } else {
6443                !!!cp ('t391');
6444              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6445                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6446                               #line => $token->{line}, column => $token->{column},
6447                              }; # SHOULD
6448              ## TODO: make this configurable              ## TODO: make this configurable
6449            }            }
6450            push @tokens,            push @tokens,
6451                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6452                             line => $token->{line}, column => $token->{column}},
6453                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6454                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6455                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6456                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6457                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6458            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6459                             line => $token->{line}, column => $token->{column}},
6460                            {type => END_TAG_TOKEN, tag_name => 'form',
6461                             line => $token->{line}, column => $token->{column}};
6462              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6463            !!!back-token (@tokens);            !!!back-token (@tokens);
6464            redo B;            !!!next-token;
6465              next B;
6466          }          }
6467        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6468          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6469          my $el;          my $el;
6470          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6471                    
6472          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6473          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4950  sub _tree_construction_main ($) { Line 6476  sub _tree_construction_main ($) {
6476          $insert->($el);          $insert->($el);
6477                    
6478          my $text = '';          my $text = '';
6479            !!!nack ('t392.1');
6480          !!!next-token;          !!!next-token;
6481          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6482            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
6483            unless (length $token->{data}) {            unless (length $token->{data}) {
6484                !!!cp ('t392');
6485              !!!next-token;              !!!next-token;
6486              } else {
6487                !!!cp ('t393');
6488            }            }
6489            } else {
6490              !!!cp ('t394');
6491          }          }
6492          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
6493              !!!cp ('t395');
6494            $text .= $token->{data};            $text .= $token->{data};
6495            !!!next-token;            !!!next-token;
6496          }          }
6497          if (length $text) {          if (length $text) {
6498              !!!cp ('t396');
6499            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6500          }          }
6501                    
# Line 4969  sub _tree_construction_main ($) { Line 6503  sub _tree_construction_main ($) {
6503                    
6504          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
6505              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6506              !!!cp ('t397');
6507            ## Ignore the token            ## Ignore the token
6508          } else {          } else {
6509            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6510              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6511          }          }
6512          !!!next-token;          !!!next-token;
6513          redo B;          next B;
6514        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6515                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
6516          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6517    
6518            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6519    
6520            ## "adjust foreign attributes" - done in insert-element-f
6521                    
6522          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6523                    
6524          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6525              pop @{$self->{open_elements}};
6526              !!!ack ('t398.1');
6527            } else {
6528              !!!cp ('t398.2');
6529              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6530              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6531              ## mode, "in body" (not "in foreign content") secondary insertion
6532              ## mode, maybe.
6533            }
6534    
6535          !!!next-token;          !!!next-token;
6536          redo B;          next B;
6537        } elsif ({        } elsif ({
6538                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6539                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6540                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6541                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6542                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6543          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
6544            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6545          ## Ignore the token          ## Ignore the token
6546            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6547          !!!next-token;          !!!next-token;
6548          redo B;          next B;
6549                    
6550          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6551        } else {        } else {
6552            if ($token->{tag_name} eq 'image') {
6553              !!!cp ('t384');
6554              !!!parse-error (type => 'image', token => $token);
6555              $token->{tag_name} = 'img';
6556            } else {
6557              !!!cp ('t385');
6558            }
6559    
6560            ## NOTE: There is an "as if <br>" code clone.
6561          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6562                    
6563          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6564    
6565            if ({
6566                 applet => 1, marquee => 1, object => 1,
6567                }->{$token->{tag_name}}) {
6568              !!!cp ('t380');
6569              push @$active_formatting_elements, ['#marker', ''];
6570              !!!nack ('t380.1');
6571            } elsif ({
6572                      b => 1, big => 1, em => 1, font => 1, i => 1,
6573                      s => 1, small => 1, strile => 1,
6574                      strong => 1, tt => 1, u => 1,
6575                     }->{$token->{tag_name}}) {
6576              !!!cp ('t375');
6577              push @$active_formatting_elements, $self->{open_elements}->[-1];
6578              !!!nack ('t375.1');
6579            } elsif ($token->{tag_name} eq 'input') {
6580              !!!cp ('t388');
6581              ## TODO: associate with $self->{form_element} if defined
6582              pop @{$self->{open_elements}};
6583              !!!ack ('t388.2');
6584            } elsif ({
6585                      area => 1, basefont => 1, bgsound => 1, br => 1,
6586                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6587                      #image => 1,
6588                     }->{$token->{tag_name}}) {
6589              !!!cp ('t388.1');
6590              pop @{$self->{open_elements}};
6591              !!!ack ('t388.3');
6592            } elsif ($token->{tag_name} eq 'select') {
6593              ## TODO: associate with $self->{form_element} if defined
6594            
6595              if ($self->{insertion_mode} & TABLE_IMS or
6596                  $self->{insertion_mode} & BODY_TABLE_IMS or
6597                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6598                !!!cp ('t400.1');
6599                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6600              } else {
6601                !!!cp ('t400.2');
6602                $self->{insertion_mode} = IN_SELECT_IM;
6603              }
6604              !!!nack ('t400.3');
6605            } else {
6606              !!!nack ('t402');
6607            }
6608                    
6609          !!!next-token;          !!!next-token;
6610          redo B;          next B;
6611        }        }
6612      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6613        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6614          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6615              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6616            for (@{$self->{open_elements}}) {          INSCOPE: {
6617              unless ({            for (reverse @{$self->{open_elements}}) {
6618                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6619                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6620                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6621                      }->{$_->[1]}) {                last INSCOPE;
6622                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
6623                  !!!cp ('t405.1');
6624                  last;
6625              }              }
6626            }            }
6627    
6628            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6629                              value => $token->{tag_name}, token => $token);
6630              ## NOTE: Ignore the token.
6631            !!!next-token;            !!!next-token;
6632            redo B;            next B;
6633          } else {          } # INSCOPE
6634            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
6635            ## Ignore the token          for (@{$self->{open_elements}}) {
6636            !!!next-token;            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6637            redo B;              !!!cp ('t403');
6638                !!!parse-error (type => 'not closed',
6639                                value => $_->[0]->manakai_local_name,
6640                                token => $token);
6641                last;
6642              } else {
6643                !!!cp ('t404');
6644              }
6645          }          }
6646    
6647            $self->{insertion_mode} = AFTER_BODY_IM;
6648            !!!next-token;
6649            next B;
6650        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6651          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          ## TODO: Update this code.  It seems that the code below is not
6652            ## up-to-date, though it has same effect as speced.
6653            if (@{$self->{open_elements}} > 1 and
6654                $self->{open_elements}->[1]->[1] & BODY_EL) {
6655            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6656            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6657              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
6658                !!!parse-error (type => 'not closed',
6659                                value => $self->{open_elements}->[1]->[0]
6660                                    ->manakai_local_name,
6661                                token => $token);
6662              } else {
6663                !!!cp ('t407');
6664            }            }
6665            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6666            ## reprocess            ## reprocess
6667            redo B;            next B;
6668          } else {          } else {
6669            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
6670              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6671            ## Ignore the token            ## Ignore the token
6672            !!!next-token;            !!!next-token;
6673            redo B;            next B;
6674          }          }
6675        } elsif ({        } elsif ({
6676                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6677                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6678                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6679                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6680                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6681                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6682          ## has an element in scope          ## has an element in scope
6683          my $i;          my $i;
6684          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6685            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6686            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6687              ## generate implied end tags              !!!cp ('t410');
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6688              $i = $_;              $i = $_;
6689              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6690            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6691                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6692              last INSCOPE;              last INSCOPE;
6693            }            }
6694          } # INSCOPE          } # INSCOPE
6695            
6696          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6697            if (defined $i) {            !!!cp ('t413');
6698              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6699            } else {
6700              ## Step 1. generate implied end tags
6701              while ({
6702                      dd => ($token->{tag_name} ne 'dd'),
6703                      dt => ($token->{tag_name} ne 'dt'),
6704                      li => ($token->{tag_name} ne 'li'),
6705                      p => 1,
6706                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6707                !!!cp ('t409');
6708                pop @{$self->{open_elements}};
6709              }
6710    
6711              ## Step 2.
6712              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6713                      ne $token->{tag_name}) {
6714                !!!cp ('t412');
6715                !!!parse-error (type => 'not closed',
6716                                value => $self->{open_elements}->[-1]->[0]
6717                                    ->manakai_local_name,
6718                                token => $token);
6719            } else {            } else {
6720              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6721            }            }
6722          }  
6723                      ## Step 3.
         if (defined $i) {  
6724            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6725          } elsif ($token->{tag_name} eq 'p') {  
6726            ## As if <p>, then reprocess the current token            ## Step 4.
6727            my $el;            $clear_up_to_marker->()
6728            !!!create-element ($el, 'p');                if {
6729            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
6730                  }->{$token->{tag_name}};
6731          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6732          !!!next-token;          !!!next-token;
6733          redo B;          next B;
6734        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6735            undef $self->{form_element};
6736    
6737          ## has an element in scope          ## has an element in scope
6738            my $i;
6739          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6740            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6741            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6742              ## generate implied end tags              !!!cp ('t418');
6743              if ({              $i = $_;
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6744              last INSCOPE;              last INSCOPE;
6745            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6746                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6747              last INSCOPE;              last INSCOPE;
6748            }            }
6749          } # INSCOPE          } # INSCOPE
6750            
6751          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6752            pop @{$self->{open_elements}};            !!!cp ('t421');
6753          } else {            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6754            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } else {
6755              ## Step 1. generate implied end tags
6756              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6757                !!!cp ('t417');
6758                pop @{$self->{open_elements}};
6759              }
6760              
6761              ## Step 2.
6762              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6763                      ne $token->{tag_name}) {
6764                !!!cp ('t417.1');
6765                !!!parse-error (type => 'not closed',
6766                                value => $self->{open_elements}->[-1]->[0]
6767                                    ->manakai_local_name,
6768                                token => $token);
6769              } else {
6770                !!!cp ('t420');
6771              }  
6772              
6773              ## Step 3.
6774              splice @{$self->{open_elements}}, $i;
6775          }          }
6776    
         undef $self->{form_element};  
6777          !!!next-token;          !!!next-token;
6778          redo B;          next B;
6779        } elsif ({        } elsif ({
6780                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6781                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5150  sub _tree_construction_main ($) { Line 6783  sub _tree_construction_main ($) {
6783          my $i;          my $i;
6784          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6785            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6786            if ({            if ($node->[1] & HEADING_EL) {
6787                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,              !!!cp ('t423');
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6788              $i = $_;              $i = $_;
6789              last INSCOPE;              last INSCOPE;
6790            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6791                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6792              last INSCOPE;              last INSCOPE;
6793            }            }
6794          } # INSCOPE          } # INSCOPE
6795            
6796          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6797            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
6798              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6799            } else {
6800              ## Step 1. generate implied end tags
6801              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6802                !!!cp ('t422');
6803                pop @{$self->{open_elements}};
6804              }
6805              
6806              ## Step 2.
6807              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6808                      ne $token->{tag_name}) {
6809                !!!cp ('t425');
6810                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6811              } else {
6812                !!!cp ('t426');
6813              }
6814    
6815              ## Step 3.
6816              splice @{$self->{open_elements}}, $i;
6817          }          }
6818                    
         splice @{$self->{open_elements}}, $i if defined $i;  
6819          !!!next-token;          !!!next-token;
6820          redo B;          next B;
6821          } elsif ($token->{tag_name} eq 'p') {
6822            ## has an element in scope
6823            my $i;
6824            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6825              my $node = $self->{open_elements}->[$_];
6826              if ($node->[1] & P_EL) {
6827                !!!cp ('t410.1');
6828                $i = $_;
6829                last INSCOPE;
6830              } elsif ($node->[1] & SCOPING_EL) {
6831                !!!cp ('t411.1');
6832                last INSCOPE;
6833              }
6834            } # INSCOPE
6835    
6836            if (defined $i) {
6837              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6838                      ne $token->{tag_name}) {
6839                !!!cp ('t412.1');
6840                !!!parse-error (type => 'not closed',
6841                                value => $self->{open_elements}->[-1]->[0]
6842                                    ->manakai_local_name,
6843                                token => $token);
6844              } else {
6845                !!!cp ('t414.1');
6846              }
6847    
6848              splice @{$self->{open_elements}}, $i;
6849            } else {
6850              !!!cp ('t413.1');
6851              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6852    
6853              !!!cp ('t415.1');
6854              ## As if <p>, then reprocess the current token
6855              my $el;
6856              !!!create-element ($el, $HTML_NS, 'p',, $token);
6857              $insert->($el);
6858              ## NOTE: Not inserted into |$self->{open_elements}|.
6859            }
6860    
6861            !!!next-token;
6862            next B;
6863        } elsif ({        } elsif ({
6864                  a => 1,                  a => 1,
6865                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
6866                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
6867                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6868                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6869          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
6870          redo B;          $formatting_end_tag->($token);
6871            next B;
6872        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6873          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
6874            !!!parse-error (type => 'unmatched end tag:br', token => $token);
6875    
6876          ## As if <br>          ## As if <br>
6877          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6878                    
6879          my $el;          my $el;
6880          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
6881          $insert->($el);          $insert->($el);
6882                    
6883          ## Ignore the token.          ## Ignore the token.
6884          !!!next-token;          !!!next-token;
6885          redo B;          next B;
6886        } elsif ({        } elsif ({
6887                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6888                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5214  sub _tree_construction_main ($) { Line 6895  sub _tree_construction_main ($) {
6895                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
6896                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6897                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6898          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
6899            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6900          ## Ignore the token          ## Ignore the token
6901          !!!next-token;          !!!next-token;
6902          redo B;          next B;
6903                    
6904          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
6905                    
# Line 5228  sub _tree_construction_main ($) { Line 6910  sub _tree_construction_main ($) {
6910    
6911          ## Step 2          ## Step 2
6912          S2: {          S2: {
6913            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6914              ## Step 1              ## Step 1
6915              ## generate implied end tags              ## generate implied end tags
6916              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6917                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
6918                   td => 1, th => 1, tr => 1,                ## ISSUE: Can this case be reached?
6919                   tbody => 1, tfoot => 1, thead => 1,                pop @{$self->{open_elements}};
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
6920              }              }
6921                    
6922              ## Step 2              ## Step 2
6923              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6924                        ne $token->{tag_name}) {
6925                  !!!cp ('t431');
6926                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6927                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6928                                  value => $self->{open_elements}->[-1]->[0]
6929                                      ->manakai_local_name,
6930                                  token => $token);
6931                } else {
6932                  !!!cp ('t432');
6933              }              }
6934                            
6935              ## Step 3              ## Step 3
# Line 5255  sub _tree_construction_main ($) { Line 6939  sub _tree_construction_main ($) {
6939              last S2;              last S2;
6940            } else {            } else {
6941              ## Step 3              ## Step 3
6942              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
6943                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6944                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
6945                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
6946                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
6947                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6948                ## Ignore the token                ## Ignore the token
6949                !!!next-token;                !!!next-token;
6950                last S2;                last S2;
6951              }              }
6952    
6953                !!!cp ('t434');
6954            }            }
6955                        
6956            ## Step 4            ## Step 4
# Line 5273  sub _tree_construction_main ($) { Line 6960  sub _tree_construction_main ($) {
6960            ## Step 5;            ## Step 5;
6961            redo S2;            redo S2;
6962          } # S2          } # S2
6963          redo B;          next B;
6964        }        }
6965      }      }
6966      redo B;      next B;
6967      } continue { # B
6968        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
6969          ## NOTE: The code below is executed in cases where it does not have
6970          ## to be, but it it is harmless even in those cases.
6971          ## has an element in scope
6972          INSCOPE: {
6973            for (reverse 0..$#{$self->{open_elements}}) {
6974              my $node = $self->{open_elements}->[$_];
6975              if ($node->[1] & FOREIGN_EL) {
6976                last INSCOPE;
6977              } elsif ($node->[1] & SCOPING_EL) {
6978                last;
6979              }
6980            }
6981            
6982            ## NOTE: No foreign element in scope.
6983            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
6984          } # INSCOPE
6985        }
6986    } # B    } # B
6987    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
6988    ## Stop parsing # MUST    ## Stop parsing # MUST
6989        
6990    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5325  sub set_inner_html ($$$) { Line 7026  sub set_inner_html ($$$) {
7026      my $p = $class->new;      my $p = $class->new;
7027      $p->{document} = $doc;      $p->{document} = $doc;
7028    
7029      ## Step 9 # MUST      ## Step 8 # MUST
7030      my $i = 0;      my $i = 0;
7031      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7032      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7033      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7034        my $self = shift;        my $self = shift;
7035    
7036        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7037        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7038    
7039          $self->{next_char} = -1 and return if $i >= length $$s;
7040          $self->{next_char} = ord substr $$s, $i++, 1;
7041    
7042          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7043          $p->{column}++;
7044    
7045        $self->{next_input_character} = -1 and return if $i >= length $$s;        if ($self->{next_char} == 0x000A) { # LF
7046        $self->{next_input_character} = ord substr $$s, $i++, 1;          $p->{line}++;
7047        $column++;          $p->{column} = 0;
7048            !!!cp ('i1');
7049        if ($self->{next_input_character} == 0x000A) { # LF        } elsif ($self->{next_char} == 0x000D) { # CR
         $line++;  
         $column = 0;  
       } elsif ($self->{next_input_character} == 0x000D) { # CR  
7050          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7051          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7052          $line++;          $p->{line}++;
7053          $column = 0;          $p->{column} = 0;
7054        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7055          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7056        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7057            !!!cp ('i3');
7058          } elsif ($self->{next_char} == 0x0000) { # NULL
7059            !!!cp ('i4');
7060          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7061          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7062        }        }
7063      };      };
7064      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7065      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7066            
7067      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7068        my (%opt) = @_;        my (%opt) = @_;
7069        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7070          my $column = $opt{column};
7071          if (defined $opt{token} and defined $opt{token}->{line}) {
7072            $line = $opt{token}->{line};
7073            $column = $opt{token}->{column};
7074          }
7075          warn "Parse error ($opt{type}) at line $line column $column\n";
7076      };      };
7077      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7078        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7079      };      };
7080            
7081      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7082      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7083    
7084      ## Step 2      ## Step 2
7085      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7086      $p->{content_model} = {      $p->{content_model} = {
7087        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7088        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5386  sub set_inner_html ($$$) { Line 7099  sub set_inner_html ($$$) {
7099          unless defined $p->{content_model};          unless defined $p->{content_model};
7100          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7101    
7102      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7103          ## TODO: Foreign element OK?
7104    
7105      ## Step 4      ## Step 3
7106      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7107        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7108    
7109      ## Step 5 # MUST      ## Step 4 # MUST
7110      $doc->append_child ($root);      $doc->append_child ($root);
7111    
7112      ## Step 6 # MUST      ## Step 5 # MUST
7113      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7114    
7115      undef $p->{head_element};      undef $p->{head_element};
7116    
7117      ## Step 7 # MUST      ## Step 6 # MUST
7118      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7119    
7120      ## Step 8 # MUST      ## Step 7 # MUST
7121      my $anode = $node;      my $anode = $node;
7122      AN: while (defined $anode) {      AN: while (defined $anode) {
7123        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7124          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7125          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7126            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7127                !!!cp ('i5');
7128              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7129              last AN;              last AN;
7130            }            }
# Line 5418  sub set_inner_html ($$$) { Line 7133  sub set_inner_html ($$$) {
7133        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7134      } # AN      } # AN
7135            
7136      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7137      {      {
7138        my $self = $p;        my $self = $p;
7139        !!!next-token;        !!!next-token;
7140      }      }
7141      $p->_tree_construction_main;      $p->_tree_construction_main;
7142    
7143      ## Step 11 # MUST      ## Step 10 # MUST
7144      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7145      for (@cn) {      for (@cn) {
7146        $node->remove_child ($_);        $node->remove_child ($_);
7147      }      }
7148      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7149    
7150      ## Step 12 # MUST      ## Step 11 # MUST
7151      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7152      for (@cn) {      for (@cn) {
7153        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5442  sub set_inner_html ($$$) { Line 7156  sub set_inner_html ($$$) {
7156      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7157    
7158      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7159    
7160        delete $p->{parse_error}; # delete loop
7161    } else {    } else {
7162      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";
7163    }    }

Legend:
Removed from v.1.65  
changed lines
  Added in v.1.131

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24