/[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.97 by wakaba, Sat Mar 8 13:45:44 2008 UTC revision 1.132 by wakaba, Sun Apr 13 10:36:40 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    
 ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  
11  ## TODO: 1252 parse error (revision 1264)  ## TODO: 1252 parse error (revision 1264)
12  ## TODO: 8859-11 = 874 (revision 1271)  ## TODO: 8859-11 = 874 (revision 1271)
13    
14  my $permitted_slash_tag_name = {  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
15    base => 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
16    link => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
17    meta => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
18    hr => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
19    br => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
20    img => 1,  
21    embed => 1,  sub A_EL () { 0b1 }
22    param => 1,  sub ADDRESS_EL () { 0b10 }
23    area => 1,  sub BODY_EL () { 0b100 }
24    col => 1,  sub BUTTON_EL () { 0b1000 }
25    input => 1,  sub CAPTION_EL () { 0b10000 }
26    sub DD_EL () { 0b100000 }
27    sub DIV_EL () { 0b1000000 }
28    sub DT_EL () { 0b10000000 }
29    sub FORM_EL () { 0b100000000 }
30    sub FORMATTING_EL () { 0b1000000000 }
31    sub FRAMESET_EL () { 0b10000000000 }
32    sub HEADING_EL () { 0b100000000000 }
33    sub HTML_EL () { 0b1000000000000 }
34    sub LI_EL () { 0b10000000000000 }
35    sub NOBR_EL () { 0b100000000000000 }
36    sub OPTION_EL () { 0b1000000000000000 }
37    sub OPTGROUP_EL () { 0b10000000000000000 }
38    sub P_EL () { 0b100000000000000000 }
39    sub SELECT_EL () { 0b1000000000000000000 }
40    sub TABLE_EL () { 0b10000000000000000000 }
41    sub TABLE_CELL_EL () { 0b100000000000000000000 }
42    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
43    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
44    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
45    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
46    sub FOREIGN_EL () { 0b10000000000000000000000000 }
47    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
48    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
49    
50    sub TABLE_ROWS_EL () {
51      TABLE_EL |
52      TABLE_ROW_EL |
53      TABLE_ROW_GROUP_EL
54    }
55    
56    sub END_TAG_OPTIONAL_EL () {
57      DD_EL |
58      DT_EL |
59      LI_EL |
60      P_EL
61    }
62    
63    sub ALL_END_TAG_OPTIONAL_EL () {
64      END_TAG_OPTIONAL_EL |
65      BODY_EL |
66      HTML_EL |
67      TABLE_CELL_EL |
68      TABLE_ROW_EL |
69      TABLE_ROW_GROUP_EL
70    }
71    
72    sub SCOPING_EL () {
73      BUTTON_EL |
74      CAPTION_EL |
75      HTML_EL |
76      TABLE_EL |
77      TABLE_CELL_EL |
78      MISC_SCOPING_EL
79    }
80    
81    sub TABLE_SCOPING_EL () {
82      HTML_EL |
83      TABLE_EL
84    }
85    
86    sub TABLE_ROWS_SCOPING_EL () {
87      HTML_EL |
88      TABLE_ROW_GROUP_EL
89    }
90    
91    sub TABLE_ROW_SCOPING_EL () {
92      HTML_EL |
93      TABLE_ROW_EL
94    }
95    
96    sub SPECIAL_EL () {
97      ADDRESS_EL |
98      BODY_EL |
99      DIV_EL |
100      END_TAG_OPTIONAL_EL |
101      FORM_EL |
102      FRAMESET_EL |
103      HEADING_EL |
104      OPTION_EL |
105      OPTGROUP_EL |
106      SELECT_EL |
107      TABLE_ROW_EL |
108      TABLE_ROW_GROUP_EL |
109      MISC_SPECIAL_EL
110    }
111    
112    my $el_category = {
113      a => A_EL | FORMATTING_EL,
114      address => ADDRESS_EL,
115      applet => MISC_SCOPING_EL,
116      area => MISC_SPECIAL_EL,
117      b => FORMATTING_EL,
118      base => MISC_SPECIAL_EL,
119      basefont => MISC_SPECIAL_EL,
120      bgsound => MISC_SPECIAL_EL,
121      big => FORMATTING_EL,
122      blockquote => MISC_SPECIAL_EL,
123      body => BODY_EL,
124      br => MISC_SPECIAL_EL,
125      button => BUTTON_EL,
126      caption => CAPTION_EL,
127      center => MISC_SPECIAL_EL,
128      col => MISC_SPECIAL_EL,
129      colgroup => MISC_SPECIAL_EL,
130      dd => DD_EL,
131      dir => MISC_SPECIAL_EL,
132      div => DIV_EL,
133      dl => MISC_SPECIAL_EL,
134      dt => DT_EL,
135      em => FORMATTING_EL,
136      embed => MISC_SPECIAL_EL,
137      fieldset => MISC_SPECIAL_EL,
138      font => FORMATTING_EL,
139      form => FORM_EL,
140      frame => MISC_SPECIAL_EL,
141      frameset => FRAMESET_EL,
142      h1 => HEADING_EL,
143      h2 => HEADING_EL,
144      h3 => HEADING_EL,
145      h4 => HEADING_EL,
146      h5 => HEADING_EL,
147      h6 => HEADING_EL,
148      head => MISC_SPECIAL_EL,
149      hr => MISC_SPECIAL_EL,
150      html => HTML_EL,
151      i => FORMATTING_EL,
152      iframe => MISC_SPECIAL_EL,
153      img => MISC_SPECIAL_EL,
154      input => MISC_SPECIAL_EL,
155      isindex => MISC_SPECIAL_EL,
156      li => LI_EL,
157      link => MISC_SPECIAL_EL,
158      listing => MISC_SPECIAL_EL,
159      marquee => MISC_SCOPING_EL,
160      menu => MISC_SPECIAL_EL,
161      meta => MISC_SPECIAL_EL,
162      nobr => NOBR_EL | FORMATTING_EL,
163      noembed => MISC_SPECIAL_EL,
164      noframes => MISC_SPECIAL_EL,
165      noscript => MISC_SPECIAL_EL,
166      object => MISC_SCOPING_EL,
167      ol => MISC_SPECIAL_EL,
168      optgroup => OPTGROUP_EL,
169      option => OPTION_EL,
170      p => P_EL,
171      param => MISC_SPECIAL_EL,
172      plaintext => MISC_SPECIAL_EL,
173      pre => MISC_SPECIAL_EL,
174      s => FORMATTING_EL,
175      script => MISC_SPECIAL_EL,
176      select => SELECT_EL,
177      small => FORMATTING_EL,
178      spacer => MISC_SPECIAL_EL,
179      strike => FORMATTING_EL,
180      strong => FORMATTING_EL,
181      style => MISC_SPECIAL_EL,
182      table => TABLE_EL,
183      tbody => TABLE_ROW_GROUP_EL,
184      td => TABLE_CELL_EL,
185      textarea => MISC_SPECIAL_EL,
186      tfoot => TABLE_ROW_GROUP_EL,
187      th => TABLE_CELL_EL,
188      thead => TABLE_ROW_GROUP_EL,
189      title => MISC_SPECIAL_EL,
190      tr => TABLE_ROW_EL,
191      tt => FORMATTING_EL,
192      u => FORMATTING_EL,
193      ul => MISC_SPECIAL_EL,
194      wbr => MISC_SPECIAL_EL,
195  };  };
196    
197    my $el_category_f = {
198      $MML_NS => {
199        'annotation-xml' => MML_AXML_EL,
200        mi => FOREIGN_FLOW_CONTENT_EL,
201        mo => FOREIGN_FLOW_CONTENT_EL,
202        mn => FOREIGN_FLOW_CONTENT_EL,
203        ms => FOREIGN_FLOW_CONTENT_EL,
204        mtext => FOREIGN_FLOW_CONTENT_EL,
205      },
206      $SVG_NS => {
207        foreignObject => FOREIGN_FLOW_CONTENT_EL,
208        desc => FOREIGN_FLOW_CONTENT_EL,
209        title => FOREIGN_FLOW_CONTENT_EL,
210      },
211      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
212    };
213    
214    my $svg_attr_name = {
215      attributetype => 'attributeType',
216      basefrequency => 'baseFrequency',
217      baseprofile => 'baseProfile',
218      calcmode => 'calcMode',
219      clippathunits => 'clipPathUnits',
220      contentscripttype => 'contentScriptType',
221      contentstyletype => 'contentStyleType',
222      diffuseconstant => 'diffuseConstant',
223      edgemode => 'edgeMode',
224      externalresourcesrequired => 'externalResourcesRequired',
225      fecolormatrix => 'feColorMatrix',
226      fecomposite => 'feComposite',
227      fegaussianblur => 'feGaussianBlur',
228      femorphology => 'feMorphology',
229      fetile => 'feTile',
230      filterres => 'filterRes',
231      filterunits => 'filterUnits',
232      glyphref => 'glyphRef',
233      gradienttransform => 'gradientTransform',
234      gradientunits => 'gradientUnits',
235      kernelmatrix => 'kernelMatrix',
236      kernelunitlength => 'kernelUnitLength',
237      keypoints => 'keyPoints',
238      keysplines => 'keySplines',
239      keytimes => 'keyTimes',
240      lengthadjust => 'lengthAdjust',
241      limitingconeangle => 'limitingConeAngle',
242      markerheight => 'markerHeight',
243      markerunits => 'markerUnits',
244      markerwidth => 'markerWidth',
245      maskcontentunits => 'maskContentUnits',
246      maskunits => 'maskUnits',
247      numoctaves => 'numOctaves',
248      pathlength => 'pathLength',
249      patterncontentunits => 'patternContentUnits',
250      patterntransform => 'patternTransform',
251      patternunits => 'patternUnits',
252      pointsatx => 'pointsAtX',
253      pointsaty => 'pointsAtY',
254      pointsatz => 'pointsAtZ',
255      preservealpha => 'preserveAlpha',
256      preserveaspectratio => 'preserveAspectRatio',
257      primitiveunits => 'primitiveUnits',
258      refx => 'refX',
259      refy => 'refY',
260      repeatcount => 'repeatCount',
261      repeatdur => 'repeatDur',
262      requiredextensions => 'requiredExtensions',
263      specularconstant => 'specularConstant',
264      specularexponent => 'specularExponent',
265      spreadmethod => 'spreadMethod',
266      startoffset => 'startOffset',
267      stddeviation => 'stdDeviation',
268      stitchtiles => 'stitchTiles',
269      surfacescale => 'surfaceScale',
270      systemlanguage => 'systemLanguage',
271      tablevalues => 'tableValues',
272      targetx => 'targetX',
273      targety => 'targetY',
274      textlength => 'textLength',
275      viewbox => 'viewBox',
276      viewtarget => 'viewTarget',
277      xchannelselector => 'xChannelSelector',
278      ychannelselector => 'yChannelSelector',
279      zoomandpan => 'zoomAndPan',
280    };
281    
282    my $foreign_attr_xname = {
283      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
284      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
285      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
286      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
287      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
288      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
289      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
290      'xml:base' => [$XML_NS, ['xml', 'base']],
291      'xml:lang' => [$XML_NS, ['xml', 'lang']],
292      'xml:space' => [$XML_NS, ['xml', 'space']],
293      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
294      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
295    };
296    
297    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
298    
299  my $c1_entity_char = {  my $c1_entity_char = {
300    0x80 => 0x20AC,    0x80 => 0x20AC,
301    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 331  my $c1_entity_char = {
331    0x9F => 0x0178,    0x9F => 0x0178,
332  }; # $c1_entity_char  }; # $c1_entity_char
333    
 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  
   
334  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
335    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
336    my $charset = shift;    my $charset = shift;
# Line 108  sub parse_byte_string ($$$$;$) { Line 356  sub parse_byte_string ($$$$;$) {
356    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
357      my $self = shift;      my $self = shift;
358      my $charset = lc shift;      my $charset = lc shift;
359        my $token = shift;
360      ## TODO: if $charset is supported      ## TODO: if $charset is supported
361      ## TODO: normalize charset name      ## TODO: normalize charset name
362    
# Line 126  sub parse_byte_string ($$$$;$) { Line 375  sub parse_byte_string ($$$$;$) {
375      }      }
376    
377      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
378          ':'.$charset, level => 'w');          ':'.$charset, level => 'w', token => $token);
379    
380      ## Step 3      ## Step 3
381      # if (can) {      # if (can) {
# Line 177  sub parse_string ($$$;$) { Line 426  sub parse_string ($$$;$) {
426        if defined $self->{input_encoding};        if defined $self->{input_encoding};
427    
428    my $i = 0;    my $i = 0;
429    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
430    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
431    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
432      my $self = shift;      my $self = shift;
433    
# Line 187  sub parse_string ($$$;$) { Line 436  sub parse_string ($$$;$) {
436    
437      $self->{next_char} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
438      $self->{next_char} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
439      $column++;  
440        ($self->{line_prev}, $self->{column_prev})
441            = ($self->{line}, $self->{column});
442        $self->{column}++;
443            
444      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
445        $line++;        !!!cp ('j1');
446        $column = 0;        $self->{line}++;
447          $self->{column} = 0;
448      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
449          !!!cp ('j2');
450        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
451        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
452        $line++;        $self->{line}++;
453        $column = 0;        $self->{column} = 0;
454      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
455          !!!cp ('j3');
456        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
457      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
458          !!!cp ('j4');
459        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
460        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
461        } elsif ($self->{next_char} <= 0x0008 or
462                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
463                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
464                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
465                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
466                 {
467                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
468                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
469                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
470                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
471                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
472                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
473                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
474                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
475                  0x10FFFE => 1, 0x10FFFF => 1,
476                 }->{$self->{next_char}}) {
477          !!!cp ('j5');
478          !!!parse-error (type => 'control char', level => $self->{must_level});
479    ## TODO: error type documentation
480      }      }
481    };    };
482    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 484  sub parse_string ($$$;$) {
484    
485    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
486      my (%opt) = @_;      my (%opt) = @_;
487      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
488        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
489        warn "Parse error ($opt{type}) at line $line column $column\n";
490    };    };
491    $self->{parse_error} = sub {    $self->{parse_error} = sub {
492      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
493    };    };
494    
495    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 497  sub parse_string ($$$;$) {
497    $self->_construct_tree;    $self->_construct_tree;
498    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
499    
500      delete $self->{parse_error}; # remove loop
501    
502    return $self->{document};    return $self->{document};
503  } # parse_string  } # parse_string
504    
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 566  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
566  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
567  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
568  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
569    sub SELF_CLOSING_START_TAG_STATE () { 34 }
570    sub CDATA_BLOCK_STATE () { 35 }
571    
572  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
573  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 584  sub TABLE_IMS ()      { 0b1000000 }
584  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
585  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
586  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
587    sub SELECT_IMS ()     { 0b10000000000 }
588    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
589        ## NOTE: "in foreign content" insertion mode is special; it is combined
590        ## with the secondary insertion mode.  In this parser, they are stored
591        ## together in the bit-or'ed form.
592    
593  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
594    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 611  sub IN_TABLE_IM () { TABLE_IMS }
611  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
612  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
613  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
614  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
615    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
616  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
617    
618  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 338  sub _initialize_tokenizer ($) { Line 625  sub _initialize_tokenizer ($) {
625    undef $self->{current_attribute};    undef $self->{current_attribute};
626    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
627    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
628      delete $self->{self_closing};
629    $self->{char} = [];    $self->{char} = [];
630    # $self->{next_char}    # $self->{next_char}
631    !!!next-input-character;    !!!next-input-character;
# Line 358  sub _initialize_tokenizer ($) { Line 646  sub _initialize_tokenizer ($) {
646  ##        ->{value}  ##        ->{value}
647  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
648  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
649    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
650    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
651    ##     while the token is pushed back to the stack.
652    
653    ## ISSUE: "When a DOCTYPE token is created, its
654    ## <i>self-closing flag</i> must be unset (its other state is that it
655    ## be set), and its attributes list must be empty.": Wrong subject?
656    
657  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
658    
# Line 384  sub _initialize_tokenizer ($) { Line 679  sub _initialize_tokenizer ($) {
679    
680  sub _get_next_token ($) {  sub _get_next_token ($) {
681    my $self = shift;    my $self = shift;
682    
683      if ($self->{self_closing}) {
684        !!!parse-error (type => 'nestc', token => $self->{current_token});
685        ## NOTE: The |self_closing| flag is only set by start tag token.
686        ## In addition, when a start tag token is emitted, it is always set to
687        ## |current_token|.
688        delete $self->{self_closing};
689      }
690    
691    if (@{$self->{token}}) {    if (@{$self->{token}}) {
692        $self->{self_closing} = $self->{token}->[0]->{self_closing};
693      return shift @{$self->{token}};      return shift @{$self->{token}};
694    }    }
695    
# Line 447  sub _get_next_token ($) { Line 752  sub _get_next_token ($) {
752          #          #
753        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
754          !!!cp (11);          !!!cp (11);
755          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
756                      line => $self->{line}, column => $self->{column}});
757          last A; ## TODO: ok?          last A; ## TODO: ok?
758        } else {        } else {
759          !!!cp (12);          !!!cp (12);
760        }        }
761        # Anything else        # Anything else
762        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
763                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
764                       line => $self->{line}, column => $self->{column},
765                      };
766        ## Stay in the data state        ## Stay in the data state
767        !!!next-input-character;        !!!next-input-character;
768    
# Line 463  sub _get_next_token ($) { Line 771  sub _get_next_token ($) {
771        redo A;        redo A;
772      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
773        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
774    
775          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
776                
777        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
778    
# Line 471  sub _get_next_token ($) { Line 781  sub _get_next_token ($) {
781    
782        unless (defined $token) {        unless (defined $token) {
783          !!!cp (13);          !!!cp (13);
784          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
785                      line => $l, column => $c,
786                     });
787        } else {        } else {
788          !!!cp (14);          !!!cp (14);
789          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 802  sub _get_next_token ($) {
802            ## reconsume            ## reconsume
803            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
804    
805            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
806                        line => $self->{line_prev},
807                        column => $self->{column_prev},
808                       });
809    
810            redo A;            redo A;
811          }          }
# Line 510  sub _get_next_token ($) { Line 825  sub _get_next_token ($) {
825            !!!cp (19);            !!!cp (19);
826            $self->{current_token}            $self->{current_token}
827              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
828                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
829                   line => $self->{line_prev},
830                   column => $self->{column_prev}};
831            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
832            !!!next-input-character;            !!!next-input-character;
833            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 835  sub _get_next_token ($) {
835                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
836            !!!cp (20);            !!!cp (20);
837            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
838                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
839                                        line => $self->{line_prev},
840                                        column => $self->{column_prev}};
841            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
842            !!!next-input-character;            !!!next-input-character;
843            redo A;            redo A;
844          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
845            !!!cp (21);            !!!cp (21);
846            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
847                              line => $self->{line_prev},
848                              column => $self->{column_prev});
849            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
850            !!!next-input-character;            !!!next-input-character;
851    
852            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
853                        line => $self->{line_prev},
854                        column => $self->{column_prev},
855                       });
856    
857            redo A;            redo A;
858          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
859            !!!cp (22);            !!!cp (22);
860            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
861                              line => $self->{line_prev},
862                              column => $self->{column_prev});
863            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
864              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
865                                        line => $self->{line_prev},
866                                        column => $self->{column_prev},
867                                       };
868            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
869            redo A;            redo A;
870          } else {          } else {
# Line 543  sub _get_next_token ($) { Line 873  sub _get_next_token ($) {
873            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
874            ## reconsume            ## reconsume
875    
876            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
877                        line => $self->{line_prev},
878                        column => $self->{column_prev},
879                       });
880    
881            redo A;            redo A;
882          }          }
# Line 551  sub _get_next_token ($) { Line 884  sub _get_next_token ($) {
884          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
885        }        }
886      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
887          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
888        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
889          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
890    
891            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
892            my @next_char;            my @next_char;
893            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++) {
# Line 569  sub _get_next_token ($) { Line 904  sub _get_next_token ($) {
904                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
905                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
906    
907                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
908                            line => $l, column => $c,
909                           });
910        
911                redo A;                redo A;
912              }              }
# Line 588  sub _get_next_token ($) { Line 925  sub _get_next_token ($) {
925              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
926              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
927              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
928              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
929                          line => $l, column => $c,
930                         });
931              redo A;              redo A;
932            } else {            } else {
933              !!!cp (27);              !!!cp (27);
# Line 601  sub _get_next_token ($) { Line 940  sub _get_next_token ($) {
940            !!!cp (28);            !!!cp (28);
941            # next-input-character is already done            # next-input-character is already done
942            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
943            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
944                        line => $l, column => $c,
945                       });
946            redo A;            redo A;
947          }          }
948        }        }
# Line 609  sub _get_next_token ($) { Line 950  sub _get_next_token ($) {
950        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
951            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
952          !!!cp (29);          !!!cp (29);
953          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
954                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
955                   tag_name => chr ($self->{next_char} + 0x0020),
956                   line => $l, column => $c};
957          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
958          !!!next-input-character;          !!!next-input-character;
959          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 961  sub _get_next_token ($) {
961                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
962          !!!cp (30);          !!!cp (30);
963          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
964                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
965                                      line => $l, column => $c};
966          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
967          !!!next-input-character;          !!!next-input-character;
968          redo A;          redo A;
969        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
970          !!!cp (31);          !!!cp (31);
971          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
972                            line => $self->{line_prev}, ## "<" in "</>"
973                            column => $self->{column_prev} - 1);
974          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
975          !!!next-input-character;          !!!next-input-character;
976          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 980  sub _get_next_token ($) {
980          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
981          # reconsume          # reconsume
982    
983          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
984                      line => $l, column => $c,
985                     });
986    
987          redo A;          redo A;
988        } else {        } else {
989          !!!cp (33);          !!!cp (33);
990          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
991          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
992            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
993                                      line => $self->{line_prev}, # "<" of "</"
994                                      column => $self->{column_prev} - 1,
995                                     };
996          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
997          redo A;          redo A;
998        }        }
# Line 657  sub _get_next_token ($) { Line 1009  sub _get_next_token ($) {
1009        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1010          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1011            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1012            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1013          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1014            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 1040  sub _get_next_token ($) {
1040          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1041          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1042            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1043            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1044          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1045            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 712  sub _get_next_token ($) { Line 1060  sub _get_next_token ($) {
1060    
1061          redo A;          redo A;
1062        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1063            !!!cp (42);
1064            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1065          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (42);  
           #  
         } else {  
           !!!cp (43);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1066          redo A;          redo A;
1067        } else {        } else {
1068          !!!cp (44);          !!!cp (44);
# Line 747  sub _get_next_token ($) { Line 1085  sub _get_next_token ($) {
1085        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1086          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1087            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1088            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1089          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1090            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 1106  sub _get_next_token ($) {
1106        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1107                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1108          !!!cp (49);          !!!cp (49);
1109          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1110                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1111                   value => '',
1112                   line => $self->{line}, column => $self->{column}};
1113          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1114          !!!next-input-character;          !!!next-input-character;
1115          redo A;          redo A;
1116        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1117            !!!cp (50);
1118            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1119          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (50);  
           #  
         } else {  
           !!!cp (51);  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
1120          redo A;          redo A;
1121        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1122          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1123          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1124            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1125            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1126          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1127            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1151  sub _get_next_token ($) {
1151          } else {          } else {
1152            !!!cp (56);            !!!cp (56);
1153          }          }
1154          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1155                                value => ''};              = {name => chr ($self->{next_char}),
1156                   value => '',
1157                   line => $self->{line}, column => $self->{column}};
1158          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1159          !!!next-input-character;          !!!next-input-character;
1160          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1164  sub _get_next_token ($) {
1164          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1165              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1166            !!!cp (57);            !!!cp (57);
1167            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1168            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1169          } else {          } else {
1170            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1193  sub _get_next_token ($) {
1193          $before_leave->();          $before_leave->();
1194          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1195            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1196            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1197          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1198            !!!cp (62);            !!!cp (62);
# Line 891  sub _get_next_token ($) { Line 1217  sub _get_next_token ($) {
1217          !!!next-input-character;          !!!next-input-character;
1218          redo A;          redo A;
1219        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1220            !!!cp (64);
1221          $before_leave->();          $before_leave->();
1222            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1223          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (64);  
           #  
         } else {  
           !!!cp (65);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1224          redo A;          redo A;
1225        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1226          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1227          $before_leave->();          $before_leave->();
1228          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1229            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1230            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1231          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1232            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1277  sub _get_next_token ($) {
1277        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1278          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1279            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1280            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1281          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1282            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1299  sub _get_next_token ($) {
1299        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1300                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1301          !!!cp (76);          !!!cp (76);
1302          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1303                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1304                   value => '',
1305                   line => $self->{line}, column => $self->{column}};
1306          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1307          !!!next-input-character;          !!!next-input-character;
1308          redo A;          redo A;
1309        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1310            !!!cp (77);
1311            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1312          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (77);  
           #  
         } else {  
           !!!cp (78);  
           !!!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  
1313          redo A;          redo A;
1314        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1315          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1316          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1317            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1318            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1319          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1320            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1035  sub _get_next_token ($) { Line 1336  sub _get_next_token ($) {
1336          redo A;          redo A;
1337        } else {        } else {
1338          !!!cp (82);          !!!cp (82);
1339          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1340                                value => ''};              = {name => chr ($self->{next_char}),
1341                   value => '',
1342                   line => $self->{line}, column => $self->{column}};
1343          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1344          !!!next-input-character;          !!!next-input-character;
1345          redo A;                  redo A;        
# Line 1069  sub _get_next_token ($) { Line 1372  sub _get_next_token ($) {
1372        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1373          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1374            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1375            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1376          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1377            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1395  sub _get_next_token ($) {
1395          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1396          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1397            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1398            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1399          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1400            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1143  sub _get_next_token ($) { Line 1442  sub _get_next_token ($) {
1442          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1443          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1444            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1445            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1446          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1447            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1187  sub _get_next_token ($) { Line 1484  sub _get_next_token ($) {
1484          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1485          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1486            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1487            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1488          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1489            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1234  sub _get_next_token ($) { Line 1529  sub _get_next_token ($) {
1529        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1530          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1531            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1532            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1533          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1534            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1552  sub _get_next_token ($) {
1552          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1553          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1554            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1555            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1556          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1557            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1331  sub _get_next_token ($) { Line 1622  sub _get_next_token ($) {
1622        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1623          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1624            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1625            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1626          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1627            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1353  sub _get_next_token ($) { Line 1642  sub _get_next_token ($) {
1642    
1643          redo A;          redo A;
1644        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1645            !!!cp (122);
1646            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1647          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (122);  
           #  
         } else {  
           !!!cp (123);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1648          redo A;          redo A;
1649        } else {        } else {
1650          !!!cp (124);          !!!cp ('124.1');
1651          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1652          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1653          ## reconsume          ## reconsume
1654          redo A;          redo A;
1655        }        }
1656        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1657          if ($self->{next_char} == 0x003E) { # >
1658            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1659              !!!cp ('124.2');
1660              !!!parse-error (type => 'nestc', token => $self->{current_token});
1661              ## TODO: Different type than slash in start tag
1662              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1663              if ($self->{current_token}->{attributes}) {
1664                !!!cp ('124.4');
1665                !!!parse-error (type => 'end tag attribute');
1666              } else {
1667                !!!cp ('124.5');
1668              }
1669              ## TODO: Test |<title></title/>|
1670            } else {
1671              !!!cp ('124.3');
1672              $self->{self_closing} = 1;
1673            }
1674    
1675            $self->{state} = DATA_STATE;
1676            !!!next-input-character;
1677    
1678            !!!emit ($self->{current_token}); # start tag or end tag
1679    
1680            redo A;
1681          } else {
1682            !!!cp ('124.4');
1683            !!!parse-error (type => 'nestc');
1684            ## TODO: This error type is wrong.
1685            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1686            ## Reconsume.
1687            redo A;
1688          }
1689      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1690        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1691                
1692        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1693          #my $token = {type => COMMENT_TOKEN, data => ''};
1694    
1695        BC: {        BC: {
1696          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1385  sub _get_next_token ($) { Line 1698  sub _get_next_token ($) {
1698            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1699            !!!next-input-character;            !!!next-input-character;
1700    
1701            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1702    
1703            redo A;            redo A;
1704          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1393  sub _get_next_token ($) { Line 1706  sub _get_next_token ($) {
1706            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1707            ## reconsume            ## reconsume
1708    
1709            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1710    
1711            redo A;            redo A;
1712          } else {          } else {
1713            !!!cp (126);            !!!cp (126);
1714            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1715            !!!next-input-character;            !!!next-input-character;
1716            redo BC;            redo BC;
1717          }          }
# Line 1408  sub _get_next_token ($) { Line 1721  sub _get_next_token ($) {
1721      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1722        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1723    
1724          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1725    
1726        my @next_char;        my @next_char;
1727        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1728                
# Line 1416  sub _get_next_token ($) { Line 1731  sub _get_next_token ($) {
1731          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1732          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1733            !!!cp (127);            !!!cp (127);
1734            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1735                                        line => $l, column => $c,
1736                                       };
1737            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1738            !!!next-input-character;            !!!next-input-character;
1739            redo A;            redo A;
# Line 1452  sub _get_next_token ($) { Line 1769  sub _get_next_token ($) {
1769                      !!!cp (129);                      !!!cp (129);
1770                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
1771                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1772                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1773                                                  quirks => 1,
1774                                                  line => $l, column => $c,
1775                                                 };
1776                      !!!next-input-character;                      !!!next-input-character;
1777                      redo A;                      redo A;
1778                    } else {                    } else {
# Line 1472  sub _get_next_token ($) { Line 1793  sub _get_next_token ($) {
1793          } else {          } else {
1794            !!!cp (135);            !!!cp (135);
1795          }          }
1796          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1797                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1798                   $self->{next_char} == 0x005B) { # [
1799            !!!next-input-character;
1800            push @next_char, $self->{next_char};
1801            if ($self->{next_char} == 0x0043) { # C
1802              !!!next-input-character;
1803              push @next_char, $self->{next_char};
1804              if ($self->{next_char} == 0x0044) { # D
1805                !!!next-input-character;
1806                push @next_char, $self->{next_char};
1807                if ($self->{next_char} == 0x0041) { # A
1808                  !!!next-input-character;
1809                  push @next_char, $self->{next_char};
1810                  if ($self->{next_char} == 0x0054) { # T
1811                    !!!next-input-character;
1812                    push @next_char, $self->{next_char};
1813                    if ($self->{next_char} == 0x0041) { # A
1814                      !!!next-input-character;
1815                      push @next_char, $self->{next_char};
1816                      if ($self->{next_char} == 0x005B) { # [
1817                        !!!cp (135.1);
1818                        $self->{state} = CDATA_BLOCK_STATE;
1819                        !!!next-input-character;
1820                        redo A;
1821                      } else {
1822                        !!!cp (135.2);
1823                      }
1824                    } else {
1825                      !!!cp (135.3);
1826                    }
1827                  } else {
1828                    !!!cp (135.4);                
1829                  }
1830                } else {
1831                  !!!cp (135.5);
1832                }
1833              } else {
1834                !!!cp (135.6);
1835              }
1836            } else {
1837              !!!cp (135.7);
1838            }
1839        } else {        } else {
1840          !!!cp (136);          !!!cp (136);
1841        }        }
# Line 1480  sub _get_next_token ($) { Line 1844  sub _get_next_token ($) {
1844        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
1845        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1846        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
1847          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1848                                    line => $l, column => $c,
1849                                   };
1850        redo A;        redo A;
1851                
1852        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 1603  sub _get_next_token ($) { Line 1970  sub _get_next_token ($) {
1970          redo A;          redo A;
1971        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1972          !!!cp (152);          !!!cp (152);
1973          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
1974                            line => $self->{line_prev},
1975                            column => $self->{column_prev});
1976          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1977          ## Stay in the state          ## Stay in the state
1978          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 1988  sub _get_next_token ($) {
1988          redo A;          redo A;
1989        } else {        } else {
1990          !!!cp (154);          !!!cp (154);
1991          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
1992                            line => $self->{line_prev},
1993                            column => $self->{column_prev});
1994          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1995          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1996          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 2029  sub _get_next_token ($) {
2029          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2030          !!!next-input-character;          !!!next-input-character;
2031    
2032          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2033    
2034          redo A;          redo A;
2035        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 2038  sub _get_next_token ($) {
2038          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2039          ## reconsume          ## reconsume
2040    
2041          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2042    
2043          redo A;          redo A;
2044        } else {        } else {
2045          !!!cp (160);          !!!cp (160);
2046          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2047              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2048  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2049          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2050          !!!next-input-character;          !!!next-input-character;
# Line 2192  sub _get_next_token ($) { Line 2560  sub _get_next_token ($) {
2560          !!!next-input-character;          !!!next-input-character;
2561          redo A;          redo A;
2562        }        }
2563        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2564          my $s = '';
2565          
2566          my ($l, $c) = ($self->{line}, $self->{column});
2567    
2568          CS: while ($self->{next_char} != -1) {
2569            if ($self->{next_char} == 0x005D) { # ]
2570              !!!next-input-character;
2571              if ($self->{next_char} == 0x005D) { # ]
2572                !!!next-input-character;
2573                MDC: {
2574                  if ($self->{next_char} == 0x003E) { # >
2575                    !!!cp (221.1);
2576                    !!!next-input-character;
2577                    last CS;
2578                  } elsif ($self->{next_char} == 0x005D) { # ]
2579                    !!!cp (221.2);
2580                    $s .= ']';
2581                    !!!next-input-character;
2582                    redo MDC;
2583                  } else {
2584                    !!!cp (221.3);
2585                    $s .= ']]';
2586                    #
2587                  }
2588                } # MDC
2589              } else {
2590                !!!cp (221.4);
2591                $s .= ']';
2592                #
2593              }
2594            } else {
2595              !!!cp (221.5);
2596              #
2597            }
2598            $s .= chr $self->{next_char};
2599            !!!next-input-character;
2600          } # CS
2601    
2602          $self->{state} = DATA_STATE;
2603          ## next-input-character done or EOF, which is reconsumed.
2604    
2605          if (length $s) {
2606            !!!cp (221.6);
2607            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2608                      line => $l, column => $c});
2609          } else {
2610            !!!cp (221.7);
2611          }
2612    
2613          redo A;
2614    
2615          ## ISSUE: "text tokens" in spec.
2616          ## TODO: Streaming support
2617      } else {      } else {
2618        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2619      }      }
# Line 2203  sub _get_next_token ($) { Line 2625  sub _get_next_token ($) {
2625  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2626    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2627    
2628      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2629    
2630    if ({    if ({
2631         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2632         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
# Line 2243  sub _tokenize_attempt_to_consume_an_enti Line 2667  sub _tokenize_attempt_to_consume_an_enti
2667            redo X;            redo X;
2668          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2669            !!!cp (1005);            !!!cp (1005);
2670            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2671            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2672            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2673            return undef;            return undef;
# Line 2252  sub _tokenize_attempt_to_consume_an_enti Line 2676  sub _tokenize_attempt_to_consume_an_enti
2676            !!!next-input-character;            !!!next-input-character;
2677          } else {          } else {
2678            !!!cp (1007);            !!!cp (1007);
2679            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2680          }          }
2681    
2682          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2683            !!!cp (1008);            !!!cp (1008);
2684            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2685            $code = 0xFFFD;            $code = 0xFFFD;
2686          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2687            !!!cp (1009);            !!!cp (1009);
2688            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2689            $code = 0xFFFD;            $code = 0xFFFD;
2690          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2691            !!!cp (1010);            !!!cp (1010);
2692            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2693            $code = 0x000A;            $code = 0x000A;
2694          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2695            !!!cp (1011);            !!!cp (1011);
2696            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2697            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2698          }          }
2699    
2700          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2701                  has_reference => 1};                  has_reference => 1,
2702                    line => $l, column => $c,
2703                   };
2704        } # X        } # X
2705      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2706               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 2721  sub _tokenize_attempt_to_consume_an_enti
2721          !!!next-input-character;          !!!next-input-character;
2722        } else {        } else {
2723          !!!cp (1014);          !!!cp (1014);
2724          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2725        }        }
2726    
2727        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2728          !!!cp (1015);          !!!cp (1015);
2729          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2730          $code = 0xFFFD;          $code = 0xFFFD;
2731        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2732          !!!cp (1016);          !!!cp (1016);
2733          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2734          $code = 0xFFFD;          $code = 0xFFFD;
2735        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2736          !!!cp (1017);          !!!cp (1017);
2737          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2738          $code = 0x000A;          $code = 0x000A;
2739        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2740          !!!cp (1018);          !!!cp (1018);
2741          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2742          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2743        }        }
2744                
2745        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2746                  line => $l, column => $c,
2747                 };
2748      } else {      } else {
2749        !!!cp (1019);        !!!cp (1019);
2750        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2751        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2752        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2753        return undef;        return undef;
# Line 2336  sub _tokenize_attempt_to_consume_an_enti Line 2764  sub _tokenize_attempt_to_consume_an_enti
2764      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2765      our $EntityChar;      our $EntityChar;
2766    
2767      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2768             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2769             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2770               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 2797  sub _tokenize_attempt_to_consume_an_enti
2797            
2798      if ($match > 0) {      if ($match > 0) {
2799        !!!cp (1023);        !!!cp (1023);
2800        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2801                  line => $l, column => $c,
2802                 };
2803      } elsif ($match < 0) {      } elsif ($match < 0) {
2804        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2805        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2806          !!!cp (1024);          !!!cp (1024);
2807          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2808                    line => $l, column => $c,
2809                   };
2810        } else {        } else {
2811          !!!cp (1025);          !!!cp (1025);
2812          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2813                    line => $l, column => $c,
2814                   };
2815        }        }
2816      } else {      } else {
2817        !!!cp (1026);        !!!cp (1026);
2818        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2819        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2820        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
2821                  line => $l, column => $c,
2822                 };
2823      }      }
2824    } else {    } else {
2825      !!!cp (1027);      !!!cp (1027);
2826      ## no characters are consumed      ## no characters are consumed
2827      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2828      return undef;      return undef;
2829    }    }
2830  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2459  sub _tree_construction_initial ($) { Line 2895  sub _tree_construction_initial ($) {
2895            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2896            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2897          !!!cp ('t1');          !!!cp ('t1');
2898          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2899        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2900          !!!cp ('t2');          !!!cp ('t2');
2901          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2902          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2903        } else {        } else {
2904          !!!cp ('t3');          !!!cp ('t3');
2905        }        }
2906                
2907        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
2908          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2909          ## NOTE: Default value for both |public_id| and |system_id| attributes
2910          ## are empty strings, so that we don't set any value in missing cases.
2911        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
2912            if defined $token->{public_identifier};            if defined $token->{public_identifier};
2913        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2602  sub _tree_construction_initial ($) { Line 3040  sub _tree_construction_initial ($) {
3040                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3041               }->{$token->{type}}) {               }->{$token->{type}}) {
3042        !!!cp ('t14');        !!!cp ('t14');
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 "before html" insertion mode.        ## Go to the "before html" insertion mode.
3046        ## reprocess        ## reprocess
3047          !!!ack-later;
3048        return;        return;
3049      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3050        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2623  sub _tree_construction_initial ($) { Line 3062  sub _tree_construction_initial ($) {
3062          !!!cp ('t17');          !!!cp ('t17');
3063        }        }
3064    
3065        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3066        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3067        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3068        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 3091  sub _tree_construction_root_element ($)
3091    B: {    B: {
3092        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3093          !!!cp ('t19');          !!!cp ('t19');
3094          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3095          ## Ignore the token          ## Ignore the token
3096          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3097          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 3125  sub _tree_construction_root_element ($)
3125        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3126          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3127            my $root_element;            my $root_element;
3128            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3129            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3130            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3131                  [$root_element, $el_category->{html}];
3132    
3133            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3134              !!!cp ('t24');              !!!cp ('t24');
3135              $self->{application_cache_selection}              $self->{application_cache_selection}
3136                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3137              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3138                ## According to Hixie (#whatwg 2008-03-19), it should be
3139                ## resolved against the base URI of the document in HTML
3140                ## or xml:base of the element in XHTML.
3141            } else {            } else {
3142              !!!cp ('t25');              !!!cp ('t25');
3143              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3144            }            }
3145    
3146              !!!nack ('t25c');
3147    
3148            !!!next-token;            !!!next-token;
3149            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3150          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 3161  sub _tree_construction_root_element ($)
3161          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3162        }        }
3163    
3164      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3165        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3166      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3167      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3168    
3169      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3170    
3171      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3172        !!!ack-later;
3173      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3174    
3175      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2746  sub _reset_insertion_mode ($) { Line 3193  sub _reset_insertion_mode ($) {
3193        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3194          $last = 1;          $last = 1;
3195          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3196            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
               $self->{inner_html_node}->[1] eq 'th') {  
3197              !!!cp ('t27');              !!!cp ('t27');
3198              #              #
3199            } else {            } else {
# Line 2757  sub _reset_insertion_mode ($) { Line 3203  sub _reset_insertion_mode ($) {
3203          }          }
3204        }        }
3205            
3206        ## Step 4..13      ## Step 4..14
3207        my $new_mode = {      my $new_mode;
3208        if ($node->[1] & FOREIGN_EL) {
3209          ## NOTE: Strictly spaking, the line below only applies to MathML and
3210          ## SVG elements.  Currently the HTML syntax supports only MathML and
3211          ## SVG elements as foreigners.
3212          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3213          ## ISSUE: What is set as the secondary insertion mode?
3214        } else {
3215          $new_mode = {
3216                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3217                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3218                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
# Line 2774  sub _reset_insertion_mode ($) { Line 3228  sub _reset_insertion_mode ($) {
3228                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3229                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3230                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3231                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3232        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3233        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3234                
3235        ## Step 14        ## Step 15
3236        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3237          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3238            !!!cp ('t29');            !!!cp ('t29');
3239            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2792  sub _reset_insertion_mode ($) { Line 3247  sub _reset_insertion_mode ($) {
3247          !!!cp ('t31');          !!!cp ('t31');
3248        }        }
3249                
3250        ## Step 15        ## Step 16
3251        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3252                
3253        ## Step 16        ## Step 17
3254        $i--;        $i--;
3255        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3256                
3257        ## Step 17        ## Step 18
3258        redo S3;        redo S3;
3259      } # S3      } # S3
3260    
# Line 2911  sub _tree_construction_main ($) { Line 3366  sub _tree_construction_main ($) {
3366      ## Step 1      ## Step 1
3367      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3368      my $el;      my $el;
3369      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3370    
3371      ## Step 2      ## Step 2
3372      $insert->($el);      $insert->($el);
# Line 2922  sub _tree_construction_main ($) { Line 3377  sub _tree_construction_main ($) {
3377    
3378      ## Step 4      ## Step 4
3379      my $text = '';      my $text = '';
3380        !!!nack ('t40.1');
3381      !!!next-token;      !!!next-token;
3382      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3383        !!!cp ('t40');        !!!cp ('t40');
# Line 2948  sub _tree_construction_main ($) { Line 3404  sub _tree_construction_main ($) {
3404        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3405        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3406          !!!cp ('t43');          !!!cp ('t43');
3407          !!!parse-error (type => 'in CDATA:#'.$token->{type});          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3408        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3409          !!!cp ('t44');          !!!cp ('t44');
3410          !!!parse-error (type => 'in RCDATA:#'.$token->{type});          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3411        } else {        } else {
3412          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3413        }        }
# Line 2961  sub _tree_construction_main ($) { Line 3417  sub _tree_construction_main ($) {
3417    
3418    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3419      my $script_el;      my $script_el;
3420      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3421      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3422    
3423      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3424      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3425            
3426      my $text = '';      my $text = '';
3427        !!!nack ('t45.1');
3428      !!!next-token;      !!!next-token;
3429      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3430        !!!cp ('t45');        !!!cp ('t45');
# Line 2987  sub _tree_construction_main ($) { Line 3444  sub _tree_construction_main ($) {
3444        ## Ignore the token        ## Ignore the token
3445      } else {      } else {
3446        !!!cp ('t48');        !!!cp ('t48');
3447        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3448        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3449        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3450      }      }
# Line 3010  sub _tree_construction_main ($) { Line 3467  sub _tree_construction_main ($) {
3467      !!!next-token;      !!!next-token;
3468    }; # $script_start_tag    }; # $script_start_tag
3469    
3470      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3471      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3472      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3473    
3474    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3475      my $tag_name = shift;      my $end_tag_token = shift;
3476        my $tag_name = $end_tag_token->{tag_name};
3477    
3478        ## NOTE: The adoption agency algorithm (AAA).
3479    
3480      FET: {      FET: {
3481        ## Step 1        ## Step 1
3482        my $formatting_element;        my $formatting_element;
3483        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3484        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3485          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3486              !!!cp ('t52');
3487              last AFE;
3488            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3489                         eq $tag_name) {
3490            !!!cp ('t51');            !!!cp ('t51');
3491            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3492            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3493            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3494          }          }
3495        } # AFE        } # AFE
3496        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3497          !!!cp ('t53');          !!!cp ('t53');
3498          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3499          ## Ignore the token          ## Ignore the token
3500          !!!next-token;          !!!next-token;
3501          return;          return;
# Line 3047  sub _tree_construction_main ($) { Line 3512  sub _tree_construction_main ($) {
3512              last INSCOPE;              last INSCOPE;
3513            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3514              !!!cp ('t55');              !!!cp ('t55');
3515              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3516                                token => $end_tag_token);
3517              ## Ignore the token              ## Ignore the token
3518              !!!next-token;              !!!next-token;
3519              return;              return;
3520            }            }
3521          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3522            !!!cp ('t56');            !!!cp ('t56');
3523            $in_scope = 0;            $in_scope = 0;
3524          }          }
3525        } # INSCOPE        } # INSCOPE
3526        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3527          !!!cp ('t57');          !!!cp ('t57');
3528          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3529                            token => $end_tag_token);
3530          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3531          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3532          return;          return;
3533        }        }
3534        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3535          !!!cp ('t58');          !!!cp ('t58');
3536          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3537                            value => $self->{open_elements}->[-1]->[0]
3538                                ->manakai_local_name,
3539                            token => $end_tag_token);
3540        }        }
3541                
3542        ## Step 2        ## Step 2
# Line 3077  sub _tree_construction_main ($) { Line 3544  sub _tree_construction_main ($) {
3544        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3545        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3546          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3547          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3548              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3549              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3550               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3551            !!!cp ('t59');            !!!cp ('t59');
3552            $furthest_block = $node;            $furthest_block = $node;
3553            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3166  sub _tree_construction_main ($) { Line 3633  sub _tree_construction_main ($) {
3633        } # S7          } # S7  
3634                
3635        ## Step 8        ## Step 8
3636        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3637            my $foster_parent_element;
3638            my $next_sibling;
3639            OE: for (reverse 0..$#{$self->{open_elements}}) {
3640              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3641                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3642                                 if (defined $parent and $parent->node_type == 1) {
3643                                   !!!cp ('t65.1');
3644                                   $foster_parent_element = $parent;
3645                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3646                                 } else {
3647                                   !!!cp ('t65.2');
3648                                   $foster_parent_element
3649                                     = $self->{open_elements}->[$_ - 1]->[0];
3650                                 }
3651                                 last OE;
3652                               }
3653                             } # OE
3654                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3655                               unless defined $foster_parent_element;
3656            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3657            $open_tables->[-1]->[1] = 1; # tainted
3658          } else {
3659            !!!cp ('t65.3');
3660            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3661          }
3662                
3663        ## Step 9        ## Step 9
3664        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3212  sub _tree_construction_main ($) { Line 3704  sub _tree_construction_main ($) {
3704      } # FET      } # FET
3705    }; # $formatting_end_tag    }; # $formatting_end_tag
3706    
   ## NOTE: $open_tables->[-1]->[0] is the "current table".  
   ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.  
   my $open_tables = [[$self->{open_elements}->[0]->[0]]];  
   
3707    $insert = my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3708      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3709    }; # $insert_to_current    }; # $insert_to_current
3710    
3711    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3712      my $child = shift;      my $child = shift;
3713      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]}) {  
3714        # MUST        # MUST
3715        my $foster_parent_element;        my $foster_parent_element;
3716        my $next_sibling;        my $next_sibling;
3717                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3718                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3719                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3720                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3721                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3254  sub _tree_construction_main ($) { Line 3740  sub _tree_construction_main ($) {
3740      }      }
3741    }; # $insert_to_foster    }; # $insert_to_foster
3742    
3743    B: {    B: while (1) {
3744      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3745        !!!cp ('t73');        !!!cp ('t73');
3746        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3747        ## Ignore the token        ## Ignore the token
3748        ## Stay in the phase        ## Stay in the phase
3749        !!!next-token;        !!!next-token;
3750        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         while ({  
                 dd => 1, dt => 1, li => 1, p => 1,  
                }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           pop @{$self->{open_elements}};  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!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') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3751      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3752               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3753        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3754          !!!cp ('t79');          !!!cp ('t79');
3755          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3756          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3757        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3758          !!!cp ('t80');          !!!cp ('t80');
3759          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3760          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3761        } else {        } else {
3762          !!!cp ('t81');          !!!cp ('t81');
3763        }        }
3764    
3765        !!!cp ('t82');        !!!cp ('t82');
3766        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
3767        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3768        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3769          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3319  sub _tree_construction_main ($) { Line 3773  sub _tree_construction_main ($) {
3773               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3774          }          }
3775        }        }
3776          !!!nack ('t84.1');
3777        !!!next-token;        !!!next-token;
3778        redo B;        next B;
3779      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3780        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3781        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3334  sub _tree_construction_main ($) { Line 3789  sub _tree_construction_main ($) {
3789          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3790        }        }
3791        !!!next-token;        !!!next-token;
3792        redo B;        next B;
3793      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3794          if ($token->{type} == CHARACTER_TOKEN) {
3795            !!!cp ('t87.1');
3796            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3797            !!!next-token;
3798            next B;
3799          } elsif ($token->{type} == START_TAG_TOKEN) {
3800            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3801                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3802                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3803                ($token->{tag_name} eq 'svg' and
3804                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3805              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3806              !!!cp ('t87.2');
3807              #
3808            } elsif ({
3809                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3810                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3811                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3812                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3813                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3814                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3815                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3816                      var => 1,
3817                     }->{$token->{tag_name}}) {
3818              !!!cp ('t87.2');
3819              !!!parse-error (type => 'not closed',
3820                              value => $self->{open_elements}->[-1]->[0]
3821                                  ->manakai_local_name,
3822                              token => $token);
3823    
3824              pop @{$self->{open_elements}}
3825                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3826    
3827              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3828              ## Reprocess.
3829              next B;
3830            } else {
3831              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3832              my $tag_name = $token->{tag_name};
3833              if ($nsuri eq $SVG_NS) {
3834                $tag_name = {
3835                   altglyph => 'altGlyph',
3836                   altglyphdef => 'altGlyphDef',
3837                   altglyphitem => 'altGlyphItem',
3838                   animatecolor => 'animateColor',
3839                   animatemotion => 'animateMotion',
3840                   animatetransform => 'animateTransform',
3841                   clippath => 'clipPath',
3842                   feblend => 'feBlend',
3843                   fecolormatrix => 'feColorMatrix',
3844                   fecomponenttransfer => 'feComponentTransfer',
3845                   fecomposite => 'feComposite',
3846                   feconvolvematrix => 'feConvolveMatrix',
3847                   fediffuselighting => 'feDiffuseLighting',
3848                   fedisplacementmap => 'feDisplacementMap',
3849                   fedistantlight => 'feDistantLight',
3850                   feflood => 'feFlood',
3851                   fefunca => 'feFuncA',
3852                   fefuncb => 'feFuncB',
3853                   fefuncg => 'feFuncG',
3854                   fefuncr => 'feFuncR',
3855                   fegaussianblur => 'feGaussianBlur',
3856                   feimage => 'feImage',
3857                   femerge => 'feMerge',
3858                   femergenode => 'feMergeNode',
3859                   femorphology => 'feMorphology',
3860                   feoffset => 'feOffset',
3861                   fepointlight => 'fePointLight',
3862                   fespecularlighting => 'feSpecularLighting',
3863                   fespotlight => 'feSpotLight',
3864                   fetile => 'feTile',
3865                   feturbulence => 'feTurbulence',
3866                   foreignobject => 'foreignObject',
3867                   glyphref => 'glyphRef',
3868                   lineargradient => 'linearGradient',
3869                   radialgradient => 'radialGradient',
3870                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
3871                   textpath => 'textPath',  
3872                }->{$tag_name} || $tag_name;
3873              }
3874    
3875              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
3876    
3877              ## "adjust foreign attributes" - done in insert-element-f
3878    
3879              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
3880    
3881              if ($self->{self_closing}) {
3882                pop @{$self->{open_elements}};
3883                !!!ack ('t87.3');
3884              } else {
3885                !!!cp ('t87.4');
3886              }
3887    
3888              !!!next-token;
3889              next B;
3890            }
3891          } elsif ($token->{type} == END_TAG_TOKEN) {
3892            ## NOTE: "using the rules for secondary insertion mode" then "continue"
3893            !!!cp ('t87.5');
3894            #
3895          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3896            ## NOTE: "using the rules for secondary insertion mode" then "continue"
3897            !!!cp ('t87.6');
3898            #
3899            ## TODO: ...
3900          } else {
3901            die "$0: $token->{type}: Unknown token type";        
3902          }
3903        }
3904    
3905        if ($self->{insertion_mode} & HEAD_IMS) {
3906        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3907          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3908            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3909                !!!cp ('t88.2');
3910                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3911              } else {
3912                !!!cp ('t88.1');
3913                ## Ignore the token.
3914                !!!next-token;
3915                next B;
3916              }
3917            unless (length $token->{data}) {            unless (length $token->{data}) {
3918              !!!cp ('t88');              !!!cp ('t88');
3919              !!!next-token;              !!!next-token;
3920              redo B;              next B;
3921            }            }
3922          }          }
3923    
3924          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3925            !!!cp ('t89');            !!!cp ('t89');
3926            ## As if <head>            ## As if <head>
3927            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
3928            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3929            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
3930                  [$self->{head_element}, $el_category->{head}];
3931    
3932            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3933            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3361  sub _tree_construction_main ($) { Line 3937  sub _tree_construction_main ($) {
3937            !!!cp ('t90');            !!!cp ('t90');
3938            ## As if </noscript>            ## As if </noscript>
3939            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3940            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
3941                        
3942            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3943            ## As if </head>            ## As if </head>
# Line 3377  sub _tree_construction_main ($) { Line 3953  sub _tree_construction_main ($) {
3953            !!!cp ('t92');            !!!cp ('t92');
3954          }          }
3955    
3956              ## "after head" insertion mode          ## "after head" insertion mode
3957              ## As if <body>          ## As if <body>
3958              !!!insert-element ('body');          !!!insert-element ('body',, $token);
3959              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
3960              ## reprocess          ## reprocess
3961              redo B;          next B;
3962            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3963              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
3964                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3965                  !!!cp ('t93');              !!!cp ('t93');
3966                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3967                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
3968                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
3969                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
3970                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
3971                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
3972                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
3973                  !!!cp ('t94');              !!!next-token;
3974                  #              next B;
3975                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3976                  !!!cp ('t95');              !!!cp ('t94');
3977                  !!!parse-error (type => 'in head:head'); # or in head noscript              #
3978                  ## Ignore the token            } else {
3979                  !!!next-token;              !!!cp ('t95');
3980                  redo B;              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3981                }              ## Ignore the token
3982              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!nack ('t95.1');
3983                !!!cp ('t96');              !!!next-token;
3984                ## As if <head>              next B;
3985                !!!create-element ($self->{head_element}, 'head');            }
3986                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3987                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!cp ('t96');
3988              ## As if <head>
3989              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
3990              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3991              push @{$self->{open_elements}},
3992                  [$self->{head_element}, $el_category->{head}];
3993    
3994                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
3995                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3996              } else {          } else {
3997                !!!cp ('t97');            !!!cp ('t97');
3998              }          }
3999    
4000              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4001                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4002                  !!!cp ('t98');                  !!!cp ('t98');
4003                  ## As if </noscript>                  ## As if </noscript>
4004                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4005                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4006                                
4007                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4008                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3432  sub _tree_construction_main ($) { Line 4013  sub _tree_construction_main ($) {
4013                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4014                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4015                  !!!cp ('t100');                  !!!cp ('t100');
4016                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4017                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4018                        [$self->{head_element}, $el_category->{head}];
4019                } else {                } else {
4020                  !!!cp ('t101');                  !!!cp ('t101');
4021                }                }
4022                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4023                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4024                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4025                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4026                  !!!nack ('t101.1');
4027                !!!next-token;                !!!next-token;
4028                redo B;                next B;
4029              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4030                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4031                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4032                  !!!cp ('t102');                  !!!cp ('t102');
4033                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4034                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4035                        [$self->{head_element}, $el_category->{head}];
4036                } else {                } else {
4037                  !!!cp ('t103');                  !!!cp ('t103');
4038                }                }
4039                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4040                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4041                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4042                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4043                  !!!ack ('t103.1');
4044                !!!next-token;                !!!next-token;
4045                redo B;                next B;
4046              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4047                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4048                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4049                  !!!cp ('t104');                  !!!cp ('t104');
4050                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4051                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4052                        [$self->{head_element}, $el_category->{head}];
4053                } else {                } else {
4054                  !!!cp ('t105');                  !!!cp ('t105');
4055                }                }
4056                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4057                my $meta_el = 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.
4058    
4059                unless ($self->{confident}) {                unless ($self->{confident}) {
4060                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
4061                    !!!cp ('t106');                    !!!cp ('t106');
4062                    $self->{change_encoding}                    $self->{change_encoding}
4063                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4064                             $token);
4065                                        
4066                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4067                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
# Line 3489  sub _tree_construction_main ($) { Line 4076  sub _tree_construction_main ($) {
4076                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4077                      !!!cp ('t107');                      !!!cp ('t107');
4078                      $self->{change_encoding}                      $self->{change_encoding}
4079                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4080                               $token);
4081                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4082                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4083                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3515  sub _tree_construction_main ($) { Line 4103  sub _tree_construction_main ($) {
4103                  }                  }
4104                }                }
4105    
4106                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4107                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4108                  !!!ack ('t110.1');
4109                !!!next-token;                !!!next-token;
4110                redo B;                next B;
4111              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4112                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4113                  !!!cp ('t111');                  !!!cp ('t111');
4114                  ## As if </noscript>                  ## As if </noscript>
4115                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4116                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4117                                
4118                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4119                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4120                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4121                  !!!cp ('t112');                  !!!cp ('t112');
4122                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4123                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4124                        [$self->{head_element}, $el_category->{head}];
4125                } else {                } else {
4126                  !!!cp ('t113');                  !!!cp ('t113');
4127                }                }
# Line 3540  sub _tree_construction_main ($) { Line 4130  sub _tree_construction_main ($) {
4130                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4131                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4132                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4133                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4134                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4135                redo B;                next B;
4136              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4137                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4138                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4139                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4140                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4141                  !!!cp ('t114');                  !!!cp ('t114');
4142                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4143                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4144                        [$self->{head_element}, $el_category->{head}];
4145                } else {                } else {
4146                  !!!cp ('t115');                  !!!cp ('t115');
4147                }                }
4148                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4149                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4150                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4151                redo B;                next B;
4152              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4153                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4154                  !!!cp ('t116');                  !!!cp ('t116');
4155                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4156                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4157                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4158                    !!!nack ('t116.1');
4159                  !!!next-token;                  !!!next-token;
4160                  redo B;                  next B;
4161                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4162                  !!!cp ('t117');                  !!!cp ('t117');
4163                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4164                  ## Ignore the token                  ## Ignore the token
4165                    !!!nack ('t117.1');
4166                  !!!next-token;                  !!!next-token;
4167                  redo B;                  next B;
4168                } else {                } else {
4169                  !!!cp ('t118');                  !!!cp ('t118');
4170                  #                  #
# Line 3581  sub _tree_construction_main ($) { Line 4174  sub _tree_construction_main ($) {
4174                  !!!cp ('t119');                  !!!cp ('t119');
4175                  ## As if </noscript>                  ## As if </noscript>
4176                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4177                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4178                                
4179                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4180                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4181                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4182                  !!!cp ('t120');                  !!!cp ('t120');
4183                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4184                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4185                        [$self->{head_element}, $el_category->{head}];
4186                } else {                } else {
4187                  !!!cp ('t121');                  !!!cp ('t121');
4188                }                }
4189    
4190                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4191                $script_start_tag->($insert_to_current);                $script_start_tag->();
4192                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4193                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4194                redo B;                next B;
4195              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4196                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4197                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4198                  !!!cp ('t122');                  !!!cp ('t122');
4199                  ## As if </noscript>                  ## As if </noscript>
4200                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4201                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4202                                    
4203                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4204                  ## As if </head>                  ## As if </head>
# Line 3621  sub _tree_construction_main ($) { Line 4215  sub _tree_construction_main ($) {
4215                }                }
4216    
4217                ## "after head" insertion mode                ## "after head" insertion mode
4218                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4219                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4220                  !!!cp ('t126');                  !!!cp ('t126');
4221                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3631  sub _tree_construction_main ($) { Line 4225  sub _tree_construction_main ($) {
4225                } else {                } else {
4226                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4227                }                }
4228                  !!!nack ('t127.1');
4229                !!!next-token;                !!!next-token;
4230                redo B;                next B;
4231              } else {              } else {
4232                !!!cp ('t128');                !!!cp ('t128');
4233                #                #
# Line 3642  sub _tree_construction_main ($) { Line 4237  sub _tree_construction_main ($) {
4237                !!!cp ('t129');                !!!cp ('t129');
4238                ## As if </noscript>                ## As if </noscript>
4239                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4240                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4241                                
4242                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4243                ## As if </head>                ## As if </head>
# Line 3661  sub _tree_construction_main ($) { Line 4256  sub _tree_construction_main ($) {
4256    
4257              ## "after head" insertion mode              ## "after head" insertion mode
4258              ## As if <body>              ## As if <body>
4259              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4260              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4261              ## reprocess              ## reprocess
4262              redo B;              !!!ack-later;
4263                next B;
4264            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4265              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4266                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4267                  !!!cp ('t132');                  !!!cp ('t132');
4268                  ## As if <head>                  ## As if <head>
4269                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4270                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4271                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4272                        [$self->{head_element}, $el_category->{head}];
4273    
4274                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4275                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4276                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4277                  !!!next-token;                  !!!next-token;
4278                  redo B;                  next B;
4279                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4280                  !!!cp ('t133');                  !!!cp ('t133');
4281                  ## As if </noscript>                  ## As if </noscript>
4282                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4283                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4284                                    
4285                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4286                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4287                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4288                  !!!next-token;                  !!!next-token;
4289                  redo B;                  next B;
4290                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4291                  !!!cp ('t134');                  !!!cp ('t134');
4292                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4293                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4294                  !!!next-token;                  !!!next-token;
4295                  redo B;                  next B;
4296                } else {                } else {
4297                  !!!cp ('t135');                  !!!cp ('t135');
4298                  #                  #
# Line 3706  sub _tree_construction_main ($) { Line 4303  sub _tree_construction_main ($) {
4303                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4304                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4305                  !!!next-token;                  !!!next-token;
4306                  redo B;                  next B;
4307                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4308                  !!!cp ('t137');                  !!!cp ('t137');
4309                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4310                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4311                  !!!next-token;                  !!!next-token;
4312                  redo B;                  next B;
4313                } else {                } else {
4314                  !!!cp ('t138');                  !!!cp ('t138');
4315                  #                  #
# Line 3723  sub _tree_construction_main ($) { Line 4320  sub _tree_construction_main ($) {
4320                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4321                  !!!cp ('t139');                  !!!cp ('t139');
4322                  ## As if <head>                  ## As if <head>
4323                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4324                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4325                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4326                        [$self->{head_element}, $el_category->{head}];
4327    
4328                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4329                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4330                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4331                  !!!cp ('t140');                  !!!cp ('t140');
4332                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4333                  ## Ignore the token                  ## Ignore the token
4334                  !!!next-token;                  !!!next-token;
4335                  redo B;                  next B;
4336                } else {                } else {
4337                  !!!cp ('t141');                  !!!cp ('t141');
4338                }                }
# Line 3746  sub _tree_construction_main ($) { Line 4344  sub _tree_construction_main ($) {
4344                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4345                  !!!cp ('t142');                  !!!cp ('t142');
4346                  ## As if <head>                  ## As if <head>
4347                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4348                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4349                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4350                        [$self->{head_element}, $el_category->{head}];
4351    
4352                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4353                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3763  sub _tree_construction_main ($) { Line 4362  sub _tree_construction_main ($) {
4362                  #                  #
4363                } else {                } else {
4364                  !!!cp ('t145');                  !!!cp ('t145');
4365                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4366                  ## Ignore the token                  ## Ignore the token
4367                  !!!next-token;                  !!!next-token;
4368                  redo B;                  next B;
4369                }                }
4370              }              }
4371    
# Line 3774  sub _tree_construction_main ($) { Line 4373  sub _tree_construction_main ($) {
4373                !!!cp ('t146');                !!!cp ('t146');
4374                ## As if </noscript>                ## As if </noscript>
4375                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4376                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4377                                
4378                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4379                ## As if </head>                ## As if </head>
# Line 3790  sub _tree_construction_main ($) { Line 4389  sub _tree_construction_main ($) {
4389              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4390  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4391                !!!cp ('t148');                !!!cp ('t148');
4392                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4393                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4394                !!!next-token;                !!!next-token;
4395                redo B;                next B;
4396              } else {              } else {
4397                !!!cp ('t149');                !!!cp ('t149');
4398              }              }
4399    
4400              ## "after head" insertion mode              ## "after head" insertion mode
4401              ## As if <body>              ## As if <body>
4402              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4403              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4404              ## reprocess              ## reprocess
4405              redo B;              next B;
4406            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4407              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4408            }            !!!cp ('t149.1');
4409    
4410              ## NOTE: As if <head>
4411              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4412              $self->{open_elements}->[-1]->[0]->append_child
4413                  ($self->{head_element});
4414              #push @{$self->{open_elements}},
4415              #    [$self->{head_element}, $el_category->{head}];
4416              #$self->{insertion_mode} = IN_HEAD_IM;
4417              ## NOTE: Reprocess.
4418    
4419              ## NOTE: As if </head>
4420              #pop @{$self->{open_elements}};
4421              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4422              ## NOTE: Reprocess.
4423              
4424              #
4425            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4426              !!!cp ('t149.2');
4427    
4428              ## NOTE: As if </head>
4429              pop @{$self->{open_elements}};
4430              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4431              ## NOTE: Reprocess.
4432    
4433              #
4434            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4435              !!!cp ('t149.3');
4436    
4437              !!!parse-error (type => 'in noscript:#eof', token => $token);
4438    
4439              ## As if </noscript>
4440              pop @{$self->{open_elements}};
4441              #$self->{insertion_mode} = IN_HEAD_IM;
4442              ## NOTE: Reprocess.
4443    
4444              ## NOTE: As if </head>
4445              pop @{$self->{open_elements}};
4446              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4447              ## NOTE: Reprocess.
4448    
4449              #
4450            } else {
4451              !!!cp ('t149.4');
4452              #
4453            }
4454    
4455            ## NOTE: As if <body>
4456            !!!insert-element ('body',, $token);
4457            $self->{insertion_mode} = IN_BODY_IM;
4458            ## NOTE: Reprocess.
4459            next B;
4460          } else {
4461            die "$0: $token->{type}: Unknown token type";
4462          }
4463    
4464            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4465      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3818  sub _tree_construction_main ($) { Line 4471  sub _tree_construction_main ($) {
4471              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4472    
4473              !!!next-token;              !!!next-token;
4474              redo B;              next B;
4475            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4476              if ({              if ({
4477                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3826  sub _tree_construction_main ($) { Line 4479  sub _tree_construction_main ($) {
4479                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4480                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4481                  ## have an element in table scope                  ## have an element in table scope
4482                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4483                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4484                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4485                      !!!cp ('t151');                      !!!cp ('t151');
4486                      $tn = $node->[1];  
4487                      last INSCOPE;                      ## Close the cell
4488                    } elsif ({                      !!!back-token; # <x>
4489                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4490                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4491                                  line => $token->{line},
4492                                  column => $token->{column}};
4493                        next B;
4494                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4495                      !!!cp ('t152');                      !!!cp ('t152');
4496                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4497                    }                      last;
                 } # INSCOPE  
                   unless (defined $tn) {  
                     !!!cp ('t153');  
 ## TODO: This error type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
4498                    }                    }
4499                                    }
4500                  !!!cp ('t154');  
4501                  ## Close the cell                  !!!cp ('t153');
4502                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
4503                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      value => $token->{tag_name}, token => $token);
4504                  redo B;                  ## Ignore the token
4505                    !!!nack ('t153.1');
4506                    !!!next-token;
4507                    next B;
4508                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4509                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4510                                    
4511                  ## As if </caption>                  ## NOTE: As if </caption>.
4512                  ## have a table element in table scope                  ## have a table element in table scope
4513                  my $i;                  my $i;
4514                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4515                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4516                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4517                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4518                      $i = $_;                        !!!cp ('t155');
4519                      last INSCOPE;                        $i = $_;
4520                    } elsif ({                        last INSCOPE;
4521                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4522                             }->{$node->[1]}) {                        !!!cp ('t156');
4523                      !!!cp ('t156');                        last;
4524                      last INSCOPE;                      }
4525                    }                    }
4526    
4527                      !!!cp ('t157');
4528                      !!!parse-error (type => 'start tag not allowed',
4529                                      value => $token->{tag_name}, token => $token);
4530                      ## Ignore the token
4531                      !!!nack ('t157.1');
4532                      !!!next-token;
4533                      next B;
4534                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
 ## TODO: this type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4535                                    
4536                  ## generate implied end tags                  ## generate implied end tags
4537                  while ({                  while ($self->{open_elements}->[-1]->[1]
4538                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4539                    !!!cp ('t158');                    !!!cp ('t158');
4540                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4541                  }                  }
4542    
4543                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4544                    !!!cp ('t159');                    !!!cp ('t159');
4545                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4546                                      value => $self->{open_elements}->[-1]->[0]
4547                                          ->manakai_local_name,
4548                                      token => $token);
4549                  } else {                  } else {
4550                    !!!cp ('t160');                    !!!cp ('t160');
4551                  }                  }
# Line 3904  sub _tree_construction_main ($) { Line 4557  sub _tree_construction_main ($) {
4557                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4558                                    
4559                  ## reprocess                  ## reprocess
4560                  redo B;                  !!!ack-later;
4561                    next B;
4562                } else {                } else {
4563                  !!!cp ('t161');                  !!!cp ('t161');
4564                  #                  #
# Line 3920  sub _tree_construction_main ($) { Line 4574  sub _tree_construction_main ($) {
4574                  my $i;                  my $i;
4575                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4576                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4577                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4578                      !!!cp ('t163');                      !!!cp ('t163');
4579                      $i = $_;                      $i = $_;
4580                      last INSCOPE;                      last INSCOPE;
4581                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4582                      !!!cp ('t164');                      !!!cp ('t164');
4583                      last INSCOPE;                      last INSCOPE;
4584                    }                    }
4585                  } # INSCOPE                  } # INSCOPE
4586                    unless (defined $i) {                    unless (defined $i) {
4587                      !!!cp ('t165');                      !!!cp ('t165');
4588                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4589                      ## Ignore the token                      ## Ignore the token
4590                      !!!next-token;                      !!!next-token;
4591                      redo B;                      next B;
4592                    }                    }
4593                                    
4594                  ## generate implied end tags                  ## generate implied end tags
4595                  while ({                  while ($self->{open_elements}->[-1]->[1]
4596                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4597                    !!!cp ('t166');                    !!!cp ('t166');
4598                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4599                  }                  }
4600    
4601                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4602                            ne $token->{tag_name}) {
4603                    !!!cp ('t167');                    !!!cp ('t167');
4604                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4605                                      value => $self->{open_elements}->[-1]->[0]
4606                                          ->manakai_local_name,
4607                                      token => $token);
4608                  } else {                  } else {
4609                    !!!cp ('t168');                    !!!cp ('t168');
4610                  }                  }
# Line 3961  sub _tree_construction_main ($) { Line 4616  sub _tree_construction_main ($) {
4616                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4617                                    
4618                  !!!next-token;                  !!!next-token;
4619                  redo B;                  next B;
4620                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4621                  !!!cp ('t169');                  !!!cp ('t169');
4622                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4623                  ## Ignore the token                  ## Ignore the token
4624                  !!!next-token;                  !!!next-token;
4625                  redo B;                  next B;
4626                } else {                } else {
4627                  !!!cp ('t170');                  !!!cp ('t170');
4628                  #                  #
# Line 3976  sub _tree_construction_main ($) { Line 4631  sub _tree_construction_main ($) {
4631                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4632                  ## have a table element in table scope                  ## have a table element in table scope
4633                  my $i;                  my $i;
4634                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4635                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4636                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4637                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4638                      $i = $_;                        !!!cp ('t171');
4639                      last INSCOPE;                        $i = $_;
4640                    } elsif ({                        last INSCOPE;
4641                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4642                             }->{$node->[1]}) {                        !!!cp ('t172');
4643                      !!!cp ('t172');                        last;
4644                      last INSCOPE;                      }
4645                    }                    }
4646    
4647                      !!!cp ('t173');
4648                      !!!parse-error (type => 'unmatched end tag',
4649                                      value => $token->{tag_name}, token => $token);
4650                      ## Ignore the token
4651                      !!!next-token;
4652                      next B;
4653                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4654                                    
4655                  ## generate implied end tags                  ## generate implied end tags
4656                  while ({                  while ($self->{open_elements}->[-1]->[1]
4657                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4658                    !!!cp ('t174');                    !!!cp ('t174');
4659                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4660                  }                  }
4661                                    
4662                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4663                    !!!cp ('t175');                    !!!cp ('t175');
4664                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4665                                      value => $self->{open_elements}->[-1]->[0]
4666                                          ->manakai_local_name,
4667                                      token => $token);
4668                  } else {                  } else {
4669                    !!!cp ('t176');                    !!!cp ('t176');
4670                  }                  }
# Line 4019  sub _tree_construction_main ($) { Line 4676  sub _tree_construction_main ($) {
4676                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4677                                    
4678                  !!!next-token;                  !!!next-token;
4679                  redo B;                  next B;
4680                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4681                  !!!cp ('t177');                  !!!cp ('t177');
4682                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4683                  ## Ignore the token                  ## Ignore the token
4684                  !!!next-token;                  !!!next-token;
4685                  redo B;                  next B;
4686                } else {                } else {
4687                  !!!cp ('t178');                  !!!cp ('t178');
4688                  #                  #
# Line 4038  sub _tree_construction_main ($) { Line 4695  sub _tree_construction_main ($) {
4695                ## have an element in table scope                ## have an element in table scope
4696                my $i;                my $i;
4697                my $tn;                my $tn;
4698                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4699                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4700                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4701                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4702                    $i = $_;                      !!!cp ('t179');
4703                    last INSCOPE;                      $i = $_;
4704                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4705                    !!!cp ('t180');                      ## Close the cell
4706                    $tn = $node->[1];                      !!!back-token; # </x>
4707                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4708                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4709                  } elsif ({                                column => $token->{column}};
4710                            table => 1, html => 1,                      next B;
4711                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4712                    !!!cp ('t181');                      !!!cp ('t180');
4713                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
4714                        ## NOTE: There is exactly one |td| or |th| element
4715                        ## in scope in the stack of open elements by definition.
4716                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4717                        ## ISSUE: Can this be reached?
4718                        !!!cp ('t181');
4719                        last;
4720                      }
4721                  }                  }
4722                } # INSCOPE  
               unless (defined $i) {  
4723                  !!!cp ('t182');                  !!!cp ('t182');
4724                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4725                        value => $token->{tag_name}, token => $token);
4726                  ## Ignore the token                  ## Ignore the token
4727                  !!!next-token;                  !!!next-token;
4728                  redo B;                  next B;
4729                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4730              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4731                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4732                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4733    
4734                ## As if </caption>                ## As if </caption>
4735                ## have a table element in table scope                ## have a table element in table scope
4736                my $i;                my $i;
4737                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4738                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4739                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4740                    !!!cp ('t184');                    !!!cp ('t184');
4741                    $i = $_;                    $i = $_;
4742                    last INSCOPE;                    last INSCOPE;
4743                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4744                    !!!cp ('t185');                    !!!cp ('t185');
4745                    last INSCOPE;                    last INSCOPE;
4746                  }                  }
4747                } # INSCOPE                } # INSCOPE
4748                unless (defined $i) {                unless (defined $i) {
4749                  !!!cp ('t186');                  !!!cp ('t186');
4750                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4751                  ## Ignore the token                  ## Ignore the token
4752                  !!!next-token;                  !!!next-token;
4753                  redo B;                  next B;
4754                }                }
4755                                
4756                ## generate implied end tags                ## generate implied end tags
4757                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
4758                  !!!cp ('t187');                  !!!cp ('t187');
4759                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4760                }                }
4761    
4762                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4763                  !!!cp ('t188');                  !!!cp ('t188');
4764                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
4765                                    value => $self->{open_elements}->[-1]->[0]
4766                                        ->manakai_local_name,
4767                                    token => $token);
4768                } else {                } else {
4769                  !!!cp ('t189');                  !!!cp ('t189');
4770                }                }
# Line 4120  sub _tree_construction_main ($) { Line 4776  sub _tree_construction_main ($) {
4776                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4777    
4778                ## reprocess                ## reprocess
4779                redo B;                next B;
4780              } elsif ({              } elsif ({
4781                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4782                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4783                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4784                  !!!cp ('t190');                  !!!cp ('t190');
4785                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4786                  ## Ignore the token                  ## Ignore the token
4787                  !!!next-token;                  !!!next-token;
4788                  redo B;                  next B;
4789                } else {                } else {
4790                  !!!cp ('t191');                  !!!cp ('t191');
4791                  #                  #
# Line 4140  sub _tree_construction_main ($) { Line 4796  sub _tree_construction_main ($) {
4796                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4797                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4798                !!!cp ('t192');                !!!cp ('t192');
4799                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4800                ## Ignore the token                ## Ignore the token
4801                !!!next-token;                !!!next-token;
4802                redo B;                next B;
4803              } else {              } else {
4804                !!!cp ('t193');                !!!cp ('t193');
4805                #                #
4806              }              }
4807          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4808            for my $entry (@{$self->{open_elements}}) {
4809              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4810                !!!cp ('t75');
4811                !!!parse-error (type => 'in body:#eof', token => $token);
4812                last;
4813              }
4814            }
4815    
4816            ## Stop parsing.
4817            last B;
4818        } else {        } else {
4819          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4820        }        }
# Line 4163  sub _tree_construction_main ($) { Line 4830  sub _tree_construction_main ($) {
4830            unless (length $token->{data}) {            unless (length $token->{data}) {
4831              !!!cp ('t194');              !!!cp ('t194');
4832              !!!next-token;              !!!next-token;
4833              redo B;              next B;
4834            } else {            } else {
4835              !!!cp ('t195');              !!!cp ('t195');
4836            }            }
4837          }          }
4838    
4839              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4840    
4841              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4842              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4177  sub _tree_construction_main ($) { Line 4844  sub _tree_construction_main ($) {
4844              ## result in a new Text node.              ## result in a new Text node.
4845              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4846                            
4847              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]}) {  
4848                # MUST                # MUST
4849                my $foster_parent_element;                my $foster_parent_element;
4850                my $next_sibling;                my $next_sibling;
4851                my $prev_sibling;                my $prev_sibling;
4852                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4853                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4854                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4855                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4856                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4221  sub _tree_construction_main ($) { Line 4885  sub _tree_construction_main ($) {
4885          }          }
4886                            
4887          !!!next-token;          !!!next-token;
4888          redo B;          next B;
4889        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4890              if ({              if ({
4891                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4229  sub _tree_construction_main ($) { Line 4893  sub _tree_construction_main ($) {
4893                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4894                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4895                  ## Clear back to table context                  ## Clear back to table context
4896                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4897                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4898                    !!!cp ('t201');                    !!!cp ('t201');
4899                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4900                  }                  }
4901                                    
4902                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
4903                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4904                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4905                }                }
# Line 4243  sub _tree_construction_main ($) { Line 4907  sub _tree_construction_main ($) {
4907                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4908                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4909                    !!!cp ('t202');                    !!!cp ('t202');
4910                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
4911                  }                  }
4912                                    
4913                  ## Clear back to table body context                  ## Clear back to table body context
4914                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4915                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4916                    !!!cp ('t203');                    !!!cp ('t203');
4917                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
4918                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4258  sub _tree_construction_main ($) { Line 4921  sub _tree_construction_main ($) {
4921                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4922                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4923                    !!!cp ('t204');                    !!!cp ('t204');
4924                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4925                      !!!nack ('t204');
4926                    !!!next-token;                    !!!next-token;
4927                    redo B;                    next B;
4928                  } else {                  } else {
4929                    !!!cp ('t205');                    !!!cp ('t205');
4930                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
4931                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4932                  }                  }
4933                } else {                } else {
# Line 4271  sub _tree_construction_main ($) { Line 4935  sub _tree_construction_main ($) {
4935                }                }
4936    
4937                ## Clear back to table row context                ## Clear back to table row context
4938                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4939                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
4940                  !!!cp ('t207');                  !!!cp ('t207');
4941                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4942                }                }
4943                                
4944                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4945                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
4946    
4947                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
4948                                
4949                  !!!nack ('t207.1');
4950                !!!next-token;                !!!next-token;
4951                redo B;                next B;
4952              } elsif ({              } elsif ({
4953                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4954                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4296  sub _tree_construction_main ($) { Line 4960  sub _tree_construction_main ($) {
4960                  my $i;                  my $i;
4961                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4962                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4963                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
4964                      !!!cp ('t208');                      !!!cp ('t208');
4965                      $i = $_;                      $i = $_;
4966                      last INSCOPE;                      last INSCOPE;
4967                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
4968                      !!!cp ('t209');                      !!!cp ('t209');
4969                      last INSCOPE;                      last INSCOPE;
4970                    }                    }
4971                  } # INSCOPE                  } # INSCOPE
4972                  unless (defined $i) {                  unless (defined $i) {
4973                   !!!cp ('t210');                    !!!cp ('t210');
4974  ## TODO: This type is wrong.  ## TODO: This type is wrong.
4975                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4976                    ## Ignore the token                    ## Ignore the token
4977                      !!!nack ('t210.1');
4978                    !!!next-token;                    !!!next-token;
4979                    redo B;                    next B;
4980                  }                  }
4981                                    
4982                  ## Clear back to table row context                  ## Clear back to table row context
4983                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4984                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4985                    !!!cp ('t211');                    !!!cp ('t211');
4986                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
4987                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4333  sub _tree_construction_main ($) { Line 4992  sub _tree_construction_main ($) {
4992                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4993                    !!!cp ('t212');                    !!!cp ('t212');
4994                    ## reprocess                    ## reprocess
4995                    redo B;                    !!!ack-later;
4996                      next B;
4997                  } else {                  } else {
4998                    !!!cp ('t213');                    !!!cp ('t213');
4999                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4345  sub _tree_construction_main ($) { Line 5005  sub _tree_construction_main ($) {
5005                  my $i;                  my $i;
5006                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5007                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5008                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5009                      !!!cp ('t214');                      !!!cp ('t214');
5010                      $i = $_;                      $i = $_;
5011                      last INSCOPE;                      last INSCOPE;
5012                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5013                      !!!cp ('t215');                      !!!cp ('t215');
5014                      last INSCOPE;                      last INSCOPE;
5015                    }                    }
# Line 4361  sub _tree_construction_main ($) { Line 5017  sub _tree_construction_main ($) {
5017                  unless (defined $i) {                  unless (defined $i) {
5018                    !!!cp ('t216');                    !!!cp ('t216');
5019  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5020                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5021                    ## Ignore the token                    ## Ignore the token
5022                      !!!nack ('t216.1');
5023                    !!!next-token;                    !!!next-token;
5024                    redo B;                    next B;
5025                  }                  }
5026    
5027                  ## Clear back to table body context                  ## Clear back to table body context
5028                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5029                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5030                    !!!cp ('t217');                    !!!cp ('t217');
5031                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5032                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4392  sub _tree_construction_main ($) { Line 5048  sub _tree_construction_main ($) {
5048    
5049                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5050                  ## Clear back to table context                  ## Clear back to table context
5051                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5052                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5053                    !!!cp ('t219');                    !!!cp ('t219');
5054                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5055                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5056                  }                  }
5057                                    
5058                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5059                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5060                  ## reprocess                  ## reprocess
5061                  redo B;                  !!!ack-later;
5062                    next B;
5063                } elsif ({                } elsif ({
5064                          caption => 1,                          caption => 1,
5065                          colgroup => 1,                          colgroup => 1,
5066                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5067                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5068                  ## Clear back to table context                  ## Clear back to table context
5069                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5070                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5071                    !!!cp ('t220');                    !!!cp ('t220');
5072                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5073                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4419  sub _tree_construction_main ($) { Line 5076  sub _tree_construction_main ($) {
5076                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5077                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5078                                    
5079                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5080                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5081                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5082                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4428  sub _tree_construction_main ($) { Line 5085  sub _tree_construction_main ($) {
5085                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5086                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5087                  !!!next-token;                  !!!next-token;
5088                  redo B;                  !!!nack ('t220.1');
5089                    next B;
5090                } else {                } else {
5091                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5092                }                }
5093              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5094                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5095                                  value => $self->{open_elements}->[-1]->[0]
5096                                      ->manakai_local_name,
5097                                  token => $token);
5098    
5099                ## As if </table>                ## As if </table>
5100                ## have a table element in table scope                ## have a table element in table scope
5101                my $i;                my $i;
5102                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5103                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5104                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5105                    !!!cp ('t221');                    !!!cp ('t221');
5106                    $i = $_;                    $i = $_;
5107                    last INSCOPE;                    last INSCOPE;
5108                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5109                    !!!cp ('t222');                    !!!cp ('t222');
5110                    last INSCOPE;                    last INSCOPE;
5111                  }                  }
# Line 4455  sub _tree_construction_main ($) { Line 5113  sub _tree_construction_main ($) {
5113                unless (defined $i) {                unless (defined $i) {
5114                  !!!cp ('t223');                  !!!cp ('t223');
5115  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5116                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5117                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5118                    !!!nack ('t223.1');
5119                  !!!next-token;                  !!!next-token;
5120                  redo B;                  next B;
5121                }                }
5122                                
5123    ## TODO: Followings are removed from the latest spec.
5124                ## generate implied end tags                ## generate implied end tags
5125                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5126                  !!!cp ('t224');                  !!!cp ('t224');
5127                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5128                }                }
5129    
5130                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5131                  !!!cp ('t225');                  !!!cp ('t225');
5132  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5133                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5134                                    value => $self->{open_elements}->[-1]->[0]
5135                                        ->manakai_local_name,
5136                                    token => $token);
5137                } else {                } else {
5138                  !!!cp ('t226');                  !!!cp ('t226');
5139                }                }
# Line 4482  sub _tree_construction_main ($) { Line 5143  sub _tree_construction_main ($) {
5143    
5144                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5145    
5146                ## reprocess            ## reprocess
5147                redo B;            !!!ack-later;
5148              next B;
5149            } elsif ($token->{tag_name} eq 'style') {
5150              if (not $open_tables->[-1]->[1]) { # tainted
5151                !!!cp ('t227.8');
5152                ## NOTE: This is a "as if in head" code clone.
5153                $parse_rcdata->(CDATA_CONTENT_MODEL);
5154                next B;
5155              } else {
5156                !!!cp ('t227.7');
5157                #
5158              }
5159            } elsif ($token->{tag_name} eq 'script') {
5160              if (not $open_tables->[-1]->[1]) { # tainted
5161                !!!cp ('t227.6');
5162                ## NOTE: This is a "as if in head" code clone.
5163                $script_start_tag->();
5164                next B;
5165              } else {
5166                !!!cp ('t227.5');
5167                #
5168              }
5169            } elsif ($token->{tag_name} eq 'input') {
5170              if (not $open_tables->[-1]->[1]) { # tainted
5171                if ($token->{attributes}->{type}) { ## TODO: case
5172                  my $type = lc $token->{attributes}->{type}->{value};
5173                  if ($type eq 'hidden') {
5174                    !!!cp ('t227.3');
5175                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5176    
5177                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5178    
5179                    ## TODO: form element pointer
5180    
5181                    pop @{$self->{open_elements}};
5182    
5183                    !!!next-token;
5184                    !!!ack ('t227.2.1');
5185                    next B;
5186                  } else {
5187                    !!!cp ('t227.2');
5188                    #
5189                  }
5190                } else {
5191                  !!!cp ('t227.1');
5192                  #
5193                }
5194              } else {
5195                !!!cp ('t227.4');
5196                #
5197              }
5198          } else {          } else {
5199            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
5200            #            #
5201          }          }
5202    
5203            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5204    
5205            $insert = $insert_to_foster;
5206            #
5207        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5208              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5209                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4498  sub _tree_construction_main ($) { Line 5211  sub _tree_construction_main ($) {
5211                my $i;                my $i;
5212                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5213                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5214                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5215                    !!!cp ('t228');                    !!!cp ('t228');
5216                    $i = $_;                    $i = $_;
5217                    last INSCOPE;                    last INSCOPE;
5218                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5219                    !!!cp ('t229');                    !!!cp ('t229');
5220                    last INSCOPE;                    last INSCOPE;
5221                  }                  }
5222                } # INSCOPE                } # INSCOPE
5223                unless (defined $i) {                unless (defined $i) {
5224                  !!!cp ('t230');                  !!!cp ('t230');
5225                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5226                  ## Ignore the token                  ## Ignore the token
5227                    !!!nack ('t230.1');
5228                  !!!next-token;                  !!!next-token;
5229                  redo B;                  next B;
5230                } else {                } else {
5231                  !!!cp ('t232');                  !!!cp ('t232');
5232                }                }
5233    
5234                ## Clear back to table row context                ## Clear back to table row context
5235                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5236                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5237                  !!!cp ('t231');                  !!!cp ('t231');
5238  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5239                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4531  sub _tree_construction_main ($) { Line 5242  sub _tree_construction_main ($) {
5242                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5243                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5244                !!!next-token;                !!!next-token;
5245                redo B;                !!!nack ('t231.1');
5246                  next B;
5247              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5248                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5249                  ## As if </tr>                  ## As if </tr>
# Line 4539  sub _tree_construction_main ($) { Line 5251  sub _tree_construction_main ($) {
5251                  my $i;                  my $i;
5252                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5253                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5254                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5255                      !!!cp ('t233');                      !!!cp ('t233');
5256                      $i = $_;                      $i = $_;
5257                      last INSCOPE;                      last INSCOPE;
5258                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5259                      !!!cp ('t234');                      !!!cp ('t234');
5260                      last INSCOPE;                      last INSCOPE;
5261                    }                    }
# Line 4553  sub _tree_construction_main ($) { Line 5263  sub _tree_construction_main ($) {
5263                  unless (defined $i) {                  unless (defined $i) {
5264                    !!!cp ('t235');                    !!!cp ('t235');
5265  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5266                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5267                    ## Ignore the token                    ## Ignore the token
5268                      !!!nack ('t236.1');
5269                    !!!next-token;                    !!!next-token;
5270                    redo B;                    next B;
5271                  }                  }
5272                                    
5273                  ## Clear back to table row context                  ## Clear back to table row context
5274                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5275                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5276                    !!!cp ('t236');                    !!!cp ('t236');
5277  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5278                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4578  sub _tree_construction_main ($) { Line 5288  sub _tree_construction_main ($) {
5288                  my $i;                  my $i;
5289                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5290                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5291                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5292                      !!!cp ('t237');                      !!!cp ('t237');
5293                      $i = $_;                      $i = $_;
5294                      last INSCOPE;                      last INSCOPE;
5295                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5296                      !!!cp ('t238');                      !!!cp ('t238');
5297                      last INSCOPE;                      last INSCOPE;
5298                    }                    }
5299                  } # INSCOPE                  } # INSCOPE
5300                  unless (defined $i) {                  unless (defined $i) {
5301                    !!!cp ('t239');                    !!!cp ('t239');
5302                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5303                    ## Ignore the token                    ## Ignore the token
5304                      !!!nack ('t239.1');
5305                    !!!next-token;                    !!!next-token;
5306                    redo B;                    next B;
5307                  }                  }
5308                                    
5309                  ## Clear back to table body context                  ## Clear back to table body context
5310                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5311                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5312                    !!!cp ('t240');                    !!!cp ('t240');
5313                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5314                  }                  }
# Line 4628  sub _tree_construction_main ($) { Line 5334  sub _tree_construction_main ($) {
5334                my $i;                my $i;
5335                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5336                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5337                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5338                    !!!cp ('t241');                    !!!cp ('t241');
5339                    $i = $_;                    $i = $_;
5340                    last INSCOPE;                    last INSCOPE;
5341                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5342                    !!!cp ('t242');                    !!!cp ('t242');
5343                    last INSCOPE;                    last INSCOPE;
5344                  }                  }
5345                } # INSCOPE                } # INSCOPE
5346                unless (defined $i) {                unless (defined $i) {
5347                  !!!cp ('t243');                  !!!cp ('t243');
5348                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5349                  ## Ignore the token                  ## Ignore the token
5350                    !!!nack ('t243.1');
5351                  !!!next-token;                  !!!next-token;
5352                  redo B;                  next B;
5353                }                }
5354                                    
5355                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4653  sub _tree_construction_main ($) { Line 5358  sub _tree_construction_main ($) {
5358                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5359                                
5360                !!!next-token;                !!!next-token;
5361                redo B;                next B;
5362              } elsif ({              } elsif ({
5363                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5364                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4663  sub _tree_construction_main ($) { Line 5368  sub _tree_construction_main ($) {
5368                  my $i;                  my $i;
5369                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5370                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5371                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5372                      !!!cp ('t247');                      !!!cp ('t247');
5373                      $i = $_;                      $i = $_;
5374                      last INSCOPE;                      last INSCOPE;
5375                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5376                      !!!cp ('t248');                      !!!cp ('t248');
5377                      last INSCOPE;                      last INSCOPE;
5378                    }                    }
5379                  } # INSCOPE                  } # INSCOPE
5380                    unless (defined $i) {                    unless (defined $i) {
5381                      !!!cp ('t249');                      !!!cp ('t249');
5382                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5383                      ## Ignore the token                      ## Ignore the token
5384                        !!!nack ('t249.1');
5385                      !!!next-token;                      !!!next-token;
5386                      redo B;                      next B;
5387                    }                    }
5388                                    
5389                  ## As if </tr>                  ## As if </tr>
# Line 4687  sub _tree_construction_main ($) { Line 5391  sub _tree_construction_main ($) {
5391                  my $i;                  my $i;
5392                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5393                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5394                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5395                      !!!cp ('t250');                      !!!cp ('t250');
5396                      $i = $_;                      $i = $_;
5397                      last INSCOPE;                      last INSCOPE;
5398                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5399                      !!!cp ('t251');                      !!!cp ('t251');
5400                      last INSCOPE;                      last INSCOPE;
5401                    }                    }
5402                  } # INSCOPE                  } # INSCOPE
5403                    unless (defined $i) {                    unless (defined $i) {
5404                      !!!cp ('t252');                      !!!cp ('t252');
5405                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5406                      ## Ignore the token                      ## Ignore the token
5407                        !!!nack ('t252.1');
5408                      !!!next-token;                      !!!next-token;
5409                      redo B;                      next B;
5410                    }                    }
5411                                    
5412                  ## Clear back to table row context                  ## Clear back to table row context
5413                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5414                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5415                    !!!cp ('t253');                    !!!cp ('t253');
5416  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5417                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4724  sub _tree_construction_main ($) { Line 5426  sub _tree_construction_main ($) {
5426                my $i;                my $i;
5427                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5428                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5429                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5430                    !!!cp ('t254');                    !!!cp ('t254');
5431                    $i = $_;                    $i = $_;
5432                    last INSCOPE;                    last INSCOPE;
5433                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5434                    !!!cp ('t255');                    !!!cp ('t255');
5435                    last INSCOPE;                    last INSCOPE;
5436                  }                  }
5437                } # INSCOPE                } # INSCOPE
5438                unless (defined $i) {                unless (defined $i) {
5439                  !!!cp ('t256');                  !!!cp ('t256');
5440                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5441                  ## Ignore the token                  ## Ignore the token
5442                    !!!nack ('t256.1');
5443                  !!!next-token;                  !!!next-token;
5444                  redo B;                  next B;
5445                }                }
5446    
5447                ## Clear back to table body context                ## Clear back to table body context
5448                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5449                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5450                  !!!cp ('t257');                  !!!cp ('t257');
5451  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5452                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4754  sub _tree_construction_main ($) { Line 5454  sub _tree_construction_main ($) {
5454    
5455                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5456                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5457                  !!!nack ('t257.1');
5458                !!!next-token;                !!!next-token;
5459                redo B;                next B;
5460              } elsif ({              } elsif ({
5461                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5462                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5463                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5464                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5465                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5466                !!!cp ('t258');            !!!cp ('t258');
5467                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5468                ## Ignore the token            ## Ignore the token
5469                !!!next-token;            !!!nack ('t258.1');
5470                redo B;             !!!next-token;
5471              next B;
5472          } else {          } else {
5473            !!!cp ('t259');            !!!cp ('t259');
5474            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5475    
5476            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5477            #            #
5478          }          }
5479          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5480            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5481                    @{$self->{open_elements}} == 1) { # redundant, maybe
5482              !!!parse-error (type => 'in body:#eof', token => $token);
5483              !!!cp ('t259.1');
5484              #
5485            } else {
5486              !!!cp ('t259.2');
5487              #
5488            }
5489    
5490            ## Stop parsing
5491            last B;
5492        } else {        } else {
5493          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5494        }        }
# Line 4784  sub _tree_construction_main ($) { Line 5499  sub _tree_construction_main ($) {
5499                unless (length $token->{data}) {                unless (length $token->{data}) {
5500                  !!!cp ('t260');                  !!!cp ('t260');
5501                  !!!next-token;                  !!!next-token;
5502                  redo B;                  next B;
5503                }                }
5504              }              }
5505                            
# Line 4793  sub _tree_construction_main ($) { Line 5508  sub _tree_construction_main ($) {
5508            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5509              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5510                !!!cp ('t262');                !!!cp ('t262');
5511                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5512                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5513                  !!!ack ('t262.1');
5514                !!!next-token;                !!!next-token;
5515                redo B;                next B;
5516              } else {              } else {
5517                !!!cp ('t263');                !!!cp ('t263');
5518                #                #
5519              }              }
5520            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5521              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5522                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5523                  !!!cp ('t264');                  !!!cp ('t264');
5524                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5525                  ## Ignore the token                  ## Ignore the token
5526                  !!!next-token;                  !!!next-token;
5527                  redo B;                  next B;
5528                } else {                } else {
5529                  !!!cp ('t265');                  !!!cp ('t265');
5530                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5531                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5532                  !!!next-token;                  !!!next-token;
5533                  redo B;                              next B;            
5534                }                }
5535              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5536                !!!cp ('t266');                !!!cp ('t266');
5537                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5538                ## Ignore the token                ## Ignore the token
5539                !!!next-token;                !!!next-token;
5540                redo B;                next B;
5541              } else {              } else {
5542                !!!cp ('t267');                !!!cp ('t267');
5543                #                #
5544              }              }
5545            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5546              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5547            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5548              !!!cp ('t270.2');
5549              ## Stop parsing.
5550              last B;
5551            } else {
5552              ## NOTE: As if </colgroup>.
5553              !!!cp ('t270.1');
5554              pop @{$self->{open_elements}}; # colgroup
5555              $self->{insertion_mode} = IN_TABLE_IM;
5556              ## Reprocess.
5557              next B;
5558            }
5559          } else {
5560            die "$0: $token->{type}: Unknown token type";
5561          }
5562    
5563            ## As if </colgroup>            ## As if </colgroup>
5564            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5565              !!!cp ('t269');              !!!cp ('t269');
5566              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5567                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5568              ## Ignore the token              ## Ignore the token
5569                !!!nack ('t269.1');
5570              !!!next-token;              !!!next-token;
5571              redo B;              next B;
5572            } else {            } else {
5573              !!!cp ('t270');              !!!cp ('t270');
5574              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5575              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5576                !!!ack-later;
5577              ## reprocess              ## reprocess
5578              redo B;              next B;
5579            }            }
5580      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5581        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5582          !!!cp ('t271');          !!!cp ('t271');
5583          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5584          !!!next-token;          !!!next-token;
5585          redo B;          next B;
5586        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5587              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5588                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5589                  !!!cp ('t272');              !!!cp ('t272');
5590                  ## As if </option>              ## As if </option>
5591                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5592                } else {            } else {
5593                  !!!cp ('t273');              !!!cp ('t273');
5594                }            }
5595    
5596                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5597                !!!next-token;            !!!nack ('t273.1');
5598                redo B;            !!!next-token;
5599              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5600                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5601                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5602                  ## As if </option>              !!!cp ('t274');
5603                  pop @{$self->{open_elements}};              ## As if </option>
5604                } else {              pop @{$self->{open_elements}};
5605                  !!!cp ('t275');            } else {
5606                }              !!!cp ('t275');
5607              }
5608    
5609                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5610                  !!!cp ('t276');              !!!cp ('t276');
5611                  ## As if </optgroup>              ## As if </optgroup>
5612                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5613                } else {            } else {
5614                  !!!cp ('t277');              !!!cp ('t277');
5615                }            }
5616    
5617                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5618                !!!next-token;            !!!nack ('t277.1');
5619                redo B;            !!!next-token;
5620              } elsif ($token->{tag_name} eq 'select') {            next B;
5621  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ($token->{tag_name} eq 'select' or
5622                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5623                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5624                ## have an element in table scope                    {
5625                my $i;                     caption => 1, table => 1,
5626                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     tbody => 1, tfoot => 1, thead => 1,
5627                  my $node = $self->{open_elements}->[$_];                     tr => 1, td => 1, th => 1,
5628                  if ($node->[1] eq $token->{tag_name}) {                    }->{$token->{tag_name}})) {
5629                    !!!cp ('t278');            ## TODO: The type below is not good - <select> is replaced by </select>
5630                    $i = $_;            !!!parse-error (type => 'not closed:select', token => $token);
5631                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
5632                  } elsif ({            ## as if there were </select> (otherwise).
5633                            table => 1, html => 1,            ## have an element in table scope
5634                           }->{$node->[1]}) {            my $i;
5635                    !!!cp ('t279');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5636                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5637                  }              if ($node->[1] & SELECT_EL) {
5638                } # INSCOPE                !!!cp ('t278');
5639                unless (defined $i) {                $i = $_;
5640                  !!!cp ('t280');                last INSCOPE;
5641                  !!!parse-error (type => 'unmatched end tag:select');              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5642                  ## Ignore the token                !!!cp ('t279');
5643                  !!!next-token;                last INSCOPE;
5644                  redo B;              }
5645                }            } # INSCOPE
5646              unless (defined $i) {
5647                !!!cp ('t280');
5648                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5649                ## Ignore the token
5650                !!!nack ('t280.1');
5651                !!!next-token;
5652                next B;
5653              }
5654                                
5655                !!!cp ('t281');            !!!cp ('t281');
5656                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5657    
5658                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5659    
5660                !!!next-token;            if ($token->{tag_name} eq 'select') {
5661                redo B;              !!!nack ('t281.2');
5662                !!!next-token;
5663                next B;
5664              } else {
5665                !!!cp ('t281.1');
5666                !!!ack-later;
5667                ## Reprocess the token.
5668                next B;
5669              }
5670          } else {          } else {
5671            !!!cp ('t282');            !!!cp ('t282');
5672            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5673            ## Ignore the token            ## Ignore the token
5674              !!!nack ('t282.1');
5675            !!!next-token;            !!!next-token;
5676            redo B;            next B;
5677          }          }
5678        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5679              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5680                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5681                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5682                  !!!cp ('t283');              !!!cp ('t283');
5683                  ## As if </option>              ## As if </option>
5684                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5685                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5686                  !!!cp ('t284');              !!!cp ('t284');
5687                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5688                } else {            } else {
5689                  !!!cp ('t285');              !!!cp ('t285');
5690                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5691                  ## Ignore the token              ## Ignore the token
5692                }            }
5693                !!!next-token;            !!!nack ('t285.1');
5694                redo B;            !!!next-token;
5695              } elsif ($token->{tag_name} eq 'option') {            next B;
5696                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5697                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5698                  pop @{$self->{open_elements}};              !!!cp ('t286');
5699                } else {              pop @{$self->{open_elements}};
5700                  !!!cp ('t287');            } else {
5701                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t287');
5702                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5703                }              ## Ignore the token
5704                !!!next-token;            }
5705                redo B;            !!!nack ('t287.1');
5706              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5707                ## have an element in table scope            next B;
5708                my $i;          } elsif ($token->{tag_name} eq 'select') {
5709                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5710                  my $node = $self->{open_elements}->[$_];            my $i;
5711                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5712                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5713                    $i = $_;              if ($node->[1] & SELECT_EL) {
5714                    last INSCOPE;                !!!cp ('t288');
5715                  } elsif ({                $i = $_;
5716                            table => 1, html => 1,                last INSCOPE;
5717                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5718                    !!!cp ('t289');                !!!cp ('t289');
5719                    last INSCOPE;                last INSCOPE;
5720                  }              }
5721                } # INSCOPE            } # INSCOPE
5722                unless (defined $i) {            unless (defined $i) {
5723                  !!!cp ('t290');              !!!cp ('t290');
5724                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5725                  ## Ignore the token              ## Ignore the token
5726                  !!!next-token;              !!!nack ('t290.1');
5727                  redo B;              !!!next-token;
5728                }              next B;
5729              }
5730                                
5731                !!!cp ('t291');            !!!cp ('t291');
5732                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5733    
5734                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5735    
5736                !!!next-token;            !!!nack ('t291.1');
5737                redo B;            !!!next-token;
5738              } elsif ({            next B;
5739                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5740                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5741                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5742                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5743                     }->{$token->{tag_name}}) {
5744  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5745                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5746                                
5747                ## have an element in table scope            ## have an element in table scope
5748                my $i;            my $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 $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5752                    !!!cp ('t292');                !!!cp ('t292');
5753                    $i = $_;                $i = $_;
5754                    last INSCOPE;                last INSCOPE;
5755                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5756                            table => 1, html => 1,                !!!cp ('t293');
5757                           }->{$node->[1]}) {                last INSCOPE;
5758                    !!!cp ('t293');              }
5759                    last INSCOPE;            } # INSCOPE
5760                  }            unless (defined $i) {
5761                } # INSCOPE              !!!cp ('t294');
5762                unless (defined $i) {              ## Ignore the token
5763                  !!!cp ('t294');              !!!nack ('t294.1');
5764                  ## Ignore the token              !!!next-token;
5765                  !!!next-token;              next B;
5766                  redo B;            }
               }  
5767                                
5768                ## As if </select>            ## As if </select>
5769                ## have an element in table scope            ## have an element in table scope
5770                undef $i;            undef $i;
5771                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5772                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5773                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5774                    !!!cp ('t295');                !!!cp ('t295');
5775                    $i = $_;                $i = $_;
5776                    last INSCOPE;                last INSCOPE;
5777                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5778  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5779                    !!!cp ('t296');                !!!cp ('t296');
5780                    last INSCOPE;                last INSCOPE;
5781                  }              }
5782                } # INSCOPE            } # INSCOPE
5783                unless (defined $i) {            unless (defined $i) {
5784                  !!!cp ('t297');              !!!cp ('t297');
5785  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
5786                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5787                  ## Ignore the </select> token              ## Ignore the </select> token
5788                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
5789                  redo B;              !!!next-token; ## TODO: ok?
5790                }              next B;
5791              }
5792                                
5793                !!!cp ('t298');            !!!cp ('t298');
5794                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5795    
5796                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5797    
5798                ## reprocess            !!!ack-later;
5799                redo B;            ## reprocess
5800              next B;
5801          } else {          } else {
5802            !!!cp ('t299');            !!!cp ('t299');
5803            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5804            ## Ignore the token            ## Ignore the token
5805              !!!nack ('t299.3');
5806            !!!next-token;            !!!next-token;
5807            redo B;            next B;
5808          }          }
5809          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5810            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5811                    @{$self->{open_elements}} == 1) { # redundant, maybe
5812              !!!cp ('t299.1');
5813              !!!parse-error (type => 'in body:#eof', token => $token);
5814            } else {
5815              !!!cp ('t299.2');
5816            }
5817    
5818            ## Stop parsing.
5819            last B;
5820        } else {        } else {
5821          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5822        }        }
# Line 5067  sub _tree_construction_main ($) { Line 5832  sub _tree_construction_main ($) {
5832            unless (length $token->{data}) {            unless (length $token->{data}) {
5833              !!!cp ('t300');              !!!cp ('t300');
5834              !!!next-token;              !!!next-token;
5835              redo B;              next B;
5836            }            }
5837          }          }
5838                    
5839          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5840            !!!cp ('t301');            !!!cp ('t301');
5841            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
5842    
5843            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5844          } else {          } else {
# Line 5081  sub _tree_construction_main ($) { Line 5846  sub _tree_construction_main ($) {
5846          }          }
5847                    
5848          ## "after body" insertion mode          ## "after body" insertion mode
5849          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5850    
5851          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5852          ## reprocess          ## reprocess
5853          redo B;          next B;
5854        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5855          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5856            !!!cp ('t303');            !!!cp ('t303');
5857            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5858                        
5859            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5860          } else {          } else {
# Line 5097  sub _tree_construction_main ($) { Line 5862  sub _tree_construction_main ($) {
5862          }          }
5863    
5864          ## "after body" insertion mode          ## "after body" insertion mode
5865          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5866    
5867          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5868            !!!ack-later;
5869          ## reprocess          ## reprocess
5870          redo B;          next B;
5871        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5872          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5873            !!!cp ('t305');            !!!cp ('t305');
5874            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5875                        
5876            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5877            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5117  sub _tree_construction_main ($) { Line 5883  sub _tree_construction_main ($) {
5883          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5884            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5885              !!!cp ('t307');              !!!cp ('t307');
5886              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
5887              ## Ignore the token              ## Ignore the token
5888              !!!next-token;              !!!next-token;
5889              redo B;              next B;
5890            } else {            } else {
5891              !!!cp ('t308');              !!!cp ('t308');
5892              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5893              !!!next-token;              !!!next-token;
5894              redo B;              next B;
5895            }            }
5896          } else {          } else {
5897            !!!cp ('t309');            !!!cp ('t309');
5898            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5899    
5900            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5901            ## reprocess            ## reprocess
5902            redo B;            next B;
5903          }          }
5904          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5905            !!!cp ('t309.2');
5906            ## Stop parsing
5907            last B;
5908        } else {        } else {
5909          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5910        }        }
# Line 5146  sub _tree_construction_main ($) { Line 5916  sub _tree_construction_main ($) {
5916            unless (length $token->{data}) {            unless (length $token->{data}) {
5917              !!!cp ('t310');              !!!cp ('t310');
5918              !!!next-token;              !!!next-token;
5919              redo B;              next B;
5920            }            }
5921          }          }
5922                    
5923          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5924            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5925              !!!cp ('t311');              !!!cp ('t311');
5926              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
5927            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5928              !!!cp ('t312');              !!!cp ('t312');
5929              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5930            } else { # "after html frameset"            } else { # "after html frameset"
5931              !!!cp ('t313');              !!!cp ('t313');
5932              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
5933    
5934              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5935              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
5936              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5937            }            }
5938                        
5939            ## Ignore the token.            ## Ignore the token.
# Line 5174  sub _tree_construction_main ($) { Line 5944  sub _tree_construction_main ($) {
5944              !!!cp ('t315');              !!!cp ('t315');
5945              !!!next-token;              !!!next-token;
5946            }            }
5947            redo B;            next B;
5948          }          }
5949                    
5950          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
5951        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5952          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5953            !!!cp ('t316');            !!!cp ('t316');
5954            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5955    
5956            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5957            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5192  sub _tree_construction_main ($) { Line 5962  sub _tree_construction_main ($) {
5962          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5963              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5964            !!!cp ('t318');            !!!cp ('t318');
5965            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5966              !!!nack ('t318.1');
5967            !!!next-token;            !!!next-token;
5968            redo B;            next B;
5969          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5970                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5971            !!!cp ('t319');            !!!cp ('t319');
5972            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5973            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5974              !!!ack ('t319.1');
5975            !!!next-token;            !!!next-token;
5976            redo B;            next B;
5977          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5978            !!!cp ('t320');            !!!cp ('t320');
5979            ## NOTE: As if in body.            ## NOTE: As if in body.
5980            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5981            redo B;            next B;
5982          } else {          } else {
5983            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5984              !!!cp ('t321');              !!!cp ('t321');
5985              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5986            } else {            } else {
5987              !!!cp ('t322');              !!!cp ('t322');
5988              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5989            }            }
5990            ## Ignore the token            ## Ignore the token
5991              !!!nack ('t322.1');
5992            !!!next-token;            !!!next-token;
5993            redo B;            next B;
5994          }          }
5995        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5996          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5997            !!!cp ('t323');            !!!cp ('t323');
5998            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5999    
6000            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6001            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5232  sub _tree_construction_main ($) { Line 6005  sub _tree_construction_main ($) {
6005    
6006          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6007              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6008            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6009                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6010              !!!cp ('t325');              !!!cp ('t325');
6011              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6012              ## Ignore the token              ## Ignore the token
6013              !!!next-token;              !!!next-token;
6014            } else {            } else {
# Line 5245  sub _tree_construction_main ($) { Line 6018  sub _tree_construction_main ($) {
6018            }            }
6019    
6020            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6021                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6022              !!!cp ('t327');              !!!cp ('t327');
6023              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6024            } else {            } else {
6025              !!!cp ('t328');              !!!cp ('t328');
6026            }            }
6027            redo B;            next B;
6028          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6029                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6030            !!!cp ('t329');            !!!cp ('t329');
6031            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6032            !!!next-token;            !!!next-token;
6033            redo B;            next B;
6034          } else {          } else {
6035            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6036              !!!cp ('t330');              !!!cp ('t330');
6037              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6038            } else {            } else {
6039              !!!cp ('t331');              !!!cp ('t331');
6040              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6041            }            }
6042            ## Ignore the token            ## Ignore the token
6043            !!!next-token;            !!!next-token;
6044            redo B;            next B;
6045          }          }
6046          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6047            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6048                    @{$self->{open_elements}} == 1) { # redundant, maybe
6049              !!!cp ('t331.1');
6050              !!!parse-error (type => 'in body:#eof', token => $token);
6051            } else {
6052              !!!cp ('t331.2');
6053            }
6054            
6055            ## Stop parsing
6056            last B;
6057        } else {        } else {
6058          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6059        }        }
# Line 5284  sub _tree_construction_main ($) { Line 6068  sub _tree_construction_main ($) {
6068        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6069          !!!cp ('t332');          !!!cp ('t332');
6070          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6071          $script_start_tag->($insert);          $script_start_tag->();
6072          redo B;          next B;
6073        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6074          !!!cp ('t333');          !!!cp ('t333');
6075          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6076          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6077          redo B;          next B;
6078        } elsif ({        } elsif ({
6079                  base => 1, link => 1,                  base => 1, link => 1,
6080                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6081          !!!cp ('t334');          !!!cp ('t334');
6082          ## 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
6083          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6084          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6085            !!!ack ('t334.1');
6086          !!!next-token;          !!!next-token;
6087          redo B;          next B;
6088        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6089          ## 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
6090          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6091          my $meta_el = 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.
6092    
6093          unless ($self->{confident}) {          unless ($self->{confident}) {
6094            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
6095              !!!cp ('t335');              !!!cp ('t335');
6096              $self->{change_encoding}              $self->{change_encoding}
6097                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6098                            
6099              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6100                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
# Line 5324  sub _tree_construction_main ($) { Line 6109  sub _tree_construction_main ($) {
6109                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6110                !!!cp ('t336');                !!!cp ('t336');
6111                $self->{change_encoding}                $self->{change_encoding}
6112                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6113                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6114                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6115                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5348  sub _tree_construction_main ($) { Line 6133  sub _tree_construction_main ($) {
6133            }            }
6134          }          }
6135    
6136            !!!ack ('t338.1');
6137          !!!next-token;          !!!next-token;
6138          redo B;          next B;
6139        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6140          !!!cp ('t341');          !!!cp ('t341');
6141          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6142          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6143          redo B;          next B;
6144        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6145          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6146                                
6147          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6148              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6149            !!!cp ('t342');            !!!cp ('t342');
6150            ## Ignore the token            ## Ignore the token
6151          } else {          } else {
# Line 5373  sub _tree_construction_main ($) { Line 6159  sub _tree_construction_main ($) {
6159              }              }
6160            }            }
6161          }          }
6162            !!!nack ('t343.1');
6163          !!!next-token;          !!!next-token;
6164          redo B;          next B;
6165        } elsif ({        } elsif ({
6166                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6167                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6168                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6169                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6170                  pre => 1, listing => 1,                  pre => 1, listing => 1,
6171                    form => 1,
6172                    table => 1,
6173                    hr => 1,
6174                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6175            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6176              !!!cp ('t350');
6177              !!!parse-error (type => 'in form:form', token => $token);
6178              ## Ignore the token
6179              !!!nack ('t350.1');
6180              !!!next-token;
6181              next B;
6182            }
6183    
6184          ## has a p element in scope          ## has a p element in scope
6185          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6186            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6187              !!!cp ('t344');              !!!cp ('t344');
6188              !!!back-token;              !!!back-token; # <form>
6189              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6190              redo B;                        line => $token->{line}, column => $token->{column}};
6191            } elsif ({              next B;
6192                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6193              !!!cp ('t345');              !!!cp ('t345');
6194              last INSCOPE;              last INSCOPE;
6195            }            }
6196          } # INSCOPE          } # INSCOPE
6197                        
6198          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6199          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6200              !!!nack ('t346.1');
6201            !!!next-token;            !!!next-token;
6202            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6203              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5412  sub _tree_construction_main ($) { Line 6210  sub _tree_construction_main ($) {
6210            } else {            } else {
6211              !!!cp ('t348');              !!!cp ('t348');
6212            }            }
6213          } else {          } elsif ($token->{tag_name} eq 'form') {
6214            !!!cp ('t347');            !!!cp ('t347.1');
6215              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6216    
6217              !!!nack ('t347.2');
6218            !!!next-token;            !!!next-token;
6219          }          } elsif ($token->{tag_name} eq 'table') {
6220          redo B;            !!!cp ('t382');
6221        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6222          if (defined $self->{form_element}) {            
6223            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6224            !!!parse-error (type => 'in form:form');  
6225            ## Ignore the token            !!!nack ('t382.1');
6226              !!!next-token;
6227            } elsif ($token->{tag_name} eq 'hr') {
6228              !!!cp ('t386');
6229              pop @{$self->{open_elements}};
6230            
6231              !!!nack ('t386.1');
6232            !!!next-token;            !!!next-token;
           redo B;  
6233          } else {          } else {
6234            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!cp ('t351');  
               !!!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]}) {  
               !!!cp ('t352');  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6235            !!!next-token;            !!!next-token;
           redo B;  
6236          }          }
6237        } elsif ($token->{tag_name} eq 'li') {          next B;
6238          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6239          ## has a p element in scope          ## has a p element in scope
6240          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6241            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6242              !!!cp ('t353');              !!!cp ('t353');
6243              !!!back-token;              !!!back-token; # <x>
6244              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6245              redo B;                        line => $token->{line}, column => $token->{column}};
6246            } elsif ({              next B;
6247                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6248              !!!cp ('t354');              !!!cp ('t354');
6249              last INSCOPE;              last INSCOPE;
6250            }            }
# Line 5466  sub _tree_construction_main ($) { Line 6253  sub _tree_construction_main ($) {
6253          ## Step 1          ## Step 1
6254          my $i = -1;          my $i = -1;
6255          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6256            my $li_or_dtdd = {li => {li => 1},
6257                              dt => {dt => 1, dd => 1},
6258                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6259          LI: {          LI: {
6260            ## Step 2            ## Step 2
6261            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6262              if ($i != -1) {              if ($i != -1) {
6263                !!!cp ('t355');                !!!cp ('t355');
6264                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6265                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
6266                                      ->manakai_local_name,
6267                                  token => $token);
6268              } else {              } else {
6269                !!!cp ('t356');                !!!cp ('t356');
6270              }              }
# Line 5483  sub _tree_construction_main ($) { Line 6275  sub _tree_construction_main ($) {
6275            }            }
6276                        
6277            ## Step 3            ## Step 3
6278            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6279                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6280                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6281                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6282                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6283                  not ($node->[1] & DIV_EL)) {
6284              !!!cp ('t358');              !!!cp ('t358');
6285              last LI;              last LI;
6286            }            }
# Line 5499  sub _tree_construction_main ($) { Line 6292  sub _tree_construction_main ($) {
6292            redo LI;            redo LI;
6293          } # LI          } # LI
6294                        
6295          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6296            !!!nack ('t359.1');
6297          !!!next-token;          !!!next-token;
6298          redo B;          next 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') {  
             !!!cp ('t360');  
             !!!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]}) {  
             !!!cp ('t361');  
             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) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## 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') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
6299        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6300          ## has a p element in scope          ## has a p element in scope
6301          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6302            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6303              !!!cp ('t367');              !!!cp ('t367');
6304              !!!back-token;              !!!back-token; # <plaintext>
6305              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6306              redo B;                        line => $token->{line}, column => $token->{column}};
6307            } elsif ({              next B;
6308                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6309              !!!cp ('t368');              !!!cp ('t368');
6310              last INSCOPE;              last INSCOPE;
6311            }            }
6312          } # INSCOPE          } # INSCOPE
6313                        
6314          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6315                        
6316          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6317                        
6318            !!!nack ('t368.1');
6319          !!!next-token;          !!!next-token;
6320          redo B;          next B;
6321        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6322          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6323            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6324            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6325              !!!cp ('t371');              !!!cp ('t371');
6326              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6327                            
6328              !!!back-token;              !!!back-token; # <a>
6329              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6330              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6331                $formatting_end_tag->($token);
6332                            
6333              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6334                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5615  sub _tree_construction_main ($) { Line 6353  sub _tree_construction_main ($) {
6353                        
6354          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6355    
6356          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6357          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6358    
6359            !!!nack ('t374.1');
6360          !!!next-token;          !!!next-token;
6361          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}}) {  
         !!!cp ('t375');  
         $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;  
6362        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6363          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6364    
6365          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6366          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6367            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6368            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6369              !!!cp ('t376');              !!!cp ('t376');
6370              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6371              !!!back-token;              !!!back-token; # <nobr>
6372              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6373              redo B;                        line => $token->{line}, column => $token->{column}};
6374            } elsif ({              next B;
6375                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6376              !!!cp ('t377');              !!!cp ('t377');
6377              last INSCOPE;              last INSCOPE;
6378            }            }
6379          } # INSCOPE          } # INSCOPE
6380                    
6381          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6382          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6383                    
6384            !!!nack ('t377.1');
6385          !!!next-token;          !!!next-token;
6386          redo B;          next B;
6387        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6388          ## has a button element in scope          ## has a button element in scope
6389          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6390            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6391            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6392              !!!cp ('t378');              !!!cp ('t378');
6393              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6394              !!!back-token;              !!!back-token; # <button>
6395              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6396              redo B;                        line => $token->{line}, column => $token->{column}};
6397            } elsif ({              next B;
6398                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6399              !!!cp ('t379');              !!!cp ('t379');
6400              last INSCOPE;              last INSCOPE;
6401            }            }
# Line 5680  sub _tree_construction_main ($) { Line 6403  sub _tree_construction_main ($) {
6403                        
6404          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6405                        
6406          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6407    
6408          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
6409    
6410          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6411    
6412            !!!nack ('t379.1');
6413          !!!next-token;          !!!next-token;
6414          redo B;          next B;
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!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]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];  
   
         $self->{insertion_mode} = IN_TABLE_IM;  
             
         !!!next-token;  
         redo B;  
6415        } elsif ({        } elsif ({
6416                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6417                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6418                  image => 1,                  noembed => 1,
6419                    noframes => 1,
6420                    noscript => 0, ## TODO: 1 if scripting is enabled
6421                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6422          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6423            !!!cp ('t384');            !!!cp ('t381');
6424            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6425          } else {          } else {
6426            !!!cp ('t385');            !!!cp ('t399');
6427          }          }
6428            ## NOTE: There is an "as if in body" code clone.
6429          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6430          $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') {  
             !!!cp ('t386');  
             !!!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]}) {  
             !!!cp ('t387');  
             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') {  
         !!!cp ('t388');  
         $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;  
6431        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6432          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6433                    
6434          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6435            !!!cp ('t389');            !!!cp ('t389');
6436            ## Ignore the token            ## Ignore the token
6437              !!!nack ('t389'); ## NOTE: Not acknowledged.
6438            !!!next-token;            !!!next-token;
6439            redo B;            next B;
6440          } else {          } else {
6441            my $at = $token->{attributes};            my $at = $token->{attributes};
6442            my $form_attrs;            my $form_attrs;
# Line 5798  sub _tree_construction_main ($) { Line 6447  sub _tree_construction_main ($) {
6447            delete $at->{prompt};            delete $at->{prompt};
6448            my @tokens = (            my @tokens = (
6449                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6450                           attributes => $form_attrs},                           attributes => $form_attrs,
6451                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6452                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6453                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6454                            {type => START_TAG_TOKEN, tag_name => 'p',
6455                             line => $token->{line}, column => $token->{column}},
6456                            {type => START_TAG_TOKEN, tag_name => 'label',
6457                             line => $token->{line}, column => $token->{column}},
6458                         );                         );
6459            if ($prompt_attr) {            if ($prompt_attr) {
6460              !!!cp ('t390');              !!!cp ('t390');
6461              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6462                               #line => $token->{line}, column => $token->{column},
6463                              };
6464            } else {            } else {
6465              !!!cp ('t391');              !!!cp ('t391');
6466              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6467                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6468                               #line => $token->{line}, column => $token->{column},
6469                              }; # SHOULD
6470              ## TODO: make this configurable              ## TODO: make this configurable
6471            }            }
6472            push @tokens,            push @tokens,
6473                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6474                             line => $token->{line}, column => $token->{column}},
6475                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6476                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6477                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6478                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6479                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6480            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6481                             line => $token->{line}, column => $token->{column}},
6482                            {type => END_TAG_TOKEN, tag_name => 'form',
6483                             line => $token->{line}, column => $token->{column}};
6484              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6485            !!!back-token (@tokens);            !!!back-token (@tokens);
6486            redo B;            !!!next-token;
6487              next B;
6488          }          }
6489        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6490          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6491          my $el;          my $el;
6492          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6493                    
6494          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6495          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5835  sub _tree_construction_main ($) { Line 6498  sub _tree_construction_main ($) {
6498          $insert->($el);          $insert->($el);
6499                    
6500          my $text = '';          my $text = '';
6501            !!!nack ('t392.1');
6502          !!!next-token;          !!!next-token;
6503          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6504            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5865  sub _tree_construction_main ($) { Line 6529  sub _tree_construction_main ($) {
6529            ## Ignore the token            ## Ignore the token
6530          } else {          } else {
6531            !!!cp ('t398');            !!!cp ('t398');
6532            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6533          }          }
6534          !!!next-token;          !!!next-token;
6535          redo B;          next B;
6536        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6537                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
6538          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6539    
6540          ## TODO: associate with $self->{form_element} if defined          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6541    
6542            ## "adjust foreign attributes" - done in insert-element-f
6543            
6544            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6545                    
6546          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6547              pop @{$self->{open_elements}};
6548              !!!ack ('t398.1');
6549            } else {
6550              !!!cp ('t398.2');
6551              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6552              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6553              ## mode, "in body" (not "in foreign content") secondary insertion
6554              ## mode, maybe.
6555            }
6556    
6557          !!!next-token;          !!!next-token;
6558          redo B;          next B;
6559        } elsif ({        } elsif ({
6560                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6561                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5897  sub _tree_construction_main ($) { Line 6563  sub _tree_construction_main ($) {
6563                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6564                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6565          !!!cp ('t401');          !!!cp ('t401');
6566          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6567          ## Ignore the token          ## Ignore the token
6568            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6569          !!!next-token;          !!!next-token;
6570          redo B;          next B;
6571                    
6572          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6573        } else {        } else {
6574          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6575              !!!cp ('t384');
6576              !!!parse-error (type => 'image', token => $token);
6577              $token->{tag_name} = 'img';
6578            } else {
6579              !!!cp ('t385');
6580            }
6581    
6582            ## NOTE: There is an "as if <br>" code clone.
6583          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6584                    
6585          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6586    
6587            if ({
6588                 applet => 1, marquee => 1, object => 1,
6589                }->{$token->{tag_name}}) {
6590              !!!cp ('t380');
6591              push @$active_formatting_elements, ['#marker', ''];
6592              !!!nack ('t380.1');
6593            } elsif ({
6594                      b => 1, big => 1, em => 1, font => 1, i => 1,
6595                      s => 1, small => 1, strile => 1,
6596                      strong => 1, tt => 1, u => 1,
6597                     }->{$token->{tag_name}}) {
6598              !!!cp ('t375');
6599              push @$active_formatting_elements, $self->{open_elements}->[-1];
6600              !!!nack ('t375.1');
6601            } elsif ($token->{tag_name} eq 'input') {
6602              !!!cp ('t388');
6603              ## TODO: associate with $self->{form_element} if defined
6604              pop @{$self->{open_elements}};
6605              !!!ack ('t388.2');
6606            } elsif ({
6607                      area => 1, basefont => 1, bgsound => 1, br => 1,
6608                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6609                      #image => 1,
6610                     }->{$token->{tag_name}}) {
6611              !!!cp ('t388.1');
6612              pop @{$self->{open_elements}};
6613              !!!ack ('t388.3');
6614            } elsif ($token->{tag_name} eq 'select') {
6615              ## TODO: associate with $self->{form_element} if defined
6616            
6617              if ($self->{insertion_mode} & TABLE_IMS or
6618                  $self->{insertion_mode} & BODY_TABLE_IMS or
6619                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6620                !!!cp ('t400.1');
6621                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6622              } else {
6623                !!!cp ('t400.2');
6624                $self->{insertion_mode} = IN_SELECT_IM;
6625              }
6626              !!!nack ('t400.3');
6627            } else {
6628              !!!nack ('t402');
6629            }
6630                    
6631          !!!next-token;          !!!next-token;
6632          redo B;          next B;
6633        }        }
6634      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6635        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6636          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6637              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6638            for (@{$self->{open_elements}}) {          INSCOPE: {
6639              unless ({            for (reverse @{$self->{open_elements}}) {
6640                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6641                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6642                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6643                      }->{$_->[1]}) {                last INSCOPE;
6644                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
6645                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
6646              } else {                last;
               !!!cp ('t404');  
6647              }              }
6648            }            }
6649    
6650            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6651            !!!next-token;                            value => $token->{tag_name}, token => $token);
6652            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6653            !!!next-token;            !!!next-token;
6654            redo B;            next B;
6655            } # INSCOPE
6656    
6657            for (@{$self->{open_elements}}) {
6658              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6659                !!!cp ('t403');
6660                !!!parse-error (type => 'not closed',
6661                                value => $_->[0]->manakai_local_name,
6662                                token => $token);
6663                last;
6664              } else {
6665                !!!cp ('t404');
6666              }
6667          }          }
6668    
6669            $self->{insertion_mode} = AFTER_BODY_IM;
6670            !!!next-token;
6671            next B;
6672        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6673          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
6674            ## up-to-date, though it has same effect as speced.
6675            if (@{$self->{open_elements}} > 1 and
6676                $self->{open_elements}->[1]->[1] & BODY_EL) {
6677            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6678            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6679              !!!cp ('t406');              !!!cp ('t406');
6680              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6681                                value => $self->{open_elements}->[1]->[0]
6682                                    ->manakai_local_name,
6683                                token => $token);
6684            } else {            } else {
6685              !!!cp ('t407');              !!!cp ('t407');
6686            }            }
6687            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6688            ## reprocess            ## reprocess
6689            redo B;            next B;
6690          } else {          } else {
6691            !!!cp ('t408');            !!!cp ('t408');
6692            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6693            ## Ignore the token            ## Ignore the token
6694            !!!next-token;            !!!next-token;
6695            redo B;            next B;
6696          }          }
6697        } elsif ({        } elsif ({
6698                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6699                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6700                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
6701                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6702                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6703                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6704          ## has an element in scope          ## has an element in scope
6705          my $i;          my $i;
6706          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6707            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6708            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6709              !!!cp ('t410');              !!!cp ('t410');
6710              $i = $_;              $i = $_;
6711              last INSCOPE;              last INSCOPE;
6712            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6713              !!!cp ('t411');              !!!cp ('t411');
6714              last INSCOPE;              last INSCOPE;
6715            }            }
# Line 5984  sub _tree_construction_main ($) { Line 6717  sub _tree_construction_main ($) {
6717    
6718          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6719            !!!cp ('t413');            !!!cp ('t413');
6720            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6721          } else {          } else {
6722            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6723            while ({            while ({
# Line 5992  sub _tree_construction_main ($) { Line 6725  sub _tree_construction_main ($) {
6725                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
6726                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
6727                    p => 1,                    p => 1,
6728                   }->{$self->{open_elements}->[-1]->[1]}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6729              !!!cp ('t409');              !!!cp ('t409');
6730              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6731            }            }
6732    
6733            ## Step 2.            ## Step 2.
6734            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6735                      ne $token->{tag_name}) {
6736              !!!cp ('t412');              !!!cp ('t412');
6737              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6738                                value => $self->{open_elements}->[-1]->[0]
6739                                    ->manakai_local_name,
6740                                token => $token);
6741            } else {            } else {
6742              !!!cp ('t414');              !!!cp ('t414');
6743            }            }
# Line 6011  sub _tree_construction_main ($) { Line 6748  sub _tree_construction_main ($) {
6748            ## Step 4.            ## Step 4.
6749            $clear_up_to_marker->()            $clear_up_to_marker->()
6750                if {                if {
6751                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6752                }->{$token->{tag_name}};                }->{$token->{tag_name}};
6753          }          }
6754          !!!next-token;          !!!next-token;
6755          redo B;          next B;
6756        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6757          undef $self->{form_element};          undef $self->{form_element};
6758    
# Line 6023  sub _tree_construction_main ($) { Line 6760  sub _tree_construction_main ($) {
6760          my $i;          my $i;
6761          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6762            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6763            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6764              !!!cp ('t418');              !!!cp ('t418');
6765              $i = $_;              $i = $_;
6766              last INSCOPE;              last INSCOPE;
6767            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6768              !!!cp ('t419');              !!!cp ('t419');
6769              last INSCOPE;              last INSCOPE;
6770            }            }
# Line 6038  sub _tree_construction_main ($) { Line 6772  sub _tree_construction_main ($) {
6772    
6773          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6774            !!!cp ('t421');            !!!cp ('t421');
6775            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6776          } else {          } else {
6777            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6778            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
6779              !!!cp ('t417');              !!!cp ('t417');
6780              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6781            }            }
6782                        
6783            ## Step 2.            ## Step 2.
6784            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6785                      ne $token->{tag_name}) {
6786              !!!cp ('t417.1');              !!!cp ('t417.1');
6787              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6788                                value => $self->{open_elements}->[-1]->[0]
6789                                    ->manakai_local_name,
6790                                token => $token);
6791            } else {            } else {
6792              !!!cp ('t420');              !!!cp ('t420');
6793            }              }  
# Line 6061  sub _tree_construction_main ($) { Line 6797  sub _tree_construction_main ($) {
6797          }          }
6798    
6799          !!!next-token;          !!!next-token;
6800          redo B;          next B;
6801        } elsif ({        } elsif ({
6802                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6803                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6069  sub _tree_construction_main ($) { Line 6805  sub _tree_construction_main ($) {
6805          my $i;          my $i;
6806          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6807            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6808            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
6809              !!!cp ('t423');              !!!cp ('t423');
6810              $i = $_;              $i = $_;
6811              last INSCOPE;              last INSCOPE;
6812            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6813              !!!cp ('t424');              !!!cp ('t424');
6814              last INSCOPE;              last INSCOPE;
6815            }            }
# Line 6086  sub _tree_construction_main ($) { Line 6817  sub _tree_construction_main ($) {
6817    
6818          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6819            !!!cp ('t425.1');            !!!cp ('t425.1');
6820            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6821          } else {          } else {
6822            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6823            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
6824              !!!cp ('t422');              !!!cp ('t422');
6825              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6826            }            }
6827                        
6828            ## Step 2.            ## Step 2.
6829            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6830                      ne $token->{tag_name}) {
6831              !!!cp ('t425');              !!!cp ('t425');
6832              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6833            } else {            } else {
6834              !!!cp ('t426');              !!!cp ('t426');
6835            }            }
# Line 6109  sub _tree_construction_main ($) { Line 6839  sub _tree_construction_main ($) {
6839          }          }
6840                    
6841          !!!next-token;          !!!next-token;
6842          redo B;          next B;
6843        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
6844          ## has an element in scope          ## has an element in scope
6845          my $i;          my $i;
6846          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6847            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6848            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
6849              !!!cp ('t410.1');              !!!cp ('t410.1');
6850              $i = $_;              $i = $_;
6851              last INSCOPE;              last INSCOPE;
6852            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6853              !!!cp ('t411.1');              !!!cp ('t411.1');
6854              last INSCOPE;              last INSCOPE;
6855            }            }
6856          } # INSCOPE          } # INSCOPE
6857    
6858          if (defined $i) {          if (defined $i) {
6859            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6860                      ne $token->{tag_name}) {
6861              !!!cp ('t412.1');              !!!cp ('t412.1');
6862              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6863                                value => $self->{open_elements}->[-1]->[0]
6864                                    ->manakai_local_name,
6865                                token => $token);
6866            } else {            } else {
6867              !!!cp ('t414.1');              !!!cp ('t414.1');
6868            }            }
# Line 6139  sub _tree_construction_main ($) { Line 6870  sub _tree_construction_main ($) {
6870            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6871          } else {          } else {
6872            !!!cp ('t413.1');            !!!cp ('t413.1');
6873            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6874    
6875            !!!cp ('t415.1');            !!!cp ('t415.1');
6876            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
6877            my $el;            my $el;
6878            !!!create-element ($el, 'p');            !!!create-element ($el, $HTML_NS, 'p',, $token);
6879            $insert->($el);            $insert->($el);
6880            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
6881          }          }
6882    
6883          !!!next-token;          !!!next-token;
6884          redo B;          next B;
6885        } elsif ({        } elsif ({
6886                  a => 1,                  a => 1,
6887                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6158  sub _tree_construction_main ($) { Line 6889  sub _tree_construction_main ($) {
6889                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6890                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6891          !!!cp ('t427');          !!!cp ('t427');
6892          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
6893          redo B;          next B;
6894        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6895          !!!cp ('t428');          !!!cp ('t428');
6896          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
6897    
6898          ## As if <br>          ## As if <br>
6899          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6900                    
6901          my $el;          my $el;
6902          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
6903          $insert->($el);          $insert->($el);
6904                    
6905          ## Ignore the token.          ## Ignore the token.
6906          !!!next-token;          !!!next-token;
6907          redo B;          next B;
6908        } elsif ({        } elsif ({
6909                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6910                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6187  sub _tree_construction_main ($) { Line 6918  sub _tree_construction_main ($) {
6918                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6919                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6920          !!!cp ('t429');          !!!cp ('t429');
6921          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6922          ## Ignore the token          ## Ignore the token
6923          !!!next-token;          !!!next-token;
6924          redo B;          next B;
6925                    
6926          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
6927                    
# Line 6201  sub _tree_construction_main ($) { Line 6932  sub _tree_construction_main ($) {
6932    
6933          ## Step 2          ## Step 2
6934          S2: {          S2: {
6935            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6936              ## Step 1              ## Step 1
6937              ## generate implied end tags              ## generate implied end tags
6938              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
6939                !!!cp ('t430');                !!!cp ('t430');
6940                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
6941                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6942              }              }
6943                    
6944              ## Step 2              ## Step 2
6945              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6946                        ne $token->{tag_name}) {
6947                !!!cp ('t431');                !!!cp ('t431');
6948                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6949                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6950                                  value => $self->{open_elements}->[-1]->[0]
6951                                      ->manakai_local_name,
6952                                  token => $token);
6953              } else {              } else {
6954                !!!cp ('t432');                !!!cp ('t432');
6955              }              }
# Line 6228  sub _tree_construction_main ($) { Line 6961  sub _tree_construction_main ($) {
6961              last S2;              last S2;
6962            } else {            } else {
6963              ## Step 3              ## Step 3
6964              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
6965                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6966                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
6967                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
6968                !!!cp ('t433');                !!!cp ('t433');
6969                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6970                ## Ignore the token                ## Ignore the token
6971                !!!next-token;                !!!next-token;
6972                last S2;                last S2;
# Line 6249  sub _tree_construction_main ($) { Line 6982  sub _tree_construction_main ($) {
6982            ## Step 5;            ## Step 5;
6983            redo S2;            redo S2;
6984          } # S2          } # S2
6985          redo B;          next B;
6986        }        }
6987      }      }
6988      redo B;      next B;
6989      } continue { # B
6990        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
6991          ## NOTE: The code below is executed in cases where it does not have
6992          ## to be, but it it is harmless even in those cases.
6993          ## has an element in scope
6994          INSCOPE: {
6995            for (reverse 0..$#{$self->{open_elements}}) {
6996              my $node = $self->{open_elements}->[$_];
6997              if ($node->[1] & FOREIGN_EL) {
6998                last INSCOPE;
6999              } elsif ($node->[1] & SCOPING_EL) {
7000                last;
7001              }
7002            }
7003            
7004            ## NOTE: No foreign element in scope.
7005            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7006          } # INSCOPE
7007        }
7008    } # B    } # B
7009    
7010    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6298  sub set_inner_html ($$$) { Line 7050  sub set_inner_html ($$$) {
7050    
7051      ## Step 8 # MUST      ## Step 8 # MUST
7052      my $i = 0;      my $i = 0;
7053      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7054      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7055      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7056        my $self = shift;        my $self = shift;
7057    
# Line 6308  sub set_inner_html ($$$) { Line 7060  sub set_inner_html ($$$) {
7060    
7061        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7062        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7063        $column++;  
7064          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7065          $p->{column}++;
7066    
7067        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7068          $line++;          $p->{line}++;
7069          $column = 0;          $p->{column} = 0;
7070          !!!cp ('i1');          !!!cp ('i1');
7071        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7072          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7073          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7074          $line++;          $p->{line}++;
7075          $column = 0;          $p->{column} = 0;
7076          !!!cp ('i2');          !!!cp ('i2');
7077        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7078          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6327  sub set_inner_html ($$$) { Line 7081  sub set_inner_html ($$$) {
7081          !!!cp ('i4');          !!!cp ('i4');
7082          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7083          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7084          } elsif ($self->{next_char} <= 0x0008 or
7085                   (0x000E <= $self->{next_char} and
7086                    $self->{next_char} <= 0x001F) or
7087                   (0x007F <= $self->{next_char} and
7088                    $self->{next_char} <= 0x009F) or
7089                   (0xD800 <= $self->{next_char} and
7090                    $self->{next_char} <= 0xDFFF) or
7091                   (0xFDD0 <= $self->{next_char} and
7092                    $self->{next_char} <= 0xFDDF) or
7093                   {
7094                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7095                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7096                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7097                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7098                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7099                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7100                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7101                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7102                    0x10FFFE => 1, 0x10FFFF => 1,
7103                   }->{$self->{next_char}}) {
7104            !!!cp ('i4.1');
7105            !!!parse-error (type => 'control char', level => $self->{must_level});
7106    ## TODO: error type documentation
7107        }        }
7108      };      };
7109      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6334  sub set_inner_html ($$$) { Line 7111  sub set_inner_html ($$$) {
7111            
7112      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7113        my (%opt) = @_;        my (%opt) = @_;
7114        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7115          my $column = $opt{column};
7116          if (defined $opt{token} and defined $opt{token}->{line}) {
7117            $line = $opt{token}->{line};
7118            $column = $opt{token}->{column};
7119          }
7120          warn "Parse error ($opt{type}) at line $line column $column\n";
7121      };      };
7122      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7123        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7124      };      };
7125            
7126      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6361  sub set_inner_html ($$$) { Line 7144  sub set_inner_html ($$$) {
7144          unless defined $p->{content_model};          unless defined $p->{content_model};
7145          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7146    
7147      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7148          ## TODO: Foreign element OK?
7149    
7150      ## Step 3      ## Step 3
7151      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6371  sub set_inner_html ($$$) { Line 7155  sub set_inner_html ($$$) {
7155      $doc->append_child ($root);      $doc->append_child ($root);
7156    
7157      ## Step 5 # MUST      ## Step 5 # MUST
7158      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7159    
7160      undef $p->{head_element};      undef $p->{head_element};
7161    
# Line 6417  sub set_inner_html ($$$) { Line 7201  sub set_inner_html ($$$) {
7201      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7202    
7203      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7204    
7205        delete $p->{parse_error}; # delete loop
7206    } else {    } else {
7207      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";
7208    }    }

Legend:
Removed from v.1.97  
changed lines
  Added in v.1.132

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24